named_pipe.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545
  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. async_close_obj_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. async_close_obj_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 data_size_t pipe_end_get_avail( struct pipe_end *pipe_end )
  530. {
  531. struct pipe_message *message;
  532. data_size_t avail = 0;
  533. LIST_FOR_EACH_ENTRY( message, &pipe_end->message_queue, struct pipe_message, entry )
  534. avail += message->iosb->in_size - message->read_pos;
  535. return avail;
  536. }
  537. static void pipe_end_get_file_info( struct fd *fd, obj_handle_t handle, unsigned int info_class )
  538. {
  539. struct pipe_end *pipe_end = get_fd_user( fd );
  540. struct named_pipe *pipe = pipe_end->pipe;
  541. switch (info_class)
  542. {
  543. case FileNameInformation:
  544. {
  545. FILE_NAME_INFORMATION *name_info;
  546. data_size_t name_len, reply_size;
  547. const WCHAR *name;
  548. if (get_reply_max_size() < sizeof(*name_info))
  549. {
  550. set_error( STATUS_INFO_LENGTH_MISMATCH );
  551. return;
  552. }
  553. /* FIXME: We should be able to return on unlinked pipe */
  554. if (!pipe || !(name = get_object_name( &pipe->obj, &name_len )))
  555. {
  556. set_error( STATUS_PIPE_DISCONNECTED );
  557. return;
  558. }
  559. reply_size = offsetof( FILE_NAME_INFORMATION, FileName[name_len/sizeof(WCHAR) + 1] );
  560. if (reply_size > get_reply_max_size())
  561. {
  562. reply_size = get_reply_max_size();
  563. set_error( STATUS_BUFFER_OVERFLOW );
  564. }
  565. if (!(name_info = set_reply_data_size( reply_size ))) return;
  566. name_info->FileNameLength = name_len + sizeof(WCHAR);
  567. name_info->FileName[0] = '\\';
  568. reply_size -= offsetof( FILE_NAME_INFORMATION, FileName[1] );
  569. if (reply_size) memcpy( &name_info->FileName[1], name, reply_size );
  570. break;
  571. }
  572. case FilePipeInformation:
  573. {
  574. FILE_PIPE_INFORMATION *pipe_info;
  575. if (!(get_handle_access( current->process, handle) & FILE_READ_ATTRIBUTES))
  576. {
  577. set_error( STATUS_ACCESS_DENIED );
  578. return;
  579. }
  580. if (get_reply_max_size() < sizeof(*pipe_info))
  581. {
  582. set_error( STATUS_INFO_LENGTH_MISMATCH );
  583. return;
  584. }
  585. if (!pipe)
  586. {
  587. set_error( STATUS_PIPE_DISCONNECTED );
  588. return;
  589. }
  590. if (!(pipe_info = set_reply_data_size( sizeof(*pipe_info) ))) return;
  591. pipe_info->ReadMode = (pipe_end->flags & NAMED_PIPE_MESSAGE_STREAM_READ)
  592. ? FILE_PIPE_MESSAGE_MODE : FILE_PIPE_BYTE_STREAM_MODE;
  593. pipe_info->CompletionMode = (pipe_end->flags & NAMED_PIPE_NONBLOCKING_MODE)
  594. ? FILE_PIPE_COMPLETE_OPERATION : FILE_PIPE_QUEUE_OPERATION;
  595. break;
  596. }
  597. case FilePipeLocalInformation:
  598. {
  599. FILE_PIPE_LOCAL_INFORMATION *pipe_info;
  600. if (!(get_handle_access( current->process, handle) & FILE_READ_ATTRIBUTES))
  601. {
  602. set_error( STATUS_ACCESS_DENIED );
  603. return;
  604. }
  605. if (get_reply_max_size() < sizeof(*pipe_info))
  606. {
  607. set_error( STATUS_INFO_LENGTH_MISMATCH );
  608. return;
  609. }
  610. if (!pipe)
  611. {
  612. set_error( STATUS_PIPE_DISCONNECTED );
  613. return;
  614. }
  615. if (!(pipe_info = set_reply_data_size( sizeof(*pipe_info) ))) return;
  616. pipe_info->NamedPipeType = pipe->message_mode;
  617. switch (pipe->sharing)
  618. {
  619. case FILE_SHARE_READ:
  620. pipe_info->NamedPipeConfiguration = FILE_PIPE_OUTBOUND;
  621. break;
  622. case FILE_SHARE_WRITE:
  623. pipe_info->NamedPipeConfiguration = FILE_PIPE_INBOUND;
  624. break;
  625. case FILE_SHARE_READ | FILE_SHARE_WRITE:
  626. pipe_info->NamedPipeConfiguration = FILE_PIPE_FULL_DUPLEX;
  627. break;
  628. }
  629. pipe_info->MaximumInstances = pipe->maxinstances;
  630. pipe_info->CurrentInstances = pipe->instances;
  631. pipe_info->InboundQuota = pipe->insize;
  632. pipe_info->ReadDataAvailable = pipe_end_get_avail( pipe_end );
  633. pipe_info->OutboundQuota = pipe->outsize;
  634. pipe_info->WriteQuotaAvailable = 0; /* FIXME */
  635. pipe_info->NamedPipeState = pipe_end->state;
  636. pipe_info->NamedPipeEnd = pipe_end->obj.ops == &pipe_server_ops
  637. ? FILE_PIPE_SERVER_END : FILE_PIPE_CLIENT_END;
  638. break;
  639. }
  640. case FileStandardInformation:
  641. {
  642. FILE_STANDARD_INFORMATION *std_info;
  643. if (!(get_handle_access( current->process, handle) & FILE_READ_ATTRIBUTES))
  644. {
  645. set_error( STATUS_ACCESS_DENIED );
  646. return;
  647. }
  648. if (get_reply_max_size() < sizeof(*std_info))
  649. {
  650. set_error( STATUS_INFO_LENGTH_MISMATCH );
  651. return;
  652. }
  653. if (!pipe)
  654. {
  655. set_error( STATUS_PIPE_DISCONNECTED );
  656. return;
  657. }
  658. if (!(std_info = set_reply_data_size( sizeof(*std_info) ))) return;
  659. std_info->AllocationSize.QuadPart = pipe->outsize + pipe->insize;
  660. std_info->EndOfFile.QuadPart = pipe_end_get_avail( pipe_end );
  661. std_info->NumberOfLinks = 1; /* FIXME */
  662. std_info->DeletePending = 0; /* FIXME */
  663. std_info->Directory = 0;
  664. break;
  665. }
  666. default:
  667. default_fd_get_file_info( fd, handle, info_class );
  668. }
  669. }
  670. static struct security_descriptor *pipe_end_get_sd( struct object *obj )
  671. {
  672. struct pipe_end *pipe_end = (struct pipe_end *) obj;
  673. if (pipe_end->pipe) return default_get_sd( &pipe_end->pipe->obj );
  674. set_error( STATUS_PIPE_DISCONNECTED );
  675. return NULL;
  676. }
  677. static int pipe_end_set_sd( struct object *obj, const struct security_descriptor *sd,
  678. unsigned int set_info )
  679. {
  680. struct pipe_end *pipe_end = (struct pipe_end *) obj;
  681. if (pipe_end->pipe) return default_set_sd( &pipe_end->pipe->obj, sd, set_info );
  682. set_error( STATUS_PIPE_DISCONNECTED );
  683. return 0;
  684. }
  685. static WCHAR *pipe_end_get_full_name( struct object *obj, data_size_t *len )
  686. {
  687. struct pipe_end *pipe_end = (struct pipe_end *) obj;
  688. return pipe_end->pipe->obj.ops->get_full_name( &pipe_end->pipe->obj, len );
  689. }
  690. static void pipe_end_get_volume_info( struct fd *fd, struct async *async, unsigned int info_class )
  691. {
  692. switch (info_class)
  693. {
  694. case FileFsDeviceInformation:
  695. {
  696. static const FILE_FS_DEVICE_INFORMATION device_info =
  697. {
  698. FILE_DEVICE_NAMED_PIPE,
  699. FILE_DEVICE_ALLOW_APPCONTAINER_TRAVERSAL
  700. };
  701. if (get_reply_max_size() >= sizeof(device_info))
  702. set_reply_data( &device_info, sizeof(device_info) );
  703. else
  704. set_error( STATUS_BUFFER_TOO_SMALL );
  705. break;
  706. }
  707. default:
  708. set_error( STATUS_NOT_IMPLEMENTED );
  709. }
  710. }
  711. static void message_queue_read( struct pipe_end *pipe_end, struct async *async )
  712. {
  713. struct iosb *iosb = async_get_iosb( async );
  714. unsigned int status = STATUS_SUCCESS;
  715. struct pipe_message *message;
  716. data_size_t out_size;
  717. if (pipe_end->flags & NAMED_PIPE_MESSAGE_STREAM_READ)
  718. {
  719. message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
  720. out_size = min( iosb->out_size, message->iosb->in_size - message->read_pos );
  721. if (message->read_pos + out_size < message->iosb->in_size)
  722. status = STATUS_BUFFER_OVERFLOW;
  723. }
  724. else
  725. {
  726. data_size_t avail = 0;
  727. LIST_FOR_EACH_ENTRY( message, &pipe_end->message_queue, struct pipe_message, entry )
  728. {
  729. avail += message->iosb->in_size - message->read_pos;
  730. if (avail >= iosb->out_size) break;
  731. }
  732. out_size = min( iosb->out_size, avail );
  733. }
  734. message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
  735. if (!message->read_pos && message->iosb->in_size == iosb->out_size) /* fast path */
  736. {
  737. async_request_complete( async, status, out_size, out_size, message->iosb->in_data );
  738. message->iosb->in_data = NULL;
  739. wake_message( message, message->iosb->in_size );
  740. free_message( message );
  741. }
  742. else
  743. {
  744. data_size_t write_pos = 0, writing;
  745. char *buf = NULL;
  746. if (out_size && !(buf = malloc( out_size )))
  747. {
  748. async_terminate( async, STATUS_NO_MEMORY );
  749. release_object( iosb );
  750. return;
  751. }
  752. do
  753. {
  754. message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
  755. writing = min( out_size - write_pos, message->iosb->in_size - message->read_pos );
  756. if (writing) memcpy( buf + write_pos, (const char *)message->iosb->in_data + message->read_pos, writing );
  757. write_pos += writing;
  758. message->read_pos += writing;
  759. if (message->read_pos == message->iosb->in_size)
  760. {
  761. wake_message(message, message->iosb->in_size);
  762. free_message(message);
  763. }
  764. } while (write_pos < out_size);
  765. async_request_complete( async, status, out_size, out_size, buf );
  766. }
  767. release_object( iosb );
  768. }
  769. /* We call async_terminate in our reselect implementation, which causes recursive reselect.
  770. * We're not interested in such reselect calls, so we ignore them. */
  771. static int ignore_reselect;
  772. static void reselect_write_queue( struct pipe_end *pipe_end );
  773. static void reselect_read_queue( struct pipe_end *pipe_end, int reselect_write )
  774. {
  775. struct async *async;
  776. ignore_reselect = 1;
  777. while (!list_empty( &pipe_end->message_queue ) && (async = find_pending_async( &pipe_end->read_q )))
  778. {
  779. message_queue_read( pipe_end, async );
  780. release_object( async );
  781. reselect_write = 1;
  782. }
  783. ignore_reselect = 0;
  784. if (pipe_end->connection)
  785. {
  786. if (list_empty( &pipe_end->message_queue ))
  787. fd_async_wake_up( pipe_end->connection->fd, ASYNC_TYPE_WAIT, STATUS_SUCCESS );
  788. else if (reselect_write)
  789. reselect_write_queue( pipe_end->connection );
  790. }
  791. }
  792. static void reselect_write_queue( struct pipe_end *pipe_end )
  793. {
  794. struct pipe_message *message, *next;
  795. struct pipe_end *reader = pipe_end->connection;
  796. data_size_t avail = 0;
  797. if (!reader) return;
  798. ignore_reselect = 1;
  799. LIST_FOR_EACH_ENTRY_SAFE( message, next, &reader->message_queue, struct pipe_message, entry )
  800. {
  801. if (message->async && message->iosb->status != STATUS_PENDING)
  802. {
  803. release_object( message->async );
  804. message->async = NULL;
  805. free_message( message );
  806. }
  807. else
  808. {
  809. avail += message->iosb->in_size - message->read_pos;
  810. if (message->async && (avail <= reader->buffer_size || !message->iosb->in_size))
  811. {
  812. wake_message( message, message->iosb->in_size );
  813. }
  814. else if (message->async && (pipe_end->flags & NAMED_PIPE_NONBLOCKING_MODE))
  815. {
  816. wake_message( message, message->read_pos );
  817. free_message( message );
  818. }
  819. }
  820. }
  821. ignore_reselect = 0;
  822. reselect_read_queue( reader, 0 );
  823. }
  824. static void pipe_end_read( struct fd *fd, struct async *async, file_pos_t pos )
  825. {
  826. struct pipe_end *pipe_end = get_fd_user( fd );
  827. switch (pipe_end->state)
  828. {
  829. case FILE_PIPE_CONNECTED_STATE:
  830. if ((pipe_end->flags & NAMED_PIPE_NONBLOCKING_MODE) && list_empty( &pipe_end->message_queue ))
  831. {
  832. set_error( STATUS_PIPE_EMPTY );
  833. return;
  834. }
  835. break;
  836. case FILE_PIPE_DISCONNECTED_STATE:
  837. set_error( STATUS_PIPE_DISCONNECTED );
  838. return;
  839. case FILE_PIPE_LISTENING_STATE:
  840. set_error( STATUS_PIPE_LISTENING );
  841. return;
  842. case FILE_PIPE_CLOSING_STATE:
  843. if (!list_empty( &pipe_end->message_queue )) break;
  844. set_error( STATUS_PIPE_BROKEN );
  845. return;
  846. }
  847. queue_async( &pipe_end->read_q, async );
  848. reselect_read_queue( pipe_end, 0 );
  849. set_error( STATUS_PENDING );
  850. }
  851. static void pipe_end_write( struct fd *fd, struct async *async, file_pos_t pos )
  852. {
  853. struct pipe_end *pipe_end = get_fd_user( fd );
  854. struct pipe_message *message;
  855. struct iosb *iosb;
  856. switch (pipe_end->state)
  857. {
  858. case FILE_PIPE_CONNECTED_STATE:
  859. break;
  860. case FILE_PIPE_DISCONNECTED_STATE:
  861. set_error( STATUS_PIPE_DISCONNECTED );
  862. return;
  863. case FILE_PIPE_LISTENING_STATE:
  864. set_error( STATUS_PIPE_LISTENING );
  865. return;
  866. case FILE_PIPE_CLOSING_STATE:
  867. set_error( STATUS_PIPE_CLOSING );
  868. return;
  869. }
  870. if (!pipe_end->pipe->message_mode && !get_req_data_size()) return;
  871. iosb = async_get_iosb( async );
  872. message = queue_message( pipe_end->connection, iosb );
  873. release_object( iosb );
  874. if (!message) return;
  875. message->async = (struct async *)grab_object( async );
  876. queue_async( &pipe_end->write_q, async );
  877. reselect_read_queue( pipe_end->connection, 1 );
  878. set_error( STATUS_PENDING );
  879. }
  880. static void pipe_end_reselect_async( struct fd *fd, struct async_queue *queue )
  881. {
  882. struct pipe_end *pipe_end = get_fd_user( fd );
  883. if (ignore_reselect) return;
  884. if (&pipe_end->write_q == queue)
  885. reselect_write_queue( pipe_end );
  886. else if (&pipe_end->read_q == queue)
  887. reselect_read_queue( pipe_end, 0 );
  888. }
  889. static enum server_fd_type pipe_end_get_fd_type( struct fd *fd )
  890. {
  891. return FD_TYPE_PIPE;
  892. }
  893. static void pipe_end_peek( struct pipe_end *pipe_end )
  894. {
  895. unsigned reply_size = get_reply_max_size();
  896. FILE_PIPE_PEEK_BUFFER *buffer;
  897. struct pipe_message *message;
  898. data_size_t avail = 0;
  899. data_size_t message_length = 0;
  900. if (reply_size < offsetof( FILE_PIPE_PEEK_BUFFER, Data ))
  901. {
  902. set_error( STATUS_INFO_LENGTH_MISMATCH );
  903. return;
  904. }
  905. reply_size -= offsetof( FILE_PIPE_PEEK_BUFFER, Data );
  906. switch (pipe_end->state)
  907. {
  908. case FILE_PIPE_CONNECTED_STATE:
  909. break;
  910. case FILE_PIPE_CLOSING_STATE:
  911. if (!list_empty( &pipe_end->message_queue )) break;
  912. set_error( STATUS_PIPE_BROKEN );
  913. return;
  914. default:
  915. set_error( pipe_end->pipe ? STATUS_INVALID_PIPE_STATE : STATUS_PIPE_DISCONNECTED );
  916. return;
  917. }
  918. LIST_FOR_EACH_ENTRY( message, &pipe_end->message_queue, struct pipe_message, entry )
  919. avail += message->iosb->in_size - message->read_pos;
  920. reply_size = min( reply_size, avail );
  921. if (avail && pipe_end->pipe->message_mode)
  922. {
  923. message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
  924. message_length = message->iosb->in_size - message->read_pos;
  925. reply_size = min( reply_size, message_length );
  926. }
  927. if (!(buffer = set_reply_data_size( offsetof( FILE_PIPE_PEEK_BUFFER, Data[reply_size] )))) return;
  928. buffer->NamedPipeState = pipe_end->state;
  929. buffer->ReadDataAvailable = avail;
  930. buffer->NumberOfMessages = 0; /* FIXME */
  931. buffer->MessageLength = message_length;
  932. if (reply_size)
  933. {
  934. data_size_t write_pos = 0, writing;
  935. LIST_FOR_EACH_ENTRY( message, &pipe_end->message_queue, struct pipe_message, entry )
  936. {
  937. writing = min( reply_size - write_pos, message->iosb->in_size - message->read_pos );
  938. memcpy( buffer->Data + write_pos, (const char *)message->iosb->in_data + message->read_pos,
  939. writing );
  940. write_pos += writing;
  941. if (write_pos == reply_size) break;
  942. }
  943. }
  944. if (message_length > reply_size) set_error( STATUS_BUFFER_OVERFLOW );
  945. }
  946. static void pipe_end_transceive( struct pipe_end *pipe_end, struct async *async )
  947. {
  948. struct pipe_message *message;
  949. struct iosb *iosb;
  950. if (!pipe_end->connection)
  951. {
  952. set_error( pipe_end->pipe ? STATUS_INVALID_PIPE_STATE : STATUS_PIPE_DISCONNECTED );
  953. return;
  954. }
  955. if (!(pipe_end->flags & NAMED_PIPE_MESSAGE_STREAM_READ))
  956. {
  957. set_error( STATUS_INVALID_READ_MODE );
  958. return;
  959. }
  960. /* not allowed if we already have read data buffered */
  961. if (!list_empty( &pipe_end->message_queue ))
  962. {
  963. set_error( STATUS_PIPE_BUSY );
  964. return;
  965. }
  966. iosb = async_get_iosb( async );
  967. /* ignore output buffer copy transferred because of METHOD_NEITHER */
  968. iosb->in_size -= iosb->out_size;
  969. /* transaction never blocks on write, so just queue a message without async */
  970. message = queue_message( pipe_end->connection, iosb );
  971. release_object( iosb );
  972. if (!message) return;
  973. reselect_read_queue( pipe_end->connection, 0 );
  974. queue_async( &pipe_end->read_q, async );
  975. reselect_read_queue( pipe_end, 0 );
  976. set_error( STATUS_PENDING );
  977. }
  978. static void pipe_end_get_connection_attribute( struct pipe_end *pipe_end )
  979. {
  980. const char *attr = get_req_data();
  981. data_size_t value_size, attr_size = get_req_data_size();
  982. void *value;
  983. if (attr_size == sizeof("ClientProcessId") && !memcmp( attr, "ClientProcessId", attr_size ))
  984. {
  985. value = &pipe_end->client_pid;
  986. value_size = sizeof(pipe_end->client_pid);
  987. }
  988. else if (attr_size == sizeof("ServerProcessId") && !memcmp( attr, "ServerProcessId", attr_size ))
  989. {
  990. value = &pipe_end->server_pid;
  991. value_size = sizeof(pipe_end->server_pid);
  992. }
  993. else
  994. {
  995. set_error( STATUS_ILLEGAL_FUNCTION );
  996. return;
  997. }
  998. if (get_reply_max_size() < value_size)
  999. {
  1000. set_error( STATUS_INFO_LENGTH_MISMATCH );
  1001. return;
  1002. }
  1003. set_reply_data( value, value_size );
  1004. }
  1005. static void pipe_end_ioctl( struct pipe_end *pipe_end, ioctl_code_t code, struct async *async )
  1006. {
  1007. switch(code)
  1008. {
  1009. case FSCTL_PIPE_GET_CONNECTION_ATTRIBUTE:
  1010. pipe_end_get_connection_attribute( pipe_end );
  1011. break;
  1012. case FSCTL_PIPE_PEEK:
  1013. pipe_end_peek( pipe_end );
  1014. break;
  1015. case FSCTL_PIPE_TRANSCEIVE:
  1016. pipe_end_transceive( pipe_end, async );
  1017. break;
  1018. default:
  1019. default_fd_ioctl( pipe_end->fd, code, async );
  1020. }
  1021. }
  1022. static void pipe_server_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
  1023. {
  1024. struct pipe_server *server = get_fd_user( fd );
  1025. switch(code)
  1026. {
  1027. case FSCTL_PIPE_LISTEN:
  1028. switch(server->pipe_end.state)
  1029. {
  1030. case FILE_PIPE_LISTENING_STATE:
  1031. break;
  1032. case FILE_PIPE_DISCONNECTED_STATE:
  1033. server->pipe_end.state = FILE_PIPE_LISTENING_STATE;
  1034. list_add_tail( &server->pipe_end.pipe->listeners, &server->entry );
  1035. break;
  1036. case FILE_PIPE_CONNECTED_STATE:
  1037. set_error( STATUS_PIPE_CONNECTED );
  1038. return;
  1039. case FILE_PIPE_CLOSING_STATE:
  1040. set_error( STATUS_PIPE_CLOSING );
  1041. return;
  1042. }
  1043. if (server->pipe_end.flags & NAMED_PIPE_NONBLOCKING_MODE)
  1044. {
  1045. set_error( STATUS_PIPE_LISTENING );
  1046. return;
  1047. }
  1048. queue_async( &server->listen_q, async );
  1049. async_wake_up( &server->pipe_end.pipe->waiters, STATUS_SUCCESS );
  1050. set_error( STATUS_PENDING );
  1051. return;
  1052. case FSCTL_PIPE_DISCONNECT:
  1053. switch(server->pipe_end.state)
  1054. {
  1055. case FILE_PIPE_CONNECTED_STATE:
  1056. /* dump the client connection - all data is lost */
  1057. assert( server->pipe_end.connection );
  1058. release_object( server->pipe_end.connection->pipe );
  1059. server->pipe_end.connection->pipe = NULL;
  1060. break;
  1061. case FILE_PIPE_CLOSING_STATE:
  1062. break;
  1063. case FILE_PIPE_LISTENING_STATE:
  1064. set_error( STATUS_PIPE_LISTENING );
  1065. return;
  1066. case FILE_PIPE_DISCONNECTED_STATE:
  1067. set_error( STATUS_PIPE_DISCONNECTED );
  1068. return;
  1069. }
  1070. pipe_end_disconnect( &server->pipe_end, STATUS_PIPE_DISCONNECTED );
  1071. return;
  1072. case FSCTL_PIPE_IMPERSONATE:
  1073. if (current->process->token) /* FIXME: use the client token */
  1074. {
  1075. struct token *token;
  1076. if (!(token = token_duplicate( current->process->token, 0, SecurityImpersonation, NULL, NULL, 0, NULL, 0 )))
  1077. return;
  1078. if (current->token) release_object( current->token );
  1079. current->token = token;
  1080. }
  1081. return;
  1082. default:
  1083. pipe_end_ioctl( &server->pipe_end, code, async );
  1084. }
  1085. }
  1086. static void pipe_client_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
  1087. {
  1088. struct pipe_end *client = get_fd_user( fd );
  1089. switch(code)
  1090. {
  1091. case FSCTL_PIPE_LISTEN:
  1092. set_error( STATUS_ILLEGAL_FUNCTION );
  1093. return;
  1094. default:
  1095. pipe_end_ioctl( client, code, async );
  1096. }
  1097. }
  1098. static void init_pipe_end( struct pipe_end *pipe_end, struct named_pipe *pipe,
  1099. unsigned int pipe_flags, data_size_t buffer_size )
  1100. {
  1101. pipe_end->pipe = (struct named_pipe *)grab_object( pipe );
  1102. pipe_end->fd = NULL;
  1103. pipe_end->flags = pipe_flags;
  1104. pipe_end->connection = NULL;
  1105. pipe_end->buffer_size = buffer_size;
  1106. init_async_queue( &pipe_end->read_q );
  1107. init_async_queue( &pipe_end->write_q );
  1108. list_init( &pipe_end->message_queue );
  1109. }
  1110. static struct pipe_server *create_pipe_server( struct named_pipe *pipe, unsigned int options,
  1111. unsigned int pipe_flags )
  1112. {
  1113. struct pipe_server *server;
  1114. server = alloc_object( &pipe_server_ops );
  1115. if (!server)
  1116. return NULL;
  1117. server->options = options;
  1118. init_pipe_end( &server->pipe_end, pipe, pipe_flags, pipe->insize );
  1119. server->pipe_end.state = FILE_PIPE_LISTENING_STATE;
  1120. server->pipe_end.server_pid = get_process_id( current->process );
  1121. init_async_queue( &server->listen_q );
  1122. list_add_tail( &pipe->listeners, &server->entry );
  1123. if (!(server->pipe_end.fd = alloc_pseudo_fd( &pipe_server_fd_ops, &server->pipe_end.obj, options )))
  1124. {
  1125. release_object( server );
  1126. return NULL;
  1127. }
  1128. allow_fd_caching( server->pipe_end.fd );
  1129. set_fd_signaled( server->pipe_end.fd, 1 );
  1130. async_wake_up( &pipe->waiters, STATUS_SUCCESS );
  1131. return server;
  1132. }
  1133. static struct pipe_end *create_pipe_client( struct named_pipe *pipe, data_size_t buffer_size, unsigned int options )
  1134. {
  1135. struct pipe_end *client;
  1136. client = alloc_object( &pipe_client_ops );
  1137. if (!client)
  1138. return NULL;
  1139. init_pipe_end( client, pipe, 0, buffer_size );
  1140. client->state = FILE_PIPE_CONNECTED_STATE;
  1141. client->client_pid = get_process_id( current->process );
  1142. client->fd = alloc_pseudo_fd( &pipe_client_fd_ops, &client->obj, options );
  1143. if (!client->fd)
  1144. {
  1145. release_object( client );
  1146. return NULL;
  1147. }
  1148. allow_fd_caching( client->fd );
  1149. set_fd_signaled( client->fd, 1 );
  1150. return client;
  1151. }
  1152. static int named_pipe_link_name( struct object *obj, struct object_name *name, struct object *parent )
  1153. {
  1154. struct named_pipe_device *dev = (struct named_pipe_device *)parent;
  1155. if (parent->ops != &named_pipe_device_ops)
  1156. {
  1157. set_error( STATUS_OBJECT_NAME_INVALID );
  1158. return 0;
  1159. }
  1160. namespace_add( dev->pipes, name );
  1161. name->parent = grab_object( parent );
  1162. return 1;
  1163. }
  1164. static struct object *named_pipe_open_file( struct object *obj, unsigned int access,
  1165. unsigned int sharing, unsigned int options )
  1166. {
  1167. struct named_pipe *pipe = (struct named_pipe *)obj;
  1168. struct pipe_server *server;
  1169. struct pipe_end *client;
  1170. unsigned int pipe_sharing;
  1171. if (list_empty( &pipe->listeners ))
  1172. {
  1173. set_error( STATUS_PIPE_NOT_AVAILABLE );
  1174. return NULL;
  1175. }
  1176. server = LIST_ENTRY( list_head( &pipe->listeners ), struct pipe_server, entry );
  1177. pipe_sharing = pipe->sharing;
  1178. if (((access & GENERIC_READ) && !(pipe_sharing & FILE_SHARE_READ)) ||
  1179. ((access & GENERIC_WRITE) && !(pipe_sharing & FILE_SHARE_WRITE)))
  1180. {
  1181. set_error( STATUS_ACCESS_DENIED );
  1182. return NULL;
  1183. }
  1184. if ((client = create_pipe_client( pipe, pipe->outsize, options )))
  1185. {
  1186. async_wake_up( &server->listen_q, STATUS_SUCCESS );
  1187. server->pipe_end.state = FILE_PIPE_CONNECTED_STATE;
  1188. server->pipe_end.connection = client;
  1189. client->connection = &server->pipe_end;
  1190. server->pipe_end.client_pid = client->client_pid;
  1191. client->server_pid = server->pipe_end.server_pid;
  1192. list_remove( &server->entry );
  1193. }
  1194. return &client->obj;
  1195. }
  1196. static void named_pipe_device_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
  1197. {
  1198. struct named_pipe_device *device = get_fd_user( fd );
  1199. switch(code)
  1200. {
  1201. case FSCTL_PIPE_WAIT:
  1202. {
  1203. const FILE_PIPE_WAIT_FOR_BUFFER *buffer = get_req_data();
  1204. data_size_t size = get_req_data_size();
  1205. struct named_pipe *pipe;
  1206. struct unicode_str name;
  1207. timeout_t when;
  1208. if (size < sizeof(*buffer) ||
  1209. size < FIELD_OFFSET(FILE_PIPE_WAIT_FOR_BUFFER, Name[buffer->NameLength/sizeof(WCHAR)]))
  1210. {
  1211. set_error( STATUS_INVALID_PARAMETER );
  1212. return;
  1213. }
  1214. name.str = buffer->Name;
  1215. name.len = (buffer->NameLength / sizeof(WCHAR)) * sizeof(WCHAR);
  1216. if (!(pipe = open_named_object( &device->obj, &named_pipe_ops, &name, 0 ))) return;
  1217. if (list_empty( &pipe->listeners ))
  1218. {
  1219. queue_async( &pipe->waiters, async );
  1220. when = buffer->TimeoutSpecified ? buffer->Timeout.QuadPart : pipe->timeout;
  1221. async_set_timeout( async, when, STATUS_IO_TIMEOUT );
  1222. set_error( STATUS_PENDING );
  1223. }
  1224. release_object( pipe );
  1225. return;
  1226. }
  1227. default:
  1228. default_fd_ioctl( fd, code, async );
  1229. }
  1230. }
  1231. DECL_HANDLER(create_named_pipe)
  1232. {
  1233. struct named_pipe *pipe;
  1234. struct pipe_server *server;
  1235. struct unicode_str name;
  1236. struct object *root;
  1237. const struct security_descriptor *sd;
  1238. const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
  1239. if (!objattr) return;
  1240. if (!req->sharing || (req->sharing & ~(FILE_SHARE_READ | FILE_SHARE_WRITE)) ||
  1241. (!(req->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) && (req->flags & NAMED_PIPE_MESSAGE_STREAM_READ)))
  1242. {
  1243. if (root) release_object( root );
  1244. set_error( STATUS_INVALID_PARAMETER );
  1245. return;
  1246. }
  1247. if (!name.len) /* pipes need a root directory even without a name */
  1248. {
  1249. if (!objattr->rootdir)
  1250. {
  1251. set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
  1252. return;
  1253. }
  1254. if (!(root = get_handle_obj( current->process, objattr->rootdir, 0, NULL ))) return;
  1255. }
  1256. switch (req->disposition)
  1257. {
  1258. case FILE_OPEN:
  1259. pipe = open_named_object( root, &named_pipe_ops, &name, objattr->attributes );
  1260. break;
  1261. case FILE_CREATE:
  1262. case FILE_OPEN_IF:
  1263. pipe = create_named_object( root, &named_pipe_ops, &name, objattr->attributes | OBJ_OPENIF, NULL );
  1264. break;
  1265. default:
  1266. pipe = NULL;
  1267. set_error( STATUS_INVALID_PARAMETER );
  1268. break;
  1269. }
  1270. if (root) release_object( root );
  1271. if (!pipe) return;
  1272. if (get_error() != STATUS_OBJECT_NAME_EXISTS && req->disposition != FILE_OPEN)
  1273. {
  1274. /* initialize it if it didn't already exist */
  1275. pipe->instances = 0;
  1276. init_async_queue( &pipe->waiters );
  1277. list_init( &pipe->listeners );
  1278. pipe->insize = req->insize;
  1279. pipe->outsize = req->outsize;
  1280. pipe->maxinstances = req->maxinstances;
  1281. pipe->timeout = req->timeout;
  1282. pipe->message_mode = (req->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) != 0;
  1283. pipe->sharing = req->sharing;
  1284. if (sd) default_set_sd( &pipe->obj, sd, OWNER_SECURITY_INFORMATION |
  1285. GROUP_SECURITY_INFORMATION |
  1286. DACL_SECURITY_INFORMATION |
  1287. SACL_SECURITY_INFORMATION );
  1288. reply->created = 1;
  1289. }
  1290. else
  1291. {
  1292. if (pipe->maxinstances <= pipe->instances)
  1293. {
  1294. set_error( STATUS_INSTANCE_NOT_AVAILABLE );
  1295. release_object( pipe );
  1296. return;
  1297. }
  1298. if (pipe->sharing != req->sharing || req->disposition == FILE_CREATE)
  1299. {
  1300. set_error( STATUS_ACCESS_DENIED );
  1301. release_object( pipe );
  1302. return;
  1303. }
  1304. clear_error(); /* clear the name collision */
  1305. }
  1306. server = create_pipe_server( pipe, req->options, req->flags );
  1307. if (server)
  1308. {
  1309. reply->handle = alloc_handle( current->process, server, req->access, objattr->attributes );
  1310. pipe->instances++;
  1311. release_object( server );
  1312. }
  1313. release_object( pipe );
  1314. }
  1315. DECL_HANDLER(set_named_pipe_info)
  1316. {
  1317. struct pipe_end *pipe_end;
  1318. pipe_end = (struct pipe_end *)get_handle_obj( current->process, req->handle,
  1319. FILE_WRITE_ATTRIBUTES, &pipe_server_ops );
  1320. if (!pipe_end)
  1321. {
  1322. if (get_error() != STATUS_OBJECT_TYPE_MISMATCH)
  1323. return;
  1324. clear_error();
  1325. pipe_end = (struct pipe_end *)get_handle_obj( current->process, req->handle,
  1326. 0, &pipe_client_ops );
  1327. if (!pipe_end) return;
  1328. }
  1329. if (!pipe_end->pipe)
  1330. {
  1331. set_error( STATUS_PIPE_DISCONNECTED );
  1332. }
  1333. else if ((req->flags & ~(NAMED_PIPE_MESSAGE_STREAM_READ | NAMED_PIPE_NONBLOCKING_MODE)) ||
  1334. ((req->flags & NAMED_PIPE_MESSAGE_STREAM_READ) && !pipe_end->pipe->message_mode))
  1335. {
  1336. set_error( STATUS_INVALID_PARAMETER );
  1337. }
  1338. else
  1339. {
  1340. pipe_end->flags = req->flags;
  1341. }
  1342. release_object( pipe_end );
  1343. }