completion.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * Server-side IO completion ports implementation
  3. *
  4. * Copyright (C) 2007 Andrey Turkin
  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. */
  21. /* FIXMEs:
  22. * - built-in wait queues used which means:
  23. * + threads are awaken FIFO and not LIFO as native does
  24. * + "max concurrent active threads" parameter not used
  25. * + completion handle is waitable, while native isn't
  26. */
  27. #include "config.h"
  28. #include <stdarg.h>
  29. #include <stdio.h>
  30. #include "ntstatus.h"
  31. #define WIN32_NO_STATUS
  32. #include "windef.h"
  33. #include "winternl.h"
  34. #include "object.h"
  35. #include "file.h"
  36. #include "handle.h"
  37. #include "request.h"
  38. static const WCHAR completion_name[] = {'I','o','C','o','m','p','l','e','t','i','o','n'};
  39. struct type_descr completion_type =
  40. {
  41. { completion_name, sizeof(completion_name) }, /* name */
  42. IO_COMPLETION_ALL_ACCESS, /* valid_access */
  43. { /* mapping */
  44. STANDARD_RIGHTS_READ | IO_COMPLETION_QUERY_STATE,
  45. STANDARD_RIGHTS_WRITE | IO_COMPLETION_MODIFY_STATE,
  46. STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE,
  47. IO_COMPLETION_ALL_ACCESS
  48. },
  49. };
  50. struct completion
  51. {
  52. struct object obj;
  53. struct list queue;
  54. unsigned int depth;
  55. struct fast_sync *fast_sync;
  56. };
  57. static void completion_dump( struct object*, int );
  58. static int completion_signaled( struct object *obj, struct wait_queue_entry *entry );
  59. static struct fast_sync *completion_get_fast_sync( struct object *obj );
  60. static void completion_destroy( struct object * );
  61. static const struct object_ops completion_ops =
  62. {
  63. sizeof(struct completion), /* size */
  64. &completion_type, /* type */
  65. completion_dump, /* dump */
  66. add_queue, /* add_queue */
  67. remove_queue, /* remove_queue */
  68. completion_signaled, /* signaled */
  69. no_satisfied, /* satisfied */
  70. no_signal, /* signal */
  71. no_get_fd, /* get_fd */
  72. default_map_access, /* map_access */
  73. default_get_sd, /* get_sd */
  74. default_set_sd, /* set_sd */
  75. default_get_full_name, /* get_full_name */
  76. no_lookup_name, /* lookup_name */
  77. directory_link_name, /* link_name */
  78. default_unlink_name, /* unlink_name */
  79. no_open_file, /* open_file */
  80. no_kernel_obj_list, /* get_kernel_obj_list */
  81. completion_get_fast_sync, /* get_fast_sync */
  82. no_close_handle, /* close_handle */
  83. completion_destroy /* destroy */
  84. };
  85. struct comp_msg
  86. {
  87. struct list queue_entry;
  88. apc_param_t ckey;
  89. apc_param_t cvalue;
  90. apc_param_t information;
  91. unsigned int status;
  92. };
  93. static void completion_destroy( struct object *obj)
  94. {
  95. struct completion *completion = (struct completion *) obj;
  96. struct comp_msg *tmp, *next;
  97. LIST_FOR_EACH_ENTRY_SAFE( tmp, next, &completion->queue, struct comp_msg, queue_entry )
  98. {
  99. free( tmp );
  100. }
  101. if (completion->fast_sync) release_object( completion->fast_sync );
  102. }
  103. static void completion_dump( struct object *obj, int verbose )
  104. {
  105. struct completion *completion = (struct completion *) obj;
  106. assert( obj->ops == &completion_ops );
  107. fprintf( stderr, "Completion depth=%u\n", completion->depth );
  108. }
  109. static int completion_signaled( struct object *obj, struct wait_queue_entry *entry )
  110. {
  111. struct completion *completion = (struct completion *)obj;
  112. return !list_empty( &completion->queue );
  113. }
  114. static struct fast_sync *completion_get_fast_sync( struct object *obj )
  115. {
  116. struct completion *completion = (struct completion *)obj;
  117. if (!completion->fast_sync)
  118. completion->fast_sync = fast_create_event( FAST_SYNC_MANUAL_SERVER, !list_empty( &completion->queue ) );
  119. if (completion->fast_sync) grab_object( completion->fast_sync );
  120. return completion->fast_sync;
  121. }
  122. static struct completion *create_completion( struct object *root, const struct unicode_str *name,
  123. unsigned int attr, unsigned int concurrent,
  124. const struct security_descriptor *sd )
  125. {
  126. struct completion *completion;
  127. if ((completion = create_named_object( root, &completion_ops, name, attr, sd )))
  128. {
  129. if (get_error() != STATUS_OBJECT_NAME_EXISTS)
  130. {
  131. list_init( &completion->queue );
  132. completion->depth = 0;
  133. completion->fast_sync = NULL;
  134. }
  135. }
  136. return completion;
  137. }
  138. struct completion *get_completion_obj( struct process *process, obj_handle_t handle, unsigned int access )
  139. {
  140. return (struct completion *) get_handle_obj( process, handle, access, &completion_ops );
  141. }
  142. void add_completion( struct completion *completion, apc_param_t ckey, apc_param_t cvalue,
  143. unsigned int status, apc_param_t information )
  144. {
  145. struct comp_msg *msg = mem_alloc( sizeof( *msg ) );
  146. if (!msg)
  147. return;
  148. msg->ckey = ckey;
  149. msg->cvalue = cvalue;
  150. msg->status = status;
  151. msg->information = information;
  152. list_add_tail( &completion->queue, &msg->queue_entry );
  153. completion->depth++;
  154. wake_up( &completion->obj, 1 );
  155. fast_set_event( completion->fast_sync );
  156. }
  157. /* create a completion */
  158. DECL_HANDLER(create_completion)
  159. {
  160. struct completion *completion;
  161. struct unicode_str name;
  162. struct object *root;
  163. const struct security_descriptor *sd;
  164. const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
  165. if (!objattr) return;
  166. if ((completion = create_completion( root, &name, objattr->attributes, req->concurrent, sd )))
  167. {
  168. reply->handle = alloc_handle( current->process, completion, req->access, objattr->attributes );
  169. release_object( completion );
  170. }
  171. if (root) release_object( root );
  172. }
  173. /* open a completion */
  174. DECL_HANDLER(open_completion)
  175. {
  176. struct unicode_str name = get_req_unicode_str();
  177. reply->handle = open_object( current->process, req->rootdir, req->access,
  178. &completion_ops, &name, req->attributes );
  179. }
  180. /* add completion to completion port */
  181. DECL_HANDLER(add_completion)
  182. {
  183. struct completion* completion = get_completion_obj( current->process, req->handle, IO_COMPLETION_MODIFY_STATE );
  184. if (!completion) return;
  185. add_completion( completion, req->ckey, req->cvalue, req->status, req->information );
  186. release_object( completion );
  187. }
  188. /* get completion from completion port */
  189. DECL_HANDLER(remove_completion)
  190. {
  191. struct completion* completion = get_completion_obj( current->process, req->handle, IO_COMPLETION_MODIFY_STATE );
  192. struct list *entry;
  193. struct comp_msg *msg;
  194. if (!completion) return;
  195. entry = list_head( &completion->queue );
  196. if (!entry)
  197. set_error( STATUS_PENDING );
  198. else
  199. {
  200. list_remove( entry );
  201. completion->depth--;
  202. msg = LIST_ENTRY( entry, struct comp_msg, queue_entry );
  203. reply->ckey = msg->ckey;
  204. reply->cvalue = msg->cvalue;
  205. reply->status = msg->status;
  206. reply->information = msg->information;
  207. free( msg );
  208. if (list_empty( &completion->queue ))
  209. fast_reset_event( completion->fast_sync );
  210. }
  211. release_object( completion );
  212. }
  213. /* get queue depth for completion port */
  214. DECL_HANDLER(query_completion)
  215. {
  216. struct completion* completion = get_completion_obj( current->process, req->handle, IO_COMPLETION_QUERY_STATE );
  217. if (!completion) return;
  218. reply->depth = completion->depth;
  219. release_object( completion );
  220. }