2 * Server-side pipe management
4 * Copyright (C) 1998 Alexandre Julliard
5 * Copyright (C) 2001 Mike McCormack
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * improve error handling
26 #include "wine/port.h"
34 #include <sys/types.h>
35 #include <sys/socket.h>
61 enum pipe_state state;
62 struct pipe_user *other;
63 struct named_pipe *pipe;
64 struct pipe_user *next;
65 struct pipe_user *prev;
66 struct thread *thread;
73 struct object obj; /* object header */
74 unsigned int pipemode;
75 unsigned int maxinstances;
79 struct pipe_user *users;
82 static void named_pipe_dump( struct object *obj, int verbose );
83 static void named_pipe_destroy( struct object *obj);
85 static const struct object_ops named_pipe_ops =
87 sizeof(struct named_pipe), /* size */
88 named_pipe_dump, /* dump */
89 no_add_queue, /* add_queue */
90 NULL, /* remove_queue */
93 NULL, /* get_poll_events */
94 NULL, /* poll_event */
95 no_get_fd, /* get_fd */
97 no_get_file_info, /* get_file_info */
98 NULL, /* queue_async */
99 named_pipe_destroy /* destroy */
102 static void pipe_user_dump( struct object *obj, int verbose );
103 static void pipe_user_destroy( struct object *obj);
104 static int pipe_user_get_fd( struct object *obj );
105 static int pipe_user_get_info( struct object *obj, struct get_file_info_reply *reply, int *flags );
107 static const struct object_ops pipe_user_ops =
109 sizeof(struct pipe_user), /* size */
110 pipe_user_dump, /* dump */
111 default_poll_add_queue, /* add_queue */
112 default_poll_remove_queue, /* remove_queue */
113 default_poll_signaled, /* signaled */
114 no_satisfied, /* satisfied */
115 NULL, /* get_poll_events */
116 default_poll_event, /* poll_event */
117 pipe_user_get_fd, /* get_fd */
118 no_flush, /* flush */
119 pipe_user_get_info, /* get_file_info */
120 NULL, /* queue_async */
121 pipe_user_destroy /* destroy */
124 static void named_pipe_dump( struct object *obj, int verbose )
126 struct named_pipe *pipe = (struct named_pipe *)obj;
127 assert( obj->ops == &named_pipe_ops );
128 fprintf( stderr, "named pipe %p\n" ,pipe);
131 static void pipe_user_dump( struct object *obj, int verbose )
133 struct pipe_user *user = (struct pipe_user *)obj;
134 assert( obj->ops == &pipe_user_ops );
135 fprintf( stderr, "named pipe user %p (state %d)\n", user, user->state );
138 static void named_pipe_destroy( struct object *obj)
140 struct named_pipe *pipe = (struct named_pipe *)obj;
141 assert( !pipe->users );
144 static void notify_waiter( struct pipe_user *user, unsigned int status)
146 if(user->thread && user->func && user->overlapped)
148 /* queue a system APC, to notify a waiting thread */
149 thread_queue_apc(user->thread,NULL,user->func,
150 APC_ASYNC,1,2,user->overlapped,status);
152 if (user->thread) release_object(user->thread);
155 user->overlapped=NULL;
158 static void pipe_user_destroy( struct object *obj)
160 struct pipe_user *user = (struct pipe_user *)obj;
162 assert( obj->ops == &pipe_user_ops );
165 notify_waiter(user,STATUS_HANDLES_CLOSED);
169 close(user->other->obj.fd);
170 user->other->obj.fd = -1;
171 switch(user->other->state)
173 case ps_connected_server:
174 user->other->state = ps_idle_server;
176 case ps_connected_client:
177 user->other->state = ps_disconnected;
180 fprintf(stderr,"connected pipe has strange state %d!\n",
183 user->other->other=NULL;
187 /* remove user from pipe's user list */
188 if (user->next) user->next->prev = user->prev;
189 if (user->prev) user->prev->next = user->next;
190 else user->pipe->users = user->next;
191 if (user->thread) release_object(user->thread);
192 release_object(user->pipe);
195 static int pipe_user_get_fd( struct object *obj )
197 struct pipe_user *user = (struct pipe_user *)obj;
198 assert( obj->ops == &pipe_user_ops );
202 static int pipe_user_get_info( struct object *obj, struct get_file_info_reply *reply, int *flags )
206 reply->type = FILE_TYPE_PIPE;
208 reply->access_time = 0;
209 reply->write_time = 0;
210 reply->size_high = 0;
213 reply->index_high = 0;
214 reply->index_low = 0;
218 return FD_TYPE_DEFAULT;
221 static struct named_pipe *create_named_pipe( const WCHAR *name, size_t len )
223 struct named_pipe *pipe;
225 if ((pipe = create_named_object( sync_namespace, &named_pipe_ops, name, len )))
227 if (get_error() != STATUS_OBJECT_NAME_COLLISION)
229 /* initialize it if it didn't already exist */
236 static struct pipe_user *get_pipe_user_obj( struct process *process, obj_handle_t handle,
237 unsigned int access )
239 return (struct pipe_user *)get_handle_obj( process, handle, access, &pipe_user_ops );
242 static struct pipe_user *create_pipe_user( struct named_pipe *pipe, int fd )
244 struct pipe_user *user;
246 user = alloc_object( &pipe_user_ops, fd );
251 user->state = ps_none;
255 user->overlapped = NULL;
257 /* add to list of pipe users */
258 if ((user->next = pipe->users)) user->next->prev = user;
267 static struct pipe_user *find_partner(struct named_pipe *pipe, enum pipe_state state)
271 for(x = pipe->users; x; x=x->next)
280 return (struct pipe_user *)grab_object( x );
283 DECL_HANDLER(create_named_pipe)
285 struct named_pipe *pipe;
286 struct pipe_user *user;
289 pipe = create_named_pipe( get_req_data(), get_req_data_size() );
293 if (get_error() != STATUS_OBJECT_NAME_COLLISION)
295 pipe->insize = req->insize;
296 pipe->outsize = req->outsize;
297 pipe->maxinstances = req->maxinstances;
298 pipe->timeout = req->timeout;
299 pipe->pipemode = req->pipemode;
302 user = create_pipe_user (pipe, -1);
306 user->state = ps_idle_server;
307 reply->handle = alloc_handle( current->process, user, GENERIC_READ|GENERIC_WRITE, 0 );
308 release_object( user );
311 release_object( pipe );
314 DECL_HANDLER(open_named_pipe)
316 struct named_pipe *pipe;
319 pipe = create_named_pipe( get_req_data(), get_req_data_size() );
323 if (get_error() == STATUS_OBJECT_NAME_COLLISION)
325 struct pipe_user *partner;
327 if ((partner = find_partner(pipe, ps_wait_open)))
331 if(!socketpair(PF_UNIX, SOCK_STREAM, 0, fds))
333 struct pipe_user *user;
335 if( (user = create_pipe_user (pipe, fds[1])) )
337 partner->obj.fd = fds[0];
338 notify_waiter(partner,STATUS_SUCCESS);
339 partner->state = ps_connected_server;
340 partner->other = user;
341 user->state = ps_connected_client;
342 user->other = partner;
343 reply->handle = alloc_handle( current->process, user, req->access, 0 );
344 release_object(user);
351 release_object( partner );
355 set_error(STATUS_PIPE_NOT_AVAILABLE);
360 set_error(STATUS_NO_SUCH_FILE);
363 release_object(pipe);
366 DECL_HANDLER(connect_named_pipe)
368 struct pipe_user *user, *partner;
370 user = get_pipe_user_obj(current->process, req->handle, 0);
374 if( user->state != ps_idle_server )
376 set_error(STATUS_PORT_ALREADY_SET);
380 user->state = ps_wait_open;
381 user->thread = (struct thread *)grab_object(current);
382 user->func = req->func;
383 user->overlapped = req->overlapped;
385 /* notify all waiters that a pipe just became available */
386 while( (partner = find_partner(user->pipe,ps_wait_connect)) )
388 notify_waiter(partner,STATUS_SUCCESS);
389 release_object(partner);
390 release_object(partner);
394 release_object(user);
397 DECL_HANDLER(wait_named_pipe)
399 struct named_pipe *pipe;
401 pipe = create_named_pipe( get_req_data(), get_req_data_size() );
404 /* only wait if the pipe already exists */
405 if(get_error() == STATUS_OBJECT_NAME_COLLISION)
407 struct pipe_user *partner;
409 set_error(STATUS_SUCCESS);
410 if( (partner = find_partner(pipe,ps_wait_open)) )
412 /* this should use notify_waiter,
413 but no pipe_user object exists now... */
414 thread_queue_apc(current,NULL,req->func,
415 APC_ASYNC,1,2,req->overlapped,STATUS_SUCCESS);
416 release_object(partner);
420 struct pipe_user *user;
422 if( (user = create_pipe_user (pipe, -1)) )
424 user->state = ps_wait_connect;
425 user->thread = (struct thread *)grab_object(current);
426 user->func = req->func;
427 user->overlapped = req->overlapped;
428 /* don't release it */
434 set_error(STATUS_PIPE_NOT_AVAILABLE);
436 release_object(pipe);
440 DECL_HANDLER(disconnect_named_pipe)
442 struct pipe_user *user;
444 user = get_pipe_user_obj(current->process, req->handle, 0);
447 if( (user->state == ps_connected_server) &&
448 (user->other->state == ps_connected_client) )
450 close(user->other->obj.fd);
451 user->other->obj.fd = -1;
452 user->other->state = ps_disconnected;
453 user->other->other = NULL;
457 user->state = ps_idle_server;
460 release_object(user);
463 DECL_HANDLER(get_named_pipe_info)
465 struct pipe_user *user;
467 user = get_pipe_user_obj(current->process, req->handle, 0);
471 reply->flags = user->pipe->pipemode;
472 reply->maxinstances = user->pipe->maxinstances;
473 reply->insize = user->pipe->insize;
474 reply->outsize = user->pipe->outsize;
476 release_object(user);