gprocess.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2001-2013 Graeme Walker <graeme_walker@users.sourceforge.net>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 // ===
20 
21 #ifndef G_PROCESS_H
22 #define G_PROCESS_H
23 
24 #include "gdef.h"
25 #include "gexception.h"
26 #include "gidentity.h"
27 #include "gpath.h"
28 #include "gstrings.h"
29 #include <iostream>
30 #include <sys/types.h>
31 #include <string>
32 
34 namespace G
35 {
36  class Process ;
37  class NewProcess ;
38 }
39 
44 class G::Process : private G::IdentityUser
45 {
46 public:
47  G_EXCEPTION( CannotChangeDirectory , "cannot cd()" ) ;
48  G_EXCEPTION( InvalidId , "invalid process-id string" ) ;
49 
50  class IdImp ;
52  class Id
53  {
54  public: Id() ;
55  public: explicit Id( std::istream & ) ;
56  public: Id( SignalSafe , const char * pid_file_path ) ; // (ctor for signal-handler)
57  public: std::string str() const ;
58  public: bool operator==( const Id & ) const ;
59  private: pid_t m_pid ;
60  friend class NewProcess ;
61  friend class Process ;
62  } ;
64  class Umask
65  {
66  public: enum Mode { Readable , Tighter , Tightest , GroupOpen } ;
67  public: explicit Umask( Mode ) ;
68  public: ~Umask() ;
69  public: static void set( Mode ) ;
70  private: Umask( const Umask & ) ; // not implemented
71  private: void operator=( const Umask & ) ; // not implemented
72  private: class UmaskImp ;
73  private: UmaskImp * m_imp ;
74  } ;
76  class NoThrow
77  {} ;
78 
79  static void closeFiles( bool keep_stderr = false ) ;
81 
82  static void closeFiles( int fd ) ;
84 
85  static void closeStderr() ;
87 
88  static void cd( const Path & dir ) ;
90 
91  static bool cd( const Path & dir , NoThrow ) ;
93 
94  static int errno_() ;
96 
97  static int errno_( int ) ;
100 
101  static std::string strerror( int errno_ ) ;
103 
104  static void revokeExtraGroups() ;
107 
108  static Identity beOrdinary( Identity nobody , bool change_group = true ) ;
120 
121  static Identity beSpecial( Identity special , bool change_group = true ) ;
128 
129  static Identity beOrdinary( SignalSafe , Identity nobody , bool change_group = true ) ;
131 
132  static Identity beSpecial( SignalSafe , Identity special , bool change_group = true ) ;
134 
135  static void beNobody( Identity ) ;
143 
144 private:
145  Process() ;
146 } ;
147 
149 namespace G
150 {
151  inline
152  std::ostream & operator<<( std::ostream & stream , const G::Process::Id & id )
153  {
154  return stream << id.str() ;
155  }
156 
157  inline
158  std::istream & operator>>( std::istream & stream , G::Process::Id & id )
159  {
160  id = G::Process::Id( stream ) ;
161  return stream ;
162  }
163 }
164 
165 #endif
166 
An empty structure that is used to indicate a signal-safe, reentrant implementation.
Definition: gsignalsafe.h:35
std::string str() const
static void closeStderr()
Closes stderr.
A very low-level interface to getpwnam() and the get/set/e/uid/gid functions.
Definition: gidentity.h:41
Used to temporarily modify the process umask.
Definition: gprocess.h:64
static int errno_()
Returns the process's current 'errno' value.
static Identity beOrdinary(Identity nobody, bool change_group=true)
Revokes special privileges (root or suid).
static Identity beSpecial(Identity special, bool change_group=true)
Re-acquires special privileges (either root or suid).
Low-level classes.
bool operator==(const Id &) const
An overload discriminator for Process.
Definition: gprocess.h:76
static void revokeExtraGroups()
Revokes secondary group memberships if really root or if suid.
A convenience class which, when used as a private base, can improve readability when calling Identity...
Definition: gidentity.h:109
#define G_EXCEPTION(class_name, description)
define as a function rather than a type if optimising for size
Definition: gexception.h:93
Process-id class.
Definition: gprocess.h:52
static void cd(const Path &dir)
Changes directory.
std::ostream & operator<<(std::ostream &stream, const G::Identity &identity)
Definition: gidentity.h:138
static void closeFiles(bool keep_stderr=false)
Closes all open file descriptors.
static void beNobody(Identity)
If currently running with a real identity of root then the real identity is set to the nobody identit...
A private implementation class used by G::Process.
A static interface for doing things with processes.
Definition: gprocess.h:44
std::istream & operator>>(std::istream &stream, G::Process::Id &id)
Definition: gprocess.h:158
A Path object represents a file system path.
Definition: gpath.h:44
static std::string strerror(int errno_)
Translates an 'errno' value into a meaningful diagnostic string.
A static interface for creating new processes.
Definition: gnewprocess.h:42
static void set(Mode)