attempt to regularize atoi mess.
[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  * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
8  */
9
10 /* 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
22 #include "busybox.h"
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <limits.h>
27 #include <errno.h>
28 #include <unistd.h>
29 #include <sys/resource.h>
30
31 #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
40
41 static inline int int_add_no_wrap(int a, int b)
42 {
43         int s = a + b;
44
45         if (b < 0) {
46                 if (s > a) s = INT_MIN;
47         } else {
48                 if (s < a) s = INT_MAX;
49         }
50
51         return s;
52 }
53
54 int 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         unsigned 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 = xatoi(*argv);
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 = strchr(opts, argv[0][1]);
85                         if (p) {
86                                 which = p[4];
87                                 continue;
88                         }
89                 }
90
91                 /* Process an ID arg. */
92                 if (which == PRIO_USER) {
93                         struct passwd *p;
94                         p = getpwnam(*argv);
95                         if (!p) {
96                                 bb_error_msg("unknown user: %s", *argv);
97                                 goto HAD_ERROR;
98                         }
99                         who = p->pw_uid;
100                 } else {
101                         if (safe_strtou(*argv, &who)) {
102                                 bb_error_msg("bad value: %s", *argv);
103                                 goto HAD_ERROR;
104                         }
105                 }
106
107                 /* Get priority to use, and set it. */
108                 if (use_relative) {
109                         int old_priority;
110
111                         errno = 0;       /* Needed for getpriority error detection. */
112                         old_priority = getpriority(which, who);
113                         if (errno) {
114                                 bb_perror_msg(Xetpriority_msg, who, 'g');
115                                 goto HAD_ERROR;
116                         }
117
118                         new_priority = int_add_no_wrap(old_priority, adjustment);
119                 } else {
120                         new_priority = adjustment;
121                 }
122
123                 if (setpriority(which, who, new_priority) == 0) {
124                         continue;
125                 }
126
127                 bb_perror_msg(Xetpriority_msg, who, 's');
128         HAD_ERROR:
129                 retval = EXIT_FAILURE;
130         }
131
132         /* No need to check for errors outputing to stderr since, if it
133          * was used, the HAD_ERROR label was reached and retval was set. */
134
135         return retval;
136 }