1 /* vi: set sw=4 ts=4: */
3 * nice implementation for busybox
5 * Copyright (C) 2005 Manuel Novoa III <mjn3@codepoet.org>
7 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
10 //config: bool "nice (2.3 kb)"
13 //config: nice runs a program with modified scheduling priority.
15 //applet:IF_NICE(APPLET_NOEXEC(nice, nice, BB_DIR_BIN, BB_SUID_DROP, nice))
17 //kbuild:lib-$(CONFIG_NICE) += nice.o
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"
27 int nice_main(int argc
, char **argv
) MAIN_EXTERNALLY_VISIBLE
;
28 int nice_main(int argc UNUSED_PARAM
, char **argv
)
30 int old_priority
, adjustment
;
32 old_priority
= getpriority(PRIO_PROCESS
, 0);
34 if (!*++argv
) { /* No args, so (GNU) output current nice value. */
35 printf("%d\n", old_priority
);
36 fflush_stdout_and_exit_SUCCESS();
39 adjustment
= 10; /* Set default adjustment. */
41 if (argv
[0][0] == '-') {
42 char *nnn
= argv
[0] + 1;
43 if (nnn
[0] == 'n') { /* -n */
45 if (!nnn
[0]) { /* "-n NNN" */
48 /* else: "-nNNN" (w/o space) */
50 /* else: "-NNN" (NNN may be negative) - same as "-n NNN" */
52 if (!nnn
|| !argv
[1]) { /* Missing priority or PROG! */
55 adjustment
= xatoi_range(nnn
, INT_MIN
/2, INT_MAX
/2);
59 { /* Set our priority. */
60 int prio
= old_priority
+ adjustment
;
62 if (setpriority(PRIO_PROCESS
, 0, prio
) < 0) {
63 bb_perror_msg_and_die("setpriority(%d)", prio
);
67 BB_EXECVP_or_die(argv
);