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 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Manuel Novoa III | 2c51160 | 2005-02-13 20:14:05 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 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" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 14 | //usage: "\n -n ADJUST Adjust priority by ADJUST" |
| 15 | |
Manuel Novoa III | 2c51160 | 2005-02-13 20:14:05 +0000 | [diff] [blame] | 16 | #include <sys/resource.h> |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 17 | #include "libbb.h" |
Manuel Novoa III | 2c51160 | 2005-02-13 20:14:05 +0000 | [diff] [blame] | 18 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 19 | int nice_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Manuel Novoa III | 2c51160 | 2005-02-13 20:14:05 +0000 | [diff] [blame] | 20 | int nice_main(int argc, char **argv) |
| 21 | { |
Manuel Novoa III | 2c51160 | 2005-02-13 20:14:05 +0000 | [diff] [blame] | 22 | int old_priority, adjustment; |
| 23 | |
Manuel Novoa III | 2c51160 | 2005-02-13 20:14:05 +0000 | [diff] [blame] | 24 | old_priority = getpriority(PRIO_PROCESS, 0); |
Manuel Novoa III | 2c51160 | 2005-02-13 20:14:05 +0000 | [diff] [blame] | 25 | |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 26 | if (!*++argv) { /* No args, so (GNU) output current nice value. */ |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 27 | printf("%d\n", old_priority); |
| 28 | fflush_stdout_and_exit(EXIT_SUCCESS); |
Manuel Novoa III | 2c51160 | 2005-02-13 20:14:05 +0000 | [diff] [blame] | 29 | } |
| 30 | |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 31 | adjustment = 10; /* Set default adjustment. */ |
Manuel Novoa III | 2c51160 | 2005-02-13 20:14:05 +0000 | [diff] [blame] | 32 | |
Denis Vlasenko | ca3c981 | 2006-10-08 23:36:17 +0000 | [diff] [blame] | 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 | } |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 41 | if (argc < 4) { /* Missing priority and/or utility! */ |
Manuel Novoa III | 2c51160 | 2005-02-13 20:14:05 +0000 | [diff] [blame] | 42 | bb_show_usage(); |
| 43 | } |
Denis Vlasenko | ca3c981 | 2006-10-08 23:36:17 +0000 | [diff] [blame] | 44 | adjustment = xatoi_range(argv[1], INT_MIN/2, INT_MAX/2); |
Manuel Novoa III | 2c51160 | 2005-02-13 20:14:05 +0000 | [diff] [blame] | 45 | argv += 2; |
| 46 | } |
| 47 | |
Denis Vlasenko | ca3c981 | 2006-10-08 23:36:17 +0000 | [diff] [blame] | 48 | { /* Set our priority. */ |
| 49 | int prio = old_priority + adjustment; |
Manuel Novoa III | 2c51160 | 2005-02-13 20:14:05 +0000 | [diff] [blame] | 50 | |
Denis Vlasenko | ca3c981 | 2006-10-08 23:36:17 +0000 | [diff] [blame] | 51 | if (setpriority(PRIO_PROCESS, 0, prio) < 0) { |
| 52 | bb_perror_msg_and_die("setpriority(%d)", prio); |
Manuel Novoa III | 2c51160 | 2005-02-13 20:14:05 +0000 | [diff] [blame] | 53 | } |
| 54 | } |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 55 | |
Pascal Bellard | 21e8e8d | 2010-07-04 00:57:03 +0200 | [diff] [blame] | 56 | BB_EXECVP_or_die(argv); |
Manuel Novoa III | 2c51160 | 2005-02-13 20:14:05 +0000 | [diff] [blame] | 57 | } |