From eae9474ac8def1c247d360480a10871497c07403 Mon Sep 17 00:00:00 2001 From: maxim Date: Wed, 10 Apr 2002 11:09:46 +0000 Subject: Implement POSIX -n option, cleanup an arguments parsing a bit. PR: bin/34076, bin/35929 Reviewed by: bde Obtained from: NetBSD MFC after: 1 week --- usr.bin/renice/renice.8 | 9 ++++++++- usr.bin/renice/renice.c | 54 ++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 55 insertions(+), 8 deletions(-) (limited to 'usr.bin') diff --git a/usr.bin/renice/renice.8 b/usr.bin/renice/renice.8 index ff3d484..56d431e 100644 --- a/usr.bin/renice/renice.8 +++ b/usr.bin/renice/renice.8 @@ -40,7 +40,10 @@ .Nd alter priority of running processes .Sh SYNOPSIS .Nm -.Ar priority +.Oo +.Ar priority | +.Op Fl n Ar increment +.Oc .Oo .Op Fl p .Ar pid ... @@ -77,6 +80,10 @@ Options supported by Force .Ar who parameters to be interpreted as process group ID's. +.It Fl n +Instead of changing the specified processes to the given priority, +interpret the following argument as an increment to be applied to +the current priority of each process. .It Fl u Force the .Ar who 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 #include +#include #include #include #include #include -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); } -- cgit v1.1