event.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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 <assert.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <stdarg.h>
  25. #include <sys/types.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. static const WCHAR event_name[] = {'E','v','e','n','t'};
  35. struct type_descr event_type =
  36. {
  37. { event_name, sizeof(event_name) }, /* name */
  38. EVENT_ALL_ACCESS, /* valid_access */
  39. { /* mapping */
  40. STANDARD_RIGHTS_READ | EVENT_QUERY_STATE,
  41. STANDARD_RIGHTS_WRITE | EVENT_MODIFY_STATE,
  42. STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE,
  43. EVENT_ALL_ACCESS
  44. },
  45. };
  46. struct event
  47. {
  48. struct object obj; /* object header */
  49. struct list kernel_object; /* list of kernel object pointers */
  50. int manual_reset; /* is it a manual reset event? */
  51. int signaled; /* event has been signaled */
  52. };
  53. static void event_dump( struct object *obj, int verbose );
  54. static int event_signaled( struct object *obj, struct wait_queue_entry *entry );
  55. static void event_satisfied( struct object *obj, struct wait_queue_entry *entry );
  56. static int event_signal( struct object *obj, unsigned int access);
  57. static struct list *event_get_kernel_obj_list( struct object *obj );
  58. static const struct object_ops event_ops =
  59. {
  60. sizeof(struct event), /* size */
  61. &event_type, /* type */
  62. event_dump, /* dump */
  63. add_queue, /* add_queue */
  64. remove_queue, /* remove_queue */
  65. event_signaled, /* signaled */
  66. event_satisfied, /* satisfied */
  67. event_signal, /* signal */
  68. no_get_fd, /* get_fd */
  69. default_map_access, /* map_access */
  70. default_get_sd, /* get_sd */
  71. default_set_sd, /* set_sd */
  72. default_get_full_name, /* get_full_name */
  73. no_lookup_name, /* lookup_name */
  74. directory_link_name, /* link_name */
  75. default_unlink_name, /* unlink_name */
  76. no_open_file, /* open_file */
  77. event_get_kernel_obj_list, /* get_kernel_obj_list */
  78. no_close_handle, /* close_handle */
  79. no_destroy /* destroy */
  80. };
  81. static const WCHAR keyed_event_name[] = {'K','e','y','e','d','E','v','e','n','t'};
  82. struct type_descr keyed_event_type =
  83. {
  84. { keyed_event_name, sizeof(keyed_event_name) }, /* name */
  85. KEYEDEVENT_ALL_ACCESS | SYNCHRONIZE, /* valid_access */
  86. { /* mapping */
  87. STANDARD_RIGHTS_READ | KEYEDEVENT_WAIT,
  88. STANDARD_RIGHTS_WRITE | KEYEDEVENT_WAKE,
  89. STANDARD_RIGHTS_EXECUTE,
  90. KEYEDEVENT_ALL_ACCESS
  91. },
  92. };
  93. struct keyed_event
  94. {
  95. struct object obj; /* object header */
  96. };
  97. static void keyed_event_dump( struct object *obj, int verbose );
  98. static int keyed_event_signaled( struct object *obj, struct wait_queue_entry *entry );
  99. static const struct object_ops keyed_event_ops =
  100. {
  101. sizeof(struct keyed_event), /* size */
  102. &keyed_event_type, /* type */
  103. keyed_event_dump, /* dump */
  104. add_queue, /* add_queue */
  105. remove_queue, /* remove_queue */
  106. keyed_event_signaled, /* signaled */
  107. no_satisfied, /* satisfied */
  108. no_signal, /* signal */
  109. no_get_fd, /* get_fd */
  110. default_map_access, /* map_access */
  111. default_get_sd, /* get_sd */
  112. default_set_sd, /* set_sd */
  113. default_get_full_name, /* get_full_name */
  114. no_lookup_name, /* lookup_name */
  115. directory_link_name, /* link_name */
  116. default_unlink_name, /* unlink_name */
  117. no_open_file, /* open_file */
  118. no_kernel_obj_list, /* get_kernel_obj_list */
  119. no_close_handle, /* close_handle */
  120. no_destroy /* destroy */
  121. };
  122. struct event *create_event( struct object *root, const struct unicode_str *name,
  123. unsigned int attr, int manual_reset, int initial_state,
  124. const struct security_descriptor *sd )
  125. {
  126. struct event *event;
  127. if ((event = create_named_object( root, &event_ops, name, attr, sd )))
  128. {
  129. if (get_error() != STATUS_OBJECT_NAME_EXISTS)
  130. {
  131. /* initialize it if it didn't already exist */
  132. list_init( &event->kernel_object );
  133. event->manual_reset = manual_reset;
  134. event->signaled = initial_state;
  135. }
  136. }
  137. return event;
  138. }
  139. struct event *get_event_obj( struct process *process, obj_handle_t handle, unsigned int access )
  140. {
  141. return (struct event *)get_handle_obj( process, handle, access, &event_ops );
  142. }
  143. static void pulse_event( struct event *event )
  144. {
  145. event->signaled = 1;
  146. /* wake up all waiters if manual reset, a single one otherwise */
  147. wake_up( &event->obj, !event->manual_reset );
  148. event->signaled = 0;
  149. }
  150. void set_event( struct event *event )
  151. {
  152. event->signaled = 1;
  153. /* wake up all waiters if manual reset, a single one otherwise */
  154. wake_up( &event->obj, !event->manual_reset );
  155. }
  156. void reset_event( struct event *event )
  157. {
  158. event->signaled = 0;
  159. }
  160. static void event_dump( struct object *obj, int verbose )
  161. {
  162. struct event *event = (struct event *)obj;
  163. assert( obj->ops == &event_ops );
  164. fprintf( stderr, "Event manual=%d signaled=%d\n",
  165. event->manual_reset, event->signaled );
  166. }
  167. static int event_signaled( struct object *obj, struct wait_queue_entry *entry )
  168. {
  169. struct event *event = (struct event *)obj;
  170. assert( obj->ops == &event_ops );
  171. return event->signaled;
  172. }
  173. static void event_satisfied( struct object *obj, struct wait_queue_entry *entry )
  174. {
  175. struct event *event = (struct event *)obj;
  176. assert( obj->ops == &event_ops );
  177. /* Reset if it's an auto-reset event */
  178. if (!event->manual_reset) event->signaled = 0;
  179. }
  180. static int event_signal( struct object *obj, unsigned int access )
  181. {
  182. struct event *event = (struct event *)obj;
  183. assert( obj->ops == &event_ops );
  184. if (!(access & EVENT_MODIFY_STATE))
  185. {
  186. set_error( STATUS_ACCESS_DENIED );
  187. return 0;
  188. }
  189. set_event( event );
  190. return 1;
  191. }
  192. static struct list *event_get_kernel_obj_list( struct object *obj )
  193. {
  194. struct event *event = (struct event *)obj;
  195. return &event->kernel_object;
  196. }
  197. struct keyed_event *create_keyed_event( struct object *root, const struct unicode_str *name,
  198. unsigned int attr, const struct security_descriptor *sd )
  199. {
  200. struct keyed_event *event;
  201. if ((event = create_named_object( root, &keyed_event_ops, name, attr, sd )))
  202. {
  203. if (get_error() != STATUS_OBJECT_NAME_EXISTS)
  204. {
  205. /* initialize it if it didn't already exist */
  206. }
  207. }
  208. return event;
  209. }
  210. struct keyed_event *get_keyed_event_obj( struct process *process, obj_handle_t handle, unsigned int access )
  211. {
  212. return (struct keyed_event *)get_handle_obj( process, handle, access, &keyed_event_ops );
  213. }
  214. static void keyed_event_dump( struct object *obj, int verbose )
  215. {
  216. fputs( "Keyed event\n", stderr );
  217. }
  218. static enum select_op matching_op( enum select_op op )
  219. {
  220. return op ^ (SELECT_KEYED_EVENT_WAIT ^ SELECT_KEYED_EVENT_RELEASE);
  221. }
  222. static int keyed_event_signaled( struct object *obj, struct wait_queue_entry *entry )
  223. {
  224. struct wait_queue_entry *ptr;
  225. struct process *process;
  226. enum select_op select_op;
  227. assert( obj->ops == &keyed_event_ops );
  228. process = get_wait_queue_thread( entry )->process;
  229. select_op = get_wait_queue_select_op( entry );
  230. if (select_op != SELECT_KEYED_EVENT_WAIT && select_op != SELECT_KEYED_EVENT_RELEASE) return 1;
  231. LIST_FOR_EACH_ENTRY( ptr, &obj->wait_queue, struct wait_queue_entry, entry )
  232. {
  233. if (ptr == entry) continue;
  234. if (get_wait_queue_thread( ptr )->process != process) continue;
  235. if (get_wait_queue_select_op( ptr ) != matching_op( select_op )) continue;
  236. if (get_wait_queue_key( ptr ) != get_wait_queue_key( entry )) continue;
  237. if (wake_thread_queue_entry( ptr )) return 1;
  238. }
  239. return 0;
  240. }
  241. /* create an event */
  242. DECL_HANDLER(create_event)
  243. {
  244. struct event *event;
  245. struct unicode_str name;
  246. struct object *root;
  247. const struct security_descriptor *sd;
  248. const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
  249. if (!objattr) return;
  250. if ((event = create_event( root, &name, objattr->attributes,
  251. req->manual_reset, req->initial_state, sd )))
  252. {
  253. if (get_error() == STATUS_OBJECT_NAME_EXISTS)
  254. reply->handle = alloc_handle( current->process, event, req->access, objattr->attributes );
  255. else
  256. reply->handle = alloc_handle_no_access_check( current->process, event,
  257. req->access, objattr->attributes );
  258. release_object( event );
  259. }
  260. if (root) release_object( root );
  261. }
  262. /* open a handle to an event */
  263. DECL_HANDLER(open_event)
  264. {
  265. struct unicode_str name = get_req_unicode_str();
  266. reply->handle = open_object( current->process, req->rootdir, req->access,
  267. &event_ops, &name, req->attributes );
  268. }
  269. /* do an event operation */
  270. DECL_HANDLER(event_op)
  271. {
  272. struct event *event;
  273. if (!(event = get_event_obj( current->process, req->handle, EVENT_MODIFY_STATE ))) return;
  274. reply->state = event->signaled;
  275. switch(req->op)
  276. {
  277. case PULSE_EVENT:
  278. pulse_event( event );
  279. break;
  280. case SET_EVENT:
  281. set_event( event );
  282. break;
  283. case RESET_EVENT:
  284. reset_event( event );
  285. break;
  286. default:
  287. set_error( STATUS_INVALID_PARAMETER );
  288. break;
  289. }
  290. release_object( event );
  291. }
  292. /* return details about the event */
  293. DECL_HANDLER(query_event)
  294. {
  295. struct event *event;
  296. if (!(event = get_event_obj( current->process, req->handle, EVENT_QUERY_STATE ))) return;
  297. reply->manual_reset = event->manual_reset;
  298. reply->state = event->signaled;
  299. release_object( event );
  300. }
  301. /* create a keyed event */
  302. DECL_HANDLER(create_keyed_event)
  303. {
  304. struct keyed_event *event;
  305. struct unicode_str name;
  306. struct object *root;
  307. const struct security_descriptor *sd;
  308. const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
  309. if (!objattr) return;
  310. if ((event = create_keyed_event( root, &name, objattr->attributes, sd )))
  311. {
  312. if (get_error() == STATUS_OBJECT_NAME_EXISTS)
  313. reply->handle = alloc_handle( current->process, event, req->access, objattr->attributes );
  314. else
  315. reply->handle = alloc_handle_no_access_check( current->process, event,
  316. req->access, objattr->attributes );
  317. release_object( event );
  318. }
  319. if (root) release_object( root );
  320. }
  321. /* open a handle to a keyed event */
  322. DECL_HANDLER(open_keyed_event)
  323. {
  324. struct unicode_str name = get_req_unicode_str();
  325. reply->handle = open_object( current->process, req->rootdir, req->access,
  326. &keyed_event_ops, &name, req->attributes );
  327. }