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 | |
| 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <string.h> |
| 13 | #include <limits.h> |
| 14 | #include <errno.h> |
| 15 | #include <unistd.h> |
Manuel Novoa III | 2c51160 | 2005-02-13 20:14:05 +0000 | [diff] [blame] | 16 | #include <sys/resource.h> |
| 17 | #include "busybox.h" |
| 18 | |
| 19 | static inline int int_add_no_wrap(int a, int b) |
| 20 | { |
| 21 | int s = a + b; |
| 22 | |
| 23 | if (b < 0) { |
| 24 | if (s > a) s = INT_MIN; |
| 25 | } else { |
| 26 | if (s < a) s = INT_MAX; |
| 27 | } |
| 28 | |
| 29 | return s; |
| 30 | } |
| 31 | |
| 32 | int nice_main(int argc, char **argv) |
| 33 | { |
| 34 | static const char Xetpriority_msg[] = "cannot %cet priority"; |
| 35 | |
| 36 | int old_priority, adjustment; |
| 37 | |
| 38 | errno = 0; /* Needed for getpriority error detection. */ |
| 39 | old_priority = getpriority(PRIO_PROCESS, 0); |
| 40 | if (errno) { |
| 41 | bb_perror_msg_and_die(Xetpriority_msg, 'g'); |
| 42 | } |
| 43 | |
| 44 | if (!*++argv) { /* No args, so (GNU) output current nice value. */ |
| 45 | bb_printf("%d\n", old_priority); |
| 46 | bb_fflush_stdout_and_exit(EXIT_SUCCESS); |
| 47 | } |
| 48 | |
| 49 | adjustment = 10; /* Set default adjustment. */ |
| 50 | |
| 51 | if ((argv[0][0] == '-') && (argv[0][1] == 'n') && !argv[0][2]) { /* "-n" */ |
| 52 | if (argc < 4) { /* Missing priority and/or utility! */ |
| 53 | bb_show_usage(); |
| 54 | } |
| 55 | adjustment = bb_xgetlarg(argv[1], 10, INT_MIN, INT_MAX); |
| 56 | argv += 2; |
| 57 | } |
| 58 | |
| 59 | { /* Set our priority. Handle integer wrapping for old + adjust. */ |
| 60 | int new_priority = int_add_no_wrap(old_priority, adjustment); |
| 61 | |
| 62 | if (setpriority(PRIO_PROCESS, 0, new_priority) < 0) { |
| 63 | bb_perror_msg_and_die(Xetpriority_msg, 's'); |
| 64 | } |
| 65 | } |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 66 | |
Manuel Novoa III | 2c51160 | 2005-02-13 20:14:05 +0000 | [diff] [blame] | 67 | execvp(*argv, argv); /* Now exec the desired program. */ |
| 68 | |
| 69 | /* The exec failed... */ |
| 70 | bb_default_error_retval = (errno == ENOENT) ? 127 : 126; /* SUSv3 */ |
| 71 | bb_perror_msg_and_die("%s", *argv); |
| 72 | } |