nice.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 "syscalls.h"
  11. #include <unistd.h>
  12. #include <sys/resource.h>
  13. libc_hidden_proto(getpriority)
  14. #ifdef __NR_nice
  15. # define __NR___syscall_nice __NR_nice
  16. static inline _syscall1(int, __syscall_nice, int, incr);
  17. #else
  18. # include <limits.h>
  19. libc_hidden_proto(setpriority)
  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. }