server: Add get_sd and set_sd object operations to allow the security descriptor...
[wine] / server / timer.c
1 /*
2  * Waitable timers management
3  *
4  * Copyright (C) 1999 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <assert.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <sys/time.h>
28 #include <sys/types.h>
29 #include <stdarg.h>
30
31 #include "ntstatus.h"
32 #define WIN32_NO_STATUS
33 #include "windef.h"
34 #include "winternl.h"
35
36 #include "file.h"
37 #include "handle.h"
38 #include "request.h"
39
40 struct timer
41 {
42     struct object        obj;       /* object header */
43     int                  manual;    /* manual reset */
44     int                  signaled;  /* current signaled state */
45     unsigned int         period;    /* timer period in ms */
46     timeout_t            when;      /* next expiration */
47     struct timeout_user *timeout;   /* timeout user */
48     struct thread       *thread;    /* thread that set the APC function */
49     void                *callback;  /* callback APC function */
50     void                *arg;       /* callback argument */
51 };
52
53 static void timer_dump( struct object *obj, int verbose );
54 static int timer_signaled( struct object *obj, struct thread *thread );
55 static int timer_satisfied( struct object *obj, struct thread *thread );
56 static unsigned int timer_map_access( struct object *obj, unsigned int access );
57 static void timer_destroy( struct object *obj );
58
59 static const struct object_ops timer_ops =
60 {
61     sizeof(struct timer),      /* size */
62     timer_dump,                /* dump */
63     add_queue,                 /* add_queue */
64     remove_queue,              /* remove_queue */
65     timer_signaled,            /* signaled */
66     timer_satisfied,           /* satisfied */
67     no_signal,                 /* signal */
68     no_get_fd,                 /* get_fd */
69     timer_map_access,          /* map_access */
70     default_get_sd,            /* get_sd */
71     default_set_sd,            /* set_sd */
72     no_lookup_name,            /* lookup_name */
73     no_open_file,              /* open_file */
74     no_close_handle,           /* close_handle */
75     timer_destroy              /* destroy */
76 };
77
78
79 /* create a timer object */
80 static struct timer *create_timer( struct directory *root, const struct unicode_str *name,
81                                    unsigned int attr, int manual )
82 {
83     struct timer *timer;
84
85     if ((timer = create_named_object_dir( root, name, attr, &timer_ops )))
86     {
87         if (get_error() != STATUS_OBJECT_NAME_EXISTS)
88         {
89             /* initialize it if it didn't already exist */
90             timer->manual   = manual;
91             timer->signaled = 0;
92             timer->when     = 0;
93             timer->period   = 0;
94             timer->timeout  = NULL;
95             timer->thread   = NULL;
96         }
97     }
98     return timer;
99 }
100
101 /* callback on timer expiration */
102 static void timer_callback( void *private )
103 {
104     struct timer *timer = (struct timer *)private;
105
106     /* queue an APC */
107     if (timer->thread)
108     {
109         apc_call_t data;
110
111         memset( &data, 0, sizeof(data) );
112         if (timer->callback)
113         {
114             data.type       = APC_TIMER;
115             data.timer.func = timer->callback;
116             data.timer.time = timer->when;
117             data.timer.arg  = timer->arg;
118         }
119         else data.type = APC_NONE;  /* wake up only */
120
121         if (!thread_queue_apc( timer->thread, &timer->obj, &data ))
122         {
123             release_object( timer->thread );
124             timer->thread = NULL;
125         }
126     }
127
128     if (timer->period)  /* schedule the next expiration */
129     {
130         timer->when += (timeout_t)timer->period * 10000;
131         timer->timeout = add_timeout_user( timer->when, timer_callback, timer );
132     }
133     else timer->timeout = NULL;
134
135     /* wake up waiters */
136     timer->signaled = 1;
137     wake_up( &timer->obj, 0 );
138 }
139
140 /* cancel a running timer */
141 static int cancel_timer( struct timer *timer )
142 {
143     int signaled = timer->signaled;
144
145     if (timer->timeout)
146     {
147         remove_timeout_user( timer->timeout );
148         timer->timeout = NULL;
149     }
150     if (timer->thread)
151     {
152         thread_cancel_apc( timer->thread, &timer->obj, APC_TIMER );
153         release_object( timer->thread );
154         timer->thread = NULL;
155     }
156     return signaled;
157 }
158
159 /* set the timer expiration and period */
160 static int set_timer( struct timer *timer, timeout_t expire, unsigned int period,
161                       void *callback, void *arg )
162 {
163     int signaled = cancel_timer( timer );
164     if (timer->manual)
165     {
166         period = 0;  /* period doesn't make any sense for a manual timer */
167         timer->signaled = 0;
168     }
169     timer->when     = (expire <= 0) ? current_time - expire : max( expire, current_time );
170     timer->period   = period;
171     timer->callback = callback;
172     timer->arg      = arg;
173     if (callback) timer->thread = (struct thread *)grab_object( current );
174     timer->timeout = add_timeout_user( timer->when, timer_callback, timer );
175     return signaled;
176 }
177
178 static void timer_dump( struct object *obj, int verbose )
179 {
180     struct timer *timer = (struct timer *)obj;
181     assert( obj->ops == &timer_ops );
182     fprintf( stderr, "Timer manual=%d when=%s period=%u ",
183              timer->manual, get_timeout_str(timer->when), timer->period );
184     dump_object_name( &timer->obj );
185     fputc( '\n', stderr );
186 }
187
188 static int timer_signaled( struct object *obj, struct thread *thread )
189 {
190     struct timer *timer = (struct timer *)obj;
191     assert( obj->ops == &timer_ops );
192     return timer->signaled;
193 }
194
195 static int timer_satisfied( struct object *obj, struct thread *thread )
196 {
197     struct timer *timer = (struct timer *)obj;
198     assert( obj->ops == &timer_ops );
199     if (!timer->manual) timer->signaled = 0;
200     return 0;
201 }
202
203 static unsigned int timer_map_access( struct object *obj, unsigned int access )
204 {
205     if (access & GENERIC_READ)    access |= STANDARD_RIGHTS_READ | SYNCHRONIZE | TIMER_QUERY_STATE;
206     if (access & GENERIC_WRITE)   access |= STANDARD_RIGHTS_WRITE | TIMER_MODIFY_STATE;
207     if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
208     if (access & GENERIC_ALL)     access |= TIMER_ALL_ACCESS;
209     return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
210 }
211
212 static void timer_destroy( struct object *obj )
213 {
214     struct timer *timer = (struct timer *)obj;
215     assert( obj->ops == &timer_ops );
216
217     if (timer->timeout) remove_timeout_user( timer->timeout );
218     if (timer->thread) release_object( timer->thread );
219 }
220
221 /* create a timer */
222 DECL_HANDLER(create_timer)
223 {
224     struct timer *timer;
225     struct unicode_str name;
226     struct directory *root = NULL;
227
228     reply->handle = 0;
229     get_req_unicode_str( &name );
230     if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
231         return;
232
233     if ((timer = create_timer( root, &name, req->attributes, req->manual )))
234     {
235         reply->handle = alloc_handle( current->process, timer, req->access, req->attributes );
236         release_object( timer );
237     }
238
239     if (root) release_object( root );
240 }
241
242 /* open a handle to a timer */
243 DECL_HANDLER(open_timer)
244 {
245     struct unicode_str name;
246     struct directory *root = NULL;
247     struct timer *timer;
248
249     get_req_unicode_str( &name );
250     if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
251         return;
252
253     if ((timer = open_object_dir( root, &name, req->attributes, &timer_ops )))
254     {
255         reply->handle = alloc_handle( current->process, &timer->obj, req->access, req->attributes );
256         release_object( timer );
257     }
258
259     if (root) release_object( root );
260 }
261
262 /* set a waitable timer */
263 DECL_HANDLER(set_timer)
264 {
265     struct timer *timer;
266
267     if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
268                                                  TIMER_MODIFY_STATE, &timer_ops )))
269     {
270         reply->signaled = set_timer( timer, req->expire, req->period, req->callback, req->arg );
271         release_object( timer );
272     }
273 }
274
275 /* cancel a waitable timer */
276 DECL_HANDLER(cancel_timer)
277 {
278     struct timer *timer;
279
280     if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
281                                                  TIMER_MODIFY_STATE, &timer_ops )))
282     {
283         reply->signaled = cancel_timer( timer );
284         release_object( timer );
285     }
286 }
287
288 /* Get information on a waitable timer */
289 DECL_HANDLER(get_timer_info)
290 {
291     struct timer *timer;
292
293     if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
294                                                  TIMER_QUERY_STATE, &timer_ops )))
295     {
296         reply->when      = timer->when;
297         reply->signaled  = timer->signaled;
298         release_object( timer );
299     }
300 }