svn commit: trunk/busybox: include miscutils scripts

aldot at busybox.net aldot at busybox.net
Fri Mar 9 16:56:38 UTC 2007


Author: aldot
Date: 2007-03-09 08:56:38 -0800 (Fri, 09 Mar 2007)
New Revision: 18055

Log:
- add chrt applet.
   text    data     bss     dec     hex filename
    769       0       0     769     301 miscutils/chrt.o
and could use some further shrinkage


Added:
   trunk/busybox/miscutils/chrt.c

Modified:
   trunk/busybox/include/applets.h
   trunk/busybox/include/usage.h
   trunk/busybox/miscutils/Config.in
   trunk/busybox/miscutils/Kbuild
   trunk/busybox/scripts/defconfig


Changeset:
Modified: trunk/busybox/include/applets.h
===================================================================
--- trunk/busybox/include/applets.h	2007-03-09 16:43:01 UTC (rev 18054)
+++ trunk/busybox/include/applets.h	2007-03-09 16:56:38 UTC (rev 18055)
@@ -74,6 +74,7 @@
 USE_CHOWN(APPLET(chown, _BB_DIR_BIN, _BB_SUID_NEVER))
 USE_CHPST(APPLET(chpst, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
 USE_CHROOT(APPLET(chroot, _BB_DIR_USR_SBIN, _BB_SUID_NEVER))
+USE_CHRT(APPLET(chrt, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
 USE_CHVT(APPLET(chvt, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
 USE_CKSUM(APPLET(cksum, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
 USE_CLEAR(APPLET(clear, _BB_DIR_USR_BIN, _BB_SUID_NEVER))

Modified: trunk/busybox/include/usage.h
===================================================================
--- trunk/busybox/include/usage.h	2007-03-09 16:43:01 UTC (rev 18054)
+++ trunk/busybox/include/usage.h	2007-03-09 16:56:38 UTC (rev 18055)
@@ -382,6 +382,22 @@
 #define bbsh_full_usage \
        "The bbsh shell (command interpreter)"
 
+#define chrt_trivial_usage \
+	"[OPTION]... [prio] [pid | command [arg]...]"
+#define chrt_full_usage \
+	"manipulate real-time attributes of a process" \
+	"\n\nOptions:\n" \
+	"	-p	operate on pid\n" \
+	"	-r	set scheduling policy to SCHED_RR\n" \
+	"	-f	set scheduling policy to SCHED_FIFO\n" \
+	"	-o	set scheduling policy to SCHED_OTHER\n" \
+	"	-m	show min and max priorities"
+
+#define chrt_example_usage \
+	"$ chrt -r 4 sleep 900 ; x=$!\n" \
+	"$ chrt -f -p 3 $x\n" \
+	"You need CAP_SYS_NICE privileges to set scheduling attributes of a process"
+
 #define cp_trivial_usage \
        "[OPTION]... SOURCE DEST"
 #define cp_full_usage \

Modified: trunk/busybox/miscutils/Config.in
===================================================================
--- trunk/busybox/miscutils/Config.in	2007-03-09 16:43:01 UTC (rev 18054)
+++ trunk/busybox/miscutils/Config.in	2007-03-09 16:56:38 UTC (rev 18055)
@@ -19,6 +19,13 @@
 	  The bbconfig applet will print the config file with which
 	  busybox was built.
 
+config CHRT
+	bool "chrt"
+	default n
+	help
+	  manipulate real-time attributes of a process.
+	  This requires sched_{g,s}etparam support in your libc.
+
 config CROND
 	bool "crond"
 	default n

Modified: trunk/busybox/miscutils/Kbuild
===================================================================
--- trunk/busybox/miscutils/Kbuild	2007-03-09 16:43:01 UTC (rev 18054)
+++ trunk/busybox/miscutils/Kbuild	2007-03-09 16:56:38 UTC (rev 18055)
@@ -7,6 +7,7 @@
 lib-y:=
 lib-$(CONFIG_ADJTIMEX)    += adjtimex.o
 lib-$(CONFIG_BBCONFIG)    += bbconfig.o
+lib-$(CONFIG_CHRT)        += chrt.o
 lib-$(CONFIG_CROND)       += crond.o
 lib-$(CONFIG_CRONTAB)     += crontab.o
 lib-$(CONFIG_DC)          += dc.o

Added: trunk/busybox/miscutils/chrt.c
===================================================================
--- trunk/busybox/miscutils/chrt.c	                        (rev 0)
+++ trunk/busybox/miscutils/chrt.c	2007-03-09 16:56:38 UTC (rev 18055)
@@ -0,0 +1,124 @@
+/* vi: set sw=4 ts=4: */
+/*
+ * chrt - manipulate real-time attributes of a process
+ * Copyright (c) 2006-2007 Bernhard Fischer
+ *
+ * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
+ */
+
+#include "busybox.h"
+#include <unistd.h>
+#include <sched.h>
+#include <getopt.h> /* optind */
+#ifndef _POSIX_PRIORITY_SCHEDULING
+#warning your system may be foobared
+#endif
+static const struct {
+	const int policy;
+	const char const name[12];
+} policies[] = {
+	{SCHED_OTHER, "SCHED_OTHER"},
+	{SCHED_FIFO, "SCHED_FIFO"},
+	{SCHED_RR, "SCHED_RR"}
+};
+
+static void show_min_max(int pol) {
+	const char *fmt = "%s min/max priority\t: %d/%d\n\0%s not supported?\n";
+	int max, min;
+	max = sched_get_priority_max(pol);
+	min = sched_get_priority_min(pol);
+	if (max >= 0 && min >= 0)
+		printf(fmt, policies[pol].name, min, max);
+	else {
+		fmt += 29;
+		printf(fmt, policies[pol].name);
+	}
+}
+
+#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);
+int chrt_main(int argc, char** argv)
+{
+	pid_t pid = 0;
+	unsigned opt;
+	struct sched_param sp;
+	char *p_opt = NULL, *priority = NULL;
+	const char *state = "current\0new";
+	int prio = 0, policy = SCHED_RR;
+
+	opt_complementary = "r--fo:f--ro:r--fo"; /* only one policy accepted */
+	opt = getopt32(argc, argv, "+mp:rfo", &p_opt);
+	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);
+	}
+	if (opt & OPT_p) {
+		if (argc == optind+1) { /* -p <priority> <pid> */
+			priority = p_opt;
+			p_opt = argv[optind];
+		}
+		argv += optind; /* me -p <arg> */
+		pid = xatoul_range(p_opt, 1, ULONG_MAX); /* -p <pid> */
+	} else {
+		argv += optind; /* me -p <arg> */
+		priority = *argv;
+	}
+	if (priority) {
+		/* 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
+		   the  static  priority  0. [...] SCHED_FIFO  or
+		   SCHED_RR can have a static priority in the range 1 to 99.
+		 */
+		prio = xstrtol_range(priority, 0, policy == SCHED_OTHER
+													 ? 0 : 1, 99);
+	}
+
+	if (opt & OPT_p) {
+		int pol = 0;
+print_rt_info:
+		pol = sched_getscheduler(pid);
+		if (pol < 0)
+			bb_perror_msg_and_die("failed to %cet pid %d's policy", 'g', pid);
+		printf("pid %d's %s scheduling policy: %s\n",
+				pid, state, policies[pol].name);
+		if (sched_getparam(pid, &sp))
+			bb_perror_msg_and_die("failed to get pid %d's attributes", pid);
+		printf("pid %d's %s scheduling priority: %d\n",
+				pid, state, sp.sched_priority);
+		if (!*argv) /* no new prio given or we did print already, done. */
+			return EXIT_SUCCESS;
+	}
+
+	sp.sched_priority = prio;
+	if (sched_setscheduler(pid, policy, &sp) < 0)
+		bb_perror_msg_and_die("failed to %cet pid %d's policy", 's', pid);
+	if (opt & OPT_p) {
+		state += 8;
+		++argv;
+		goto print_rt_info;
+	}
+	++argv;
+	BB_EXECVP(*argv, argv);
+	bb_perror_msg_and_die("%s", *argv);
+}
+#undef OPT_p
+#undef OPT_r
+#undef OPT_f
+#undef OPT_o
+#undef OPT_m

Modified: trunk/busybox/scripts/defconfig
===================================================================
--- trunk/busybox/scripts/defconfig	2007-03-09 16:43:01 UTC (rev 18054)
+++ trunk/busybox/scripts/defconfig	2007-03-09 16:56:38 UTC (rev 18055)
@@ -454,6 +454,7 @@
 #
 CONFIG_ADJTIMEX=y
 # CONFIG_BBCONFIG is not set
+CONFIG_CHRT=y
 CONFIG_CROND=y
 # CONFIG_DEBUG_CROND_OPTION is not set
 CONFIG_FEATURE_CROND_CALL_SENDMAIL=y




More information about the busybox-cvs mailing list