2013-06-11 18:33:52 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2015-02-14 20:02:41 +00:00
|
|
|
#include <sys/resource.h>
|
|
|
|
|
2014-11-17 16:32:30 +00:00
|
|
|
#include <errno.h>
|
2013-06-11 18:33:52 +00:00
|
|
|
#include <pwd.h>
|
|
|
|
#include <stdlib.h>
|
2014-11-13 17:29:30 +00:00
|
|
|
|
2013-06-11 18:33:52 +00:00
|
|
|
#include "util.h"
|
|
|
|
|
2015-03-07 14:39:39 +00:00
|
|
|
static int
|
|
|
|
renice(int which, int who, long adj)
|
|
|
|
{
|
|
|
|
errno = 0;
|
|
|
|
adj += getpriority(which, who);
|
2015-03-17 21:15:09 +00:00
|
|
|
if (errno) {
|
2015-03-07 14:39:39 +00:00
|
|
|
weprintf("getpriority %d:", who);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
adj = MAX(PRIO_MIN, MIN(adj, PRIO_MAX));
|
|
|
|
if (setpriority(which, who, (int)adj) < 0) {
|
|
|
|
weprintf("setpriority %d:", who);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-04-22 12:13:51 +00:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2015-03-17 21:15:09 +00:00
|
|
|
eprintf("renice -n num [-g | -p | -u] id ...\n");
|
2014-04-22 12:13:51 +00:00
|
|
|
}
|
2013-06-11 18:33:52 +00:00
|
|
|
|
|
|
|
int
|
2014-04-18 10:51:18 +00:00
|
|
|
main(int argc, char *argv[])
|
2013-06-11 18:33:52 +00:00
|
|
|
{
|
|
|
|
const char *adj = NULL;
|
|
|
|
long val;
|
2015-03-17 21:15:09 +00:00
|
|
|
int which = PRIO_PROCESS, ret = 0;
|
2014-11-17 16:32:30 +00:00
|
|
|
struct passwd *pw;
|
|
|
|
int who;
|
2013-06-11 18:33:52 +00:00
|
|
|
|
|
|
|
ARGBEGIN {
|
|
|
|
case 'n':
|
2013-10-07 16:13:01 +00:00
|
|
|
adj = EARGF(usage());
|
2013-06-11 18:33:52 +00:00
|
|
|
break;
|
|
|
|
case 'g':
|
|
|
|
which = PRIO_PGRP;
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
which = PRIO_PROCESS;
|
|
|
|
break;
|
|
|
|
case 'u':
|
|
|
|
which = PRIO_USER;
|
|
|
|
break;
|
|
|
|
default:
|
2013-10-07 16:13:01 +00:00
|
|
|
usage();
|
2015-11-01 10:16:49 +00:00
|
|
|
} ARGEND
|
2013-06-11 18:33:52 +00:00
|
|
|
|
2015-03-17 21:15:09 +00:00
|
|
|
if (!argc || !adj)
|
2013-10-07 16:13:01 +00:00
|
|
|
usage();
|
2013-06-11 18:33:52 +00:00
|
|
|
|
2015-01-30 15:52:44 +00:00
|
|
|
val = estrtonum(adj, PRIO_MIN, PRIO_MAX);
|
2015-03-17 21:15:09 +00:00
|
|
|
for (; *argv; argc--, argv++) {
|
2014-11-13 17:29:30 +00:00
|
|
|
if (which == PRIO_USER) {
|
2013-06-11 18:33:52 +00:00
|
|
|
errno = 0;
|
2015-03-17 21:15:09 +00:00
|
|
|
if (!(pw = getpwnam(*argv))) {
|
|
|
|
if (errno)
|
|
|
|
weprintf("getpwnam %s:", *argv);
|
2014-11-17 16:32:30 +00:00
|
|
|
else
|
2015-03-17 21:15:09 +00:00
|
|
|
weprintf("getpwnam %s: no user found\n", *argv);
|
|
|
|
ret = 1;
|
2013-06-11 18:33:52 +00:00
|
|
|
continue;
|
|
|
|
}
|
2014-11-17 16:32:30 +00:00
|
|
|
who = pw->pw_uid;
|
2015-03-17 21:15:09 +00:00
|
|
|
} else {
|
|
|
|
who = estrtonum(*argv, 1, INT_MAX);
|
2013-06-11 18:33:52 +00:00
|
|
|
}
|
2015-03-17 21:15:09 +00:00
|
|
|
if (!renice(which, who, val))
|
|
|
|
ret = 1;
|
2013-06-11 18:33:52 +00:00
|
|
|
}
|
|
|
|
|
2015-03-17 21:15:09 +00:00
|
|
|
return ret;
|
2013-06-11 18:33:52 +00:00
|
|
|
}
|