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