2 * SPDX-License-Identifier: BSD-4-Clause-UC
4 * Copyright (c) 1983, 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
36 /* 1999-02-22 Arkadiusz MiĆkiewicz <misiek@pld.ORG.PL>
37 * - added Native Language Support
40 #include <sys/types.h>
42 #include <sys/resource.h>
51 #include "closestream.h"
53 static const char *idtype
[] = {
54 [PRIO_PROCESS
] = N_("process ID"),
55 [PRIO_PGRP
] = N_("process group ID"),
56 [PRIO_USER
] = N_("user ID"),
59 static void __attribute__((__noreturn__
)) usage(void)
62 fputs(USAGE_HEADER
, 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
);
69 fputs(USAGE_SEPARATOR
, out
);
70 fputs(_("Alter the priority of running processes.\n"), out
);
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)"));
88 static int getprio(const int which
, const int who
, int *prio
)
91 *prio
= getpriority(which
, who
);
92 if (*prio
== -1 && errno
) {
93 warn(_("failed to get priority for %d (%s)"), who
, idtype
[which
]);
99 static int donice(const int which
, const int who
, const int prio
, const int relative
)
101 int oldprio
, newprio
;
103 if (getprio(which
, who
, &oldprio
) != 0)
106 newprio
= prio
; // if not relative, set absolute priority
109 newprio
= oldprio
+ prio
;
111 if (setpriority(which
, who
, newprio
) < 0) {
112 warn(_("failed to set priority for %d (%s)"), who
, idtype
[which
]);
115 if (getprio(which
, who
, &newprio
) != 0)
117 printf(_("%d (%s) old priority %d, new priority %d\n"),
118 who
, idtype
[which
], oldprio
, newprio
);
123 * Change the priority (the nice value) of processes
124 * or groups of processes which are already running.
126 int main(int argc
, char **argv
)
128 int which
= PRIO_PROCESS
;
129 int who
= 0, prio
, errs
= 0;
133 setlocale(LC_ALL
, "");
134 bindtextdomain(PACKAGE
, LOCALEDIR
);
136 close_stdout_atexit();
142 if (strcmp(*argv
, "-h") == 0 ||
143 strcmp(*argv
, "--help") == 0)
146 if (strcmp(*argv
, "-v") == 0 ||
147 strcmp(*argv
, "-V") == 0 ||
148 strcmp(*argv
, "--version") == 0)
149 print_version(EXIT_SUCCESS
);
154 if (strcmp(*argv
, "-n") == 0)
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;
163 else if (strcmp(*argv
, "--relative") == 0)
169 else if (strcmp(*argv
, "--priority") == 0) {
176 if (argc
< 2 || !*argv
) {
177 warnx(_("not enough arguments"));
178 errtryhelp(EXIT_FAILURE
);
181 prio
= strtol(*argv
, &endptr
, 10);
183 warnx(_("invalid priority '%s'"), *argv
);
184 errtryhelp(EXIT_FAILURE
);
189 for (; argc
> 0; argc
--, argv
++) {
190 if (strcmp(*argv
, "-g") == 0 || strcmp(*argv
, "--pgrp") == 0) {
194 if (strcmp(*argv
, "-u") == 0 || strcmp(*argv
, "--user") == 0) {
198 if (strcmp(*argv
, "-p") == 0 || strcmp(*argv
, "--pid") == 0) {
199 which
= PRIO_PROCESS
;
202 if (which
== PRIO_USER
) {
203 struct passwd
*pwd
= getpwnam(*argv
);
208 who
= strtol(*argv
, &endptr
, 10);
209 if (who
< 0 || *endptr
) {
210 warnx(_("unknown user %s"), *argv
);
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
);
224 errs
|= donice(which
, who
, prio
, relative
);
226 return errs
!= 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;