summaryrefslogtreecommitdiffstats
path: root/usr.bin
diff options
context:
space:
mode:
authormaxim <maxim@FreeBSD.org>2002-04-10 11:09:46 +0000
committermaxim <maxim@FreeBSD.org>2002-04-10 11:09:46 +0000
commiteae9474ac8def1c247d360480a10871497c07403 (patch)
treed1546bb1f6c1cadbb22b66207b21e42c8eeb2fb3 /usr.bin
parent7b6414ec32f8750ea22f3a5e9839b0226358c3ba (diff)
downloadFreeBSD-src-eae9474ac8def1c247d360480a10871497c07403.zip
FreeBSD-src-eae9474ac8def1c247d360480a10871497c07403.tar.gz
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
Diffstat (limited to 'usr.bin')
-rw-r--r--usr.bin/renice/renice.89
-rw-r--r--usr.bin/renice/renice.c54
2 files changed, 55 insertions, 8 deletions
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 <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);
}
OpenPOWER on IntegriCloud