1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29#include <sys/syscall.h>
30#include <asm/unistd.h>
31#include "libbb.h"
32
33static int ioprio_set(int which, int who, int ioprio)
34{
35 return syscall(SYS_ioprio_set, which, who, ioprio);
36}
37
38static int ioprio_get(int which, int who)
39{
40 return syscall(SYS_ioprio_get, which, who);
41}
42
43enum {
44 IOPRIO_WHO_PROCESS = 1,
45 IOPRIO_WHO_PGRP,
46 IOPRIO_WHO_USER
47};
48
49enum {
50 IOPRIO_CLASS_NONE,
51 IOPRIO_CLASS_RT,
52 IOPRIO_CLASS_BE,
53 IOPRIO_CLASS_IDLE
54};
55
56static const char to_prio[] ALIGN1 = "none\0realtime\0best-effort\0idle";
57
58#define IOPRIO_CLASS_SHIFT 13
59
60int ionice_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
61int ionice_main(int argc UNUSED_PARAM, char **argv)
62{
63
64 int ioclass = 0;
65 int pri = 0;
66 int pid = 0;
67 int opt;
68 enum {
69 OPT_n = 1 << 0,
70 OPT_c = 1 << 1,
71 OPT_p = 1 << 2,
72 OPT_t = 1 << 3,
73 };
74
75
76
77 opt = getopt32(argv, "+""n:+c:+p:+t", &pri, &ioclass, &pid);
78 argv += optind;
79
80 if (opt & OPT_c) {
81 if (ioclass > 3)
82 bb_error_msg_and_die("bad class %d", ioclass);
83
84
85
86
87
88
89
90
91 }
92
93 if (!(opt & (OPT_n|OPT_c))) {
94 if (!(opt & OPT_p) && *argv)
95 pid = xatoi_positive(*argv);
96
97 pri = ioprio_get(IOPRIO_WHO_PROCESS, pid);
98 if (pri == -1)
99 bb_perror_msg_and_die("ioprio_%cet", 'g');
100
101 ioclass = (pri >> IOPRIO_CLASS_SHIFT) & 0x3;
102 pri &= 0xff;
103 printf((ioclass == IOPRIO_CLASS_IDLE) ? "%s\n" : "%s: prio %d\n",
104 nth_string(to_prio, ioclass), pri);
105 } else {
106
107
108 pri |= (ioclass << IOPRIO_CLASS_SHIFT);
109 if (ioprio_set(IOPRIO_WHO_PROCESS, pid, pri) == -1)
110 if (!(opt & OPT_t))
111 bb_perror_msg_and_die("ioprio_%cet", 's');
112 if (argv[0]) {
113 BB_EXECVP_or_die(argv);
114 }
115 }
116
117 return EXIT_SUCCESS;
118}
119