OSDN Git Service

hidden_def/hidden_proto: convert all users (I hope) termios split, add some missing...
[uclinux-h8/uClibc.git] / libc / sysdeps / linux / common / nice.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * nice() for uClibc
4  *
5  * Copyright (C) 2000-2004 by Erik Andersen <andersen@codepoet.org>
6  * Copyright (C) 2005 by Manuel Novoa III <mjn3@codepoet.org>
7  *
8  * GNU Library General Public License (LGPL) version 2 or later.
9  */
10
11 #include "syscalls.h"
12 #include <unistd.h>
13 #include <sys/resource.h>
14
15 libc_hidden_proto(getpriority)
16
17 #ifdef __NR_nice
18
19 #define __NR___syscall_nice __NR_nice
20 static inline _syscall1(int, __syscall_nice, int, incr);
21
22 #else
23
24 #include <limits.h>
25
26 libc_hidden_proto(setpriority)
27
28 static inline int int_add_no_wrap(int a, int b)
29 {
30         int s = a + b;
31
32         if (b < 0) {
33                 if (s > a) s = INT_MIN;
34         } else {
35                 if (s < a) s = INT_MAX;
36         }
37
38         return s;
39 }
40
41 static inline int __syscall_nice(int incr)
42 {
43         int old_priority;
44 #if 1
45         /* This should never fail. */
46         old_priority = getpriority(PRIO_PROCESS, 0);
47 #else
48         /* But if you want to be paranoid... */
49         int old_errno;
50
51         old_errno = errno;
52         __set_errno(0);
53         old_priority = getpriority(PRIO_PROCESS, 0);
54         if ((old_priority == -1) && errno) {
55                 return -1;
56         }
57         __set_errno(old_errno);
58 #endif
59
60         if (setpriority(PRIO_PROCESS, 0, int_add_no_wrap(old_priority, incr))) {
61                 __set_errno(EPERM);     /* SUSv3 mandates EPERM for nice failure. */
62                 return -1;
63         }
64
65         return 0;
66 }
67
68 #endif
69
70 int nice(int incr)
71 {
72         if (__syscall_nice(incr)) {
73                 return -1;
74         }
75
76         return getpriority(PRIO_PROCESS, 0);
77 }