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
30
31
32
33
34
35
36
37
38#define FOR_ionice
39#include "toys.h"
40#include <sys/syscall.h>
41
42GLOBALS(
43 long pid;
44 long level;
45 long class;
46)
47
48static int ioprio_get(void)
49{
50 return syscall(__NR_ioprio_get, 1, (int)TT.pid);
51}
52
53static int ioprio_set(void)
54{
55 int prio = ((int)TT.class << 13) | (int)TT.level;
56
57 return syscall(__NR_ioprio_set, 1, (int)TT.pid, prio);
58}
59
60void ionice_main(void)
61{
62 if (!TT.pid && !toys.optc) error_exit("Need -p or COMMAND");
63 if (toys.optflags == FLAG_p) {
64 int p = ioprio_get();
65 xprintf("%s: prio %d\n",
66 (char *[]){"unknown", "Realtime", "Best-effort", "Idle"}[(p>>13)&3],
67 p&7);
68 } else {
69 if (-1 == ioprio_set() && !(toys.optflags&FLAG_t)) perror_exit("set");
70 if (!TT.pid) xexec(toys.optargs);
71 }
72}
73
74void iorenice_main(void)
75{
76 char *classes[] = {"none", "rt", "be", "idle"};
77
78 TT.pid = atolx(*toys.optargs);
79 if (toys.optc == 1) {
80 int p = ioprio_get();
81
82 if (p == -1) perror_exit("read priority");
83 TT.class = (p>>13)&3;
84 p &= 7;
85 xprintf("Pid %ld, class %s (%ld), prio %d\n",
86 TT.pid, classes[TT.class], TT.class, p);
87 return;
88 }
89
90 for (TT.class = 0; TT.class<4; TT.class++)
91 if (!strcmp(toys.optargs[toys.optc-1], classes[TT.class])) break;
92 if (toys.optc == 3 || TT.class == 4) TT.level = atolx(toys.optargs[1]);
93 else TT.level = 4;
94 TT.class &= 3;
95
96 if (-1 == ioprio_set()) perror_exit("set");
97}
98