2 * Server-side pipe management
4 * Copyright (C) 1998 Alexandre Julliard
5 * Copyright (C) 2001 Mike McCormack
8 * improve error handling
19 #include <sys/types.h>
20 #include <sys/socket.h>
46 enum pipe_state state
;
47 struct pipe_user
*other
;
48 struct named_pipe
*pipe
;
49 struct pipe_user
*next
;
50 struct pipe_user
*prev
;
58 struct object obj
; /* object header */
59 unsigned int pipemode
;
60 unsigned int maxinstances
;
64 struct pipe_user
*users
;
67 static void named_pipe_dump( struct object
*obj
, int verbose
);
68 static void named_pipe_destroy( struct object
*obj
);
70 static const struct object_ops named_pipe_ops
=
72 sizeof(struct named_pipe
), /* size */
73 named_pipe_dump
, /* dump */
74 no_add_queue
, /* add_queue */
75 NULL
, /* remove_queue */
78 NULL
, /* get_poll_events */
79 NULL
, /* poll_event */
80 no_get_fd
, /* get_fd */
82 no_get_file_info
, /* get_file_info */
83 NULL
, /* queue_async */
84 named_pipe_destroy
/* destroy */
87 static void pipe_user_dump( struct object
*obj
, int verbose
);
88 static void pipe_user_destroy( struct object
*obj
);
89 static int pipe_user_get_fd( struct object
*obj
);
90 static int pipe_user_get_info( struct object
*obj
, struct get_file_info_reply
*reply
);
92 static const struct object_ops pipe_user_ops
=
94 sizeof(struct pipe_user
), /* size */
95 pipe_user_dump
, /* dump */
96 default_poll_add_queue
, /* add_queue */
97 default_poll_remove_queue
, /* remove_queue */
98 default_poll_signaled
, /* signaled */
99 no_satisfied
, /* satisfied */
100 NULL
, /* get_poll_events */
101 default_poll_event
, /* poll_event */
102 pipe_user_get_fd
, /* get_fd */
103 no_flush
, /* flush */
104 pipe_user_get_info
, /* get_file_info */
105 NULL
, /* queue_async */
106 pipe_user_destroy
/* destroy */
109 static void named_pipe_dump( struct object
*obj
, int verbose
)
111 struct named_pipe
*pipe
= (struct named_pipe
*)obj
;
112 assert( obj
->ops
== &named_pipe_ops
);
113 fprintf( stderr
, "named pipe %p\n" ,pipe
);
116 static void pipe_user_dump( struct object
*obj
, int verbose
)
118 struct pipe_user
*user
= (struct pipe_user
*)obj
;
119 assert( obj
->ops
== &pipe_user_ops
);
120 fprintf( stderr
, "named pipe user %p (state %d)\n", user
, user
->state
);
123 static void named_pipe_destroy( struct object
*obj
)
125 struct named_pipe
*pipe
= (struct named_pipe
*)obj
;
126 assert( !pipe
->users
);
129 static void notify_waiter( struct pipe_user
*user
, unsigned int status
)
131 if(user
->thread
&& user
->func
&& user
->overlapped
)
133 /* queue a system APC, to notify a waiting thread */
134 thread_queue_apc(user
->thread
,NULL
,user
->func
,
135 APC_ASYNC
,1,2,user
->overlapped
,status
);
139 user
->overlapped
=NULL
;
142 static void pipe_user_destroy( struct object
*obj
)
144 struct pipe_user
*user
= (struct pipe_user
*)obj
;
146 assert( obj
->ops
== &pipe_user_ops
);
149 notify_waiter(user
,STATUS_HANDLES_CLOSED
);
153 close(user
->other
->obj
.fd
);
154 user
->other
->obj
.fd
= -1;
155 switch(user
->other
->state
)
157 case ps_connected_server
:
158 user
->other
->state
= ps_idle_server
;
160 case ps_connected_client
:
161 user
->other
->state
= ps_disconnected
;
164 fprintf(stderr
,"connected pipe has strange state %d!\n",
167 user
->other
->other
=NULL
;
171 /* remove user from pipe's user list */
172 if (user
->next
) user
->next
->prev
= user
->prev
;
173 if (user
->prev
) user
->prev
->next
= user
->next
;
174 else user
->pipe
->users
= user
->next
;
175 release_object(user
->pipe
);
178 static int pipe_user_get_fd( struct object
*obj
)
180 struct pipe_user
*user
= (struct pipe_user
*)obj
;
181 assert( obj
->ops
== &pipe_user_ops
);
185 static int pipe_user_get_info( struct object
*obj
, struct get_file_info_reply
*reply
)
189 reply
->type
= FILE_TYPE_PIPE
;
191 reply
->access_time
= 0;
192 reply
->write_time
= 0;
193 reply
->size_high
= 0;
196 reply
->index_high
= 0;
197 reply
->index_low
= 0;
200 return FD_TYPE_DEFAULT
;
203 static struct named_pipe
*create_named_pipe( const WCHAR
*name
, size_t len
)
205 struct named_pipe
*pipe
;
207 if ((pipe
= create_named_object( &named_pipe_ops
, name
, len
)))
209 if (get_error() != STATUS_OBJECT_NAME_COLLISION
)
211 /* initialize it if it didn't already exist */
218 static struct pipe_user
*get_pipe_user_obj( struct process
*process
, handle_t handle
,
219 unsigned int access
)
221 return (struct pipe_user
*)get_handle_obj( process
, handle
, access
, &pipe_user_ops
);
224 static struct pipe_user
*create_pipe_user( struct named_pipe
*pipe
, int fd
)
226 struct pipe_user
*user
;
228 user
= alloc_object( &pipe_user_ops
, fd
);
233 user
->state
= ps_none
;
237 user
->overlapped
= NULL
;
239 /* add to list of pipe users */
240 if ((user
->next
= pipe
->users
)) user
->next
->prev
= user
;
249 static struct pipe_user
*find_partner(struct named_pipe
*pipe
, enum pipe_state state
)
253 for(x
= pipe
->users
; x
; x
=x
->next
)
262 return (struct pipe_user
*)grab_object( x
);
265 DECL_HANDLER(create_named_pipe
)
267 struct named_pipe
*pipe
;
268 struct pipe_user
*user
;
271 pipe
= create_named_pipe( get_req_data(), get_req_data_size() );
275 if (get_error() != STATUS_OBJECT_NAME_COLLISION
)
277 pipe
->insize
= req
->insize
;
278 pipe
->outsize
= req
->outsize
;
279 pipe
->maxinstances
= req
->maxinstances
;
280 pipe
->timeout
= req
->timeout
;
281 pipe
->pipemode
= req
->pipemode
;
284 user
= create_pipe_user (pipe
, -1);
288 user
->state
= ps_idle_server
;
289 reply
->handle
= alloc_handle( current
->process
, user
, GENERIC_READ
|GENERIC_WRITE
, 0 );
290 release_object( user
);
293 release_object( pipe
);
296 DECL_HANDLER(open_named_pipe
)
298 struct named_pipe
*pipe
;
301 pipe
= create_named_pipe( get_req_data(), get_req_data_size() );
305 if (get_error() == STATUS_OBJECT_NAME_COLLISION
)
307 struct pipe_user
*partner
;
309 if ((partner
= find_partner(pipe
, ps_wait_open
)))
313 if(!socketpair(PF_UNIX
, SOCK_STREAM
, 0, fds
))
315 struct pipe_user
*user
;
317 if( (user
= create_pipe_user (pipe
, fds
[1])) )
319 partner
->obj
.fd
= fds
[0];
320 notify_waiter(partner
,STATUS_SUCCESS
);
321 partner
->state
= ps_connected_server
;
322 partner
->other
= user
;
323 user
->state
= ps_connected_client
;
324 user
->other
= partner
;
325 reply
->handle
= alloc_handle( current
->process
, user
, req
->access
, 0 );
326 release_object(user
);
333 release_object( partner
);
337 set_error(STATUS_PIPE_NOT_AVAILABLE
);
342 set_error(STATUS_NO_SUCH_FILE
);
345 release_object(pipe
);
348 DECL_HANDLER(connect_named_pipe
)
350 struct pipe_user
*user
, *partner
;
352 user
= get_pipe_user_obj(current
->process
, req
->handle
, 0);
356 if( user
->state
!= ps_idle_server
)
358 set_error(STATUS_PORT_ALREADY_SET
);
362 user
->state
= ps_wait_open
;
363 user
->thread
= current
;
364 user
->func
= req
->func
;
365 user
->overlapped
= req
->overlapped
;
367 /* notify all waiters that a pipe just became available */
368 while( (partner
= find_partner(user
->pipe
,ps_wait_connect
)) )
370 notify_waiter(partner
,STATUS_SUCCESS
);
371 release_object(partner
);
372 release_object(partner
);
376 release_object(user
);
379 DECL_HANDLER(wait_named_pipe
)
381 struct named_pipe
*pipe
;
383 pipe
= create_named_pipe( get_req_data(), get_req_data_size() );
386 /* only wait if the pipe already exists */
387 if(get_error() == STATUS_OBJECT_NAME_COLLISION
)
389 struct pipe_user
*partner
;
391 set_error(STATUS_SUCCESS
);
392 if( (partner
= find_partner(pipe
,ps_wait_open
)) )
394 /* this should use notify_waiter,
395 but no pipe_user object exists now... */
396 thread_queue_apc(current
,NULL
,req
->func
,
397 APC_ASYNC
,1,2,req
->overlapped
,STATUS_SUCCESS
);
398 release_object(partner
);
402 struct pipe_user
*user
;
404 if( (user
= create_pipe_user (pipe
, -1)) )
406 user
->state
= ps_wait_connect
;
407 user
->thread
= current
;
408 user
->func
= req
->func
;
409 user
->overlapped
= req
->overlapped
;
410 /* don't release it */
416 set_error(STATUS_PIPE_NOT_AVAILABLE
);
418 release_object(pipe
);
422 DECL_HANDLER(disconnect_named_pipe
)
424 struct pipe_user
*user
;
426 user
= get_pipe_user_obj(current
->process
, req
->handle
, 0);
429 if( (user
->state
== ps_connected_server
) &&
430 (user
->other
->state
== ps_connected_client
) )
432 close(user
->other
->obj
.fd
);
433 user
->other
->obj
.fd
= -1;
434 user
->other
->state
= ps_disconnected
;
435 user
->other
->other
= NULL
;
439 user
->state
= ps_idle_server
;
442 release_object(user
);
445 DECL_HANDLER(get_named_pipe_info
)
447 struct pipe_user
*user
;
449 user
= get_pipe_user_obj(current
->process
, req
->handle
, 0);
453 reply
->flags
= user
->pipe
->pipemode
;
454 reply
->maxinstances
= user
->pipe
->maxinstances
;
455 reply
->insize
= user
->pipe
->insize
;
456 reply
->outsize
= user
->pipe
->outsize
;
458 release_object(user
);