4

I want to use execl() to start a script that has no execution rights. When done from the commandline, this works fine:

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

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

int main(int argc, char **argv) {
  execl("/bin/sh", "/home/bf/playground/test.sh", 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).

Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195

1 Answers1

6

Try

execl("/bin/sh", "sh", "/home/bf/playground/test.sh", (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.

alk
  • 69,737
  • 10
  • 105
  • 255