execve() failing to launch program in C
Solution 1
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.
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.
Solution 2
From the man pages,
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
Solution 3
Try reading man execve
again. You are passing the wrong arguments to it. Pay particular attention to what the second argument should be.
Also, running your program under strace
could be illuminating.
user99545
Updated on June 15, 2022Comments
-
user99545 almost 2 years
I am trying to spawn a new process using
execve()
fromunistd.h
on Linux. I have tried passing it the following parametersexecve("/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 useexecve()
is because I am trying to get it to work in an assembly call like soprogram: db "/bin/ls",0 mov eax, 0xb mov ebx, program mov ecx, program mov edx, 0 int 0x80
Thank you!
-
user99545 over 12 years
strace
gives me-1 EFAULT (Bad address)
, I have re-read the man page on it, but I am still confused. Am I passing the second argument incorrectly? (the process arguments) -
user99545 over 12 yearsGotcha,
man execve
said an array, I assumed just a normal string would do. Lesson learned, thanks!