blob: f52c3cd1b963b1d301cfdb764d9aa8d4cf9b8b08 [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
4 * Copyright (c) 2006-2007 Bernhard Fischer
5 *
6 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
7 */
8
9#include "busybox.h"
10#include <unistd.h>
11#include <sched.h>
12#include <getopt.h> /* optind */
13#ifndef _POSIX_PRIORITY_SCHEDULING
14#warning your system may be foobared
15#endif
16static const struct {
17 const int policy;
18 const char const name[12];
19} policies[] = {
20 {SCHED_OTHER, "SCHED_OTHER"},
21 {SCHED_FIFO, "SCHED_FIFO"},
22 {SCHED_RR, "SCHED_RR"}
23};
24
Denis Vlasenkoac678ec2007-04-16 22:32:04 +000025static void show_min_max(int pol)
26{
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000027 const char *fmt = "%s min/max priority\t: %d/%d\n\0%s not supported?\n";
28 int max, min;
29 max = sched_get_priority_max(pol);
30 min = sched_get_priority_min(pol);
31 if (max >= 0 && min >= 0)
32 printf(fmt, policies[pol].name, min, max);
33 else {
34 fmt += 29;
35 printf(fmt, policies[pol].name);
36 }
37}
38
39#define OPT_m (1<<0)
40#define OPT_p (1<<1)
41#define OPT_r (1<<2)
42#define OPT_f (1<<3)
43#define OPT_o (1<<4)
44
45int chrt_main(int argc, char** argv);
46int chrt_main(int argc, char** argv)
47{
48 pid_t pid = 0;
49 unsigned opt;
50 struct sched_param sp;
51 char *p_opt = NULL, *priority = NULL;
52 const char *state = "current\0new";
53 int prio = 0, policy = SCHED_RR;
54
55 opt_complementary = "r--fo:f--ro:r--fo"; /* only one policy accepted */
56 opt = getopt32(argc, argv, "+mp:rfo", &p_opt);
57 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;
63
64 if (opt & OPT_m) { /* print min/max */
65 show_min_max(SCHED_FIFO);
66 show_min_max(SCHED_RR);
67 show_min_max(SCHED_OTHER);
68 fflush_stdout_and_exit(EXIT_SUCCESS);
69 }
70 if (opt & OPT_p) {
71 if (argc == optind+1) { /* -p <priority> <pid> */
72 priority = p_opt;
73 p_opt = argv[optind];
74 }
75 argv += optind; /* me -p <arg> */
76 pid = xatoul_range(p_opt, 1, ULONG_MAX); /* -p <pid> */
77 } else {
78 argv += optind; /* me -p <arg> */
79 priority = *argv;
80 }
81 if (priority) {
82 /* from the manpage of sched_getscheduler:
83 [...] sched_priority can have a value
84 in the range 0 to 99.
85 [...] SCHED_OTHER or SCHED_BATCH must be assigned
86 the static priority 0. [...] SCHED_FIFO or
87 SCHED_RR can have a static priority in the range 1 to 99.
88 */
89 prio = xstrtol_range(priority, 0, policy == SCHED_OTHER
90 ? 0 : 1, 99);
91 }
92
93 if (opt & OPT_p) {
94 int pol = 0;
95print_rt_info:
96 pol = sched_getscheduler(pid);
97 if (pol < 0)
98 bb_perror_msg_and_die("failed to %cet pid %d's policy", 'g', pid);
99 printf("pid %d's %s scheduling policy: %s\n",
100 pid, state, policies[pol].name);
101 if (sched_getparam(pid, &sp))
102 bb_perror_msg_and_die("failed to get pid %d's attributes", pid);
103 printf("pid %d's %s scheduling priority: %d\n",
104 pid, state, sp.sched_priority);
105 if (!*argv) /* no new prio given or we did print already, done. */
106 return EXIT_SUCCESS;
107 }
108
109 sp.sched_priority = prio;
110 if (sched_setscheduler(pid, policy, &sp) < 0)
111 bb_perror_msg_and_die("failed to %cet pid %d's policy", 's', pid);
112 if (opt & OPT_p) {
113 state += 8;
114 ++argv;
115 goto print_rt_info;
116 }
117 ++argv;
118 BB_EXECVP(*argv, argv);
119 bb_perror_msg_and_die("%s", *argv);
120}
121#undef OPT_p
122#undef OPT_r
123#undef OPT_f
124#undef OPT_o
125#undef OPT_m