Files
sbase/renice.c
T

94 lines
1.5 KiB
C
Raw Normal View History

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