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