3
0

renice.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * renice implementation for busybox
  4. *
  5. * Copyright (C) 2005 Manuel Novoa III <mjn3@codepoet.org>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  8. */
  9. /* Notes:
  10. * Setting an absolute priority was obsoleted in SUSv2 and removed
  11. * in SUSv3. However, the common linux version of renice does
  12. * absolute and not relative. So we'll continue supporting absolute,
  13. * although the stdout logging has been removed since both SUSv2 and
  14. * SUSv3 specify that stdout isn't used.
  15. *
  16. * This version is lenient in that it doesn't require any IDs. The
  17. * options -p, -g, and -u are treated as mode switches for the
  18. * following IDs (if any). Multiple switches are allowed.
  19. */
  20. //usage:#define renice_trivial_usage
  21. //usage: "{{-n INCREMENT} | PRIORITY} [[-p | -g | -u] ID...]"
  22. //usage:#define renice_full_usage "\n\n"
  23. //usage: "Change scheduling priority for a running process\n"
  24. //usage: "\n -n Adjust current nice value (smaller is faster)"
  25. //usage: "\n -p Process id(s) (default)"
  26. //usage: "\n -g Process group id(s)"
  27. //usage: "\n -u Process user name(s) and/or id(s)"
  28. #include "libbb.h"
  29. #include <sys/resource.h>
  30. void BUG_bad_PRIO_PROCESS(void);
  31. void BUG_bad_PRIO_PGRP(void);
  32. void BUG_bad_PRIO_USER(void);
  33. int renice_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  34. int renice_main(int argc UNUSED_PARAM, char **argv)
  35. {
  36. static const char Xetpriority_msg[] ALIGN1 = "%cetpriority";
  37. int retval = EXIT_SUCCESS;
  38. int which = PRIO_PROCESS; /* Default 'which' value. */
  39. int use_relative = 0;
  40. int adjustment, new_priority;
  41. unsigned who;
  42. char *arg;
  43. /* Yes, they are not #defines in glibc 2.4! #if won't work */
  44. if (PRIO_PROCESS < CHAR_MIN || PRIO_PROCESS > CHAR_MAX)
  45. BUG_bad_PRIO_PROCESS();
  46. if (PRIO_PGRP < CHAR_MIN || PRIO_PGRP > CHAR_MAX)
  47. BUG_bad_PRIO_PGRP();
  48. if (PRIO_USER < CHAR_MIN || PRIO_USER > CHAR_MAX)
  49. BUG_bad_PRIO_USER();
  50. arg = *++argv;
  51. /* Check if we are using a relative adjustment. */
  52. if (arg && arg[0] == '-' && arg[1] == 'n') {
  53. use_relative = 1;
  54. if (!arg[2])
  55. arg = *++argv;
  56. else
  57. arg += 2;
  58. }
  59. if (!arg) { /* No args? Then show usage. */
  60. bb_show_usage();
  61. }
  62. /* Get the priority adjustment (absolute or relative). */
  63. adjustment = xatoi_range(arg, INT_MIN/2, INT_MAX/2);
  64. while ((arg = *++argv) != NULL) {
  65. /* Check for a mode switch. */
  66. if (arg[0] == '-' && arg[1]) {
  67. static const char opts[] ALIGN1 = {
  68. 'p', 'g', 'u', 0, PRIO_PROCESS, PRIO_PGRP, PRIO_USER
  69. };
  70. const char *p = strchr(opts, arg[1]);
  71. if (p) {
  72. which = p[4];
  73. if (!arg[2])
  74. continue;
  75. arg += 2;
  76. }
  77. }
  78. /* Process an ID arg. */
  79. if (which == PRIO_USER) {
  80. struct passwd *p;
  81. p = getpwnam(arg);
  82. if (!p) {
  83. bb_error_msg("unknown user %s", arg);
  84. goto HAD_ERROR;
  85. }
  86. who = p->pw_uid;
  87. } else {
  88. who = bb_strtou(arg, NULL, 10);
  89. if (errno) {
  90. bb_error_msg("invalid number '%s'", arg);
  91. goto HAD_ERROR;
  92. }
  93. }
  94. /* Get priority to use, and set it. */
  95. if (use_relative) {
  96. int old_priority;
  97. errno = 0; /* Needed for getpriority error detection. */
  98. old_priority = getpriority(which, who);
  99. if (errno) {
  100. bb_perror_msg(Xetpriority_msg, 'g');
  101. goto HAD_ERROR;
  102. }
  103. new_priority = old_priority + adjustment;
  104. } else {
  105. new_priority = adjustment;
  106. }
  107. if (setpriority(which, who, new_priority) == 0) {
  108. continue;
  109. }
  110. bb_perror_msg(Xetpriority_msg, 's');
  111. HAD_ERROR:
  112. retval = EXIT_FAILURE;
  113. }
  114. /* No need to check for errors outputing to stderr since, if it
  115. * was used, the HAD_ERROR label was reached and retval was set. */
  116. return retval;
  117. }