E-MailRelay

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

gprocess.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2001-2018 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 // ===
17 ///
18 /// \file gprocess.h
19 ///
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 "gsignalsafe.h"
30 #include <iostream>
31 #include <sys/types.h>
32 #include <string>
33
34 namespace G
35 {
36 class Process ;
37 class NewProcess ;
38 }
39
40 /// \class G::Process
41 /// A static interface for doing things with processes.
42 /// \see G::Identity
43 ///
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 ;
51 class Id /// Process-id class.
52 {
53 public: Id() ;
54 public: explicit Id( std::istream & ) ;
55 public: Id( SignalSafe , const char * pid_file_path ) ; // (ctor for signal-handler)
56 public: std::string str() const ;
57 public: bool operator==( const Id & ) const ;
58 private: pid_t m_pid ;
59 friend class NewProcess ;
60 friend class Process ;
61 } ;
62 class Umask /// Used to temporarily modify the process umask.
63 {
64 public: enum Mode { Readable , Tighter , Tightest , GroupOpen } ;
65 public: explicit Umask( Mode ) ;
66 public: ~Umask() ;
67 public: static void set( Mode ) ;
68 public: static void tighten() ; // no "other" access, user and group unchanged
69 private: Umask( const Umask & ) ; // not implemented
70 private: void operator=( const Umask & ) ; // not implemented
71 private: class UmaskImp ;
72 private: UmaskImp * m_imp ;
73 } ;
74 class NoThrow /// An overload discriminator for Process.
75 {} ;
76
77 static void closeFiles( bool keep_stderr = false ) ;
78 ///< Closes all open file descriptors, but optionally not stderr.
79 ///< Stdin, stdout, and possibly stderr are reopened to the
80 ///< null device.
81
82 static void closeFilesExcept( int fd_1 , int fd_2 = -1 ) ;
83 ///< Closes all open file descriptors except the given ones.
84 ///< If stdin, stdout, or stderr are closed then they are
85 ///< reopened to the null device.
86
87 static void closeStderr() ;
88 ///< Closes stderr and reopens it to the null device.
89
90 static void cd( const Path & dir ) ;
91 ///< Changes directory.
92
93 static bool cd( const Path & dir , NoThrow ) ;
94 ///< Changes directory. Returns false on error.
95
96 static int errno_( const SignalSafe & = G::SignalSafe() ) ;
97 ///< Returns the process's current 'errno' value.
98 ///< (Beware of destructors of c++ temporaries disrupting
99 ///< the global errno value.)
100
101 static int errno_( const SignalSafe & , int e_new ) ;
102 ///< Sets the process's 'errno' value. Returns the old
103 ///< value. Used in signal handlers.
104
105 static std::string strerror( int errno_ ) ;
106 ///< Translates an 'errno' value into a meaningful diagnostic string.
107 ///< The returned string is non-empty, even for a zero errno.
108
109 static void revokeExtraGroups() ;
110 ///< Revokes secondary group memberships if really root
111 ///< or if suid.
112
113 static Identity beOrdinary( Identity ordinary_id , bool change_group = true ) ;
114 ///< Revokes special privileges (root or suid).
115 ///<
116 ///< If the real-id is root then the effective id is changed to whatever
117 ///< is passed in. Otherwise the effective id is changed to the real id,
118 ///< and the parameter is ignored.
119 ///<
120 ///< Returns the old effective-id, which can be passed to beSpecial().
121 ///<
122 ///< See also class G::Root.
123
124 static Identity beSpecial( Identity special_id , bool change_group = true ) ;
125 ///< Re-acquires special privileges (either root or suid). The
126 ///< parameter must have come from a previous call to beOrdinary().
127 ///<
128 ///< Returns the old effective-id (which is normally ignored).
129 ///<
130 ///< See also class G::Root.
131
132 static Identity beOrdinary( SignalSafe , Identity ordinary_id , bool change_group = true ) ;
133 ///< A signal-safe overload.
134
135 static Identity beSpecial( SignalSafe , Identity special_id , bool change_group = true ) ;
136 ///< A signal-safe overload.
137
138 static void beOrdinaryForExec( Identity run_as_id ) ;
139 ///< Sets the real and effective user and group ids to those
140 ///< given. Errors are ignored. This should only be used in
141 ///< a forked child process prior to doing an exec().
142
143 static std::string cwd( bool no_throw = false ) ;
144 ///< Returns the current working directory. Throws on error
145 ///< by default or returns the empty string.
146
147 static std::string exe() ;
148 ///< Returns the absolute path of the current executable,
149 ///< independent of the argv array passed to main(). Returns
150 ///< the empty string if unknown.
151
152 private:
153 Process() ;
154 } ;
155
156 namespace G
157 {
158 inline
159 std::ostream & operator<<( std::ostream & stream , const G::Process::Id & id )
160 {
161 return stream << id.str() ;
162 }
163
164 inline
165 std::istream & operator>>( std::istream & stream , G::Process::Id & id )
166 {
167 id = G::Process::Id( stream ) ;
168 return stream ;
169 }
170 }
171
172 #endif
gstrings.h
G::SignalSafe
An empty structure that is used to indicate a signal-safe, reentrant implementation.
Definition: gsignalsafe.h:36
gidentity.h
gsignalsafe.h
G
Low-level classes.
Definition: garg.h:29
G::IdentityUser
A convenience class which, when used as a private base, can improve readability when calling Identity...
Definition: gidentity.h:115
gexception.h
gpath.h
G::Process
A static interface for doing things with processes.