nice.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * nice() for uClibc
  4. *
  5. * Copyright (C) 2005 by Manuel Novoa III <mjn3@codepoet.org>
  6. * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
  7. *
  8. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  9. */
  10. #include <sys/syscall.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. }