Added a small section about expressions in winedbg.
[wine.git] / server / request.c
blobcddf43808e224cde76bb728bee30ef3896e6183a
1 /*
2 * Server-side request handling
4 * Copyright (C) 1998 Alexandre Julliard
5 */
7 #include "config.h"
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>
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"
38 /* Some versions of glibc don't define this */
39 #ifndef SCM_RIGHTS
40 #define SCM_RIGHTS 1
41 #endif
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 */
48 struct master_socket
50 struct object obj; /* object header */
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 );
57 static const struct object_ops master_socket_ops =
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 */
74 struct thread *current = NULL; /* thread handling the current request */
75 unsigned int global_error = 0; /* global error code for when no thread is current */
77 static struct master_socket *master_socket; /* the master socket object */
79 /* socket communication static structures */
80 static struct iovec myiovec;
81 static struct msghdr msghdr;
82 #ifndef HAVE_MSGHDR_ACCRIGHTS
83 struct cmsg_fd
85 int len; /* sizeof structure */
86 int level; /* SOL_SOCKET */
87 int type; /* SCM_RIGHTS */
88 int fd; /* fd to pass */
90 static struct cmsg_fd cmsg = { sizeof(cmsg), SOL_SOCKET, SCM_RIGHTS, -1 };
91 #endif /* HAVE_MSGHDR_ACCRIGHTS */
93 /* complain about a protocol error and terminate the client connection */
94 void fatal_protocol_error( struct thread *thread, const char *err, ... )
96 va_list args;
98 va_start( args, err );
99 fprintf( stderr, "Protocol error:%p: ", thread );
100 vfprintf( stderr, err, args );
101 va_end( args );
102 thread->exit_code = 1;
103 kill_thread( thread, 1 );
106 /* complain about a protocol error and terminate the client connection */
107 void fatal_protocol_perror( struct thread *thread, const char *err, ... )
109 va_list args;
111 va_start( args, err );
112 fprintf( stderr, "Protocol error:%p: ", thread );
113 vfprintf( stderr, err, args );
114 perror( " " );
115 va_end( args );
116 thread->exit_code = 1;
117 kill_thread( thread, 1 );
120 /* die on a fatal error */
121 void fatal_error( const char *err, ... )
123 va_list args;
125 va_start( args, err );
126 fprintf( stderr, "wineserver: " );
127 vfprintf( stderr, err, args );
128 va_end( args );
129 exit(1);
132 /* die on a fatal error */
133 void fatal_perror( const char *err, ... )
135 va_list args;
137 va_start( args, err );
138 fprintf( stderr, "wineserver: " );
139 vfprintf( stderr, err, args );
140 perror( " " );
141 va_end( args );
142 exit(1);
145 /* call a request handler */
146 static inline void call_req_handler( struct thread *thread, union generic_request *request )
148 enum request req = request->header.req;
150 current = thread;
151 clear_error();
153 if (debug_level) trace_request( thread, request );
155 if (request->header.var_size)
157 if ((unsigned int)request->header.var_offset +
158 request->header.var_size > MAX_REQUEST_LENGTH)
160 fatal_protocol_error( current, "bad request offset/size %d/%d\n",
161 request->header.var_offset, request->header.var_size );
162 return;
166 if (req < REQ_NB_REQUESTS)
168 req_handlers[req]( request );
169 if (current) send_reply( current, request );
170 current = NULL;
171 return;
173 fatal_protocol_error( current, "bad request %d\n", req );
176 /* read a request from a thread */
177 void read_request( struct thread *thread )
179 union generic_request req;
180 int ret;
182 if ((ret = read( thread->obj.fd, &req, sizeof(req) )) == sizeof(req))
184 call_req_handler( thread, &req );
185 return;
187 if (!ret) /* closed pipe */
188 kill_thread( thread, 0 );
189 else if (ret > 0)
190 fatal_protocol_error( thread, "partial read %d\n", ret );
191 else
192 fatal_protocol_perror( thread, "read" );
195 /* send a reply to a thread */
196 void send_reply( struct thread *thread, union generic_request *request )
198 int ret;
200 if (debug_level) trace_reply( thread, request );
202 request->header.error = thread->error;
204 if ((ret = write( thread->reply_fd, request, sizeof(*request) )) != sizeof(*request))
206 if (ret >= 0)
207 fatal_protocol_error( thread, "partial write %d\n", ret );
208 else if (errno == EPIPE)
209 kill_thread( thread, 0 ); /* normal death */
210 else
211 fatal_protocol_perror( thread, "reply write" );
215 /* receive a file descriptor on the process socket */
216 int receive_fd( struct process *process )
218 struct send_fd data;
219 int fd, ret;
221 #ifdef HAVE_MSGHDR_ACCRIGHTS
222 msghdr.msg_accrightslen = sizeof(int);
223 msghdr.msg_accrights = (void *)&fd;
224 #else /* HAVE_MSGHDR_ACCRIGHTS */
225 msghdr.msg_control = &cmsg;
226 msghdr.msg_controllen = sizeof(cmsg);
227 cmsg.fd = -1;
228 #endif /* HAVE_MSGHDR_ACCRIGHTS */
230 myiovec.iov_base = &data;
231 myiovec.iov_len = sizeof(data);
233 ret = recvmsg( process->obj.fd, &msghdr, 0 );
234 #ifndef HAVE_MSGHDR_ACCRIGHTS
235 fd = cmsg.fd;
236 #endif
238 if (ret == sizeof(data))
240 struct thread *thread;
242 if (data.tid) thread = get_thread_from_id( data.tid );
243 else thread = (struct thread *)grab_object( process->thread_list );
245 if (!thread || thread->process != process)
247 if (debug_level)
248 fprintf( stderr, "%08x: *fd* %d <- %d bad thread id\n",
249 (unsigned int)data.tid, data.fd, fd );
250 close( fd );
252 else
254 if (debug_level)
255 fprintf( stderr, "%08x: *fd* %d <- %d\n",
256 (unsigned int)thread, data.fd, fd );
257 thread_add_inflight_fd( thread, data.fd, fd );
259 if (thread) release_object( thread );
260 return 0;
263 if (ret >= 0)
265 if (ret > 0)
266 fprintf( stderr, "Protocol error: process %p: partial recvmsg %d for fd\n",
267 process, ret );
268 kill_process( process, NULL, 1 );
270 else
272 if (errno != EWOULDBLOCK && errno != EAGAIN)
274 fprintf( stderr, "Protocol error: process %p: ", process );
275 perror( "recvmsg" );
276 kill_process( process, NULL, 1 );
279 return -1;
282 /* send an fd to a client */
283 int send_client_fd( struct process *process, int fd, handle_t handle )
285 int ret;
287 if (debug_level)
288 fprintf( stderr, "%08x: *fd* %d -> %d\n", (unsigned int)current, handle, fd );
290 #ifdef HAVE_MSGHDR_ACCRIGHTS
291 msghdr.msg_accrightslen = sizeof(fd);
292 msghdr.msg_accrights = (void *)&fd;
293 #else /* HAVE_MSGHDR_ACCRIGHTS */
294 msghdr.msg_control = &cmsg;
295 msghdr.msg_controllen = sizeof(cmsg);
296 cmsg.fd = fd;
297 #endif /* HAVE_MSGHDR_ACCRIGHTS */
299 myiovec.iov_base = (void *)&handle;
300 myiovec.iov_len = sizeof(handle);
302 ret = sendmsg( process->obj.fd, &msghdr, 0 );
304 if (ret == sizeof(handle)) return 0;
306 if (ret >= 0)
308 if (ret > 0)
309 fprintf( stderr, "Protocol error: process %p: partial sendmsg %d\n", process, ret );
310 kill_process( process, NULL, 1 );
312 else
314 fprintf( stderr, "Protocol error: process %p: ", process );
315 perror( "sendmsg" );
316 kill_process( process, NULL, 1 );
318 return -1;
321 static void master_socket_dump( struct object *obj, int verbose )
323 struct master_socket *sock = (struct master_socket *)obj;
324 assert( obj->ops == &master_socket_ops );
325 fprintf( stderr, "Master socket fd=%d\n", sock->obj.fd );
328 /* handle a socket event */
329 static void master_socket_poll_event( struct object *obj, int event )
331 struct master_socket *sock = (struct master_socket *)obj;
332 assert( obj->ops == &master_socket_ops );
334 assert( sock == master_socket ); /* there is only one master socket */
336 if (event & (POLLERR | POLLHUP))
338 /* this is not supposed to happen */
339 fprintf( stderr, "wineserver: Error on master socket\n" );
340 release_object( obj );
342 else if (event & POLLIN)
344 struct sockaddr_un dummy;
345 int len = sizeof(dummy);
346 int client = accept( master_socket->obj.fd, (struct sockaddr *) &dummy, &len );
347 if (client == -1) return;
348 fcntl( client, F_SETFL, O_NONBLOCK );
349 create_process( client );
353 /* remove the socket upon exit */
354 static void socket_cleanup(void)
356 static int do_it_once;
357 if (!do_it_once++) unlink( SOCKETNAME );
360 static void master_socket_destroy( struct object *obj )
362 socket_cleanup();
365 /* return the configuration directory ($WINEPREFIX or $HOME/.wine) */
366 const char *get_config_dir(void)
368 static char *confdir;
369 if (!confdir)
371 const char *prefix = getenv( "WINEPREFIX" );
372 if (prefix)
374 int len = strlen(prefix);
375 if (!(confdir = strdup( prefix ))) fatal_error( "out of memory\n" );
376 if (len > 1 && confdir[len-1] == '/') confdir[len-1] = 0;
378 else
380 const char *home = getenv( "HOME" );
381 if (!home)
383 struct passwd *pwd = getpwuid( getuid() );
384 if (!pwd) fatal_error( "could not find your home directory\n" );
385 home = pwd->pw_dir;
387 if (!(confdir = malloc( strlen(home) + strlen(CONFDIR) + 1 )))
388 fatal_error( "out of memory\n" );
389 strcpy( confdir, home );
390 strcat( confdir, CONFDIR );
392 mkdir( confdir, 0755 ); /* just in case */
394 return confdir;
397 /* create the server directory and chdir to it */
398 static void create_server_dir(void)
400 char hostname[64];
401 char *serverdir;
402 const char *confdir = get_config_dir();
403 struct stat st;
405 if (gethostname( hostname, sizeof(hostname) ) == -1) fatal_perror( "gethostname" );
407 if (!(serverdir = malloc( strlen(confdir) + strlen(SERVERDIR) + strlen(hostname) + 1 )))
408 fatal_error( "out of memory\n" );
410 strcpy( serverdir, confdir );
411 strcat( serverdir, SERVERDIR );
412 strcat( serverdir, hostname );
414 if (chdir( serverdir ) == -1)
416 if (errno != ENOENT) fatal_perror( "chdir %s", serverdir );
417 if (mkdir( serverdir, 0700 ) == -1) fatal_perror( "mkdir %s", serverdir );
418 if (chdir( serverdir ) == -1) fatal_perror( "chdir %s", serverdir );
420 if (stat( ".", &st ) == -1) fatal_perror( "stat %s", serverdir );
421 if (!S_ISDIR(st.st_mode)) fatal_error( "%s is not a directory\n", serverdir );
422 if (st.st_uid != getuid()) fatal_error( "%s is not owned by you\n", serverdir );
423 if (st.st_mode & 077) fatal_error( "%s must not be accessible by other users\n", serverdir );
426 /* open the master server socket and start waiting for new clients */
427 void open_master_socket(void)
429 struct sockaddr_un addr;
430 int fd, slen;
432 /* make sure no request is larger than the maximum size */
433 assert( sizeof(union generic_request) == sizeof(struct request_max_size) );
435 create_server_dir();
436 if ((fd = socket( AF_UNIX, SOCK_STREAM, 0 )) == -1) fatal_perror( "socket" );
437 addr.sun_family = AF_UNIX;
438 strcpy( addr.sun_path, SOCKETNAME );
439 slen = sizeof(addr) - sizeof(addr.sun_path) + strlen(addr.sun_path) + 1;
440 #ifdef HAVE_SOCKADDR_SUN_LEN
441 addr.sun_len = slen;
442 #endif
443 if (bind( fd, (struct sockaddr *)&addr, slen ) == -1)
445 if ((errno == EEXIST) || (errno == EADDRINUSE))
446 exit(0); /* pretend we succeeded to start */
447 else
448 fatal_perror( "bind" );
450 atexit( socket_cleanup );
452 chmod( SOCKETNAME, 0600 ); /* make sure no other user can connect */
453 if (listen( fd, 5 ) == -1) fatal_perror( "listen" );
455 if (!(master_socket = alloc_object( &master_socket_ops, fd )))
456 fatal_error( "out of memory\n" );
457 set_select_events( &master_socket->obj, POLLIN );
459 /* setup msghdr structure constant fields */
460 msghdr.msg_name = NULL;
461 msghdr.msg_namelen = 0;
462 msghdr.msg_iov = &myiovec;
463 msghdr.msg_iovlen = 1;
465 /* go in the background */
466 switch(fork())
468 case -1:
469 fatal_perror( "fork" );
470 case 0:
471 setsid();
472 break;
473 default:
474 _exit(0); /* do not call atexit functions */
478 /* close the master socket and stop waiting for new clients */
479 void close_master_socket(void)
481 /* if a new client is waiting, we keep on running */
482 if (!check_select_events( master_socket->obj.fd, POLLIN ))
483 release_object( master_socket );
486 /* lock/unlock the master socket to stop accepting new clients */
487 void lock_master_socket( int locked )
489 set_select_events( &master_socket->obj, locked ? 0 : POLLIN );