nice.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * nice() for uClibc
  4. *
  5. * Copyright (C) 2000-2004 by Erik Andersen <andersen@codepoet.org>
  6. * Copyright (C) 2005 by Manuel Novoa III <mjn3@codepoet.org>
  7. *
  8. * GNU Library General Public License (LGPL) version 2 or later.
  9. */
  10. #define getpriority __getpriority
  11. #define setpriority __setpriority
  12. #include "syscalls.h"
  13. #include <unistd.h>
  14. #include <sys/resource.h>
  15. #ifdef __NR_nice
  16. #define __NR___syscall_nice __NR_nice
  17. static inline _syscall1(int, __syscall_nice, int, incr);
  18. #else
  19. #include <limits.h>
  20. static inline int int_add_no_wrap(int a, int b)
  21. {
  22. int s = a + b;
  23. if (b < 0) {
  24. if (s > a) s = INT_MIN;
  25. } else {
  26. if (s < a) s = INT_MAX;
  27. }
  28. return s;
  29. }
  30. static inline int __syscall_nice(int incr)
  31. {
  32. int old_priority;
  33. #if 1
  34. /* This should never fail. */
  35. old_priority = getpriority(PRIO_PROCESS, 0);
  36. #else
  37. /* But if you want to be paranoid... */
  38. int old_errno;
  39. old_errno = errno;
  40. __set_errno(0);
  41. old_priority = getpriority(PRIO_PROCESS, 0);
  42. if ((old_priority == -1) && errno) {
  43. return -1;
  44. }
  45. __set_errno(old_errno);
  46. #endif
  47. if (setpriority(PRIO_PROCESS, 0, int_add_no_wrap(old_priority, incr))) {
  48. __set_errno(EPERM); /* SUSv3 mandates EPERM for nice failure. */
  49. return -1;
  50. }
  51. return 0;
  52. }
  53. #endif
  54. int nice(int incr)
  55. {
  56. if (__syscall_nice(incr)) {
  57. return -1;
  58. }
  59. return getpriority(PRIO_PROCESS, 0);
  60. }