widl: Write proper C++ forward declarations for interfaces inside namespace.
[wine.git] / server / event.c
blob4d3c5621fca5edc695f9a2c485cf18e77556176e
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 int manual_reset; /* is it a manual reset event? */
43 int signaled; /* event has been signaled */
46 static void event_dump( struct object *obj, int verbose );
47 static struct object_type *event_get_type( struct object *obj );
48 static int event_signaled( struct object *obj, struct wait_queue_entry *entry );
49 static void event_satisfied( struct object *obj, struct wait_queue_entry *entry );
50 static unsigned int event_map_access( struct object *obj, unsigned int access );
51 static int event_signal( struct object *obj, unsigned int access);
53 static const struct object_ops event_ops =
55 sizeof(struct event), /* size */
56 event_dump, /* dump */
57 event_get_type, /* get_type */
58 add_queue, /* add_queue */
59 remove_queue, /* remove_queue */
60 event_signaled, /* signaled */
61 event_satisfied, /* satisfied */
62 event_signal, /* signal */
63 no_get_fd, /* get_fd */
64 event_map_access, /* map_access */
65 default_get_sd, /* get_sd */
66 default_set_sd, /* set_sd */
67 no_lookup_name, /* lookup_name */
68 no_open_file, /* open_file */
69 no_close_handle, /* close_handle */
70 no_destroy /* destroy */
74 struct keyed_event
76 struct object obj; /* object header */
79 static void keyed_event_dump( struct object *obj, int verbose );
80 static struct object_type *keyed_event_get_type( struct object *obj );
81 static int keyed_event_signaled( struct object *obj, struct wait_queue_entry *entry );
82 static unsigned int keyed_event_map_access( struct object *obj, unsigned int access );
84 static const struct object_ops keyed_event_ops =
86 sizeof(struct keyed_event), /* size */
87 keyed_event_dump, /* dump */
88 keyed_event_get_type, /* get_type */
89 add_queue, /* add_queue */
90 remove_queue, /* remove_queue */
91 keyed_event_signaled, /* signaled */
92 no_satisfied, /* satisfied */
93 no_signal, /* signal */
94 no_get_fd, /* get_fd */
95 keyed_event_map_access, /* map_access */
96 default_get_sd, /* get_sd */
97 default_set_sd, /* set_sd */
98 no_lookup_name, /* lookup_name */
99 no_open_file, /* open_file */
100 no_close_handle, /* close_handle */
101 no_destroy /* destroy */
105 struct event *create_event( struct directory *root, const struct unicode_str *name,
106 unsigned int attr, int manual_reset, int initial_state,
107 const struct security_descriptor *sd )
109 struct event *event;
111 if ((event = create_named_object_dir( root, name, attr, &event_ops )))
113 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
115 /* initialize it if it didn't already exist */
116 event->manual_reset = manual_reset;
117 event->signaled = initial_state;
118 if (sd) default_set_sd( &event->obj, sd, OWNER_SECURITY_INFORMATION|
119 GROUP_SECURITY_INFORMATION|
120 DACL_SECURITY_INFORMATION|
121 SACL_SECURITY_INFORMATION );
124 return event;
127 struct event *get_event_obj( struct process *process, obj_handle_t handle, unsigned int access )
129 return (struct event *)get_handle_obj( process, handle, access, &event_ops );
132 void pulse_event( struct event *event )
134 event->signaled = 1;
135 /* wake up all waiters if manual reset, a single one otherwise */
136 wake_up( &event->obj, !event->manual_reset );
137 event->signaled = 0;
140 void set_event( struct event *event )
142 event->signaled = 1;
143 /* wake up all waiters if manual reset, a single one otherwise */
144 wake_up( &event->obj, !event->manual_reset );
147 void reset_event( struct event *event )
149 event->signaled = 0;
152 static void event_dump( struct object *obj, int verbose )
154 struct event *event = (struct event *)obj;
155 assert( obj->ops == &event_ops );
156 fprintf( stderr, "Event manual=%d signaled=%d ",
157 event->manual_reset, event->signaled );
158 dump_object_name( &event->obj );
159 fputc( '\n', stderr );
162 static struct object_type *event_get_type( struct object *obj )
164 static const WCHAR name[] = {'E','v','e','n','t'};
165 static const struct unicode_str str = { name, sizeof(name) };
166 return get_object_type( &str );
169 static int event_signaled( struct object *obj, struct wait_queue_entry *entry )
171 struct event *event = (struct event *)obj;
172 assert( obj->ops == &event_ops );
173 return event->signaled;
176 static void event_satisfied( struct object *obj, struct wait_queue_entry *entry )
178 struct event *event = (struct event *)obj;
179 assert( obj->ops == &event_ops );
180 /* Reset if it's an auto-reset event */
181 if (!event->manual_reset) event->signaled = 0;
184 static unsigned int event_map_access( struct object *obj, unsigned int access )
186 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | EVENT_QUERY_STATE;
187 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | EVENT_MODIFY_STATE;
188 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE;
189 if (access & GENERIC_ALL) access |= STANDARD_RIGHTS_ALL | EVENT_QUERY_STATE | EVENT_MODIFY_STATE;
190 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
193 static int event_signal( struct object *obj, unsigned int access )
195 struct event *event = (struct event *)obj;
196 assert( obj->ops == &event_ops );
198 if (!(access & EVENT_MODIFY_STATE))
200 set_error( STATUS_ACCESS_DENIED );
201 return 0;
203 set_event( event );
204 return 1;
207 struct keyed_event *create_keyed_event( struct directory *root, const struct unicode_str *name,
208 unsigned int attr, const struct security_descriptor *sd )
210 struct keyed_event *event;
212 if ((event = create_named_object_dir( root, name, attr, &keyed_event_ops )))
214 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
216 /* initialize it if it didn't already exist */
217 if (sd) default_set_sd( &event->obj, sd, OWNER_SECURITY_INFORMATION|
218 GROUP_SECURITY_INFORMATION|
219 DACL_SECURITY_INFORMATION|
220 SACL_SECURITY_INFORMATION );
223 return event;
226 struct keyed_event *get_keyed_event_obj( struct process *process, obj_handle_t handle, unsigned int access )
228 return (struct keyed_event *)get_handle_obj( process, handle, access, &keyed_event_ops );
231 static void keyed_event_dump( struct object *obj, int verbose )
233 struct keyed_event *event = (struct keyed_event *)obj;
234 assert( obj->ops == &keyed_event_ops );
235 fprintf( stderr, "Keyed event " );
236 dump_object_name( &event->obj );
237 fputc( '\n', stderr );
240 static struct object_type *keyed_event_get_type( struct object *obj )
242 static const WCHAR name[] = {'K','e','y','e','d','E','v','e','n','t'};
243 static const struct unicode_str str = { name, sizeof(name) };
244 return get_object_type( &str );
247 static enum select_op matching_op( enum select_op op )
249 return op ^ (SELECT_KEYED_EVENT_WAIT ^ SELECT_KEYED_EVENT_RELEASE);
252 static int keyed_event_signaled( struct object *obj, struct wait_queue_entry *entry )
254 struct wait_queue_entry *ptr;
255 struct process *process;
256 enum select_op select_op;
258 assert( obj->ops == &keyed_event_ops );
260 process = get_wait_queue_thread( entry )->process;
261 select_op = get_wait_queue_select_op( entry );
262 if (select_op != SELECT_KEYED_EVENT_WAIT && select_op != SELECT_KEYED_EVENT_RELEASE) return 1;
264 LIST_FOR_EACH_ENTRY( ptr, &obj->wait_queue, struct wait_queue_entry, entry )
266 if (ptr == entry) continue;
267 if (get_wait_queue_thread( ptr )->process != process) continue;
268 if (get_wait_queue_select_op( ptr ) != matching_op( select_op )) continue;
269 if (get_wait_queue_key( ptr ) != get_wait_queue_key( entry )) continue;
270 if (wake_thread_queue_entry( ptr )) return 1;
272 return 0;
275 static unsigned int keyed_event_map_access( struct object *obj, unsigned int access )
277 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | KEYEDEVENT_WAIT;
278 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | KEYEDEVENT_WAKE;
279 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
280 if (access & GENERIC_ALL) access |= KEYEDEVENT_ALL_ACCESS;
281 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
284 /* create an event */
285 DECL_HANDLER(create_event)
287 struct event *event;
288 struct unicode_str name;
289 struct directory *root = NULL;
290 const struct object_attributes *objattr = get_req_data();
291 const struct security_descriptor *sd;
293 reply->handle = 0;
295 if (!objattr_is_valid( objattr, get_req_data_size() ))
296 return;
298 sd = objattr->sd_len ? (const struct security_descriptor *)(objattr + 1) : NULL;
299 objattr_get_name( objattr, &name );
301 if (objattr->rootdir && !(root = get_directory_obj( current->process, objattr->rootdir, 0 )))
302 return;
304 if ((event = create_event( root, &name, req->attributes, req->manual_reset, req->initial_state, sd )))
306 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
307 reply->handle = alloc_handle( current->process, event, req->access, req->attributes );
308 else
309 reply->handle = alloc_handle_no_access_check( current->process, event, req->access, req->attributes );
310 release_object( event );
313 if (root) release_object( root );
316 /* open a handle to an event */
317 DECL_HANDLER(open_event)
319 struct unicode_str name;
320 struct directory *root = NULL;
321 struct event *event;
323 get_req_unicode_str( &name );
324 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
325 return;
327 if ((event = open_object_dir( root, &name, req->attributes, &event_ops )))
329 reply->handle = alloc_handle( current->process, &event->obj, req->access, req->attributes );
330 release_object( event );
333 if (root) release_object( root );
336 /* do an event operation */
337 DECL_HANDLER(event_op)
339 struct event *event;
341 if (!(event = get_event_obj( current->process, req->handle, EVENT_MODIFY_STATE ))) return;
342 switch(req->op)
344 case PULSE_EVENT:
345 pulse_event( event );
346 break;
347 case SET_EVENT:
348 set_event( event );
349 break;
350 case RESET_EVENT:
351 reset_event( event );
352 break;
353 default:
354 set_error( STATUS_INVALID_PARAMETER );
355 break;
357 release_object( event );
360 /* return details about the event */
361 DECL_HANDLER(query_event)
363 struct event *event;
365 if (!(event = get_event_obj( current->process, req->handle, EVENT_QUERY_STATE ))) return;
367 reply->manual_reset = event->manual_reset;
368 reply->state = event->signaled;
370 release_object( event );
373 /* create a keyed event */
374 DECL_HANDLER(create_keyed_event)
376 struct keyed_event *event;
377 struct unicode_str name;
378 struct directory *root = NULL;
379 const struct object_attributes *objattr = get_req_data();
380 const struct security_descriptor *sd;
382 if (!objattr_is_valid( objattr, get_req_data_size() )) return;
384 sd = objattr->sd_len ? (const struct security_descriptor *)(objattr + 1) : NULL;
385 objattr_get_name( objattr, &name );
387 if (objattr->rootdir && !(root = get_directory_obj( current->process, objattr->rootdir, 0 ))) return;
389 if ((event = create_keyed_event( root, &name, req->attributes, sd )))
391 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
392 reply->handle = alloc_handle( current->process, event, req->access, req->attributes );
393 else
394 reply->handle = alloc_handle_no_access_check( current->process, event, req->access, req->attributes );
395 release_object( event );
397 if (root) release_object( root );
400 /* open a handle to a keyed event */
401 DECL_HANDLER(open_keyed_event)
403 struct unicode_str name;
404 struct directory *root = NULL;
405 struct keyed_event *event;
407 get_req_unicode_str( &name );
408 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 ))) return;
410 if ((event = open_object_dir( root, &name, req->attributes, &keyed_event_ops )))
412 reply->handle = alloc_handle( current->process, &event->obj, req->access, req->attributes );
413 release_object( event );
415 if (root) release_object( root );