blob: cc5660be7ba215b71affb82c6aaada963f577cad [file] [log] [blame]
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +00001/* vi: set sw=4 ts=4: */
2/*
3 * chrt - manipulate real-time attributes of a process
Bernhard Reutner-Fischer6c4dade2008-09-25 12:13:34 +00004 * Copyright (c) 2006-2007 Bernhard Reutner-Fischer
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +00005 *
6 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
7 */
8
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +00009#include <sched.h>
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000010#include "libbb.h"
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000011#ifndef _POSIX_PRIORITY_SCHEDULING
12#warning your system may be foobared
13#endif
14static const struct {
Denis Vlasenkob44c7902008-03-17 09:29:43 +000015 int policy;
16 char name[12];
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000017} policies[] = {
18 {SCHED_OTHER, "SCHED_OTHER"},
19 {SCHED_FIFO, "SCHED_FIFO"},
20 {SCHED_RR, "SCHED_RR"}
21};
22
Denis Vlasenkoac678ec2007-04-16 22:32:04 +000023static void show_min_max(int pol)
24{
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000025 const char *fmt = "%s min/max priority\t: %d/%d\n\0%s not supported?\n";
26 int max, min;
27 max = sched_get_priority_max(pol);
28 min = sched_get_priority_min(pol);
29 if (max >= 0 && min >= 0)
30 printf(fmt, policies[pol].name, min, max);
31 else {
32 fmt += 29;
33 printf(fmt, policies[pol].name);
34 }
35}
36
37#define OPT_m (1<<0)
38#define OPT_p (1<<1)
39#define OPT_r (1<<2)
40#define OPT_f (1<<3)
41#define OPT_o (1<<4)
42
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000043int chrt_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000044int chrt_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000045{
46 pid_t pid = 0;
47 unsigned opt;
48 struct sched_param sp;
Denis Vlasenkob44c7902008-03-17 09:29:43 +000049 char *pid_str;
50 char *priority = priority; /* for compiler */
51 const char *current_new;
52 int policy = SCHED_RR;
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000053
Denis Vlasenkob44c7902008-03-17 09:29:43 +000054 /* at least 1 arg; only one policy accepted */
55 opt_complementary = "-1:r--fo:f--ro:r--fo";
56 opt = getopt32(argv, "+mprfo");
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000057 if (opt & OPT_r)
58 policy = SCHED_RR;
59 if (opt & OPT_f)
60 policy = SCHED_FIFO;
61 if (opt & OPT_o)
62 policy = SCHED_OTHER;
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000063 if (opt & OPT_m) { /* print min/max */
64 show_min_max(SCHED_FIFO);
65 show_min_max(SCHED_RR);
66 show_min_max(SCHED_OTHER);
67 fflush_stdout_and_exit(EXIT_SUCCESS);
68 }
Denis Vlasenkob44c7902008-03-17 09:29:43 +000069
Denis Vlasenko42cc3042008-03-24 02:05:58 +000070 argv += optind;
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000071 if (opt & OPT_p) {
Denis Vlasenkob44c7902008-03-17 09:29:43 +000072 pid_str = *argv++;
73 if (*argv) { /* "-p <priority> <pid> [...]" */
74 priority = pid_str;
75 pid_str = *argv;
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000076 }
Denis Vlasenkob44c7902008-03-17 09:29:43 +000077 /* else "-p <pid>", and *argv == NULL */
78 pid = xatoul_range(pid_str, 1, ((unsigned)(pid_t)ULONG_MAX) >> 1);
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000079 } else {
Denis Vlasenkob44c7902008-03-17 09:29:43 +000080 priority = *argv++;
81 if (!*argv)
82 bb_show_usage();
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000083 }
84
Denis Vlasenkob44c7902008-03-17 09:29:43 +000085 current_new = "current\0new";
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000086 if (opt & OPT_p) {
Denis Vlasenkob44c7902008-03-17 09:29:43 +000087 int pol;
88 print_rt_info:
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000089 pol = sched_getscheduler(pid);
90 if (pol < 0)
Denis Vlasenkob44c7902008-03-17 09:29:43 +000091 bb_perror_msg_and_die("can't %cet pid %d's policy", 'g', pid);
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000092 printf("pid %d's %s scheduling policy: %s\n",
Denis Vlasenkob44c7902008-03-17 09:29:43 +000093 pid, current_new, policies[pol].name);
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000094 if (sched_getparam(pid, &sp))
Denis Vlasenkob44c7902008-03-17 09:29:43 +000095 bb_perror_msg_and_die("can't get pid %d's attributes", pid);
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000096 printf("pid %d's %s scheduling priority: %d\n",
Denis Vlasenkob44c7902008-03-17 09:29:43 +000097 pid, current_new, sp.sched_priority);
98 if (!*argv) {
99 /* Either it was just "-p <pid>",
100 * or it was "-p <priority> <pid>" and we came here
101 * for the second time (see goto below) */
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +0000102 return EXIT_SUCCESS;
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000103 }
104 *argv = NULL;
105 current_new += 8;
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +0000106 }
107
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000108 /* from the manpage of sched_getscheduler:
109 [...] sched_priority can have a value in the range 0 to 99.
110 [...] SCHED_OTHER or SCHED_BATCH must be assigned static priority 0.
111 [...] SCHED_FIFO or SCHED_RR can have static priority in 1..99 range.
112 */
113 sp.sched_priority = xstrtou_range(priority, 0, policy != SCHED_OTHER ? 1 : 0, 99);
114
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +0000115 if (sched_setscheduler(pid, policy, &sp) < 0)
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000116 bb_perror_msg_and_die("can't %cet pid %d's policy", 's', pid);
117
118 if (!*argv) /* "-p <priority> <pid> [...]" */
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +0000119 goto print_rt_info;
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000120
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +0000121 BB_EXECVP(*argv, argv);
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000122 bb_simple_perror_msg_and_die(*argv);
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +0000123}