blob: aa8b06cce0cb69d4454765066f39f50ccc734419 [file] [log] [blame]
Manuel Novoa III 2c511602005-02-13 20:14:05 +00001/* 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 Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Manuel Novoa III 2c511602005-02-13 20:14:05 +00008 */
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +01009//config:config NICE
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020010//config: bool "nice (1.8 kb)"
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010011//config: default y
12//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020013//config: nice runs a program with modified scheduling priority.
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010014
Denys Vlasenko5c527dc2017-08-04 19:55:01 +020015//applet:IF_NICE(APPLET_NOEXEC(nice, nice, BB_DIR_BIN, BB_SUID_DROP, nice))
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010016
17//kbuild:lib-$(CONFIG_NICE) += nice.o
Manuel Novoa III 2c511602005-02-13 20:14:05 +000018
Pere Orga34425382011-03-31 14:43:25 +020019//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 Orga34425382011-03-31 14:43:25 +020023//usage: "\n -n ADJUST Adjust priority by ADJUST"
24
Manuel Novoa III 2c511602005-02-13 20:14:05 +000025#include <sys/resource.h>
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000026#include "libbb.h"
Manuel Novoa III 2c511602005-02-13 20:14:05 +000027
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000028int nice_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenkoc9807d72018-01-08 10:13:11 +010029int nice_main(int argc UNUSED_PARAM, char **argv)
Manuel Novoa III 2c511602005-02-13 20:14:05 +000030{
Manuel Novoa III 2c511602005-02-13 20:14:05 +000031 int old_priority, adjustment;
32
Manuel Novoa III 2c511602005-02-13 20:14:05 +000033 old_priority = getpriority(PRIO_PROCESS, 0);
Manuel Novoa III 2c511602005-02-13 20:14:05 +000034
Denys Vlasenkofb132e42010-10-29 11:46:52 +020035 if (!*++argv) { /* No args, so (GNU) output current nice value. */
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000036 printf("%d\n", old_priority);
37 fflush_stdout_and_exit(EXIT_SUCCESS);
Manuel Novoa III 2c511602005-02-13 20:14:05 +000038 }
39
Denys Vlasenkofb132e42010-10-29 11:46:52 +020040 adjustment = 10; /* Set default adjustment. */
Manuel Novoa III 2c511602005-02-13 20:14:05 +000041
Denis Vlasenkoca3c9812006-10-08 23:36:17 +000042 if (argv[0][0] == '-') {
Denys Vlasenkoc9807d72018-01-08 10:13:11 +010043 char *nnn = argv[0] + 1;
44 if (nnn[0] == 'n') { /* -n */
45 nnn += 1;
46 if (!nnn[0]) { /* "-n NNN" */
47 nnn = *++argv;
Denis Vlasenkoca3c9812006-10-08 23:36:17 +000048 }
Denys Vlasenkoc9807d72018-01-08 10:13:11 +010049 /* else: "-nNNN" (w/o space) */
Denis Vlasenkoca3c9812006-10-08 23:36:17 +000050 }
Denys Vlasenkoc9807d72018-01-08 10:13:11 +010051 /* else: "-NNN" (NNN may be negative) - same as "-n NNN" */
52
53 if (!nnn || !argv[1]) { /* Missing priority or PROG! */
Manuel Novoa III 2c511602005-02-13 20:14:05 +000054 bb_show_usage();
55 }
Denys Vlasenkoc9807d72018-01-08 10:13:11 +010056 adjustment = xatoi_range(nnn, INT_MIN/2, INT_MAX/2);
57 argv++;
Manuel Novoa III 2c511602005-02-13 20:14:05 +000058 }
59
Denis Vlasenkoca3c9812006-10-08 23:36:17 +000060 { /* Set our priority. */
61 int prio = old_priority + adjustment;
Manuel Novoa III 2c511602005-02-13 20:14:05 +000062
Denis Vlasenkoca3c9812006-10-08 23:36:17 +000063 if (setpriority(PRIO_PROCESS, 0, prio) < 0) {
64 bb_perror_msg_and_die("setpriority(%d)", prio);
Manuel Novoa III 2c511602005-02-13 20:14:05 +000065 }
66 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000067
Pascal Bellard21e8e8d2010-07-04 00:57:03 +020068 BB_EXECVP_or_die(argv);
Manuel Novoa III 2c511602005-02-13 20:14:05 +000069}