crypt32: Fix a leaked key.
[wine/wine64.git] / server / event.c
blob99d0f4b218243d0705e17ae1a1a1b3ee84beeee9
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 int event_signaled( struct object *obj, struct thread *thread );
48 static int event_satisfied( struct object *obj, struct thread *thread );
49 static unsigned int event_map_access( struct object *obj, unsigned int access );
50 static int event_signal( struct object *obj, unsigned int access);
52 static const struct object_ops event_ops =
54 sizeof(struct event), /* size */
55 event_dump, /* dump */
56 add_queue, /* add_queue */
57 remove_queue, /* remove_queue */
58 event_signaled, /* signaled */
59 event_satisfied, /* satisfied */
60 event_signal, /* signal */
61 no_get_fd, /* get_fd */
62 event_map_access, /* map_access */
63 default_get_sd, /* get_sd */
64 default_set_sd, /* set_sd */
65 no_lookup_name, /* lookup_name */
66 no_open_file, /* open_file */
67 no_close_handle, /* close_handle */
68 no_destroy /* destroy */
72 struct event *create_event( struct directory *root, const struct unicode_str *name,
73 unsigned int attr, int manual_reset, int initial_state,
74 const struct security_descriptor *sd )
76 struct event *event;
78 if ((event = create_named_object_dir( root, name, attr, &event_ops )))
80 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
82 /* initialize it if it didn't already exist */
83 event->manual_reset = manual_reset;
84 event->signaled = initial_state;
85 if (sd) default_set_sd( &event->obj, sd, OWNER_SECURITY_INFORMATION|
86 GROUP_SECURITY_INFORMATION|
87 DACL_SECURITY_INFORMATION|
88 SACL_SECURITY_INFORMATION );
91 return event;
94 struct event *get_event_obj( struct process *process, obj_handle_t handle, unsigned int access )
96 return (struct event *)get_handle_obj( process, handle, access, &event_ops );
99 void pulse_event( struct event *event )
101 event->signaled = 1;
102 /* wake up all waiters if manual reset, a single one otherwise */
103 wake_up( &event->obj, !event->manual_reset );
104 event->signaled = 0;
107 void set_event( struct event *event )
109 event->signaled = 1;
110 /* wake up all waiters if manual reset, a single one otherwise */
111 wake_up( &event->obj, !event->manual_reset );
114 void reset_event( struct event *event )
116 event->signaled = 0;
119 static void event_dump( struct object *obj, int verbose )
121 struct event *event = (struct event *)obj;
122 assert( obj->ops == &event_ops );
123 fprintf( stderr, "Event manual=%d signaled=%d ",
124 event->manual_reset, event->signaled );
125 dump_object_name( &event->obj );
126 fputc( '\n', stderr );
129 static int event_signaled( struct object *obj, struct thread *thread )
131 struct event *event = (struct event *)obj;
132 assert( obj->ops == &event_ops );
133 return event->signaled;
136 static int event_satisfied( struct object *obj, struct thread *thread )
138 struct event *event = (struct event *)obj;
139 assert( obj->ops == &event_ops );
140 /* Reset if it's an auto-reset event */
141 if (!event->manual_reset) event->signaled = 0;
142 return 0; /* Not abandoned */
145 static unsigned int event_map_access( struct object *obj, unsigned int access )
147 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | SYNCHRONIZE | EVENT_QUERY_STATE;
148 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE;
149 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
150 if (access & GENERIC_ALL) access |= STANDARD_RIGHTS_ALL | EVENT_ALL_ACCESS;
151 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
154 static int event_signal( struct object *obj, unsigned int access )
156 struct event *event = (struct event *)obj;
157 assert( obj->ops == &event_ops );
159 if (!(access & EVENT_MODIFY_STATE))
161 set_error( STATUS_ACCESS_DENIED );
162 return 0;
164 set_event( event );
165 return 1;
168 /* create an event */
169 DECL_HANDLER(create_event)
171 struct event *event;
172 struct unicode_str name;
173 struct directory *root = NULL;
174 const struct object_attributes *objattr = get_req_data();
175 const struct security_descriptor *sd;
177 reply->handle = 0;
179 if (!objattr_is_valid( objattr, get_req_data_size() ))
180 return;
182 sd = objattr->sd_len ? (const struct security_descriptor *)(objattr + 1) : NULL;
183 objattr_get_name( objattr, &name );
185 if (objattr->rootdir && !(root = get_directory_obj( current->process, objattr->rootdir, 0 )))
186 return;
188 if ((event = create_event( root, &name, req->attributes, req->manual_reset, req->initial_state, sd )))
190 reply->handle = alloc_handle( current->process, event, req->access, req->attributes );
191 release_object( event );
194 if (root) release_object( root );
197 /* open a handle to an event */
198 DECL_HANDLER(open_event)
200 struct unicode_str name;
201 struct directory *root = NULL;
202 struct event *event;
204 get_req_unicode_str( &name );
205 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
206 return;
208 if ((event = open_object_dir( root, &name, req->attributes, &event_ops )))
210 reply->handle = alloc_handle( current->process, &event->obj, req->access, req->attributes );
211 release_object( event );
214 if (root) release_object( root );
217 /* do an event operation */
218 DECL_HANDLER(event_op)
220 struct event *event;
222 if (!(event = get_event_obj( current->process, req->handle, EVENT_MODIFY_STATE ))) return;
223 switch(req->op)
225 case PULSE_EVENT:
226 pulse_event( event );
227 break;
228 case SET_EVENT:
229 set_event( event );
230 break;
231 case RESET_EVENT:
232 reset_event( event );
233 break;
234 default:
235 set_error( STATUS_INVALID_PARAMETER );
236 break;
238 release_object( event );