/usr.bin/renice/renice.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 182 lines · 135 code · 14 blank · 33 comment · 41 complexity · b0f7e55317a1b91603db47bd6ce5e03d MD5 · raw file

  1. /*
  2. * Copyright (c) 1983, 1989, 1993
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 4. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. #ifndef lint
  30. static const char copyright[] =
  31. "@(#) Copyright (c) 1983, 1989, 1993\n\
  32. The Regents of the University of California. All rights reserved.\n";
  33. #endif /* not lint */
  34. #if 0
  35. #ifndef lint
  36. static char sccsid[] = "@(#)renice.c 8.1 (Berkeley) 6/9/93";
  37. #endif /* not lint */
  38. #endif
  39. #include <sys/cdefs.h>
  40. __FBSDID("$FreeBSD$");
  41. #include <sys/types.h>
  42. #include <sys/time.h>
  43. #include <sys/resource.h>
  44. #include <err.h>
  45. #include <errno.h>
  46. #include <limits.h>
  47. #include <pwd.h>
  48. #include <stdio.h>
  49. #include <stdlib.h>
  50. #include <string.h>
  51. static int donice(int, int, int, int);
  52. static int getnum(const char *, const char *, int *);
  53. static void usage(void);
  54. /*
  55. * Change the priority (nice) of processes
  56. * or groups of processes which are already
  57. * running.
  58. */
  59. int
  60. main(int argc, char *argv[])
  61. {
  62. struct passwd *pwd;
  63. int errs, incr, prio, which, who;
  64. errs = 0;
  65. incr = 0;
  66. which = PRIO_PROCESS;
  67. who = 0;
  68. argc--, argv++;
  69. if (argc < 2)
  70. usage();
  71. if (strcmp(*argv, "-n") == 0) {
  72. incr = 1;
  73. argc--, argv++;
  74. if (argc < 2)
  75. usage();
  76. }
  77. if (getnum("priority", *argv, &prio))
  78. return (1);
  79. argc--, argv++;
  80. for (; argc > 0; argc--, argv++) {
  81. if (strcmp(*argv, "-g") == 0) {
  82. which = PRIO_PGRP;
  83. continue;
  84. }
  85. if (strcmp(*argv, "-u") == 0) {
  86. which = PRIO_USER;
  87. continue;
  88. }
  89. if (strcmp(*argv, "-p") == 0) {
  90. which = PRIO_PROCESS;
  91. continue;
  92. }
  93. if (which == PRIO_USER) {
  94. if ((pwd = getpwnam(*argv)) != NULL)
  95. who = pwd->pw_uid;
  96. else if (getnum("uid", *argv, &who)) {
  97. errs++;
  98. continue;
  99. } else if (who < 0) {
  100. warnx("%s: bad value", *argv);
  101. errs++;
  102. continue;
  103. }
  104. } else {
  105. if (getnum("pid", *argv, &who)) {
  106. errs++;
  107. continue;
  108. }
  109. if (who < 0) {
  110. warnx("%s: bad value", *argv);
  111. errs++;
  112. continue;
  113. }
  114. }
  115. errs += donice(which, who, prio, incr);
  116. }
  117. exit(errs != 0);
  118. }
  119. static int
  120. donice(int which, int who, int prio, int incr)
  121. {
  122. int oldprio;
  123. errno = 0;
  124. oldprio = getpriority(which, who);
  125. if (oldprio == -1 && errno) {
  126. warn("%d: getpriority", who);
  127. return (1);
  128. }
  129. if (incr)
  130. prio = oldprio + prio;
  131. if (prio > PRIO_MAX)
  132. prio = PRIO_MAX;
  133. if (prio < PRIO_MIN)
  134. prio = PRIO_MIN;
  135. if (setpriority(which, who, prio) < 0) {
  136. warn("%d: setpriority", who);
  137. return (1);
  138. }
  139. fprintf(stderr, "%d: old priority %d, new priority %d\n", who,
  140. oldprio, prio);
  141. return (0);
  142. }
  143. static int
  144. getnum(const char *com, const char *str, int *val)
  145. {
  146. long v;
  147. char *ep;
  148. errno = 0;
  149. v = strtol(str, &ep, 10);
  150. if (v < INT_MIN || v > INT_MAX || errno == ERANGE) {
  151. warnx("%s argument %s is out of range.", com, str);
  152. return (1);
  153. }
  154. if (ep == str || *ep != '\0' || errno != 0) {
  155. warnx("Bad %s argument: %s.", com, str);
  156. return (1);
  157. }
  158. *val = (int)v;
  159. return (0);
  160. }
  161. static void
  162. usage(void)
  163. {
  164. fprintf(stderr, "%s\n%s\n",
  165. "usage: renice priority [[-p] pid ...] [[-g] pgrp ...] [[-u] user ...]",
  166. " renice -n increment [[-p] pid ...] [[-g] pgrp ...] [[-u] user ...]");
  167. exit(1);
  168. }