Don't return but _exit after failed exec*() and fork()

Quoting POSIX[0]:
"Care should be taken, also, to call _exit() rather than exit() if exec cannot be used, since
exit() flushes and closes standard I/O channels, thereby damaging the parent process' standard
I/O data structures. (Even with fork(), it is wrong to call exit(), since buffered data would
then be flushed twice.)"

[0]: http://pubs.opengroup.org/onlinepubs/009695399/functions/vfork.html
This commit is contained in:
FRIGN
2015-03-09 01:04:34 +01:00
parent 4414a17e1b
commit 6f207dac5f
8 changed files with 30 additions and 22 deletions

View File

@@ -26,7 +26,8 @@ main(int argc, char *argv[])
if (getpgrp() == getpid()) {
switch (fork()) {
case -1:
eprintf("fork:");
weprintf("fork:");
_exit(1);
case 0:
break;
default:
@@ -39,5 +40,5 @@ main(int argc, char *argv[])
savederrno = errno;
weprintf("execvp %s:", argv[0]);
return 126 + (savederrno == ENOENT);
_exit(126 + (savederrno == ENOENT));
}