Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * chrt - manipulate real-time attributes of a process |
Bernhard Reutner-Fischer | 6c4dade | 2008-09-25 12:13:34 +0000 | [diff] [blame] | 4 | * Copyright (c) 2006-2007 Bernhard Reutner-Fischer |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 5 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 6 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 7 | */ |
Denys Vlasenko | fb4da16 | 2016-11-22 23:14:24 +0100 | [diff] [blame] | 8 | //config:config CHRT |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 9 | //config: bool "chrt (4.7 kb)" |
Denys Vlasenko | fb4da16 | 2016-11-22 23:14:24 +0100 | [diff] [blame] | 10 | //config: default y |
| 11 | //config: help |
Denys Vlasenko | a497231 | 2018-01-25 09:34:23 +0100 | [diff] [blame] | 12 | //config: Manipulate real-time attributes of a process. |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 13 | //config: This requires sched_{g,s}etparam support in your libc. |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 14 | |
Denys Vlasenko | 5c527dc | 2017-08-04 19:55:01 +0200 | [diff] [blame] | 15 | //applet:IF_CHRT(APPLET_NOEXEC(chrt, chrt, BB_DIR_USR_BIN, BB_SUID_DROP, chrt)) |
Denys Vlasenko | f88e3bf | 2016-11-22 23:54:17 +0100 | [diff] [blame] | 16 | |
| 17 | //kbuild:lib-$(CONFIG_CHRT) += chrt.o |
| 18 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 19 | //usage:#define chrt_trivial_usage |
Denys Vlasenko | a2f18d9 | 2020-12-18 04:12:51 +0100 | [diff] [blame] | 20 | //usage: "-m | -p [PRIO] PID | [-rfobi] PRIO PROG ARGS" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 21 | //usage:#define chrt_full_usage "\n\n" |
| 22 | //usage: "Change scheduling priority and class for a process\n" |
Denys Vlasenko | ae5ca6f | 2019-04-05 12:03:48 +0200 | [diff] [blame] | 23 | //usage: "\n -m Show min/max priorities" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 24 | //usage: "\n -p Operate on PID" |
| 25 | //usage: "\n -r Set SCHED_RR class" |
| 26 | //usage: "\n -f Set SCHED_FIFO class" |
| 27 | //usage: "\n -o Set SCHED_OTHER class" |
Povilas Kanapickas | 1616fb8 | 2018-01-13 23:26:39 +0200 | [diff] [blame] | 28 | //usage: "\n -b Set SCHED_BATCH class" |
Povilas Kanapickas | 0f6e435 | 2018-01-13 23:27:37 +0200 | [diff] [blame] | 29 | //usage: "\n -i Set SCHED_IDLE class" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 30 | //usage: |
| 31 | //usage:#define chrt_example_usage |
| 32 | //usage: "$ chrt -r 4 sleep 900; x=$!\n" |
| 33 | //usage: "$ chrt -f -p 3 $x\n" |
| 34 | //usage: "You need CAP_SYS_NICE privileges to set scheduling attributes of a process" |
| 35 | |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 36 | #include <sched.h> |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 37 | #include "libbb.h" |
Denys Vlasenko | 66ad9be | 2018-01-23 16:01:13 +0100 | [diff] [blame] | 38 | #ifndef SCHED_IDLE |
| 39 | # define SCHED_IDLE 5 |
| 40 | #endif |
Denys Vlasenko | 922f6f5 | 2010-07-01 16:42:27 +0200 | [diff] [blame] | 41 | |
Christian Eggers | 89f063b | 2020-11-15 20:25:09 +0100 | [diff] [blame] | 42 | //musl has no __MUSL__ or similar define to check for, |
| 43 | //but its <sys/types.h> has these lines: |
| 44 | // #define __NEED_fsblkcnt_t |
| 45 | // #define __NEED_fsfilcnt_t |
| 46 | #if defined(__linux__) && defined(__NEED_fsblkcnt_t) && defined(__NEED_fsfilcnt_t) |
| 47 | # define LIBC_IS_MUSL 1 |
| 48 | # include <sys/syscall.h> |
| 49 | #else |
| 50 | # define LIBC_IS_MUSL 0 |
| 51 | #endif |
| 52 | |
Denys Vlasenko | ae5ca6f | 2019-04-05 12:03:48 +0200 | [diff] [blame] | 53 | static const char *policy_name(int pol) |
| 54 | { |
| 55 | if (pol > 6) |
| 56 | return utoa(pol); |
| 57 | return nth_string( |
| 58 | "OTHER" "\0" /* 0:SCHED_OTHER */ |
| 59 | "FIFO" "\0" /* 1:SCHED_FIFO */ |
| 60 | "RR" "\0" /* 2:SCHED_RR */ |
| 61 | "BATCH" "\0" /* 3:SCHED_BATCH */ |
| 62 | "ISO" "\0" /* 4:SCHED_ISO */ |
| 63 | "IDLE" "\0" /* 5:SCHED_IDLE */ |
| 64 | "DEADLINE", /* 6:SCHED_DEADLINE */ |
| 65 | pol |
| 66 | ); |
| 67 | } |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 68 | |
Denis Vlasenko | ac678ec | 2007-04-16 22:32:04 +0000 | [diff] [blame] | 69 | static void show_min_max(int pol) |
| 70 | { |
Denys Vlasenko | ae5ca6f | 2019-04-05 12:03:48 +0200 | [diff] [blame] | 71 | const char *fmt = "SCHED_%s min/max priority\t: %u/%u\n"; |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 72 | int max, min; |
Denys Vlasenko | 922f6f5 | 2010-07-01 16:42:27 +0200 | [diff] [blame] | 73 | |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 74 | max = sched_get_priority_max(pol); |
| 75 | min = sched_get_priority_min(pol); |
Denys Vlasenko | 922f6f5 | 2010-07-01 16:42:27 +0200 | [diff] [blame] | 76 | if ((max|min) < 0) |
Denys Vlasenko | ae5ca6f | 2019-04-05 12:03:48 +0200 | [diff] [blame] | 77 | fmt = "SCHED_%s not supported\n"; |
| 78 | printf(fmt, policy_name(pol), min, max); |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | #define OPT_m (1<<0) |
| 82 | #define OPT_p (1<<1) |
| 83 | #define OPT_r (1<<2) |
| 84 | #define OPT_f (1<<3) |
| 85 | #define OPT_o (1<<4) |
Povilas Kanapickas | 1616fb8 | 2018-01-13 23:26:39 +0200 | [diff] [blame] | 86 | #define OPT_b (1<<5) |
Povilas Kanapickas | 0f6e435 | 2018-01-13 23:27:37 +0200 | [diff] [blame] | 87 | #define OPT_i (1<<6) |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 88 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 89 | int chrt_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 90 | int chrt_main(int argc UNUSED_PARAM, char **argv) |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 91 | { |
| 92 | pid_t pid = 0; |
| 93 | unsigned opt; |
| 94 | struct sched_param sp; |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 95 | char *pid_str; |
Bernhard Reutner-Fischer | 376b2ce | 2021-10-10 10:19:08 +0200 | [diff] [blame] | 96 | char *priority = NULL; |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 97 | const char *current_new; |
| 98 | int policy = SCHED_RR; |
Christian Eggers | 89f063b | 2020-11-15 20:25:09 +0100 | [diff] [blame] | 99 | int ret; |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 100 | |
Denys Vlasenko | 66ad9be | 2018-01-23 16:01:13 +0100 | [diff] [blame] | 101 | opt = getopt32(argv, "^" |
| 102 | "+" "mprfobi" |
| 103 | "\0" |
| 104 | /* only one policy accepted: */ |
| 105 | "r--fobi:f--robi:o--rfbi:b--rfoi:i--rfob" |
| 106 | ); |
Denys Vlasenko | 1393fc1 | 2010-12-19 04:07:50 +0100 | [diff] [blame] | 107 | if (opt & OPT_m) { /* print min/max and exit */ |
Denys Vlasenko | 66ad9be | 2018-01-23 16:01:13 +0100 | [diff] [blame] | 108 | show_min_max(SCHED_OTHER); |
Denys Vlasenko | 1393fc1 | 2010-12-19 04:07:50 +0100 | [diff] [blame] | 109 | show_min_max(SCHED_FIFO); |
| 110 | show_min_max(SCHED_RR); |
Povilas Kanapickas | 1616fb8 | 2018-01-13 23:26:39 +0200 | [diff] [blame] | 111 | show_min_max(SCHED_BATCH); |
Povilas Kanapickas | 0f6e435 | 2018-01-13 23:27:37 +0200 | [diff] [blame] | 112 | show_min_max(SCHED_IDLE); |
Denys Vlasenko | 31f45c1 | 2022-01-04 23:31:58 +0100 | [diff] [blame] | 113 | fflush_stdout_and_exit_SUCCESS(); |
Denys Vlasenko | 1393fc1 | 2010-12-19 04:07:50 +0100 | [diff] [blame] | 114 | } |
Denys Vlasenko | 66ad9be | 2018-01-23 16:01:13 +0100 | [diff] [blame] | 115 | //if (opt & OPT_r) |
| 116 | // policy = SCHED_RR; - default, already set |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 117 | if (opt & OPT_f) |
| 118 | policy = SCHED_FIFO; |
| 119 | if (opt & OPT_o) |
| 120 | policy = SCHED_OTHER; |
Povilas Kanapickas | 1616fb8 | 2018-01-13 23:26:39 +0200 | [diff] [blame] | 121 | if (opt & OPT_b) |
| 122 | policy = SCHED_BATCH; |
Povilas Kanapickas | 0f6e435 | 2018-01-13 23:27:37 +0200 | [diff] [blame] | 123 | if (opt & OPT_i) |
| 124 | policy = SCHED_IDLE; |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 125 | |
Denis Vlasenko | 42cc304 | 2008-03-24 02:05:58 +0000 | [diff] [blame] | 126 | argv += optind; |
Denys Vlasenko | 1393fc1 | 2010-12-19 04:07:50 +0100 | [diff] [blame] | 127 | if (!argv[0]) |
| 128 | bb_show_usage(); |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 129 | if (opt & OPT_p) { |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 130 | pid_str = *argv++; |
Denys Vlasenko | ae5ca6f | 2019-04-05 12:03:48 +0200 | [diff] [blame] | 131 | if (*argv) { /* "-p PRIO PID [...]" */ |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 132 | priority = pid_str; |
| 133 | pid_str = *argv; |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 134 | } |
Denys Vlasenko | ae5ca6f | 2019-04-05 12:03:48 +0200 | [diff] [blame] | 135 | /* else "-p PID", and *argv == NULL */ |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 136 | pid = xatoul_range(pid_str, 1, ((unsigned)(pid_t)ULONG_MAX) >> 1); |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 137 | } else { |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 138 | priority = *argv++; |
| 139 | if (!*argv) |
| 140 | bb_show_usage(); |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 143 | current_new = "current\0new"; |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 144 | if (opt & OPT_p) { |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 145 | int pol; |
| 146 | print_rt_info: |
Christian Eggers | 89f063b | 2020-11-15 20:25:09 +0100 | [diff] [blame] | 147 | #if LIBC_IS_MUSL |
| 148 | /* musl libc returns ENOSYS for its sched_getscheduler library |
| 149 | * function, because the sched_getscheduler Linux kernel system call |
| 150 | * does not conform to Posix; so we use the system call directly |
| 151 | */ |
| 152 | pol = syscall(SYS_sched_getscheduler, pid); |
| 153 | #else |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 154 | pol = sched_getscheduler(pid); |
Christian Eggers | 89f063b | 2020-11-15 20:25:09 +0100 | [diff] [blame] | 155 | #endif |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 156 | if (pol < 0) |
Denys Vlasenko | ae5ca6f | 2019-04-05 12:03:48 +0200 | [diff] [blame] | 157 | bb_perror_msg_and_die("can't %cet pid %u's policy", 'g', (int)pid); |
Denys Vlasenko | 43d09e7 | 2019-04-05 16:59:07 +0200 | [diff] [blame] | 158 | #ifdef SCHED_RESET_ON_FORK |
| 159 | /* "Since Linux 2.6.32, the SCHED_RESET_ON_FORK flag |
| 160 | * can be ORed in policy when calling sched_setscheduler(). |
| 161 | * As a result of including this flag, children created by |
| 162 | * fork(2) do not inherit privileged scheduling policies" |
| 163 | * |
| 164 | * This bit is also returned by sched_getscheduler()! |
| 165 | * (TODO: do we want to show it?) |
| 166 | */ |
| 167 | pol &= ~SCHED_RESET_ON_FORK; |
| 168 | #endif |
Denys Vlasenko | ae5ca6f | 2019-04-05 12:03:48 +0200 | [diff] [blame] | 169 | printf("pid %u's %s scheduling policy: SCHED_%s\n", |
| 170 | pid, current_new, policy_name(pol) |
| 171 | ); |
Christian Eggers | 89f063b | 2020-11-15 20:25:09 +0100 | [diff] [blame] | 172 | #if LIBC_IS_MUSL |
| 173 | ret = syscall(SYS_sched_getparam, pid, &sp); |
| 174 | #else |
| 175 | ret = sched_getparam(pid, &sp); |
| 176 | #endif |
| 177 | if (ret) |
Denys Vlasenko | ae5ca6f | 2019-04-05 12:03:48 +0200 | [diff] [blame] | 178 | bb_perror_msg_and_die("can't get pid %u's attributes", (int)pid); |
| 179 | printf("pid %u's %s scheduling priority: %d\n", |
| 180 | (int)pid, current_new, sp.sched_priority |
| 181 | ); |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 182 | if (!*argv) { |
Denys Vlasenko | ae5ca6f | 2019-04-05 12:03:48 +0200 | [diff] [blame] | 183 | /* Either it was just "-p PID", |
| 184 | * or it was "-p PRIO PID" and we came here |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 185 | * for the second time (see goto below) */ |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 186 | return EXIT_SUCCESS; |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 187 | } |
| 188 | *argv = NULL; |
| 189 | current_new += 8; |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Povilas Kanapickas | 1616fb8 | 2018-01-13 23:26:39 +0200 | [diff] [blame] | 192 | sp.sched_priority = xstrtou_range(priority, 0, |
Denys Vlasenko | 66ad9be | 2018-01-23 16:01:13 +0100 | [diff] [blame] | 193 | sched_get_priority_min(policy), sched_get_priority_max(policy) |
| 194 | ); |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 195 | |
Christian Eggers | 89f063b | 2020-11-15 20:25:09 +0100 | [diff] [blame] | 196 | #if LIBC_IS_MUSL |
| 197 | ret = syscall(SYS_sched_setscheduler, pid, policy, &sp); |
| 198 | #else |
| 199 | ret = sched_setscheduler(pid, policy, &sp); |
| 200 | #endif |
| 201 | if (ret) |
Denys Vlasenko | ae5ca6f | 2019-04-05 12:03:48 +0200 | [diff] [blame] | 202 | bb_perror_msg_and_die("can't %cet pid %u's policy", 's', (int)pid); |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 203 | |
Denys Vlasenko | ae5ca6f | 2019-04-05 12:03:48 +0200 | [diff] [blame] | 204 | if (!argv[0]) /* "-p PRIO PID [...]" */ |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 205 | goto print_rt_info; |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 206 | |
Pascal Bellard | 21e8e8d | 2010-07-04 00:57:03 +0200 | [diff] [blame] | 207 | BB_EXECVP_or_die(argv); |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 208 | } |