nice.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. #include "syscalls.h"
  11. #include <unistd.h>
  12. #include <sys/resource.h>
  13. #ifdef __NR_nice
  14. #define __NR___syscall_nice __NR_nice
  15. static inline _syscall1(int, __syscall_nice, int, incr);
  16. #else
  17. #include <limits.h>
  18. static inline int int_add_no_wrap(int a, int b)
  19. {
  20. int s = a + b;
  21. if (b < 0) {
  22. if (s > a) s = INT_MIN;
  23. } else {
  24. if (s < a) s = INT_MAX;
  25. }
  26. return s;
  27. }
  28. static inline int __syscall_nice(int incr)
  29. {
  30. int old_priority;
  31. #if 1
  32. /* This should never fail. */
  33. old_priority = getpriority(PRIO_PROCESS, 0);
  34. #else
  35. /* But if you want to be paranoid... */
  36. int old_errno;
  37. old_errno = errno;
  38. __set_errno(0);
  39. old_priority = getpriority(PRIO_PROCESS, 0);
  40. if ((old_priority == -1) && errno) {
  41. return -1;
  42. }
  43. __set_errno(old_errno);
  44. #endif
  45. if (setpriority(PRIO_PROCESS, 0, int_add_no_wrap(old_priority, incr))) {
  46. __set_errno(EPERM); /* SUSv3 mandates EPERM for nice failure. */
  47. return -1;
  48. }
  49. return 0;
  50. }
  51. #endif
  52. int nice(int incr)
  53. {
  54. if (__syscall_nice(incr)) {
  55. return -1;
  56. }
  57. return getpriority(PRIO_PROCESS, 0);
  58. }