Rewrote Unix process launching to allow passing startup information to
[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
37 /* Some versions of glibc don't define this */
38 #ifndef SCM_RIGHTS
39 #define SCM_RIGHTS 1
40 #endif
41
42  /* path names for server master Unix socket */
43 #define CONFDIR    "/.wine"        /* directory for Wine config relative to $HOME */
44 #define SERVERDIR  "/wineserver-"  /* server socket directory (hostname appended) */
45 #define SOCKETNAME "socket"        /* name of the socket file */
46
47 struct master_socket
48 {
49     struct object       obj;         /* object header */
50 };
51
52 static void master_socket_dump( struct object *obj, int verbose );
53 static void master_socket_poll_event( struct object *obj, int event );
54 static void master_socket_destroy( struct object *obj );
55
56 static const struct object_ops master_socket_ops =
57 {
58     sizeof(struct master_socket),  /* size */
59     master_socket_dump,            /* dump */
60     no_add_queue,                  /* add_queue */
61     NULL,                          /* remove_queue */
62     NULL,                          /* signaled */
63     NULL,                          /* satisfied */
64     NULL,                          /* get_poll_events */
65     master_socket_poll_event,      /* poll_event */
66     no_read_fd,                    /* get_read_fd */
67     no_write_fd,                   /* get_write_fd */
68     no_flush,                      /* flush */
69     no_get_file_info,              /* get_file_info */
70     master_socket_destroy          /* destroy */
71 };
72
73
74 struct thread *current = NULL;  /* thread handling the current request */
75
76 static struct master_socket *master_socket;  /* the master socket object */
77
78 /* socket communication static structures */
79 static struct iovec myiovec;
80 static struct msghdr msghdr = { NULL, 0, &myiovec, 1, };
81 #ifndef HAVE_MSGHDR_ACCRIGHTS
82 struct cmsg_fd
83 {
84     int len;   /* sizeof structure */
85     int level; /* SOL_SOCKET */
86     int type;  /* SCM_RIGHTS */
87     int fd;    /* fd to pass */
88 };
89 static struct cmsg_fd cmsg = { sizeof(cmsg), SOL_SOCKET, SCM_RIGHTS, -1 };
90 #endif  /* HAVE_MSGHDR_ACCRIGHTS */
91
92 /* complain about a protocol error and terminate the client connection */
93 void fatal_protocol_error( struct thread *thread, const char *err, ... )
94 {
95     va_list args;
96
97     va_start( args, err );
98     fprintf( stderr, "Protocol error:%p: ", thread );
99     vfprintf( stderr, err, args );
100     va_end( args );
101     thread->exit_code = 1;
102     kill_thread( thread, 1 );
103 }
104
105 /* die on a fatal error */
106 static void fatal_error( const char *err, ... ) WINE_NORETURN;
107 static void fatal_error( const char *err, ... )
108 {
109     va_list args;
110
111     va_start( args, err );
112     fprintf( stderr, "wineserver: " );
113     vfprintf( stderr, err, args );
114     va_end( args );
115     exit(1);
116 }
117
118 /* die on a fatal error */
119 static void fatal_perror( const char *err, ... ) WINE_NORETURN;
120 static void fatal_perror( const char *err, ... )
121 {
122     va_list args;
123
124     va_start( args, err );
125     fprintf( stderr, "wineserver: " );
126     vfprintf( stderr, err, args );
127     perror( " " );
128     va_end( args );
129     exit(1);
130 }
131
132 /* call a request handler */
133 static inline void call_req_handler( struct thread *thread, enum request req )
134 {
135     current = thread;
136     clear_error();
137
138     if (debug_level) trace_request( req );
139
140     if (req < REQ_NB_REQUESTS)
141     {
142         req_handlers[req]( current->buffer );
143         if (current && !current->wait) send_reply( current );
144         current = NULL;
145         return;
146     }
147     fatal_protocol_error( current, "bad request %d\n", req );
148 }
149
150 /* set the fd to pass to the thread */
151 void set_reply_fd( struct thread *thread, int pass_fd )
152 {
153     assert( thread->pass_fd == -1 );
154     thread->pass_fd = pass_fd;
155 }
156
157 /* send a reply to a thread */
158 void send_reply( struct thread *thread )
159 {
160     assert( !thread->wait );
161     if (debug_level) trace_reply( thread );
162     if (!write_request( thread )) set_select_events( &thread->obj, POLLOUT );
163 }
164
165 /* read a message from a client that has something to say */
166 void read_request( struct thread *thread )
167 {
168     int ret;
169     enum request req;
170
171 #ifdef HAVE_MSGHDR_ACCRIGHTS
172     msghdr.msg_accrightslen = sizeof(int);
173     msghdr.msg_accrights = (void *)&thread->pass_fd;
174 #else  /* HAVE_MSGHDR_ACCRIGHTS */
175     msghdr.msg_control    = &cmsg;
176     msghdr.msg_controllen = sizeof(cmsg);
177     cmsg.fd = -1;
178 #endif  /* HAVE_MSGHDR_ACCRIGHTS */
179
180     assert( thread->pass_fd == -1 );
181
182     myiovec.iov_base = (void *)&req;
183     myiovec.iov_len  = sizeof(req);
184
185     ret = recvmsg( thread->obj.fd, &msghdr, 0 );
186 #ifndef HAVE_MSGHDR_ACCRIGHTS
187     thread->pass_fd = cmsg.fd;
188 #endif
189
190     if (ret == sizeof(req))
191     {
192         call_req_handler( thread, req );
193         thread->pass_fd = -1;
194         return;
195     }
196     if (ret == -1)
197     {
198         perror("recvmsg");
199         thread->exit_code = 1;
200         kill_thread( thread, 1 );
201         return;
202     }
203     if (!ret)  /* closed pipe */
204     {
205         kill_thread( thread, 0 );
206         return;
207     }
208     fatal_protocol_error( thread, "partial message received %d/%d\n", ret, sizeof(req) );
209 }
210
211 /* send a message to a client that is ready to receive something */
212 int write_request( struct thread *thread )
213 {
214     int ret;
215
216     if (thread->pass_fd == -1)
217     {
218         ret = write( thread->obj.fd, &thread->error, sizeof(thread->error) );
219     }
220     else  /* we have an fd to send */
221     {
222 #ifdef HAVE_MSGHDR_ACCRIGHTS
223         msghdr.msg_accrightslen = sizeof(int);
224         msghdr.msg_accrights = (void *)&thread->pass_fd;
225 #else  /* HAVE_MSGHDR_ACCRIGHTS */
226         msghdr.msg_control    = &cmsg;
227         msghdr.msg_controllen = sizeof(cmsg);
228         cmsg.fd = thread->pass_fd;
229 #endif  /* HAVE_MSGHDR_ACCRIGHTS */
230
231         myiovec.iov_base = (void *)&thread->error;
232         myiovec.iov_len  = sizeof(thread->error);
233
234         ret = sendmsg( thread->obj.fd, &msghdr, 0 );
235         close( thread->pass_fd );
236         thread->pass_fd = -1;
237     }
238     if (ret == sizeof(thread->error))
239     {
240         set_select_events( &thread->obj, POLLIN );
241         return 1;
242     }
243     if (ret == -1)
244     {
245         if (errno == EWOULDBLOCK) return 0;  /* not a fatal error */
246         if (errno == EPIPE)
247         {
248             kill_thread( thread, 0 );  /* normal death */
249         }
250         else
251         {
252             perror("sendmsg");
253             thread->exit_code = 1;
254             kill_thread( thread, 1 );
255         }
256     }
257     else
258     {
259         thread->exit_code = 1;
260         kill_thread( thread, 1 );
261         fprintf( stderr, "Partial message sent %d/%d\n", ret, sizeof(thread->error) );
262     }
263     return -1;
264 }
265
266 static void master_socket_dump( struct object *obj, int verbose )
267 {
268     struct master_socket *sock = (struct master_socket *)obj;
269     assert( obj->ops == &master_socket_ops );
270     fprintf( stderr, "Master socket fd=%d\n", sock->obj.fd );
271 }
272
273 /* handle a socket event */
274 static void master_socket_poll_event( struct object *obj, int event )
275 {
276     struct master_socket *sock = (struct master_socket *)obj;
277     assert( obj->ops == &master_socket_ops );
278
279     assert( sock == master_socket );  /* there is only one master socket */
280
281     if (event & (POLLERR | POLLHUP))
282     {
283         /* this is not supposed to happen */
284         fprintf( stderr, "wineserver: Error on master socket\n" );
285         release_object( obj );
286     }
287     else if (event & POLLIN)
288     {
289         struct sockaddr_un dummy;
290         int len = sizeof(dummy);
291         int client = accept( master_socket->obj.fd, (struct sockaddr *) &dummy, &len );
292         if (client != -1) create_process( client );
293     }
294 }
295
296 /* remove the socket upon exit */
297 static void socket_cleanup(void)
298 {
299     unlink( SOCKETNAME );
300 }
301
302 static void master_socket_destroy( struct object *obj )
303 {
304     socket_cleanup();
305 }
306
307 /* return the configuration directory ($WINEPREFIX or $HOME/.wine) */
308 static const char *get_config_dir(void)
309 {
310     static char *confdir;
311     if (!confdir)
312     {
313         const char *prefix = getenv( "WINEPREFIX" );
314         if (prefix)
315         {
316             int len = strlen(prefix);
317             if (!(confdir = strdup( prefix ))) fatal_error( "out of memory\n" );
318             if (len > 1 && confdir[len-1] == '/') confdir[len-1] = 0;
319         }
320         else
321         {
322             const char *home = getenv( "HOME" );
323             if (!home)
324             {
325                 struct passwd *pwd = getpwuid( getuid() );
326                 if (!pwd) fatal_error( "could not find your home directory\n" );
327                 home = pwd->pw_dir;
328             }
329             if (!(confdir = malloc( strlen(home) + strlen(CONFDIR) + 1 )))
330                 fatal_error( "out of memory\n" );
331             strcpy( confdir, home );
332             strcat( confdir, CONFDIR );
333         }
334         mkdir( confdir, 0755 );  /* just in case */
335     }
336     return confdir;
337 }
338
339 /* create the server directory and chdir to it */
340 static void create_server_dir(void)
341 {
342     char hostname[64];
343     char *serverdir;
344     const char *confdir = get_config_dir();
345     struct stat st;
346
347     if (gethostname( hostname, sizeof(hostname) ) == -1) fatal_perror( "gethostname" );
348
349     if (!(serverdir = malloc( strlen(confdir) + strlen(SERVERDIR) + strlen(hostname) + 1 )))
350         fatal_error( "out of memory\n" );
351
352     strcpy( serverdir, confdir );
353     strcat( serverdir, SERVERDIR );
354     strcat( serverdir, hostname );
355
356     if (chdir( serverdir ) == -1)
357     {
358         if (errno != ENOENT) fatal_perror( "chdir %s", serverdir );
359         if (mkdir( serverdir, 0700 ) == -1) fatal_perror( "mkdir %s", serverdir );
360         if (chdir( serverdir ) == -1) fatal_perror( "chdir %s", serverdir );
361     }
362     if (stat( ".", &st ) == -1) fatal_perror( "stat %s", serverdir );
363     if (!S_ISDIR(st.st_mode)) fatal_error( "%s is not a directory\n", serverdir );
364     if (st.st_uid != getuid()) fatal_error( "%s is not owned by you\n", serverdir );
365     if (st.st_mode & 077) fatal_error( "%s must not be accessible by other users\n", serverdir );
366 }
367
368 /* open the master server socket and start waiting for new clients */
369 void open_master_socket(void)
370 {
371     struct sockaddr_un addr;
372     int fd, slen;
373
374     create_server_dir();
375     if ((fd = socket( AF_UNIX, SOCK_STREAM, 0 )) == -1) fatal_perror( "socket" );
376     addr.sun_family = AF_UNIX;
377     strcpy( addr.sun_path, SOCKETNAME );
378     slen = sizeof(addr) - sizeof(addr.sun_path) + strlen(addr.sun_path) + 1;
379 #ifdef HAVE_SOCKADDR_SUN_LEN
380     addr.sun_len = slen;
381 #endif
382     if (bind( fd, (struct sockaddr *)&addr, slen ) == -1)
383     {
384         if ((errno == EEXIST) || (errno == EADDRINUSE))
385             fatal_error( "another server is already running\n" );
386         else
387             fatal_perror( "bind" );
388     }
389     atexit( socket_cleanup );
390
391     chmod( SOCKETNAME, 0600 );  /* make sure no other user can connect */
392     if (listen( fd, 5 ) == -1) fatal_perror( "listen" );
393
394     if (!(master_socket = alloc_object( &master_socket_ops, fd )))
395         fatal_error( "out of memory\n" );
396     set_select_events( &master_socket->obj, POLLIN );
397
398     /* go in the background */
399     switch(fork())
400     {
401     case -1:
402         fatal_perror( "fork" );
403     case 0:
404         setsid();
405         break;
406     default:
407         _exit(0);  /* do not call atexit functions */
408     }
409 }
410
411 /* close the master socket and stop waiting for new clients */
412 void close_master_socket(void)
413 {
414     release_object( master_socket );
415 }
416
417 /* lock/unlock the master socket to stop accepting new clients */
418 void lock_master_socket( int locked )
419 {
420     set_select_events( &master_socket->obj, locked ? 0 : POLLIN );
421 }