1 /* vi: set sw=4 ts=4: */
3 * ionice implementation for busybox based on linux-utils-ng 2.14
5 * Copyright (C) 2008 by <u173034@informatik.uni-oldenburg.de>
7 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
10 //config: bool "ionice (3.6 kb)"
12 //config: select PLATFORM_LINUX
14 //config: Set/set program io scheduling class and priority
15 //config: Requires kernel >= 2.6.13
17 //applet:IF_IONICE(APPLET_NOEXEC(ionice, ionice, BB_DIR_BIN, BB_SUID_DROP, ionice))
19 //kbuild:lib-$(CONFIG_IONICE) += ionice.o
21 //usage:#define ionice_trivial_usage
22 //usage: "[-c 1-3] [-n 0-7] [-p PID] [PROG]"
23 //usage:#define ionice_full_usage "\n\n"
24 //usage: "Change I/O priority and class\n"
25 //usage: "\n -c Class. 1:realtime 2:best-effort 3:idle"
26 //usage: "\n -n Priority"
28 #include <sys/syscall.h>
29 #include <asm/unistd.h>
32 static int ioprio_set(int which, int who, int ioprio)
34 return syscall(SYS_ioprio_set, which, who, ioprio);
37 static int ioprio_get(int which, int who)
39 return syscall(SYS_ioprio_get, which, who);
43 IOPRIO_WHO_PROCESS = 1,
55 static const char to_prio[] ALIGN1 = "none\0realtime\0best-effort\0idle";
57 #define IOPRIO_CLASS_SHIFT 13
59 int ionice_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
60 int ionice_main(int argc UNUSED_PARAM, char **argv)
65 int pid = 0; /* affect own porcess */
74 /* '+': stop at first non-option */
75 opt = getopt32(argv, "+n:+c:+p:+", &pri, &ioclass, &pid);
80 bb_error_msg_and_die("bad class %d", ioclass);
81 // Do we need this (compat?)?
82 // if (ioclass == IOPRIO_CLASS_NONE)
83 // ioclass = IOPRIO_CLASS_BE;
84 // if (ioclass == IOPRIO_CLASS_IDLE) {
86 // // bb_error_msg("ignoring priority for idle class");
91 if (!(opt & (OPT_n|OPT_c))) {
92 if (!(opt & OPT_p) && *argv)
93 pid = xatoi_positive(*argv);
95 pri = ioprio_get(IOPRIO_WHO_PROCESS, pid);
97 bb_perror_msg_and_die("ioprio_%cet", 'g');
99 ioclass = (pri >> IOPRIO_CLASS_SHIFT) & 0x3;
101 printf((ioclass == IOPRIO_CLASS_IDLE) ? "%s\n" : "%s: prio %d\n",
102 nth_string(to_prio, ioclass), pri);
104 //printf("pri=%d class=%d val=%x\n",
105 //pri, ioclass, pri | (ioclass << IOPRIO_CLASS_SHIFT));
106 pri |= (ioclass << IOPRIO_CLASS_SHIFT);
107 if (ioprio_set(IOPRIO_WHO_PROCESS, pid, pri) == -1)
108 bb_perror_msg_and_die("ioprio_%cet", 's');
110 BB_EXECVP_or_die(argv);