VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gprocess.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2017 Graeme Walker
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 
99  static int errno_( const SignalSafe & , int e_new ) ;
100  ///< Sets the process's 'errno' value. Returns the old
101  ///< value. Used in signal handlers.
102 
103  static std::string strerror( int errno_ ) ;
104  ///< Translates an 'errno' value into a meaningful diagnostic string.
105  ///< The returned string is non-empty, even for a zero errno.
106 
107  static void revokeExtraGroups() ;
108  ///< Revokes secondary group memberships if really root
109  ///< or if suid.
110 
111  static Identity beOrdinary( Identity ordinary_id , bool change_group = true ) ;
112  ///< Revokes special privileges (root or suid).
113  ///<
114  ///< If the real-id is root then the effective id is changed to whatever
115  ///< is passed in. Otherwise the effective id is changed to the real id,
116  ///< and the parameter is ignored.
117  ///<
118  ///< Returns the old effective-id, which can be passed to beSpecial().
119  ///<
120  ///< See also class G::Root.
121 
122  static Identity beSpecial( Identity special_id , bool change_group = true ) ;
123  ///< Re-acquires special privileges (either root or suid). The
124  ///< parameter must have come from a previous call to beOrdinary().
125  ///<
126  ///< Returns the old effective-id (which is normally ignored).
127  ///<
128  ///< See also class G::Root.
129 
130  static Identity beOrdinary( SignalSafe , Identity ordinary_id , bool change_group = true ) ;
131  ///< A signal-safe overload.
132 
133  static Identity beSpecial( SignalSafe , Identity special_id , bool change_group = true ) ;
134  ///< A signal-safe overload.
135 
136  static void beOrdinaryForExec( Identity run_as_id ) ;
137  ///< Sets the real and effective user and group ids to those
138  ///< given. Errors are ignored. This should only be used in
139  ///< a forked child process prior to doing an exec().
140 
141  static std::string cwd( bool no_throw = false ) ;
142  ///< Returns the current working directory. Throws on error
143  ///< by default or returns the empty string.
144 
145  static std::string exe() ;
146  ///< Returns the absolute path of the current executable,
147  ///< independent of the argv array passed to main(). Returns
148  ///< the empty string if unknown.
149 
150 private:
151  Process() ;
152 } ;
153 
154 namespace G
155 {
156  inline
157  std::ostream & operator<<( std::ostream & stream , const G::Process::Id & id )
158  {
159  return stream << id.str() ;
160  }
161 
162  inline
163  std::istream & operator>>( std::istream & stream , G::Process::Id & id )
164  {
165  id = G::Process::Id( stream ) ;
166  return stream ;
167  }
168 }
169 
170 #endif
An empty structure that is used to indicate a signal-safe, reentrant implementation.
Definition: gsignalsafe.h:36
A convenience class which, when used as a private base, can improve readability when calling Identity...
Definition: gidentity.h:115
A static interface for doing things with processes.