completion.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. };
  56. static void completion_dump( struct object*, int );
  57. static int completion_signaled( struct object *obj, struct wait_queue_entry *entry );
  58. static void completion_destroy( struct object * );
  59. static const struct object_ops completion_ops =
  60. {
  61. sizeof(struct completion), /* size */
  62. &completion_type, /* type */
  63. completion_dump, /* dump */
  64. add_queue, /* add_queue */
  65. remove_queue, /* remove_queue */
  66. completion_signaled, /* signaled */
  67. no_satisfied, /* satisfied */
  68. no_signal, /* signal */
  69. no_get_fd, /* get_fd */
  70. default_map_access, /* map_access */
  71. default_get_sd, /* get_sd */
  72. default_set_sd, /* set_sd */
  73. default_get_full_name, /* get_full_name */
  74. no_lookup_name, /* lookup_name */
  75. directory_link_name, /* link_name */
  76. default_unlink_name, /* unlink_name */
  77. no_open_file, /* open_file */
  78. no_kernel_obj_list, /* get_kernel_obj_list */
  79. no_close_handle, /* close_handle */
  80. completion_destroy /* destroy */
  81. };
  82. struct comp_msg
  83. {
  84. struct list queue_entry;
  85. apc_param_t ckey;
  86. apc_param_t cvalue;
  87. apc_param_t information;
  88. unsigned int status;
  89. };
  90. static void completion_destroy( struct object *obj)
  91. {
  92. struct completion *completion = (struct completion *) obj;
  93. struct comp_msg *tmp, *next;
  94. LIST_FOR_EACH_ENTRY_SAFE( tmp, next, &completion->queue, struct comp_msg, queue_entry )
  95. {
  96. free( tmp );
  97. }
  98. }
  99. static void completion_dump( struct object *obj, int verbose )
  100. {
  101. struct completion *completion = (struct completion *) obj;
  102. assert( obj->ops == &completion_ops );
  103. fprintf( stderr, "Completion depth=%u\n", completion->depth );
  104. }
  105. static int completion_signaled( struct object *obj, struct wait_queue_entry *entry )
  106. {
  107. struct completion *completion = (struct completion *)obj;
  108. return !list_empty( &completion->queue );
  109. }
  110. static struct completion *create_completion( struct object *root, const struct unicode_str *name,
  111. unsigned int attr, unsigned int concurrent,
  112. const struct security_descriptor *sd )
  113. {
  114. struct completion *completion;
  115. if ((completion = create_named_object( root, &completion_ops, name, attr, sd )))
  116. {
  117. if (get_error() != STATUS_OBJECT_NAME_EXISTS)
  118. {
  119. list_init( &completion->queue );
  120. completion->depth = 0;
  121. }
  122. }
  123. return completion;
  124. }
  125. struct completion *get_completion_obj( struct process *process, obj_handle_t handle, unsigned int access )
  126. {
  127. return (struct completion *) get_handle_obj( process, handle, access, &completion_ops );
  128. }
  129. void add_completion( struct completion *completion, apc_param_t ckey, apc_param_t cvalue,
  130. unsigned int status, apc_param_t information )
  131. {
  132. struct comp_msg *msg = mem_alloc( sizeof( *msg ) );
  133. if (!msg)
  134. return;
  135. msg->ckey = ckey;
  136. msg->cvalue = cvalue;
  137. msg->status = status;
  138. msg->information = information;
  139. list_add_tail( &completion->queue, &msg->queue_entry );
  140. completion->depth++;
  141. wake_up( &completion->obj, 1 );
  142. }
  143. /* create a completion */
  144. DECL_HANDLER(create_completion)
  145. {
  146. struct completion *completion;
  147. struct unicode_str name;
  148. struct object *root;
  149. const struct security_descriptor *sd;
  150. const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
  151. if (!objattr) return;
  152. if ((completion = create_completion( root, &name, objattr->attributes, req->concurrent, sd )))
  153. {
  154. reply->handle = alloc_handle( current->process, completion, req->access, objattr->attributes );
  155. release_object( completion );
  156. }
  157. if (root) release_object( root );
  158. }
  159. /* open a completion */
  160. DECL_HANDLER(open_completion)
  161. {
  162. struct unicode_str name = get_req_unicode_str();
  163. reply->handle = open_object( current->process, req->rootdir, req->access,
  164. &completion_ops, &name, req->attributes );
  165. }
  166. /* add completion to completion port */
  167. DECL_HANDLER(add_completion)
  168. {
  169. struct completion* completion = get_completion_obj( current->process, req->handle, IO_COMPLETION_MODIFY_STATE );
  170. if (!completion) return;
  171. add_completion( completion, req->ckey, req->cvalue, req->status, req->information );
  172. release_object( completion );
  173. }
  174. /* get completion from completion port */
  175. DECL_HANDLER(remove_completion)
  176. {
  177. struct completion* completion = get_completion_obj( current->process, req->handle, IO_COMPLETION_MODIFY_STATE );
  178. struct list *entry;
  179. struct comp_msg *msg;
  180. if (!completion) return;
  181. entry = list_head( &completion->queue );
  182. if (!entry)
  183. set_error( STATUS_PENDING );
  184. else
  185. {
  186. list_remove( entry );
  187. completion->depth--;
  188. msg = LIST_ENTRY( entry, struct comp_msg, queue_entry );
  189. reply->ckey = msg->ckey;
  190. reply->cvalue = msg->cvalue;
  191. reply->status = msg->status;
  192. reply->information = msg->information;
  193. free( msg );
  194. }
  195. release_object( completion );
  196. }
  197. /* get queue depth for completion port */
  198. DECL_HANDLER(query_completion)
  199. {
  200. struct completion* completion = get_completion_obj( current->process, req->handle, IO_COMPLETION_QUERY_STATE );
  201. if (!completion) return;
  202. reply->depth = completion->depth;
  203. release_object( completion );
  204. }