named_pipe.c 51 KB

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