renice.c 991 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /* renice.c - renice process
  2. *
  3. * Copyright 2013 CE Strake <strake888 at gmail.com>
  4. *
  5. * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/renice.html
  6. USE_RENICE(NEWTOY(renice, "<1gpun#|", TOYFLAG_USR|TOYFLAG_BIN))
  7. config RENICE
  8. bool "renice"
  9. default y
  10. help
  11. usage: renice [-gpu] -n INCREMENT ID...
  12. */
  13. #define FOR_renice
  14. #include "toys.h"
  15. GLOBALS(
  16. long n;
  17. )
  18. void renice_main(void) {
  19. int which = (toys.optflags & FLAG_g) ? PRIO_PGRP :
  20. ((toys.optflags & FLAG_u) ? PRIO_USER : PRIO_PROCESS);
  21. char **arg;
  22. for (arg = toys.optargs; *arg; arg++) {
  23. char *s = *arg;
  24. int id = -1;
  25. if (toys.optflags & FLAG_u) {
  26. struct passwd *p = getpwnam(s);
  27. if (p) id = p->pw_uid;
  28. } else {
  29. id = strtol(s, &s, 10);
  30. if (*s) id = -1;
  31. }
  32. if (id < 0) {
  33. error_msg("bad '%s'", *arg);
  34. continue;
  35. }
  36. if (setpriority(which, id, getpriority(which, id)+TT.n) < 0)
  37. perror_msg("setpriority %d", id);
  38. }
  39. }