Implemented RtlExpandEnvironmentStrings_U and fixed an incorrect
[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, APC_ASYNC, 1,
155                          user->overlapped, (void *)status, NULL);
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     if (user->fd) return (struct fd *)grab_object( user->fd );
167     set_error( STATUS_PIPE_DISCONNECTED );
168     return NULL;
169 }
170
171 static void pipe_user_destroy( struct object *obj)
172 {
173     struct pipe_user *user = (struct pipe_user *)obj;
174
175     assert( obj->ops == &pipe_user_ops );
176
177     if(user->overlapped)
178         notify_waiter(user,STATUS_HANDLES_CLOSED);
179
180     if(user->other)
181     {
182         release_object( user->other->fd );
183         user->other->fd = NULL;
184         switch(user->other->state)
185         {
186         case ps_connected_server:
187             user->other->state = ps_idle_server;
188             break;
189         case ps_connected_client:
190             user->other->state = ps_disconnected;
191             break;
192         default:
193             fprintf(stderr,"connected pipe has strange state %d!\n",
194                             user->other->state);
195         }
196         user->other->other=NULL;
197         user->other = NULL;
198     }
199
200     /* remove user from pipe's user list */
201     if (user->next) user->next->prev = user->prev;
202     if (user->prev) user->prev->next = user->next;
203     else user->pipe->users = user->next;
204     if (user->thread) release_object(user->thread);
205     release_object(user->pipe);
206     if (user->fd) release_object( user->fd );
207 }
208
209 static int pipe_user_get_poll_events( struct fd *fd )
210 {
211     return POLLIN | POLLOUT;  /* FIXME */
212 }
213
214 static int pipe_user_get_info( struct fd *fd, struct get_file_info_reply *reply, int *flags )
215 {
216     if (reply)
217     {
218         reply->type        = FILE_TYPE_PIPE;
219         reply->attr        = 0;
220         reply->access_time = 0;
221         reply->write_time  = 0;
222         reply->size_high   = 0;
223         reply->size_low    = 0;
224         reply->links       = 0;
225         reply->index_high  = 0;
226         reply->index_low   = 0;
227         reply->serial      = 0;
228     }
229     *flags = 0;
230     return FD_TYPE_DEFAULT;
231 }
232
233 static struct named_pipe *create_named_pipe( const WCHAR *name, size_t len )
234 {
235     struct named_pipe *pipe;
236
237     if ((pipe = create_named_object( sync_namespace, &named_pipe_ops, name, len )))
238     {
239         if (get_error() != STATUS_OBJECT_NAME_COLLISION)
240         {
241             /* initialize it if it didn't already exist */
242             pipe->users = 0;
243         }
244     }
245     return pipe;
246 }
247
248 static struct named_pipe *open_named_pipe( const WCHAR *name, size_t len )
249 {
250     struct object *obj;
251
252     if ((obj = find_object( sync_namespace, name, len )))
253     {
254         if (obj->ops == &named_pipe_ops) return (struct named_pipe *)obj;
255         release_object( obj );
256         set_error( STATUS_OBJECT_TYPE_MISMATCH );
257     }
258     else set_error( STATUS_OBJECT_NAME_NOT_FOUND );
259
260     return NULL;
261 }
262
263 static struct pipe_user *get_pipe_user_obj( struct process *process, obj_handle_t handle,
264                                             unsigned int access )
265 {
266     return (struct pipe_user *)get_handle_obj( process, handle, access, &pipe_user_ops );
267 }
268
269 static struct pipe_user *create_pipe_user( struct named_pipe *pipe )
270 {
271     struct pipe_user *user;
272
273     user = alloc_object( &pipe_user_ops );
274     if(!user)
275         return NULL;
276
277     user->fd = NULL;
278     user->pipe = pipe;
279     user->state = ps_none;
280     user->other = NULL;
281     user->thread = NULL;
282     user->func = NULL;
283     user->overlapped = NULL;
284
285     /* add to list of pipe users */
286     if ((user->next = pipe->users)) user->next->prev = user;
287     user->prev = NULL;
288     pipe->users = user;
289
290     grab_object(pipe);
291
292     return user;
293 }
294
295 static struct pipe_user *find_partner(struct named_pipe *pipe, enum pipe_state state)
296 {
297     struct pipe_user *x;
298
299     for(x = pipe->users; x; x=x->next)
300     {
301         if(x->state==state)
302         break;
303     }
304
305     if(!x)
306         return NULL;
307
308     return (struct pipe_user *)grab_object( x );
309 }
310
311 DECL_HANDLER(create_named_pipe)
312 {
313     struct named_pipe *pipe;
314     struct pipe_user *user;
315
316     reply->handle = 0;
317     pipe = create_named_pipe( get_req_data(), get_req_data_size() );
318     if(!pipe)
319         return;
320
321     if (get_error() != STATUS_OBJECT_NAME_COLLISION)
322     {
323         pipe->insize = req->insize;
324         pipe->outsize = req->outsize;
325         pipe->maxinstances = req->maxinstances;
326         pipe->timeout = req->timeout;
327         pipe->pipemode = req->pipemode;
328     }
329
330     user = create_pipe_user( pipe );
331
332     if(user)
333     {
334         user->state = ps_idle_server;
335         reply->handle = alloc_handle( current->process, user, GENERIC_READ|GENERIC_WRITE, 0 );
336         release_object( user );
337     }
338
339     release_object( pipe );
340 }
341
342 DECL_HANDLER(open_named_pipe)
343 {
344     struct pipe_user *user, *partner;
345     struct named_pipe *pipe;
346
347     reply->handle = 0;
348
349     if (!(pipe = open_named_pipe( get_req_data(), get_req_data_size() )))
350     {
351         set_error( STATUS_NO_SUCH_FILE );
352         return;
353     }
354     if (!(partner = find_partner(pipe, ps_wait_open)))
355     {
356         release_object(pipe);
357         set_error( STATUS_PIPE_NOT_AVAILABLE );
358         return;
359     }
360     if ((user = create_pipe_user( pipe )))
361     {
362         int fds[2];
363
364         if(!socketpair(PF_UNIX, SOCK_STREAM, 0, fds))
365         {
366             user->fd = create_anonymous_fd( &pipe_user_fd_ops, fds[1], &user->obj );
367             partner->fd = create_anonymous_fd( &pipe_user_fd_ops, fds[0], &partner->obj );
368             if (user->fd && partner->fd)
369             {
370                 notify_waiter(partner,STATUS_SUCCESS);
371                 partner->state = ps_connected_server;
372                 partner->other = user;
373                 user->state = ps_connected_client;
374                 user->other = partner;
375                 reply->handle = alloc_handle( current->process, user, req->access, 0 );
376             }
377         }
378         else file_set_error();
379
380         release_object( user );
381     }
382     release_object( partner );
383     release_object( pipe );
384 }
385
386 DECL_HANDLER(connect_named_pipe)
387 {
388     struct pipe_user *user, *partner;
389
390     user = get_pipe_user_obj(current->process, req->handle, 0);
391     if(!user)
392         return;
393
394     if( user->state != ps_idle_server )
395     {
396         set_error(STATUS_PORT_ALREADY_SET);
397     }
398     else
399     {
400         user->state = ps_wait_open;
401         user->thread = (struct thread *)grab_object(current);
402         user->func = req->func;
403         user->overlapped = req->overlapped;
404
405         /* notify all waiters that a pipe just became available */
406         while( (partner = find_partner(user->pipe,ps_wait_connect)) )
407         {
408             notify_waiter(partner,STATUS_SUCCESS);
409             release_object(partner);
410         }
411     }
412
413     release_object(user);
414 }
415
416 DECL_HANDLER(wait_named_pipe)
417 {
418     struct named_pipe *pipe;
419     struct pipe_user *partner;
420
421     if (!(pipe = open_named_pipe( get_req_data(), get_req_data_size() )))
422     {
423         set_error( STATUS_PIPE_NOT_AVAILABLE );
424         return;
425     }
426     if( (partner = find_partner(pipe,ps_wait_open)) )
427     {
428         /* this should use notify_waiter,
429            but no pipe_user object exists now... */
430         thread_queue_apc(current,NULL,req->func,
431                          APC_ASYNC, 1, req->overlapped, STATUS_SUCCESS, NULL);
432         release_object(partner);
433     }
434     else
435     {
436         struct pipe_user *user;
437
438         if( (user = create_pipe_user( pipe )) )
439         {
440             user->state = ps_wait_connect;
441             user->thread = (struct thread *)grab_object(current);
442             user->func = req->func;
443             user->overlapped = req->overlapped;
444             /* don't release it */
445         }
446     }
447     release_object(pipe);
448 }
449
450 DECL_HANDLER(disconnect_named_pipe)
451 {
452     struct pipe_user *user;
453
454     user = get_pipe_user_obj(current->process, req->handle, 0);
455     if(!user)
456         return;
457     if( (user->state == ps_connected_server) &&
458         (user->other->state == ps_connected_client) )
459     {
460         release_object( user->other->fd );
461         user->other->fd = NULL;
462         user->other->state = ps_disconnected;
463         user->other->other = NULL;
464
465         release_object( user->fd );
466         user->fd = NULL;
467         user->state = ps_idle_server;
468         user->other = NULL;
469     }
470     release_object(user);
471 }
472
473 DECL_HANDLER(get_named_pipe_info)
474 {
475     struct pipe_user *user;
476
477     user = get_pipe_user_obj(current->process, req->handle, 0);
478     if(!user)
479         return;
480
481     reply->flags        = user->pipe->pipemode;
482     reply->maxinstances = user->pipe->maxinstances;
483     reply->insize       = user->pipe->insize;
484     reply->outsize      = user->pipe->outsize;
485
486     release_object(user);
487 }