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 tarball for details.
10 #include <sys/syscall.h>
11 #include <asm/unistd.h>
14 static int ioprio_set(int which, int who, int ioprio)
16 return syscall(SYS_ioprio_set, which, who, ioprio);
19 static int ioprio_get(int which, int who)
21 return syscall(SYS_ioprio_get, which, who);
25 IOPRIO_WHO_PROCESS = 1,
37 static const char to_prio[] = "none\0realtime\0best-effort\0idle";
39 #define IOPRIO_CLASS_SHIFT 13
41 int ionice_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
42 int ionice_main(int argc UNUSED_PARAM, char **argv)
47 int pid = 0; /* affect own porcess */
56 opt_complementary = "n+:c+:p+";
57 /* '+': stop at first non-option */
58 opt = getopt32(argv, "+n:c:p:", &pri, &ioclass, &pid);
63 bb_error_msg_and_die("bad class %d", ioclass);
64 // Do we need this (compat?)?
65 // if (ioclass == IOPRIO_CLASS_NONE)
66 // ioclass = IOPRIO_CLASS_BE;
67 // if (ioclass == IOPRIO_CLASS_IDLE) {
69 // // bb_error_msg("ignoring priority for idle class");
74 if (!(opt & (OPT_n|OPT_c))) {
75 if (!(opt & OPT_p) && *argv)
78 pri = ioprio_get(IOPRIO_WHO_PROCESS, pid);
80 bb_perror_msg_and_die("ioprio_%cet", 'g');
82 ioclass = (pri >> IOPRIO_CLASS_SHIFT) & 0x3;
84 printf((ioclass == IOPRIO_CLASS_IDLE) ? "%s\n" : "%s: prio %d\n",
85 nth_string(to_prio, ioclass), pri);
87 //printf("pri=%d class=%d val=%x\n",
88 //pri, ioclass, pri | (ioclass << IOPRIO_CLASS_SHIFT));
89 pri |= (ioclass << IOPRIO_CLASS_SHIFT);
90 if (ioprio_set(IOPRIO_WHO_PROCESS, pid, pri) == -1)
91 bb_perror_msg_and_die("ioprio_%cet", 's');
93 BB_EXECVP(*argv, argv);
94 bb_simple_perror_msg_and_die(*argv);