ionice.c 2.2 KB

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