ionice.c 2.4 KB

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