3
0

renice.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 the GPL v2 or later, see the file LICENSE in this tarball.
  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. #include "libbb.h"
  21. #include <sys/resource.h>
  22. void BUG_bad_PRIO_PROCESS(void);
  23. void BUG_bad_PRIO_PGRP(void);
  24. void BUG_bad_PRIO_USER(void);
  25. int renice_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  26. int renice_main(int argc UNUSED_PARAM, char **argv)
  27. {
  28. static const char Xetpriority_msg[] ALIGN1 = "%cetpriority";
  29. int retval = EXIT_SUCCESS;
  30. int which = PRIO_PROCESS; /* Default 'which' value. */
  31. int use_relative = 0;
  32. int adjustment, new_priority;
  33. unsigned who;
  34. char *arg;
  35. /* Yes, they are not #defines in glibc 2.4! #if won't work */
  36. if (PRIO_PROCESS < CHAR_MIN || PRIO_PROCESS > CHAR_MAX)
  37. BUG_bad_PRIO_PROCESS();
  38. if (PRIO_PGRP < CHAR_MIN || PRIO_PGRP > CHAR_MAX)
  39. BUG_bad_PRIO_PGRP();
  40. if (PRIO_USER < CHAR_MIN || PRIO_USER > CHAR_MAX)
  41. BUG_bad_PRIO_USER();
  42. arg = *++argv;
  43. /* Check if we are using a relative adjustment. */
  44. if (arg && arg[0] == '-' && arg[1] == 'n') {
  45. use_relative = 1;
  46. if (!arg[2])
  47. arg = *++argv;
  48. else
  49. arg += 2;
  50. }
  51. if (!arg) { /* No args? Then show usage. */
  52. bb_show_usage();
  53. }
  54. /* Get the priority adjustment (absolute or relative). */
  55. adjustment = xatoi_range(arg, INT_MIN/2, INT_MAX/2);
  56. while ((arg = *++argv) != NULL) {
  57. /* Check for a mode switch. */
  58. if (arg[0] == '-' && arg[1]) {
  59. static const char opts[] ALIGN1 = {
  60. 'p', 'g', 'u', 0, PRIO_PROCESS, PRIO_PGRP, PRIO_USER
  61. };
  62. const char *p = strchr(opts, arg[1]);
  63. if (p) {
  64. which = p[4];
  65. if (!arg[2])
  66. continue;
  67. arg += 2;
  68. }
  69. }
  70. /* Process an ID arg. */
  71. if (which == PRIO_USER) {
  72. struct passwd *p;
  73. p = getpwnam(arg);
  74. if (!p) {
  75. bb_error_msg("unknown user %s", arg);
  76. goto HAD_ERROR;
  77. }
  78. who = p->pw_uid;
  79. } else {
  80. who = bb_strtou(arg, NULL, 10);
  81. if (errno) {
  82. bb_error_msg("bad value: %s", arg);
  83. goto HAD_ERROR;
  84. }
  85. }
  86. /* Get priority to use, and set it. */
  87. if (use_relative) {
  88. int old_priority;
  89. errno = 0; /* Needed for getpriority error detection. */
  90. old_priority = getpriority(which, who);
  91. if (errno) {
  92. bb_perror_msg(Xetpriority_msg, 'g');
  93. goto HAD_ERROR;
  94. }
  95. new_priority = old_priority + adjustment;
  96. } else {
  97. new_priority = adjustment;
  98. }
  99. if (setpriority(which, who, new_priority) == 0) {
  100. continue;
  101. }
  102. bb_perror_msg(Xetpriority_msg, 's');
  103. HAD_ERROR:
  104. retval = EXIT_FAILURE;
  105. }
  106. /* No need to check for errors outputing to stderr since, if it
  107. * was used, the HAD_ERROR label was reached and retval was set. */
  108. return retval;
  109. }