named_pipe.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494
  1. /*
  2. * Server-side pipe management
  3. *
  4. * Copyright (C) 1998 Alexandre Julliard
  5. * Copyright (C) 2001 Mike McCormack
  6. * Copyright 2016 Jacek Caban for CodeWeavers
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  21. */
  22. #include "config.h"
  23. #include <assert.h>
  24. #include <string.h>
  25. #include <stdarg.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <sys/types.h>
  29. #include "ntstatus.h"
  30. #define WIN32_NO_STATUS
  31. #include "windef.h"
  32. #include "winternl.h"
  33. #include "winioctl.h"
  34. #include "file.h"
  35. #include "handle.h"
  36. #include "thread.h"
  37. #include "request.h"
  38. #include "security.h"
  39. #include "process.h"
  40. struct named_pipe;
  41. struct pipe_message
  42. {
  43. struct list entry; /* entry in message queue */
  44. data_size_t read_pos; /* already read bytes */
  45. struct iosb *iosb; /* message iosb */
  46. struct async *async; /* async of pending write */
  47. };
  48. struct pipe_end
  49. {
  50. struct object obj; /* object header */
  51. struct fd *fd; /* pipe file descriptor */
  52. unsigned int flags; /* pipe flags */
  53. unsigned int state; /* pipe state */
  54. struct named_pipe *pipe;
  55. struct pipe_end *connection; /* the other end of the pipe */
  56. process_id_t client_pid; /* process that created the client */
  57. process_id_t server_pid; /* process that created the server */
  58. data_size_t buffer_size;/* size of buffered data that doesn't block caller */
  59. struct list message_queue;
  60. struct async_queue read_q; /* read queue */
  61. struct async_queue write_q; /* write queue */
  62. };
  63. struct pipe_server
  64. {
  65. struct pipe_end pipe_end; /* common header for both pipe ends */
  66. struct list entry; /* entry in named pipe listeners list */
  67. unsigned int options; /* pipe options */
  68. struct async_queue listen_q; /* listen queue */
  69. };
  70. struct named_pipe
  71. {
  72. struct object obj; /* object header */
  73. int message_mode;
  74. unsigned int sharing;
  75. unsigned int maxinstances;
  76. unsigned int outsize;
  77. unsigned int insize;
  78. unsigned int instances;
  79. timeout_t timeout;
  80. struct list listeners; /* list of servers listening on this pipe */
  81. struct async_queue waiters; /* list of clients waiting to connect */
  82. };
  83. struct named_pipe_device
  84. {
  85. struct object obj; /* object header */
  86. struct namespace *pipes; /* named pipe namespace */
  87. };
  88. struct named_pipe_device_file
  89. {
  90. struct object obj; /* object header */
  91. struct fd *fd; /* pseudo-fd for ioctls */
  92. struct named_pipe_device *device; /* named pipe device */
  93. };
  94. static void named_pipe_dump( struct object *obj, int verbose );
  95. static unsigned int named_pipe_map_access( struct object *obj, unsigned int access );
  96. static WCHAR *named_pipe_get_full_name( struct object *obj, data_size_t *ret_len );
  97. static int named_pipe_link_name( struct object *obj, struct object_name *name, struct object *parent );
  98. static struct object *named_pipe_open_file( struct object *obj, unsigned int access,
  99. unsigned int sharing, unsigned int options );
  100. static void named_pipe_destroy( struct object *obj );
  101. static const struct object_ops named_pipe_ops =
  102. {
  103. sizeof(struct named_pipe), /* size */
  104. &no_type, /* type */
  105. named_pipe_dump, /* dump */
  106. no_add_queue, /* add_queue */
  107. NULL, /* remove_queue */
  108. NULL, /* signaled */
  109. NULL, /* satisfied */
  110. no_signal, /* signal */
  111. no_get_fd, /* get_fd */
  112. named_pipe_map_access, /* map_access */
  113. default_get_sd, /* get_sd */
  114. default_set_sd, /* set_sd */
  115. named_pipe_get_full_name, /* get_full_name */
  116. no_lookup_name, /* lookup_name */
  117. named_pipe_link_name, /* link_name */
  118. default_unlink_name, /* unlink_name */
  119. named_pipe_open_file, /* open_file */
  120. no_kernel_obj_list, /* get_kernel_obj_list */
  121. no_get_fast_sync, /* get_fast_sync */
  122. no_close_handle, /* close_handle */
  123. named_pipe_destroy /* destroy */
  124. };
  125. /* common server and client pipe end functions */
  126. static void pipe_end_destroy( struct object *obj );
  127. static enum server_fd_type pipe_end_get_fd_type( struct fd *fd );
  128. static struct fd *pipe_end_get_fd( struct object *obj );
  129. static struct security_descriptor *pipe_end_get_sd( struct object *obj );
  130. static int pipe_end_set_sd( struct object *obj, const struct security_descriptor *sd,
  131. unsigned int set_info );
  132. static WCHAR *pipe_end_get_full_name( struct object *obj, data_size_t *len );
  133. static void pipe_end_read( struct fd *fd, struct async *async, file_pos_t pos );
  134. static void pipe_end_write( struct fd *fd, struct async *async_data, file_pos_t pos );
  135. static void pipe_end_flush( struct fd *fd, struct async *async );
  136. static void pipe_end_get_volume_info( struct fd *fd, struct async *async, unsigned int info_class );
  137. static void pipe_end_reselect_async( struct fd *fd, struct async_queue *queue );
  138. static void pipe_end_get_file_info( struct fd *fd, obj_handle_t handle, unsigned int info_class );
  139. /* server end functions */
  140. static void pipe_server_dump( struct object *obj, int verbose );
  141. static struct object *pipe_server_lookup_name( struct object *obj, struct unicode_str *name,
  142. unsigned int attr, struct object *root );
  143. static struct object *pipe_server_open_file( struct object *obj, unsigned int access,
  144. unsigned int sharing, unsigned int options );
  145. static void pipe_server_destroy( struct object *obj);
  146. static void pipe_server_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
  147. static const struct object_ops pipe_server_ops =
  148. {
  149. sizeof(struct pipe_server), /* size */
  150. &file_type, /* type */
  151. pipe_server_dump, /* dump */
  152. add_queue, /* add_queue */
  153. remove_queue, /* remove_queue */
  154. default_fd_signaled, /* signaled */
  155. no_satisfied, /* satisfied */
  156. no_signal, /* signal */
  157. pipe_end_get_fd, /* get_fd */
  158. default_map_access, /* map_access */
  159. pipe_end_get_sd, /* get_sd */
  160. pipe_end_set_sd, /* set_sd */
  161. pipe_end_get_full_name, /* get_full_name */
  162. pipe_server_lookup_name, /* lookup_name */
  163. no_link_name, /* link_name */
  164. NULL, /* unlink_name */
  165. pipe_server_open_file, /* open_file */
  166. no_kernel_obj_list, /* get_kernel_obj_list */
  167. default_fd_get_fast_sync, /* get_fast_sync */
  168. no_close_handle, /* close_handle */
  169. pipe_server_destroy /* destroy */
  170. };
  171. static const struct fd_ops pipe_server_fd_ops =
  172. {
  173. default_fd_get_poll_events, /* get_poll_events */
  174. default_poll_event, /* poll_event */
  175. pipe_end_get_fd_type, /* get_fd_type */
  176. pipe_end_read, /* read */
  177. pipe_end_write, /* write */
  178. pipe_end_flush, /* flush */
  179. pipe_end_get_file_info, /* get_file_info */
  180. pipe_end_get_volume_info, /* get_volume_info */
  181. pipe_server_ioctl, /* ioctl */
  182. default_fd_cancel_async, /* cancel_async */
  183. no_fd_queue_async, /* queue_async */
  184. pipe_end_reselect_async /* reselect_async */
  185. };
  186. /* client end functions */
  187. static void pipe_client_dump( struct object *obj, int verbose );
  188. static void pipe_client_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
  189. static const struct object_ops pipe_client_ops =
  190. {
  191. sizeof(struct pipe_end), /* size */
  192. &file_type, /* type */
  193. pipe_client_dump, /* dump */
  194. add_queue, /* add_queue */
  195. remove_queue, /* remove_queue */
  196. default_fd_signaled, /* signaled */
  197. no_satisfied, /* satisfied */
  198. no_signal, /* signal */
  199. pipe_end_get_fd, /* get_fd */
  200. default_map_access, /* map_access */
  201. pipe_end_get_sd, /* get_sd */
  202. pipe_end_set_sd, /* set_sd */
  203. pipe_end_get_full_name, /* get_full_name */
  204. no_lookup_name, /* lookup_name */
  205. no_link_name, /* link_name */
  206. NULL, /* unlink_name */
  207. no_open_file, /* open_file */
  208. no_kernel_obj_list, /* get_kernel_obj_list */
  209. default_fd_get_fast_sync, /* get_fast_sync */
  210. no_close_handle, /* close_handle */
  211. pipe_end_destroy /* destroy */
  212. };
  213. static const struct fd_ops pipe_client_fd_ops =
  214. {
  215. default_fd_get_poll_events, /* get_poll_events */
  216. default_poll_event, /* poll_event */
  217. pipe_end_get_fd_type, /* get_fd_type */
  218. pipe_end_read, /* read */
  219. pipe_end_write, /* write */
  220. pipe_end_flush, /* flush */
  221. pipe_end_get_file_info, /* get_file_info */
  222. pipe_end_get_volume_info, /* get_volume_info */
  223. pipe_client_ioctl, /* ioctl */
  224. default_fd_cancel_async, /* cancel_async */
  225. no_fd_queue_async, /* queue_async */
  226. pipe_end_reselect_async /* reselect_async */
  227. };
  228. static void named_pipe_device_dump( struct object *obj, int verbose );
  229. static struct object *named_pipe_device_lookup_name( struct object *obj,
  230. struct unicode_str *name, unsigned int attr, struct object *root );
  231. static struct object *named_pipe_device_open_file( struct object *obj, unsigned int access,
  232. unsigned int sharing, unsigned int options );
  233. static void named_pipe_device_destroy( struct object *obj );
  234. static const struct object_ops named_pipe_device_ops =
  235. {
  236. sizeof(struct named_pipe_device), /* size */
  237. &device_type, /* type */
  238. named_pipe_device_dump, /* dump */
  239. no_add_queue, /* add_queue */
  240. NULL, /* remove_queue */
  241. NULL, /* signaled */
  242. no_satisfied, /* satisfied */
  243. no_signal, /* signal */
  244. no_get_fd, /* get_fd */
  245. default_map_access, /* map_access */
  246. default_get_sd, /* get_sd */
  247. default_set_sd, /* set_sd */
  248. default_get_full_name, /* get_full_name */
  249. named_pipe_device_lookup_name, /* lookup_name */
  250. directory_link_name, /* link_name */
  251. default_unlink_name, /* unlink_name */
  252. named_pipe_device_open_file, /* open_file */
  253. no_kernel_obj_list, /* get_kernel_obj_list */
  254. no_get_fast_sync, /* get_fast_sync */
  255. no_close_handle, /* close_handle */
  256. named_pipe_device_destroy /* destroy */
  257. };
  258. static void named_pipe_device_file_dump( struct object *obj, int verbose );
  259. static struct fd *named_pipe_device_file_get_fd( struct object *obj );
  260. static WCHAR *named_pipe_device_file_get_full_name( struct object *obj, data_size_t *len );
  261. static void named_pipe_device_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
  262. static enum server_fd_type named_pipe_device_file_get_fd_type( struct fd *fd );
  263. static void named_pipe_device_file_destroy( struct object *obj );
  264. static const struct object_ops named_pipe_device_file_ops =
  265. {
  266. sizeof(struct named_pipe_device_file), /* size */
  267. &file_type, /* type */
  268. named_pipe_device_file_dump, /* dump */
  269. add_queue, /* add_queue */
  270. remove_queue, /* remove_queue */
  271. default_fd_signaled, /* signaled */
  272. no_satisfied, /* satisfied */
  273. no_signal, /* signal */
  274. named_pipe_device_file_get_fd, /* get_fd */
  275. default_map_access, /* map_access */
  276. default_get_sd, /* get_sd */
  277. default_set_sd, /* set_sd */
  278. named_pipe_device_file_get_full_name, /* get_full_name */
  279. no_lookup_name, /* lookup_name */
  280. no_link_name, /* link_name */
  281. NULL, /* unlink_name */
  282. no_open_file, /* open_file */
  283. no_kernel_obj_list, /* get_kernel_obj_list */
  284. default_fd_get_fast_sync, /* get_fast_sync */
  285. no_close_handle, /* close_handle */
  286. named_pipe_device_file_destroy /* destroy */
  287. };
  288. static const struct fd_ops named_pipe_device_fd_ops =
  289. {
  290. default_fd_get_poll_events, /* get_poll_events */
  291. default_poll_event, /* poll_event */
  292. named_pipe_device_file_get_fd_type, /* get_fd_type */
  293. no_fd_read, /* read */
  294. no_fd_write, /* write */
  295. no_fd_flush, /* flush */
  296. default_fd_get_file_info, /* get_file_info */
  297. no_fd_get_volume_info, /* get_volume_info */
  298. named_pipe_device_ioctl, /* ioctl */
  299. default_fd_cancel_async, /* cancel_async */
  300. default_fd_queue_async, /* queue_async */
  301. default_fd_reselect_async /* reselect_async */
  302. };
  303. static void named_pipe_dump( struct object *obj, int verbose )
  304. {
  305. fputs( "Named pipe\n", stderr );
  306. }
  307. static unsigned int named_pipe_map_access( struct object *obj, unsigned int access )
  308. {
  309. if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ;
  310. if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | FILE_CREATE_PIPE_INSTANCE;
  311. if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
  312. if (access & GENERIC_ALL) access |= STANDARD_RIGHTS_ALL;
  313. return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
  314. }
  315. static WCHAR *named_pipe_get_full_name( struct object *obj, data_size_t *ret_len )
  316. {
  317. WCHAR *ret;
  318. if (!(ret = default_get_full_name( obj, ret_len )))
  319. set_error( STATUS_OBJECT_PATH_INVALID );
  320. return ret;
  321. }
  322. static void pipe_server_dump( struct object *obj, int verbose )
  323. {
  324. struct pipe_server *server = (struct pipe_server *) obj;
  325. assert( obj->ops == &pipe_server_ops );
  326. fprintf( stderr, "Named pipe server pipe=%p state=%d\n", server->pipe_end.pipe,
  327. server->pipe_end.state );
  328. }
  329. static void pipe_client_dump( struct object *obj, int verbose )
  330. {
  331. struct pipe_end *client = (struct pipe_end *) obj;
  332. assert( obj->ops == &pipe_client_ops );
  333. fprintf( stderr, "Named pipe client server=%p\n", client->connection );
  334. }
  335. static void named_pipe_destroy( struct object *obj)
  336. {
  337. struct named_pipe *pipe = (struct named_pipe *) obj;
  338. assert( list_empty( &pipe->listeners ) );
  339. assert( !pipe->instances );
  340. free_async_queue( &pipe->waiters );
  341. }
  342. static struct fd *pipe_end_get_fd( struct object *obj )
  343. {
  344. struct pipe_end *pipe_end = (struct pipe_end *) obj;
  345. return (struct fd *) grab_object( pipe_end->fd );
  346. }
  347. static struct pipe_message *queue_message( struct pipe_end *pipe_end, struct iosb *iosb )
  348. {
  349. struct pipe_message *message;
  350. if (!(message = mem_alloc( sizeof(*message) ))) return NULL;
  351. message->iosb = (struct iosb *)grab_object( iosb );
  352. message->async = NULL;
  353. message->read_pos = 0;
  354. list_add_tail( &pipe_end->message_queue, &message->entry );
  355. return message;
  356. }
  357. static void wake_message( struct pipe_message *message, data_size_t result )
  358. {
  359. struct async *async = message->async;
  360. message->async = NULL;
  361. if (!async) return;
  362. async_request_complete( async, STATUS_SUCCESS, result, 0, NULL );
  363. release_object( async );
  364. }
  365. static void free_message( struct pipe_message *message )
  366. {
  367. list_remove( &message->entry );
  368. if (message->iosb) release_object( message->iosb );
  369. free( message );
  370. }
  371. static void pipe_end_disconnect( struct pipe_end *pipe_end, unsigned int status )
  372. {
  373. struct pipe_end *connection = pipe_end->connection;
  374. struct pipe_message *message, *next;
  375. struct async *async;
  376. pipe_end->connection = NULL;
  377. pipe_end->state = status == STATUS_PIPE_DISCONNECTED
  378. ? FILE_PIPE_DISCONNECTED_STATE : FILE_PIPE_CLOSING_STATE;
  379. fd_async_wake_up( pipe_end->fd, ASYNC_TYPE_WAIT, status );
  380. async_wake_up( &pipe_end->read_q, status );
  381. LIST_FOR_EACH_ENTRY_SAFE( message, next, &pipe_end->message_queue, struct pipe_message, entry )
  382. {
  383. async = message->async;
  384. if (async || status == STATUS_PIPE_DISCONNECTED) free_message( message );
  385. if (!async) continue;
  386. async_terminate( async, status );
  387. release_object( async );
  388. }
  389. if (status == STATUS_PIPE_DISCONNECTED) set_fd_signaled( pipe_end->fd, 0 );
  390. if (connection)
  391. {
  392. connection->connection = NULL;
  393. pipe_end_disconnect( connection, status );
  394. }
  395. }
  396. static void pipe_end_destroy( struct object *obj )
  397. {
  398. struct pipe_end *pipe_end = (struct pipe_end *)obj;
  399. struct pipe_message *message;
  400. pipe_end_disconnect( pipe_end, STATUS_PIPE_BROKEN );
  401. while (!list_empty( &pipe_end->message_queue ))
  402. {
  403. message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
  404. assert( !message->async );
  405. free_message( message );
  406. }
  407. free_async_queue( &pipe_end->read_q );
  408. free_async_queue( &pipe_end->write_q );
  409. if (pipe_end->fd) release_object( pipe_end->fd );
  410. if (pipe_end->pipe) release_object( pipe_end->pipe );
  411. }
  412. static struct object *pipe_server_lookup_name( struct object *obj, struct unicode_str *name,
  413. unsigned int attr, struct object *root )
  414. {
  415. if (name && name->len)
  416. set_error( STATUS_OBJECT_NAME_INVALID );
  417. return NULL;
  418. }
  419. static struct object *pipe_server_open_file( struct object *obj, unsigned int access,
  420. unsigned int sharing, unsigned int options )
  421. {
  422. struct pipe_server *server = (struct pipe_server *)obj;
  423. return server->pipe_end.pipe->obj.ops->open_file( &server->pipe_end.pipe->obj, access, sharing, options );
  424. }
  425. static void pipe_server_destroy( struct object *obj )
  426. {
  427. struct pipe_server *server = (struct pipe_server *)obj;
  428. struct named_pipe *pipe = server->pipe_end.pipe;
  429. assert( obj->ops == &pipe_server_ops );
  430. assert( pipe->instances );
  431. if (!--pipe->instances) unlink_named_object( &pipe->obj );
  432. if (server->pipe_end.state == FILE_PIPE_LISTENING_STATE)
  433. list_remove( &server->entry );
  434. free_async_queue( &server->listen_q );
  435. pipe_end_destroy( obj );
  436. }
  437. static void named_pipe_device_dump( struct object *obj, int verbose )
  438. {
  439. fputs( "Named pipe device\n", stderr );
  440. }
  441. static struct object *named_pipe_device_lookup_name( struct object *obj, struct unicode_str *name,
  442. unsigned int attr, struct object *root )
  443. {
  444. struct named_pipe_device *device = (struct named_pipe_device*)obj;
  445. struct object *found;
  446. assert( obj->ops == &named_pipe_device_ops );
  447. assert( device->pipes );
  448. if (!name) return NULL; /* open the device itself */
  449. if ((found = find_object( device->pipes, name, attr | OBJ_CASE_INSENSITIVE )))
  450. name->len = 0;
  451. return found;
  452. }
  453. static struct object *named_pipe_device_open_file( struct object *obj, unsigned int access,
  454. unsigned int sharing, unsigned int options )
  455. {
  456. struct named_pipe_device_file *file;
  457. if (!(file = alloc_object( &named_pipe_device_file_ops ))) return NULL;
  458. file->device = (struct named_pipe_device *)grab_object( obj );
  459. if (!(file->fd = alloc_pseudo_fd( &named_pipe_device_fd_ops, obj, options )))
  460. {
  461. release_object( file );
  462. return NULL;
  463. }
  464. allow_fd_caching( file->fd );
  465. return &file->obj;
  466. }
  467. static void named_pipe_device_destroy( struct object *obj )
  468. {
  469. struct named_pipe_device *device = (struct named_pipe_device*)obj;
  470. assert( obj->ops == &named_pipe_device_ops );
  471. free( device->pipes );
  472. }
  473. struct object *create_named_pipe_device( struct object *root, const struct unicode_str *name,
  474. unsigned int attr, const struct security_descriptor *sd )
  475. {
  476. struct named_pipe_device *dev;
  477. if ((dev = create_named_object( root, &named_pipe_device_ops, name, attr, sd )) &&
  478. get_error() != STATUS_OBJECT_NAME_EXISTS)
  479. {
  480. dev->pipes = NULL;
  481. if (!(dev->pipes = create_namespace( 7 )))
  482. {
  483. release_object( dev );
  484. dev = NULL;
  485. }
  486. }
  487. return &dev->obj;
  488. }
  489. static void named_pipe_device_file_dump( struct object *obj, int verbose )
  490. {
  491. struct named_pipe_device_file *file = (struct named_pipe_device_file *)obj;
  492. fprintf( stderr, "File on named pipe device %p\n", file->device );
  493. }
  494. static struct fd *named_pipe_device_file_get_fd( struct object *obj )
  495. {
  496. struct named_pipe_device_file *file = (struct named_pipe_device_file *)obj;
  497. return (struct fd *)grab_object( file->fd );
  498. }
  499. static WCHAR *named_pipe_device_file_get_full_name( struct object *obj, data_size_t *len )
  500. {
  501. struct named_pipe_device_file *file = (struct named_pipe_device_file *)obj;
  502. return file->device->obj.ops->get_full_name( &file->device->obj, len );
  503. }
  504. static enum server_fd_type named_pipe_device_file_get_fd_type( struct fd *fd )
  505. {
  506. return FD_TYPE_DEVICE;
  507. }
  508. static void named_pipe_device_file_destroy( struct object *obj )
  509. {
  510. struct named_pipe_device_file *file = (struct named_pipe_device_file*)obj;
  511. assert( obj->ops == &named_pipe_device_file_ops );
  512. if (file->fd) release_object( file->fd );
  513. release_object( file->device );
  514. }
  515. static void pipe_end_flush( struct fd *fd, struct async *async )
  516. {
  517. struct pipe_end *pipe_end = get_fd_user( fd );
  518. if (!pipe_end->pipe)
  519. {
  520. set_error( STATUS_PIPE_DISCONNECTED );
  521. return;
  522. }
  523. if (pipe_end->connection && !list_empty( &pipe_end->connection->message_queue ))
  524. {
  525. fd_queue_async( pipe_end->fd, async, ASYNC_TYPE_WAIT );
  526. set_error( STATUS_PENDING );
  527. }
  528. }
  529. static void pipe_end_get_file_info( struct fd *fd, obj_handle_t handle, unsigned int info_class )
  530. {
  531. struct pipe_end *pipe_end = get_fd_user( fd );
  532. struct named_pipe *pipe = pipe_end->pipe;
  533. switch (info_class)
  534. {
  535. case FileNameInformation:
  536. {
  537. FILE_NAME_INFORMATION *name_info;
  538. data_size_t name_len, reply_size;
  539. const WCHAR *name;
  540. if (get_reply_max_size() < sizeof(*name_info))
  541. {
  542. set_error( STATUS_INFO_LENGTH_MISMATCH );
  543. return;
  544. }
  545. /* FIXME: We should be able to return on unlinked pipe */
  546. if (!pipe || !(name = get_object_name( &pipe->obj, &name_len )))
  547. {
  548. set_error( STATUS_PIPE_DISCONNECTED );
  549. return;
  550. }
  551. reply_size = offsetof( FILE_NAME_INFORMATION, FileName[name_len/sizeof(WCHAR) + 1] );
  552. if (reply_size > get_reply_max_size())
  553. {
  554. reply_size = get_reply_max_size();
  555. set_error( STATUS_BUFFER_OVERFLOW );
  556. }
  557. if (!(name_info = set_reply_data_size( reply_size ))) return;
  558. name_info->FileNameLength = name_len + sizeof(WCHAR);
  559. name_info->FileName[0] = '\\';
  560. reply_size -= offsetof( FILE_NAME_INFORMATION, FileName[1] );
  561. if (reply_size) memcpy( &name_info->FileName[1], name, reply_size );
  562. break;
  563. }
  564. case FilePipeInformation:
  565. {
  566. FILE_PIPE_INFORMATION *pipe_info;
  567. if (!(get_handle_access( current->process, handle) & FILE_READ_ATTRIBUTES))
  568. {
  569. set_error( STATUS_ACCESS_DENIED );
  570. return;
  571. }
  572. if (get_reply_max_size() < sizeof(*pipe_info))
  573. {
  574. set_error( STATUS_INFO_LENGTH_MISMATCH );
  575. return;
  576. }
  577. if (!pipe)
  578. {
  579. set_error( STATUS_PIPE_DISCONNECTED );
  580. return;
  581. }
  582. if (!(pipe_info = set_reply_data_size( sizeof(*pipe_info) ))) return;
  583. pipe_info->ReadMode = (pipe_end->flags & NAMED_PIPE_MESSAGE_STREAM_READ)
  584. ? FILE_PIPE_MESSAGE_MODE : FILE_PIPE_BYTE_STREAM_MODE;
  585. pipe_info->CompletionMode = (pipe_end->flags & NAMED_PIPE_NONBLOCKING_MODE)
  586. ? FILE_PIPE_COMPLETE_OPERATION : FILE_PIPE_QUEUE_OPERATION;
  587. break;
  588. }
  589. case FilePipeLocalInformation:
  590. {
  591. FILE_PIPE_LOCAL_INFORMATION *pipe_info;
  592. struct pipe_message *message;
  593. data_size_t avail = 0;
  594. if (!(get_handle_access( current->process, handle) & FILE_READ_ATTRIBUTES))
  595. {
  596. set_error( STATUS_ACCESS_DENIED );
  597. return;
  598. }
  599. if (get_reply_max_size() < sizeof(*pipe_info))
  600. {
  601. set_error( STATUS_INFO_LENGTH_MISMATCH );
  602. return;
  603. }
  604. if (!pipe)
  605. {
  606. set_error( STATUS_PIPE_DISCONNECTED );
  607. return;
  608. }
  609. if (!(pipe_info = set_reply_data_size( sizeof(*pipe_info) ))) return;
  610. pipe_info->NamedPipeType = pipe->message_mode;
  611. switch (pipe->sharing)
  612. {
  613. case FILE_SHARE_READ:
  614. pipe_info->NamedPipeConfiguration = FILE_PIPE_OUTBOUND;
  615. break;
  616. case FILE_SHARE_WRITE:
  617. pipe_info->NamedPipeConfiguration = FILE_PIPE_INBOUND;
  618. break;
  619. case FILE_SHARE_READ | FILE_SHARE_WRITE:
  620. pipe_info->NamedPipeConfiguration = FILE_PIPE_FULL_DUPLEX;
  621. break;
  622. }
  623. pipe_info->MaximumInstances = pipe->maxinstances;
  624. pipe_info->CurrentInstances = pipe->instances;
  625. pipe_info->InboundQuota = pipe->insize;
  626. LIST_FOR_EACH_ENTRY( message, &pipe_end->message_queue, struct pipe_message, entry )
  627. avail += message->iosb->in_size - message->read_pos;
  628. pipe_info->ReadDataAvailable = avail;
  629. pipe_info->OutboundQuota = pipe->outsize;
  630. pipe_info->WriteQuotaAvailable = 0; /* FIXME */
  631. pipe_info->NamedPipeState = pipe_end->state;
  632. pipe_info->NamedPipeEnd = pipe_end->obj.ops == &pipe_server_ops
  633. ? FILE_PIPE_SERVER_END : FILE_PIPE_CLIENT_END;
  634. break;
  635. }
  636. default:
  637. default_fd_get_file_info( fd, handle, info_class );
  638. }
  639. }
  640. static struct security_descriptor *pipe_end_get_sd( struct object *obj )
  641. {
  642. struct pipe_end *pipe_end = (struct pipe_end *) obj;
  643. if (pipe_end->pipe) return default_get_sd( &pipe_end->pipe->obj );
  644. set_error( STATUS_PIPE_DISCONNECTED );
  645. return NULL;
  646. }
  647. static int pipe_end_set_sd( struct object *obj, const struct security_descriptor *sd,
  648. unsigned int set_info )
  649. {
  650. struct pipe_end *pipe_end = (struct pipe_end *) obj;
  651. if (pipe_end->pipe) return default_set_sd( &pipe_end->pipe->obj, sd, set_info );
  652. set_error( STATUS_PIPE_DISCONNECTED );
  653. return 0;
  654. }
  655. static WCHAR *pipe_end_get_full_name( struct object *obj, data_size_t *len )
  656. {
  657. struct pipe_end *pipe_end = (struct pipe_end *) obj;
  658. return pipe_end->pipe->obj.ops->get_full_name( &pipe_end->pipe->obj, len );
  659. }
  660. static void pipe_end_get_volume_info( struct fd *fd, struct async *async, unsigned int info_class )
  661. {
  662. switch (info_class)
  663. {
  664. case FileFsDeviceInformation:
  665. {
  666. static const FILE_FS_DEVICE_INFORMATION device_info =
  667. {
  668. FILE_DEVICE_NAMED_PIPE,
  669. FILE_DEVICE_ALLOW_APPCONTAINER_TRAVERSAL
  670. };
  671. if (get_reply_max_size() >= sizeof(device_info))
  672. set_reply_data( &device_info, sizeof(device_info) );
  673. else
  674. set_error( STATUS_BUFFER_TOO_SMALL );
  675. break;
  676. }
  677. default:
  678. set_error( STATUS_NOT_IMPLEMENTED );
  679. }
  680. }
  681. static void message_queue_read( struct pipe_end *pipe_end, struct async *async )
  682. {
  683. struct iosb *iosb = async_get_iosb( async );
  684. unsigned int status = STATUS_SUCCESS;
  685. struct pipe_message *message;
  686. data_size_t out_size;
  687. if (pipe_end->flags & NAMED_PIPE_MESSAGE_STREAM_READ)
  688. {
  689. message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
  690. out_size = min( iosb->out_size, message->iosb->in_size - message->read_pos );
  691. if (message->read_pos + out_size < message->iosb->in_size)
  692. status = STATUS_BUFFER_OVERFLOW;
  693. }
  694. else
  695. {
  696. data_size_t avail = 0;
  697. LIST_FOR_EACH_ENTRY( message, &pipe_end->message_queue, struct pipe_message, entry )
  698. {
  699. avail += message->iosb->in_size - message->read_pos;
  700. if (avail >= iosb->out_size) break;
  701. }
  702. out_size = min( iosb->out_size, avail );
  703. }
  704. message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
  705. if (!message->read_pos && message->iosb->in_size == iosb->out_size) /* fast path */
  706. {
  707. async_request_complete( async, status, out_size, out_size, message->iosb->in_data );
  708. message->iosb->in_data = NULL;
  709. wake_message( message, message->iosb->in_size );
  710. free_message( message );
  711. }
  712. else
  713. {
  714. data_size_t write_pos = 0, writing;
  715. char *buf = NULL;
  716. if (out_size && !(buf = malloc( out_size )))
  717. {
  718. async_terminate( async, STATUS_NO_MEMORY );
  719. release_object( iosb );
  720. return;
  721. }
  722. do
  723. {
  724. message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
  725. writing = min( out_size - write_pos, message->iosb->in_size - message->read_pos );
  726. if (writing) memcpy( buf + write_pos, (const char *)message->iosb->in_data + message->read_pos, writing );
  727. write_pos += writing;
  728. message->read_pos += writing;
  729. if (message->read_pos == message->iosb->in_size)
  730. {
  731. wake_message(message, message->iosb->in_size);
  732. free_message(message);
  733. }
  734. } while (write_pos < out_size);
  735. async_request_complete( async, status, out_size, out_size, buf );
  736. }
  737. release_object( iosb );
  738. }
  739. /* We call async_terminate in our reselect implementation, which causes recursive reselect.
  740. * We're not interested in such reselect calls, so we ignore them. */
  741. static int ignore_reselect;
  742. static void reselect_write_queue( struct pipe_end *pipe_end );
  743. static void reselect_read_queue( struct pipe_end *pipe_end, int reselect_write )
  744. {
  745. struct async *async;
  746. ignore_reselect = 1;
  747. while (!list_empty( &pipe_end->message_queue ) && (async = find_pending_async( &pipe_end->read_q )))
  748. {
  749. message_queue_read( pipe_end, async );
  750. release_object( async );
  751. reselect_write = 1;
  752. }
  753. ignore_reselect = 0;
  754. if (pipe_end->connection)
  755. {
  756. if (list_empty( &pipe_end->message_queue ))
  757. fd_async_wake_up( pipe_end->connection->fd, ASYNC_TYPE_WAIT, STATUS_SUCCESS );
  758. else if (reselect_write)
  759. reselect_write_queue( pipe_end->connection );
  760. }
  761. }
  762. static void reselect_write_queue( struct pipe_end *pipe_end )
  763. {
  764. struct pipe_message *message, *next;
  765. struct pipe_end *reader = pipe_end->connection;
  766. data_size_t avail = 0;
  767. if (!reader) return;
  768. ignore_reselect = 1;
  769. LIST_FOR_EACH_ENTRY_SAFE( message, next, &reader->message_queue, struct pipe_message, entry )
  770. {
  771. if (message->async && message->iosb->status != STATUS_PENDING)
  772. {
  773. release_object( message->async );
  774. message->async = NULL;
  775. free_message( message );
  776. }
  777. else
  778. {
  779. avail += message->iosb->in_size - message->read_pos;
  780. if (message->async && (avail <= reader->buffer_size || !message->iosb->in_size))
  781. {
  782. wake_message( message, message->iosb->in_size );
  783. }
  784. else if (message->async && (pipe_end->flags & NAMED_PIPE_NONBLOCKING_MODE))
  785. {
  786. wake_message( message, message->read_pos );
  787. free_message( message );
  788. }
  789. }
  790. }
  791. ignore_reselect = 0;
  792. reselect_read_queue( reader, 0 );
  793. }
  794. static void pipe_end_read( struct fd *fd, struct async *async, file_pos_t pos )
  795. {
  796. struct pipe_end *pipe_end = get_fd_user( fd );
  797. switch (pipe_end->state)
  798. {
  799. case FILE_PIPE_CONNECTED_STATE:
  800. if ((pipe_end->flags & NAMED_PIPE_NONBLOCKING_MODE) && list_empty( &pipe_end->message_queue ))
  801. {
  802. set_error( STATUS_PIPE_EMPTY );
  803. return;
  804. }
  805. break;
  806. case FILE_PIPE_DISCONNECTED_STATE:
  807. set_error( STATUS_PIPE_DISCONNECTED );
  808. return;
  809. case FILE_PIPE_LISTENING_STATE:
  810. set_error( STATUS_PIPE_LISTENING );
  811. return;
  812. case FILE_PIPE_CLOSING_STATE:
  813. if (!list_empty( &pipe_end->message_queue )) break;
  814. set_error( STATUS_PIPE_BROKEN );
  815. return;
  816. }
  817. queue_async( &pipe_end->read_q, async );
  818. reselect_read_queue( pipe_end, 0 );
  819. set_error( STATUS_PENDING );
  820. }
  821. static void pipe_end_write( struct fd *fd, struct async *async, file_pos_t pos )
  822. {
  823. struct pipe_end *pipe_end = get_fd_user( fd );
  824. struct pipe_message *message;
  825. struct iosb *iosb;
  826. switch (pipe_end->state)
  827. {
  828. case FILE_PIPE_CONNECTED_STATE:
  829. break;
  830. case FILE_PIPE_DISCONNECTED_STATE:
  831. set_error( STATUS_PIPE_DISCONNECTED );
  832. return;
  833. case FILE_PIPE_LISTENING_STATE:
  834. set_error( STATUS_PIPE_LISTENING );
  835. return;
  836. case FILE_PIPE_CLOSING_STATE:
  837. set_error( STATUS_PIPE_CLOSING );
  838. return;
  839. }
  840. if (!pipe_end->pipe->message_mode && !get_req_data_size()) return;
  841. iosb = async_get_iosb( async );
  842. message = queue_message( pipe_end->connection, iosb );
  843. release_object( iosb );
  844. if (!message) return;
  845. message->async = (struct async *)grab_object( async );
  846. queue_async( &pipe_end->write_q, async );
  847. reselect_read_queue( pipe_end->connection, 1 );
  848. set_error( STATUS_PENDING );
  849. }
  850. static void pipe_end_reselect_async( struct fd *fd, struct async_queue *queue )
  851. {
  852. struct pipe_end *pipe_end = get_fd_user( fd );
  853. if (ignore_reselect) return;
  854. if (&pipe_end->write_q == queue)
  855. reselect_write_queue( pipe_end );
  856. else if (&pipe_end->read_q == queue)
  857. reselect_read_queue( pipe_end, 0 );
  858. }
  859. static enum server_fd_type pipe_end_get_fd_type( struct fd *fd )
  860. {
  861. return FD_TYPE_PIPE;
  862. }
  863. static void pipe_end_peek( struct pipe_end *pipe_end )
  864. {
  865. unsigned reply_size = get_reply_max_size();
  866. FILE_PIPE_PEEK_BUFFER *buffer;
  867. struct pipe_message *message;
  868. data_size_t avail = 0;
  869. data_size_t message_length = 0;
  870. if (reply_size < offsetof( FILE_PIPE_PEEK_BUFFER, Data ))
  871. {
  872. set_error( STATUS_INFO_LENGTH_MISMATCH );
  873. return;
  874. }
  875. reply_size -= offsetof( FILE_PIPE_PEEK_BUFFER, Data );
  876. switch (pipe_end->state)
  877. {
  878. case FILE_PIPE_CONNECTED_STATE:
  879. break;
  880. case FILE_PIPE_CLOSING_STATE:
  881. if (!list_empty( &pipe_end->message_queue )) break;
  882. set_error( STATUS_PIPE_BROKEN );
  883. return;
  884. default:
  885. set_error( pipe_end->pipe ? STATUS_INVALID_PIPE_STATE : STATUS_PIPE_DISCONNECTED );
  886. return;
  887. }
  888. LIST_FOR_EACH_ENTRY( message, &pipe_end->message_queue, struct pipe_message, entry )
  889. avail += message->iosb->in_size - message->read_pos;
  890. reply_size = min( reply_size, avail );
  891. if (avail && pipe_end->pipe->message_mode)
  892. {
  893. message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
  894. message_length = message->iosb->in_size - message->read_pos;
  895. reply_size = min( reply_size, message_length );
  896. }
  897. if (!(buffer = set_reply_data_size( offsetof( FILE_PIPE_PEEK_BUFFER, Data[reply_size] )))) return;
  898. buffer->NamedPipeState = pipe_end->state;
  899. buffer->ReadDataAvailable = avail;
  900. buffer->NumberOfMessages = 0; /* FIXME */
  901. buffer->MessageLength = message_length;
  902. if (reply_size)
  903. {
  904. data_size_t write_pos = 0, writing;
  905. LIST_FOR_EACH_ENTRY( message, &pipe_end->message_queue, struct pipe_message, entry )
  906. {
  907. writing = min( reply_size - write_pos, message->iosb->in_size - message->read_pos );
  908. memcpy( buffer->Data + write_pos, (const char *)message->iosb->in_data + message->read_pos,
  909. writing );
  910. write_pos += writing;
  911. if (write_pos == reply_size) break;
  912. }
  913. }
  914. if (message_length > reply_size) set_error( STATUS_BUFFER_OVERFLOW );
  915. }
  916. static void pipe_end_transceive( struct pipe_end *pipe_end, struct async *async )
  917. {
  918. struct pipe_message *message;
  919. struct iosb *iosb;
  920. if (!pipe_end->connection)
  921. {
  922. set_error( pipe_end->pipe ? STATUS_INVALID_PIPE_STATE : STATUS_PIPE_DISCONNECTED );
  923. return;
  924. }
  925. if (!(pipe_end->flags & NAMED_PIPE_MESSAGE_STREAM_READ))
  926. {
  927. set_error( STATUS_INVALID_READ_MODE );
  928. return;
  929. }
  930. /* not allowed if we already have read data buffered */
  931. if (!list_empty( &pipe_end->message_queue ))
  932. {
  933. set_error( STATUS_PIPE_BUSY );
  934. return;
  935. }
  936. iosb = async_get_iosb( async );
  937. /* ignore output buffer copy transferred because of METHOD_NEITHER */
  938. iosb->in_size -= iosb->out_size;
  939. /* transaction never blocks on write, so just queue a message without async */
  940. message = queue_message( pipe_end->connection, iosb );
  941. release_object( iosb );
  942. if (!message) return;
  943. reselect_read_queue( pipe_end->connection, 0 );
  944. queue_async( &pipe_end->read_q, async );
  945. reselect_read_queue( pipe_end, 0 );
  946. set_error( STATUS_PENDING );
  947. }
  948. static void pipe_end_get_connection_attribute( struct pipe_end *pipe_end )
  949. {
  950. const char *attr = get_req_data();
  951. data_size_t value_size, attr_size = get_req_data_size();
  952. void *value;
  953. if (attr_size == sizeof("ClientProcessId") && !memcmp( attr, "ClientProcessId", attr_size ))
  954. {
  955. value = &pipe_end->client_pid;
  956. value_size = sizeof(pipe_end->client_pid);
  957. }
  958. else if (attr_size == sizeof("ServerProcessId") && !memcmp( attr, "ServerProcessId", attr_size ))
  959. {
  960. value = &pipe_end->server_pid;
  961. value_size = sizeof(pipe_end->server_pid);
  962. }
  963. else
  964. {
  965. set_error( STATUS_ILLEGAL_FUNCTION );
  966. return;
  967. }
  968. if (get_reply_max_size() < value_size)
  969. {
  970. set_error( STATUS_INFO_LENGTH_MISMATCH );
  971. return;
  972. }
  973. set_reply_data( value, value_size );
  974. }
  975. static void pipe_end_ioctl( struct pipe_end *pipe_end, ioctl_code_t code, struct async *async )
  976. {
  977. switch(code)
  978. {
  979. case FSCTL_PIPE_GET_CONNECTION_ATTRIBUTE:
  980. pipe_end_get_connection_attribute( pipe_end );
  981. break;
  982. case FSCTL_PIPE_PEEK:
  983. pipe_end_peek( pipe_end );
  984. break;
  985. case FSCTL_PIPE_TRANSCEIVE:
  986. pipe_end_transceive( pipe_end, async );
  987. break;
  988. default:
  989. default_fd_ioctl( pipe_end->fd, code, async );
  990. }
  991. }
  992. static void pipe_server_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
  993. {
  994. struct pipe_server *server = get_fd_user( fd );
  995. switch(code)
  996. {
  997. case FSCTL_PIPE_LISTEN:
  998. switch(server->pipe_end.state)
  999. {
  1000. case FILE_PIPE_LISTENING_STATE:
  1001. break;
  1002. case FILE_PIPE_DISCONNECTED_STATE:
  1003. server->pipe_end.state = FILE_PIPE_LISTENING_STATE;
  1004. list_add_tail( &server->pipe_end.pipe->listeners, &server->entry );
  1005. break;
  1006. case FILE_PIPE_CONNECTED_STATE:
  1007. set_error( STATUS_PIPE_CONNECTED );
  1008. return;
  1009. case FILE_PIPE_CLOSING_STATE:
  1010. set_error( STATUS_PIPE_CLOSING );
  1011. return;
  1012. }
  1013. if (server->pipe_end.flags & NAMED_PIPE_NONBLOCKING_MODE)
  1014. {
  1015. set_error( STATUS_PIPE_LISTENING );
  1016. return;
  1017. }
  1018. queue_async( &server->listen_q, async );
  1019. async_wake_up( &server->pipe_end.pipe->waiters, STATUS_SUCCESS );
  1020. set_error( STATUS_PENDING );
  1021. return;
  1022. case FSCTL_PIPE_DISCONNECT:
  1023. switch(server->pipe_end.state)
  1024. {
  1025. case FILE_PIPE_CONNECTED_STATE:
  1026. /* dump the client connection - all data is lost */
  1027. assert( server->pipe_end.connection );
  1028. release_object( server->pipe_end.connection->pipe );
  1029. server->pipe_end.connection->pipe = NULL;
  1030. break;
  1031. case FILE_PIPE_CLOSING_STATE:
  1032. break;
  1033. case FILE_PIPE_LISTENING_STATE:
  1034. set_error( STATUS_PIPE_LISTENING );
  1035. return;
  1036. case FILE_PIPE_DISCONNECTED_STATE:
  1037. set_error( STATUS_PIPE_DISCONNECTED );
  1038. return;
  1039. }
  1040. pipe_end_disconnect( &server->pipe_end, STATUS_PIPE_DISCONNECTED );
  1041. return;
  1042. case FSCTL_PIPE_IMPERSONATE:
  1043. if (current->process->token) /* FIXME: use the client token */
  1044. {
  1045. struct token *token;
  1046. if (!(token = token_duplicate( current->process->token, 0, SecurityImpersonation, NULL, NULL, 0, NULL, 0 )))
  1047. return;
  1048. if (current->token) release_object( current->token );
  1049. current->token = token;
  1050. }
  1051. return;
  1052. default:
  1053. pipe_end_ioctl( &server->pipe_end, code, async );
  1054. }
  1055. }
  1056. static void pipe_client_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
  1057. {
  1058. struct pipe_end *client = get_fd_user( fd );
  1059. switch(code)
  1060. {
  1061. case FSCTL_PIPE_LISTEN:
  1062. set_error( STATUS_ILLEGAL_FUNCTION );
  1063. return;
  1064. default:
  1065. pipe_end_ioctl( client, code, async );
  1066. }
  1067. }
  1068. static void init_pipe_end( struct pipe_end *pipe_end, struct named_pipe *pipe,
  1069. unsigned int pipe_flags, data_size_t buffer_size )
  1070. {
  1071. pipe_end->pipe = (struct named_pipe *)grab_object( pipe );
  1072. pipe_end->fd = NULL;
  1073. pipe_end->flags = pipe_flags;
  1074. pipe_end->connection = NULL;
  1075. pipe_end->buffer_size = buffer_size;
  1076. init_async_queue( &pipe_end->read_q );
  1077. init_async_queue( &pipe_end->write_q );
  1078. list_init( &pipe_end->message_queue );
  1079. }
  1080. static struct pipe_server *create_pipe_server( struct named_pipe *pipe, unsigned int options,
  1081. unsigned int pipe_flags )
  1082. {
  1083. struct pipe_server *server;
  1084. server = alloc_object( &pipe_server_ops );
  1085. if (!server)
  1086. return NULL;
  1087. server->options = options;
  1088. init_pipe_end( &server->pipe_end, pipe, pipe_flags, pipe->insize );
  1089. server->pipe_end.state = FILE_PIPE_LISTENING_STATE;
  1090. server->pipe_end.server_pid = get_process_id( current->process );
  1091. init_async_queue( &server->listen_q );
  1092. list_add_tail( &pipe->listeners, &server->entry );
  1093. if (!(server->pipe_end.fd = alloc_pseudo_fd( &pipe_server_fd_ops, &server->pipe_end.obj, options )))
  1094. {
  1095. release_object( server );
  1096. return NULL;
  1097. }
  1098. allow_fd_caching( server->pipe_end.fd );
  1099. set_fd_signaled( server->pipe_end.fd, 1 );
  1100. async_wake_up( &pipe->waiters, STATUS_SUCCESS );
  1101. return server;
  1102. }
  1103. static struct pipe_end *create_pipe_client( struct named_pipe *pipe, data_size_t buffer_size, unsigned int options )
  1104. {
  1105. struct pipe_end *client;
  1106. client = alloc_object( &pipe_client_ops );
  1107. if (!client)
  1108. return NULL;
  1109. init_pipe_end( client, pipe, 0, buffer_size );
  1110. client->state = FILE_PIPE_CONNECTED_STATE;
  1111. client->client_pid = get_process_id( current->process );
  1112. client->fd = alloc_pseudo_fd( &pipe_client_fd_ops, &client->obj, options );
  1113. if (!client->fd)
  1114. {
  1115. release_object( client );
  1116. return NULL;
  1117. }
  1118. allow_fd_caching( client->fd );
  1119. set_fd_signaled( client->fd, 1 );
  1120. return client;
  1121. }
  1122. static int named_pipe_link_name( struct object *obj, struct object_name *name, struct object *parent )
  1123. {
  1124. struct named_pipe_device *dev = (struct named_pipe_device *)parent;
  1125. if (parent->ops != &named_pipe_device_ops)
  1126. {
  1127. set_error( STATUS_OBJECT_NAME_INVALID );
  1128. return 0;
  1129. }
  1130. namespace_add( dev->pipes, name );
  1131. name->parent = grab_object( parent );
  1132. return 1;
  1133. }
  1134. static struct object *named_pipe_open_file( struct object *obj, unsigned int access,
  1135. unsigned int sharing, unsigned int options )
  1136. {
  1137. struct named_pipe *pipe = (struct named_pipe *)obj;
  1138. struct pipe_server *server;
  1139. struct pipe_end *client;
  1140. unsigned int pipe_sharing;
  1141. if (list_empty( &pipe->listeners ))
  1142. {
  1143. set_error( STATUS_PIPE_NOT_AVAILABLE );
  1144. return NULL;
  1145. }
  1146. server = LIST_ENTRY( list_head( &pipe->listeners ), struct pipe_server, entry );
  1147. pipe_sharing = pipe->sharing;
  1148. if (((access & GENERIC_READ) && !(pipe_sharing & FILE_SHARE_READ)) ||
  1149. ((access & GENERIC_WRITE) && !(pipe_sharing & FILE_SHARE_WRITE)))
  1150. {
  1151. set_error( STATUS_ACCESS_DENIED );
  1152. return NULL;
  1153. }
  1154. if ((client = create_pipe_client( pipe, pipe->outsize, options )))
  1155. {
  1156. async_wake_up( &server->listen_q, STATUS_SUCCESS );
  1157. server->pipe_end.state = FILE_PIPE_CONNECTED_STATE;
  1158. server->pipe_end.connection = client;
  1159. client->connection = &server->pipe_end;
  1160. server->pipe_end.client_pid = client->client_pid;
  1161. client->server_pid = server->pipe_end.server_pid;
  1162. list_remove( &server->entry );
  1163. }
  1164. return &client->obj;
  1165. }
  1166. static void named_pipe_device_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
  1167. {
  1168. struct named_pipe_device *device = get_fd_user( fd );
  1169. switch(code)
  1170. {
  1171. case FSCTL_PIPE_WAIT:
  1172. {
  1173. const FILE_PIPE_WAIT_FOR_BUFFER *buffer = get_req_data();
  1174. data_size_t size = get_req_data_size();
  1175. struct named_pipe *pipe;
  1176. struct unicode_str name;
  1177. timeout_t when;
  1178. if (size < sizeof(*buffer) ||
  1179. size < FIELD_OFFSET(FILE_PIPE_WAIT_FOR_BUFFER, Name[buffer->NameLength/sizeof(WCHAR)]))
  1180. {
  1181. set_error( STATUS_INVALID_PARAMETER );
  1182. return;
  1183. }
  1184. name.str = buffer->Name;
  1185. name.len = (buffer->NameLength / sizeof(WCHAR)) * sizeof(WCHAR);
  1186. if (!(pipe = open_named_object( &device->obj, &named_pipe_ops, &name, 0 ))) return;
  1187. if (list_empty( &pipe->listeners ))
  1188. {
  1189. queue_async( &pipe->waiters, async );
  1190. when = buffer->TimeoutSpecified ? buffer->Timeout.QuadPart : pipe->timeout;
  1191. async_set_timeout( async, when, STATUS_IO_TIMEOUT );
  1192. set_error( STATUS_PENDING );
  1193. }
  1194. release_object( pipe );
  1195. return;
  1196. }
  1197. default:
  1198. default_fd_ioctl( fd, code, async );
  1199. }
  1200. }
  1201. DECL_HANDLER(create_named_pipe)
  1202. {
  1203. struct named_pipe *pipe;
  1204. struct pipe_server *server;
  1205. struct unicode_str name;
  1206. struct object *root;
  1207. const struct security_descriptor *sd;
  1208. const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
  1209. if (!objattr) return;
  1210. if (!req->sharing || (req->sharing & ~(FILE_SHARE_READ | FILE_SHARE_WRITE)) ||
  1211. (!(req->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) && (req->flags & NAMED_PIPE_MESSAGE_STREAM_READ)))
  1212. {
  1213. if (root) release_object( root );
  1214. set_error( STATUS_INVALID_PARAMETER );
  1215. return;
  1216. }
  1217. if (!name.len) /* pipes need a root directory even without a name */
  1218. {
  1219. if (!objattr->rootdir)
  1220. {
  1221. set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
  1222. return;
  1223. }
  1224. if (!(root = get_handle_obj( current->process, objattr->rootdir, 0, NULL ))) return;
  1225. }
  1226. pipe = create_named_object( root, &named_pipe_ops, &name, objattr->attributes | OBJ_OPENIF, NULL );
  1227. if (root) release_object( root );
  1228. if (!pipe) return;
  1229. if (get_error() != STATUS_OBJECT_NAME_EXISTS)
  1230. {
  1231. /* initialize it if it didn't already exist */
  1232. pipe->instances = 0;
  1233. init_async_queue( &pipe->waiters );
  1234. list_init( &pipe->listeners );
  1235. pipe->insize = req->insize;
  1236. pipe->outsize = req->outsize;
  1237. pipe->maxinstances = req->maxinstances;
  1238. pipe->timeout = req->timeout;
  1239. pipe->message_mode = (req->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) != 0;
  1240. pipe->sharing = req->sharing;
  1241. if (sd) default_set_sd( &pipe->obj, sd, OWNER_SECURITY_INFORMATION |
  1242. GROUP_SECURITY_INFORMATION |
  1243. DACL_SECURITY_INFORMATION |
  1244. SACL_SECURITY_INFORMATION );
  1245. }
  1246. else
  1247. {
  1248. if (pipe->maxinstances <= pipe->instances)
  1249. {
  1250. set_error( STATUS_INSTANCE_NOT_AVAILABLE );
  1251. release_object( pipe );
  1252. return;
  1253. }
  1254. if (pipe->sharing != req->sharing)
  1255. {
  1256. set_error( STATUS_ACCESS_DENIED );
  1257. release_object( pipe );
  1258. return;
  1259. }
  1260. clear_error(); /* clear the name collision */
  1261. }
  1262. server = create_pipe_server( pipe, req->options, req->flags );
  1263. if (server)
  1264. {
  1265. reply->handle = alloc_handle( current->process, server, req->access, objattr->attributes );
  1266. pipe->instances++;
  1267. release_object( server );
  1268. }
  1269. release_object( pipe );
  1270. }
  1271. DECL_HANDLER(set_named_pipe_info)
  1272. {
  1273. struct pipe_end *pipe_end;
  1274. pipe_end = (struct pipe_end *)get_handle_obj( current->process, req->handle,
  1275. FILE_WRITE_ATTRIBUTES, &pipe_server_ops );
  1276. if (!pipe_end)
  1277. {
  1278. if (get_error() != STATUS_OBJECT_TYPE_MISMATCH)
  1279. return;
  1280. clear_error();
  1281. pipe_end = (struct pipe_end *)get_handle_obj( current->process, req->handle,
  1282. 0, &pipe_client_ops );
  1283. if (!pipe_end) return;
  1284. }
  1285. if (!pipe_end->pipe)
  1286. {
  1287. set_error( STATUS_PIPE_DISCONNECTED );
  1288. }
  1289. else if ((req->flags & ~(NAMED_PIPE_MESSAGE_STREAM_READ | NAMED_PIPE_NONBLOCKING_MODE)) ||
  1290. ((req->flags & NAMED_PIPE_MESSAGE_STREAM_READ) && !pipe_end->pipe->message_mode))
  1291. {
  1292. set_error( STATUS_INVALID_PARAMETER );
  1293. }
  1294. else
  1295. {
  1296. pipe_end->flags = req->flags;
  1297. }
  1298. release_object( pipe_end );
  1299. }