blob: 7a7331de321025581efafb7a1622788e61cfb924 [file] [log] [blame]
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001/*
2 * Server-side event management
3 *
4 * Copyright (C) 1998 Alexandre Julliard
Alexandre Julliard0799c1a2002-03-09 23:29:33 +00005 *
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.
10 *
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.
15 *
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
Jonathan Ernst360a3f92006-05-18 14:49:52 +020018 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000019 */
20
Alexandre Julliard5769d1d2002-04-26 19:05:15 +000021#include "config.h"
22#include "wine/port.h"
23
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000024#include <assert.h>
25#include <stdio.h>
26#include <stdlib.h>
Robert Lunnon95414ef2005-11-22 12:01:05 +000027#include <stdarg.h>
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000028
Ge van Geldorp1a1583a2005-11-28 17:32:54 +010029#include "ntstatus.h"
30#define WIN32_NO_STATUS
Alexandre Julliard435e2e62002-12-10 22:56:43 +000031#include "windef.h"
Vitaliy Margolena9960002005-10-27 18:30:37 +000032#include "winternl.h"
Alexandre Julliard43c190e1999-05-15 10:48:19 +000033
34#include "handle.h"
35#include "thread.h"
Alexandre Julliard5bc78081999-06-22 17:26:53 +000036#include "request.h"
Rob Shearmandd9e3922007-10-24 16:04:42 +010037#include "security.h"
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000038
39struct event
40{
41 struct object obj; /* object header */
42 int manual_reset; /* is it a manual reset event? */
43 int signaled; /* event has been signaled */
44};
45
Alexandre Julliard338e7571998-12-27 15:28:54 +000046static void event_dump( struct object *obj, int verbose );
Alexandre Julliard8382eb02007-12-05 18:16:42 +010047static struct object_type *event_get_type( struct object *obj );
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000048static int event_signaled( struct object *obj, struct thread *thread );
49static int event_satisfied( struct object *obj, struct thread *thread );
Alexandre Julliard03f46e12005-12-12 14:58:44 +010050static unsigned int event_map_access( struct object *obj, unsigned int access );
Mike McCormackf92fff62005-04-24 17:35:52 +000051static int event_signal( struct object *obj, unsigned int access);
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000052
53static const struct object_ops event_ops =
54{
Alexandre Julliard1dca5e22000-01-01 00:56:27 +000055 sizeof(struct event), /* size */
56 event_dump, /* dump */
Alexandre Julliard8382eb02007-12-05 18:16:42 +010057 event_get_type, /* get_type */
Alexandre Julliard1dca5e22000-01-01 00:56:27 +000058 add_queue, /* add_queue */
59 remove_queue, /* remove_queue */
60 event_signaled, /* signaled */
61 event_satisfied, /* satisfied */
Mike McCormackf92fff62005-04-24 17:35:52 +000062 event_signal, /* signal */
Alexandre Julliard1ab243b2000-12-19 02:12:45 +000063 no_get_fd, /* get_fd */
Alexandre Julliard03f46e12005-12-12 14:58:44 +010064 event_map_access, /* map_access */
Rob Shearmanc1707d82007-10-03 13:10:37 +010065 default_get_sd, /* get_sd */
66 default_set_sd, /* set_sd */
Vitaliy Margolenbaffcb92005-11-22 14:55:42 +000067 no_lookup_name, /* lookup_name */
Alexandre Julliard7e71c1d2007-03-22 11:44:29 +010068 no_open_file, /* open_file */
Alexandre Julliardb9b1ea92005-06-09 15:39:52 +000069 no_close_handle, /* close_handle */
Alexandre Julliard1dca5e22000-01-01 00:56:27 +000070 no_destroy /* destroy */
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000071};
72
73
Vitaliy Margolenf676bc82005-12-02 15:55:48 +010074struct event *create_event( struct directory *root, const struct unicode_str *name,
Rob Shearmandd9e3922007-10-24 16:04:42 +010075 unsigned int attr, int manual_reset, int initial_state,
76 const struct security_descriptor *sd )
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000077{
78 struct event *event;
79
Vitaliy Margolenf676bc82005-12-02 15:55:48 +010080 if ((event = create_named_object_dir( root, name, attr, &event_ops )))
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000081 {
Vitaliy Margolen893987b2005-11-21 16:27:03 +000082 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
Alexandre Julliard5bc78081999-06-22 17:26:53 +000083 {
84 /* initialize it if it didn't already exist */
85 event->manual_reset = manual_reset;
86 event->signaled = initial_state;
Rob Shearmandd9e3922007-10-24 16:04:42 +010087 if (sd) default_set_sd( &event->obj, sd, OWNER_SECURITY_INFORMATION|
88 GROUP_SECURITY_INFORMATION|
89 DACL_SECURITY_INFORMATION|
90 SACL_SECURITY_INFORMATION );
Alexandre Julliard5bc78081999-06-22 17:26:53 +000091 }
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000092 }
Alexandre Julliard5bc78081999-06-22 17:26:53 +000093 return event;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000094}
95
Alexandre Julliard51885742002-05-30 20:12:58 +000096struct event *get_event_obj( struct process *process, obj_handle_t handle, unsigned int access )
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000097{
Alexandre Julliard61ec6c11999-11-29 02:17:08 +000098 return (struct event *)get_handle_obj( process, handle, access, &event_ops );
Alexandre Julliardd6d994f1999-09-28 16:40:07 +000099}
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000100
Alexandre Julliardd6d994f1999-09-28 16:40:07 +0000101void pulse_event( struct event *event )
102{
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000103 event->signaled = 1;
104 /* wake up all waiters if manual reset, a single one otherwise */
105 wake_up( &event->obj, !event->manual_reset );
106 event->signaled = 0;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000107}
108
Alexandre Julliardd6d994f1999-09-28 16:40:07 +0000109void set_event( struct event *event )
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000110{
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000111 event->signaled = 1;
112 /* wake up all waiters if manual reset, a single one otherwise */
113 wake_up( &event->obj, !event->manual_reset );
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000114}
115
Alexandre Julliardd6d994f1999-09-28 16:40:07 +0000116void reset_event( struct event *event )
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000117{
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000118 event->signaled = 0;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000119}
120
Alexandre Julliard338e7571998-12-27 15:28:54 +0000121static void event_dump( struct object *obj, int verbose )
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000122{
123 struct event *event = (struct event *)obj;
124 assert( obj->ops == &event_ops );
Alexandre Julliardd16319c1999-11-25 21:30:24 +0000125 fprintf( stderr, "Event manual=%d signaled=%d ",
126 event->manual_reset, event->signaled );
127 dump_object_name( &event->obj );
128 fputc( '\n', stderr );
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000129}
130
Alexandre Julliard8382eb02007-12-05 18:16:42 +0100131static struct object_type *event_get_type( struct object *obj )
132{
133 static const WCHAR name[] = {'E','v','e','n','t'};
134 static const struct unicode_str str = { name, sizeof(name) };
135 return get_object_type( &str );
136}
137
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000138static int event_signaled( struct object *obj, struct thread *thread )
139{
140 struct event *event = (struct event *)obj;
141 assert( obj->ops == &event_ops );
142 return event->signaled;
143}
144
145static int event_satisfied( struct object *obj, struct thread *thread )
146{
147 struct event *event = (struct event *)obj;
148 assert( obj->ops == &event_ops );
149 /* Reset if it's an auto-reset event */
150 if (!event->manual_reset) event->signaled = 0;
151 return 0; /* Not abandoned */
152}
153
Alexandre Julliard03f46e12005-12-12 14:58:44 +0100154static unsigned int event_map_access( struct object *obj, unsigned int access )
155{
156 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | SYNCHRONIZE | EVENT_QUERY_STATE;
157 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE;
158 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
159 if (access & GENERIC_ALL) access |= STANDARD_RIGHTS_ALL | EVENT_ALL_ACCESS;
160 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
161}
162
Mike McCormackf92fff62005-04-24 17:35:52 +0000163static int event_signal( struct object *obj, unsigned int access )
164{
165 struct event *event = (struct event *)obj;
166 assert( obj->ops == &event_ops );
167
168 if (!(access & EVENT_MODIFY_STATE))
169 {
170 set_error( STATUS_ACCESS_DENIED );
171 return 0;
172 }
173 set_event( event );
174 return 1;
175}
176
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000177/* create an event */
178DECL_HANDLER(create_event)
179{
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000180 struct event *event;
Alexandre Julliardead9b062005-11-18 16:31:18 +0000181 struct unicode_str name;
Vitaliy Margolenf676bc82005-12-02 15:55:48 +0100182 struct directory *root = NULL;
Rob Shearmandd9e3922007-10-24 16:04:42 +0100183 const struct object_attributes *objattr = get_req_data();
184 const struct security_descriptor *sd;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000185
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000186 reply->handle = 0;
Rob Shearmandd9e3922007-10-24 16:04:42 +0100187
188 if (!objattr_is_valid( objattr, get_req_data_size() ))
Vitaliy Margolenf676bc82005-12-02 15:55:48 +0100189 return;
190
Rob Shearmandd9e3922007-10-24 16:04:42 +0100191 sd = objattr->sd_len ? (const struct security_descriptor *)(objattr + 1) : NULL;
Rob Shearmanf98556c2007-10-26 17:01:33 +0100192 objattr_get_name( objattr, &name );
Rob Shearmandd9e3922007-10-24 16:04:42 +0100193
194 if (objattr->rootdir && !(root = get_directory_obj( current->process, objattr->rootdir, 0 )))
195 return;
196
197 if ((event = create_event( root, &name, req->attributes, req->manual_reset, req->initial_state, sd )))
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000198 {
Rob Shearman92db6d22007-11-05 14:23:36 +0000199 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
200 reply->handle = alloc_handle( current->process, event, req->access, req->attributes );
201 else
202 reply->handle = alloc_handle_no_access_check( current->process, event, req->access, req->attributes );
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000203 release_object( event );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000204 }
Vitaliy Margolenf676bc82005-12-02 15:55:48 +0100205
206 if (root) release_object( root );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000207}
208
209/* open a handle to an event */
210DECL_HANDLER(open_event)
211{
Alexandre Julliardead9b062005-11-18 16:31:18 +0000212 struct unicode_str name;
Vitaliy Margolenf676bc82005-12-02 15:55:48 +0100213 struct directory *root = NULL;
Alexandre Julliard3764da62005-12-05 12:52:05 +0100214 struct event *event;
Alexandre Julliardead9b062005-11-18 16:31:18 +0000215
216 get_req_unicode_str( &name );
Vitaliy Margolenf676bc82005-12-02 15:55:48 +0100217 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
218 return;
219
Alexandre Julliard3764da62005-12-05 12:52:05 +0100220 if ((event = open_object_dir( root, &name, req->attributes, &event_ops )))
221 {
Alexandre Julliard24560e72005-12-09 13:58:25 +0100222 reply->handle = alloc_handle( current->process, &event->obj, req->access, req->attributes );
Alexandre Julliard3764da62005-12-05 12:52:05 +0100223 release_object( event );
224 }
Vitaliy Margolenf676bc82005-12-02 15:55:48 +0100225
226 if (root) release_object( root );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000227}
228
229/* do an event operation */
230DECL_HANDLER(event_op)
231{
Alexandre Julliardd6d994f1999-09-28 16:40:07 +0000232 struct event *event;
233
234 if (!(event = get_event_obj( current->process, req->handle, EVENT_MODIFY_STATE ))) return;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000235 switch(req->op)
236 {
237 case PULSE_EVENT:
Alexandre Julliardd6d994f1999-09-28 16:40:07 +0000238 pulse_event( event );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000239 break;
240 case SET_EVENT:
Alexandre Julliardd6d994f1999-09-28 16:40:07 +0000241 set_event( event );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000242 break;
243 case RESET_EVENT:
Alexandre Julliardd6d994f1999-09-28 16:40:07 +0000244 reset_event( event );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000245 break;
246 default:
Alexandre Julliard55586522006-06-08 10:26:02 +0200247 set_error( STATUS_INVALID_PARAMETER );
248 break;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000249 }
Alexandre Julliardd6d994f1999-09-28 16:40:07 +0000250 release_object( event );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000251}