Audit nice(1)
1) val is sufficient as "int" (read the standard) 2) BUGFIX: If getpriority fails, it returns -1 and sets errno. Previously, it would correctly catch the errno but not take care of the fact that by then val has been decremented by 1. Only change val if the getpriority-call has been successful. 3) Add LIMIT()-macro from st to increase readability. 4) setpriority returns < 0 on failure 5) Remove bikeshedding-comment. Read the standard if you wonder. 6) return-value trick from env(1)
This commit is contained in:
19
nice.c
19
nice.c
@@ -16,8 +16,7 @@ usage(void)
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
long val = 10;
|
||||
int savederrno;
|
||||
int val = 10, r, savederrno;
|
||||
|
||||
ARGBEGIN {
|
||||
case 'n':
|
||||
@@ -28,20 +27,22 @@ main(int argc, char *argv[])
|
||||
break;
|
||||
} ARGEND;
|
||||
|
||||
if (argc == 0)
|
||||
if (!argc)
|
||||
usage();
|
||||
|
||||
errno = 0;
|
||||
val += getpriority(PRIO_PROCESS, 0);
|
||||
if (errno != 0)
|
||||
r = getpriority(PRIO_PROCESS, 0);
|
||||
if (errno)
|
||||
weprintf("getpriority:");
|
||||
val = MAX(PRIO_MIN, MIN(val, PRIO_MAX));
|
||||
if (setpriority(PRIO_PROCESS, 0, val) != 0)
|
||||
else
|
||||
val += r;
|
||||
LIMIT(val, PRIO_MIN, PRIO_MAX);
|
||||
if (setpriority(PRIO_PROCESS, 0, val) < 0)
|
||||
weprintf("setpriority:");
|
||||
|
||||
/* POSIX specifies the nice failure still invokes the command */
|
||||
execvp(argv[0], argv);
|
||||
savederrno = errno;
|
||||
weprintf("execvp %s:", argv[0]);
|
||||
return (savederrno == ENOENT)? 127 : 126;
|
||||
|
||||
return 126 + (savederrno == ENOENT);
|
||||
}
|
||||
|
Reference in New Issue
Block a user