[821] | 1 | /* vi: set sw=4 ts=4: */
|
---|
| 2 | /*
|
---|
| 3 | * nice implementation for busybox
|
---|
| 4 | *
|
---|
| 5 | * Copyright (C) 2005 Manuel Novoa III <mjn3@codepoet.org>
|
---|
| 6 | *
|
---|
[2725] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
---|
[821] | 8 | */
|
---|
| 9 |
|
---|
[3232] | 10 | //usage:#define nice_trivial_usage
|
---|
| 11 | //usage: "[-n ADJUST] [PROG ARGS]"
|
---|
| 12 | //usage:#define nice_full_usage "\n\n"
|
---|
| 13 | //usage: "Change scheduling priority, run PROG\n"
|
---|
| 14 | //usage: "\n -n ADJUST Adjust priority by ADJUST"
|
---|
| 15 |
|
---|
[821] | 16 | #include <sys/resource.h>
|
---|
[1765] | 17 | #include "libbb.h"
|
---|
[821] | 18 |
|
---|
[2725] | 19 | int nice_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
---|
[821] | 20 | int nice_main(int argc, char **argv)
|
---|
| 21 | {
|
---|
| 22 | int old_priority, adjustment;
|
---|
| 23 |
|
---|
| 24 | old_priority = getpriority(PRIO_PROCESS, 0);
|
---|
| 25 |
|
---|
[2725] | 26 | if (!*++argv) { /* No args, so (GNU) output current nice value. */
|
---|
[1765] | 27 | printf("%d\n", old_priority);
|
---|
| 28 | fflush_stdout_and_exit(EXIT_SUCCESS);
|
---|
[821] | 29 | }
|
---|
| 30 |
|
---|
[2725] | 31 | adjustment = 10; /* Set default adjustment. */
|
---|
[821] | 32 |
|
---|
[1765] | 33 | if (argv[0][0] == '-') {
|
---|
| 34 | if (argv[0][1] == 'n') { /* -n */
|
---|
| 35 | if (argv[0][2]) { /* -nNNNN (w/o space) */
|
---|
| 36 | argv[0] += 2; argv--; argc++;
|
---|
| 37 | }
|
---|
| 38 | } else { /* -NNN (NNN may be negative) == -n NNN */
|
---|
| 39 | argv[0] += 1; argv--; argc++;
|
---|
| 40 | }
|
---|
[2725] | 41 | if (argc < 4) { /* Missing priority and/or utility! */
|
---|
[821] | 42 | bb_show_usage();
|
---|
| 43 | }
|
---|
[1765] | 44 | adjustment = xatoi_range(argv[1], INT_MIN/2, INT_MAX/2);
|
---|
[821] | 45 | argv += 2;
|
---|
| 46 | }
|
---|
| 47 |
|
---|
[1765] | 48 | { /* Set our priority. */
|
---|
| 49 | int prio = old_priority + adjustment;
|
---|
[821] | 50 |
|
---|
[1765] | 51 | if (setpriority(PRIO_PROCESS, 0, prio) < 0) {
|
---|
| 52 | bb_perror_msg_and_die("setpriority(%d)", prio);
|
---|
[821] | 53 | }
|
---|
| 54 | }
|
---|
| 55 |
|
---|
[2725] | 56 | BB_EXECVP_or_die(argv);
|
---|
[821] | 57 | }
|
---|