blob: 361c141b82d4859542af304918f77ef4ccdd9244 [file] [log] [blame]
Denis Vlasenko4acdb462009-01-31 21:45:57 +00001/* vi: set sw=4 ts=4: */
2/*
3 * ionice implementation for busybox based on linux-utils-ng 2.14
4 *
5 * Copyright (C) 2008 by <u173034@informatik.uni-oldenburg.de>
6 *
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8 */
9
10#include <sys/syscall.h>
11#include <asm/unistd.h>
12#include "libbb.h"
13
14static int ioprio_set(int which, int who, int ioprio)
15{
16 return syscall(SYS_ioprio_set, which, who, ioprio);
17}
18
19static int ioprio_get(int which, int who)
20{
21 return syscall(SYS_ioprio_get, which, who);
22}
23
24enum {
25 IOPRIO_WHO_PROCESS = 1,
26 IOPRIO_WHO_PGRP,
27 IOPRIO_WHO_USER
28};
29
30enum {
31 IOPRIO_CLASS_NONE,
32 IOPRIO_CLASS_RT,
33 IOPRIO_CLASS_BE,
34 IOPRIO_CLASS_IDLE
35};
36
37static const char to_prio[] = "none\0realtime\0best-effort\0idle";
38
39#define IOPRIO_CLASS_SHIFT 13
40
41int ionice_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
42int ionice_main(int argc UNUSED_PARAM, char **argv)
43{
44 /* Defaults */
45 int ioclass = 0;
46 int pri = 0;
47 int pid = 0; /* affect own porcess */
48 int opt;
49 enum {
50 OPT_n = 1,
51 OPT_c = 2,
52 OPT_p = 4,
53 };
54
55 /* Numeric params */
56 opt_complementary = "n+:c+:p+";
57 /* '+': stop at first non-option */
58 opt = getopt32(argv, "+n:c:p:", &pri, &ioclass, &pid);
59 argv += optind;
Denis Vlasenko3266aa92009-04-01 11:24:04 +000060
Denis Vlasenko4acdb462009-01-31 21:45:57 +000061 if (opt & OPT_c) {
62 if (ioclass > 3)
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) {
68// //if (opt & OPT_n)
69// // bb_error_msg("ignoring priority for idle class");
70// pri = 7;
71// }
72 }
Denis Vlasenko3266aa92009-04-01 11:24:04 +000073
Denis Vlasenko4acdb462009-01-31 21:45:57 +000074 if (!(opt & (OPT_n|OPT_c))) {
75 if (!(opt & OPT_p) && *argv)
76 pid = xatoi_u(*argv);
77
78 pri = ioprio_get(IOPRIO_WHO_PROCESS, pid);
79 if (pri == -1)
80 bb_perror_msg_and_die("ioprio_%cet", 'g');
81
82 ioclass = (pri >> IOPRIO_CLASS_SHIFT) & 0x3;
83 pri &= 0xff;
84 printf((ioclass == IOPRIO_CLASS_IDLE) ? "%s\n" : "%s: prio %d\n",
85 nth_string(to_prio, ioclass), pri);
86 } else {
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');
92 if (*argv) {
93 BB_EXECVP(*argv, argv);
94 bb_simple_perror_msg_and_die(*argv);
95 }
96 }
97
98 return EXIT_SUCCESS;
99}