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