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. if (b < 0) {
  21. if (a < INT_MIN - b)
  22. return INT_MIN;
  23. } else {
  24. if (a > INT_MAX - b)
  25. return INT_MAX;
  26. }
  27. return a + b;
  28. }
  29. static __inline__ int __syscall_nice(int incr)
  30. {
  31. int old_priority;
  32. # if 1
  33. /* This should never fail. */
  34. old_priority = getpriority(PRIO_PROCESS, 0);
  35. # else
  36. /* But if you want to be paranoid... */
  37. int old_errno;
  38. old_errno = errno;
  39. __set_errno(0);
  40. old_priority = getpriority(PRIO_PROCESS, 0);
  41. if ((old_priority == -1) && errno) {
  42. return -1;
  43. }
  44. __set_errno(old_errno);
  45. # endif
  46. if (setpriority(PRIO_PROCESS, 0, int_add_no_wrap(old_priority, incr))) {
  47. __set_errno(EPERM); /* SUSv3 mandates EPERM for nice failure. */
  48. return -1;
  49. }
  50. return 0;
  51. }
  52. #endif
  53. int nice(int incr)
  54. {
  55. if (__syscall_nice(incr)) {
  56. return -1;
  57. }
  58. return getpriority(PRIO_PROCESS, 0);
  59. }