32 #include <sys/types.h>
54 void dupTo(
int stdxxx ) ;
55 void write(
const std::string & ) ;
69 int stdxxx ,
bool clean_env ,
bool strict_path ,
70 Identity run_as_id ,
bool strict_id ,
71 int exec_error_exit ,
const std::string & exec_error_format ,
72 std::string (*exec_error_format_fn)(std::string,
int) ) ;
75 static std::pair<bool,pid_t> fork() ;
79 static void printError(
int ,
const std::string & s ) ;
80 std::string execErrorFormat(
const std::string & format ,
int errno_ ) ;
96 int stdxxx ,
bool clean_env ,
bool strict_path ,
97 Identity run_as_id ,
bool strict_id ,
98 int exec_error_exit ,
const std::string & exec_error_format ,
99 std::string (*exec_error_format_fn)(std::string,
int) ) :
100 m_imp(new
NewProcessImp(exe,args,stdxxx,clean_env,strict_path,
101 run_as_id,strict_id,exec_error_exit,exec_error_format,exec_error_format_fn) )
112 return m_imp->wait() ;
117 return NewProcessImp::fork() ;
132 G::NewProcessImp::NewProcessImp(
const Path & exe ,
const StringArray & args ,
133 int stdxxx ,
bool clean_env ,
bool strict_path ,
134 Identity run_as_id ,
bool strict_id ,
135 int exec_error_exit ,
const std::string & exec_error_format ,
136 std::string (*exec_error_format_fn)(std::string,
int) ) :
142 G_ASSERT( stdxxx == -1 || stdxxx == 1 || stdxxx == 2 ) ;
143 if( stdxxx == 1 ) stdxxx = STDOUT_FILENO ;
144 else if( stdxxx == 2 ) stdxxx = STDERR_FILENO ;
148 throw NewProcess::InvalidPath( exe.
str() ) ;
151 throw NewProcess::Insecure() ;
154 std::pair<bool,pid_t> f =
fork() ;
155 bool in_child = f.first ;
156 m_child_pid = f.second ;
164 Process::beOrdinaryForExec( run_as_id ) ;
168 Process::closeFilesExcept( m_pipe.fd() ) ;
169 m_pipe.dupTo( stdxxx ) ;
173 ::signal( SIGPIPE , SIG_DFL ) ;
179 int e = run( exe , args , clean_env , strict_path ) ;
182 if( exec_error_format_fn != 0 )
183 printError( stdxxx , (*exec_error_format_fn)(exec_error_format,e) ) ;
184 else if( !exec_error_format.empty() )
185 printError( stdxxx , execErrorFormat(exec_error_format,e) ) ;
190 ::_exit( exec_error_exit ) ;
195 m_wait_future = NewProcessWaitFuture( m_child_pid , m_pipe.fd() ) ;
199 std::pair<bool,pid_t> G::NewProcessImp::fork()
201 std::cout << std::flush ;
202 std::cerr << std::flush ;
203 pid_t rc = ::fork() ;
204 const bool ok = rc != -1 ;
205 if( !ok )
throw NewProcess::CannotFork() ;
206 bool in_child = rc == 0 ;
207 pid_t child_pid =
static_cast<pid_t
>(rc) ;
208 return std::make_pair( in_child , child_pid ) ;
211 void G::NewProcessImp::printError(
int stdxxx ,
const std::string & s )
214 G_IGNORE_RETURN(
int , ::write( stdxxx , s.c_str() , s.length() ) ) ;
217 int G::NewProcessImp::run(
const G::Path & exe ,
const StringArray & args ,
bool clean_env ,
bool strict )
220 std::string path(
"PATH=/usr/bin:/bin" ) ;
221 std::string ifs(
"IFS= \t\n" ) ;
222 env[0U] =
const_cast<char*
>( path.c_str() ) ;
223 env[1U] =
const_cast<char*
>( ifs.c_str() ) ;
226 char ** argv =
new char* [ args.size() + 2U ] ;
227 std::string str_exe = exe.
str() ;
228 argv[0U] =
const_cast<char*
>( str_exe.c_str() ) ;
229 unsigned int argc = 1U ;
230 for( StringArray::const_iterator arg_p = args.begin() ; arg_p != args.end() ; ++arg_p , argc++ )
231 argv[argc] = const_cast<char*>(arg_p->c_str()) ;
232 argv[argc] = nullptr ;
234 const std::string exe_str = exe.
str() ;
235 const char * exe_p = exe_str.c_str() ;
240 ::execve( exe_p , argv , env ) ;
241 e = Process::errno_() ;
245 ::execv( exe_p , argv ) ;
246 e = Process::errno_() ;
250 ::execvp( exe_p , argv ) ;
251 e = Process::errno_() ;
255 G_DEBUG(
"G::NewProcess::run: execve() returned: errno=" << e <<
": " << exe ) ;
259 int G::NewProcessImp::id()
const
261 return static_cast<int>(m_child_pid) ;
266 return m_wait_future ;
269 void G::NewProcessImp::kill()
271 G_DEBUG(
"G::NewProcessImp::kill: killing process group " << m_child_pid ) ;
272 if( !m_killed && m_child_pid != -1 )
275 ::kill( -m_child_pid , SIGTERM ) ;
280 std::string G::NewProcessImp::execErrorFormat(
const std::string & format ,
int errno_ )
282 std::string result = format ;
284 Str::replaceAll( result ,
"__strerror__" , Process::strerror(errno_) ) ;
293 m_fds[0] = m_fds[1] = -1 ;
294 if( ::socketpair( AF_UNIX , SOCK_STREAM , 0 , m_fds ) < 0 )
295 throw NewProcess::PipeError() ;
296 G_DEBUG(
"G::Pipe::ctor: " << m_fds[0] <<
" " << m_fds[1] ) ;
301 if( m_fds[0] >= 0 ) ::close( m_fds[0] ) ;
302 if( m_fds[1] >= 0 ) ::close( m_fds[1] ) ;
305 void G::Pipe::inChild()
307 ::close( m_fds[0] ) ;
312 void G::Pipe::inParent()
314 ::close( m_fds[1] ) ;
319 int G::Pipe::fd()
const
324 void G::Pipe::dupTo(
int stdxxx )
326 if( m_fd != -1 && stdxxx != -1 && m_fd != stdxxx )
328 if( ::dup2(m_fd,stdxxx) != stdxxx )
329 throw NewProcess::PipeError() ;
334 int flags = ::fcntl( stdxxx , F_GETFD ) ;
335 flags &= ~FD_CLOEXEC ;
336 ::fcntl( stdxxx , F_SETFD , flags ) ;
370 char * p = &m_buffer[0] ;
371 size_t space = m_buffer.size() ;
375 ssize_t n = ::read( m_fd , p?p:more , p?space:
sizeof(more) ) ;
376 m_read_error = errno ;
377 if( n < 0 && m_error == EINTR )
388 m_buffer.resize( size ) ;
405 m_rc = ::waitpid( m_pid , &m_status , 0 ) ;
407 if( m_rc == -1 && m_error == EINTR )
420 if( m_error || m_read_error )
422 std::ostringstream ss ;
423 ss <<
"errno=" << (m_read_error?m_read_error:m_error) ;
424 throw NewProcess::WaitError( ss.str() ) ;
426 if( ! WIFEXITED(m_status) )
429 std::ostringstream ss ;
430 ss <<
"status=" << m_status ;
431 throw NewProcess::ChildError( ss.str() ) ;
433 result = WEXITSTATUS(m_status) ;
440 if( m_fd < 0 || m_read_error != 0 )
441 return std::string() ;
443 return std::string( &m_buffer[0] , m_buffer.size() ) ;
int get()
Returns the result of the run() as either the process exit code or as a thrown exception.
void kill()
Tries to kill the spawned process.
std::string str() const
Returns the path string.
bool isRoot() const
Returns true if the userid is zero.
static Identity invalid()
Returns an invalid identity.
A class that holds the parameters and the results of a waitpid() system call.
A combination of user-id and group-id, with a very low-level interface to the get/set/e/uid/gid funct...
std::vector< std::string > StringArray
A std::vector of std::strings.
static Identity effective()
Returns the current effective identity.
int id() const
Returns the process id.
static std::string fromInt(int i)
Converts int 'i' to a string.
bool isRelative() const
Returns true if the path is a relative path.
static std::pair< bool, pid_t > fork()
A utility function that forks the calling process and returns twice; once in the parent and once in t...
std::string output()
Returns the first bit of child-process output.
NewProcessWaitFuture & run()
Waits for the process identified by the constructor parameter to exit.
static unsigned int replaceAll(std::string &s, const std::string &from, const std::string &to)
Does a global replace on string 's', replacing all occurances of sub-string 'from' with 'to'...
NewProcessWaitFuture & wait()
Returns a reference to the WaitFuture sub-object so that the caller can wait for the child process to...
NewProcessWaitFuture()
Default constructor for an object where run() does nothing and get() returns zero.
A pimple-pattern implementation class used by G::NewProcess.
NewProcess(const Path &exe, const StringArray &args, int capture_stdxxx=1, bool clean_environment=true, bool strict_path=true, Identity run_as_id=Identity::invalid(), bool strict_id=true, int exec_error_exit=127, const std::string &exec_error_format=std::string(), std::string(*exec_error_format_fn)(std::string, int)=0)
Constructor.
A Path object represents a file system path.
A private implementation class used by G::NewProcess that wraps a unix pipe.