server: Copy completion information for accepted socket into the new file descriptor.
[wine.git] / server / event.c
blob0daa5b26355ae0dff70a3cb1b3f6d3c319501f0d
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 obj_handle_t alloc_wait_event( struct process *process )
129 obj_handle_t handle = 0;
130 struct event *event = create_event( NULL, NULL, 0, 1, 0, NULL );
132 if (event)
134 handle = alloc_handle( process, event, EVENT_ALL_ACCESS, 0 );
135 release_object( event );
137 return handle;
140 struct event *get_event_obj( struct process *process, obj_handle_t handle, unsigned int access )
142 return (struct event *)get_handle_obj( process, handle, access, &event_ops );
145 void pulse_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 );
150 event->signaled = 0;
153 void set_event( struct event *event )
155 event->signaled = 1;
156 /* wake up all waiters if manual reset, a single one otherwise */
157 wake_up( &event->obj, !event->manual_reset );
160 void reset_event( struct event *event )
162 event->signaled = 0;
165 static void event_dump( struct object *obj, int verbose )
167 struct event *event = (struct event *)obj;
168 assert( obj->ops == &event_ops );
169 fprintf( stderr, "Event manual=%d signaled=%d ",
170 event->manual_reset, event->signaled );
171 dump_object_name( &event->obj );
172 fputc( '\n', stderr );
175 static struct object_type *event_get_type( struct object *obj )
177 static const WCHAR name[] = {'E','v','e','n','t'};
178 static const struct unicode_str str = { name, sizeof(name) };
179 return get_object_type( &str );
182 static int event_signaled( struct object *obj, struct wait_queue_entry *entry )
184 struct event *event = (struct event *)obj;
185 assert( obj->ops == &event_ops );
186 return event->signaled;
189 static void event_satisfied( struct object *obj, struct wait_queue_entry *entry )
191 struct event *event = (struct event *)obj;
192 assert( obj->ops == &event_ops );
193 /* Reset if it's an auto-reset event */
194 if (!event->manual_reset) event->signaled = 0;
197 static unsigned int event_map_access( struct object *obj, unsigned int access )
199 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | EVENT_QUERY_STATE;
200 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | EVENT_MODIFY_STATE;
201 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE;
202 if (access & GENERIC_ALL) access |= STANDARD_RIGHTS_ALL | EVENT_QUERY_STATE | EVENT_MODIFY_STATE;
203 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
206 static int event_signal( struct object *obj, unsigned int access )
208 struct event *event = (struct event *)obj;
209 assert( obj->ops == &event_ops );
211 if (!(access & EVENT_MODIFY_STATE))
213 set_error( STATUS_ACCESS_DENIED );
214 return 0;
216 set_event( event );
217 return 1;
220 struct keyed_event *create_keyed_event( struct directory *root, const struct unicode_str *name,
221 unsigned int attr, const struct security_descriptor *sd )
223 struct keyed_event *event;
225 if ((event = create_named_object_dir( root, name, attr, &keyed_event_ops )))
227 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
229 /* initialize it if it didn't already exist */
230 if (sd) default_set_sd( &event->obj, sd, OWNER_SECURITY_INFORMATION|
231 GROUP_SECURITY_INFORMATION|
232 DACL_SECURITY_INFORMATION|
233 SACL_SECURITY_INFORMATION );
236 return event;
239 struct keyed_event *get_keyed_event_obj( struct process *process, obj_handle_t handle, unsigned int access )
241 return (struct keyed_event *)get_handle_obj( process, handle, access, &keyed_event_ops );
244 static void keyed_event_dump( struct object *obj, int verbose )
246 struct keyed_event *event = (struct keyed_event *)obj;
247 assert( obj->ops == &keyed_event_ops );
248 fprintf( stderr, "Keyed event " );
249 dump_object_name( &event->obj );
250 fputc( '\n', stderr );
253 static struct object_type *keyed_event_get_type( struct object *obj )
255 static const WCHAR name[] = {'K','e','y','e','d','E','v','e','n','t'};
256 static const struct unicode_str str = { name, sizeof(name) };
257 return get_object_type( &str );
260 static enum select_op matching_op( enum select_op op )
262 return op ^ (SELECT_KEYED_EVENT_WAIT ^ SELECT_KEYED_EVENT_RELEASE);
265 static int keyed_event_signaled( struct object *obj, struct wait_queue_entry *entry )
267 struct wait_queue_entry *ptr;
268 struct process *process;
269 enum select_op select_op;
271 assert( obj->ops == &keyed_event_ops );
273 process = get_wait_queue_thread( entry )->process;
274 select_op = get_wait_queue_select_op( entry );
275 if (select_op != SELECT_KEYED_EVENT_WAIT && select_op != SELECT_KEYED_EVENT_RELEASE) return 1;
277 LIST_FOR_EACH_ENTRY( ptr, &obj->wait_queue, struct wait_queue_entry, entry )
279 if (ptr == entry) continue;
280 if (get_wait_queue_thread( ptr )->process != process) continue;
281 if (get_wait_queue_select_op( ptr ) != matching_op( select_op )) continue;
282 if (get_wait_queue_key( ptr ) != get_wait_queue_key( entry )) continue;
283 if (wake_thread_queue_entry( ptr )) return 1;
285 return 0;
288 static unsigned int keyed_event_map_access( struct object *obj, unsigned int access )
290 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | KEYEDEVENT_WAIT;
291 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | KEYEDEVENT_WAKE;
292 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
293 if (access & GENERIC_ALL) access |= KEYEDEVENT_ALL_ACCESS;
294 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
297 /* create an event */
298 DECL_HANDLER(create_event)
300 struct event *event;
301 struct unicode_str name;
302 struct directory *root = NULL;
303 const struct object_attributes *objattr = get_req_data();
304 const struct security_descriptor *sd;
306 reply->handle = 0;
308 if (!objattr_is_valid( objattr, get_req_data_size() ))
309 return;
311 sd = objattr->sd_len ? (const struct security_descriptor *)(objattr + 1) : NULL;
312 objattr_get_name( objattr, &name );
314 if (objattr->rootdir && !(root = get_directory_obj( current->process, objattr->rootdir, 0 )))
315 return;
317 if ((event = create_event( root, &name, req->attributes, req->manual_reset, req->initial_state, sd )))
319 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
320 reply->handle = alloc_handle( current->process, event, req->access, req->attributes );
321 else
322 reply->handle = alloc_handle_no_access_check( current->process, event, req->access, req->attributes );
323 release_object( event );
326 if (root) release_object( root );
329 /* open a handle to an event */
330 DECL_HANDLER(open_event)
332 struct unicode_str name;
333 struct directory *root = NULL;
334 struct event *event;
336 get_req_unicode_str( &name );
337 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
338 return;
340 if ((event = open_object_dir( root, &name, req->attributes, &event_ops )))
342 reply->handle = alloc_handle( current->process, &event->obj, req->access, req->attributes );
343 release_object( event );
346 if (root) release_object( root );
349 /* do an event operation */
350 DECL_HANDLER(event_op)
352 struct event *event;
354 if (!(event = get_event_obj( current->process, req->handle, EVENT_MODIFY_STATE ))) return;
355 switch(req->op)
357 case PULSE_EVENT:
358 pulse_event( event );
359 break;
360 case SET_EVENT:
361 set_event( event );
362 break;
363 case RESET_EVENT:
364 reset_event( event );
365 break;
366 default:
367 set_error( STATUS_INVALID_PARAMETER );
368 break;
370 release_object( event );
373 /* return details about the event */
374 DECL_HANDLER(query_event)
376 struct event *event;
378 if (!(event = get_event_obj( current->process, req->handle, EVENT_QUERY_STATE ))) return;
380 reply->manual_reset = event->manual_reset;
381 reply->state = event->signaled;
383 release_object( event );
386 /* create a keyed event */
387 DECL_HANDLER(create_keyed_event)
389 struct keyed_event *event;
390 struct unicode_str name;
391 struct directory *root = NULL;
392 const struct object_attributes *objattr = get_req_data();
393 const struct security_descriptor *sd;
395 if (!objattr_is_valid( objattr, get_req_data_size() )) return;
397 sd = objattr->sd_len ? (const struct security_descriptor *)(objattr + 1) : NULL;
398 objattr_get_name( objattr, &name );
400 if (objattr->rootdir && !(root = get_directory_obj( current->process, objattr->rootdir, 0 ))) return;
402 if ((event = create_keyed_event( root, &name, req->attributes, sd )))
404 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
405 reply->handle = alloc_handle( current->process, event, req->access, req->attributes );
406 else
407 reply->handle = alloc_handle_no_access_check( current->process, event, req->access, req->attributes );
408 release_object( event );
410 if (root) release_object( root );
413 /* open a handle to a keyed event */
414 DECL_HANDLER(open_keyed_event)
416 struct unicode_str name;
417 struct directory *root = NULL;
418 struct keyed_event *event;
420 get_req_unicode_str( &name );
421 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 ))) return;
423 if ((event = open_object_dir( root, &name, req->attributes, &keyed_event_ops )))
425 reply->handle = alloc_handle( current->process, &event->obj, req->access, req->attributes );
426 release_object( event );
428 if (root) release_object( root );