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