执行路径搜索? [英] execve with path search?

查看:75
本文介绍了执行路径搜索?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的代码中执行一个程序,并为其提供环境变量和参数. AFAICT,execve是正确的选择.

I want to execute a program from my code, and supply it with environment variables and arguments. AFAICT, execve is the right choice.

但是,execve收到一个path参数,而不是filename,这意味着它希望第一个参数是可执行文件的路径.

But, execve receives a path argument, not a filename, meaning it expects the first argument to be a path to the executable.

我知道我可以自己解析$PATH来找到路径,但实际上,别无选择吗?没有其他人可以在我某个地方实现它了吗?

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?

推荐答案

某些系统可能会提供execvpe(). Google搜索"execvpe"显示了多种选择,包括至少一个实现(比后面的实现复杂得多,但是在其自己的代码中包含了大多数execvp()).

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 of execvp() in its own code).

对于那些没有的人,您可以自己提供:

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;
}

由于execvp()仅返回-1(并且仅在出现错误时返回),因此您可能没有rc(只是强行返回-1)就可以生存.

You probably could survive without rc (just forcibly returning -1) since execvp() only ever returns -1 (and it only ever returns on an error).

您甚至不必担心此代码中的线程安全性.在fork()之后将使用它,这是正常情况,此时,进程中只有一个线程.如果您认为可以在有多个线程的情况下使用它,那么您需要仔细考虑一下,即使短暂地修改全局环境是否安全.显然,如果execvp()成功,就不会有问题(所有线程都会突然终止).如果execvp()失败,则其他线程之一可能会看到修改后的环境,并可能据此做出错误的决定.在这种情况下,您需要适当地保护环境(并且可能涉及(互斥)锁定getenv()setenv()putenv()以及execvpe()).

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 the execvp() succeeds, there won't be a problem (all the threads will be abruptly terminated). If the execvp() 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 in getenv(), setenv() and putenv() as well as in execvpe()).

(我发现execvpe()的实现通过实现execvp()逻辑,然后使用execve()执行程序来避免线程安全性问题.)

(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()返回,则该过程将退出,因此,通常,恢复环境不会影响程序.但是,这比后悔更好.

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.

这篇关于执行路径搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆