]>
Commit | Line | Data |
---|---|---|
6dbe3af9 | 1 | /* |
9abd5e4b KZ |
2 | * SPDX-License-Identifier: BSD-4-Clause-UC |
3 | * | |
6dbe3af9 KZ |
4 | * Copyright (c) 1983, 1989, 1993 |
5 | * The Regents of the University of California. All rights reserved. | |
6 | * | |
7 | * Redistribution and use in source and binary forms, with or without | |
8 | * modification, are permitted provided that the following conditions | |
9 | * are met: | |
10 | * 1. Redistributions of source code must retain the above copyright | |
11 | * notice, this list of conditions and the following disclaimer. | |
12 | * 2. Redistributions in binary form must reproduce the above copyright | |
13 | * notice, this list of conditions and the following disclaimer in the | |
14 | * documentation and/or other materials provided with the distribution. | |
15 | * 3. All advertising materials mentioning features or use of this software | |
16 | * must display the following acknowledgement: | |
17 | * This product includes software developed by the University of | |
18 | * California, Berkeley and its contributors. | |
19 | * 4. Neither the name of the University nor the names of its contributors | |
20 | * may be used to endorse or promote products derived from this software | |
21 | * without specific prior written permission. | |
22 | * | |
23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
33 | * SUCH DAMAGE. | |
34 | */ | |
35 | ||
b50945d4 | 36 | /* 1999-02-22 Arkadiusz MiĆkiewicz <misiek@pld.ORG.PL> |
7eda085c KZ |
37 | * - added Native Language Support |
38 | */ | |
39 | ||
6dbe3af9 KZ |
40 | #include <sys/types.h> |
41 | #include <sys/time.h> | |
42 | #include <sys/resource.h> | |
43 | ||
44 | #include <stdio.h> | |
45 | #include <pwd.h> | |
fd6b7a7f | 46 | #include <stdlib.h> |
66ee8158 | 47 | #include <string.h> |
7eda085c KZ |
48 | #include <errno.h> |
49 | #include "nls.h" | |
eb76ca98 | 50 | #include "c.h" |
efb8854f | 51 | #include "closestream.h" |
fd6b7a7f | 52 | |
2ba641e5 | 53 | static const char *idtype[] = { |
ce4030e4 SK |
54 | [PRIO_PROCESS] = N_("process ID"), |
55 | [PRIO_PGRP] = N_("process group ID"), | |
56 | [PRIO_USER] = N_("user ID"), | |
57 | }; | |
58 | ||
9325dbfd | 59 | static void __attribute__((__noreturn__)) usage(void) |
628cab7c | 60 | { |
9325dbfd | 61 | FILE *out = stdout; |
6dcc066f | 62 | fputs(USAGE_HEADER, out); |
ae1ae49e | 63 | fprintf(out, |
56ce8f61 DA |
64 | _(" %1$s [-n|--priority|--relative] <priority> [-p|--pid] <pid>...\n" |
65 | " %1$s [-n|--priority|--relative] <priority> -g|--pgrp <pgid>...\n" | |
66 | " %1$s [-n|--priority|--relative] <priority> -u|--user <user>...\n"), | |
296351b0 | 67 | program_invocation_short_name); |
451dbcfa BS |
68 | |
69 | fputs(USAGE_SEPARATOR, out); | |
70 | fputs(_("Alter the priority of running processes.\n"), out); | |
71 | ||
6dcc066f | 72 | fputs(USAGE_OPTIONS, out); |
56ce8f61 DA |
73 | fputs(_(" -n <num> specify the nice value\n"), out); |
74 | fputs(_(" If POSIXLY_CORRECT flag is set in environment\n"), out); | |
37c6b24c DA |
75 | fputs(_(" then the priority is 'relative' to current\n"), out); |
76 | fputs(_(" process priority. Otherwise it is 'absolute'.\n"), out); | |
56ce8f61 DA |
77 | fputs(_(" --priority <num> specify the 'absolute' nice value\n"), out); |
78 | fputs(_(" --relative <num> specify the 'relative' nice value\n"), out); | |
390ba85c KZ |
79 | fputs(_(" -p, --pid interpret arguments as process ID (default)\n"), out); |
80 | fputs(_(" -g, --pgrp interpret arguments as process group ID\n"), out); | |
81 | fputs(_(" -u, --user interpret arguments as username or user ID\n"), out); | |
94b1623b | 82 | fputs(USAGE_SEPARATOR, out); |
bad4c729 MY |
83 | fprintf(out, USAGE_HELP_OPTIONS(24)); |
84 | fprintf(out, USAGE_MAN_TAIL("renice(1)")); | |
9325dbfd | 85 | exit(EXIT_SUCCESS); |
628cab7c | 86 | } |
7cfbafda | 87 | |
ce4030e4 | 88 | static int getprio(const int which, const int who, int *prio) |
00b490f0 SK |
89 | { |
90 | errno = 0; | |
91 | *prio = getpriority(which, who); | |
92 | if (*prio == -1 && errno) { | |
ce4030e4 | 93 | warn(_("failed to get priority for %d (%s)"), who, idtype[which]); |
00b490f0 SK |
94 | return -errno; |
95 | } | |
96 | return 0; | |
97 | } | |
98 | ||
56ce8f61 | 99 | static int donice(const int which, const int who, const int prio, const int relative) |
00b490f0 | 100 | { |
5ebcab66 | 101 | int oldprio, newprio; |
5ebcab66 | 102 | |
ce4030e4 | 103 | if (getprio(which, who, &oldprio) != 0) |
5ebcab66 | 104 | return 1; |
bad4c729 | 105 | |
37c6b24c | 106 | newprio = prio; // if not relative, set absolute priority |
56ce8f61 | 107 | |
56ce8f61 | 108 | if (relative) |
37c6b24c | 109 | newprio = oldprio + prio; |
56ce8f61 DA |
110 | |
111 | if (setpriority(which, who, newprio) < 0) { | |
ce4030e4 | 112 | warn(_("failed to set priority for %d (%s)"), who, idtype[which]); |
5ebcab66 SK |
113 | return 1; |
114 | } | |
ce4030e4 | 115 | if (getprio(which, who, &newprio) != 0) |
5ebcab66 | 116 | return 1; |
5ebcab66 | 117 | printf(_("%d (%s) old priority %d, new priority %d\n"), |
ce4030e4 | 118 | who, idtype[which], oldprio, newprio); |
5ebcab66 SK |
119 | return 0; |
120 | } | |
121 | ||
6dbe3af9 | 122 | /* |
5cc224f5 BS |
123 | * Change the priority (the nice value) of processes |
124 | * or groups of processes which are already running. | |
6dbe3af9 | 125 | */ |
00b490f0 | 126 | int main(int argc, char **argv) |
6dbe3af9 KZ |
127 | { |
128 | int which = PRIO_PROCESS; | |
129 | int who = 0, prio, errs = 0; | |
56ce8f61 | 130 | int relative = 0; |
628cab7c | 131 | char *endptr = NULL; |
6dbe3af9 | 132 | |
7eda085c KZ |
133 | setlocale(LC_ALL, ""); |
134 | bindtextdomain(PACKAGE, LOCALEDIR); | |
135 | textdomain(PACKAGE); | |
2c308875 | 136 | close_stdout_atexit(); |
7eda085c | 137 | |
628cab7c LJ |
138 | argc--; |
139 | argv++; | |
140 | ||
141 | if (argc == 1) { | |
142 | if (strcmp(*argv, "-h") == 0 || | |
143 | strcmp(*argv, "--help") == 0) | |
9325dbfd | 144 | usage(); |
628cab7c LJ |
145 | |
146 | if (strcmp(*argv, "-v") == 0 || | |
5cc224f5 | 147 | strcmp(*argv, "-V") == 0 || |
2c308875 KZ |
148 | strcmp(*argv, "--version") == 0) |
149 | print_version(EXIT_SUCCESS); | |
6dbe3af9 | 150 | } |
628cab7c | 151 | |
56ce8f61 DA |
152 | if (*argv) |
153 | { | |
154 | if (strcmp(*argv, "-n") == 0) | |
155 | { | |
156 | // Fully conform to posix only if POSIXLY_CORRECT is | |
157 | // set in the environment. If not, use the absolute | |
158 | // value as it's been used (incorrectly) since 2009 | |
159 | relative = getenv("POSIXLY_CORRECT") == NULL ? 0 : 1; | |
160 | argc--; | |
161 | argv++; | |
162 | } | |
163 | else if (strcmp(*argv, "--relative") == 0) | |
164 | { | |
165 | relative = 1; | |
166 | argc--; | |
167 | argv++; | |
168 | } | |
169 | else if (strcmp(*argv, "--priority") == 0) { | |
170 | relative = 0; | |
171 | argc--; | |
172 | argv++; | |
173 | } | |
7cfbafda KZ |
174 | } |
175 | ||
9f74aa07 | 176 | if (argc < 2 || !*argv) { |
9325dbfd RM |
177 | warnx(_("not enough arguments")); |
178 | errtryhelp(EXIT_FAILURE); | |
179 | } | |
0bb01bb0 | 180 | |
628cab7c | 181 | prio = strtol(*argv, &endptr, 10); |
9325dbfd | 182 | if (*endptr) { |
e2215a85 | 183 | warnx(_("invalid priority '%s'"), *argv); |
9325dbfd RM |
184 | errtryhelp(EXIT_FAILURE); |
185 | } | |
628cab7c LJ |
186 | argc--; |
187 | argv++; | |
188 | ||
6dbe3af9 | 189 | for (; argc > 0; argc--, argv++) { |
628cab7c | 190 | if (strcmp(*argv, "-g") == 0 || strcmp(*argv, "--pgrp") == 0) { |
6dbe3af9 KZ |
191 | which = PRIO_PGRP; |
192 | continue; | |
193 | } | |
628cab7c | 194 | if (strcmp(*argv, "-u") == 0 || strcmp(*argv, "--user") == 0) { |
6dbe3af9 KZ |
195 | which = PRIO_USER; |
196 | continue; | |
197 | } | |
628cab7c | 198 | if (strcmp(*argv, "-p") == 0 || strcmp(*argv, "--pid") == 0) { |
6dbe3af9 KZ |
199 | which = PRIO_PROCESS; |
200 | continue; | |
201 | } | |
202 | if (which == PRIO_USER) { | |
80ca9e20 | 203 | struct passwd *pwd = getpwnam(*argv); |
628cab7c | 204 | |
80ca9e20 SK |
205 | if (pwd != NULL) |
206 | who = pwd->pw_uid; | |
207 | else | |
208 | who = strtol(*argv, &endptr, 10); | |
209 | if (who < 0 || *endptr) { | |
40cebc2d | 210 | warnx(_("unknown user %s"), *argv); |
665b3c85 | 211 | errs = 1; |
6dbe3af9 KZ |
212 | continue; |
213 | } | |
6dbe3af9 | 214 | } else { |
628cab7c LJ |
215 | who = strtol(*argv, &endptr, 10); |
216 | if (who < 0 || *endptr) { | |
09638694 BS |
217 | /* TRANSLATORS: The first %s is one of the above |
218 | * three ID names. Read: "bad value for %s: %s" */ | |
ce4030e4 | 219 | warnx(_("bad %s value: %s"), idtype[which], *argv); |
665b3c85 | 220 | errs = 1; |
6dbe3af9 KZ |
221 | continue; |
222 | } | |
223 | } | |
56ce8f61 | 224 | errs |= donice(which, who, prio, relative); |
6dbe3af9 | 225 | } |
628cab7c | 226 | return errs != 0 ? EXIT_FAILURE : EXIT_SUCCESS; |
6dbe3af9 | 227 | } |