New set of macros for server calls; makes requests without variable
[wine] / server / request.c
1 /*
2  * Server-side request handling
3  *
4  * Copyright (C) 1998 Alexandre Julliard
5  */
6
7 #include "config.h"
8
9 #include <assert.h>
10 #include <errno.h>
11 #include <fcntl.h>
12 #include <pwd.h>
13 #include <signal.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <stdarg.h>
17 #include <string.h>
18 #include <sys/stat.h>
19 #include <sys/time.h>
20 #include <sys/types.h>
21 #ifdef HAVE_SYS_SOCKET_H
22 # include <sys/socket.h>
23 #endif
24 #include <sys/uio.h>
25 #include <sys/un.h>
26 #include <unistd.h>
27
28 #include "winnt.h"
29 #include "winbase.h"
30 #include "wincon.h"
31 #include "thread.h"
32 #include "process.h"
33 #include "server.h"
34 #define WANT_REQUEST_HANDLERS
35 #include "request.h"
36 #include "wine/port.h"
37
38 /* Some versions of glibc don't define this */
39 #ifndef SCM_RIGHTS
40 #define SCM_RIGHTS 1
41 #endif
42
43  /* path names for server master Unix socket */
44 #define CONFDIR    "/.wine"        /* directory for Wine config relative to $HOME */
45 #define SERVERDIR  "/wineserver-"  /* server socket directory (hostname appended) */
46 #define SOCKETNAME "socket"        /* name of the socket file */
47
48 struct master_socket
49 {
50     struct object       obj;         /* object header */
51 };
52
53 static void master_socket_dump( struct object *obj, int verbose );
54 static void master_socket_poll_event( struct object *obj, int event );
55 static void master_socket_destroy( struct object *obj );
56
57 static const struct object_ops master_socket_ops =
58 {
59     sizeof(struct master_socket),  /* size */
60     master_socket_dump,            /* dump */
61     no_add_queue,                  /* add_queue */
62     NULL,                          /* remove_queue */
63     NULL,                          /* signaled */
64     NULL,                          /* satisfied */
65     NULL,                          /* get_poll_events */
66     master_socket_poll_event,      /* poll_event */
67     no_get_fd,                     /* get_fd */
68     no_flush,                      /* flush */
69     no_get_file_info,              /* get_file_info */
70     master_socket_destroy          /* destroy */
71 };
72
73
74 struct request_socket
75 {
76     struct object       obj;         /* object header */
77     struct thread      *thread;      /* owning thread */
78 };
79
80 static void request_socket_dump( struct object *obj, int verbose );
81 static void request_socket_poll_event( struct object *obj, int event );
82
83 static const struct object_ops request_socket_ops =
84 {
85     sizeof(struct request_socket), /* size */
86     request_socket_dump,           /* dump */
87     no_add_queue,                  /* add_queue */
88     NULL,                          /* remove_queue */
89     NULL,                          /* signaled */
90     NULL,                          /* satisfied */
91     NULL,                          /* get_poll_events */
92     request_socket_poll_event,     /* poll_event */
93     no_get_fd,                     /* get_fd */
94     no_flush,                      /* flush */
95     no_get_file_info,              /* get_file_info */
96     no_destroy                     /* destroy */
97 };
98
99
100 struct thread *current = NULL;  /* thread handling the current request */
101 unsigned int global_error = 0;  /* global error code for when no thread is current */
102
103 static struct master_socket *master_socket;  /* the master socket object */
104
105 /* socket communication static structures */
106 static struct iovec myiovec;
107 static struct msghdr msghdr;
108 #ifndef HAVE_MSGHDR_ACCRIGHTS
109 struct cmsg_fd
110 {
111     int len;   /* sizeof structure */
112     int level; /* SOL_SOCKET */
113     int type;  /* SCM_RIGHTS */
114     int fd;    /* fd to pass */
115 };
116 static struct cmsg_fd cmsg = { sizeof(cmsg), SOL_SOCKET, SCM_RIGHTS, -1 };
117 #endif  /* HAVE_MSGHDR_ACCRIGHTS */
118
119 /* complain about a protocol error and terminate the client connection */
120 void fatal_protocol_error( struct thread *thread, const char *err, ... )
121 {
122     va_list args;
123
124     va_start( args, err );
125     fprintf( stderr, "Protocol error:%p: ", thread );
126     vfprintf( stderr, err, args );
127     va_end( args );
128     thread->exit_code = 1;
129     kill_thread( thread, 1 );
130 }
131
132 /* complain about a protocol error and terminate the client connection */
133 void fatal_protocol_perror( struct thread *thread, const char *err, ... )
134 {
135     va_list args;
136
137     va_start( args, err );
138     fprintf( stderr, "Protocol error:%p: ", thread );
139     vfprintf( stderr, err, args );
140     perror( " " );
141     va_end( args );
142     thread->exit_code = 1;
143     kill_thread( thread, 1 );
144 }
145
146 /* die on a fatal error */
147 void fatal_error( const char *err, ... )
148 {
149     va_list args;
150
151     va_start( args, err );
152     fprintf( stderr, "wineserver: " );
153     vfprintf( stderr, err, args );
154     va_end( args );
155     exit(1);
156 }
157
158 /* die on a fatal error */
159 void fatal_perror( const char *err, ... )
160 {
161     va_list args;
162
163     va_start( args, err );
164     fprintf( stderr, "wineserver: " );
165     vfprintf( stderr, err, args );
166     perror( " " );
167     va_end( args );
168     exit(1);
169 }
170
171 /* call a request handler */
172 static inline void call_req_handler( struct thread *thread, union generic_request *request )
173 {
174     enum request req = request->header.req;
175
176     current = thread;
177     clear_error();
178
179     if (debug_level) trace_request( thread, request );
180
181     if (request->header.var_size)
182     {
183         if ((unsigned int)request->header.var_offset +
184                           request->header.var_size > MAX_REQUEST_LENGTH)
185         {
186             fatal_protocol_error( current, "bad request offset/size %d/%d\n",
187                                   request->header.var_offset, request->header.var_size );
188             return;
189         }
190     }
191
192     if (req < REQ_NB_REQUESTS)
193     {
194         req_handlers[req]( request );
195         if (current) send_reply( current, request );
196         current = NULL;
197         return;
198     }
199     fatal_protocol_error( current, "bad request %d\n", req );
200 }
201
202 /* send a reply to a thread */
203 void send_reply( struct thread *thread, union generic_request *request )
204 {
205     int ret;
206
207     assert (thread->pass_fd == -1);
208
209     if (debug_level) trace_reply( thread, request );
210
211     request->header.error = thread->error;
212
213     if ((ret = write( thread->reply_fd, request, sizeof(*request) )) != sizeof(*request))
214     {
215         if (ret >= 0)
216             fatal_protocol_error( thread, "partial write %d\n", ret );
217         else if (errno == EPIPE)
218             kill_thread( thread, 0 );  /* normal death */
219         else
220             fatal_protocol_perror( thread, "sendmsg" );
221     }
222 }
223
224 /* read a message from a client that has something to say */
225 void read_request( struct thread *thread )
226 {
227     union generic_request req;
228     int ret;
229
230 #ifdef HAVE_MSGHDR_ACCRIGHTS
231     msghdr.msg_accrightslen = sizeof(int);
232     msghdr.msg_accrights = (void *)&thread->pass_fd;
233 #else  /* HAVE_MSGHDR_ACCRIGHTS */
234     msghdr.msg_control    = &cmsg;
235     msghdr.msg_controllen = sizeof(cmsg);
236     cmsg.fd = -1;
237 #endif  /* HAVE_MSGHDR_ACCRIGHTS */
238
239     assert( thread->pass_fd == -1 );
240
241     myiovec.iov_base = &req;
242     myiovec.iov_len  = sizeof(req);
243
244     ret = recvmsg( thread->obj.fd, &msghdr, 0 );
245 #ifndef HAVE_MSGHDR_ACCRIGHTS
246     thread->pass_fd = cmsg.fd;
247 #endif
248
249     if (ret == sizeof(req))
250     {
251         call_req_handler( thread, &req );
252         thread->pass_fd = -1;
253         return;
254     }
255     if (!ret)  /* closed pipe */
256         kill_thread( thread, 0 );
257     else if (ret > 0)
258         fatal_protocol_error( thread, "partial recvmsg %d\n", ret );
259     else
260         fatal_protocol_perror( thread, "recvmsg" );
261 }
262
263 /* send the wakeup signal to a thread */
264 int send_thread_wakeup( struct thread *thread, int signaled )
265 {
266     int ret = write( thread->wait_fd, &signaled, sizeof(signaled) );
267     if (ret == sizeof(signaled)) return 0;
268     if (ret >= 0)
269         fatal_protocol_error( thread, "partial wakeup write %d\n", ret );
270     else if (errno == EPIPE)
271         kill_thread( thread, 0 );  /* normal death */
272     else
273         fatal_protocol_perror( thread, "write" );
274     return -1;
275 }
276
277 /* send an fd to a client */
278 int send_client_fd( struct thread *thread, int fd, handle_t handle )
279 {
280     int ret;
281
282     if (debug_level)
283         fprintf( stderr, "%08x: *fd* %d = %d\n", (unsigned int)thread, handle, fd );
284
285 #ifdef HAVE_MSGHDR_ACCRIGHTS
286     msghdr.msg_accrightslen = sizeof(fd);
287     msghdr.msg_accrights = (void *)&fd;
288 #else  /* HAVE_MSGHDR_ACCRIGHTS */
289     msghdr.msg_control    = &cmsg;
290     msghdr.msg_controllen = sizeof(cmsg);
291     cmsg.fd = fd;
292 #endif  /* HAVE_MSGHDR_ACCRIGHTS */
293
294     myiovec.iov_base = (void *)&handle;
295     myiovec.iov_len  = sizeof(handle);
296
297     ret = sendmsg( thread->obj.fd, &msghdr, 0 );
298
299     if (ret > 0) return 0;
300     if (errno == EPIPE)
301     {
302         kill_thread( thread, 0 );  /* normal death */
303     }
304     else
305     {
306         fatal_protocol_perror( thread, "sendmsg" );
307     }
308     return -1;
309 }
310
311 static void master_socket_dump( struct object *obj, int verbose )
312 {
313     struct master_socket *sock = (struct master_socket *)obj;
314     assert( obj->ops == &master_socket_ops );
315     fprintf( stderr, "Master socket fd=%d\n", sock->obj.fd );
316 }
317
318 /* handle a socket event */
319 static void master_socket_poll_event( struct object *obj, int event )
320 {
321     struct master_socket *sock = (struct master_socket *)obj;
322     assert( obj->ops == &master_socket_ops );
323
324     assert( sock == master_socket );  /* there is only one master socket */
325
326     if (event & (POLLERR | POLLHUP))
327     {
328         /* this is not supposed to happen */
329         fprintf( stderr, "wineserver: Error on master socket\n" );
330         release_object( obj );
331     }
332     else if (event & POLLIN)
333     {
334         struct sockaddr_un dummy;
335         int len = sizeof(dummy);
336         int client = accept( master_socket->obj.fd, (struct sockaddr *) &dummy, &len );
337         if (client != -1) create_process( client );
338     }
339 }
340
341 /* remove the socket upon exit */
342 static void socket_cleanup(void)
343 {
344     static int do_it_once;
345     if (!do_it_once++) unlink( SOCKETNAME );
346 }
347
348 static void master_socket_destroy( struct object *obj )
349 {
350     socket_cleanup();
351 }
352
353 static void request_socket_dump( struct object *obj, int verbose )
354 {
355     struct request_socket *sock = (struct request_socket *)obj;
356     assert( obj->ops == &request_socket_ops );
357     fprintf( stderr, "Request socket fd=%d thread=%p\n", sock->obj.fd, sock->thread );
358 }
359
360 /* handle a request socket event */
361 static void request_socket_poll_event( struct object *obj, int event )
362 {
363     struct request_socket *sock = (struct request_socket *)obj;
364     assert( obj->ops == &request_socket_ops );
365
366     if (event & (POLLERR | POLLHUP)) kill_thread( sock->thread, 0 );
367     else if (event & POLLIN)
368     {
369         struct thread *thread = sock->thread;
370         union generic_request req;
371         int ret;
372
373         if ((ret = read( sock->obj.fd, &req, sizeof(req) )) == sizeof(req))
374         {
375             call_req_handler( thread, &req );
376             return;
377         }
378         if (!ret)  /* closed pipe */
379             kill_thread( thread, 0 );
380         else if (ret > 0)
381             fatal_protocol_error( thread, "partial read %d\n", ret );
382         else
383             fatal_protocol_perror( thread, "read" );
384     }
385 }
386
387 /* create a request socket and send the fd to the client thread */
388 struct object *create_request_socket( struct thread *thread )
389 {
390     struct request_socket *sock;
391     int fd[2];
392
393     if (pipe( fd )) return NULL;
394     if (!(sock = alloc_object( &request_socket_ops, fd[0] )))
395     {
396         close( fd[1] );
397         return NULL;
398     }
399     sock->thread = thread;
400     send_client_fd( thread, fd[1], 0 );
401     close( fd[1] );
402     fcntl( fd[0], F_SETFL, O_NONBLOCK );
403     set_select_events( &sock->obj, POLLIN );
404     return &sock->obj;
405 }
406
407 /* return the configuration directory ($WINEPREFIX or $HOME/.wine) */
408 const char *get_config_dir(void)
409 {
410     static char *confdir;
411     if (!confdir)
412     {
413         const char *prefix = getenv( "WINEPREFIX" );
414         if (prefix)
415         {
416             int len = strlen(prefix);
417             if (!(confdir = strdup( prefix ))) fatal_error( "out of memory\n" );
418             if (len > 1 && confdir[len-1] == '/') confdir[len-1] = 0;
419         }
420         else
421         {
422             const char *home = getenv( "HOME" );
423             if (!home)
424             {
425                 struct passwd *pwd = getpwuid( getuid() );
426                 if (!pwd) fatal_error( "could not find your home directory\n" );
427                 home = pwd->pw_dir;
428             }
429             if (!(confdir = malloc( strlen(home) + strlen(CONFDIR) + 1 )))
430                 fatal_error( "out of memory\n" );
431             strcpy( confdir, home );
432             strcat( confdir, CONFDIR );
433         }
434         mkdir( confdir, 0755 );  /* just in case */
435     }
436     return confdir;
437 }
438
439 /* create the server directory and chdir to it */
440 static void create_server_dir(void)
441 {
442     char hostname[64];
443     char *serverdir;
444     const char *confdir = get_config_dir();
445     struct stat st;
446
447     if (gethostname( hostname, sizeof(hostname) ) == -1) fatal_perror( "gethostname" );
448
449     if (!(serverdir = malloc( strlen(confdir) + strlen(SERVERDIR) + strlen(hostname) + 1 )))
450         fatal_error( "out of memory\n" );
451
452     strcpy( serverdir, confdir );
453     strcat( serverdir, SERVERDIR );
454     strcat( serverdir, hostname );
455
456     if (chdir( serverdir ) == -1)
457     {
458         if (errno != ENOENT) fatal_perror( "chdir %s", serverdir );
459         if (mkdir( serverdir, 0700 ) == -1) fatal_perror( "mkdir %s", serverdir );
460         if (chdir( serverdir ) == -1) fatal_perror( "chdir %s", serverdir );
461     }
462     if (stat( ".", &st ) == -1) fatal_perror( "stat %s", serverdir );
463     if (!S_ISDIR(st.st_mode)) fatal_error( "%s is not a directory\n", serverdir );
464     if (st.st_uid != getuid()) fatal_error( "%s is not owned by you\n", serverdir );
465     if (st.st_mode & 077) fatal_error( "%s must not be accessible by other users\n", serverdir );
466 }
467
468 /* open the master server socket and start waiting for new clients */
469 void open_master_socket(void)
470 {
471     struct sockaddr_un addr;
472     int fd, slen;
473
474     /* make sure no request is larger than the maximum size */
475     assert( sizeof(union generic_request) == sizeof(struct request_max_size) );
476
477     create_server_dir();
478     if ((fd = socket( AF_UNIX, SOCK_STREAM, 0 )) == -1) fatal_perror( "socket" );
479     addr.sun_family = AF_UNIX;
480     strcpy( addr.sun_path, SOCKETNAME );
481     slen = sizeof(addr) - sizeof(addr.sun_path) + strlen(addr.sun_path) + 1;
482 #ifdef HAVE_SOCKADDR_SUN_LEN
483     addr.sun_len = slen;
484 #endif
485     if (bind( fd, (struct sockaddr *)&addr, slen ) == -1)
486     {
487         if ((errno == EEXIST) || (errno == EADDRINUSE))
488             exit(0);  /* pretend we succeeded to start */
489         else
490             fatal_perror( "bind" );
491     }
492     atexit( socket_cleanup );
493
494     chmod( SOCKETNAME, 0600 );  /* make sure no other user can connect */
495     if (listen( fd, 5 ) == -1) fatal_perror( "listen" );
496
497     if (!(master_socket = alloc_object( &master_socket_ops, fd )))
498         fatal_error( "out of memory\n" );
499     set_select_events( &master_socket->obj, POLLIN );
500
501     /* setup msghdr structure constant fields */
502     msghdr.msg_name    = NULL;
503     msghdr.msg_namelen = 0;
504     msghdr.msg_iov     = &myiovec;
505     msghdr.msg_iovlen  = 1;
506
507     /* go in the background */
508     switch(fork())
509     {
510     case -1:
511         fatal_perror( "fork" );
512     case 0:
513         setsid();
514         break;
515     default:
516         _exit(0);  /* do not call atexit functions */
517     }
518 }
519
520 /* close the master socket and stop waiting for new clients */
521 void close_master_socket(void)
522 {
523     /* if a new client is waiting, we keep on running */
524     if (!check_select_events( master_socket->obj.fd, POLLIN ))
525         release_object( master_socket );
526 }
527
528 /* lock/unlock the master socket to stop accepting new clients */
529 void lock_master_socket( int locked )
530 {
531     set_select_events( &master_socket->obj, locked ? 0 : POLLIN );
532 }