1 /* vi: set sw=4 ts=4: */
3 * renice implementation for busybox
5 * Copyright (C) 2005 Manuel Novoa III <mjn3@codepoet.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * Setting an absolute priority was obsoleted in SUSv2 and removed
25 * in SUSv3. However, the common linux version of renice does
26 * absolute and not relative. So we'll continue supporting absolute,
27 * although the stdout logging has been removed since both SUSv2 and
28 * SUSv3 specify that stdout isn't used.
30 * This version is lenient in that it doesn't require any IDs. The
31 * options -p, -g, and -u are treated as mode switches for the
32 * following IDs (if any). Multiple switches are allowed.
42 #include <sys/resource.h>
45 #if (PRIO_PROCESS < CHAR_MIN) || (PRIO_PROCESS > CHAR_MAX)
46 #error Assumption violated : PRIO_PROCESS value
48 #if (PRIO_PGRP < CHAR_MIN) || (PRIO_PGRP > CHAR_MAX)
49 #error Assumption violated : PRIO_PGRP value
51 #if (PRIO_USER < CHAR_MIN) || (PRIO_USER > CHAR_MAX)
52 #error Assumption violated : PRIO_USER value
55 static inline int int_add_no_wrap(int a, int b)
60 if (s > a) s = INT_MIN;
62 if (s < a) s = INT_MAX;
68 int renice_main(int argc, char **argv)
70 static const char Xetpriority_msg[] = "%d : %cetpriority";
72 int retval = EXIT_SUCCESS;
73 int which = PRIO_PROCESS; /* Default 'which' value. */
75 int adjustment, new_priority;
80 /* Check if we are using a relative adjustment. */
81 if (argv[0] && (argv[0][0] == '-') && (argv[0][1] == 'n') && !argv[0][2]) {
86 if (!*argv) { /* No args? Then show usage. */
90 /* Get the priority adjustment (absolute or relative). */
91 adjustment = bb_xgetlarg(*argv, 10, INT_MIN, INT_MAX);
94 /* Check for a mode switch. */
95 if ((argv[0][0] == '-') && argv[0][1] && !argv[0][2]) {
96 static const char opts[]
97 = { 'p', 'g', 'u', 0, PRIO_PROCESS, PRIO_PGRP, PRIO_USER };
99 if ((p = strchr(opts, argv[0][1]))) {
105 /* Process an ID arg. */
106 if (which == PRIO_USER) {
108 if (!(p = getpwnam(*argv))) {
109 bb_error_msg("unknown user: %s", *argv);
116 who = strtoul(*argv, &e, 10);
117 if (*e || (*argv == e) || errno) {
118 bb_error_msg("bad value: %s", *argv);
123 /* Get priority to use, and set it. */
127 errno = 0; /* Needed for getpriority error detection. */
128 old_priority = getpriority(which, who);
130 bb_perror_msg(Xetpriority_msg, who, 'g');
134 new_priority = int_add_no_wrap(old_priority, adjustment);
136 new_priority = adjustment;
139 if (setpriority(which, who, new_priority) == 0) {
143 bb_perror_msg(Xetpriority_msg, who, 's');
145 retval = EXIT_FAILURE;
148 /* No need to check for errors outputing to stderr since, if it
149 * was used, the HAD_ERROR label was reached and retval was set. */