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

@ -36,15 +36,14 @@ main(int argc, char *argv[])
if (argc == 1) { if (argc == 1) {
cmd = *shell; cmd = *shell;
execvp(*shell, shell); execvp(cmd, shell);
} else { } else {
cmd = argv[1]; cmd = argv[1];
execvp(argv[1], argv + 1); execvp(cmd, argv + 1);
} }
savederrno = errno; savederrno = errno;
weprintf("execvp %s:", cmd); weprintf("execvp %s:", cmd);
_exit(126 + (savederrno == ENOENT));
return 0; /* not reached */ _exit(126 + (savederrno == ENOENT));
} }

6
env.c
View File

@ -18,6 +18,8 @@ usage(void)
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
int savederrno;
ARGBEGIN { ARGBEGIN {
case 'i': case 'i':
if (environ) if (environ)
@ -35,7 +37,9 @@ main(int argc, char *argv[])
if (*argv) { if (*argv) {
execvp(*argv, argv); execvp(*argv, argv);
enprintf(126 + (errno == EEXIST), "execvp: %s:", *argv); savederrno = errno;
weprintf("execvp %s:", *argv);
_exit(126 + (savederrno == EEXIST));
} }
for (; environ && *environ; environ++) for (; environ && *environ; environ++)

2
nice.c
View File

@ -44,5 +44,5 @@ main(int argc, char *argv[])
savederrno = errno; savederrno = errno;
weprintf("execvp %s:", argv[0]); weprintf("execvp %s:", argv[0]);
return 126 + (savederrno == ENOENT); _exit(126 + (savederrno == ENOENT));
} }

View File

@ -42,8 +42,7 @@ main(int argc, char *argv[])
execvp(argv[0], argv); execvp(argv[0], argv);
savederrno = errno; savederrno = errno;
weprintf("exec %s:", argv[0]); weprintf("execvp %s:", argv[0]);
_exit(126 + (savederrno == ENOENT));
return 0; /* not reached */ _exit(126 + (savederrno == ENOENT));
} }

View File

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

14
tar.c
View File

@ -55,7 +55,8 @@ decomp(FILE *fp)
pid = fork(); pid = fork();
if (pid < 0) { if (pid < 0) {
eprintf("fork:"); weprintf("fork:");
_exit(1);
} else if (!pid) { } else if (!pid) {
dup2(fileno(fp), 0); dup2(fileno(fp), 0);
dup2(fds[1], 1); dup2(fds[1], 1);
@ -64,12 +65,13 @@ decomp(FILE *fp)
switch (filtermode) { switch (filtermode) {
case 'j': case 'j':
execlp("bzip2", "bzip2", "-cd", (char *)0); execlp("bzip2", "bzip2", "-cd", NULL);
eprintf("execlp bzip2:"); weprintf("execlp bzip2:");
_exit(1);
case 'z': case 'z':
execlp("gzip", "gzip", "-cd", (char *)0); execlp("gzip", "gzip", "-cd", NULL);
eprintf("execlp gzip:"); weprintf("execlp gzip:");
break; _exit(1);
} }
} }
close(fds[1]); close(fds[1]);

5
time.c
View File

@ -41,11 +41,12 @@ main(int argc, char *argv[])
switch ((pid = fork())) { switch ((pid = fork())) {
case -1: case -1:
eprintf("fork:"); weprintf("fork:");
_exit(1);
case 0: case 0:
execvp(argv[0], argv); execvp(argv[0], argv);
savederrno = errno; savederrno = errno;
weprintf("exec %s:", argv[0]); weprintf("execvp %s:", argv[0]);
_exit(126 + (savederrno == ENOENT)); _exit(126 + (savederrno == ENOENT));
default: default:
break; break;

View File

@ -168,13 +168,15 @@ spawn(void)
int savederrno; int savederrno;
pid = fork(); pid = fork();
if (pid < 0) if (pid < 0) {
eprintf("fork:"); weprintf("fork:");
_exit(1);
}
if (pid == 0) { if (pid == 0) {
execvp(*cmd, cmd); execvp(*cmd, cmd);
savederrno = errno; savederrno = errno;
weprintf("execvp %s:", *cmd); weprintf("execvp %s:", *cmd);
_exit(savederrno == ENOENT ? 127 : 126); _exit(126 + (savederrno == ENOENT));
} }
waitchld(); waitchld();
} }