busybox/miscutils/chrt.c
Pascal Bellard 21e8e8da64 libbb: introduce and use BB_EXECVP_or_die()
function                                             old     new   delta
BB_EXECVP_or_die                                       -      47     +47
time_main                                           1042    1043      +1
chrt_main                                            371     364      -7
ionice_main                                          292     282     -10
setsid_main                                           69      56     -13
nohup_main                                           236     223     -13
cttyhack_main                                        266     253     -13
chroot_main                                           94      81     -13
chpst_main                                           746     733     -13
timeout_main                                         297     279     -18
taskset_main                                         541     522     -19
vfork_child                                           67      45     -22
parse                                                975     953     -22
lpd_main                                             770     748     -22
launch_helper                                        192     170     -22
tcpudpsvd_main                                      1810    1782     -28
nice_main                                            190     156     -34
env_main                                             242     206     -36
run_command                                          221     174     -47
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 1/17 up/down: 48/-352)         Total: -304 bytes

Signed-off-by: Pascal Bellard <pascal.bellard@ads-lu.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2010-07-04 00:57:03 +02:00

125 lines
3.1 KiB
C

/* vi: set sw=4 ts=4: */
/*
* chrt - manipulate real-time attributes of a process
* Copyright (c) 2006-2007 Bernhard Reutner-Fischer
*
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
*/
#include <sched.h>
#include "libbb.h"
#ifndef _POSIX_PRIORITY_SCHEDULING
#warning your system may be foobared
#endif
static const struct {
int policy;
char name[sizeof("SCHED_OTHER")];
} policies[] = {
{SCHED_OTHER, "SCHED_OTHER"},
{SCHED_FIFO, "SCHED_FIFO"},
{SCHED_RR, "SCHED_RR"}
};
//TODO: add
// -b, SCHED_BATCH
// -i, SCHED_IDLE
static void show_min_max(int pol)
{
const char *fmt = "%s min/max priority\t: %u/%u\n";
int max, min;
max = sched_get_priority_max(pol);
min = sched_get_priority_min(pol);
if ((max|min) < 0)
fmt = "%s not supported\n";
printf(fmt, policies[pol].name, min, max);
}
#define OPT_m (1<<0)
#define OPT_p (1<<1)
#define OPT_r (1<<2)
#define OPT_f (1<<3)
#define OPT_o (1<<4)
int chrt_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int chrt_main(int argc UNUSED_PARAM, char **argv)
{
pid_t pid = 0;
unsigned opt;
struct sched_param sp;
char *pid_str;
char *priority = priority; /* for compiler */
const char *current_new;
int policy = SCHED_RR;
/* at least 1 arg; only one policy accepted */
opt_complementary = "-1:r--fo:f--ro:r--fo";
opt = getopt32(argv, "+mprfo");
if (opt & OPT_r)
policy = SCHED_RR;
if (opt & OPT_f)
policy = SCHED_FIFO;
if (opt & OPT_o)
policy = SCHED_OTHER;
if (opt & OPT_m) { /* print min/max */
show_min_max(SCHED_FIFO);
show_min_max(SCHED_RR);
show_min_max(SCHED_OTHER);
fflush_stdout_and_exit(EXIT_SUCCESS);
}
argv += optind;
if (opt & OPT_p) {
pid_str = *argv++;
if (*argv) { /* "-p <priority> <pid> [...]" */
priority = pid_str;
pid_str = *argv;
}
/* else "-p <pid>", and *argv == NULL */
pid = xatoul_range(pid_str, 1, ((unsigned)(pid_t)ULONG_MAX) >> 1);
} else {
priority = *argv++;
if (!*argv)
bb_show_usage();
}
current_new = "current\0new";
if (opt & OPT_p) {
int pol;
print_rt_info:
pol = sched_getscheduler(pid);
if (pol < 0)
bb_perror_msg_and_die("can't %cet pid %d's policy", 'g', pid);
printf("pid %d's %s scheduling policy: %s\n",
pid, current_new, policies[pol].name);
if (sched_getparam(pid, &sp))
bb_perror_msg_and_die("can't get pid %d's attributes", pid);
printf("pid %d's %s scheduling priority: %d\n",
pid, current_new, sp.sched_priority);
if (!*argv) {
/* Either it was just "-p <pid>",
* or it was "-p <priority> <pid>" and we came here
* for the second time (see goto below) */
return EXIT_SUCCESS;
}
*argv = NULL;
current_new += 8;
}
/* from the manpage of sched_getscheduler:
[...] sched_priority can have a value in the range 0 to 99.
[...] SCHED_OTHER or SCHED_BATCH must be assigned static priority 0.
[...] SCHED_FIFO or SCHED_RR can have static priority in 1..99 range.
*/
sp.sched_priority = xstrtou_range(priority, 0, policy != SCHED_OTHER ? 1 : 0, 99);
if (sched_setscheduler(pid, policy, &sp) < 0)
bb_perror_msg_and_die("can't %cet pid %d's policy", 's', pid);
if (!argv[0]) /* "-p <priority> <pid> [...]" */
goto print_rt_info;
BB_EXECVP_or_die(argv);
}