Learn more about Russian war crimes in Ukraine.

How do I use execve in C?

The execve system call is part of how new processes are created in UNIX. A call to execve replaces the current process image with a new process image. Example:

#include <stdio.h>
#include <unistd.h>
int main(void) {
  printf("Main program started\n");
  char* argv[] = { "jim", "jams", NULL };
  char* envp[] = { "some", "environment", NULL };
  if (execve("./sub", argv, envp) == -1)
    perror("Could not execve");
  return 1;
}

Running this program starts a process which prints some text, then replaces itself with a different program. In this example, the program is given by "./sub", a path to the program. After executing execve("./sub", ...), the process behaves like the program ./sub. Here’s the output, with a ./sub program which prints its arguments and environment:

% cc main.c -o main
% ./main
Main program started
Sub program started with args: jim jams
Sub program environment:
some
environment

The full call is execve(path, argv, env). The second argument provides the process’s argument array (the argv seen in the second argument to main in the ./sub program). The third argument provides the process’s environment (the environ seen by the ./sub program).

The argv and envp values are deliberately strange in this example. It is idiomatic to pass the program’s name as its first argument, but, as this shows, this idiom can be broken: the program is "./sub", but we pass the first argument "jim". Thus the sub program never knows that it was executed from the program at "./sub". The envp is strange because is it idiomatic to pass key-value strings in the environment, such as "FOO=bar". Again, this is just an idiom, and it can be broken.

The execve call ordinarily never returns, since the process that called it is replaced. It only returns if it fails, in which case it returns -1.

Notice that execve does not create a new process! So, how then can we get from one process to two, and so on? The answer is fork - another system call, which I cover in the next post.

What can computers do? What are the limits of mathematics? And just how busy can a busy beaver be? This year, I’m writing Busy Beavers, a unique interactive book on computability theory. You and I will take a practical and modern approach to answering these questions — or at least learning why some questions are unanswerable!

It’s only $19, and you can get 50% off if you find the discount code ... Not quite. Hackers use the console!

After months of secret toil, I and Andrew Carr released Everyday Data Science, a unique interactive online course! You’ll make the perfect glass of lemonade using Thompson sampling. You’ll lose weight with differential equations. And you might just qualify for the Olympics with a bit of statistics!

It’s $29, but you can get 50% off if you find the discount code ... Not quite. Hackers use the console!

More by Jim

Tagged #programming, #c. All content copyright James Fisher 2017. This post is not associated with my employer. Found an error? Edit this page.