]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/uclibc/lib/contrib/uclibc/libc/sysdeps/linux/common/nice.c
update
[l4.git] / l4 / pkg / uclibc / lib / contrib / uclibc / libc / sysdeps / linux / common / nice.c
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
11 #include <sys/syscall.h>
12 #include <unistd.h>
13 #include <sys/resource.h>
14
15
16 #ifdef __NR_nice
17
18 # define __NR___syscall_nice __NR_nice
19 static __inline__ _syscall1(int, __syscall_nice, int, incr)
20
21 #else
22
23 # include <limits.h>
24
25
26 static __inline__ int int_add_no_wrap(int a, int b)
27 {
28         if (b < 0) {
29                 if (a < INT_MIN - b)
30                         return INT_MIN;
31         } else {
32                 if (a > INT_MAX - b)
33                         return INT_MAX;
34         }
35
36         return a + b;
37 }
38
39 static __inline__ int __syscall_nice(int incr)
40 {
41         int old_priority;
42 # if 1
43         /* This should never fail. */
44         old_priority = getpriority(PRIO_PROCESS, 0);
45 # else
46         /* But if you want to be paranoid... */
47         int old_errno;
48
49         old_errno = errno;
50         __set_errno(0);
51         old_priority = getpriority(PRIO_PROCESS, 0);
52         if ((old_priority == -1) && errno) {
53                 return -1;
54         }
55         __set_errno(old_errno);
56 # endif
57
58         if (setpriority(PRIO_PROCESS, 0, int_add_no_wrap(old_priority, incr))) {
59                 __set_errno(EPERM);     /* SUSv3 mandates EPERM for nice failure. */
60                 return -1;
61         }
62
63         return 0;
64 }
65
66 #endif
67
68 int nice(int incr)
69 {
70         if (__syscall_nice(incr)) {
71                 return -1;
72         }
73
74         return getpriority(PRIO_PROCESS, 0);
75 }