Manuel Novoa III | 2c51160 | 2005-02-13 20:14:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | bf960f5 | 2000-07-21 21:32:12 +0000 | [diff] [blame] | 2 | /* |
Manuel Novoa III | 2c51160 | 2005-02-13 20:14:05 +0000 | [diff] [blame] | 3 | * renice implementation for busybox |
Eric Andersen | bf960f5 | 2000-07-21 21:32:12 +0000 | [diff] [blame] | 4 | * |
Manuel Novoa III | 2c51160 | 2005-02-13 20:14:05 +0000 | [diff] [blame] | 5 | * Copyright (C) 2005 Manuel Novoa III <mjn3@codepoet.org> |
Eric Andersen | bf960f5 | 2000-07-21 21:32:12 +0000 | [diff] [blame] | 6 | * |
Bernhard Reutner-Fischer | e15d757 | 2006-06-02 20:56:16 +0000 | [diff] [blame] | 7 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. |
Eric Andersen | bf960f5 | 2000-07-21 21:32:12 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Manuel Novoa III | 2c51160 | 2005-02-13 20:14:05 +0000 | [diff] [blame] | 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 | |
Bernhard Reutner-Fischer | e15d757 | 2006-06-02 20:56:16 +0000 | [diff] [blame] | 22 | #include "busybox.h" |
Eric Andersen | bf960f5 | 2000-07-21 21:32:12 +0000 | [diff] [blame] | 23 | #include <stdio.h> |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 24 | #include <stdlib.h> |
Manuel Novoa III | 2c51160 | 2005-02-13 20:14:05 +0000 | [diff] [blame] | 25 | #include <string.h> |
| 26 | #include <limits.h> |
| 27 | #include <errno.h> |
| 28 | #include <unistd.h> |
Eric Andersen | bf960f5 | 2000-07-21 21:32:12 +0000 | [diff] [blame] | 29 | #include <sys/resource.h> |
| 30 | |
Manuel Novoa III | 2c51160 | 2005-02-13 20:14:05 +0000 | [diff] [blame] | 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 |
Eric Andersen | bf960f5 | 2000-07-21 21:32:12 +0000 | [diff] [blame] | 40 | |
Manuel Novoa III | 2c51160 | 2005-02-13 20:14:05 +0000 | [diff] [blame] | 41 | int renice_main(int argc, char **argv) |
| 42 | { |
Denis Vlasenko | ca3c981 | 2006-10-08 23:36:17 +0000 | [diff] [blame] | 43 | static const char Xetpriority_msg[] = "%cetpriority"; |
Manuel Novoa III | 2c51160 | 2005-02-13 20:14:05 +0000 | [diff] [blame] | 44 | |
| 45 | int retval = EXIT_SUCCESS; |
| 46 | int which = PRIO_PROCESS; /* Default 'which' value. */ |
| 47 | int use_relative = 0; |
| 48 | int adjustment, new_priority; |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 49 | unsigned who; |
Denis Vlasenko | ca3c981 | 2006-10-08 23:36:17 +0000 | [diff] [blame] | 50 | char *arg; |
Manuel Novoa III | 2c51160 | 2005-02-13 20:14:05 +0000 | [diff] [blame] | 51 | |
Denis Vlasenko | ca3c981 | 2006-10-08 23:36:17 +0000 | [diff] [blame] | 52 | arg = *++argv; |
Manuel Novoa III | 2c51160 | 2005-02-13 20:14:05 +0000 | [diff] [blame] | 53 | |
| 54 | /* Check if we are using a relative adjustment. */ |
Denis Vlasenko | ca3c981 | 2006-10-08 23:36:17 +0000 | [diff] [blame] | 55 | if (arg && arg[0] == '-' && arg[1] == 'n') { |
Manuel Novoa III | 2c51160 | 2005-02-13 20:14:05 +0000 | [diff] [blame] | 56 | use_relative = 1; |
Denis Vlasenko | ca3c981 | 2006-10-08 23:36:17 +0000 | [diff] [blame] | 57 | if (!arg[2]) |
| 58 | arg = *++argv; |
| 59 | else |
| 60 | arg += 2; |
Manuel Novoa III | 2c51160 | 2005-02-13 20:14:05 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Denis Vlasenko | ca3c981 | 2006-10-08 23:36:17 +0000 | [diff] [blame] | 63 | if (!arg) { /* No args? Then show usage. */ |
Manuel Novoa III | 2c51160 | 2005-02-13 20:14:05 +0000 | [diff] [blame] | 64 | bb_show_usage(); |
| 65 | } |
| 66 | |
| 67 | /* Get the priority adjustment (absolute or relative). */ |
Denis Vlasenko | ca3c981 | 2006-10-08 23:36:17 +0000 | [diff] [blame] | 68 | adjustment = xatoi_range(arg, INT_MIN/2, INT_MAX/2); |
Manuel Novoa III | 2c51160 | 2005-02-13 20:14:05 +0000 | [diff] [blame] | 69 | |
Denis Vlasenko | ca3c981 | 2006-10-08 23:36:17 +0000 | [diff] [blame] | 70 | while ((arg = *++argv) != NULL) { |
Manuel Novoa III | 2c51160 | 2005-02-13 20:14:05 +0000 | [diff] [blame] | 71 | /* Check for a mode switch. */ |
Denis Vlasenko | ca3c981 | 2006-10-08 23:36:17 +0000 | [diff] [blame] | 72 | if (arg[0] == '-' && arg[1]) { |
Manuel Novoa III | 2c51160 | 2005-02-13 20:14:05 +0000 | [diff] [blame] | 73 | static const char opts[] |
| 74 | = { 'p', 'g', 'u', 0, PRIO_PROCESS, PRIO_PGRP, PRIO_USER }; |
Denis Vlasenko | ca3c981 | 2006-10-08 23:36:17 +0000 | [diff] [blame] | 75 | const char *p = strchr(opts, arg[1]); |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 76 | if (p) { |
Manuel Novoa III | 2c51160 | 2005-02-13 20:14:05 +0000 | [diff] [blame] | 77 | which = p[4]; |
Denis Vlasenko | ca3c981 | 2006-10-08 23:36:17 +0000 | [diff] [blame] | 78 | if (!arg[2]) |
| 79 | continue; |
| 80 | arg += 2; |
Manuel Novoa III | 2c51160 | 2005-02-13 20:14:05 +0000 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
| 84 | /* Process an ID arg. */ |
| 85 | if (which == PRIO_USER) { |
| 86 | struct passwd *p; |
Denis Vlasenko | ca3c981 | 2006-10-08 23:36:17 +0000 | [diff] [blame] | 87 | p = getpwnam(arg); |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 88 | if (!p) { |
Denis Vlasenko | ca3c981 | 2006-10-08 23:36:17 +0000 | [diff] [blame] | 89 | bb_error_msg("unknown user: %s", arg); |
Manuel Novoa III | 2c51160 | 2005-02-13 20:14:05 +0000 | [diff] [blame] | 90 | goto HAD_ERROR; |
| 91 | } |
| 92 | who = p->pw_uid; |
| 93 | } else { |
Denis Vlasenko | ca3c981 | 2006-10-08 23:36:17 +0000 | [diff] [blame] | 94 | if (safe_strtou(arg, &who)) { |
| 95 | bb_error_msg("bad value: %s", arg); |
Manuel Novoa III | 2c51160 | 2005-02-13 20:14:05 +0000 | [diff] [blame] | 96 | goto HAD_ERROR; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | /* Get priority to use, and set it. */ |
| 101 | if (use_relative) { |
| 102 | int old_priority; |
| 103 | |
| 104 | errno = 0; /* Needed for getpriority error detection. */ |
| 105 | old_priority = getpriority(which, who); |
| 106 | if (errno) { |
Denis Vlasenko | ca3c981 | 2006-10-08 23:36:17 +0000 | [diff] [blame] | 107 | bb_perror_msg(Xetpriority_msg, 'g'); |
Manuel Novoa III | 2c51160 | 2005-02-13 20:14:05 +0000 | [diff] [blame] | 108 | goto HAD_ERROR; |
| 109 | } |
| 110 | |
Denis Vlasenko | ca3c981 | 2006-10-08 23:36:17 +0000 | [diff] [blame] | 111 | new_priority = old_priority + adjustment; |
Manuel Novoa III | 2c51160 | 2005-02-13 20:14:05 +0000 | [diff] [blame] | 112 | } else { |
| 113 | new_priority = adjustment; |
| 114 | } |
| 115 | |
| 116 | if (setpriority(which, who, new_priority) == 0) { |
| 117 | continue; |
| 118 | } |
| 119 | |
Denis Vlasenko | ca3c981 | 2006-10-08 23:36:17 +0000 | [diff] [blame] | 120 | bb_perror_msg(Xetpriority_msg, 's'); |
| 121 | HAD_ERROR: |
Manuel Novoa III | 2c51160 | 2005-02-13 20:14:05 +0000 | [diff] [blame] | 122 | retval = EXIT_FAILURE; |
| 123 | } |
| 124 | |
| 125 | /* No need to check for errors outputing to stderr since, if it |
| 126 | * was used, the HAD_ERROR label was reached and retval was set. */ |
| 127 | |
| 128 | return retval; |
Eric Andersen | bf960f5 | 2000-07-21 21:32:12 +0000 | [diff] [blame] | 129 | } |