【问题标题】:Writing to openssl's stdio in C using pipe使用管道在 C 中写入 openssl 的 stdio
【发布时间】:2017-02-24 10:42:24
【问题描述】:

我正在尝试像这样将一些数据传递给 openssl:

int fd[2];
char buff[MAXBUFF];
pid_t childpid;
pipe(fd);

childpid = fork();

if (childpid == 0){
  dup2(0, fd[0]);
  close(fd[1]);
  execlp("openssl", "s_client", "-connect", "imap.gmail.com:993", "-crlf", NULL);
}
else {
  close(fd[0]);
  dosomething(buff);
  write(fd[1], buff, strlen(buff) + 1);
  sleep(4);
}

但似乎 openssl 没有从 buff 获取数据,它只是挂起等待我认为的一些输入。 如果我用 bash 做同样的事情

a.sh:

.
.
.
echo $data
sleep 3
.
.
.

b.sh:

openssl s_client -connect imap.gmail.com:993 -crlf

./a.sh | ./b.sh

一切正常。

【问题讨论】:

  • 你可以尝试冲洗管道吗?
  • buff 是否包含尾随换行符?
  • @orion78fr 好吧,我尝试将 fd[1] 复制到父进程中的标准输出,然后使用 printf 和 fflush 写入管道,但它再次不起作用。
  • @Ctx 是的,确实如此。
  • @vacso 好吧,那你为什么怀疑 openssl 根本收不到数据呢?您是否可能误解了您所观察到的内容?

标签: c linux openssl pipe exec


【解决方案1】:

你切换了dup2()的参数。

原型是:

int dup2(int oldfd, int newfd);

所以你应该这样做

dup2(fd[0], 0);

将fd[0] 设置为子进程的标准输入。然后它应该按预期工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多