sapi/tests: Use wide-char string literals.
[wine.git] / server / event.c
blobcba9472aa3d635cb50581d80dc33117a791862cd
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 default_get_full_name, /* get_full_name */
70 no_lookup_name, /* lookup_name */
71 directory_link_name, /* link_name */
72 default_unlink_name, /* unlink_name */
73 no_open_file, /* open_file */
74 event_get_kernel_obj_list, /* get_kernel_obj_list */
75 no_close_handle, /* close_handle */
76 no_destroy /* destroy */
80 struct keyed_event
82 struct object obj; /* object header */
85 static void keyed_event_dump( struct object *obj, int verbose );
86 static struct object_type *keyed_event_get_type( struct object *obj );
87 static int keyed_event_signaled( struct object *obj, struct wait_queue_entry *entry );
88 static unsigned int keyed_event_map_access( struct object *obj, unsigned int access );
90 static const struct object_ops keyed_event_ops =
92 sizeof(struct keyed_event), /* size */
93 keyed_event_dump, /* dump */
94 keyed_event_get_type, /* get_type */
95 add_queue, /* add_queue */
96 remove_queue, /* remove_queue */
97 keyed_event_signaled, /* signaled */
98 no_satisfied, /* satisfied */
99 no_signal, /* signal */
100 no_get_fd, /* get_fd */
101 keyed_event_map_access, /* map_access */
102 default_get_sd, /* get_sd */
103 default_set_sd, /* set_sd */
104 default_get_full_name, /* get_full_name */
105 no_lookup_name, /* lookup_name */
106 directory_link_name, /* link_name */
107 default_unlink_name, /* unlink_name */
108 no_open_file, /* open_file */
109 no_kernel_obj_list, /* get_kernel_obj_list */
110 no_close_handle, /* close_handle */
111 no_destroy /* destroy */
115 struct event *create_event( struct object *root, const struct unicode_str *name,
116 unsigned int attr, int manual_reset, int initial_state,
117 const struct security_descriptor *sd )
119 struct event *event;
121 if ((event = create_named_object( root, &event_ops, name, attr, sd )))
123 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
125 /* initialize it if it didn't already exist */
126 list_init( &event->kernel_object );
127 event->manual_reset = manual_reset;
128 event->signaled = initial_state;
131 return event;
134 struct event *get_event_obj( struct process *process, obj_handle_t handle, unsigned int access )
136 return (struct event *)get_handle_obj( process, handle, access, &event_ops );
139 void pulse_event( struct event *event )
141 event->signaled = 1;
142 /* wake up all waiters if manual reset, a single one otherwise */
143 wake_up( &event->obj, !event->manual_reset );
144 event->signaled = 0;
147 void set_event( struct event *event )
149 event->signaled = 1;
150 /* wake up all waiters if manual reset, a single one otherwise */
151 wake_up( &event->obj, !event->manual_reset );
154 void reset_event( struct event *event )
156 event->signaled = 0;
159 static void event_dump( struct object *obj, int verbose )
161 struct event *event = (struct event *)obj;
162 assert( obj->ops == &event_ops );
163 fprintf( stderr, "Event manual=%d signaled=%d\n",
164 event->manual_reset, event->signaled );
167 static struct object_type *event_get_type( struct object *obj )
169 static const WCHAR name[] = {'E','v','e','n','t'};
170 static const struct unicode_str str = { name, sizeof(name) };
171 return get_object_type( &str );
174 static int event_signaled( struct object *obj, struct wait_queue_entry *entry )
176 struct event *event = (struct event *)obj;
177 assert( obj->ops == &event_ops );
178 return event->signaled;
181 static void event_satisfied( struct object *obj, struct wait_queue_entry *entry )
183 struct event *event = (struct event *)obj;
184 assert( obj->ops == &event_ops );
185 /* Reset if it's an auto-reset event */
186 if (!event->manual_reset) event->signaled = 0;
189 static unsigned int event_map_access( struct object *obj, unsigned int access )
191 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | EVENT_QUERY_STATE;
192 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | EVENT_MODIFY_STATE;
193 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE;
194 if (access & GENERIC_ALL) access |= STANDARD_RIGHTS_ALL | EVENT_QUERY_STATE | EVENT_MODIFY_STATE;
195 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
198 static int event_signal( struct object *obj, unsigned int access )
200 struct event *event = (struct event *)obj;
201 assert( obj->ops == &event_ops );
203 if (!(access & EVENT_MODIFY_STATE))
205 set_error( STATUS_ACCESS_DENIED );
206 return 0;
208 set_event( event );
209 return 1;
212 static struct list *event_get_kernel_obj_list( struct object *obj )
214 struct event *event = (struct event *)obj;
215 return &event->kernel_object;
218 struct keyed_event *create_keyed_event( struct object *root, const struct unicode_str *name,
219 unsigned int attr, const struct security_descriptor *sd )
221 struct keyed_event *event;
223 if ((event = create_named_object( root, &keyed_event_ops, name, attr, sd )))
225 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
227 /* initialize it if it didn't already exist */
230 return event;
233 struct keyed_event *get_keyed_event_obj( struct process *process, obj_handle_t handle, unsigned int access )
235 return (struct keyed_event *)get_handle_obj( process, handle, access, &keyed_event_ops );
238 static void keyed_event_dump( struct object *obj, int verbose )
240 fputs( "Keyed event\n", stderr );
243 static struct object_type *keyed_event_get_type( struct object *obj )
245 static const WCHAR name[] = {'K','e','y','e','d','E','v','e','n','t'};
246 static const struct unicode_str str = { name, sizeof(name) };
247 return get_object_type( &str );
250 static enum select_op matching_op( enum select_op op )
252 return op ^ (SELECT_KEYED_EVENT_WAIT ^ SELECT_KEYED_EVENT_RELEASE);
255 static int keyed_event_signaled( struct object *obj, struct wait_queue_entry *entry )
257 struct wait_queue_entry *ptr;
258 struct process *process;
259 enum select_op select_op;
261 assert( obj->ops == &keyed_event_ops );
263 process = get_wait_queue_thread( entry )->process;
264 select_op = get_wait_queue_select_op( entry );
265 if (select_op != SELECT_KEYED_EVENT_WAIT && select_op != SELECT_KEYED_EVENT_RELEASE) return 1;
267 LIST_FOR_EACH_ENTRY( ptr, &obj->wait_queue, struct wait_queue_entry, entry )
269 if (ptr == entry) continue;
270 if (get_wait_queue_thread( ptr )->process != process) continue;
271 if (get_wait_queue_select_op( ptr ) != matching_op( select_op )) continue;
272 if (get_wait_queue_key( ptr ) != get_wait_queue_key( entry )) continue;
273 if (wake_thread_queue_entry( ptr )) return 1;
275 return 0;
278 static unsigned int keyed_event_map_access( struct object *obj, unsigned int access )
280 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | KEYEDEVENT_WAIT;
281 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | KEYEDEVENT_WAKE;
282 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
283 if (access & GENERIC_ALL) access |= KEYEDEVENT_ALL_ACCESS;
284 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
287 /* create an event */
288 DECL_HANDLER(create_event)
290 struct event *event;
291 struct unicode_str name;
292 struct object *root;
293 const struct security_descriptor *sd;
294 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
296 if (!objattr) return;
298 if ((event = create_event( root, &name, objattr->attributes,
299 req->manual_reset, req->initial_state, sd )))
301 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
302 reply->handle = alloc_handle( current->process, event, req->access, objattr->attributes );
303 else
304 reply->handle = alloc_handle_no_access_check( current->process, event,
305 req->access, objattr->attributes );
306 release_object( event );
309 if (root) release_object( root );
312 /* open a handle to an event */
313 DECL_HANDLER(open_event)
315 struct unicode_str name = get_req_unicode_str();
317 reply->handle = open_object( current->process, req->rootdir, req->access,
318 &event_ops, &name, req->attributes );
321 /* do an event operation */
322 DECL_HANDLER(event_op)
324 struct event *event;
326 if (!(event = get_event_obj( current->process, req->handle, EVENT_MODIFY_STATE ))) return;
327 reply->state = event->signaled;
328 switch(req->op)
330 case PULSE_EVENT:
331 pulse_event( event );
332 break;
333 case SET_EVENT:
334 set_event( event );
335 break;
336 case RESET_EVENT:
337 reset_event( event );
338 break;
339 default:
340 set_error( STATUS_INVALID_PARAMETER );
341 break;
343 release_object( event );
346 /* return details about the event */
347 DECL_HANDLER(query_event)
349 struct event *event;
351 if (!(event = get_event_obj( current->process, req->handle, EVENT_QUERY_STATE ))) return;
353 reply->manual_reset = event->manual_reset;
354 reply->state = event->signaled;
356 release_object( event );
359 /* create a keyed event */
360 DECL_HANDLER(create_keyed_event)
362 struct keyed_event *event;
363 struct unicode_str name;
364 struct object *root;
365 const struct security_descriptor *sd;
366 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
368 if (!objattr) return;
370 if ((event = create_keyed_event( root, &name, objattr->attributes, sd )))
372 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
373 reply->handle = alloc_handle( current->process, event, req->access, objattr->attributes );
374 else
375 reply->handle = alloc_handle_no_access_check( current->process, event,
376 req->access, objattr->attributes );
377 release_object( event );
379 if (root) release_object( root );
382 /* open a handle to a keyed event */
383 DECL_HANDLER(open_keyed_event)
385 struct unicode_str name = get_req_unicode_str();
387 reply->handle = open_object( current->process, req->rootdir, req->access,
388 &keyed_event_ops, &name, req->attributes );