diff options
Diffstat (limited to 'usr.bin/renice/renice.c')
-rw-r--r-- | usr.bin/renice/renice.c | 54 |
1 files changed, 47 insertions, 7 deletions
diff --git a/usr.bin/renice/renice.c b/usr.bin/renice/renice.c index de6bcf3..f9e5836 100644 --- a/usr.bin/renice/renice.c +++ b/usr.bin/renice/renice.c @@ -51,12 +51,14 @@ static const char rcsid[] = #include <err.h> #include <errno.h> +#include <limits.h> #include <pwd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> -static int donice(int, int, int); +static int donice(int, int, int, int); +static int getnum(const char *, const char *, int *); static void usage(void); /* @@ -68,14 +70,23 @@ int main(int argc, char *argv[]) { struct passwd *pwd; - int errs, prio, which, who; + int errs, incr, prio, which, who; errs = 0; + incr = 0; which = PRIO_PROCESS; who = 0; argc--, argv++; if (argc < 2) usage(); + if (strcmp(*argv, "-n") == 0) { + incr = 1; + argc--, argv++; + if (argc == 0) + usage(); + } + if (getnum("priority", *argv, &prio)) + return (1); prio = atoi(*argv); argc--, argv++; if (prio > PRIO_MAX) @@ -103,19 +114,20 @@ main(int argc, char *argv[]) } who = pwd->pw_uid; } else { - who = atoi(*argv); + if (getnum("pid", *argv, &who)) + continue; if (who < 0) { warnx("%s: bad value", *argv); continue; } } - errs += donice(which, who, prio); + errs += donice(which, who, prio, incr); } exit(errs != 0); } static int -donice(int which, int who, int prio) +donice(int which, int who, int prio, int incr) { int oldprio; @@ -125,6 +137,12 @@ donice(int which, int who, int prio) warn("%d: getpriority", who); return (1); } + if (incr) + prio = oldprio + prio; + if (prio > PRIO_MAX) + prio = PRIO_MAX; + if (prio < PRIO_MIN) + prio = PRIO_MIN; if (setpriority(which, who, prio) < 0) { warn("%d: setpriority", who); return (1); @@ -133,10 +151,32 @@ donice(int which, int who, int prio) return (0); } +static int +getnum(const char *com, const char *str, int *val) +{ + long v; + char *ep; + + errno = 0; + v = strtol(str, &ep, NULL); + if (v < INT_MIN || v > INT_MAX || errno == ERANGE) { + warnx("%s argument %s is out of range.", com, str); + return (1); + } + if (ep == str || *ep != '\0' || errno != 0) { + warnx("Bad %s argument: %s.", com, str); + return (1); + } + + *val = (int)v; + return (0); +} + static void usage() { - fprintf(stderr, -"usage: renice priority [ [ -p ] pids ] [ [ -g ] pgrps ] [ [ -u ] users ]\n"); + fprintf(stderr, "%s\n%s\n", +"usage: renice [priority | [-n incr]] [ [ -p ] pids ] [ [ -g ] pgrps ]", +" [ [ -u ] users ]"); exit(1); } |