0

The man page of my linux (Ubuntu), does not mention the need of NULL as last parameter, the man page of a minix version execl mentions the need of NULL as last parameter. Yet, I had strange behavior while invoking modprobe that I solved putting NULL as last parameter.

The way that worked for me:

execl("/sbin/modprobe","modprobe","pl2303",NULL);

While if I do it this way:

execl("/sbin/modprobe","modprobe","pl2303")

I get an error no 14 (EFAULT).`

Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
André Puel
  • 8,741
  • 9
  • 52
  • 83

3 Answers3

6

The man page on my Linux does say it explicitly:

The const char *arg and subsequent ellipses in the execl(), execlp(), and execle() functions can be thought of as arg0, arg1, ..., argn. [....]

The list of arguments must be terminated by a NULL pointer, and, since these are variadic functions, this pointer must be cast (char *) NULL.

With all due respect to Linux and Minix, the more authoritative page I think is the one from opengroup, which says:

The arguments represented by arg0,... are pointers to null-terminated character strings. These strings shall constitute the argument list available to the new process image. The list is terminated by a null pointer.

cnicutar
  • 178,505
  • 25
  • 365
  • 392
  • I though it was refering just to the execv version of the command. But know I see it repeats this statement in the execl paragraph. – André Puel Mar 21 '14 at 23:03
2

The first argument is the path of the executable and the following arguments are actually the argv of the executed program. The list of these arguments is terminated by a (char*)0

Citing from the man page:

The list of arguments must be terminated by a null pointer, and, since these are variadic functions, this pointer must be cast (char *) NULL.

Also references: https://stackoverflow.com/a/12677236/1938163 , http://man7.org/linux/man-pages/man3/exec.3.html

Community
  • 1
  • 1
Marco A.
  • 43,032
  • 26
  • 132
  • 246
2

Yes, it does, the ISO C standard mandates for main that argv[argc] should be NULL to allow for programs to either use argc or a sentinel at the end, and the same reasoning applies to the exec-like calls.

The POSIX docs for this can be found at http://pubs.opengroup.org/onlinepubs/9699919799/functions/exec.html and they state:

The argv and environ arrays are each terminated by a null pointer. The null pointer terminating the argv array is not counted in argc.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953