Fix ubirsvol usage information
[platform/upstream/busybox.git] / miscutils / ionice.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * ionice implementation for busybox based on linux-utils-ng 2.14
4  *
5  * Copyright (C) 2008 by  <u173034@informatik.uni-oldenburg.de>
6  *
7  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
8  */
9
10 //usage:#define ionice_trivial_usage
11 //usage:        "[-c 1-3] [-n 0-7] [-p PID] [PROG]"
12 //usage:#define ionice_full_usage "\n\n"
13 //usage:       "Change I/O priority and class\n"
14 //usage:     "\nOptions:"
15 //usage:     "\n        -c      Class. 1:realtime 2:best-effort 3:idle"
16 //usage:     "\n        -n      Priority"
17
18 #include <sys/syscall.h>
19 #include <asm/unistd.h>
20 #include "libbb.h"
21
22 static int ioprio_set(int which, int who, int ioprio)
23 {
24         return syscall(SYS_ioprio_set, which, who, ioprio);
25 }
26
27 static int ioprio_get(int which, int who)
28 {
29         return syscall(SYS_ioprio_get, which, who);
30 }
31
32 enum {
33         IOPRIO_WHO_PROCESS = 1,
34         IOPRIO_WHO_PGRP,
35         IOPRIO_WHO_USER
36 };
37
38 enum {
39         IOPRIO_CLASS_NONE,
40         IOPRIO_CLASS_RT,
41         IOPRIO_CLASS_BE,
42         IOPRIO_CLASS_IDLE
43 };
44
45 static const char to_prio[] = "none\0realtime\0best-effort\0idle";
46
47 #define IOPRIO_CLASS_SHIFT      13
48
49 int ionice_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
50 int ionice_main(int argc UNUSED_PARAM, char **argv)
51 {
52         /* Defaults */
53         int ioclass = 0;
54         int pri = 0;
55         int pid = 0; /* affect own porcess */
56         int opt;
57         enum {
58                 OPT_n = 1,
59                 OPT_c = 2,
60                 OPT_p = 4,
61         };
62
63         /* Numeric params */
64         opt_complementary = "n+:c+:p+";
65         /* '+': stop at first non-option */
66         opt = getopt32(argv, "+n:c:p:", &pri, &ioclass, &pid);
67         argv += optind;
68
69         if (opt & OPT_c) {
70                 if (ioclass > 3)
71                         bb_error_msg_and_die("bad class %d", ioclass);
72 // Do we need this (compat?)?
73 //              if (ioclass == IOPRIO_CLASS_NONE)
74 //                      ioclass = IOPRIO_CLASS_BE;
75 //              if (ioclass == IOPRIO_CLASS_IDLE) {
76 //                      //if (opt & OPT_n)
77 //                      //      bb_error_msg("ignoring priority for idle class");
78 //                      pri = 7;
79 //              }
80         }
81
82         if (!(opt & (OPT_n|OPT_c))) {
83                 if (!(opt & OPT_p) && *argv)
84                         pid = xatoi_positive(*argv);
85
86                 pri = ioprio_get(IOPRIO_WHO_PROCESS, pid);
87                 if (pri == -1)
88                         bb_perror_msg_and_die("ioprio_%cet", 'g');
89
90                 ioclass = (pri >> IOPRIO_CLASS_SHIFT) & 0x3;
91                 pri &= 0xff;
92                 printf((ioclass == IOPRIO_CLASS_IDLE) ? "%s\n" : "%s: prio %d\n",
93                                 nth_string(to_prio, ioclass), pri);
94         } else {
95 //printf("pri=%d class=%d val=%x\n",
96 //pri, ioclass, pri | (ioclass << IOPRIO_CLASS_SHIFT));
97                 pri |= (ioclass << IOPRIO_CLASS_SHIFT);
98                 if (ioprio_set(IOPRIO_WHO_PROCESS, pid, pri) == -1)
99                         bb_perror_msg_and_die("ioprio_%cet", 's');
100                 if (argv[0]) {
101                         BB_EXECVP_or_die(argv);
102                 }
103         }
104
105         return EXIT_SUCCESS;
106 }