2005-02-14 01:44:05 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* nice implementation for busybox
|
|
|
|
*
|
|
|
|
* Copyright (C) 2005 Manuel Novoa III <mjn3@codepoet.org>
|
|
|
|
*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
2005-02-14 01:44:05 +05:30
|
|
|
*/
|
2016-11-23 19:16:56 +05:30
|
|
|
//config:config NICE
|
2017-07-19 01:31:24 +05:30
|
|
|
//config: bool "nice (1.8 kb)"
|
2016-11-23 19:16:56 +05:30
|
|
|
//config: default y
|
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: nice runs a program with modified scheduling priority.
|
2016-11-23 19:16:56 +05:30
|
|
|
|
2017-08-04 23:25:01 +05:30
|
|
|
//applet:IF_NICE(APPLET_NOEXEC(nice, nice, BB_DIR_BIN, BB_SUID_DROP, nice))
|
2016-11-23 19:16:56 +05:30
|
|
|
|
|
|
|
//kbuild:lib-$(CONFIG_NICE) += nice.o
|
2005-02-14 01:44:05 +05:30
|
|
|
|
2011-03-31 18:13:25 +05:30
|
|
|
//usage:#define nice_trivial_usage
|
|
|
|
//usage: "[-n ADJUST] [PROG ARGS]"
|
|
|
|
//usage:#define nice_full_usage "\n\n"
|
|
|
|
//usage: "Change scheduling priority, run PROG\n"
|
|
|
|
//usage: "\n -n ADJUST Adjust priority by ADJUST"
|
|
|
|
|
2005-02-14 01:44:05 +05:30
|
|
|
#include <sys/resource.h>
|
2007-05-27 00:30:18 +05:30
|
|
|
#include "libbb.h"
|
2005-02-14 01:44:05 +05:30
|
|
|
|
2007-10-11 15:35:36 +05:30
|
|
|
int nice_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2005-02-14 01:44:05 +05:30
|
|
|
int nice_main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int old_priority, adjustment;
|
|
|
|
|
|
|
|
old_priority = getpriority(PRIO_PROCESS, 0);
|
|
|
|
|
2010-10-29 15:16:52 +05:30
|
|
|
if (!*++argv) { /* No args, so (GNU) output current nice value. */
|
2006-10-27 04:51:47 +05:30
|
|
|
printf("%d\n", old_priority);
|
|
|
|
fflush_stdout_and_exit(EXIT_SUCCESS);
|
2005-02-14 01:44:05 +05:30
|
|
|
}
|
|
|
|
|
2010-10-29 15:16:52 +05:30
|
|
|
adjustment = 10; /* Set default adjustment. */
|
2005-02-14 01:44:05 +05:30
|
|
|
|
2006-10-09 05:06:17 +05:30
|
|
|
if (argv[0][0] == '-') {
|
|
|
|
if (argv[0][1] == 'n') { /* -n */
|
|
|
|
if (argv[0][2]) { /* -nNNNN (w/o space) */
|
|
|
|
argv[0] += 2; argv--; argc++;
|
|
|
|
}
|
|
|
|
} else { /* -NNN (NNN may be negative) == -n NNN */
|
|
|
|
argv[0] += 1; argv--; argc++;
|
|
|
|
}
|
2010-10-29 15:16:52 +05:30
|
|
|
if (argc < 4) { /* Missing priority and/or utility! */
|
2005-02-14 01:44:05 +05:30
|
|
|
bb_show_usage();
|
|
|
|
}
|
2006-10-09 05:06:17 +05:30
|
|
|
adjustment = xatoi_range(argv[1], INT_MIN/2, INT_MAX/2);
|
2005-02-14 01:44:05 +05:30
|
|
|
argv += 2;
|
|
|
|
}
|
|
|
|
|
2006-10-09 05:06:17 +05:30
|
|
|
{ /* Set our priority. */
|
|
|
|
int prio = old_priority + adjustment;
|
2005-02-14 01:44:05 +05:30
|
|
|
|
2006-10-09 05:06:17 +05:30
|
|
|
if (setpriority(PRIO_PROCESS, 0, prio) < 0) {
|
|
|
|
bb_perror_msg_and_die("setpriority(%d)", prio);
|
2005-02-14 01:44:05 +05:30
|
|
|
}
|
|
|
|
}
|
2006-01-25 05:38:53 +05:30
|
|
|
|
2010-07-04 04:27:03 +05:30
|
|
|
BB_EXECVP_or_die(argv);
|
2005-02-14 01:44:05 +05:30
|
|
|
}
|