blob: b30d5f78d3d0450703e4d3e842a9625a9d3a02bc [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 */
Denys Vlasenkofb4da162016-11-22 23:14:24 +01009//config:config IONICE
Denys Vlasenkob097a842018-12-28 03:20:17 +010010//config: bool "ionice (3.8 kb)"
Denys Vlasenkofb4da162016-11-22 23:14:24 +010011//config: default y
Samuel Thibault77216c32022-10-16 02:04:59 +020012//config: select PLATFORM_LINUX
Denys Vlasenkofb4da162016-11-22 23:14:24 +010013//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020014//config: Set/set program io scheduling class and priority
15//config: Requires kernel >= 2.6.13
Denis Vlasenko4acdb462009-01-31 21:45:57 +000016
Denys Vlasenko5c527dc2017-08-04 19:55:01 +020017//applet:IF_IONICE(APPLET_NOEXEC(ionice, ionice, BB_DIR_BIN, BB_SUID_DROP, ionice))
Denys Vlasenkof88e3bf2016-11-22 23:54:17 +010018
19//kbuild:lib-$(CONFIG_IONICE) += ionice.o
20
Pere Orga5bc8c002011-04-11 03:29:49 +020021//usage:#define ionice_trivial_usage
Denys Vlasenkof02b64d2021-06-17 13:45:13 +020022//usage: "[-c 1-3] [-n 0-7] [-t] { -p PID | PROG ARGS }"
Denys Vlasenkoe2b92152021-06-14 20:47:20 +020023//TODO: | -P PGID | -u UID; also -pPu can take _list of_ IDs
Pere Orga5bc8c002011-04-11 03:29:49 +020024//usage:#define ionice_full_usage "\n\n"
25//usage: "Change I/O priority and class\n"
Denys Vlasenkoa2f18d92020-12-18 04:12:51 +010026//usage: "\n -c N Class. 1:realtime 2:best-effort 3:idle"
27//usage: "\n -n N Priority"
Denys Vlasenkof02b64d2021-06-17 13:45:13 +020028//usage: "\n -t Ignore errors"
Pere Orga5bc8c002011-04-11 03:29:49 +020029
Denis Vlasenko4acdb462009-01-31 21:45:57 +000030#include <sys/syscall.h>
31#include <asm/unistd.h>
32#include "libbb.h"
33
34static int ioprio_set(int which, int who, int ioprio)
35{
36 return syscall(SYS_ioprio_set, which, who, ioprio);
37}
38
39static int ioprio_get(int which, int who)
40{
41 return syscall(SYS_ioprio_get, which, who);
42}
43
44enum {
45 IOPRIO_WHO_PROCESS = 1,
46 IOPRIO_WHO_PGRP,
47 IOPRIO_WHO_USER
48};
49
50enum {
51 IOPRIO_CLASS_NONE,
52 IOPRIO_CLASS_RT,
53 IOPRIO_CLASS_BE,
54 IOPRIO_CLASS_IDLE
55};
56
Denys Vlasenko3e134eb2016-04-22 18:09:21 +020057static const char to_prio[] ALIGN1 = "none\0realtime\0best-effort\0idle";
Denis Vlasenko4acdb462009-01-31 21:45:57 +000058
59#define IOPRIO_CLASS_SHIFT 13
60
61int ionice_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
62int ionice_main(int argc UNUSED_PARAM, char **argv)
63{
64 /* Defaults */
65 int ioclass = 0;
66 int pri = 0;
Denys Vlasenkoa2f18d92020-12-18 04:12:51 +010067 int pid = 0; /* affect own process */
Denis Vlasenko4acdb462009-01-31 21:45:57 +000068 int opt;
69 enum {
Denys Vlasenkof02b64d2021-06-17 13:45:13 +020070 OPT_n = 1 << 0,
71 OPT_c = 1 << 1,
72 OPT_p = 1 << 2,
73 OPT_t = 1 << 3,
Denis Vlasenko4acdb462009-01-31 21:45:57 +000074 };
75
Denis Vlasenko4acdb462009-01-31 21:45:57 +000076 /* '+': stop at first non-option */
Denys Vlasenkof02b64d2021-06-17 13:45:13 +020077 /* numeric params for -n -c -p */
78 opt = getopt32(argv, "+""n:+c:+p:+t", &pri, &ioclass, &pid);
Denis Vlasenko4acdb462009-01-31 21:45:57 +000079 argv += optind;
Denis Vlasenko3266aa92009-04-01 11:24:04 +000080
Denis Vlasenko4acdb462009-01-31 21:45:57 +000081 if (opt & OPT_c) {
82 if (ioclass > 3)
83 bb_error_msg_and_die("bad class %d", ioclass);
84// Do we need this (compat?)?
85// if (ioclass == IOPRIO_CLASS_NONE)
86// ioclass = IOPRIO_CLASS_BE;
87// if (ioclass == IOPRIO_CLASS_IDLE) {
88// //if (opt & OPT_n)
89// // bb_error_msg("ignoring priority for idle class");
90// pri = 7;
91// }
92 }
Denis Vlasenko3266aa92009-04-01 11:24:04 +000093
Denis Vlasenko4acdb462009-01-31 21:45:57 +000094 if (!(opt & (OPT_n|OPT_c))) {
95 if (!(opt & OPT_p) && *argv)
Denys Vlasenko77832482010-08-12 14:14:45 +020096 pid = xatoi_positive(*argv);
Denis Vlasenko4acdb462009-01-31 21:45:57 +000097
98 pri = ioprio_get(IOPRIO_WHO_PROCESS, pid);
99 if (pri == -1)
100 bb_perror_msg_and_die("ioprio_%cet", 'g');
101
102 ioclass = (pri >> IOPRIO_CLASS_SHIFT) & 0x3;
103 pri &= 0xff;
104 printf((ioclass == IOPRIO_CLASS_IDLE) ? "%s\n" : "%s: prio %d\n",
105 nth_string(to_prio, ioclass), pri);
106 } else {
107//printf("pri=%d class=%d val=%x\n",
108//pri, ioclass, pri | (ioclass << IOPRIO_CLASS_SHIFT));
109 pri |= (ioclass << IOPRIO_CLASS_SHIFT);
110 if (ioprio_set(IOPRIO_WHO_PROCESS, pid, pri) == -1)
Denys Vlasenkof02b64d2021-06-17 13:45:13 +0200111 if (!(opt & OPT_t))
112 bb_perror_msg_and_die("ioprio_%cet", 's');
Denys Vlasenko41ddd9f2010-06-25 01:46:53 +0200113 if (argv[0]) {
Pascal Bellard21e8e8d2010-07-04 00:57:03 +0200114 BB_EXECVP_or_die(argv);
Denis Vlasenko4acdb462009-01-31 21:45:57 +0000115 }
116 }
117
118 return EXIT_SUCCESS;
119}