I want to execute a program from my code, and supply it with environment variables and arguments. AFAICT, execve
is the right choice.
But, execve
receives a path
argument, not a filename
, meaning it expects the first argument to be a path to the executable.
I know I can parse $PATH
myself to find the path, but really, is there no other choice? Has no one else implemented it somewhere for me to use?
Some systems may provide
execvpe()
. A Google search for 'execvpe' shows a variety of options, including at least one implementation (considerably more complex than what follows, but it includes most ofexecvp()
in its own code).For those that do not, you can provide it for yourself:
You probably could survive without
rc
(just forcibly returning -1) sinceexecvp()
only ever returns-1
(and it only ever returns on an error).You probably do not even have to worry about thread safety in this code. The normal scenario that will use it is just after a
fork()
, and at that point, there is only one thread in the process. If you think you may use it when there are multiple threads around, then you need to think rather carefully about whether it is safe to modify the global environment even briefly. Clearly, if theexecvp()
succeeds, there won't be a problem (all the threads will be abruptly terminated). If theexecvp()
fails, then maybe one of the other threads would see the modified environment and might make bad decisions based on that. In which case, you would need to protect the environment appropriately (and that probably involves (mutual exclusion) locking ingetenv()
,setenv()
andputenv()
as well as inexecvpe()
).(The implementation of
execvpe()
that I found avoids the thread-safety issues by implementingexecvp()
logic and then usingexecve()
to execute the program.)Normally, if
execvpe()
returns, the process will exit, so very often reinstating the environment is not going to affect the program. However, it is better safe than sorry.