Linux execve() 无法在 C 中启动程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8469818/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 03:35:28  来源:igfitidea点击:

execve() failing to launch program in C

clinuxassemblyexecuteexecve

提问by user99545

I am trying to spawn a new process using execve()from unistd.hon 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

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

program: db "/bin/ls",0

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

Thank you!

谢谢!

采纳答案by paxdiablo

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

你传递给的论点execve是错误的。第二个和第三个必须是一个带有 NULL 标记值的字符指针数组,而不是单个指针。

In other words, something like:

换句话说,类似于:

#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.

当我运行它时,我确实得到了当前目录中的文件列表。

回答by Employed Russian

Try reading man execveagain. You are passing the wrong arguments to it. Pay particular attention to what the second argument should be.

man execve再次尝试阅读。您正在向它传递错误的参数。特别注意第二个参数应该是什么。

Also, running your program under stracecould be illuminating.

此外,在下面运行您的程序strace可能会很有启发性。

回答by Lelouch Lamperouge

From the manpages,

手册页,

int execve(const char *filename, char *const argv[], char *const envp[]);

So the problem in your case is that you haven't passed the 2nd and the 3rd argument correctly.

所以你的问题是你没有正确传递第二个和第三个参数。

/* execve.c */

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

int
main(int argc, char *argv[])
{
    char *newargv[] = { NULL, "hello", "world", NULL };
    char *newenviron[] = { NULL };


newargv[0] = argv[1];

execve(argv[1], newargv, newenviron);


}
//This is a over-simplified version of the example in the man page

Run this as:

运行它:

$ cc execve.c -o execve
$ ./execve ls