[821] | 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 | id_t 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 = bb_xgetlarg(*argv, 10, INT_MIN, INT_MAX);
|
---|
| 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;
|
---|
| 85 | if ((p = strchr(opts, argv[0][1]))) {
|
---|
| 86 | which = p[4];
|
---|
| 87 | continue;
|
---|
| 88 | }
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | /* Process an ID arg. */
|
---|
| 92 | if (which == PRIO_USER) {
|
---|
| 93 | struct passwd *p;
|
---|
| 94 | if (!(p = getpwnam(*argv))) {
|
---|
| 95 | bb_error_msg("unknown user: %s", *argv);
|
---|
| 96 | goto HAD_ERROR;
|
---|
| 97 | }
|
---|
| 98 | who = p->pw_uid;
|
---|
| 99 | } else {
|
---|
| 100 | char *e;
|
---|
| 101 | errno = 0;
|
---|
| 102 | who = strtoul(*argv, &e, 10);
|
---|
| 103 | if (*e || (*argv == e) || errno) {
|
---|
| 104 | bb_error_msg("bad value: %s", *argv);
|
---|
| 105 | goto HAD_ERROR;
|
---|
| 106 | }
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | /* Get priority to use, and set it. */
|
---|
| 110 | if (use_relative) {
|
---|
| 111 | int old_priority;
|
---|
| 112 |
|
---|
| 113 | errno = 0; /* Needed for getpriority error detection. */
|
---|
| 114 | old_priority = getpriority(which, who);
|
---|
| 115 | if (errno) {
|
---|
| 116 | bb_perror_msg(Xetpriority_msg, who, 'g');
|
---|
| 117 | goto HAD_ERROR;
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | new_priority = int_add_no_wrap(old_priority, adjustment);
|
---|
| 121 | } else {
|
---|
| 122 | new_priority = adjustment;
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | if (setpriority(which, who, new_priority) == 0) {
|
---|
| 126 | continue;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | bb_perror_msg(Xetpriority_msg, who, 's');
|
---|
| 130 | HAD_ERROR:
|
---|
| 131 | retval = EXIT_FAILURE;
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | /* No need to check for errors outputing to stderr since, if it
|
---|
| 135 | * was used, the HAD_ERROR label was reached and retval was set. */
|
---|
| 136 |
|
---|
| 137 | return retval;
|
---|
| 138 | }
|
---|