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