简体   繁体   English

无法dup2将管道的末端写入stdout

[英]cannot dup2 write end of a pipe to stdout

i'm having a problem with dup2() and pipe(). 我在dup2()和pipe()时遇到问题。

i'm receiving EBADF when i try to dup2 a write end of a pipe to STDOUT_FILENO. 当我尝试将管道的写入端dup2发送到STDOUT_FILENO时,我正在接收EBADF。

i breaked on dup2(pout[1], STDOUT_FILENO) with gdb and checked that /proc/$pid/fdinfo/$pout[1] has the O_WRONLY flag. 我用gdb在dup2(pout[1], STDOUT_FILENO)/proc/$pid/fdinfo/$pout[1]了,并检查了/proc/$pid/fdinfo/$pout[1]是否具有O_WRONLY标志。 this trouble is driving me mad. 这个麻烦使我发疯。

NOTES: 笔记:

  • at the start of the function i initialize all pipes to -1. 在函数开始时,我将所有管道初始化为-1。
  • this issue happens only on x86, on my x86_64 workstation everything goes fine. 此问题仅发生在x86上,在我的x86_64工作站上一切正常。
  • OS is Gentoo GNU/Linux ( both x86 and x86_64 machines ) 操作系统是Gentoo GNU / Linux(同时使用x86和x86_64机器)
if(pipe2(pexec, O_CLOEXEC)) {
  fprintf(stderr, "%s: exec pipe: %s\n", __func__, strerror(errno));
  goto start_fail;
}

if(h->have_stdin && pipe(pin)) {
  fprintf(stderr, "%s: input pipe: %s\n", __func__, strerror(errno));
  goto start_fail;
}

if(h->have_stdout && pipe(pout)) {
  fprintf(stderr, "%s: output pipe: %s\n", __func__, strerror(errno));
  goto start_fail;
}

if(h->have_stdout) {
  printf("%s: output pipes: rd=%d wr=%d\n", __func__, pout[0], pout[1]);
}

if((pid = fork()) < 0) {
  fprintf(stderr, "%s: fork: %s\n", __func__, strerror(errno));
  goto start_fail;
} else if(!pid) {
  // child
  close(pexec[0]);
  close(pin[1]);
  close(pout[0]);

  i= open("/dev/null", O_RDWR);

  if(pin[0] == -1)
    pin[0] = i;
  if(pout[1] == -1)
    pout[1] = i;

  if(h->workdir)
    chdir(h->workdir);

  if(dup2(pin[0], STDIN_FILENO)) {
    fprintf(stderr, "%s: dup2(%d, %d): %s\n", __func__, pin[0], STDIN_FILENO, strerror(errno));
  }
  if(dup2(pout[1], STDOUT_FILENO)) {
    fprintf(stderr, "%s: dup2(%d, %d): %s\n", __func__, pout[1], STDOUT_FILENO, strerror(errno));
  }
#ifdef NDEBUG
  dup2(i, STDERR_FILENO);
#endif

  close(i);
  close(pin[0]);
  close(pout[1]);

  execv(argv[0], argv);
  fprintf(stderr, "%s: execv: %s\n", __func__, strerror(errno));
  write(pexec[1], "!", 1);
  close(pexec[1]);
  exit(-1);
} else {
  // parent
  close(pexec[1]);
  close(pin[0]);
  close(pout[1]);
  if(read(pexec[0],&exec_failed, 1)) {
    fprintf(stderr, "%s: exec failed\n", __func__);
    waitpid(pid, NULL, 0);
    goto start_fail;
  }
#ifndef NDEBUG
  printf("%s: successfully started command '%s' (pid=%d)\n", __func__, argv[0], pid);
#endif
  close(pexec[0]);
}

the output is the following: 输出如下:

...
handle_cmd_start: output pipes: rd=12 wr=13
...
...
handle_cmd_start: dup2(13, 1): Bad file descriptor
handle_cmd_start: successfully started command 'tools/nmap/nmap' (pid=1154)
....

thanks in advance for any help. 在此先感谢您的帮助。

dup2 returns the destination file descriptor on success, not zero. dup2成功返回目标文件描述符,而不是零。 Your checks should not be if (dup2(...)) but if (dup2(...) < 0) . 您的检查不应为if (dup2(...))而应为if (dup2(...) < 0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM