upgrade to linux 2.6.10-1.12_FC2
[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         if (send_group_sig_info(SIGALRM, SEND_SIG_PRIV, p))
73                 printk("*warning*: failed to send SIGALRM to %u\n", p->pid);
74
75         interval = p->it_real_incr;
76         if (interval) {
77                 if (interval > (unsigned long) LONG_MAX)
78                         interval = LONG_MAX;
79                 p->real_timer.expires = jiffies + interval;
80                 add_timer(&p->real_timer);
81         }
82 }
83
84 int do_setitimer(int which, struct itimerval *value, struct itimerval *ovalue)
85 {
86         register unsigned long i, j;
87         int k;
88
89         i = timeval_to_jiffies(&value->it_interval);
90         j = timeval_to_jiffies(&value->it_value);
91         if (ovalue && (k = do_getitimer(which, ovalue)) < 0)
92                 return k;
93         switch (which) {
94                 case ITIMER_REAL:
95                         del_timer_sync(&current->real_timer);
96                         current->it_real_value = j;
97                         current->it_real_incr = i;
98                         if (!j)
99                                 break;
100                         if (j > (unsigned long) LONG_MAX)
101                                 j = LONG_MAX;
102                         i = j + jiffies;
103                         current->real_timer.expires = i;
104                         add_timer(&current->real_timer);
105                         break;
106                 case ITIMER_VIRTUAL:
107                         if (j)
108                                 j++;
109                         current->it_virt_value = j;
110                         current->it_virt_incr = i;
111                         break;
112                 case ITIMER_PROF:
113                         if (j)
114                                 j++;
115                         current->it_prof_value = j;
116                         current->it_prof_incr = i;
117                         break;
118                 default:
119                         return -EINVAL;
120         }
121         return 0;
122 }
123
124 /* SMP: Again, only we play with our itimers, and signals are SMP safe
125  *      now so that is not an issue at all anymore.
126  */
127 asmlinkage long sys_setitimer(int which,
128                               struct itimerval __user *value,
129                               struct itimerval __user *ovalue)
130 {
131         struct itimerval set_buffer, get_buffer;
132         int error;
133
134         if (value) {
135                 if(copy_from_user(&set_buffer, value, sizeof(set_buffer)))
136                         return -EFAULT;
137         } else
138                 memset((char *) &set_buffer, 0, sizeof(set_buffer));
139
140         error = do_setitimer(which, &set_buffer, ovalue ? &get_buffer : NULL);
141         if (error || !ovalue)
142                 return error;
143
144         if (copy_to_user(ovalue, &get_buffer, sizeof(get_buffer)))
145                 return -EFAULT; 
146         return 0;
147 }