Solved why does the execve always execute failed

Raffeale

Active Member

Reaction score: 32
Messages: 193

Code:
int main() {
    int ret1 = execve("/bin/ls"," "," " );

    if(ret1<1){
        err(ret1,"execve failed");
    }
return 0;

}


When I run this code, I got execve failed: Bad address
It's my fault, it need pass the argv array pointer and envp array pointer.
 
Last edited by a moderator:
OP
R

Raffeale

Active Member

Reaction score: 32
Messages: 193

When I change this code to this, I can run but the result is wrong. It output some environment variable.
Code:
    char *argv[1] = {'\0'};
    int ret1 = execv("/bin/ls",argv );
 
Last edited by a moderator:
OP
R

Raffeale

Active Member

Reaction score: 32
Messages: 193

Code:
char *argv[] = { "ls", (char *)0 };
char *envp[] = { (char *)0 };
execve("/bin/ls", argv, envp);
how to get evironment variables ? is there a function to get it? or run setenv with execv?
 

vigole

Aspiring Daemon

Reaction score: 999
Messages: 972

how to get evironment variables ? is there a function to get it? or run setenv with execv?

execve(2)
The argument envp is also a pointer to a null-terminated array of character pointers to null-terminated strings.
A pointer to this array is normally stored in the global variable environ.

environ(7)
A process can query, update, and delete these strings using the getenv(3), setenv(3), and unsetenv(3) functions, respectively.

SEE ALSO
execve(2), getenv(3), setenv(3), unsetenv(3) and environ(7)

EXAMPLES

L145: src/contrib/nvi/common/util.c


L070: src/usr.sbin/pw/pw_log.c


L079: src/stand/efi/loader/bootinfo.c


L175: src/bin/ls/ls.c


L143: src/crypto/openssh/readpass.c

 
Top