我有下面的帮助函数,用于执行命令并获取posix系统上的返回值。我曾经使用popen
,但如果它运行并且在popen
/pclose
有机会完成其工作之前退出,则无法获得应用程序的返回码popen
。waitpid/wexitstatus返回0而不是正确的返回码
以下帮助函数创建一个进程分叉,使用execvp
运行所需的外部进程,然后父代使用waitpid
获取返回码。我看到它拒绝运行的奇怪情况。
当用wait
= true
调用时,waitpid
应该返回应用程序的退出代码,无论如何。但是,我看到stdout
输出指定返回码应该不为零,但返回码为零。在一个普通的shell中测试外部进程,然后echo
ing $?
返回非零值,所以在外部进程没有返回正确的代码时这不成问题。如果有任何帮助,正在运行的外部进程是mount(8)
(是的,我知道我可以使用mount(2)
,但除此之外)。
对于代码转储,我提前表示歉意。大部分是调试/日志:
inline int ForkAndRun(const std::string &command, const std::vector<std::string> &args, bool wait = false, std::string *output = NULL)
{
std::string debug;
std::vector<char*> argv;
for(size_t i = 0; i < args.size(); ++i)
{
argv.push_back(const_cast<char*>(args[i].c_str()));
debug += "\"";
debug += args[i];
debug += "\" ";
}
argv.push_back((char*)NULL);
neosmart::logger.Debug("Executing %s", debug.c_str());
int pipefd[2];
if (pipe(pipefd) != 0)
{
neosmart::logger.Error("Failed to create pipe descriptor when trying to launch %s", debug.c_str());
return EXIT_FAILURE;
}
pid_t pid = fork();
if (pid == 0)
{
close(pipefd[STDIN_FILENO]); //child isn't going to be reading
dup2(pipefd[STDOUT_FILENO], STDOUT_FILENO);
close(pipefd[STDOUT_FILENO]); //now that it's been dup2'd
dup2(pipefd[STDOUT_FILENO], STDERR_FILENO);
if (execvp(command.c_str(), &argv[0]) != 0)
{
exit(EXIT_FAILURE);
}
return 0;
}
else if (pid < 0)
{
neosmart::logger.Error("Failed to fork when trying to launch %s", debug.c_str());
return EXIT_FAILURE;
}
else
{
close(pipefd[STDOUT_FILENO]);
int exitCode = 0;
if (wait)
{
waitpid(pid, &exitCode, wait ? __WALL : (WNOHANG | WUNTRACED));
std::string result;
char buffer[128];
ssize_t bytesRead;
while ((bytesRead = read(pipefd[STDIN_FILENO], buffer, sizeof(buffer)-1)) != 0)
{
buffer[bytesRead] = '\0';
result += buffer;
}
if (wait)
{
if ((WIFEXITED(exitCode)) == 0)
{
neosmart::logger.Error("Failed to run command %s", debug.c_str());
neosmart::logger.Info("Output:\n%s", result.c_str());
}
else
{
neosmart::logger.Debug("Output:\n%s", result.c_str());
exitCode = WEXITSTATUS(exitCode);
if (exitCode != 0)
{
neosmart::logger.Info("Return code %d", (exitCode));
}
}
}
if (output)
{
result.swap(*output);
}
}
close(pipefd[STDIN_FILENO]);
return exitCode;
}
}
注意,运行该命令使用正确的参数,函数收益没有任何问题,并WIFEXITED
回报TRUE
确定。然而,WEXITSTATUS
返回0,当它应该返回别的东西。
您应该检查系统调用的返回码。调用'dup2(pipefd [STDOUT_FILENO],STDERR_FILENO);'会失败,[EBADF]和'waitpid()'也可能失败(什么是__WALL?)。另外,如果输出大于管道中的缓冲区,则应在等待进程前读取输出以避免死锁。此外,“等待”的一些检查是多余的。 – jilles
你假设'std :: vector'中的元素在内存中是连续的是不正确的。而C++标准开发人员却选择保证'push_back'不会改变现有元素的地址。我建议创建一个'char *'数组,但可能会有更多的STL技巧。 – jilles
@jilles C++ 03和C++ 11保证'vector'的连续内存分配,不是吗? –