ionice.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. //config:config IONICE
  10. //config: bool "ionice"
  11. //config: default y
  12. //config: select PLATFORM_LINUX
  13. //config: help
  14. //config: Set/set program io scheduling class and priority
  15. //config: Requires kernel >= 2.6.13
  16. //applet:IF_IONICE(APPLET(ionice, BB_DIR_BIN, BB_SUID_DROP))
  17. //kbuild:lib-$(CONFIG_IONICE) += ionice.o
  18. //usage:#define ionice_trivial_usage
  19. //usage: "[-c 1-3] [-n 0-7] [-p PID] [PROG]"
  20. //usage:#define ionice_full_usage "\n\n"
  21. //usage: "Change I/O priority and class\n"
  22. //usage: "\n -c Class. 1:realtime 2:best-effort 3:idle"
  23. //usage: "\n -n Priority"
  24. #include <sys/syscall.h>
  25. #include <asm/unistd.h>
  26. #include "libbb.h"
  27. static int ioprio_set(int which, int who, int ioprio)
  28. {
  29. return syscall(SYS_ioprio_set, which, who, ioprio);
  30. }
  31. static int ioprio_get(int which, int who)
  32. {
  33. return syscall(SYS_ioprio_get, which, who);
  34. }
  35. enum {
  36. IOPRIO_WHO_PROCESS = 1,
  37. IOPRIO_WHO_PGRP,
  38. IOPRIO_WHO_USER
  39. };
  40. enum {
  41. IOPRIO_CLASS_NONE,
  42. IOPRIO_CLASS_RT,
  43. IOPRIO_CLASS_BE,
  44. IOPRIO_CLASS_IDLE
  45. };
  46. static const char to_prio[] ALIGN1 = "none\0realtime\0best-effort\0idle";
  47. #define IOPRIO_CLASS_SHIFT 13
  48. int ionice_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  49. int ionice_main(int argc UNUSED_PARAM, char **argv)
  50. {
  51. /* Defaults */
  52. int ioclass = 0;
  53. int pri = 0;
  54. int pid = 0; /* affect own porcess */
  55. int opt;
  56. enum {
  57. OPT_n = 1,
  58. OPT_c = 2,
  59. OPT_p = 4,
  60. };
  61. /* Numeric params */
  62. /* '+': stop at first non-option */
  63. opt = getopt32(argv, "+n:+c:+p:+", &pri, &ioclass, &pid);
  64. argv += optind;
  65. if (opt & OPT_c) {
  66. if (ioclass > 3)
  67. bb_error_msg_and_die("bad class %d", ioclass);
  68. // Do we need this (compat?)?
  69. // if (ioclass == IOPRIO_CLASS_NONE)
  70. // ioclass = IOPRIO_CLASS_BE;
  71. // if (ioclass == IOPRIO_CLASS_IDLE) {
  72. // //if (opt & OPT_n)
  73. // // bb_error_msg("ignoring priority for idle class");
  74. // pri = 7;
  75. // }
  76. }
  77. if (!(opt & (OPT_n|OPT_c))) {
  78. if (!(opt & OPT_p) && *argv)
  79. pid = xatoi_positive(*argv);
  80. pri = ioprio_get(IOPRIO_WHO_PROCESS, pid);
  81. if (pri == -1)
  82. bb_perror_msg_and_die("ioprio_%cet", 'g');
  83. ioclass = (pri >> IOPRIO_CLASS_SHIFT) & 0x3;
  84. pri &= 0xff;
  85. printf((ioclass == IOPRIO_CLASS_IDLE) ? "%s\n" : "%s: prio %d\n",
  86. nth_string(to_prio, ioclass), pri);
  87. } else {
  88. //printf("pri=%d class=%d val=%x\n",
  89. //pri, ioclass, pri | (ioclass << IOPRIO_CLASS_SHIFT));
  90. pri |= (ioclass << IOPRIO_CLASS_SHIFT);
  91. if (ioprio_set(IOPRIO_WHO_PROCESS, pid, pri) == -1)
  92. bb_perror_msg_and_die("ioprio_%cet", 's');
  93. if (argv[0]) {
  94. BB_EXECVP_or_die(argv);
  95. }
  96. }
  97. return EXIT_SUCCESS;
  98. }