- Some systems/drives are very slow to read the TOC. To address this
[wine] / server / named_pipe.c
1 /*
2  * Server-side pipe management
3  *
4  * Copyright (C) 1998 Alexandre Julliard
5  * Copyright (C) 2001 Mike McCormack
6  *
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.
11  *
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.
16  *
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
20  *
21  * TODO:
22  *   improve error handling
23  */
24
25 #include "config.h"
26 #include "wine/port.h"
27
28 #include <assert.h>
29 #include <fcntl.h>
30 #include <string.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <sys/time.h>
34 #include <sys/types.h>
35 #ifdef HAVE_SYS_SOCKET_H
36 #include <sys/socket.h>
37 #endif
38 #include <time.h>
39 #include <unistd.h>
40
41 #include "winbase.h"
42
43 #include "file.h"
44 #include "handle.h"
45 #include "thread.h"
46 #include "request.h"
47
48 enum pipe_state
49 {
50     ps_none,
51     ps_idle_server,
52     ps_wait_open,
53     ps_wait_connect,
54     ps_connected_server,
55     ps_connected_client,
56     ps_disconnected
57 };
58
59 struct named_pipe;
60
61 struct pipe_user
62 {
63     struct object       obj;
64     struct fd          *fd;
65     enum pipe_state     state;
66     struct pipe_user   *other;
67     struct named_pipe  *pipe;
68     struct pipe_user   *next;
69     struct pipe_user   *prev;
70     struct thread      *thread;
71     void               *func;
72     void               *overlapped;
73 };
74
75 struct named_pipe
76 {
77     struct object       obj;         /* object header */
78     unsigned int        pipemode;
79     unsigned int        maxinstances;
80     unsigned int        outsize;
81     unsigned int        insize;
82     unsigned int        timeout;
83     struct pipe_user   *users;
84 };
85
86 static void named_pipe_dump( struct object *obj, int verbose );
87 static void named_pipe_destroy( struct object *obj);
88
89 static const struct object_ops named_pipe_ops =
90 {
91     sizeof(struct named_pipe),    /* size */
92     named_pipe_dump,              /* dump */
93     no_add_queue,                 /* add_queue */
94     NULL,                         /* remove_queue */
95     NULL,                         /* signaled */
96     NULL,                         /* satisfied */
97     no_get_fd,                    /* get_fd */
98     named_pipe_destroy            /* destroy */
99 };
100
101 static void pipe_user_dump( struct object *obj, int verbose );
102 static struct fd *pipe_user_get_fd( struct object *obj );
103 static void pipe_user_destroy( struct object *obj);
104
105 static int pipe_user_get_poll_events( struct fd *fd );
106 static int pipe_user_get_info( struct fd *fd, struct get_file_info_reply *reply, int *flags );
107
108 static const struct object_ops pipe_user_ops =
109 {
110     sizeof(struct pipe_user),     /* size */
111     pipe_user_dump,               /* dump */
112     default_fd_add_queue,         /* add_queue */
113     default_fd_remove_queue,      /* remove_queue */
114     default_fd_signaled,          /* signaled */
115     no_satisfied,                 /* satisfied */
116     pipe_user_get_fd,             /* get_fd */
117     pipe_user_destroy             /* destroy */
118 };
119
120 static const struct fd_ops pipe_user_fd_ops =
121 {
122     pipe_user_get_poll_events,    /* get_poll_events */
123     default_poll_event,           /* poll_event */
124     no_flush,                     /* flush */
125     pipe_user_get_info,           /* get_file_info */
126     no_queue_async                /* queue_async */
127 };
128
129 static void named_pipe_dump( struct object *obj, int verbose )
130 {
131     struct named_pipe *pipe = (struct named_pipe *)obj;
132     assert( obj->ops == &named_pipe_ops );
133     fprintf( stderr, "named pipe %p\n" ,pipe);
134 }
135
136 static void pipe_user_dump( struct object *obj, int verbose )
137 {
138     struct pipe_user *user = (struct pipe_user *)obj;
139     assert( obj->ops == &pipe_user_ops );
140     fprintf( stderr, "named pipe user %p (state %d)\n", user, user->state );
141 }
142
143 static void named_pipe_destroy( struct object *obj)
144 {
145     struct named_pipe *pipe = (struct named_pipe *)obj;
146     assert( !pipe->users );
147 }
148
149 static void notify_waiter( struct pipe_user *user, unsigned int status)
150 {
151     if(user->thread && user->func && user->overlapped)
152     {
153         /* queue a system APC, to notify a waiting thread */
154         thread_queue_apc(user->thread,NULL,user->func,
155             APC_ASYNC,1,2,user->overlapped,status);
156     }
157     if (user->thread) release_object(user->thread);
158     user->thread = NULL;
159     user->func = NULL;
160     user->overlapped=NULL;
161 }
162
163 static struct fd *pipe_user_get_fd( struct object *obj )
164 {
165     struct pipe_user *user = (struct pipe_user *)obj;
166     return (struct fd *)grab_object( user->fd );
167 }
168
169 static void pipe_user_destroy( struct object *obj)
170 {
171     struct pipe_user *user = (struct pipe_user *)obj;
172
173     assert( obj->ops == &pipe_user_ops );
174
175     if(user->overlapped)
176         notify_waiter(user,STATUS_HANDLES_CLOSED);
177
178     if(user->other)
179     {
180         release_object( user->other->fd );
181         user->other->fd = NULL;
182         switch(user->other->state)
183         {
184         case ps_connected_server:
185             user->other->state = ps_idle_server;
186             break;
187         case ps_connected_client:
188             user->other->state = ps_disconnected;
189             break;
190         default:
191             fprintf(stderr,"connected pipe has strange state %d!\n",
192                             user->other->state);
193         }
194         user->other->other=NULL;
195         user->other = NULL;
196     }
197
198     /* remove user from pipe's user list */
199     if (user->next) user->next->prev = user->prev;
200     if (user->prev) user->prev->next = user->next;
201     else user->pipe->users = user->next;
202     if (user->thread) release_object(user->thread);
203     release_object(user->pipe);
204     if (user->fd) release_object( user->fd );
205 }
206
207 static int pipe_user_get_poll_events( struct fd *fd )
208 {
209     return POLLIN | POLLOUT;  /* FIXME */
210 }
211
212 static int pipe_user_get_info( struct fd *fd, struct get_file_info_reply *reply, int *flags )
213 {
214     if (reply)
215     {
216         reply->type        = FILE_TYPE_PIPE;
217         reply->attr        = 0;
218         reply->access_time = 0;
219         reply->write_time  = 0;
220         reply->size_high   = 0;
221         reply->size_low    = 0;
222         reply->links       = 0;
223         reply->index_high  = 0;
224         reply->index_low   = 0;
225         reply->serial      = 0;
226     }
227     *flags = 0;
228     return FD_TYPE_DEFAULT;
229 }
230
231 static struct named_pipe *create_named_pipe( const WCHAR *name, size_t len )
232 {
233     struct named_pipe *pipe;
234
235     if ((pipe = create_named_object( sync_namespace, &named_pipe_ops, name, len )))
236     {
237         if (get_error() != STATUS_OBJECT_NAME_COLLISION)
238         {
239             /* initialize it if it didn't already exist */
240             pipe->users = 0;
241         }
242     }
243     return pipe;
244 }
245
246 static struct named_pipe *open_named_pipe( const WCHAR *name, size_t len )
247 {
248     struct object *obj;
249
250     if ((obj = find_object( sync_namespace, name, len )))
251     {
252         if (obj->ops == &named_pipe_ops) return (struct named_pipe *)obj;
253         release_object( obj );
254         set_error( STATUS_OBJECT_TYPE_MISMATCH );
255     }
256     else set_error( STATUS_OBJECT_NAME_NOT_FOUND );
257
258     return NULL;
259 }
260
261 static struct pipe_user *get_pipe_user_obj( struct process *process, obj_handle_t handle,
262                                             unsigned int access )
263 {
264     return (struct pipe_user *)get_handle_obj( process, handle, access, &pipe_user_ops );
265 }
266
267 static struct pipe_user *create_pipe_user( struct named_pipe *pipe )
268 {
269     struct pipe_user *user;
270
271     user = alloc_object( &pipe_user_ops );
272     if(!user)
273         return NULL;
274
275     user->fd = NULL;
276     user->pipe = pipe;
277     user->state = ps_none;
278     user->other = NULL;
279     user->thread = NULL;
280     user->func = NULL;
281     user->overlapped = NULL;
282
283     /* add to list of pipe users */
284     if ((user->next = pipe->users)) user->next->prev = user;
285     user->prev = NULL;
286     pipe->users = user;
287
288     grab_object(pipe);
289
290     return user;
291 }
292
293 static struct pipe_user *find_partner(struct named_pipe *pipe, enum pipe_state state)
294 {
295     struct pipe_user *x;
296
297     for(x = pipe->users; x; x=x->next)
298     {
299         if(x->state==state)
300         break;
301     }
302
303     if(!x)
304         return NULL;
305
306     return (struct pipe_user *)grab_object( x );
307 }
308
309 DECL_HANDLER(create_named_pipe)
310 {
311     struct named_pipe *pipe;
312     struct pipe_user *user;
313
314     reply->handle = 0;
315     pipe = create_named_pipe( get_req_data(), get_req_data_size() );
316     if(!pipe)
317         return;
318
319     if (get_error() != STATUS_OBJECT_NAME_COLLISION)
320     {
321         pipe->insize = req->insize;
322         pipe->outsize = req->outsize;
323         pipe->maxinstances = req->maxinstances;
324         pipe->timeout = req->timeout;
325         pipe->pipemode = req->pipemode;
326     }
327
328     user = create_pipe_user( pipe );
329
330     if(user)
331     {
332         user->state = ps_idle_server;
333         reply->handle = alloc_handle( current->process, user, GENERIC_READ|GENERIC_WRITE, 0 );
334         release_object( user );
335     }
336
337     release_object( pipe );
338 }
339
340 DECL_HANDLER(open_named_pipe)
341 {
342     struct pipe_user *user, *partner;
343     struct named_pipe *pipe;
344
345     reply->handle = 0;
346
347     if (!(pipe = open_named_pipe( get_req_data(), get_req_data_size() )))
348     {
349         set_error( STATUS_NO_SUCH_FILE );
350         return;
351     }
352     if (!(partner = find_partner(pipe, ps_wait_open)))
353     {
354         release_object(pipe);
355         set_error( STATUS_PIPE_NOT_AVAILABLE );
356         return;
357     }
358     if ((user = create_pipe_user( pipe )))
359     {
360         int fds[2];
361
362         if(!socketpair(PF_UNIX, SOCK_STREAM, 0, fds))
363         {
364             user->fd = alloc_fd( &pipe_user_fd_ops, fds[1], &user->obj );
365             partner->fd = alloc_fd( &pipe_user_fd_ops, fds[0], &partner->obj );
366             if (user->fd && partner->fd)
367             {
368                 notify_waiter(partner,STATUS_SUCCESS);
369                 partner->state = ps_connected_server;
370                 partner->other = user;
371                 user->state = ps_connected_client;
372                 user->other = partner;
373                 reply->handle = alloc_handle( current->process, user, req->access, 0 );
374             }
375         }
376         else file_set_error();
377
378         release_object( user );
379     }
380     release_object( partner );
381     release_object( pipe );
382 }
383
384 DECL_HANDLER(connect_named_pipe)
385 {
386     struct pipe_user *user, *partner;
387
388     user = get_pipe_user_obj(current->process, req->handle, 0);
389     if(!user)
390         return;
391
392     if( user->state != ps_idle_server )
393     {
394         set_error(STATUS_PORT_ALREADY_SET);
395     }
396     else
397     {
398         user->state = ps_wait_open;
399         user->thread = (struct thread *)grab_object(current);
400         user->func = req->func;
401         user->overlapped = req->overlapped;
402
403         /* notify all waiters that a pipe just became available */
404         while( (partner = find_partner(user->pipe,ps_wait_connect)) )
405         {
406             notify_waiter(partner,STATUS_SUCCESS);
407             release_object(partner);
408         }
409     }
410
411     release_object(user);
412 }
413
414 DECL_HANDLER(wait_named_pipe)
415 {
416     struct named_pipe *pipe;
417     struct pipe_user *partner;
418
419     if (!(pipe = open_named_pipe( get_req_data(), get_req_data_size() )))
420     {
421         set_error( STATUS_PIPE_NOT_AVAILABLE );
422         return;
423     }
424     if( (partner = find_partner(pipe,ps_wait_open)) )
425     {
426         /* this should use notify_waiter,
427            but no pipe_user object exists now... */
428         thread_queue_apc(current,NULL,req->func,
429                          APC_ASYNC,1,2,req->overlapped,STATUS_SUCCESS);
430         release_object(partner);
431     }
432     else
433     {
434         struct pipe_user *user;
435
436         if( (user = create_pipe_user( pipe )) )
437         {
438             user->state = ps_wait_connect;
439             user->thread = (struct thread *)grab_object(current);
440             user->func = req->func;
441             user->overlapped = req->overlapped;
442             /* don't release it */
443         }
444     }
445     release_object(pipe);
446 }
447
448 DECL_HANDLER(disconnect_named_pipe)
449 {
450     struct pipe_user *user;
451
452     user = get_pipe_user_obj(current->process, req->handle, 0);
453     if(!user)
454         return;
455     if( (user->state == ps_connected_server) &&
456         (user->other->state == ps_connected_client) )
457     {
458         release_object( user->other->fd );
459         user->other->fd = NULL;
460         user->other->state = ps_disconnected;
461         user->other->other = NULL;
462
463         release_object( user->fd );
464         user->fd = NULL;
465         user->state = ps_idle_server;
466         user->other = NULL;
467     }
468     release_object(user);
469 }
470
471 DECL_HANDLER(get_named_pipe_info)
472 {
473     struct pipe_user *user;
474
475     user = get_pipe_user_obj(current->process, req->handle, 0);
476     if(!user)
477         return;
478
479     reply->flags        = user->pipe->pipemode;
480     reply->maxinstances = user->pipe->maxinstances;
481     reply->insize       = user->pipe->insize;
482     reply->outsize      = user->pipe->outsize;
483
484     release_object(user);
485 }