1
2
3
4
5
6
7
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
45 int ioclass = 0;
46 int pri = 0;
47 int pid = 0;
48 int opt;
49 enum {
50 OPT_n = 1,
51 OPT_c = 2,
52 OPT_p = 4,
53 };
54
55
56 opt_complementary = "n+:c+:p+";
57
58 opt = getopt32(argv, "+n:c:p:", &pri, &ioclass, &pid);
59 argv += optind;
60
61 if (opt & OPT_c) {
62 if (ioclass > 3)
63 bb_error_msg_and_die("bad class %d", ioclass);
64
65
66
67
68
69
70
71
72 }
73
74 if (!(opt & (OPT_n|OPT_c))) {
75 if (!(opt & OPT_p) && *argv)
76 pid = xatoi_positive(*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
88
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[0]) {
93 BB_EXECVP_or_die(argv);
94 }
95 }
96
97 return EXIT_SUCCESS;
98}
99