- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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
G::Process::Umask::Umask
Umask(Mode)
Definition: gprocess_unix.cpp:270
G::Process::Umask::Tightest
Definition: gprocess.h:66
gstrings.h
G::SignalSafe
An empty structure that is used to indicate a signal-safe, reentrant implementation.
Definition: gsignalsafe.h:35
gidentity.h
G::Process::Id::str
std::string str() const
Definition: gprocess_unix.cpp:236
G::Process::closeStderr
static void closeStderr()
Closes stderr.
Definition: gprocess_unix.cpp:63
G::Identity
A very low-level interface to getpwnam() and the get/set/e/uid/gid functions.
Definition: gidentity.h:41
G::Process::Id::Id
Id()
Definition: gprocess_unix.cpp:204
G::Process::Umask
Used to temporarily modify the process umask.
Definition: gprocess.h:64
G::Process::Umask::Readable
Definition: gprocess.h:66
G::Process::errno_
static int errno_()
Returns the process's current 'errno' value.
Definition: gprocess_unix.cpp:107
G::Process::beOrdinary
static Identity beOrdinary(Identity nobody, bool change_group=true)
Revokes special privileges (root or suid).
Definition: gprocess_unix.cpp:154
G::Process::Umask::~Umask
~Umask()
Definition: gprocess_unix.cpp:276
G::Process::beSpecial
static Identity beSpecial(Identity special, bool change_group=true)
Re-acquires special privileges (either root or suid).
Definition: gprocess_unix.cpp:134
G
Low-level classes.
G::Process::Id::operator==
bool operator==(const Id &) const
Definition: gprocess_unix.cpp:243
G::Process::NoThrow
An overload discriminator for Process.
Definition: gprocess.h:76
G::Process::revokeExtraGroups
static void revokeExtraGroups()
Revokes secondary group memberships if really root or if suid.
Definition: gprocess_unix.cpp:125
G::IdentityUser
A convenience class which, when used as a private base, can improve readability when calling Identity...
Definition: gidentity.h:109
G::Process::Umask::GroupOpen
Definition: gprocess.h:66
G::Process::Umask::Mode
Mode
Definition: gprocess.h:66
gexception.h
G_EXCEPTION
#define G_EXCEPTION(class_name, description)
define as a function rather than a type if optimising for size
Definition: gexception.h:93
G::Process::Id
Process-id class.
Definition: gprocess.h:52
gpath.h
G::Process::cd
static void cd(const Path &dir)
Changes directory.
Definition: gprocess_unix.cpp:52
G::operator<<
std::ostream & operator<<(std::ostream &stream, const G::Identity &identity)
Definition: gidentity.h:138
G::Process::closeFiles
static void closeFiles(bool keep_stderr=false)
Closes all open file descriptors.
Definition: gprocess_unix.cpp:70
G::Process::beNobody
static void beNobody(Identity)
If currently running with a real identity of root then the real identity is set to the nobody identit...
Definition: gprocess_unix.cpp:192
G::Process::IdImp
A private implementation class used by G::Process.
Definition: gprocess_unix.cpp:44
G::Process::Umask::Tighter
Definition: gprocess.h:66
G::Process
A static interface for doing things with processes.
Definition: gprocess.h:44
G::operator>>
std::istream & operator>>(std::istream &stream, G::Process::Id &id)
Definition: gprocess.h:158
G::Path
A Path object represents a file system path.
Definition: gpath.h:44
G::Process::strerror
static std::string strerror(int errno_)
Translates an 'errno' value into a meaningful diagnostic string.
Definition: gprocess_unix.cpp:119
G::NewProcess
A static interface for creating new processes.
Definition: gnewprocess.h:42
G::Process::Umask::set
static void set(Mode)
Definition: gprocess_unix.cpp:282