blob: 53dc5785536ef647557c2639d631c99c3c43d06f [file] [log] [blame]
Manuel Novoa III 2c511602005-02-13 20:14:05 +00001/* vi: set sw=4 ts=4: */
Eric Andersenbf960f52000-07-21 21:32:12 +00002/*
Manuel Novoa III 2c511602005-02-13 20:14:05 +00003 * renice implementation for busybox
Eric Andersenbf960f52000-07-21 21:32:12 +00004 *
Manuel Novoa III 2c511602005-02-13 20:14:05 +00005 * Copyright (C) 2005 Manuel Novoa III <mjn3@codepoet.org>
Eric Andersenbf960f52000-07-21 21:32:12 +00006 *
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +00007 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Eric Andersenbf960f52000-07-21 21:32:12 +00008 */
9
Manuel Novoa III 2c511602005-02-13 20:14:05 +000010/* Notes:
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.
16 *
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.
20 */
21
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +000022#include "busybox.h"
Eric Andersenbf960f52000-07-21 21:32:12 +000023#include <stdio.h>
Eric Andersened3ef502001-01-27 08:24:39 +000024#include <stdlib.h>
Manuel Novoa III 2c511602005-02-13 20:14:05 +000025#include <string.h>
26#include <limits.h>
27#include <errno.h>
28#include <unistd.h>
Eric Andersenbf960f52000-07-21 21:32:12 +000029#include <sys/resource.h>
30
Manuel Novoa III 2c511602005-02-13 20:14:05 +000031#if (PRIO_PROCESS < CHAR_MIN) || (PRIO_PROCESS > CHAR_MAX)
32#error Assumption violated : PRIO_PROCESS value
33#endif
34#if (PRIO_PGRP < CHAR_MIN) || (PRIO_PGRP > CHAR_MAX)
35#error Assumption violated : PRIO_PGRP value
36#endif
37#if (PRIO_USER < CHAR_MIN) || (PRIO_USER > CHAR_MAX)
38#error Assumption violated : PRIO_USER value
39#endif
Eric Andersenbf960f52000-07-21 21:32:12 +000040
Manuel Novoa III 2c511602005-02-13 20:14:05 +000041static inline int int_add_no_wrap(int a, int b)
Eric Andersenbf960f52000-07-21 21:32:12 +000042{
Manuel Novoa III 2c511602005-02-13 20:14:05 +000043 int s = a + b;
Eric Andersenc7bda1c2004-03-15 08:29:22 +000044
Manuel Novoa III 2c511602005-02-13 20:14:05 +000045 if (b < 0) {
46 if (s > a) s = INT_MIN;
47 } else {
48 if (s < a) s = INT_MAX;
Eric Andersenbf960f52000-07-21 21:32:12 +000049 }
Matt Kraaideb9d4d2000-12-05 20:07:27 +000050
Manuel Novoa III 2c511602005-02-13 20:14:05 +000051 return s;
52}
53
54int renice_main(int argc, char **argv)
55{
56 static const char Xetpriority_msg[] = "%d : %cetpriority";
57
58 int retval = EXIT_SUCCESS;
59 int which = PRIO_PROCESS; /* Default 'which' value. */
60 int use_relative = 0;
61 int adjustment, new_priority;
62 id_t who;
63
64 ++argv;
65
66 /* Check if we are using a relative adjustment. */
67 if (argv[0] && (argv[0][0] == '-') && (argv[0][1] == 'n') && !argv[0][2]) {
68 use_relative = 1;
69 ++argv;
70 }
71
72 if (!*argv) { /* No args? Then show usage. */
73 bb_show_usage();
74 }
75
76 /* Get the priority adjustment (absolute or relative). */
77 adjustment = bb_xgetlarg(*argv, 10, INT_MIN, INT_MAX);
78
79 while (*++argv) {
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 };
84 const char *p;
85 if ((p = strchr(opts, argv[0][1]))) {
86 which = p[4];
87 continue;
88 }
89 }
90
91 /* Process an ID arg. */
92 if (which == PRIO_USER) {
93 struct passwd *p;
94 if (!(p = getpwnam(*argv))) {
95 bb_error_msg("unknown user: %s", *argv);
96 goto HAD_ERROR;
97 }
98 who = p->pw_uid;
99 } else {
100 char *e;
101 errno = 0;
102 who = strtoul(*argv, &e, 10);
103 if (*e || (*argv == e) || errno) {
104 bb_error_msg("bad value: %s", *argv);
105 goto HAD_ERROR;
106 }
107 }
108
109 /* Get priority to use, and set it. */
110 if (use_relative) {
111 int old_priority;
112
113 errno = 0; /* Needed for getpriority error detection. */
114 old_priority = getpriority(which, who);
115 if (errno) {
116 bb_perror_msg(Xetpriority_msg, who, 'g');
117 goto HAD_ERROR;
118 }
119
120 new_priority = int_add_no_wrap(old_priority, adjustment);
121 } else {
122 new_priority = adjustment;
123 }
124
125 if (setpriority(which, who, new_priority) == 0) {
126 continue;
127 }
128
129 bb_perror_msg(Xetpriority_msg, who, 's');
130 HAD_ERROR:
131 retval = EXIT_FAILURE;
132 }
133
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. */
136
137 return retval;
Eric Andersenbf960f52000-07-21 21:32:12 +0000138}