diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2009-01-31 21:45:57 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2009-01-31 21:45:57 +0000 |
commit | 4acdb46ff17a3dd966ddccdc149cda5a18d51a48 (patch) | |
tree | e467e075ec91dd753eb1359d6b350c09ed52d78e /miscutils/ionice.c | |
parent | 802cab15e5407269cc3bb3a53fe076e86b62307e (diff) | |
download | busybox-w32-4acdb46ff17a3dd966ddccdc149cda5a18d51a48.tar.gz busybox-w32-4acdb46ff17a3dd966ddccdc149cda5a18d51a48.tar.bz2 busybox-w32-4acdb46ff17a3dd966ddccdc149cda5a18d51a48.zip |
ionice: new applet, adapted from Linux kernel' example by Walter Harms
text data bss dec hex filename
1050316 924 10952 1062192 103530 busybox_old
1050758 924 10952 1062634 1036ea busybox_unstripped
Diffstat (limited to 'miscutils/ionice.c')
-rw-r--r-- | miscutils/ionice.c | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/miscutils/ionice.c b/miscutils/ionice.c new file mode 100644 index 000000000..88d771c30 --- /dev/null +++ b/miscutils/ionice.c | |||
@@ -0,0 +1,99 @@ | |||
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 tarball for details. | ||
8 | */ | ||
9 | |||
10 | #include <sys/syscall.h> | ||
11 | #include <asm/unistd.h> | ||
12 | #include "libbb.h" | ||
13 | |||
14 | static int ioprio_set(int which, int who, int ioprio) | ||
15 | { | ||
16 | return syscall(SYS_ioprio_set, which, who, ioprio); | ||
17 | } | ||
18 | |||
19 | static int ioprio_get(int which, int who) | ||
20 | { | ||
21 | return syscall(SYS_ioprio_get, which, who); | ||
22 | } | ||
23 | |||
24 | enum { | ||
25 | IOPRIO_WHO_PROCESS = 1, | ||
26 | IOPRIO_WHO_PGRP, | ||
27 | IOPRIO_WHO_USER | ||
28 | }; | ||
29 | |||
30 | enum { | ||
31 | IOPRIO_CLASS_NONE, | ||
32 | IOPRIO_CLASS_RT, | ||
33 | IOPRIO_CLASS_BE, | ||
34 | IOPRIO_CLASS_IDLE | ||
35 | }; | ||
36 | |||
37 | static const char to_prio[] = "none\0realtime\0best-effort\0idle"; | ||
38 | |||
39 | #define IOPRIO_CLASS_SHIFT 13 | ||
40 | |||
41 | int ionice_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | ||
42 | int ionice_main(int argc UNUSED_PARAM, char **argv) | ||
43 | { | ||
44 | /* Defaults */ | ||
45 | int ioclass = 0; | ||
46 | int pri = 0; | ||
47 | int pid = 0; /* affect own porcess */ | ||
48 | int opt; | ||
49 | enum { | ||
50 | OPT_n = 1, | ||
51 | OPT_c = 2, | ||
52 | OPT_p = 4, | ||
53 | }; | ||
54 | |||
55 | /* Numeric params */ | ||
56 | opt_complementary = "n+:c+:p+"; | ||
57 | /* '+': stop at first non-option */ | ||
58 | opt = getopt32(argv, "+n:c:p:", &pri, &ioclass, &pid); | ||
59 | argv += optind; | ||
60 | |||
61 | if (opt & OPT_c) { | ||
62 | if (ioclass > 3) | ||
63 | bb_error_msg_and_die("bad class %d", ioclass); | ||
64 | // Do we need this (compat?)? | ||
65 | // if (ioclass == IOPRIO_CLASS_NONE) | ||
66 | // ioclass = IOPRIO_CLASS_BE; | ||
67 | // if (ioclass == IOPRIO_CLASS_IDLE) { | ||
68 | // //if (opt & OPT_n) | ||
69 | // // bb_error_msg("ignoring priority for idle class"); | ||
70 | // pri = 7; | ||
71 | // } | ||
72 | } | ||
73 | |||
74 | if (!(opt & (OPT_n|OPT_c))) { | ||
75 | if (!(opt & OPT_p) && *argv) | ||
76 | pid = xatoi_u(*argv); | ||
77 | |||
78 | pri = ioprio_get(IOPRIO_WHO_PROCESS, pid); | ||
79 | if (pri == -1) | ||
80 | bb_perror_msg_and_die("ioprio_%cet", 'g'); | ||
81 | |||
82 | ioclass = (pri >> IOPRIO_CLASS_SHIFT) & 0x3; | ||
83 | pri &= 0xff; | ||
84 | printf((ioclass == IOPRIO_CLASS_IDLE) ? "%s\n" : "%s: prio %d\n", | ||
85 | nth_string(to_prio, ioclass), pri); | ||
86 | } else { | ||
87 | //printf("pri=%d class=%d val=%x\n", | ||
88 | //pri, ioclass, pri | (ioclass << IOPRIO_CLASS_SHIFT)); | ||
89 | pri |= (ioclass << IOPRIO_CLASS_SHIFT); | ||
90 | if (ioprio_set(IOPRIO_WHO_PROCESS, pid, pri) == -1) | ||
91 | bb_perror_msg_and_die("ioprio_%cet", 's'); | ||
92 | if (*argv) { | ||
93 | BB_EXECVP(*argv, argv); | ||
94 | bb_simple_perror_msg_and_die(*argv); | ||
95 | } | ||
96 | } | ||
97 | |||
98 | return EXIT_SUCCESS; | ||
99 | } | ||