named_pipe.c 51 KB

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