【问题标题】:system("/bin/sh") closes immediately when input given through pipesystem("/bin/sh") 通过管道输入时立即关闭
【发布时间】:2021-07-29 22:49:02
【问题描述】:

我写了一个c程序如下:

#include <stdio.h>
#include <stdlib.h>

int main() {
        int a;
        scanf("%d",&a);
        system("/bin/sh");
        return 0;
}

编译它,当我执行 a.out 文件时,它显示如下:

user@system:~/dir$ ./a.out
123                           <- this is the input that I gave
$ whoami
user

所以在这里我得到了一个通过 c 调用的适当子 shell
现在,如果我通过管道使用输入执行 a.out 文件,它会显示如下:

user@system:~/dir$ echo 123 | ./a.out
user@system:~/dir$

它只是简单地执行命令,创建 shell,终止 shell 并退出。

我确信它会执行子 shell,因为在执行 echo 123 | ltrace ./a.out 时它显示如下:

system("/bin/sh" <no return ...>
--- SIGCHLD (Child exited) ---
<... system resumed> )                                                              = 0
+++ exited (status 0) +++

这意味着创建了一个子shell
我不明白这两种方法有什么区别

【问题讨论】:

  • 你期待什么?

标签: c shell terminal pipe system


【解决方案1】:

sh 将读取 stdin 直到遇到 EOF。 在第一种情况下,您不发送EOF(这可以通过按Ctrl-D 来完成,具体取决于您的环境)。在第二种情况下,EOF 发生在管道输入耗尽之后(即在echo 123 完成之后)。 为了更好地理解,您可以使用一个简单的程序模拟sh 行为:

#include <stdio.h>
int main(void)
{
    char input[100];
    while(fgets(input, 100, stdin)) {
        printf("Echoing back: %s\n", input);
    }
    return 0;
}

您可以编译它并使用您的system() 调用而不是sh 运行并观察类似的行为。

【讨论】:

  • 当我运行(echo 123; echo pwd) | ./a.out 时,它不会在终端中运行命令。在将 123 分配给变量之后,“pwd”仍在输入流中。没有遇到 EOF,但它仍然不打印目录。在更改原始代码中system("/bin/sh")scanf 的顺序时,它可以正常工作。
猜你喜欢
  • 2022-11-03
  • 1970-01-01
  • 2021-10-19
  • 1970-01-01
  • 2017-05-19
  • 2012-01-20
  • 2013-09-02
  • 1970-01-01
  • 2016-12-14
相关资源
最近更新 更多