vbscript/tests: Add tests for persistent named items.
[wine.git] / server / event.c
blobd339d85aa4537fc7fcea98acff636aa78f8f0dc0
1 /*
2 * Server-side event management
4 * Copyright (C) 1998 Alexandre Julliard
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.
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.
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <stdarg.h>
29 #include "ntstatus.h"
30 #define WIN32_NO_STATUS
31 #include "windef.h"
32 #include "winternl.h"
34 #include "handle.h"
35 #include "thread.h"
36 #include "request.h"
37 #include "security.h"
39 struct event
41 struct object obj; /* object header */
42 struct list kernel_object; /* list of kernel object pointers */
43 int manual_reset; /* is it a manual reset event? */
44 int signaled; /* event has been signaled */
47 static void event_dump( struct object *obj, int verbose );
48 static struct object_type *event_get_type( struct object *obj );
49 static int event_signaled( struct object *obj, struct wait_queue_entry *entry );
50 static void event_satisfied( struct object *obj, struct wait_queue_entry *entry );
51 static unsigned int event_map_access( struct object *obj, unsigned int access );
52 static int event_signal( struct object *obj, unsigned int access);
53 static struct list *event_get_kernel_obj_list( struct object *obj );
55 static const struct object_ops event_ops =
57 sizeof(struct event), /* size */
58 event_dump, /* dump */
59 event_get_type, /* get_type */
60 add_queue, /* add_queue */
61 remove_queue, /* remove_queue */
62 event_signaled, /* signaled */
63 event_satisfied, /* satisfied */
64 event_signal, /* signal */
65 no_get_fd, /* get_fd */
66 event_map_access, /* map_access */
67 default_get_sd, /* get_sd */
68 default_set_sd, /* set_sd */
69 no_lookup_name, /* lookup_name */
70 directory_link_name, /* link_name */
71 default_unlink_name, /* unlink_name */
72 no_open_file, /* open_file */
73 event_get_kernel_obj_list, /* get_kernel_obj_list */
74 no_close_handle, /* close_handle */
75 no_destroy /* destroy */
79 struct keyed_event
81 struct object obj; /* object header */
84 static void keyed_event_dump( struct object *obj, int verbose );
85 static struct object_type *keyed_event_get_type( struct object *obj );
86 static int keyed_event_signaled( struct object *obj, struct wait_queue_entry *entry );
87 static unsigned int keyed_event_map_access( struct object *obj, unsigned int access );
89 static const struct object_ops keyed_event_ops =
91 sizeof(struct keyed_event), /* size */
92 keyed_event_dump, /* dump */
93 keyed_event_get_type, /* get_type */
94 add_queue, /* add_queue */
95 remove_queue, /* remove_queue */
96 keyed_event_signaled, /* signaled */
97 no_satisfied, /* satisfied */
98 no_signal, /* signal */
99 no_get_fd, /* get_fd */
100 keyed_event_map_access, /* map_access */
101 default_get_sd, /* get_sd */
102 default_set_sd, /* set_sd */
103 no_lookup_name, /* lookup_name */
104 directory_link_name, /* link_name */
105 default_unlink_name, /* unlink_name */
106 no_open_file, /* open_file */
107 no_kernel_obj_list, /* get_kernel_obj_list */
108 no_close_handle, /* close_handle */
109 no_destroy /* destroy */
113 struct event *create_event( struct object *root, const struct unicode_str *name,
114 unsigned int attr, int manual_reset, int initial_state,
115 const struct security_descriptor *sd )
117 struct event *event;
119 if ((event = create_named_object( root, &event_ops, name, attr, sd )))
121 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
123 /* initialize it if it didn't already exist */
124 list_init( &event->kernel_object );
125 event->manual_reset = manual_reset;
126 event->signaled = initial_state;
129 return event;
132 struct event *get_event_obj( struct process *process, obj_handle_t handle, unsigned int access )
134 return (struct event *)get_handle_obj( process, handle, access, &event_ops );
137 void pulse_event( struct event *event )
139 event->signaled = 1;
140 /* wake up all waiters if manual reset, a single one otherwise */
141 wake_up( &event->obj, !event->manual_reset );
142 event->signaled = 0;
145 void set_event( struct event *event )
147 event->signaled = 1;
148 /* wake up all waiters if manual reset, a single one otherwise */
149 wake_up( &event->obj, !event->manual_reset );
152 void reset_event( struct event *event )
154 event->signaled = 0;
157 static void event_dump( struct object *obj, int verbose )
159 struct event *event = (struct event *)obj;
160 assert( obj->ops == &event_ops );
161 fprintf( stderr, "Event manual=%d signaled=%d\n",
162 event->manual_reset, event->signaled );
165 static struct object_type *event_get_type( struct object *obj )
167 static const WCHAR name[] = {'E','v','e','n','t'};
168 static const struct unicode_str str = { name, sizeof(name) };
169 return get_object_type( &str );
172 static int event_signaled( struct object *obj, struct wait_queue_entry *entry )
174 struct event *event = (struct event *)obj;
175 assert( obj->ops == &event_ops );
176 return event->signaled;
179 static void event_satisfied( struct object *obj, struct wait_queue_entry *entry )
181 struct event *event = (struct event *)obj;
182 assert( obj->ops == &event_ops );
183 /* Reset if it's an auto-reset event */
184 if (!event->manual_reset) event->signaled = 0;
187 static unsigned int event_map_access( struct object *obj, unsigned int access )
189 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | EVENT_QUERY_STATE;
190 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | EVENT_MODIFY_STATE;
191 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE;
192 if (access & GENERIC_ALL) access |= STANDARD_RIGHTS_ALL | EVENT_QUERY_STATE | EVENT_MODIFY_STATE;
193 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
196 static int event_signal( struct object *obj, unsigned int access )
198 struct event *event = (struct event *)obj;
199 assert( obj->ops == &event_ops );
201 if (!(access & EVENT_MODIFY_STATE))
203 set_error( STATUS_ACCESS_DENIED );
204 return 0;
206 set_event( event );
207 return 1;
210 static struct list *event_get_kernel_obj_list( struct object *obj )
212 struct event *event = (struct event *)obj;
213 return &event->kernel_object;
216 struct keyed_event *create_keyed_event( struct object *root, const struct unicode_str *name,
217 unsigned int attr, const struct security_descriptor *sd )
219 struct keyed_event *event;
221 if ((event = create_named_object( root, &keyed_event_ops, name, attr, sd )))
223 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
225 /* initialize it if it didn't already exist */
228 return event;
231 struct keyed_event *get_keyed_event_obj( struct process *process, obj_handle_t handle, unsigned int access )
233 return (struct keyed_event *)get_handle_obj( process, handle, access, &keyed_event_ops );
236 static void keyed_event_dump( struct object *obj, int verbose )
238 fputs( "Keyed event\n", stderr );
241 static struct object_type *keyed_event_get_type( struct object *obj )
243 static const WCHAR name[] = {'K','e','y','e','d','E','v','e','n','t'};
244 static const struct unicode_str str = { name, sizeof(name) };
245 return get_object_type( &str );
248 static enum select_op matching_op( enum select_op op )
250 return op ^ (SELECT_KEYED_EVENT_WAIT ^ SELECT_KEYED_EVENT_RELEASE);
253 static int keyed_event_signaled( struct object *obj, struct wait_queue_entry *entry )
255 struct wait_queue_entry *ptr;
256 struct process *process;
257 enum select_op select_op;
259 assert( obj->ops == &keyed_event_ops );
261 process = get_wait_queue_thread( entry )->process;
262 select_op = get_wait_queue_select_op( entry );
263 if (select_op != SELECT_KEYED_EVENT_WAIT && select_op != SELECT_KEYED_EVENT_RELEASE) return 1;
265 LIST_FOR_EACH_ENTRY( ptr, &obj->wait_queue, struct wait_queue_entry, entry )
267 if (ptr == entry) continue;
268 if (get_wait_queue_thread( ptr )->process != process) continue;
269 if (get_wait_queue_select_op( ptr ) != matching_op( select_op )) continue;
270 if (get_wait_queue_key( ptr ) != get_wait_queue_key( entry )) continue;
271 if (wake_thread_queue_entry( ptr )) return 1;
273 return 0;
276 static unsigned int keyed_event_map_access( struct object *obj, unsigned int access )
278 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | KEYEDEVENT_WAIT;
279 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | KEYEDEVENT_WAKE;
280 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
281 if (access & GENERIC_ALL) access |= KEYEDEVENT_ALL_ACCESS;
282 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
285 /* create an event */
286 DECL_HANDLER(create_event)
288 struct event *event;
289 struct unicode_str name;
290 struct object *root;
291 const struct security_descriptor *sd;
292 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
294 if (!objattr) return;
296 if ((event = create_event( root, &name, objattr->attributes,
297 req->manual_reset, req->initial_state, sd )))
299 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
300 reply->handle = alloc_handle( current->process, event, req->access, objattr->attributes );
301 else
302 reply->handle = alloc_handle_no_access_check( current->process, event,
303 req->access, objattr->attributes );
304 release_object( event );
307 if (root) release_object( root );
310 /* open a handle to an event */
311 DECL_HANDLER(open_event)
313 struct unicode_str name = get_req_unicode_str();
315 reply->handle = open_object( current->process, req->rootdir, req->access,
316 &event_ops, &name, req->attributes );
319 /* do an event operation */
320 DECL_HANDLER(event_op)
322 struct event *event;
324 if (!(event = get_event_obj( current->process, req->handle, EVENT_MODIFY_STATE ))) return;
325 reply->state = event->signaled;
326 switch(req->op)
328 case PULSE_EVENT:
329 pulse_event( event );
330 break;
331 case SET_EVENT:
332 set_event( event );
333 break;
334 case RESET_EVENT:
335 reset_event( event );
336 break;
337 default:
338 set_error( STATUS_INVALID_PARAMETER );
339 break;
341 release_object( event );
344 /* return details about the event */
345 DECL_HANDLER(query_event)
347 struct event *event;
349 if (!(event = get_event_obj( current->process, req->handle, EVENT_QUERY_STATE ))) return;
351 reply->manual_reset = event->manual_reset;
352 reply->state = event->signaled;
354 release_object( event );
357 /* create a keyed event */
358 DECL_HANDLER(create_keyed_event)
360 struct keyed_event *event;
361 struct unicode_str name;
362 struct object *root;
363 const struct security_descriptor *sd;
364 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
366 if (!objattr) return;
368 if ((event = create_keyed_event( root, &name, objattr->attributes, sd )))
370 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
371 reply->handle = alloc_handle( current->process, event, req->access, objattr->attributes );
372 else
373 reply->handle = alloc_handle_no_access_check( current->process, event,
374 req->access, objattr->attributes );
375 release_object( event );
377 if (root) release_object( root );
380 /* open a handle to a keyed event */
381 DECL_HANDLER(open_keyed_event)
383 struct unicode_str name = get_req_unicode_str();
385 reply->handle = open_object( current->process, req->rootdir, req->access,
386 &keyed_event_ops, &name, req->attributes );