named_pipe.c 49 KB

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