Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does it make sense to exit(0) after a call to execl()?

Tags:

c

linux

Consider the following code:

close(channel_data->pty_master);

if (login_tty(channel_data->pty_slave) != 0) // new terminal session
{
    exit(1); // fail
}

execl("/bin/sh", "sh", mode, command, NULL); // replace process image
exit(0);

According to the documentation of execl(), the current process image is being replaced and the call returns only on errors.

But why call exit() after a call to execl() if the process image is replaced?

like image 663
Shuzheng Avatar asked Jul 27 '17 12:07

Shuzheng


1 Answers

Exec calls may fail. A common epilogue would be:

perror("Some eror message");
_exit(127); /*or _exit(1); (127 is what shells return) */

You would usually run _exit rather than exit here, in order to skip atexit hooks and stdio buffer flushes.

like image 58
PSkocik Avatar answered Nov 14 '22 22:11

PSkocik