为什么C不启动新的shell?

发布于 2025-01-13 19:12:46 字数 336 浏览 1 评论 0原文

我有一小块 C 代码。

#include<stdio.h>

int main()
{
    char *name[2];

    name[0] = '/bin/sh';
    name[1] = NULL;
    execve(name[0], name, NULL);
}

我有一个符号链接 sh ->嘘。当我运行该程序时,没有任何反应,并且我停留在同一个 shell 中。我知道我不在新的外壳中,因为当我在终端退出后退出时。如果我运行 /bin/sh 我会得到另一个 shell。我的代码是错误的还是某种类型的安全措施不允许我这样做?

I have a small chunk of C code.

#include<stdio.h>

int main()
{
    char *name[2];

    name[0] = '/bin/sh';
    name[1] = NULL;
    execve(name[0], name, NULL);
}

I have a symlink sh -> zsh. When I run the program nothing happens and I stay in the same shell. I know I am not in a new shell since when i exit once the terminal exits. If i run /bin/sh I get another shell. Is my code wrong or is this some type of security measure not allowing me to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼眸印温柔 2025-01-20 19:12:46

编译这个带有警告的代码会立即显示出了什么问题:

gcc -g t.c
t.c: In function ‘main’:
t.c:7:15: warning: character constant too long for its type
    7 |     name[0] = '/bin/sh';
      |               ^~~~~~~~~
t.c:7:13: warning: assignment to ‘char *’ from ‘int’ makes pointer from integer without a cast [-Wint-conversion]
    7 |     name[0] = '/bin/sh';
      |             ^
t.c:9:5: warning: implicit declaration of function ‘execve’ [-Wimplicit-function-declaration]
    9 |     execve(name[0], name, NULL);
      |     ^~~~~~

当然这个程序不起作用。您想要的字符串文字是 "/bin/sh" (您在 C 中使用的引号很重要)。

Compiling this with warnings immediately shows what's wrong:

gcc -g t.c
t.c: In function ‘main’:
t.c:7:15: warning: character constant too long for its type
    7 |     name[0] = '/bin/sh';
      |               ^~~~~~~~~
t.c:7:13: warning: assignment to ‘char *’ from ‘int’ makes pointer from integer without a cast [-Wint-conversion]
    7 |     name[0] = '/bin/sh';
      |             ^
t.c:9:5: warning: implicit declaration of function ‘execve’ [-Wimplicit-function-declaration]
    9 |     execve(name[0], name, NULL);
      |     ^~~~~~

Of course this program doesn't work. A string literal you want is "/bin/sh" (the kind of quotes you use matters in C).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文