2013-06-11 18:33:52 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2013-08-19 15:32:23 +00:00
|
|
|
#include <sys/resource.h>
|
2013-06-11 18:33:52 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
static int strtop(const char *);
|
|
|
|
static bool renice(int, int, long);
|
2013-10-07 16:13:01 +00:00
|
|
|
static void usage(void);
|
2013-06-11 18:33:52 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
const char *adj = NULL;
|
|
|
|
long val;
|
2013-10-07 15:41:55 +00:00
|
|
|
int i, which = PRIO_PROCESS, status = EXIT_SUCCESS;
|
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();
|
2013-06-11 18:33:52 +00:00
|
|
|
break;
|
|
|
|
} ARGEND;
|
|
|
|
|
|
|
|
if(argc == 0 || !adj)
|
2013-10-07 16:13:01 +00:00
|
|
|
usage();
|
2013-06-11 18:33:52 +00:00
|
|
|
|
|
|
|
val = estrtol(adj, 10);
|
|
|
|
for(i = 0; i < argc; i++) {
|
|
|
|
int who = -1;
|
|
|
|
|
|
|
|
if(which == PRIO_USER) {
|
|
|
|
const struct passwd *pwd;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
do pwd = getpwnam(argv[i]); while(errno == EINTR);
|
|
|
|
|
|
|
|
if(pwd)
|
|
|
|
who = pwd->pw_uid;
|
|
|
|
else if(errno != 0) {
|
|
|
|
perror("can't read passwd");
|
2013-10-07 15:41:55 +00:00
|
|
|
status = EXIT_FAILURE;
|
2013-06-11 18:33:52 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(who < 0)
|
|
|
|
who = strtop(argv[i]);
|
|
|
|
|
|
|
|
if(who < 0 || !renice(which, who, val))
|
2013-10-07 15:41:55 +00:00
|
|
|
status = EXIT_FAILURE;
|
2013-06-11 18:33:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
strtop(const char *s)
|
|
|
|
{
|
|
|
|
char *end;
|
|
|
|
long n;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
n = strtol(s, &end, 10);
|
|
|
|
if(*end != '\0') {
|
|
|
|
fprintf(stderr, "%s: not an integer\n", s);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if(errno != 0 || n <= 0 || n > INT_MAX) {
|
|
|
|
fprintf(stderr, "%s: invalid value\n", s);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (int)n;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
renice(int which, int who, long adj)
|
|
|
|
{
|
|
|
|
errno = 0;
|
|
|
|
adj += getpriority(which, who);
|
|
|
|
if(errno != 0) {
|
|
|
|
fprintf(stderr, "can't get %d nice level: %s\n",
|
|
|
|
who, strerror(errno));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-02-27 14:19:36 +00:00
|
|
|
adj = MAX(-NZERO, MIN(adj, NZERO - 1));
|
2013-06-11 18:33:52 +00:00
|
|
|
if(setpriority(which, who, (int)adj) == -1) {
|
|
|
|
fprintf(stderr, "can't set %d nice level: %s\n",
|
|
|
|
who, strerror(errno));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-10-07 16:13:01 +00:00
|
|
|
usage(void)
|
2013-06-11 18:33:52 +00:00
|
|
|
{
|
|
|
|
eprintf("renice -n inc [-g | -p | -u] ID ...\n");
|
|
|
|
}
|