Bart Friederichs Bart Friederichs - 23 days ago 5x
C Question

Running a script from execl()

I want to use

to start a script that has no execution rights. When done from the commandline, this works fine:

[bf@bf-laptop-tbwb playground]$ /bin/sh
I run !

However, when I want to use
from C, it just starts another shell, without running my script.

int main(int argc, char **argv) {
execl("/bin/sh", "/home/bf/playground/", NULL);
return 0;

I cannot just run the script, because I have no guarantee the script is executable (it is on an embedded device, that gets loaded with FTP scripts).

alk alk


execl("/bin/sh", "sh", "/home/bf/playground/", (char *) NULL);
/* exec*()-functions do not return on success, so we only get here in case of error. */
perror("execl() failed");

From man 3 exec

The initial argument for these functions is the name of a file that is to be executed.

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

arg0 is equivalent to arg[0] which is the program's name. The 1st argument to a program is arg[1].

Also please note (further down of exec*()'s 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.