linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* execv fails with EFAULT
@ 2007-03-07 11:54 Prasanta Sadhukhan
  2007-03-07 12:46 ` Steve Graegert
  2007-03-07 14:20 ` Mathieu Dube
  0 siblings, 2 replies; 6+ messages in thread
From: Prasanta Sadhukhan @ 2007-03-07 11:54 UTC (permalink / raw)
  To: linux-c-programming

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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: execv fails with EFAULT
  2007-03-07 11:54 execv fails with EFAULT Prasanta Sadhukhan
@ 2007-03-07 12:46 ` Steve Graegert
  2007-03-07 13:14   ` Prasanta Sadhukhan
  2007-03-07 14:20 ` Mathieu Dube
  1 sibling, 1 reply; 6+ messages in thread
From: Steve Graegert @ 2007-03-07 12:46 UTC (permalink / raw)
  To: Prasanta Sadhukhan; +Cc: linux-c-programming

On 3/7/07, Prasanta Sadhukhan <Prasanta.Sadhukhan@sun.com> wrote:
> 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 <steve@graegert.com>
Jabber    xmpp://graegerts@jabber.org
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 majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: execv fails with EFAULT
  2007-03-07 12:46 ` Steve Graegert
@ 2007-03-07 13:14   ` Prasanta Sadhukhan
  2007-03-07 13:50     ` Steve Graegert
  2007-03-07 16:58     ` Glynn Clements
  0 siblings, 2 replies; 6+ messages in thread
From: Prasanta Sadhukhan @ 2007-03-07 13:14 UTC (permalink / raw)
  To: Steve Graegert; +Cc: linux-c-programming

Steve Graegert wrote:

> On 3/7/07, Prasanta Sadhukhan <Prasanta.Sadhukhan@sun.com> wrote:
>
>> 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

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

>
> -- 
>
> Steve Grägert <steve@graegert.com>
> Jabber    xmpp://graegerts@jabber.org
> 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 majordomo@vger.kernel.org
> 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 majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: execv fails with EFAULT
  2007-03-07 13:14   ` Prasanta Sadhukhan
@ 2007-03-07 13:50     ` Steve Graegert
  2007-03-07 16:58     ` Glynn Clements
  1 sibling, 0 replies; 6+ messages in thread
From: Steve Graegert @ 2007-03-07 13:50 UTC (permalink / raw)
  To: Prasanta Sadhukhan; +Cc: linux-c-programming

On 3/7/07, Prasanta Sadhukhan <Prasanta.Sadhukhan@sun.com> wrote:
> Steve Graegert wrote:
>
> > On 3/7/07, Prasanta Sadhukhan <Prasanta.Sadhukhan@sun.com> wrote:
> >
> >> 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
>
> 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 <steve@graegert.com>
Jabber    xmpp://graegerts@jabber.org
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 majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: execv fails with EFAULT
  2007-03-07 11:54 execv fails with EFAULT Prasanta Sadhukhan
  2007-03-07 12:46 ` Steve Graegert
@ 2007-03-07 14:20 ` Mathieu Dube
  1 sibling, 0 replies; 6+ messages in thread
From: Mathieu Dube @ 2007-03-07 14:20 UTC (permalink / raw)
  To: Prasanta Sadhukhan; +Cc: linux-c-programming

On Wed, Mar 07, 2007 at 05:24:32PM +0530, Prasanta Sadhukhan wrote:
>    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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: execv fails with EFAULT
  2007-03-07 13:14   ` Prasanta Sadhukhan
  2007-03-07 13:50     ` Steve Graegert
@ 2007-03-07 16:58     ` Glynn Clements
  1 sibling, 0 replies; 6+ messages in thread
From: Glynn Clements @ 2007-03-07 16:58 UTC (permalink / raw)
  To: Prasanta Sadhukhan; +Cc: Steve Graegert, linux-c-programming


Prasanta Sadhukhan wrote:

> >> 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
> 
> 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().

-- 
Glynn Clements <glynn@gclements.plus.com>

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2007-03-07 16:58 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-03-07 11:54 execv fails with EFAULT Prasanta Sadhukhan
2007-03-07 12:46 ` Steve Graegert
2007-03-07 13:14   ` Prasanta Sadhukhan
2007-03-07 13:50     ` Steve Graegert
2007-03-07 16:58     ` Glynn Clements
2007-03-07 14:20 ` Mathieu Dube

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).