2

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?

4

1 回答 1

2
qDebug() << "(" << errno << ") " << strerror(errno);
_exit(errno);

几乎任何对标准库函数的调用都可以修改errno. 可能会qDebug调用一些设置的 I/O 函数errno,甚至可能是<<I/O 运算符。errno大多数成功调用都不会修改,但是您获得的级别越高,您就越不知道引擎盖下没有一些正常的失败调用。因此errno,您正在打印的值不是errno您传递给_exit.

作为 的一般原则errno,如果您正在做的事情比仅仅打印一次更复杂,请在执行任何其他操作之前将值保存到变量中。

正如评论中已经提到的,请注意大多数 Unix 系统(包括所有常见系统)仅传递 8 位值作为退出状态,但errno可以大于 255。例如,如果您在可能出现 256 错误的系统上运行此程序代码,调用_exit(256)将导致调用者看到返回码 0,因此错误地认为成功。

通常,将所有错误值折叠为成功/失败就足够了。如果您需要区分更多,请确保您通过exit/传递的信息wait在 0-255 范围内。

int exec_error = errno;
qDebug() << "(" << exec_error << ") " << strerror(exec_error);
_exit(!!exec_error);
于 2015-08-02T13:26:10.023 回答