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