[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 | *
|
---|
[1765] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
---|
[821] | 8 | */
|
---|
| 9 |
|
---|
| 10 | #include <sys/resource.h>
|
---|
[1765] | 11 | #include "libbb.h"
|
---|
[821] | 12 |
|
---|
[1765] | 13 | int nice_main(int argc, char **argv);
|
---|
[821] | 14 | int nice_main(int argc, char **argv)
|
---|
| 15 | {
|
---|
| 16 | int old_priority, adjustment;
|
---|
| 17 |
|
---|
| 18 | old_priority = getpriority(PRIO_PROCESS, 0);
|
---|
| 19 |
|
---|
| 20 | if (!*++argv) { /* No args, so (GNU) output current nice value. */
|
---|
[1765] | 21 | printf("%d\n", old_priority);
|
---|
| 22 | fflush_stdout_and_exit(EXIT_SUCCESS);
|
---|
[821] | 23 | }
|
---|
| 24 |
|
---|
| 25 | adjustment = 10; /* Set default adjustment. */
|
---|
| 26 |
|
---|
[1765] | 27 | if (argv[0][0] == '-') {
|
---|
| 28 | if (argv[0][1] == 'n') { /* -n */
|
---|
| 29 | if (argv[0][2]) { /* -nNNNN (w/o space) */
|
---|
| 30 | argv[0] += 2; argv--; argc++;
|
---|
| 31 | }
|
---|
| 32 | } else { /* -NNN (NNN may be negative) == -n NNN */
|
---|
| 33 | argv[0] += 1; argv--; argc++;
|
---|
| 34 | }
|
---|
[821] | 35 | if (argc < 4) { /* Missing priority and/or utility! */
|
---|
| 36 | bb_show_usage();
|
---|
| 37 | }
|
---|
[1765] | 38 | adjustment = xatoi_range(argv[1], INT_MIN/2, INT_MAX/2);
|
---|
[821] | 39 | argv += 2;
|
---|
| 40 | }
|
---|
| 41 |
|
---|
[1765] | 42 | { /* Set our priority. */
|
---|
| 43 | int prio = old_priority + adjustment;
|
---|
[821] | 44 |
|
---|
[1765] | 45 | if (setpriority(PRIO_PROCESS, 0, prio) < 0) {
|
---|
| 46 | bb_perror_msg_and_die("setpriority(%d)", prio);
|
---|
[821] | 47 | }
|
---|
| 48 | }
|
---|
| 49 |
|
---|
[1765] | 50 | BB_EXECVP(*argv, argv); /* Now exec the desired program. */
|
---|
[821] | 51 |
|
---|
| 52 | /* The exec failed... */
|
---|
[1765] | 53 | xfunc_error_retval = (errno == ENOENT) ? 127 : 126; /* SUSv3 */
|
---|
[821] | 54 | bb_perror_msg_and_die("%s", *argv);
|
---|
| 55 | }
|
---|