2

In a book about Linux and Unix programming, I´ve found this statement (emphasize mine):

"All functions (exec()-family) return -1 in the case of an error. Otherwise at successful execution there is no return back to the calling program. Thus, it is redundant to check the return value; you can directly continue with the error routine."

This seems to fit with the according Linux man page:

"RETURN VALUE - The exec() functions return only if an error has occurred. The return value is -1, and errno is set to indicate the error."

Thus, If the current process image was successfully replaced with a new process image, there should be no return. Only if there was happen an error at the creation of the new image, one of the exec()-functions actually returns (-1).


Means, in turn, instead of, f.e.:

if ( execl("/bin/foo", "foo", "bar", (char *) NULL) == -1 )
{
    perror("Execution of foo failed");
    // further error routine.
}

One can simply do:

execl("/bin/foo", "foo", "bar", (char *) NULL);

perror("Execution of foo failed");
// further error routine.

  • But why do the functions of the exec()-family have a return value then in general, if the further program execution of the caller already indicates that an error has happen?

  • Wouldn´t a simple return; statement at the end of the functions and a respective return type of void be sufficient?

  • Is it just to fit the common method/practice to always checking return values or has it a different purpose?


Related:

  • Consistency with other syscalls? – Shawn May 21 '20 at 09:22
  • 2
    Yes, a return from an `exec*` function is a sufficient indication of an error. I suppose those functions return an `int` for historical reasons, since `int` used to be an implicit value type for old C functions and the void return type didn't exist for several years while C was already being used to implement UNIX. – Petr Skocik May 21 '20 at 09:23
  • @PSkocik That was even way before C89/C90. No need to adjust the functions to the standard? Or is this for compatibility with pre-standard C systems? – RobertS supports Monica Cellio May 21 '20 at 09:32
  • AFAIK, the first standards literally promised compatibility and codification of existing practices rather than design by committee. (The later standards have been breaking the latter promise more and more.) – Petr Skocik May 21 '20 at 09:35

0 Answers0