Manuel Novoa III | 2c51160 | 2005-02-13 20:14:05 +0000 | [diff] [blame] | 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 | * |
"Robert P. J. Day" | 801ab14 | 2006-07-12 07:56:04 +0000 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Manuel Novoa III | 2c51160 | 2005-02-13 20:14:05 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Manuel Novoa III | 2c51160 | 2005-02-13 20:14:05 +0000 | [diff] [blame] | 10 | #include <sys/resource.h> |
| 11 | #include "busybox.h" |
| 12 | |
Manuel Novoa III | 2c51160 | 2005-02-13 20:14:05 +0000 | [diff] [blame] | 13 | int nice_main(int argc, char **argv) |
| 14 | { |
Manuel Novoa III | 2c51160 | 2005-02-13 20:14:05 +0000 | [diff] [blame] | 15 | int old_priority, adjustment; |
| 16 | |
Manuel Novoa III | 2c51160 | 2005-02-13 20:14:05 +0000 | [diff] [blame] | 17 | old_priority = getpriority(PRIO_PROCESS, 0); |
Manuel Novoa III | 2c51160 | 2005-02-13 20:14:05 +0000 | [diff] [blame] | 18 | |
| 19 | if (!*++argv) { /* No args, so (GNU) output current nice value. */ |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 20 | printf("%d\n", old_priority); |
| 21 | fflush_stdout_and_exit(EXIT_SUCCESS); |
Manuel Novoa III | 2c51160 | 2005-02-13 20:14:05 +0000 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | adjustment = 10; /* Set default adjustment. */ |
| 25 | |
Denis Vlasenko | ca3c981 | 2006-10-08 23:36:17 +0000 | [diff] [blame] | 26 | if (argv[0][0] == '-') { |
| 27 | if (argv[0][1] == 'n') { /* -n */ |
| 28 | if (argv[0][2]) { /* -nNNNN (w/o space) */ |
| 29 | argv[0] += 2; argv--; argc++; |
| 30 | } |
| 31 | } else { /* -NNN (NNN may be negative) == -n NNN */ |
| 32 | argv[0] += 1; argv--; argc++; |
| 33 | } |
Manuel Novoa III | 2c51160 | 2005-02-13 20:14:05 +0000 | [diff] [blame] | 34 | if (argc < 4) { /* Missing priority and/or utility! */ |
| 35 | bb_show_usage(); |
| 36 | } |
Denis Vlasenko | ca3c981 | 2006-10-08 23:36:17 +0000 | [diff] [blame] | 37 | adjustment = xatoi_range(argv[1], INT_MIN/2, INT_MAX/2); |
Manuel Novoa III | 2c51160 | 2005-02-13 20:14:05 +0000 | [diff] [blame] | 38 | argv += 2; |
| 39 | } |
| 40 | |
Denis Vlasenko | ca3c981 | 2006-10-08 23:36:17 +0000 | [diff] [blame] | 41 | { /* Set our priority. */ |
| 42 | int prio = old_priority + adjustment; |
Manuel Novoa III | 2c51160 | 2005-02-13 20:14:05 +0000 | [diff] [blame] | 43 | |
Denis Vlasenko | ca3c981 | 2006-10-08 23:36:17 +0000 | [diff] [blame] | 44 | if (setpriority(PRIO_PROCESS, 0, prio) < 0) { |
| 45 | bb_perror_msg_and_die("setpriority(%d)", prio); |
Manuel Novoa III | 2c51160 | 2005-02-13 20:14:05 +0000 | [diff] [blame] | 46 | } |
| 47 | } |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 48 | |
Manuel Novoa III | 2c51160 | 2005-02-13 20:14:05 +0000 | [diff] [blame] | 49 | execvp(*argv, argv); /* Now exec the desired program. */ |
| 50 | |
| 51 | /* The exec failed... */ |
Denis Vlasenko | 4092082 | 2006-10-03 20:28:06 +0000 | [diff] [blame] | 52 | xfunc_error_retval = (errno == ENOENT) ? 127 : 126; /* SUSv3 */ |
Manuel Novoa III | 2c51160 | 2005-02-13 20:14:05 +0000 | [diff] [blame] | 53 | bb_perror_msg_and_die("%s", *argv); |
| 54 | } |