I'm calling execvp()
with a deliberately wrong argument in a fork()
'ed child. The errno
number is properly set to ENOENT
in the child process. I then terminate the child process with _exit(errno);
.
My main process calls wait()
. When I inspect the returned status with WIFEXITED
and WEXITSTATUS
I always get EINVAL
for the first invocation. All other invocations return the correct ENOENT
code.
I cannot explain this behavior. Below is the complete function, which does all of the above things, but a bit more complex.
QVariantMap
System::exec(const QString & prog, const QStringList & args)
{
pid_t pid = fork();
if (pid == 0) {
int cargs_len = args.length() + 2;
char * cargs[cargs_len];
cargs[cargs_len - 1] = NULL;
QByteArrayList as;
as.push_back(prog.toLocal8Bit());
std::transform(args.begin(), args.end(), std::back_inserter(as),
[](const QString & s) { return s.toLocal8Bit(); });
for (int i = 0; i < as.length(); ++i) {
cargs[i] = as[i].data();
}
execvp(cargs[0], cargs);
// in case execvp fails, terminate the child process immediately
qDebug() << "(" << errno << ") " << strerror(errno); // <----------
_exit(errno);
} else if (pid < 0) {
goto fail;
} else {
sigset_t mask;
sigset_t orig_mask;
sigemptyset(&mask);
sigaddset(&mask, SIGCHLD);
if (sigprocmask(SIG_BLOCK, &mask, &orig_mask) < 0) {
goto fail;
}
struct timespec timeout;
timeout.tv_sec = 0;
timeout.tv_nsec = 10 * 1000 * 1000;
while (true) {
int ret = sigtimedwait(&mask, NULL, &timeout);
if (ret < 0) {
if (errno == EAGAIN) {
// timeout
goto win;
} else {
// error
goto fail;
}
} else {
if (errno == EINTR) {
// not SIGCHLD
continue;
} else {
int status = 0;
if (wait(&status) == pid) {
if (WIFEXITED(status)) {
return { { "error", strerror(WEXITSTATUS(status)) } };
} else {
goto fail;
}
} else {
goto fail;
}
}
}
}
}
win:
return {};
fail:
return { { "error", strerror(errno) } };
}
It turns out that removing the line with the qDebug()
call makes the problem go away. Why does adding a debugging call change the behavior of the program?