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