1 /* vi: set sw=4 ts=4: */
3 * renice implementation for busybox
5 * Copyright (C) 2005 Manuel Novoa III <mjn3@codepoet.org>
7 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
11 * Setting an absolute priority was obsoleted in SUSv2 and removed
12 * in SUSv3. However, the common linux version of renice does
13 * absolute and not relative. So we'll continue supporting absolute,
14 * although the stdout logging has been removed since both SUSv2 and
15 * SUSv3 specify that stdout isn't used.
17 * This version is lenient in that it doesn't require any IDs. The
18 * options -p, -g, and -u are treated as mode switches for the
19 * following IDs (if any). Multiple switches are allowed.
29 #include <sys/resource.h>
31 #if (PRIO_PROCESS < CHAR_MIN) || (PRIO_PROCESS > CHAR_MAX)
32 #error Assumption violated : PRIO_PROCESS value
34 #if (PRIO_PGRP < CHAR_MIN) || (PRIO_PGRP > CHAR_MAX)
35 #error Assumption violated : PRIO_PGRP value
37 #if (PRIO_USER < CHAR_MIN) || (PRIO_USER > CHAR_MAX)
38 #error Assumption violated : PRIO_USER value
41 static inline int int_add_no_wrap(int a, int b)
46 if (s > a) s = INT_MIN;
48 if (s < a) s = INT_MAX;
54 int renice_main(int argc, char **argv)
56 static const char Xetpriority_msg[] = "%d : %cetpriority";
58 int retval = EXIT_SUCCESS;
59 int which = PRIO_PROCESS; /* Default 'which' value. */
61 int adjustment, new_priority;
66 /* Check if we are using a relative adjustment. */
67 if (argv[0] && (argv[0][0] == '-') && (argv[0][1] == 'n') && !argv[0][2]) {
72 if (!*argv) { /* No args? Then show usage. */
76 /* Get the priority adjustment (absolute or relative). */
77 adjustment = bb_xgetlarg(*argv, 10, INT_MIN, INT_MAX);
80 /* Check for a mode switch. */
81 if ((argv[0][0] == '-') && argv[0][1] && !argv[0][2]) {
82 static const char opts[]
83 = { 'p', 'g', 'u', 0, PRIO_PROCESS, PRIO_PGRP, PRIO_USER };
85 if ((p = strchr(opts, argv[0][1]))) {
91 /* Process an ID arg. */
92 if (which == PRIO_USER) {
94 if (!(p = getpwnam(*argv))) {
95 bb_error_msg("unknown user: %s", *argv);
102 who = strtoul(*argv, &e, 10);
103 if (*e || (*argv == e) || errno) {
104 bb_error_msg("bad value: %s", *argv);
109 /* Get priority to use, and set it. */
113 errno = 0; /* Needed for getpriority error detection. */
114 old_priority = getpriority(which, who);
116 bb_perror_msg(Xetpriority_msg, who, 'g');
120 new_priority = int_add_no_wrap(old_priority, adjustment);
122 new_priority = adjustment;
125 if (setpriority(which, who, new_priority) == 0) {
129 bb_perror_msg(Xetpriority_msg, who, 's');
131 retval = EXIT_FAILURE;
134 /* No need to check for errors outputing to stderr since, if it
135 * was used, the HAD_ERROR label was reached and retval was set. */