event.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /*
  2. * Server-side event management
  3. *
  4. * Copyright (C) 1998 Alexandre Julliard
  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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  19. */
  20. #include "config.h"
  21. #include "wine/port.h"
  22. #include <assert.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <stdarg.h>
  26. #include "ntstatus.h"
  27. #define WIN32_NO_STATUS
  28. #include "windef.h"
  29. #include "winternl.h"
  30. #include "handle.h"
  31. #include "thread.h"
  32. #include "request.h"
  33. #include "security.h"
  34. #include "esync.h"
  35. struct event
  36. {
  37. struct object obj; /* object header */
  38. struct list kernel_object; /* list of kernel object pointers */
  39. int manual_reset; /* is it a manual reset event? */
  40. int signaled; /* event has been signaled */
  41. int esync_fd; /* esync file descriptor */
  42. };
  43. static void event_dump( struct object *obj, int verbose );
  44. static struct object_type *event_get_type( struct object *obj );
  45. static int event_signaled( struct object *obj, struct wait_queue_entry *entry );
  46. static void event_satisfied( struct object *obj, struct wait_queue_entry *entry );
  47. static int event_get_esync_fd( struct object *obj, enum esync_type *type );
  48. static unsigned int event_map_access( struct object *obj, unsigned int access );
  49. static int event_signal( struct object *obj, unsigned int access);
  50. static struct list *event_get_kernel_obj_list( struct object *obj );
  51. static void event_destroy( struct object *obj );
  52. static const struct object_ops event_ops =
  53. {
  54. sizeof(struct event), /* size */
  55. event_dump, /* dump */
  56. event_get_type, /* get_type */
  57. add_queue, /* add_queue */
  58. remove_queue, /* remove_queue */
  59. event_signaled, /* signaled */
  60. event_get_esync_fd, /* get_esync_fd */
  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. event_get_kernel_obj_list, /* get_kernel_obj_list */
  72. no_alloc_handle, /* alloc_handle */
  73. no_close_handle, /* close_handle */
  74. event_destroy /* destroy */
  75. };
  76. struct keyed_event
  77. {
  78. struct object obj; /* object header */
  79. };
  80. static void keyed_event_dump( struct object *obj, int verbose );
  81. static struct object_type *keyed_event_get_type( struct object *obj );
  82. static int keyed_event_signaled( struct object *obj, struct wait_queue_entry *entry );
  83. static unsigned int keyed_event_map_access( struct object *obj, unsigned int access );
  84. static const struct object_ops keyed_event_ops =
  85. {
  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. NULL, /* get_esync_fd */
  93. no_satisfied, /* satisfied */
  94. no_signal, /* signal */
  95. no_get_fd, /* get_fd */
  96. keyed_event_map_access, /* map_access */
  97. default_get_sd, /* get_sd */
  98. default_set_sd, /* set_sd */
  99. no_lookup_name, /* lookup_name */
  100. directory_link_name, /* link_name */
  101. default_unlink_name, /* unlink_name */
  102. no_open_file, /* open_file */
  103. no_kernel_obj_list, /* get_kernel_obj_list */
  104. no_alloc_handle, /* alloc_handle */
  105. no_close_handle, /* close_handle */
  106. no_destroy /* destroy */
  107. };
  108. struct event *create_event( struct object *root, const struct unicode_str *name,
  109. unsigned int attr, int manual_reset, int initial_state,
  110. const struct security_descriptor *sd )
  111. {
  112. struct event *event;
  113. if ((event = create_named_object( root, &event_ops, name, attr, sd )))
  114. {
  115. if (get_error() != STATUS_OBJECT_NAME_EXISTS)
  116. {
  117. /* initialize it if it didn't already exist */
  118. list_init( &event->kernel_object );
  119. event->manual_reset = manual_reset;
  120. event->signaled = initial_state;
  121. if (do_esync())
  122. event->esync_fd = esync_create_fd( initial_state, 0 );
  123. }
  124. }
  125. return event;
  126. }
  127. struct event *get_event_obj( struct process *process, obj_handle_t handle, unsigned int access )
  128. {
  129. struct object *obj;
  130. if (do_esync() && (obj = get_handle_obj( process, handle, access, &esync_ops)))
  131. return (struct event *)obj; /* even though it's not an event */
  132. return (struct event *)get_handle_obj( process, handle, access, &event_ops );
  133. }
  134. void pulse_event( struct event *event )
  135. {
  136. event->signaled = 1;
  137. /* wake up all waiters if manual reset, a single one otherwise */
  138. wake_up( &event->obj, !event->manual_reset );
  139. event->signaled = 0;
  140. }
  141. void set_event( struct event *event )
  142. {
  143. if (do_esync() && event->obj.ops == &esync_ops)
  144. {
  145. esync_set_event( (struct esync *)event );
  146. return;
  147. }
  148. event->signaled = 1;
  149. /* wake up all waiters if manual reset, a single one otherwise */
  150. wake_up( &event->obj, !event->manual_reset );
  151. }
  152. void reset_event( struct event *event )
  153. {
  154. if (do_esync() && event->obj.ops == &esync_ops)
  155. {
  156. esync_reset_event( (struct esync *)event );
  157. return;
  158. }
  159. event->signaled = 0;
  160. if (do_esync())
  161. esync_clear( event->esync_fd );
  162. }
  163. static void event_dump( struct object *obj, int verbose )
  164. {
  165. struct event *event = (struct event *)obj;
  166. assert( obj->ops == &event_ops );
  167. fprintf( stderr, "Event manual=%d signaled=%d\n",
  168. event->manual_reset, event->signaled );
  169. }
  170. static struct object_type *event_get_type( struct object *obj )
  171. {
  172. static const struct unicode_str str = { type_Event, sizeof(type_Event) };
  173. return get_object_type( &str );
  174. }
  175. static int event_signaled( struct object *obj, struct wait_queue_entry *entry )
  176. {
  177. struct event *event = (struct event *)obj;
  178. assert( obj->ops == &event_ops );
  179. return event->signaled;
  180. }
  181. static int event_get_esync_fd( struct object *obj, enum esync_type *type )
  182. {
  183. struct event *event = (struct event *)obj;
  184. *type = ESYNC_MANUAL_SERVER; /* all server-created events are manual-reset */
  185. return event->esync_fd;
  186. }
  187. static void event_satisfied( struct object *obj, struct wait_queue_entry *entry )
  188. {
  189. struct event *event = (struct event *)obj;
  190. assert( obj->ops == &event_ops );
  191. /* Reset if it's an auto-reset event */
  192. if (!event->manual_reset) event->signaled = 0;
  193. }
  194. static unsigned int event_map_access( struct object *obj, unsigned int access )
  195. {
  196. if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | EVENT_QUERY_STATE;
  197. if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | EVENT_MODIFY_STATE;
  198. if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE;
  199. if (access & GENERIC_ALL) access |= STANDARD_RIGHTS_ALL | EVENT_QUERY_STATE | EVENT_MODIFY_STATE;
  200. return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
  201. }
  202. static int event_signal( struct object *obj, unsigned int access )
  203. {
  204. struct event *event = (struct event *)obj;
  205. assert( obj->ops == &event_ops );
  206. if (!(access & EVENT_MODIFY_STATE))
  207. {
  208. set_error( STATUS_ACCESS_DENIED );
  209. return 0;
  210. }
  211. set_event( event );
  212. return 1;
  213. }
  214. static struct list *event_get_kernel_obj_list( struct object *obj )
  215. {
  216. struct event *event = (struct event *)obj;
  217. return &event->kernel_object;
  218. }
  219. static void event_destroy( struct object *obj )
  220. {
  221. struct event *event = (struct event *)obj;
  222. if (do_esync())
  223. close( event->esync_fd );
  224. }
  225. struct keyed_event *create_keyed_event( struct object *root, const struct unicode_str *name,
  226. unsigned int attr, const struct security_descriptor *sd )
  227. {
  228. struct keyed_event *event;
  229. if ((event = create_named_object( root, &keyed_event_ops, name, attr, sd )))
  230. {
  231. if (get_error() != STATUS_OBJECT_NAME_EXISTS)
  232. {
  233. /* initialize it if it didn't already exist */
  234. }
  235. }
  236. return event;
  237. }
  238. struct keyed_event *get_keyed_event_obj( struct process *process, obj_handle_t handle, unsigned int access )
  239. {
  240. return (struct keyed_event *)get_handle_obj( process, handle, access, &keyed_event_ops );
  241. }
  242. static void keyed_event_dump( struct object *obj, int verbose )
  243. {
  244. fputs( "Keyed event\n", stderr );
  245. }
  246. static struct object_type *keyed_event_get_type( struct object *obj )
  247. {
  248. static const struct unicode_str str = { type_KeyedEvent, sizeof(type_KeyedEvent) };
  249. return get_object_type( &str );
  250. }
  251. static enum select_op matching_op( enum select_op op )
  252. {
  253. return op ^ (SELECT_KEYED_EVENT_WAIT ^ SELECT_KEYED_EVENT_RELEASE);
  254. }
  255. static int keyed_event_signaled( struct object *obj, struct wait_queue_entry *entry )
  256. {
  257. struct wait_queue_entry *ptr;
  258. struct process *process;
  259. enum select_op select_op;
  260. assert( obj->ops == &keyed_event_ops );
  261. process = get_wait_queue_thread( entry )->process;
  262. select_op = get_wait_queue_select_op( entry );
  263. if (select_op != SELECT_KEYED_EVENT_WAIT && select_op != SELECT_KEYED_EVENT_RELEASE) return 1;
  264. LIST_FOR_EACH_ENTRY( ptr, &obj->wait_queue, struct wait_queue_entry, entry )
  265. {
  266. if (ptr == entry) continue;
  267. if (get_wait_queue_thread( ptr )->process != process) continue;
  268. if (get_wait_queue_select_op( ptr ) != matching_op( select_op )) continue;
  269. if (get_wait_queue_key( ptr ) != get_wait_queue_key( entry )) continue;
  270. if (wake_thread_queue_entry( ptr )) return 1;
  271. }
  272. return 0;
  273. }
  274. static unsigned int keyed_event_map_access( struct object *obj, unsigned int access )
  275. {
  276. if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | KEYEDEVENT_WAIT;
  277. if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | KEYEDEVENT_WAKE;
  278. if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
  279. if (access & GENERIC_ALL) access |= KEYEDEVENT_ALL_ACCESS;
  280. return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
  281. }
  282. /* create an event */
  283. DECL_HANDLER(create_event)
  284. {
  285. struct event *event;
  286. struct unicode_str name;
  287. struct object *root;
  288. const struct security_descriptor *sd;
  289. const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
  290. if (!objattr) return;
  291. if ((event = create_event( root, &name, objattr->attributes,
  292. req->manual_reset, req->initial_state, sd )))
  293. {
  294. if (get_error() == STATUS_OBJECT_NAME_EXISTS)
  295. reply->handle = alloc_handle( current->process, event, req->access, objattr->attributes );
  296. else
  297. reply->handle = alloc_handle_no_access_check( current->process, event,
  298. req->access, objattr->attributes );
  299. release_object( event );
  300. }
  301. if (root) release_object( root );
  302. }
  303. /* open a handle to an event */
  304. DECL_HANDLER(open_event)
  305. {
  306. struct unicode_str name = get_req_unicode_str();
  307. reply->handle = open_object( current->process, req->rootdir, req->access,
  308. &event_ops, &name, req->attributes );
  309. }
  310. /* do an event operation */
  311. DECL_HANDLER(event_op)
  312. {
  313. struct event *event;
  314. if (!(event = get_event_obj( current->process, req->handle, EVENT_MODIFY_STATE ))) return;
  315. reply->state = event->signaled;
  316. switch(req->op)
  317. {
  318. case PULSE_EVENT:
  319. pulse_event( event );
  320. break;
  321. case SET_EVENT:
  322. set_event( event );
  323. break;
  324. case RESET_EVENT:
  325. reset_event( event );
  326. break;
  327. default:
  328. set_error( STATUS_INVALID_PARAMETER );
  329. break;
  330. }
  331. release_object( event );
  332. }
  333. /* return details about the event */
  334. DECL_HANDLER(query_event)
  335. {
  336. struct event *event;
  337. if (!(event = get_event_obj( current->process, req->handle, EVENT_QUERY_STATE ))) return;
  338. reply->manual_reset = event->manual_reset;
  339. reply->state = event->signaled;
  340. release_object( event );
  341. }
  342. /* create a keyed event */
  343. DECL_HANDLER(create_keyed_event)
  344. {
  345. struct keyed_event *event;
  346. struct unicode_str name;
  347. struct object *root;
  348. const struct security_descriptor *sd;
  349. const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
  350. if (!objattr) return;
  351. if ((event = create_keyed_event( root, &name, objattr->attributes, sd )))
  352. {
  353. if (get_error() == STATUS_OBJECT_NAME_EXISTS)
  354. reply->handle = alloc_handle( current->process, event, req->access, objattr->attributes );
  355. else
  356. reply->handle = alloc_handle_no_access_check( current->process, event,
  357. req->access, objattr->attributes );
  358. release_object( event );
  359. }
  360. if (root) release_object( root );
  361. }
  362. /* open a handle to a keyed event */
  363. DECL_HANDLER(open_keyed_event)
  364. {
  365. struct unicode_str name = get_req_unicode_str();
  366. reply->handle = open_object( current->process, req->rootdir, req->access,
  367. &keyed_event_ops, &name, req->attributes );
  368. }