diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2006-10-08 23:36:17 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2006-10-08 23:36:17 +0000 |
commit | ca3c981c07ade7f8fd50ba4bb452a2fadaddc326 (patch) | |
tree | 8763d9f89197adda0ef45ac57418743766345fe4 /procps/renice.c | |
parent | 7039a66b58706457c7423de60556e04545432943 (diff) | |
download | busybox-w32-ca3c981c07ade7f8fd50ba4bb452a2fadaddc326.tar.gz busybox-w32-ca3c981c07ade7f8fd50ba4bb452a2fadaddc326.tar.bz2 busybox-w32-ca3c981c07ade7f8fd50ba4bb452a2fadaddc326.zip |
start_stop_daemon: add -N <nice> compat
[re]nice: add support for -nNNN w/o spaces, -NNN (nice only),
simplified code
Diffstat (limited to 'procps/renice.c')
-rw-r--r-- | procps/renice.c | 55 |
1 files changed, 24 insertions, 31 deletions
diff --git a/procps/renice.c b/procps/renice.c index a91328f53..bcaa94cf1 100644 --- a/procps/renice.c +++ b/procps/renice.c | |||
@@ -38,68 +38,61 @@ | |||
38 | #error Assumption violated : PRIO_USER value | 38 | #error Assumption violated : PRIO_USER value |
39 | #endif | 39 | #endif |
40 | 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) | 41 | int renice_main(int argc, char **argv) |
55 | { | 42 | { |
56 | static const char Xetpriority_msg[] = "%d: %cetpriority"; | 43 | static const char Xetpriority_msg[] = "%cetpriority"; |
57 | 44 | ||
58 | int retval = EXIT_SUCCESS; | 45 | int retval = EXIT_SUCCESS; |
59 | int which = PRIO_PROCESS; /* Default 'which' value. */ | 46 | int which = PRIO_PROCESS; /* Default 'which' value. */ |
60 | int use_relative = 0; | 47 | int use_relative = 0; |
61 | int adjustment, new_priority; | 48 | int adjustment, new_priority; |
62 | unsigned who; | 49 | unsigned who; |
50 | char *arg; | ||
63 | 51 | ||
64 | ++argv; | 52 | arg = *++argv; |
65 | 53 | ||
66 | /* Check if we are using a relative adjustment. */ | 54 | /* Check if we are using a relative adjustment. */ |
67 | if (argv[0] && (argv[0][0] == '-') && (argv[0][1] == 'n') && !argv[0][2]) { | 55 | if (arg && arg[0] == '-' && arg[1] == 'n') { |
68 | use_relative = 1; | 56 | use_relative = 1; |
69 | ++argv; | 57 | if (!arg[2]) |
58 | arg = *++argv; | ||
59 | else | ||
60 | arg += 2; | ||
70 | } | 61 | } |
71 | 62 | ||
72 | if (!*argv) { /* No args? Then show usage. */ | 63 | if (!arg) { /* No args? Then show usage. */ |
73 | bb_show_usage(); | 64 | bb_show_usage(); |
74 | } | 65 | } |
75 | 66 | ||
76 | /* Get the priority adjustment (absolute or relative). */ | 67 | /* Get the priority adjustment (absolute or relative). */ |
77 | adjustment = xatoi(*argv); | 68 | adjustment = xatoi_range(arg, INT_MIN/2, INT_MAX/2); |
78 | 69 | ||
79 | while (*++argv) { | 70 | while ((arg = *++argv) != NULL) { |
80 | /* Check for a mode switch. */ | 71 | /* Check for a mode switch. */ |
81 | if ((argv[0][0] == '-') && argv[0][1] && !argv[0][2]) { | 72 | if (arg[0] == '-' && arg[1]) { |
82 | static const char opts[] | 73 | static const char opts[] |
83 | = { 'p', 'g', 'u', 0, PRIO_PROCESS, PRIO_PGRP, PRIO_USER }; | 74 | = { 'p', 'g', 'u', 0, PRIO_PROCESS, PRIO_PGRP, PRIO_USER }; |
84 | const char *p = strchr(opts, argv[0][1]); | 75 | const char *p = strchr(opts, arg[1]); |
85 | if (p) { | 76 | if (p) { |
86 | which = p[4]; | 77 | which = p[4]; |
87 | continue; | 78 | if (!arg[2]) |
79 | continue; | ||
80 | arg += 2; | ||
88 | } | 81 | } |
89 | } | 82 | } |
90 | 83 | ||
91 | /* Process an ID arg. */ | 84 | /* Process an ID arg. */ |
92 | if (which == PRIO_USER) { | 85 | if (which == PRIO_USER) { |
93 | struct passwd *p; | 86 | struct passwd *p; |
94 | p = getpwnam(*argv); | 87 | p = getpwnam(arg); |
95 | if (!p) { | 88 | if (!p) { |
96 | bb_error_msg("unknown user: %s", *argv); | 89 | bb_error_msg("unknown user: %s", arg); |
97 | goto HAD_ERROR; | 90 | goto HAD_ERROR; |
98 | } | 91 | } |
99 | who = p->pw_uid; | 92 | who = p->pw_uid; |
100 | } else { | 93 | } else { |
101 | if (safe_strtou(*argv, &who)) { | 94 | if (safe_strtou(arg, &who)) { |
102 | bb_error_msg("bad value: %s", *argv); | 95 | bb_error_msg("bad value: %s", arg); |
103 | goto HAD_ERROR; | 96 | goto HAD_ERROR; |
104 | } | 97 | } |
105 | } | 98 | } |
@@ -111,11 +104,11 @@ int renice_main(int argc, char **argv) | |||
111 | errno = 0; /* Needed for getpriority error detection. */ | 104 | errno = 0; /* Needed for getpriority error detection. */ |
112 | old_priority = getpriority(which, who); | 105 | old_priority = getpriority(which, who); |
113 | if (errno) { | 106 | if (errno) { |
114 | bb_perror_msg(Xetpriority_msg, who, 'g'); | 107 | bb_perror_msg(Xetpriority_msg, 'g'); |
115 | goto HAD_ERROR; | 108 | goto HAD_ERROR; |
116 | } | 109 | } |
117 | 110 | ||
118 | new_priority = int_add_no_wrap(old_priority, adjustment); | 111 | new_priority = old_priority + adjustment; |
119 | } else { | 112 | } else { |
120 | new_priority = adjustment; | 113 | new_priority = adjustment; |
121 | } | 114 | } |
@@ -124,8 +117,8 @@ int renice_main(int argc, char **argv) | |||
124 | continue; | 117 | continue; |
125 | } | 118 | } |
126 | 119 | ||
127 | bb_perror_msg(Xetpriority_msg, who, 's'); | 120 | bb_perror_msg(Xetpriority_msg, 's'); |
128 | HAD_ERROR: | 121 | HAD_ERROR: |
129 | retval = EXIT_FAILURE; | 122 | retval = EXIT_FAILURE; |
130 | } | 123 | } |
131 | 124 | ||