Implemented GetAncestor and removed WIN_GetTopParent.
[wine/gsoc_dplay.git] / server / named_pipe.c
blobbc068a5c9e74c8070f062f5eab45f1f30f5d5702
1 /*
2 * Server-side pipe management
4 * Copyright (C) 1998 Alexandre Julliard
5 * Copyright (C) 2001 Mike McCormack
7 * TODO:
8 * improve error handling
9 */
11 #include "config.h"
13 #include <assert.h>
14 #include <fcntl.h>
15 #include <string.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <sys/time.h>
19 #include <sys/types.h>
20 #include <sys/socket.h>
21 #include <time.h>
22 #include <unistd.h>
24 #include "winbase.h"
26 #include "handle.h"
27 #include "thread.h"
28 #include "request.h"
30 enum pipe_state
32 ps_none,
33 ps_idle_server,
34 ps_wait_open,
35 ps_wait_connect,
36 ps_connected_server,
37 ps_connected_client,
38 ps_disconnected
41 struct named_pipe;
43 struct pipe_user
45 struct object obj;
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;
51 struct event *event;
54 struct named_pipe
56 struct object obj; /* object header */
57 unsigned int pipemode;
58 unsigned int maxinstances;
59 unsigned int outsize;
60 unsigned int insize;
61 unsigned int timeout;
62 struct pipe_user *users;
65 static void named_pipe_dump( struct object *obj, int verbose );
66 static void named_pipe_destroy( struct object *obj);
68 static const struct object_ops named_pipe_ops =
70 sizeof(struct named_pipe), /* size */
71 named_pipe_dump, /* dump */
72 no_add_queue, /* add_queue */
73 NULL, /* remove_queue */
74 NULL, /* signaled */
75 NULL, /* satisfied */
76 NULL, /* get_poll_events */
77 NULL, /* poll_event */
78 no_get_fd, /* get_fd */
79 no_flush, /* flush */
80 no_get_file_info, /* get_file_info */
81 named_pipe_destroy /* destroy */
84 static void pipe_user_dump( struct object *obj, int verbose );
85 static void pipe_user_destroy( struct object *obj);
86 static int pipe_user_get_fd( struct object *obj );
88 static const struct object_ops pipe_user_ops =
90 sizeof(struct pipe_user), /* size */
91 pipe_user_dump, /* dump */
92 default_poll_add_queue, /* add_queue */
93 default_poll_remove_queue, /* remove_queue */
94 default_poll_signaled, /* signaled */
95 no_satisfied, /* satisfied */
96 NULL, /* get_poll_events */
97 default_poll_event, /* poll_event */
98 pipe_user_get_fd, /* get_fd */
99 no_flush, /* flush */
100 no_get_file_info, /* get_file_info */
101 pipe_user_destroy /* destroy */
104 static void named_pipe_dump( struct object *obj, int verbose )
106 struct named_pipe *pipe = (struct named_pipe *)obj;
107 assert( obj->ops == &named_pipe_ops );
108 fprintf( stderr, "named pipe %p\n" ,pipe);
111 static void pipe_user_dump( struct object *obj, int verbose )
113 struct pipe_user *user = (struct pipe_user *)obj;
114 assert( obj->ops == &pipe_user_ops );
115 fprintf( stderr, "named pipe user %p (state %d)\n", user, user->state );
118 static void named_pipe_destroy( struct object *obj)
120 struct named_pipe *pipe = (struct named_pipe *)obj;
121 assert( !pipe->users );
124 static void pipe_user_destroy( struct object *obj)
126 struct pipe_user *user = (struct pipe_user *)obj;
128 assert( obj->ops == &pipe_user_ops );
130 if(user->event)
132 /* FIXME: signal waiter of failure */
133 release_object(user->event);
134 user->event = NULL;
136 if(user->other)
138 close(user->other->obj.fd);
139 user->other->obj.fd = -1;
140 switch(user->other->state)
142 case ps_connected_server:
143 user->other->state = ps_idle_server;
144 break;
145 case ps_connected_client:
146 user->other->state = ps_disconnected;
147 break;
148 default:
149 fprintf(stderr,"connected pipe has strange state %d!\n",
150 user->other->state);
152 user->other->other=NULL;
153 user->other = NULL;
156 /* remove user from pipe's user list */
157 if (user->next) user->next->prev = user->prev;
158 if (user->prev) user->prev->next = user->next;
159 else user->pipe->users = user->next;
160 release_object(user->pipe);
163 static int pipe_user_get_fd( struct object *obj )
165 struct pipe_user *user = (struct pipe_user *)obj;
166 assert( obj->ops == &pipe_user_ops );
167 return user->obj.fd;
170 static struct named_pipe *create_named_pipe( const WCHAR *name, size_t len )
172 struct named_pipe *pipe;
174 if ((pipe = create_named_object( &named_pipe_ops, name, len )))
176 if (get_error() != STATUS_OBJECT_NAME_COLLISION)
178 /* initialize it if it didn't already exist */
179 pipe->users = 0;
182 return pipe;
185 static struct pipe_user *get_pipe_user_obj( struct process *process, handle_t handle,
186 unsigned int access )
188 return (struct pipe_user *)get_handle_obj( process, handle, access, &pipe_user_ops );
191 static struct pipe_user *create_pipe_user( struct named_pipe *pipe, int fd )
193 struct pipe_user *user;
195 user = alloc_object( &pipe_user_ops, fd );
196 if(!user)
197 return NULL;
199 user->pipe = pipe;
200 user->state = ps_none;
201 user->event = NULL; /* thread wait on this pipe */
202 user->other = NULL;
204 /* add to list of pipe users */
205 if ((user->next = pipe->users)) user->next->prev = user;
206 user->prev = NULL;
207 pipe->users = user;
209 grab_object(pipe);
211 return user;
214 static struct pipe_user *find_partner(struct named_pipe *pipe, enum pipe_state state)
216 struct pipe_user *x;
218 for(x = pipe->users; x; x=x->next)
220 if(x->state==state)
221 break;
224 if(!x)
225 return NULL;
227 return (struct pipe_user *)grab_object( x );
230 DECL_HANDLER(create_named_pipe)
232 struct named_pipe *pipe;
233 struct pipe_user *user;
235 req->handle = 0;
236 pipe = create_named_pipe( get_req_data(req), get_req_data_size(req) );
237 if(!pipe)
238 return;
240 user = create_pipe_user (pipe, -1);
242 if(user)
244 user->state = ps_idle_server;
245 req->handle = alloc_handle( current->process, user, GENERIC_READ|GENERIC_WRITE, 0 );
246 release_object( user );
249 release_object( pipe );
252 DECL_HANDLER(open_named_pipe)
254 struct named_pipe *pipe;
256 req->handle = 0;
257 pipe = create_named_pipe( get_req_data(req), get_req_data_size(req) );
258 if(!pipe)
259 return;
261 if (get_error() == STATUS_OBJECT_NAME_COLLISION)
263 struct pipe_user *partner;
265 if ((partner = find_partner(pipe, ps_wait_open)))
267 int fds[2];
269 if(!socketpair(PF_UNIX, SOCK_STREAM, 0, fds))
271 struct pipe_user *user;
273 if( (user = create_pipe_user (pipe, fds[1])) )
275 partner->obj.fd = fds[0];
276 set_event(partner->event);
277 release_object(partner->event);
278 partner->event = NULL;
279 partner->state = ps_connected_server;
280 partner->other = user;
281 user->state = ps_connected_client;
282 user->other = partner;
283 req->handle = alloc_handle( current->process, user, req->access, 0 );
284 release_object(user);
286 else
288 close(fds[0]);
291 release_object( partner );
293 else
295 set_error(STATUS_PIPE_NOT_AVAILABLE);
298 else
300 set_error(STATUS_NO_SUCH_FILE);
303 release_object(pipe);
306 DECL_HANDLER(connect_named_pipe)
308 struct pipe_user *user, *partner;
309 struct event *event;
311 user = get_pipe_user_obj(current->process, req->handle, 0);
312 if(!user)
313 return;
315 if( user->state != ps_idle_server )
317 set_error(STATUS_PORT_ALREADY_SET);
319 else
321 user->state = ps_wait_open;
322 event = get_event_obj(current->process, req->event, 0);
323 if(event)
324 user->event = event;
326 /* notify all waiters that a pipe just became available */
327 while( (partner = find_partner(user->pipe,ps_wait_connect)) )
329 set_event(partner->event);
330 release_object(partner->event);
331 partner->event = NULL;
332 release_object(partner);
333 release_object(partner);
337 release_object(user);
340 DECL_HANDLER(wait_named_pipe)
342 struct event *event;
343 struct named_pipe *pipe;
345 event = get_event_obj(current->process, req->event, 0);
346 if(!event)
347 return;
349 pipe = create_named_pipe( get_req_data(req), get_req_data_size(req) );
350 if( pipe )
352 /* only wait if the pipe already exists */
353 if(get_error() == STATUS_OBJECT_NAME_COLLISION)
355 struct pipe_user *partner;
357 set_error(STATUS_SUCCESS);
358 if( (partner = find_partner(pipe,ps_wait_open)) )
360 set_event(event);
361 release_object(partner);
363 else
365 struct pipe_user *user;
367 if( (user = create_pipe_user (pipe, -1)) )
369 user->event = (struct event *)grab_object( event );
370 user->state = ps_wait_connect;
371 /* don't release it */
375 else
377 set_error(STATUS_PIPE_NOT_AVAILABLE);
379 release_object(pipe);
381 release_object(event);
384 DECL_HANDLER(disconnect_named_pipe)
386 struct pipe_user *user;
388 user = get_pipe_user_obj(current->process, req->handle, 0);
389 if(!user)
390 return;
391 if( (user->state == ps_connected_server) &&
392 (user->other->state == ps_connected_client) )
394 close(user->other->obj.fd);
395 user->other->obj.fd = -1;
396 user->other->state = ps_disconnected;
397 user->other->other = NULL;
399 close(user->obj.fd);
400 user->obj.fd = -1;
401 user->state = ps_idle_server;
402 user->other = NULL;
404 release_object(user);