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 | */ |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 9 | //config:config NICE |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 10 | //config: bool "nice (2.1 kb)" |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 11 | //config: default y |
| 12 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 13 | //config: nice runs a program with modified scheduling priority. |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 14 | |
Denys Vlasenko | 5c527dc | 2017-08-04 19:55:01 +0200 | [diff] [blame] | 15 | //applet:IF_NICE(APPLET_NOEXEC(nice, nice, BB_DIR_BIN, BB_SUID_DROP, nice)) |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 16 | |
| 17 | //kbuild:lib-$(CONFIG_NICE) += nice.o |
Manuel Novoa III | 2c51160 | 2005-02-13 20:14:05 +0000 | [diff] [blame] | 18 | |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 19 | //usage:#define nice_trivial_usage |
| 20 | //usage: "[-n ADJUST] [PROG ARGS]" |
| 21 | //usage:#define nice_full_usage "\n\n" |
| 22 | //usage: "Change scheduling priority, run PROG\n" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 23 | //usage: "\n -n ADJUST Adjust priority by ADJUST" |
| 24 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 25 | #include "libbb.h" |
Manuel Novoa III | 2c51160 | 2005-02-13 20:14:05 +0000 | [diff] [blame] | 26 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 27 | int nice_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | c9807d7 | 2018-01-08 10:13:11 +0100 | [diff] [blame] | 28 | int nice_main(int argc UNUSED_PARAM, char **argv) |
Manuel Novoa III | 2c51160 | 2005-02-13 20:14:05 +0000 | [diff] [blame] | 29 | { |
Manuel Novoa III | 2c51160 | 2005-02-13 20:14:05 +0000 | [diff] [blame] | 30 | int old_priority, adjustment; |
| 31 | |
Manuel Novoa III | 2c51160 | 2005-02-13 20:14:05 +0000 | [diff] [blame] | 32 | old_priority = getpriority(PRIO_PROCESS, 0); |
Manuel Novoa III | 2c51160 | 2005-02-13 20:14:05 +0000 | [diff] [blame] | 33 | |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 34 | if (!*++argv) { /* No args, so (GNU) output current nice value. */ |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 35 | printf("%d\n", old_priority); |
Denys Vlasenko | 31f45c1 | 2022-01-04 23:31:58 +0100 | [diff] [blame] | 36 | fflush_stdout_and_exit_SUCCESS(); |
Manuel Novoa III | 2c51160 | 2005-02-13 20:14:05 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 39 | adjustment = 10; /* Set default adjustment. */ |
Manuel Novoa III | 2c51160 | 2005-02-13 20:14:05 +0000 | [diff] [blame] | 40 | |
Denis Vlasenko | ca3c981 | 2006-10-08 23:36:17 +0000 | [diff] [blame] | 41 | if (argv[0][0] == '-') { |
Denys Vlasenko | c9807d7 | 2018-01-08 10:13:11 +0100 | [diff] [blame] | 42 | char *nnn = argv[0] + 1; |
| 43 | if (nnn[0] == 'n') { /* -n */ |
| 44 | nnn += 1; |
| 45 | if (!nnn[0]) { /* "-n NNN" */ |
| 46 | nnn = *++argv; |
Denis Vlasenko | ca3c981 | 2006-10-08 23:36:17 +0000 | [diff] [blame] | 47 | } |
Denys Vlasenko | c9807d7 | 2018-01-08 10:13:11 +0100 | [diff] [blame] | 48 | /* else: "-nNNN" (w/o space) */ |
Denis Vlasenko | ca3c981 | 2006-10-08 23:36:17 +0000 | [diff] [blame] | 49 | } |
Denys Vlasenko | c9807d7 | 2018-01-08 10:13:11 +0100 | [diff] [blame] | 50 | /* else: "-NNN" (NNN may be negative) - same as "-n NNN" */ |
| 51 | |
| 52 | if (!nnn || !argv[1]) { /* Missing priority or PROG! */ |
Manuel Novoa III | 2c51160 | 2005-02-13 20:14:05 +0000 | [diff] [blame] | 53 | bb_show_usage(); |
| 54 | } |
Denys Vlasenko | c9807d7 | 2018-01-08 10:13:11 +0100 | [diff] [blame] | 55 | adjustment = xatoi_range(nnn, INT_MIN/2, INT_MAX/2); |
| 56 | argv++; |
Manuel Novoa III | 2c51160 | 2005-02-13 20:14:05 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Denis Vlasenko | ca3c981 | 2006-10-08 23:36:17 +0000 | [diff] [blame] | 59 | { /* Set our priority. */ |
| 60 | int prio = old_priority + adjustment; |
Manuel Novoa III | 2c51160 | 2005-02-13 20:14:05 +0000 | [diff] [blame] | 61 | |
Denis Vlasenko | ca3c981 | 2006-10-08 23:36:17 +0000 | [diff] [blame] | 62 | if (setpriority(PRIO_PROCESS, 0, prio) < 0) { |
| 63 | bb_perror_msg_and_die("setpriority(%d)", prio); |
Manuel Novoa III | 2c51160 | 2005-02-13 20:14:05 +0000 | [diff] [blame] | 64 | } |
| 65 | } |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 66 | |
Pascal Bellard | 21e8e8d | 2010-07-04 00:57:03 +0200 | [diff] [blame] | 67 | BB_EXECVP_or_die(argv); |
Manuel Novoa III | 2c51160 | 2005-02-13 20:14:05 +0000 | [diff] [blame] | 68 | } |