简体   繁体   English

用路径搜索执行?

[英]execve with path search?

I want to execute a program from my code, and supply it with environment variables and arguments. 我想从我的代码中执行一个程序,并为它提供环境变量和参数。 AFAICT, execve is the right choice. AFAICT, execve是正确的选择。

But, execve receives a path argument, not a filename , meaning it expects the first argument to be a path to the executable. 但是, execve接收path参数,而不是filename ,这意味着它期望第一个参数是可执行文件的路径。

I know I can parse $PATH myself to find the path, but really, is there no other choice? 我知道我可以自己解析$PATH来寻找路径,但实际上,没有其他选择吗? Has no one else implemented it somewhere for me to use? 没有其他人在某处实现它供我使用吗?

Some systems may provide execvpe() . 有些系统可能提供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 of execvp() in its own code). Google搜索“execvpe”会显示各种选项,包括至少一个实现(比后面的内容复杂得多,但它在自己的代码中包含了大部分execvp() )。

For those that do not, you can provide it for yourself: 对于那些没有的人,您可以为自己提供:

int execvpe(const char *program, char **argv, char **envp)
{
    char **saved = environ;
    int rc;
    environ = envp;
    rc = execvp(program, argv);
    environ = saved;
    return rc;
}

You probably could survive without rc (just forcibly returning -1) since execvp() only ever returns -1 (and it only ever returns on an error). 你可能在没有rc情况下生存(只是强行返回-1),因为execvp()只返回-1 (它只会在错误时返回)。

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. 将使用它的正常方案就在fork()之后,此时,进程中只有一个线程。 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 the execvp() succeeds, there won't be a problem (all the threads will be abruptly terminated). 显然,如果execvp()成功,则不会出现问题(所有线程都会突然终止)。 If the execvp() fails, then maybe one of the other threads would see the modified environment and might make bad decisions based on that. 如果execvp()失败,那么其他一个线程可能会看到修改后的环境,并可能在此基础上做出错误的决定。 In which case, you would need to protect the environment appropriately (and that probably involves (mutual exclusion) locking in getenv() , setenv() and putenv() as well as in execvpe() ). 在这种情况下,你需要适当地保护环境(并可能涉及(互斥)锁定getenv() setenv()putenv()中以及execvpe()

(The implementation of execvpe() that I found avoids the thread-safety issues by implementing execvp() logic and then using execve() to execute the program.) (我发现的execvpe()的实现通过实现execvp()逻辑然后使用execve()来执行程序来避免线程安全问题。)

Normally, if execvpe() returns, the process will exit, so very often reinstating the environment is not going to affect the program. 通常,如果execvpe()返回,则进程将退出,因此通常恢复环境不会影响程序。 However, it is better safe than sorry. 但是,它比抱歉更安全。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM