Consolidate #include <sys/time.h> so libbb.h does it.
[oweals/busybox.git] / procps / renice.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * renice implementation for busybox
4  *
5  * Copyright (C) 2005  Manuel Novoa III  <mjn3@codepoet.org>
6  *
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.
11  *
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.
16  *
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
20  *
21  */
22
23 /* Notes:
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.
29  *
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.
33  */
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <limits.h>
39 #include <errno.h>
40 #include <unistd.h>
41 #include <sys/resource.h>
42 #include "busybox.h"
43
44 #if (PRIO_PROCESS < CHAR_MIN) || (PRIO_PROCESS > CHAR_MAX)
45 #error Assumption violated : PRIO_PROCESS value
46 #endif
47 #if (PRIO_PGRP < CHAR_MIN) || (PRIO_PGRP > CHAR_MAX)
48 #error Assumption violated : PRIO_PGRP value
49 #endif
50 #if (PRIO_USER < CHAR_MIN) || (PRIO_USER > CHAR_MAX)
51 #error Assumption violated : PRIO_USER value
52 #endif
53
54 static inline int int_add_no_wrap(int a, int b)
55 {
56         int s = a + b;
57
58         if (b < 0) {
59                 if (s > a) s = INT_MIN;
60         } else {
61                 if (s < a) s = INT_MAX;
62         }
63
64         return s;
65 }
66
67 int renice_main(int argc, char **argv)
68 {
69         static const char Xetpriority_msg[] = "%d : %cetpriority";
70
71         int retval = EXIT_SUCCESS;
72         int which = PRIO_PROCESS;       /* Default 'which' value. */
73         int use_relative = 0;
74         int adjustment, new_priority;
75         id_t who;
76
77         ++argv;
78
79         /* Check if we are using a relative adjustment. */
80         if (argv[0] && (argv[0][0] == '-') && (argv[0][1] == 'n') && !argv[0][2]) {
81                 use_relative = 1;
82                 ++argv;
83         }
84
85         if (!*argv) {                           /* No args?  Then show usage. */
86                 bb_show_usage();
87         }
88
89         /* Get the priority adjustment (absolute or relative). */
90         adjustment = bb_xgetlarg(*argv, 10, INT_MIN, INT_MAX);
91
92         while (*++argv) {
93                 /* Check for a mode switch. */
94                 if ((argv[0][0] == '-') && argv[0][1] && !argv[0][2]) {
95                         static const char opts[]
96                                 = { 'p', 'g', 'u', 0, PRIO_PROCESS, PRIO_PGRP, PRIO_USER };
97                         const char *p;
98                         if ((p = strchr(opts, argv[0][1]))) {
99                                 which = p[4];
100                                 continue;
101                         }
102                 }
103
104                 /* Process an ID arg. */
105                 if (which == PRIO_USER) {
106                         struct passwd *p;
107                         if (!(p = getpwnam(*argv))) {
108                                 bb_error_msg("unknown user: %s", *argv);
109                                 goto HAD_ERROR;
110                         }
111                         who = p->pw_uid;
112                 } else {
113                         char *e;
114                         errno = 0;
115                         who = strtoul(*argv, &e, 10);
116                         if (*e || (*argv == e) || errno) {
117                                 bb_error_msg("bad value: %s", *argv);
118                                 goto HAD_ERROR;
119                         }
120                 }
121
122                 /* Get priority to use, and set it. */
123                 if (use_relative) {
124                         int old_priority;
125
126                         errno = 0;       /* Needed for getpriority error detection. */
127                         old_priority = getpriority(which, who);
128                         if (errno) {
129                                 bb_perror_msg(Xetpriority_msg, who, 'g');
130                                 goto HAD_ERROR;
131                         }
132
133                         new_priority = int_add_no_wrap(old_priority, adjustment);
134                 } else {
135                         new_priority = adjustment;
136                 }
137
138                 if (setpriority(which, who, new_priority) == 0) {
139                         continue;
140                 }
141
142                 bb_perror_msg(Xetpriority_msg, who, 's');
143         HAD_ERROR:
144                 retval = EXIT_FAILURE;
145         }
146
147         /* No need to check for errors outputing to stderr since, if it
148          * was used, the HAD_ERROR label was reached and retval was set. */
149
150         return retval;
151 }