2005-02-14 01:44:05 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
2000-07-22 03:02:12 +05:30
|
|
|
/*
|
2005-02-14 01:44:05 +05:30
|
|
|
* renice implementation for busybox
|
2000-07-22 03:02:12 +05:30
|
|
|
*
|
2005-02-14 01:44:05 +05:30
|
|
|
* Copyright (C) 2005 Manuel Novoa III <mjn3@codepoet.org>
|
2000-07-22 03:02:12 +05:30
|
|
|
*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
2000-07-22 03:02:12 +05:30
|
|
|
*/
|
2005-02-14 01:44:05 +05:30
|
|
|
/* Notes:
|
|
|
|
* Setting an absolute priority was obsoleted in SUSv2 and removed
|
|
|
|
* in SUSv3. However, the common linux version of renice does
|
|
|
|
* absolute and not relative. So we'll continue supporting absolute,
|
|
|
|
* although the stdout logging has been removed since both SUSv2 and
|
|
|
|
* SUSv3 specify that stdout isn't used.
|
|
|
|
*
|
|
|
|
* This version is lenient in that it doesn't require any IDs. The
|
|
|
|
* options -p, -g, and -u are treated as mode switches for the
|
|
|
|
* following IDs (if any). Multiple switches are allowed.
|
|
|
|
*/
|
2016-11-23 10:53:44 +05:30
|
|
|
//config:config RENICE
|
2017-07-19 01:31:24 +05:30
|
|
|
//config: bool "renice (3.8 kb)"
|
2016-11-23 10:53:44 +05:30
|
|
|
//config: default y
|
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: Renice alters the scheduling priority of one or more running
|
|
|
|
//config: processes.
|
2016-11-23 10:53:44 +05:30
|
|
|
|
2017-08-07 20:17:34 +05:30
|
|
|
//applet:IF_RENICE(APPLET_NOEXEC(renice, renice, BB_DIR_USR_BIN, BB_SUID_DROP, renice))
|
2016-11-23 10:53:44 +05:30
|
|
|
|
|
|
|
//kbuild:lib-$(CONFIG_RENICE) += renice.o
|
2005-02-14 01:44:05 +05:30
|
|
|
|
2011-04-11 06:59:49 +05:30
|
|
|
//usage:#define renice_trivial_usage
|
2016-03-07 19:51:54 +05:30
|
|
|
//usage: "[-n] PRIORITY [[-p | -g | -u] ID...]..."
|
2011-04-11 06:59:49 +05:30
|
|
|
//usage:#define renice_full_usage "\n\n"
|
2016-03-07 19:51:54 +05:30
|
|
|
//usage: "Change scheduling priority of a running process\n"
|
|
|
|
//usage: "\n -n Add PRIORITY to current nice value"
|
|
|
|
//usage: "\n Without -n, nice value is set to PRIORITY"
|
|
|
|
//usage: "\n -p Process ids (default)"
|
|
|
|
//usage: "\n -g Process group ids"
|
|
|
|
//usage: "\n -u Process user names"
|
2011-04-11 06:59:49 +05:30
|
|
|
|
2007-05-27 00:30:18 +05:30
|
|
|
#include "libbb.h"
|
2000-07-22 03:02:12 +05:30
|
|
|
#include <sys/resource.h>
|
|
|
|
|
2007-10-11 15:35:36 +05:30
|
|
|
int renice_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2008-07-05 14:48:54 +05:30
|
|
|
int renice_main(int argc UNUSED_PARAM, char **argv)
|
2000-07-22 03:02:12 +05:30
|
|
|
{
|
2007-08-13 02:28:27 +05:30
|
|
|
static const char Xetpriority_msg[] ALIGN1 = "%cetpriority";
|
2005-02-14 01:44:05 +05:30
|
|
|
|
|
|
|
int retval = EXIT_SUCCESS;
|
2010-10-28 22:27:19 +05:30
|
|
|
int which = PRIO_PROCESS; /* Default 'which' value. */
|
2005-02-14 01:44:05 +05:30
|
|
|
int use_relative = 0;
|
|
|
|
int adjustment, new_priority;
|
2006-10-08 18:19:22 +05:30
|
|
|
unsigned who;
|
2006-10-09 05:06:17 +05:30
|
|
|
char *arg;
|
2004-03-15 13:59:22 +05:30
|
|
|
|
2006-11-18 01:59:00 +05:30
|
|
|
/* Yes, they are not #defines in glibc 2.4! #if won't work */
|
2018-01-14 23:35:02 +05:30
|
|
|
BUILD_BUG_ON(PRIO_PROCESS < CHAR_MIN || PRIO_PROCESS > CHAR_MAX);
|
|
|
|
BUILD_BUG_ON(PRIO_PGRP < CHAR_MIN || PRIO_PGRP > CHAR_MAX);
|
|
|
|
BUILD_BUG_ON(PRIO_USER < CHAR_MIN || PRIO_USER > CHAR_MAX);
|
2006-11-18 01:59:00 +05:30
|
|
|
|
2006-10-09 05:06:17 +05:30
|
|
|
arg = *++argv;
|
2004-03-15 13:59:22 +05:30
|
|
|
|
2005-02-14 01:44:05 +05:30
|
|
|
/* Check if we are using a relative adjustment. */
|
2006-10-09 05:06:17 +05:30
|
|
|
if (arg && arg[0] == '-' && arg[1] == 'n') {
|
2005-02-14 01:44:05 +05:30
|
|
|
use_relative = 1;
|
2006-10-09 05:06:17 +05:30
|
|
|
if (!arg[2])
|
|
|
|
arg = *++argv;
|
|
|
|
else
|
|
|
|
arg += 2;
|
2005-02-14 01:44:05 +05:30
|
|
|
}
|
|
|
|
|
2010-10-28 22:27:19 +05:30
|
|
|
if (!arg) { /* No args? Then show usage. */
|
2005-02-14 01:44:05 +05:30
|
|
|
bb_show_usage();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the priority adjustment (absolute or relative). */
|
2006-10-09 05:06:17 +05:30
|
|
|
adjustment = xatoi_range(arg, INT_MIN/2, INT_MAX/2);
|
2004-03-15 13:59:22 +05:30
|
|
|
|
2006-10-09 05:06:17 +05:30
|
|
|
while ((arg = *++argv) != NULL) {
|
2005-02-14 01:44:05 +05:30
|
|
|
/* Check for a mode switch. */
|
2006-10-09 05:06:17 +05:30
|
|
|
if (arg[0] == '-' && arg[1]) {
|
2007-08-13 02:28:27 +05:30
|
|
|
static const char opts[] ALIGN1 = {
|
|
|
|
'p', 'g', 'u', 0, PRIO_PROCESS, PRIO_PGRP, PRIO_USER
|
|
|
|
};
|
2006-10-09 05:06:17 +05:30
|
|
|
const char *p = strchr(opts, arg[1]);
|
2006-10-08 18:19:22 +05:30
|
|
|
if (p) {
|
2005-02-14 01:44:05 +05:30
|
|
|
which = p[4];
|
2006-10-09 05:06:17 +05:30
|
|
|
if (!arg[2])
|
|
|
|
continue;
|
|
|
|
arg += 2;
|
2005-02-14 01:44:05 +05:30
|
|
|
}
|
|
|
|
}
|
2004-03-15 13:59:22 +05:30
|
|
|
|
2005-02-14 01:44:05 +05:30
|
|
|
/* Process an ID arg. */
|
|
|
|
if (which == PRIO_USER) {
|
|
|
|
struct passwd *p;
|
2018-01-15 00:10:00 +05:30
|
|
|
/* NB: use of getpwnam makes it risky to be NOFORK, switch to getpwnam_r? */
|
2006-10-09 05:06:17 +05:30
|
|
|
p = getpwnam(arg);
|
2006-10-08 18:19:22 +05:30
|
|
|
if (!p) {
|
2008-07-21 20:11:33 +05:30
|
|
|
bb_error_msg("unknown user %s", arg);
|
2005-02-14 01:44:05 +05:30
|
|
|
goto HAD_ERROR;
|
|
|
|
}
|
|
|
|
who = p->pw_uid;
|
2000-07-22 03:02:12 +05:30
|
|
|
} else {
|
2006-11-27 20:13:21 +05:30
|
|
|
who = bb_strtou(arg, NULL, 10);
|
|
|
|
if (errno) {
|
2010-08-29 16:59:02 +05:30
|
|
|
bb_error_msg("invalid number '%s'", arg);
|
2005-02-14 01:44:05 +05:30
|
|
|
goto HAD_ERROR;
|
|
|
|
}
|
2000-07-22 03:02:12 +05:30
|
|
|
}
|
2005-02-14 01:44:05 +05:30
|
|
|
|
|
|
|
/* Get priority to use, and set it. */
|
|
|
|
if (use_relative) {
|
|
|
|
int old_priority;
|
|
|
|
|
2010-10-28 22:27:19 +05:30
|
|
|
errno = 0; /* Needed for getpriority error detection. */
|
2005-02-14 01:44:05 +05:30
|
|
|
old_priority = getpriority(which, who);
|
|
|
|
if (errno) {
|
2006-10-09 05:06:17 +05:30
|
|
|
bb_perror_msg(Xetpriority_msg, 'g');
|
2005-02-14 01:44:05 +05:30
|
|
|
goto HAD_ERROR;
|
|
|
|
}
|
|
|
|
|
2006-10-09 05:06:17 +05:30
|
|
|
new_priority = old_priority + adjustment;
|
2005-02-14 01:44:05 +05:30
|
|
|
} else {
|
|
|
|
new_priority = adjustment;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (setpriority(which, who, new_priority) == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2006-10-09 05:06:17 +05:30
|
|
|
bb_perror_msg(Xetpriority_msg, 's');
|
|
|
|
HAD_ERROR:
|
2005-02-14 01:44:05 +05:30
|
|
|
retval = EXIT_FAILURE;
|
2000-07-22 03:02:12 +05:30
|
|
|
}
|
2000-12-06 01:37:27 +05:30
|
|
|
|
2017-04-17 19:43:32 +05:30
|
|
|
/* No need to check for errors outputting to stderr since, if it
|
2005-02-14 01:44:05 +05:30
|
|
|
* was used, the HAD_ERROR label was reached and retval was set. */
|
|
|
|
|
|
|
|
return retval;
|
2000-07-22 03:02:12 +05:30
|
|
|
}
|