]> git.ipfire.org Git - thirdparty/util-linux.git/blob - sys-utils/renice.c
sys-utils: cleanup license lines, add SPDX
[thirdparty/util-linux.git] / sys-utils / renice.c
1 /*
2 * SPDX-License-Identifier: BSD-4-Clause-UC
3 *
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
36 /* 1999-02-22 Arkadiusz Miƛkiewicz <misiek@pld.ORG.PL>
37 * - added Native Language Support
38 */
39
40 #include <sys/types.h>
41 #include <sys/time.h>
42 #include <sys/resource.h>
43
44 #include <stdio.h>
45 #include <pwd.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <errno.h>
49 #include "nls.h"
50 #include "c.h"
51 #include "closestream.h"
52
53 static const char *idtype[] = {
54 [PRIO_PROCESS] = N_("process ID"),
55 [PRIO_PGRP] = N_("process group ID"),
56 [PRIO_USER] = N_("user ID"),
57 };
58
59 static void __attribute__((__noreturn__)) usage(void)
60 {
61 FILE *out = stdout;
62 fputs(USAGE_HEADER, out);
63 fprintf(out,
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"),
67 program_invocation_short_name);
68
69 fputs(USAGE_SEPARATOR, out);
70 fputs(_("Alter the priority of running processes.\n"), out);
71
72 fputs(USAGE_OPTIONS, out);
73 fputs(_(" -n <num> specify the nice value\n"), out);
74 fputs(_(" If POSIXLY_CORRECT flag is set in environment\n"), out);
75 fputs(_(" then the priority is 'relative' to current\n"), out);
76 fputs(_(" process priority. Otherwise it is 'absolute'.\n"), out);
77 fputs(_(" --priority <num> specify the 'absolute' nice value\n"), out);
78 fputs(_(" --relative <num> specify the 'relative' nice value\n"), out);
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);
82 fputs(USAGE_SEPARATOR, out);
83 fprintf(out, USAGE_HELP_OPTIONS(24));
84 fprintf(out, USAGE_MAN_TAIL("renice(1)"));
85 exit(EXIT_SUCCESS);
86 }
87
88 static int getprio(const int which, const int who, int *prio)
89 {
90 errno = 0;
91 *prio = getpriority(which, who);
92 if (*prio == -1 && errno) {
93 warn(_("failed to get priority for %d (%s)"), who, idtype[which]);
94 return -errno;
95 }
96 return 0;
97 }
98
99 static int donice(const int which, const int who, const int prio, const int relative)
100 {
101 int oldprio, newprio;
102
103 if (getprio(which, who, &oldprio) != 0)
104 return 1;
105
106 newprio = prio; // if not relative, set absolute priority
107
108 if (relative)
109 newprio = oldprio + prio;
110
111 if (setpriority(which, who, newprio) < 0) {
112 warn(_("failed to set priority for %d (%s)"), who, idtype[which]);
113 return 1;
114 }
115 if (getprio(which, who, &newprio) != 0)
116 return 1;
117 printf(_("%d (%s) old priority %d, new priority %d\n"),
118 who, idtype[which], oldprio, newprio);
119 return 0;
120 }
121
122 /*
123 * Change the priority (the nice value) of processes
124 * or groups of processes which are already running.
125 */
126 int main(int argc, char **argv)
127 {
128 int which = PRIO_PROCESS;
129 int who = 0, prio, errs = 0;
130 int relative = 0;
131 char *endptr = NULL;
132
133 setlocale(LC_ALL, "");
134 bindtextdomain(PACKAGE, LOCALEDIR);
135 textdomain(PACKAGE);
136 close_stdout_atexit();
137
138 argc--;
139 argv++;
140
141 if (argc == 1) {
142 if (strcmp(*argv, "-h") == 0 ||
143 strcmp(*argv, "--help") == 0)
144 usage();
145
146 if (strcmp(*argv, "-v") == 0 ||
147 strcmp(*argv, "-V") == 0 ||
148 strcmp(*argv, "--version") == 0)
149 print_version(EXIT_SUCCESS);
150 }
151
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 }
174 }
175
176 if (argc < 2 || !*argv) {
177 warnx(_("not enough arguments"));
178 errtryhelp(EXIT_FAILURE);
179 }
180
181 prio = strtol(*argv, &endptr, 10);
182 if (*endptr) {
183 warnx(_("invalid priority '%s'"), *argv);
184 errtryhelp(EXIT_FAILURE);
185 }
186 argc--;
187 argv++;
188
189 for (; argc > 0; argc--, argv++) {
190 if (strcmp(*argv, "-g") == 0 || strcmp(*argv, "--pgrp") == 0) {
191 which = PRIO_PGRP;
192 continue;
193 }
194 if (strcmp(*argv, "-u") == 0 || strcmp(*argv, "--user") == 0) {
195 which = PRIO_USER;
196 continue;
197 }
198 if (strcmp(*argv, "-p") == 0 || strcmp(*argv, "--pid") == 0) {
199 which = PRIO_PROCESS;
200 continue;
201 }
202 if (which == PRIO_USER) {
203 struct passwd *pwd = getpwnam(*argv);
204
205 if (pwd != NULL)
206 who = pwd->pw_uid;
207 else
208 who = strtol(*argv, &endptr, 10);
209 if (who < 0 || *endptr) {
210 warnx(_("unknown user %s"), *argv);
211 errs = 1;
212 continue;
213 }
214 } else {
215 who = strtol(*argv, &endptr, 10);
216 if (who < 0 || *endptr) {
217 /* TRANSLATORS: The first %s is one of the above
218 * three ID names. Read: "bad value for %s: %s" */
219 warnx(_("bad %s value: %s"), idtype[which], *argv);
220 errs = 1;
221 continue;
222 }
223 }
224 errs |= donice(which, who, prio, relative);
225 }
226 return errs != 0 ? EXIT_FAILURE : EXIT_SUCCESS;
227 }