Skip to contentSkip to conversation

execv fails with EFAULT

DISCUSSION Prasanta Sadhukhan 4 participants 6 messages Mar 7, 2007

lore
Share & tools
lore
#0 lore raw

Hi,

When I tried the following program
void test4()
{
printf("doing execv\n");
if(execv("/bin/ls", "-l") == -1)

       printf("exec failed with errno %d\n", errno);
   else
       printf("exec succeed\n");
}

I see that execv is failing with errno 14(EFAULT).
I am using RH9 2.6 kernel. Does anybody know what is the cause?

Regards
Prasanta

#1 lore raw
Quoted 11 lines · Prasanta Sadhukhan

Hi,

When I tried the following program
void test4()

{
    printf("doing execv\n");
    if(execv("/bin/ls", "-l") == -1)
        printf("exec failed with errno %d\n", errno);
    else
        printf("exec succeed\n");
}

Prasanta,

A NULL terminated array of arguments must be passed to execv(2) as the prototype indicates: int execv(const char *path, char *const argv[]);

For example:

#include <unistd.h>

char *cmd[] = { "ls", "-l", NULL };
int result = execv ("/bin/ls", cmd);

An error of EFAULT usually means that an argument points to an illegal address.

\Steve

--

Steve Grägert <[email protected]>
Jabber xmpp://[email protected]
Internet http://eth0.graegert.com, http://blog.graegert.com
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#2 lore raw
Quoted 22 lines · Steve Graegert
Quoted 11 lines · Prasanta Sadhukhan

Hi,

When I tried the following program
void test4()

{
    printf("doing execv\n");
    if(execv("/bin/ls", "-l") == -1)
        printf("exec failed with errno %d\n", errno);
    else
        printf("exec succeed\n");
}

Prasanta,

A NULL terminated array of arguments must be passed to execv(2) as the prototype indicates: int execv(const char *path, char *const argv[]);

For example:

Quoted 8 lines

#include <unistd.h>

char *cmd[] = { "ls", "-l", NULL };
int result = execv ("/bin/ls", cmd);

An error of EFAULT usually means that an argument points to an illegal address.

\Steve

Thanks Steve... If I have a string like this sprintf(str, "-Xparameter:%d %s, value, command"), how to make it NULL terminated. Is this declaration char *cmd[] = {str, NULL} and invocation execv(path, cmd) correct?

Regards
Prasanta

Quoted 10 lines
Signature · 9 lines
-- 

Steve Grägert <[email protected]>
Jabber    xmpp://[email protected]
Internet  http://eth0.graegert.com, http://blog.graegert.com
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#3 lore raw
Quoted 32 lines · Prasanta Sadhukhan
Quoted 30 lines · Steve Graegert
Quoted 11 lines · Prasanta Sadhukhan

Hi,

When I tried the following program
void test4()

{
    printf("doing execv\n");
    if(execv("/bin/ls", "-l") == -1)
        printf("exec failed with errno %d\n", errno);
    else
        printf("exec succeed\n");
}

Prasanta,

A NULL terminated array of arguments must be passed to execv(2) as the prototype indicates: int execv(const char *path, char *const argv[]);

For example:

#include <unistd.h>

char *cmd[] = { "ls", "-l", NULL };
int result = execv ("/bin/ls", cmd);

An error of EFAULT usually means that an argument points to an illegal address.

Quoted 6 lines
Quoted 1 line

\Steve

Thanks Steve... If I have a string like this sprintf(str, "-Xparameter:%d %s, value, command"), how to make it NULL terminated. Is this declaration char *cmd[] = {str, NULL} and invocation execv(path, cmd) correct?

If you mean

       sprintf(str, "-Xparameter:%d %s", value, command);

your declaration and invocation of

       char *cmd[] = {str, NULL}
       execv(path, cmd);

should be OK.

\Steve

--

Steve Grägert <[email protected]>
Jabber xmpp://[email protected]
Internet http://eth0.graegert.com, http://blog.graegert.com
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#4 lore raw
In reply to Prasanta Sadhukhan
Quoted 26 lines · Prasanta Sadhukhan
Quoted 26 lines
Quoted 9 lines

When I tried the following program
void test4()

{
    printf("doing execv\n");
    if(execv("/bin/ls", "-l") == -1)
        printf("exec failed with errno %d\n", errno);
    else
        printf("exec succeed\n");
}

Prasanta,

A NULL terminated array of arguments must be passed to execv(2) as the prototype indicates: int execv(const char *path, char *const argv[]);

For example:

#include <unistd.h>

char *cmd[] = { "ls", "-l", NULL };
int result = execv ("/bin/ls", cmd);

An error of EFAULT usually means that an argument points to an illegal address.

Quoted 6 lines
Quoted 1 line

\Steve

Thanks Steve... If I have a string like this sprintf(str, "-Xparameter:%d %s, value, command"), how to make it NULL terminated. Is this declaration char *cmd[] = {str, NULL} and invocation execv(path, cmd) correct?

Probably not. It's correct in that the second argument to execv() has the correct type, but if you're trying to pass multiple arguments, it won't work.

It's more likely that you want something like:

	char str[20];
	char *args[4];

	sprintf(str, "-Xparameter:%d", value);

	args[0] = path;
	args[1] = str;
	args[2] = command;
	args[3] = NULL;

	result = execv(path, args);

This assumes that "command" is meant to be a single argument, not a list of arguments. If it's a list (e.g. with individual items separated by spaces), you'll have to split it back into a list before passing it to execv().

Signature · 1 line
Glynn Clements <[email protected]>
#5 lore raw
In reply to Prasanta Sadhukhan
Quoted 2 lines · Prasanta Sadhukhan
   if(execv("/bin/ls", "-l") == -1)

hi

from man execv:

int execv(const char *path, char *const argv[]);

see the second argument here is an array of strings, not a string

so you cant just pass a string, you need to pass an array of pointers and it must be terminated by a NULL pointer

-M