blob: 5fcb653a839485c044e17cb4f71f0882c1e48492 [file] [log] [blame]
Denis Vlasenko4acdb462009-01-31 21:45:57 +00001/* vi: set sw=4 ts=4: */
2/*
3 * ionice implementation for busybox based on linux-utils-ng 2.14
4 *
5 * Copyright (C) 2008 by <u173034@informatik.uni-oldenburg.de>
6 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Denis Vlasenko4acdb462009-01-31 21:45:57 +00008 */
9
Pere Orga5bc8c002011-04-11 03:29:49 +020010//usage:#define ionice_trivial_usage
11//usage: "[-c 1-3] [-n 0-7] [-p PID] [PROG]"
12//usage:#define ionice_full_usage "\n\n"
13//usage: "Change I/O priority and class\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020014//usage: "\n -c Class. 1:realtime 2:best-effort 3:idle"
15//usage: "\n -n Priority"
16
Denis Vlasenko4acdb462009-01-31 21:45:57 +000017#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
Denys Vlasenko3e134eb2016-04-22 18:09:21 +020044static const char to_prio[] ALIGN1 = "none\0realtime\0best-effort\0idle";
Denis Vlasenko4acdb462009-01-31 21:45:57 +000045
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 /* Defaults */
52 int ioclass = 0;
53 int pri = 0;
54 int pid = 0; /* affect own porcess */
55 int opt;
56 enum {
57 OPT_n = 1,
58 OPT_c = 2,
59 OPT_p = 4,
60 };
61
62 /* Numeric params */
Denis Vlasenko4acdb462009-01-31 21:45:57 +000063 /* '+': stop at first non-option */
Denys Vlasenko237bedd2016-07-06 21:58:02 +020064 opt = getopt32(argv, "+n:+c:+p:+", &pri, &ioclass, &pid);
Denis Vlasenko4acdb462009-01-31 21:45:57 +000065 argv += optind;
Denis Vlasenko3266aa92009-04-01 11:24:04 +000066
Denis Vlasenko4acdb462009-01-31 21:45:57 +000067 if (opt & OPT_c) {
68 if (ioclass > 3)
69 bb_error_msg_and_die("bad class %d", ioclass);
70// Do we need this (compat?)?
71// if (ioclass == IOPRIO_CLASS_NONE)
72// ioclass = IOPRIO_CLASS_BE;
73// if (ioclass == IOPRIO_CLASS_IDLE) {
74// //if (opt & OPT_n)
75// // bb_error_msg("ignoring priority for idle class");
76// pri = 7;
77// }
78 }
Denis Vlasenko3266aa92009-04-01 11:24:04 +000079
Denis Vlasenko4acdb462009-01-31 21:45:57 +000080 if (!(opt & (OPT_n|OPT_c))) {
81 if (!(opt & OPT_p) && *argv)
Denys Vlasenko77832482010-08-12 14:14:45 +020082 pid = xatoi_positive(*argv);
Denis Vlasenko4acdb462009-01-31 21:45:57 +000083
84 pri = ioprio_get(IOPRIO_WHO_PROCESS, pid);
85 if (pri == -1)
86 bb_perror_msg_and_die("ioprio_%cet", 'g');
87
88 ioclass = (pri >> IOPRIO_CLASS_SHIFT) & 0x3;
89 pri &= 0xff;
90 printf((ioclass == IOPRIO_CLASS_IDLE) ? "%s\n" : "%s: prio %d\n",
91 nth_string(to_prio, ioclass), pri);
92 } else {
93//printf("pri=%d class=%d val=%x\n",
94//pri, ioclass, pri | (ioclass << IOPRIO_CLASS_SHIFT));
95 pri |= (ioclass << IOPRIO_CLASS_SHIFT);
96 if (ioprio_set(IOPRIO_WHO_PROCESS, pid, pri) == -1)
97 bb_perror_msg_and_die("ioprio_%cet", 's');
Denys Vlasenko41ddd9f2010-06-25 01:46:53 +020098 if (argv[0]) {
Pascal Bellard21e8e8d2010-07-04 00:57:03 +020099 BB_EXECVP_or_die(argv);
Denis Vlasenko4acdb462009-01-31 21:45:57 +0000100 }
101 }
102
103 return EXIT_SUCCESS;
104}