vserver 1.9.5.x5
[linux-2.6.git] / kernel / itimer.c
1 /*
2  * linux/kernel/itimer.c
3  *
4  * Copyright (C) 1992 Darren Senn
5  */
6
7 /* These are all the functions necessary to implement itimers */
8
9 #include <linux/mm.h>
10 #include <linux/smp_lock.h>
11 #include <linux/interrupt.h>
12 #include <linux/syscalls.h>
13 #include <linux/time.h>
14
15 #include <asm/uaccess.h>
16
17 int do_getitimer(int which, struct itimerval *value)
18 {
19         register unsigned long val;
20
21         switch (which) {
22         case ITIMER_REAL:
23                 val = 0;
24                 /* 
25                  * FIXME! This needs to be atomic, in case the kernel timer happens!
26                  */
27                 if (timer_pending(&current->real_timer)) {
28                         val = current->real_timer.expires - jiffies;
29
30                         /* look out for negative/zero itimer.. */
31                         if ((long) val <= 0)
32                                 val = 1;
33                 }
34                 jiffies_to_timeval(val, &value->it_value);
35                 jiffies_to_timeval(current->it_real_incr, &value->it_interval);
36                 break;
37         case ITIMER_VIRTUAL:
38                 cputime_to_timeval(current->it_virt_value, &value->it_value);
39                 cputime_to_timeval(current->it_virt_incr, &value->it_interval);
40                 break;
41         case ITIMER_PROF:
42                 cputime_to_timeval(current->it_prof_value, &value->it_value);
43                 cputime_to_timeval(current->it_prof_incr, &value->it_interval);
44                 break;
45         default:
46                 return(-EINVAL);
47         }
48         return 0;
49 }
50
51 /* SMP: Only we modify our itimer values. */
52 asmlinkage long sys_getitimer(int which, struct itimerval __user *value)
53 {
54         int error = -EFAULT;
55         struct itimerval get_buffer;
56
57         if (value) {
58                 error = do_getitimer(which, &get_buffer);
59                 if (!error &&
60                     copy_to_user(value, &get_buffer, sizeof(get_buffer)))
61                         error = -EFAULT;
62         }
63         return error;
64 }
65
66 void it_real_fn(unsigned long __data)
67 {
68         struct task_struct * p = (struct task_struct *) __data;
69         unsigned long interval;
70
71         send_group_sig_info(SIGALRM, SEND_SIG_PRIV, p);
72         interval = p->it_real_incr;
73         if (interval) {
74                 if (interval > (unsigned long) LONG_MAX)
75                         interval = LONG_MAX;
76                 p->real_timer.expires = jiffies + interval;
77                 add_timer(&p->real_timer);
78         }
79 }
80
81 int do_setitimer(int which, struct itimerval *value, struct itimerval *ovalue)
82 {
83         unsigned long expire;
84         cputime_t cputime;
85         int k;
86
87         if (ovalue && (k = do_getitimer(which, ovalue)) < 0)
88                 return k;
89         switch (which) {
90                 case ITIMER_REAL:
91                         del_timer_sync(&current->real_timer);
92                         expire = timeval_to_jiffies(&value->it_value);
93                         current->it_real_value = expire;
94                         current->it_real_incr =
95                                 timeval_to_jiffies(&value->it_interval);
96                         if (!expire)
97                                 break;
98                         if (expire > (unsigned long) LONG_MAX)
99                                 expire = LONG_MAX;
100                         current->real_timer.expires = jiffies + expire;
101                         add_timer(&current->real_timer);
102                         break;
103                 case ITIMER_VIRTUAL:
104                         cputime = timeval_to_cputime(&value->it_value);
105                         if (cputime_gt(cputime, cputime_zero))
106                                 cputime = cputime_add(cputime,
107                                                       jiffies_to_cputime(1));
108                         current->it_virt_value = cputime;
109                         cputime = timeval_to_cputime(&value->it_interval);
110                         current->it_virt_incr = cputime;
111                         break;
112                 case ITIMER_PROF:
113                         cputime = timeval_to_cputime(&value->it_value);
114                         if (cputime_gt(cputime, cputime_zero))
115                                 cputime = cputime_add(cputime,
116                                                       jiffies_to_cputime(1));
117                         current->it_prof_value = cputime;
118                         cputime = timeval_to_cputime(&value->it_interval);
119                         current->it_prof_incr = cputime;
120                         break;
121                 default:
122                         return -EINVAL;
123         }
124         return 0;
125 }
126
127 /* SMP: Again, only we play with our itimers, and signals are SMP safe
128  *      now so that is not an issue at all anymore.
129  */
130 asmlinkage long sys_setitimer(int which,
131                               struct itimerval __user *value,
132                               struct itimerval __user *ovalue)
133 {
134         struct itimerval set_buffer, get_buffer;
135         int error;
136
137         if (value) {
138                 if(copy_from_user(&set_buffer, value, sizeof(set_buffer)))
139                         return -EFAULT;
140         } else
141                 memset((char *) &set_buffer, 0, sizeof(set_buffer));
142
143         error = do_setitimer(which, &set_buffer, ovalue ? &get_buffer : NULL);
144         if (error || !ovalue)
145                 return error;
146
147         if (copy_to_user(ovalue, &get_buffer, sizeof(get_buffer)))
148                 return -EFAULT; 
149         return 0;
150 }