Audit nohup(1)

1) no need to include sys/stat.h
2) remove the enum which just added a layer too thick on this simple
   program
3) argc-style, other style
4) weprintf instead of enprintf, then save the error-message of
   execvp before and return the proper status.
5) write consistent "not reached" comment.
This commit is contained in:
FRIGN 2015-03-04 22:39:12 +01:00
parent 4c4e5a3eb2
commit a6ee96af7c
2 changed files with 15 additions and 21 deletions

2
README
View File

@ -50,7 +50,7 @@ The following tools are implemented ('*' == finished, '#' == UTF-8 support,
=* mv yes none (-i) =* mv yes none (-i)
=*| nice yes none =*| nice yes none
= nl no -d, -f, -h, -l, -p = nl no -d, -f, -h, -l, -p
=* nohup yes none =*| nohup yes none
#* paste yes none #* paste yes none
=*| printenv non-posix none =*| printenv non-posix none
#* printf yes none #* printf yes none

34
nohup.c
View File

@ -1,6 +1,4 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
#include <sys/stat.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <signal.h> #include <signal.h>
@ -8,8 +6,6 @@
#include "util.h" #include "util.h"
enum { Error = 127, Found = 126 };
static void static void
usage(void) usage(void)
{ {
@ -19,35 +15,33 @@ usage(void)
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
int fd; int fd, savederrno;
ARGBEGIN { ARGBEGIN {
default: default:
usage(); usage();
} ARGEND; } ARGEND;
if (argc == 0) if (!argc)
usage(); usage();
if (signal(SIGHUP, SIG_IGN) == SIG_ERR) if (signal(SIGHUP, SIG_IGN) == SIG_ERR)
enprintf(Error, "signal HUP:"); enprintf(127, "signal HUP:");
if (isatty(STDOUT_FILENO)) { if (isatty(STDOUT_FILENO)) {
if ((fd = open("nohup.out", O_APPEND|O_CREAT, if ((fd = open("nohup.out", O_APPEND | O_CREAT, S_IRUSR | S_IWUSR)) < 0)
S_IRUSR|S_IWUSR)) < 0) { enprintf(127, "open nohup.out:");
enprintf(Error, "open nohup.out:");
}
if (dup2(fd, STDOUT_FILENO) < 0) if (dup2(fd, STDOUT_FILENO) < 0)
enprintf(Error, "dup2:"); enprintf(127, "dup2:");
close(fd); close(fd);
} }
if (isatty(STDERR_FILENO)) if (isatty(STDERR_FILENO) && dup2(STDOUT_FILENO, STDERR_FILENO) < 0)
if (dup2(STDOUT_FILENO, STDERR_FILENO) < 0) enprintf(127, "dup2:");
enprintf(Error, "dup2:");
execvp(argv[0], &argv[0]); execvp(argv[0], argv);
enprintf(errno == ENOENT ? Error : Found, "exec %s:", argv[0]); savederrno = errno;
_exit(Error); weprintf("exec %s:", argv[0]);
/* unreachable */ _exit(126 + (savederrno == ENOENT));
return 0;
return 0; /* not reached */
} }