44
44
#include <stdlib.h>
45
45
#include <string.h>
49
int donice(int,int,int);
50
static int donice(int,int,int);
52
static void __attribute__((__noreturn__)) usage(FILE *out)
53
printf( _("\nUsage:\n"
54
" renice [-n] priority [-p|--pid] pid [... pid]\n"
55
" renice [-n] priority -g|--pgrp pgrp [... pgrp]\n"
56
" renice [-n] priority -u|--user user [... user]\n"
57
" renice -h | --help\n"
58
" renice -v | --version\n\n"));
56
" %1$s [-n] <priority> [-p] <pid> [<pid> ...]\n"
57
" %1$s [-n] <priority> -g <pgrp> [<pgrp> ...]\n"
58
" %1$s [-n] <priority> -u <user> [<user> ...]\n"),
59
program_invocation_short_name);
63
" -g, --pgrp <id> interpret as process group ID\n"
64
" -h, --help print help\n"
65
" -n, --priority <num> set the nice increment value\n"
66
" -p, --pid <id> force to be interpreted as process ID\n"
67
" -u, --user <name|id> interpret as username or user ID\n"
68
" -v, --version print version\n"));
70
fprintf(out, _("\nFor more information see renice(1).\n"));
72
exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
83
95
if (strcmp(*argv, "-h") == 0 ||
84
96
strcmp(*argv, "--help") == 0)
87
99
if (strcmp(*argv, "-v") == 0 ||
88
100
strcmp(*argv, "--version") == 0) {
123
135
register struct passwd *pwd = getpwnam(*argv);
125
137
if (pwd == NULL) {
126
fprintf(stderr, _("renice: %s: unknown user\n"),
138
warnx(_("unknown user %s"), *argv);
130
141
who = pwd->pw_uid;
132
143
who = strtol(*argv, &endptr, 10);
133
144
if (who < 0 || *endptr) {
134
fprintf(stderr, _("renice: %s: bad value\n"),
145
warnx(_("bad value %s"), *argv);
141
151
return errs != 0 ? EXIT_FAILURE : EXIT_SUCCESS;
145
155
donice(int which, int who, int prio) {
146
156
int oldprio, newprio;
157
const char *idtype = _("process ID");
159
if (which == PRIO_USER)
160
idtype = _("user ID");
161
else if (which == PRIO_PGRP)
162
idtype = _("process group ID");
149
165
oldprio = getpriority(which, who);
150
166
if (oldprio == -1 && errno) {
151
fprintf(stderr, "renice: %d: ", who);
152
perror(_("getpriority"));
167
warn(_("failed to get priority for %d (%s)"), who, idtype);
155
170
if (setpriority(which, who, prio) < 0) {
156
fprintf(stderr, "renice: %d: ", who);
157
perror(_("setpriority"));
171
warn(_("failed to set priority for %d (%s)"), who, idtype);
161
175
newprio = getpriority(which, who);
162
176
if (newprio == -1 && errno) {
163
fprintf(stderr, "renice: %d: ", who);
164
perror(_("getpriority"));
177
warn(_("failed to get priority for %d (%s)"), who, idtype);
168
printf(_("%d: old priority %d, new priority %d\n"),
169
who, oldprio, newprio);
181
printf(_("%d (%s) old priority %d, new priority %d\n"),
182
who, idtype, oldprio, newprio);