blob: 3676ee6630448db87b0d94b9b1bf59f3a7e9e494
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 | * |
7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
8 | */ |
9 | //config:config NICE |
10 | //config: bool "nice" |
11 | //config: default y |
12 | //config: help |
13 | //config: nice runs a program with modified scheduling priority. |
14 | |
15 | //applet:IF_NICE(APPLET(nice, BB_DIR_BIN, BB_SUID_DROP)) |
16 | |
17 | //kbuild:lib-$(CONFIG_NICE) += nice.o |
18 | |
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" |
23 | //usage: "\n -n ADJUST Adjust priority by ADJUST" |
24 | |
25 | #include <sys/resource.h> |
26 | #include "libbb.h" |
27 | |
28 | int nice_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
29 | int nice_main(int argc, char **argv) |
30 | { |
31 | int old_priority, adjustment; |
32 | |
33 | old_priority = getpriority(PRIO_PROCESS, 0); |
34 | |
35 | if (!*++argv) { /* No args, so (GNU) output current nice value. */ |
36 | printf("%d\n", old_priority); |
37 | fflush_stdout_and_exit(EXIT_SUCCESS); |
38 | } |
39 | |
40 | adjustment = 10; /* Set default adjustment. */ |
41 | |
42 | if (argv[0][0] == '-') { |
43 | if (argv[0][1] == 'n') { /* -n */ |
44 | if (argv[0][2]) { /* -nNNNN (w/o space) */ |
45 | argv[0] += 2; argv--; argc++; |
46 | } |
47 | } else { /* -NNN (NNN may be negative) == -n NNN */ |
48 | argv[0] += 1; argv--; argc++; |
49 | } |
50 | if (argc < 4) { /* Missing priority and/or utility! */ |
51 | bb_show_usage(); |
52 | } |
53 | adjustment = xatoi_range(argv[1], INT_MIN/2, INT_MAX/2); |
54 | argv += 2; |
55 | } |
56 | |
57 | { /* Set our priority. */ |
58 | int prio = old_priority + adjustment; |
59 | |
60 | if (setpriority(PRIO_PROCESS, 0, prio) < 0) { |
61 | bb_perror_msg_and_die("setpriority(%d)", prio); |
62 | } |
63 | } |
64 | |
65 | BB_EXECVP_or_die(argv); |
66 | } |
67 |