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:
parent
4c4e5a3eb2
commit
a6ee96af7c
2
README
2
README
|
@ -50,7 +50,7 @@ The following tools are implemented ('*' == finished, '#' == UTF-8 support,
|
|||
=* mv yes none (-i)
|
||||
=*| nice yes none
|
||||
= nl no -d, -f, -h, -l, -p
|
||||
=* nohup yes none
|
||||
=*| nohup yes none
|
||||
#* paste yes none
|
||||
=*| printenv non-posix none
|
||||
#* printf yes none
|
||||
|
|
34
nohup.c
34
nohup.c
|
@ -1,6 +1,4 @@
|
|||
/* See LICENSE file for copyright and license details. */
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <signal.h>
|
||||
|
@ -8,8 +6,6 @@
|
|||
|
||||
#include "util.h"
|
||||
|
||||
enum { Error = 127, Found = 126 };
|
||||
|
||||
static void
|
||||
usage(void)
|
||||
{
|
||||
|
@ -19,35 +15,33 @@ usage(void)
|
|||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
int fd;
|
||||
int fd, savederrno;
|
||||
|
||||
ARGBEGIN {
|
||||
default:
|
||||
usage();
|
||||
} ARGEND;
|
||||
|
||||
if (argc == 0)
|
||||
if (!argc)
|
||||
usage();
|
||||
|
||||
if (signal(SIGHUP, SIG_IGN) == SIG_ERR)
|
||||
enprintf(Error, "signal HUP:");
|
||||
enprintf(127, "signal HUP:");
|
||||
|
||||
if (isatty(STDOUT_FILENO)) {
|
||||
if ((fd = open("nohup.out", O_APPEND|O_CREAT,
|
||||
S_IRUSR|S_IWUSR)) < 0) {
|
||||
enprintf(Error, "open nohup.out:");
|
||||
}
|
||||
if ((fd = open("nohup.out", O_APPEND | O_CREAT, S_IRUSR | S_IWUSR)) < 0)
|
||||
enprintf(127, "open nohup.out:");
|
||||
if (dup2(fd, STDOUT_FILENO) < 0)
|
||||
enprintf(Error, "dup2:");
|
||||
enprintf(127, "dup2:");
|
||||
close(fd);
|
||||
}
|
||||
if (isatty(STDERR_FILENO))
|
||||
if (dup2(STDOUT_FILENO, STDERR_FILENO) < 0)
|
||||
enprintf(Error, "dup2:");
|
||||
if (isatty(STDERR_FILENO) && dup2(STDOUT_FILENO, STDERR_FILENO) < 0)
|
||||
enprintf(127, "dup2:");
|
||||
|
||||
execvp(argv[0], &argv[0]);
|
||||
enprintf(errno == ENOENT ? Error : Found, "exec %s:", argv[0]);
|
||||
_exit(Error);
|
||||
/* unreachable */
|
||||
return 0;
|
||||
execvp(argv[0], argv);
|
||||
savederrno = errno;
|
||||
weprintf("exec %s:", argv[0]);
|
||||
_exit(126 + (savederrno == ENOENT));
|
||||
|
||||
return 0; /* not reached */
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user