nice.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * nice() for uClibc
  3. *
  4. * Copyright (C) 2005 by Manuel Novoa III <mjn3@codepoet.org>
  5. * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
  6. *
  7. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  8. */
  9. #include <sys/syscall.h>
  10. #include <unistd.h>
  11. #include <sys/resource.h>
  12. #ifdef __NR_nice
  13. # define __NR___syscall_nice __NR_nice
  14. static __inline__ _syscall1(int, __syscall_nice, int, incr)
  15. #else
  16. # include <limits.h>
  17. static __inline__ int int_add_no_wrap(int a, int b)
  18. {
  19. if (b < 0) {
  20. if (a < INT_MIN - b)
  21. return INT_MIN;
  22. } else {
  23. if (a > INT_MAX - b)
  24. return INT_MAX;
  25. }
  26. return a + b;
  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. }