diff options
Diffstat (limited to 'libc/sysdeps')
-rw-r--r-- | libc/sysdeps/linux/common/nice.c | 68 |
1 files changed, 52 insertions, 16 deletions
diff --git a/libc/sysdeps/linux/common/nice.c b/libc/sysdeps/linux/common/nice.c index 27b0a7590..b21f63e08 100644 --- a/libc/sysdeps/linux/common/nice.c +++ b/libc/sysdeps/linux/common/nice.c @@ -3,35 +3,71 @@ * nice() for uClibc * * Copyright (C) 2000-2004 by Erik Andersen <andersen@codepoet.org> + * Copyright (C) 2005 by Manuel Novoa III <mjn3@codepoet.org> * * GNU Library General Public License (LGPL) version 2 or later. */ #include "syscalls.h" #include <unistd.h> +#include <sys/resource.h> + #ifdef __NR_nice -_syscall1(int, nice, int, inc); + +#define __NR___syscall_nice __NR_nice +static inline _syscall1(int, __syscall_nice, int, incr); + #else -#include <sys/resource.h> -int nice(int incr) + +#include <limits.h> + +static inline int int_add_no_wrap(int a, int b) { - int save, prio, result; + int s = a + b; - save = errno; + if (b < 0) { + if (s > a) s = INT_MIN; + } else { + if (s < a) s = INT_MAX; + } + + return s; +} + +static inline int __syscall_nice(int incr) +{ + int old_priority; +#if 1 + /* This should never fail. */ + old_priority = getpriority(PRIO_PROCESS, 0); +#else + /* But if you want to be paranoid... */ + int old_errno; + + old_errno = errno; __set_errno(0); - prio = getpriority(PRIO_PROCESS, 0); - if (prio == -1) { - if (errno != 0) { - return -1; - } else { - __set_errno(save); - } + old_priority = getpriority(PRIO_PROCESS, 0); + if ((old_priority == -1) && errno) { + return -1; } - result = setpriority(PRIO_PROCESS, 0, prio + incr); - if (result != -1) { - return prio + incr; - } else { + __set_errno(old_errno); +#endif + + if (setpriority(PRIO_PROCESS, 0, int_add_no_wrap(old_priority, incr))) { + __set_errno(EPERM); /* SUSv3 mandates EPERM for nice failure. */ return -1; } + + return 0; } + #endif + +int nice(int incr) +{ + if (__syscall_nice(incr)) { + return -1; + } + + return getpriority(PRIO_PROCESS, 0); +} |