本文介绍了execve()无法在C中启动程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Linux上 unistd.h 中的 execve()生成新进程.我尝试将以下参数传递给它 execve("/bin/ls","/bin/ls",NULL); ,但没有结果.我也没有收到错误,程序刚刚退出.有这种情况发生的原因吗?我尝试过以root和普通用户身份启动它.我需要使用 execve()的原因是因为我正在尝试使其在像这样的程序集调用中工作

I am trying to spawn a new process using execve() from unistd.h on Linux. I have tried passing it the following parameters execve("/bin/ls", "/bin/ls", NULL); but get no result. I do not get an error either, the program just exits. Is there a reason why this is happening? I have tried launching it as root and regular user. The reason I need to use execve() is because I am trying to get it to work in an assembly call like so

program: db "/bin/ls",0

mov eax, 0xb
mov ebx, program
mov ecx, program
mov edx, 0
int 0x80

谢谢!

推荐答案

您要传递给 execve 的参数是错误的.第二个和第三个必须都是char指针的数组,该指针的值应为NULL,而不是单个指针.

The arguments that you're passing to execve are wrong. Both the second and third must be an array of char pointers with a NULL sentinel value, not a single pointer.

换句话说,是这样的:

#include <unistd.h>
int main (void) {
    char * const argv[] = {"/bin/ls", NULL};
    char * const envp[] = {NULL};
    int rc = execve ("/bin/ls", argv, envp);
    return rc;
}

运行该命令时,确实可以得到当前目录中文件的列表.

When I run that, I do indeed get a list of the files in the current directory.

这篇关于execve()无法在C中启动程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-10 00:52