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