oledb32/tests: Simplify recount check.
[wine.git] / server / event.c
blobcfc0f6afc0de327cfabd5fbe178facef8c9783eb
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 directory_link_name, /* link_name */
69 default_unlink_name, /* unlink_name */
70 no_open_file, /* open_file */
71 no_close_handle, /* close_handle */
72 no_destroy /* destroy */
76 struct keyed_event
78 struct object obj; /* object header */
81 static void keyed_event_dump( struct object *obj, int verbose );
82 static struct object_type *keyed_event_get_type( struct object *obj );
83 static int keyed_event_signaled( struct object *obj, struct wait_queue_entry *entry );
84 static unsigned int keyed_event_map_access( struct object *obj, unsigned int access );
86 static const struct object_ops keyed_event_ops =
88 sizeof(struct keyed_event), /* size */
89 keyed_event_dump, /* dump */
90 keyed_event_get_type, /* get_type */
91 add_queue, /* add_queue */
92 remove_queue, /* remove_queue */
93 keyed_event_signaled, /* signaled */
94 no_satisfied, /* satisfied */
95 no_signal, /* signal */
96 no_get_fd, /* get_fd */
97 keyed_event_map_access, /* map_access */
98 default_get_sd, /* get_sd */
99 default_set_sd, /* set_sd */
100 no_lookup_name, /* lookup_name */
101 directory_link_name, /* link_name */
102 default_unlink_name, /* unlink_name */
103 no_open_file, /* open_file */
104 no_close_handle, /* close_handle */
105 no_destroy /* destroy */
109 struct event *create_event( struct object *root, const struct unicode_str *name,
110 unsigned int attr, int manual_reset, int initial_state,
111 const struct security_descriptor *sd )
113 struct event *event;
115 if ((event = create_named_object( root, &event_ops, name, attr, sd )))
117 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
119 /* initialize it if it didn't already exist */
120 event->manual_reset = manual_reset;
121 event->signaled = initial_state;
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\n",
157 event->manual_reset, event->signaled );
160 static struct object_type *event_get_type( struct object *obj )
162 static const WCHAR name[] = {'E','v','e','n','t'};
163 static const struct unicode_str str = { name, sizeof(name) };
164 return get_object_type( &str );
167 static int event_signaled( struct object *obj, struct wait_queue_entry *entry )
169 struct event *event = (struct event *)obj;
170 assert( obj->ops == &event_ops );
171 return event->signaled;
174 static void event_satisfied( struct object *obj, struct wait_queue_entry *entry )
176 struct event *event = (struct event *)obj;
177 assert( obj->ops == &event_ops );
178 /* Reset if it's an auto-reset event */
179 if (!event->manual_reset) event->signaled = 0;
182 static unsigned int event_map_access( struct object *obj, unsigned int access )
184 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | EVENT_QUERY_STATE;
185 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | EVENT_MODIFY_STATE;
186 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE;
187 if (access & GENERIC_ALL) access |= STANDARD_RIGHTS_ALL | EVENT_QUERY_STATE | EVENT_MODIFY_STATE;
188 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
191 static int event_signal( struct object *obj, unsigned int access )
193 struct event *event = (struct event *)obj;
194 assert( obj->ops == &event_ops );
196 if (!(access & EVENT_MODIFY_STATE))
198 set_error( STATUS_ACCESS_DENIED );
199 return 0;
201 set_event( event );
202 return 1;
205 struct keyed_event *create_keyed_event( struct object *root, const struct unicode_str *name,
206 unsigned int attr, const struct security_descriptor *sd )
208 struct keyed_event *event;
210 if ((event = create_named_object( root, &keyed_event_ops, name, attr, sd )))
212 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
214 /* initialize it if it didn't already exist */
217 return event;
220 struct keyed_event *get_keyed_event_obj( struct process *process, obj_handle_t handle, unsigned int access )
222 return (struct keyed_event *)get_handle_obj( process, handle, access, &keyed_event_ops );
225 static void keyed_event_dump( struct object *obj, int verbose )
227 fputs( "Keyed event\n", stderr );
230 static struct object_type *keyed_event_get_type( struct object *obj )
232 static const WCHAR name[] = {'K','e','y','e','d','E','v','e','n','t'};
233 static const struct unicode_str str = { name, sizeof(name) };
234 return get_object_type( &str );
237 static enum select_op matching_op( enum select_op op )
239 return op ^ (SELECT_KEYED_EVENT_WAIT ^ SELECT_KEYED_EVENT_RELEASE);
242 static int keyed_event_signaled( struct object *obj, struct wait_queue_entry *entry )
244 struct wait_queue_entry *ptr;
245 struct process *process;
246 enum select_op select_op;
248 assert( obj->ops == &keyed_event_ops );
250 process = get_wait_queue_thread( entry )->process;
251 select_op = get_wait_queue_select_op( entry );
252 if (select_op != SELECT_KEYED_EVENT_WAIT && select_op != SELECT_KEYED_EVENT_RELEASE) return 1;
254 LIST_FOR_EACH_ENTRY( ptr, &obj->wait_queue, struct wait_queue_entry, entry )
256 if (ptr == entry) continue;
257 if (get_wait_queue_thread( ptr )->process != process) continue;
258 if (get_wait_queue_select_op( ptr ) != matching_op( select_op )) continue;
259 if (get_wait_queue_key( ptr ) != get_wait_queue_key( entry )) continue;
260 if (wake_thread_queue_entry( ptr )) return 1;
262 return 0;
265 static unsigned int keyed_event_map_access( struct object *obj, unsigned int access )
267 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | KEYEDEVENT_WAIT;
268 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | KEYEDEVENT_WAKE;
269 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
270 if (access & GENERIC_ALL) access |= KEYEDEVENT_ALL_ACCESS;
271 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
274 /* create an event */
275 DECL_HANDLER(create_event)
277 struct event *event;
278 struct unicode_str name;
279 struct object *root;
280 const struct security_descriptor *sd;
281 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
283 if (!objattr) return;
285 if ((event = create_event( root, &name, objattr->attributes,
286 req->manual_reset, req->initial_state, sd )))
288 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
289 reply->handle = alloc_handle( current->process, event, req->access, objattr->attributes );
290 else
291 reply->handle = alloc_handle_no_access_check( current->process, event,
292 req->access, objattr->attributes );
293 release_object( event );
296 if (root) release_object( root );
299 /* open a handle to an event */
300 DECL_HANDLER(open_event)
302 struct unicode_str name = get_req_unicode_str();
304 reply->handle = open_object( current->process, req->rootdir, req->access,
305 &event_ops, &name, req->attributes );
308 /* do an event operation */
309 DECL_HANDLER(event_op)
311 struct event *event;
313 if (!(event = get_event_obj( current->process, req->handle, EVENT_MODIFY_STATE ))) return;
314 switch(req->op)
316 case PULSE_EVENT:
317 pulse_event( event );
318 break;
319 case SET_EVENT:
320 set_event( event );
321 break;
322 case RESET_EVENT:
323 reset_event( event );
324 break;
325 default:
326 set_error( STATUS_INVALID_PARAMETER );
327 break;
329 release_object( event );
332 /* return details about the event */
333 DECL_HANDLER(query_event)
335 struct event *event;
337 if (!(event = get_event_obj( current->process, req->handle, EVENT_QUERY_STATE ))) return;
339 reply->manual_reset = event->manual_reset;
340 reply->state = event->signaled;
342 release_object( event );
345 /* create a keyed event */
346 DECL_HANDLER(create_keyed_event)
348 struct keyed_event *event;
349 struct unicode_str name;
350 struct object *root;
351 const struct security_descriptor *sd;
352 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
354 if (!objattr) return;
356 if ((event = create_keyed_event( root, &name, objattr->attributes, sd )))
358 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
359 reply->handle = alloc_handle( current->process, event, req->access, objattr->attributes );
360 else
361 reply->handle = alloc_handle_no_access_check( current->process, event,
362 req->access, objattr->attributes );
363 release_object( event );
365 if (root) release_object( root );
368 /* open a handle to a keyed event */
369 DECL_HANDLER(open_keyed_event)
371 struct unicode_str name = get_req_unicode_str();
373 reply->handle = open_object( current->process, req->rootdir, req->access,
374 &keyed_event_ops, &name, req->attributes );