diff options
author | Dmitry Shmidt <dimitrysh@google.com> | 2013-01-22 15:41:58 -0800 |
---|---|---|
committer | Dmitry Shmidt <dimitrysh@google.com> | 2013-01-22 16:59:36 -0800 |
commit | 4db7df71da5641dfa6c89543b9843dca1999d78e (patch) | |
tree | 549fde808e12abceca8e7bda5f91387b84a59974 /toolbox/renice.c | |
parent | 2840647b15fe91069aee02ce0e203943346bf294 (diff) | |
download | system_core-4db7df71da5641dfa6c89543b9843dca1999d78e.zip system_core-4db7df71da5641dfa6c89543b9843dca1999d78e.tar.gz system_core-4db7df71da5641dfa6c89543b9843dca1999d78e.tar.bz2 |
toolbox: renice: Add -t TYPE parameter and switch to getopt
Change-Id: Idaedabe1505a9c373629ef1fd31aa4b4beb068c2
Signed-off-by: Dmitry Shmidt <dimitrysh@google.com>
Diffstat (limited to 'toolbox/renice.c')
-rw-r--r-- | toolbox/renice.c | 64 |
1 files changed, 40 insertions, 24 deletions
diff --git a/toolbox/renice.c b/toolbox/renice.c index 978b329..9dfeb51 100644 --- a/toolbox/renice.c +++ b/toolbox/renice.c @@ -35,11 +35,12 @@ #include <sys/time.h> #include <sys/resource.h> #include <sched.h> +#include <getopt.h> static void usage(const char *s) { - fprintf(stderr, "USAGE: %s [[-r] priority pids ...] [-g pid]\n", s); + fprintf(stderr, "USAGE: %s [[-r] [-t TYPE] priority pids ...] [-g pid]\n", s); exit(EXIT_FAILURE); } @@ -74,32 +75,49 @@ void print_prio(pid_t pid) sched_get_priority_min(sched), sched_get_priority_max(sched)); } +int get_sched(char *str) +{ + if (strcasecmp(str, "RR") == 0) + return SCHED_RR; + else if (strcasecmp(str, "FIFO") == 0) + return SCHED_FIFO; + else if (strcasecmp(str, "NORMAL") == 0) + return SCHED_OTHER; + else if (strcasecmp(str, "OTHER") == 0) + return SCHED_OTHER; + return SCHED_RR; +} + int renice_main(int argc, char *argv[]) { int prio; int realtime = 0; + int opt; + int sched = SCHED_RR; char *cmd = argv[0]; - // consume command name - argc--; - argv++; - - if (argc < 1) - usage(cmd); - - if(strcmp("-r", argv[0]) == 0) { - // do realtime priority adjustment - realtime = 1; - argc--; - argv++; - } - - if(strcmp("-g", argv[0]) == 0) { - if (argc < 2) + do { + opt = getopt(argc, argv, "rt:g:"); + if (opt == -1) + break; + switch (opt) { + case 'r': + // do realtime priority adjustment + realtime = 1; + break; + case 't': + sched = get_sched(optarg); + break; + case 'g': + print_prio(atoi(optarg)); + return 0; + default: usage(cmd); - print_prio(atoi(argv[1])); - return 0; - } + } + } while (1); + + argc -= optind; + argv += optind; if (argc < 1) usage(cmd); @@ -122,7 +140,7 @@ int renice_main(int argc, char *argv[]) struct sched_param sp = { .sched_priority = prio }; int ret; - ret = sched_setscheduler(pid, SCHED_RR, &sp); + ret = sched_setscheduler(pid, sched, &sp); if (ret) { perror("sched_set_scheduler"); exit(EXIT_FAILURE); @@ -137,8 +155,6 @@ int renice_main(int argc, char *argv[]) } } } - + return 0; } - - |