mirror of
https://github.com/freebsd/freebsd-src
synced 2024-07-24 03:37:16 +00:00
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
This commit is contained in:
parent
d9db58c9dc
commit
d006e72a51
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=94355
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue