diff options
author | Manuel Novoa III <mjn3@codepoet.org> | 2005-02-13 20:14:05 +0000 |
---|---|---|
committer | Manuel Novoa III <mjn3@codepoet.org> | 2005-02-13 20:14:05 +0000 |
commit | 2c511609c4d92ee4e3e603d449c13579d1ea641a (patch) | |
tree | d57c8dd9efa4c30cd01290f878cae688ae792cc0 /procps/renice.c | |
parent | d2fe81706c9d65dd1580c85338036cc403364fae (diff) | |
download | busybox-w32-2c511609c4d92ee4e3e603d449c13579d1ea641a.tar.gz busybox-w32-2c511609c4d92ee4e3e603d449c13579d1ea641a.tar.bz2 busybox-w32-2c511609c4d92ee4e3e603d449c13579d1ea641a.zip |
Add 'nice' and replace 'renice' with a new implementation.
Diffstat (limited to 'procps/renice.c')
-rw-r--r-- | procps/renice.c | 132 |
1 files changed, 115 insertions, 17 deletions
diff --git a/procps/renice.c b/procps/renice.c index a6f0820df..d4ce66d2e 100644 --- a/procps/renice.c +++ b/procps/renice.c | |||
@@ -1,8 +1,8 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
1 | /* | 2 | /* |
2 | * Mini renice implementation for busybox | 3 | * renice implementation for busybox |
3 | * | 4 | * |
4 | * | 5 | * Copyright (C) 2005 Manuel Novoa III <mjn3@codepoet.org> |
5 | * Copyright (C) 2000 Dave 'Kill a Cop' Cinege <dcinege@psychosis.com> | ||
6 | * | 6 | * |
7 | * This program is free software; you can redistribute it and/or modify | 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 | 8 | * it under the terms of the GNU General Public License as published by |
@@ -20,35 +20,133 @@ | |||
20 | * | 20 | * |
21 | */ | 21 | */ |
22 | 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 | |||
23 | #include <stdio.h> | 35 | #include <stdio.h> |
24 | #include <errno.h> | ||
25 | #include <stdlib.h> | 36 | #include <stdlib.h> |
37 | #include <string.h> | ||
38 | #include <limits.h> | ||
39 | #include <errno.h> | ||
40 | #include <unistd.h> | ||
26 | #include <sys/time.h> | 41 | #include <sys/time.h> |
27 | #include <sys/resource.h> | 42 | #include <sys/resource.h> |
28 | #include "busybox.h" | 43 | #include "busybox.h" |
29 | 44 | ||
45 | #if (PRIO_PROCESS < CHAR_MIN) || (PRIO_PROCESS > CHAR_MAX) | ||
46 | #error Assumption violated : PRIO_PROCESS value | ||
47 | #endif | ||
48 | #if (PRIO_PGRP < CHAR_MIN) || (PRIO_PGRP > CHAR_MAX) | ||
49 | #error Assumption violated : PRIO_PGRP value | ||
50 | #endif | ||
51 | #if (PRIO_USER < CHAR_MIN) || (PRIO_USER > CHAR_MAX) | ||
52 | #error Assumption violated : PRIO_USER value | ||
53 | #endif | ||
54 | |||
55 | static inline int int_add_no_wrap(int a, int b) | ||
56 | { | ||
57 | int s = a + b; | ||
58 | |||
59 | if (b < 0) { | ||
60 | if (s > a) s = INT_MIN; | ||
61 | } else { | ||
62 | if (s < a) s = INT_MAX; | ||
63 | } | ||
30 | 64 | ||
31 | extern int renice_main(int argc, char **argv) | 65 | return s; |
66 | } | ||
67 | |||
68 | int renice_main(int argc, char **argv) | ||
32 | { | 69 | { |
33 | int prio, status = EXIT_SUCCESS; | 70 | static const char Xetpriority_msg[] = "%d : %cetpriority"; |
71 | |||
72 | int retval = EXIT_SUCCESS; | ||
73 | int which = PRIO_PROCESS; /* Default 'which' value. */ | ||
74 | int use_relative = 0; | ||
75 | int adjustment, new_priority; | ||
76 | id_t who; | ||
34 | 77 | ||
35 | if (argc < 3) bb_show_usage(); | 78 | ++argv; |
36 | 79 | ||
37 | prio = atoi(*++argv); | 80 | /* Check if we are using a relative adjustment. */ |
38 | if (prio > 20) prio = 20; | 81 | if (argv[0] && (argv[0][0] == '-') && (argv[0][1] == 'n') && !argv[0][2]) { |
39 | if (prio < -20) prio = -20; | 82 | use_relative = 1; |
83 | ++argv; | ||
84 | } | ||
85 | |||
86 | if (!*argv) { /* No args? Then show usage. */ | ||
87 | bb_show_usage(); | ||
88 | } | ||
89 | |||
90 | /* Get the priority adjustment (absolute or relative). */ | ||
91 | adjustment = bb_xgetlarg(*argv, 10, INT_MIN, INT_MAX); | ||
40 | 92 | ||
41 | while (*++argv) { | 93 | while (*++argv) { |
42 | int ps = atoi(*argv); | 94 | /* Check for a mode switch. */ |
43 | int oldp = getpriority(PRIO_PROCESS, ps); | 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 }; | ||
98 | const char *p; | ||
99 | if ((p = strchr(opts, argv[0][1]))) { | ||
100 | which = p[4]; | ||
101 | continue; | ||
102 | } | ||
103 | } | ||
44 | 104 | ||
45 | if (setpriority(PRIO_PROCESS, ps, prio) == 0) { | 105 | /* Process an ID arg. */ |
46 | printf("%d: old priority %d, new priority %d\n", ps, oldp, prio ); | 106 | if (which == PRIO_USER) { |
107 | struct passwd *p; | ||
108 | if (!(p = getpwnam(*argv))) { | ||
109 | bb_error_msg("unknown user: %s", *argv); | ||
110 | goto HAD_ERROR; | ||
111 | } | ||
112 | who = p->pw_uid; | ||
47 | } else { | 113 | } else { |
48 | bb_perror_msg("%d: setpriority", ps); | 114 | char *e; |
49 | status = EXIT_FAILURE; | 115 | errno = 0; |
116 | who = strtoul(*argv, &e, 10); | ||
117 | if (*e || (*argv == e) || errno) { | ||
118 | bb_error_msg("bad value: %s", *argv); | ||
119 | goto HAD_ERROR; | ||
120 | } | ||
50 | } | 121 | } |
122 | |||
123 | /* Get priority to use, and set it. */ | ||
124 | if (use_relative) { | ||
125 | int old_priority; | ||
126 | |||
127 | errno = 0; /* Needed for getpriority error detection. */ | ||
128 | old_priority = getpriority(which, who); | ||
129 | if (errno) { | ||
130 | bb_perror_msg(Xetpriority_msg, who, 'g'); | ||
131 | goto HAD_ERROR; | ||
132 | } | ||
133 | |||
134 | new_priority = int_add_no_wrap(old_priority, adjustment); | ||
135 | } else { | ||
136 | new_priority = adjustment; | ||
137 | } | ||
138 | |||
139 | if (setpriority(which, who, new_priority) == 0) { | ||
140 | continue; | ||
141 | } | ||
142 | |||
143 | bb_perror_msg(Xetpriority_msg, who, 's'); | ||
144 | HAD_ERROR: | ||
145 | retval = EXIT_FAILURE; | ||
51 | } | 146 | } |
52 | 147 | ||
53 | return status; | 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. */ | ||
150 | |||
151 | return retval; | ||
54 | } | 152 | } |