以下のヘルパー関数を使用して、コマンドを実行し、posix システムで戻り値を取得します。以前は を使用していましたが、アプリケーションを実行して終了する前にpopen
アプリケーションのリターン コードを取得することは不可能でした。popen
popen
pclose
次のヘルパー関数は、プロセス フォークを作成しexecvp
、目的の外部プロセスを実行するために使用し、次に、親waitpid
がリターン コードを取得するために使用します。実行を拒否する奇妙なケースが見られます。
wait
=true
で呼び出された場合waitpid
、何があってもアプリケーションの終了コードを返す必要があります。ただし、stdout
戻りコードがゼロ以外である必要があることを指定する出力が表示されますが、戻りコードはゼロです。通常のシェルで外部プロセスをテストすると、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 を返します。