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