Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Server-side event management |
| 3 | * |
| 4 | * Copyright (C) 1998 Alexandre Julliard |
Alexandre Julliard | 0799c1a | 2002-03-09 23:29:33 +0000 | [diff] [blame] | 5 | * |
| 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 |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 19 | */ |
| 20 | |
Alexandre Julliard | 5769d1d | 2002-04-26 19:05:15 +0000 | [diff] [blame] | 21 | #include "config.h" |
| 22 | #include "wine/port.h" |
| 23 | |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 24 | #include <assert.h> |
| 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
Robert Lunnon | 95414ef | 2005-11-22 12:01:05 +0000 | [diff] [blame] | 27 | #include <stdarg.h> |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 28 | |
Ge van Geldorp | 1a1583a | 2005-11-28 17:32:54 +0100 | [diff] [blame] | 29 | #include "ntstatus.h" |
| 30 | #define WIN32_NO_STATUS |
Alexandre Julliard | 435e2e6 | 2002-12-10 22:56:43 +0000 | [diff] [blame] | 31 | #include "windef.h" |
Vitaliy Margolen | a996000 | 2005-10-27 18:30:37 +0000 | [diff] [blame] | 32 | #include "winternl.h" |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 33 | |
| 34 | #include "handle.h" |
| 35 | #include "thread.h" |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 36 | #include "request.h" |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 37 | |
| 38 | struct event |
| 39 | { |
| 40 | struct object obj; /* object header */ |
| 41 | int manual_reset; /* is it a manual reset event? */ |
| 42 | int signaled; /* event has been signaled */ |
| 43 | }; |
| 44 | |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 45 | static void event_dump( struct object *obj, int verbose ); |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 46 | static int event_signaled( struct object *obj, struct thread *thread ); |
| 47 | static int event_satisfied( struct object *obj, struct thread *thread ); |
Mike McCormack | f92fff6 | 2005-04-24 17:35:52 +0000 | [diff] [blame] | 48 | static int event_signal( struct object *obj, unsigned int access); |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 49 | |
| 50 | static const struct object_ops event_ops = |
| 51 | { |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 52 | sizeof(struct event), /* size */ |
| 53 | event_dump, /* dump */ |
| 54 | add_queue, /* add_queue */ |
| 55 | remove_queue, /* remove_queue */ |
| 56 | event_signaled, /* signaled */ |
| 57 | event_satisfied, /* satisfied */ |
Mike McCormack | f92fff6 | 2005-04-24 17:35:52 +0000 | [diff] [blame] | 58 | event_signal, /* signal */ |
Alexandre Julliard | 1ab243b | 2000-12-19 02:12:45 +0000 | [diff] [blame] | 59 | no_get_fd, /* get_fd */ |
Vitaliy Margolen | baffcb9 | 2005-11-22 14:55:42 +0000 | [diff] [blame] | 60 | no_lookup_name, /* lookup_name */ |
Alexandre Julliard | b9b1ea9 | 2005-06-09 15:39:52 +0000 | [diff] [blame] | 61 | no_close_handle, /* close_handle */ |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 62 | no_destroy /* destroy */ |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | |
Vitaliy Margolen | f676bc8 | 2005-12-02 15:55:48 +0100 | [diff] [blame^] | 66 | struct event *create_event( struct directory *root, const struct unicode_str *name, |
| 67 | unsigned int attr, int manual_reset, int initial_state ) |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 68 | { |
| 69 | struct event *event; |
| 70 | |
Vitaliy Margolen | f676bc8 | 2005-12-02 15:55:48 +0100 | [diff] [blame^] | 71 | if ((event = create_named_object_dir( root, name, attr, &event_ops ))) |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 72 | { |
Vitaliy Margolen | 893987b | 2005-11-21 16:27:03 +0000 | [diff] [blame] | 73 | if (get_error() != STATUS_OBJECT_NAME_EXISTS) |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 74 | { |
| 75 | /* initialize it if it didn't already exist */ |
| 76 | event->manual_reset = manual_reset; |
| 77 | event->signaled = initial_state; |
| 78 | } |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 79 | } |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 80 | return event; |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Alexandre Julliard | 5188574 | 2002-05-30 20:12:58 +0000 | [diff] [blame] | 83 | struct event *get_event_obj( struct process *process, obj_handle_t handle, unsigned int access ) |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 84 | { |
Alexandre Julliard | 61ec6c1 | 1999-11-29 02:17:08 +0000 | [diff] [blame] | 85 | return (struct event *)get_handle_obj( process, handle, access, &event_ops ); |
Alexandre Julliard | d6d994f | 1999-09-28 16:40:07 +0000 | [diff] [blame] | 86 | } |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 87 | |
Alexandre Julliard | d6d994f | 1999-09-28 16:40:07 +0000 | [diff] [blame] | 88 | void pulse_event( struct event *event ) |
| 89 | { |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 90 | event->signaled = 1; |
| 91 | /* wake up all waiters if manual reset, a single one otherwise */ |
| 92 | wake_up( &event->obj, !event->manual_reset ); |
| 93 | event->signaled = 0; |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Alexandre Julliard | d6d994f | 1999-09-28 16:40:07 +0000 | [diff] [blame] | 96 | void set_event( struct event *event ) |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 97 | { |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 98 | event->signaled = 1; |
| 99 | /* wake up all waiters if manual reset, a single one otherwise */ |
| 100 | wake_up( &event->obj, !event->manual_reset ); |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Alexandre Julliard | d6d994f | 1999-09-28 16:40:07 +0000 | [diff] [blame] | 103 | void reset_event( struct event *event ) |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 104 | { |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 105 | event->signaled = 0; |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 108 | static void event_dump( struct object *obj, int verbose ) |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 109 | { |
| 110 | struct event *event = (struct event *)obj; |
| 111 | assert( obj->ops == &event_ops ); |
Alexandre Julliard | d16319c | 1999-11-25 21:30:24 +0000 | [diff] [blame] | 112 | fprintf( stderr, "Event manual=%d signaled=%d ", |
| 113 | event->manual_reset, event->signaled ); |
| 114 | dump_object_name( &event->obj ); |
| 115 | fputc( '\n', stderr ); |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | static int event_signaled( struct object *obj, struct thread *thread ) |
| 119 | { |
| 120 | struct event *event = (struct event *)obj; |
| 121 | assert( obj->ops == &event_ops ); |
| 122 | return event->signaled; |
| 123 | } |
| 124 | |
| 125 | static int event_satisfied( struct object *obj, struct thread *thread ) |
| 126 | { |
| 127 | struct event *event = (struct event *)obj; |
| 128 | assert( obj->ops == &event_ops ); |
| 129 | /* Reset if it's an auto-reset event */ |
| 130 | if (!event->manual_reset) event->signaled = 0; |
| 131 | return 0; /* Not abandoned */ |
| 132 | } |
| 133 | |
Mike McCormack | f92fff6 | 2005-04-24 17:35:52 +0000 | [diff] [blame] | 134 | static int event_signal( struct object *obj, unsigned int access ) |
| 135 | { |
| 136 | struct event *event = (struct event *)obj; |
| 137 | assert( obj->ops == &event_ops ); |
| 138 | |
| 139 | if (!(access & EVENT_MODIFY_STATE)) |
| 140 | { |
| 141 | set_error( STATUS_ACCESS_DENIED ); |
| 142 | return 0; |
| 143 | } |
| 144 | set_event( event ); |
| 145 | return 1; |
| 146 | } |
| 147 | |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 148 | /* create an event */ |
| 149 | DECL_HANDLER(create_event) |
| 150 | { |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 151 | struct event *event; |
Alexandre Julliard | ead9b06 | 2005-11-18 16:31:18 +0000 | [diff] [blame] | 152 | struct unicode_str name; |
Vitaliy Margolen | f676bc8 | 2005-12-02 15:55:48 +0100 | [diff] [blame^] | 153 | struct directory *root = NULL; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 154 | |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 155 | reply->handle = 0; |
Alexandre Julliard | ead9b06 | 2005-11-18 16:31:18 +0000 | [diff] [blame] | 156 | get_req_unicode_str( &name ); |
Vitaliy Margolen | f676bc8 | 2005-12-02 15:55:48 +0100 | [diff] [blame^] | 157 | if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 ))) |
| 158 | return; |
| 159 | |
| 160 | if ((event = create_event( root, &name, req->attributes, req->manual_reset, req->initial_state ))) |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 161 | { |
Vitaliy Margolen | a996000 | 2005-10-27 18:30:37 +0000 | [diff] [blame] | 162 | reply->handle = alloc_handle( current->process, event, req->access, |
| 163 | req->attributes & OBJ_INHERIT ); |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 164 | release_object( event ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 165 | } |
Vitaliy Margolen | f676bc8 | 2005-12-02 15:55:48 +0100 | [diff] [blame^] | 166 | |
| 167 | if (root) release_object( root ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | /* open a handle to an event */ |
| 171 | DECL_HANDLER(open_event) |
| 172 | { |
Alexandre Julliard | ead9b06 | 2005-11-18 16:31:18 +0000 | [diff] [blame] | 173 | struct unicode_str name; |
Vitaliy Margolen | f676bc8 | 2005-12-02 15:55:48 +0100 | [diff] [blame^] | 174 | struct directory *root = NULL; |
Alexandre Julliard | ead9b06 | 2005-11-18 16:31:18 +0000 | [diff] [blame] | 175 | |
| 176 | get_req_unicode_str( &name ); |
Vitaliy Margolen | f676bc8 | 2005-12-02 15:55:48 +0100 | [diff] [blame^] | 177 | if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 ))) |
| 178 | return; |
| 179 | |
| 180 | reply->handle = open_object_dir( root, &name, req->attributes, &event_ops, req->access ); |
| 181 | |
| 182 | if (root) release_object( root ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | /* do an event operation */ |
| 186 | DECL_HANDLER(event_op) |
| 187 | { |
Alexandre Julliard | d6d994f | 1999-09-28 16:40:07 +0000 | [diff] [blame] | 188 | struct event *event; |
| 189 | |
| 190 | if (!(event = get_event_obj( current->process, req->handle, EVENT_MODIFY_STATE ))) return; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 191 | switch(req->op) |
| 192 | { |
| 193 | case PULSE_EVENT: |
Alexandre Julliard | d6d994f | 1999-09-28 16:40:07 +0000 | [diff] [blame] | 194 | pulse_event( event ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 195 | break; |
| 196 | case SET_EVENT: |
Alexandre Julliard | d6d994f | 1999-09-28 16:40:07 +0000 | [diff] [blame] | 197 | set_event( event ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 198 | break; |
| 199 | case RESET_EVENT: |
Alexandre Julliard | d6d994f | 1999-09-28 16:40:07 +0000 | [diff] [blame] | 200 | reset_event( event ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 201 | break; |
| 202 | default: |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 203 | fatal_protocol_error( current, "event_op: invalid operation %d\n", req->op ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 204 | } |
Alexandre Julliard | d6d994f | 1999-09-28 16:40:07 +0000 | [diff] [blame] | 205 | release_object( event ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 206 | } |