gnewprocess.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_NEW_PROCESS_H
22 #define G_NEW_PROCESS_H
23 
24 #include "gdef.h"
25 #include "gexception.h"
26 #include "gidentity.h"
27 #include "gprocess.h"
28 #include "gpath.h"
29 #include "gstrings.h"
30 #include <string>
31 
33 namespace G
34 {
35  class NewProcess ;
36 }
37 
43 {
44 public:
45  G_EXCEPTION( CannotFork , "cannot fork()" ) ;
46  G_EXCEPTION( WaitError , "cannot wait()" ) ;
47  G_EXCEPTION( ChildError , "child process terminated abnormally or stopped" ) ;
48  G_EXCEPTION( Insecure , "refusing to exec() while the user-id is zero" ) ;
49  G_EXCEPTION( PipeError , "pipe error" ) ;
50  G_EXCEPTION( InvalidPath , "invalid executable path -- must be absolute" ) ;
51  G_EXCEPTION( NoExtension , "refusing to CreateProcess() without a file extension such as .exe" ) ; // windows
52 
53  enum Who { Parent , Child } ;
54  class ChildProcessImp ;
56  class ChildProcess
57  {
58  private: explicit ChildProcess( ChildProcessImp * ) ;
59  public: ChildProcess( const ChildProcess & ) ;
60  public: ~ChildProcess() ;
61  public: void operator=( const ChildProcess & ) ;
62  public: int wait() ;
63  public: std::string read() ;
64  private: ChildProcessImp * m_imp ;
65  friend class NewProcess ;
66  } ;
67 
68  static Who fork() ;
70 
71  static Who fork( Process::Id & child ) ;
74 
75  static int spawn( Identity nobody , const Path & exe , const Strings & args ,
76  std::string * pipe_result_p = NULL , int error_return = 127 ,
77  std::string (*error_decode_fn)(int) = 0 ) ;
90 
91  static ChildProcess spawn( const Path & exe , const Strings & args ) ;
94 
95 private:
96  friend class ChildProcess ;
97  NewProcess() ;
98  static int wait( const Process::Id & child ) ;
99  static int wait( const Process::Id & child , int error_return ) ;
100  static int execCore( const Path & , const Strings & ) ;
101 } ;
102 
103 #endif
104 
static int spawn(Identity nobody, const Path &exe, const Strings &args, std::string *pipe_result_p=NULL, int error_return=127, std::string(*error_decode_fn)(int)=0)
Runs a command in an unprivileged child process.
std::list< std::string > Strings
A std::list of std::strings.
Definition: gstrings.h:39
A very low-level interface to getpwnam() and the get/set/e/uid/gid functions.
Definition: gidentity.h:41
Low-level classes.
Represents the state of a child process.
Definition: gnewprocess.h:56
static Who fork()
Forks a child process.
A private implementation class used by G::NewProcess.
#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
A Path object represents a file system path.
Definition: gpath.h:44
A static interface for creating new processes.
Definition: gnewprocess.h:42