Include config.h.
[wine/gsoc_dplay.git] / scheduler / client.c
blobfc69779ba265ee6c13186b6ce1dee589489eba4b
1 /*
2 * Client part of the client/server communication
4 * Copyright (C) 1998 Alexandre Julliard
5 */
7 #include "config.h"
9 #include <assert.h>
10 #include <ctype.h>
11 #include <errno.h>
12 #include <fcntl.h>
13 #include <pwd.h>
14 #include <signal.h>
15 #include <stdio.h>
16 #include <string.h>
17 #include <sys/types.h>
18 #ifdef HAVE_SYS_SOCKET_H
19 # include <sys/socket.h>
20 #endif
21 #ifdef HAVE_SYS_WAIT_H
22 #include <sys/wait.h>
23 #endif
24 #include <sys/un.h>
25 #ifdef HAVE_SYS_MMAN_H
26 #include <sys/mman.h>
27 #endif
28 #include <sys/stat.h>
29 #include <sys/uio.h>
30 #include <unistd.h>
31 #include <stdarg.h>
33 #include "wine/port.h"
34 #include "process.h"
35 #include "thread.h"
36 #include "server.h"
37 #include "winerror.h"
38 #include "options.h"
40 /* Some versions of glibc don't define this */
41 #ifndef SCM_RIGHTS
42 #define SCM_RIGHTS 1
43 #endif
45 #define CONFDIR "/.wine" /* directory for Wine config relative to $HOME */
46 #define SERVERDIR "/wineserver-" /* server socket directory (hostname appended) */
47 #define SOCKETNAME "socket" /* name of the socket file */
49 #undef server_alloc_req
51 /* data structure used to pass an fd with sendmsg/recvmsg */
52 struct cmsg_fd
54 int len; /* sizeof structure */
55 int level; /* SOL_SOCKET */
56 int type; /* SCM_RIGHTS */
57 int fd; /* fd to pass */
60 static void *boot_thread_id;
63 /* die on a fatal error; use only during initialization */
64 static void fatal_error( const char *err, ... ) WINE_NORETURN;
65 static void fatal_error( const char *err, ... )
67 va_list args;
69 va_start( args, err );
70 fprintf( stderr, "wine: " );
71 vfprintf( stderr, err, args );
72 va_end( args );
73 exit(1);
76 /* die on a fatal error; use only during initialization */
77 static void fatal_perror( const char *err, ... ) WINE_NORETURN;
78 static void fatal_perror( const char *err, ... )
80 va_list args;
82 va_start( args, err );
83 fprintf( stderr, "wine: " );
84 vfprintf( stderr, err, args );
85 perror( " " );
86 va_end( args );
87 exit(1);
90 /***********************************************************************
91 * server_protocol_error
93 void server_protocol_error( const char *err, ... )
95 va_list args;
97 va_start( args, err );
98 fprintf( stderr, "Client protocol error:%p: ", NtCurrentTeb()->tid );
99 vfprintf( stderr, err, args );
100 va_end( args );
101 SYSDEPS_ExitThread(1);
105 /***********************************************************************
106 * server_perror
108 static void server_perror( const char *err )
110 fprintf( stderr, "Client protocol error:%p: ", NtCurrentTeb()->tid );
111 perror( err );
112 SYSDEPS_ExitThread(1);
116 /***********************************************************************
117 * __wine_server_exception_handler
119 DWORD __wine_server_exception_handler( PEXCEPTION_RECORD record, EXCEPTION_FRAME *frame,
120 CONTEXT *context, EXCEPTION_FRAME **pdispatcher )
122 struct __server_exception_frame *server_frame = (struct __server_exception_frame *)frame;
123 if ((record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
124 *NtCurrentTeb()->buffer_info = server_frame->info;
125 return ExceptionContinueSearch;
129 /***********************************************************************
130 * wine_server_alloc_req
132 void *wine_server_alloc_req( size_t fixed_size, size_t var_size )
134 unsigned int pos = NtCurrentTeb()->buffer_info->cur_pos;
135 union generic_request *req = (union generic_request *)((char *)NtCurrentTeb()->buffer + pos);
136 size_t size = sizeof(*req) + var_size;
138 assert( fixed_size <= sizeof(*req) );
139 assert( var_size <= REQUEST_MAX_VAR_SIZE );
141 if ((char *)req + size > (char *)NtCurrentTeb()->buffer_info)
142 server_protocol_error( "buffer overflow %d bytes\n",
143 (char *)req + size - (char *)NtCurrentTeb()->buffer_info );
144 NtCurrentTeb()->buffer_info->cur_pos = pos + size;
145 req->header.fixed_size = fixed_size;
146 req->header.var_size = var_size;
147 return req;
151 /***********************************************************************
152 * send_request
154 * Send a request to the server.
156 static void send_request( enum request req, struct request_header *header )
158 header->req = req;
159 NtCurrentTeb()->buffer_info->cur_req = (char *)header - (char *)NtCurrentTeb()->buffer;
160 /* write a single byte; the value is ignored anyway */
161 if (write( NtCurrentTeb()->socket, header, 1 ) == -1)
163 if (errno == EPIPE) SYSDEPS_ExitThread(0);
164 server_perror( "sendmsg" );
168 /***********************************************************************
169 * send_request_fd
171 * Send a request to the server, passing a file descriptor.
173 static void send_request_fd( enum request req, struct request_header *header, int fd )
175 #ifndef HAVE_MSGHDR_ACCRIGHTS
176 struct cmsg_fd cmsg;
177 #endif
178 struct msghdr msghdr;
179 struct iovec vec;
181 /* write a single byte; the value is ignored anyway */
182 vec.iov_base = (void *)header;
183 vec.iov_len = 1;
185 msghdr.msg_name = NULL;
186 msghdr.msg_namelen = 0;
187 msghdr.msg_iov = &vec;
188 msghdr.msg_iovlen = 1;
190 #ifdef HAVE_MSGHDR_ACCRIGHTS
191 msghdr.msg_accrights = (void *)&fd;
192 msghdr.msg_accrightslen = sizeof(fd);
193 #else /* HAVE_MSGHDR_ACCRIGHTS */
194 cmsg.len = sizeof(cmsg);
195 cmsg.level = SOL_SOCKET;
196 cmsg.type = SCM_RIGHTS;
197 cmsg.fd = fd;
198 msghdr.msg_control = &cmsg;
199 msghdr.msg_controllen = sizeof(cmsg);
200 msghdr.msg_flags = 0;
201 #endif /* HAVE_MSGHDR_ACCRIGHTS */
203 header->req = req;
205 if (sendmsg( NtCurrentTeb()->socket, &msghdr, 0 ) == -1)
207 if (errno == EPIPE) SYSDEPS_ExitThread(0);
208 server_perror( "sendmsg" );
212 /***********************************************************************
213 * wait_reply
215 * Wait for a reply from the server.
217 static void wait_reply(void)
219 int ret;
220 char dummy[1];
222 for (;;)
224 if ((ret = read( NtCurrentTeb()->socket, dummy, 1 )) > 0) return;
225 if (!ret) break;
226 if (errno == EINTR) continue;
227 if (errno == EPIPE) break;
228 server_perror("read");
230 /* the server closed the connection; time to die... */
231 SYSDEPS_ExitThread(0);
235 /***********************************************************************
236 * wait_reply_fd
238 * Wait for a reply from the server, when a file descriptor is passed.
240 static void wait_reply_fd( int *fd )
242 struct iovec vec;
243 int ret;
244 char dummy[1];
246 #ifdef HAVE_MSGHDR_ACCRIGHTS
247 struct msghdr msghdr;
249 *fd = -1;
250 msghdr.msg_accrights = (void *)fd;
251 msghdr.msg_accrightslen = sizeof(*fd);
252 #else /* HAVE_MSGHDR_ACCRIGHTS */
253 struct msghdr msghdr;
254 struct cmsg_fd cmsg;
256 cmsg.len = sizeof(cmsg);
257 cmsg.level = SOL_SOCKET;
258 cmsg.type = SCM_RIGHTS;
259 cmsg.fd = -1;
260 msghdr.msg_control = &cmsg;
261 msghdr.msg_controllen = sizeof(cmsg);
262 msghdr.msg_flags = 0;
263 #endif /* HAVE_MSGHDR_ACCRIGHTS */
265 msghdr.msg_name = NULL;
266 msghdr.msg_namelen = 0;
267 msghdr.msg_iov = &vec;
268 msghdr.msg_iovlen = 1;
269 vec.iov_base = (void *)dummy;
270 vec.iov_len = 1;
272 for (;;)
274 if ((ret = recvmsg( NtCurrentTeb()->socket, &msghdr, 0 )) > 0)
276 #ifndef HAVE_MSGHDR_ACCRIGHTS
277 *fd = cmsg.fd;
278 #endif
279 return;
281 if (!ret) break;
282 if (errno == EINTR) continue;
283 if (errno == EPIPE) break;
284 server_perror("recvmsg");
286 /* the server closed the connection; time to die... */
287 SYSDEPS_ExitThread(0);
291 /***********************************************************************
292 * wine_server_call
294 * Perform a server call.
296 unsigned int wine_server_call( enum request req )
298 void *req_ptr = get_req_buffer();
299 send_request( req, req_ptr );
300 wait_reply();
301 return ((struct request_header *)req_ptr)->error;
305 /***********************************************************************
306 * server_call_fd
308 * Perform a server call, passing a file descriptor.
309 * If *fd is != -1, it will be passed to the server.
310 * If the server passes an fd, it will be stored into *fd.
312 unsigned int server_call_fd( enum request req, int fd_out, int *fd_in )
314 unsigned int res;
315 void *req_ptr = get_req_buffer();
317 if (fd_out == -1) send_request( req, req_ptr );
318 else send_request_fd( req, req_ptr, fd_out );
320 if (fd_in) wait_reply_fd( fd_in );
321 else wait_reply();
323 if ((res = ((struct request_header *)req_ptr)->error))
324 SetLastError( RtlNtStatusToDosError(res) );
325 return res; /* error code */
329 /***********************************************************************
330 * get_config_dir
332 * Return the configuration directory ($WINEPREFIX or $HOME/.wine)
334 const char *get_config_dir(void)
336 static char *confdir;
337 if (!confdir)
339 const char *prefix = getenv( "WINEPREFIX" );
340 if (prefix)
342 int len = strlen(prefix);
343 if (!(confdir = strdup( prefix ))) fatal_error( "out of memory\n" );
344 if (len > 1 && confdir[len-1] == '/') confdir[len-1] = 0;
346 else
348 const char *home = getenv( "HOME" );
349 if (!home)
351 struct passwd *pwd = getpwuid( getuid() );
352 if (!pwd) fatal_error( "could not find your home directory\n" );
353 home = pwd->pw_dir;
355 if (!(confdir = malloc( strlen(home) + strlen(CONFDIR) + 1 )))
356 fatal_error( "out of memory\n" );
357 strcpy( confdir, home );
358 strcat( confdir, CONFDIR );
360 mkdir( confdir, 0755 ); /* just in case */
362 return confdir;
366 /***********************************************************************
367 * start_server
369 * Start a new wine server.
371 static void start_server( const char *oldcwd )
373 static int started; /* we only try once */
374 char *path, *p;
375 if (!started)
377 int status;
378 int pid = fork();
379 if (pid == -1) fatal_perror( "fork" );
380 if (!pid)
382 /* if server is explicitly specified, use this */
383 if ((p = getenv("WINESERVER")))
385 execl( p, "wineserver", NULL );
386 fatal_perror( "could not exec the server '%s'\n"
387 " specified in the WINESERVER environment variable", p );
390 /* first try the installation dir */
391 execl( BINDIR "/wineserver", "wineserver", NULL );
393 /* now try the dir we were launched from */
394 if (full_argv0)
396 if (!(path = malloc( strlen(full_argv0) + 20 )))
397 fatal_error( "out of memory\n" );
398 if ((p = strrchr( strcpy( path, full_argv0 ), '/' )))
400 strcpy( p, "/wineserver" );
401 execl( path, "wineserver", NULL );
402 strcpy( p, "/server/wineserver" );
403 execl( path, "wineserver", NULL );
405 free(path);
408 /* now try the path */
409 execlp( "wineserver", "wineserver", NULL );
411 /* and finally the current dir */
412 if (!(path = malloc( strlen(oldcwd) + 20 )))
413 fatal_error( "out of memory\n" );
414 p = strcpy( path, oldcwd ) + strlen( oldcwd );
415 strcpy( p, "/wineserver" );
416 execl( path, "wineserver", NULL );
417 strcpy( p, "/server/wineserver" );
418 execl( path, "wineserver", NULL );
419 free(path);
420 fatal_error( "could not exec wineserver\n" );
422 started = 1;
423 waitpid( pid, &status, 0 );
424 status = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
425 if (status) exit(status); /* server failed */
429 /***********************************************************************
430 * server_connect
432 * Attempt to connect to an existing server socket.
433 * We need to be in the server directory already.
435 static int server_connect( const char *oldcwd, const char *serverdir )
437 struct sockaddr_un addr;
438 struct stat st;
439 int s, slen, retry;
441 /* chdir to the server directory */
442 if (chdir( serverdir ) == -1)
444 if (errno != ENOENT) fatal_perror( "chdir to %s", serverdir );
445 start_server( "." );
446 if (chdir( serverdir ) == -1) fatal_perror( "chdir to %s", serverdir );
449 /* make sure we are at the right place */
450 if (stat( ".", &st ) == -1) fatal_perror( "stat %s", serverdir );
451 if (st.st_uid != getuid()) fatal_error( "'%s' is not owned by you\n", serverdir );
452 if (st.st_mode & 077) fatal_error( "'%s' must not be accessible by other users\n", serverdir );
454 for (retry = 0; retry < 3; retry++)
456 /* if not the first try, wait a bit to leave the server time to exit */
457 if (retry) usleep( 100000 * retry * retry );
459 /* check for an existing socket */
460 if (lstat( SOCKETNAME, &st ) == -1)
462 if (errno != ENOENT) fatal_perror( "lstat %s/%s", serverdir, SOCKETNAME );
463 start_server( oldcwd );
464 if (lstat( SOCKETNAME, &st ) == -1) fatal_perror( "lstat %s/%s", serverdir, SOCKETNAME );
467 /* make sure the socket is sane (ISFIFO needed for Solaris) */
468 if (!S_ISSOCK(st.st_mode) && !S_ISFIFO(st.st_mode))
469 fatal_error( "'%s/%s' is not a socket\n", serverdir, SOCKETNAME );
470 if (st.st_uid != getuid())
471 fatal_error( "'%s/%s' is not owned by you\n", serverdir, SOCKETNAME );
473 /* try to connect to it */
474 addr.sun_family = AF_UNIX;
475 strcpy( addr.sun_path, SOCKETNAME );
476 slen = sizeof(addr) - sizeof(addr.sun_path) + strlen(addr.sun_path) + 1;
477 #ifdef HAVE_SOCKADDR_SUN_LEN
478 addr.sun_len = slen;
479 #endif
480 if ((s = socket( AF_UNIX, SOCK_STREAM, 0 )) == -1) fatal_perror( "socket" );
481 if (connect( s, (struct sockaddr *)&addr, slen ) != -1)
483 fcntl( s, F_SETFD, 1 ); /* set close on exec flag */
484 return s;
486 close( s );
488 fatal_error( "file '%s/%s' exists,\n"
489 " but I cannot connect to it; maybe the server has crashed?\n"
490 " If this is the case, you should remove this socket file and try again.\n",
491 serverdir, SOCKETNAME );
495 /***********************************************************************
496 * CLIENT_InitServer
498 * Start the server and create the initial socket pair.
500 int CLIENT_InitServer(void)
502 int fd, size;
503 char hostname[64];
504 char *oldcwd, *serverdir;
505 const char *configdir;
507 /* retrieve the current directory */
508 for (size = 512; ; size *= 2)
510 if (!(oldcwd = malloc( size ))) break;
511 if (getcwd( oldcwd, size )) break;
512 free( oldcwd );
513 if (errno == ERANGE) continue;
514 oldcwd = NULL;
515 break;
518 /* if argv[0] is a relative path, make it absolute */
519 full_argv0 = argv0;
520 if (oldcwd && argv0[0] != '/' && strchr( argv0, '/' ))
522 char *new_argv0 = malloc( strlen(oldcwd) + strlen(argv0) + 2 );
523 if (new_argv0)
525 strcpy( new_argv0, oldcwd );
526 strcat( new_argv0, "/" );
527 strcat( new_argv0, argv0 );
528 full_argv0 = new_argv0;
532 /* get the server directory name */
533 if (gethostname( hostname, sizeof(hostname) ) == -1) fatal_perror( "gethostname" );
534 configdir = get_config_dir();
535 serverdir = malloc( strlen(configdir) + strlen(SERVERDIR) + strlen(hostname) + 1 );
536 if (!serverdir) fatal_error( "out of memory\n" );
537 strcpy( serverdir, configdir );
538 strcat( serverdir, SERVERDIR );
539 strcat( serverdir, hostname );
541 /* connect to the server */
542 fd = server_connect( oldcwd, serverdir );
544 /* switch back to the starting directory */
545 if (oldcwd)
547 chdir( oldcwd );
548 free( oldcwd );
550 return fd;
554 /***********************************************************************
555 * CLIENT_InitThread
557 * Send an init thread request. Return 0 if OK.
559 int CLIENT_InitThread(void)
561 struct get_thread_buffer_request *req;
562 TEB *teb = NtCurrentTeb();
563 int fd, ret, size;
565 /* ignore SIGPIPE so that we get a EPIPE error instead */
566 signal( SIGPIPE, SIG_IGN );
568 wait_reply_fd( &fd );
569 if (fd == -1) server_protocol_error( "no fd passed on first request\n" );
571 if ((size = lseek( fd, 0, SEEK_END )) == -1) server_perror( "lseek" );
572 teb->buffer = mmap( 0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 );
573 close( fd );
574 if (teb->buffer == (void*)-1) server_perror( "mmap" );
575 teb->buffer_info = (struct server_buffer_info *)((char *)teb->buffer + size) - 1;
577 req = (struct get_thread_buffer_request *)teb->buffer;
578 teb->pid = req->pid;
579 teb->tid = req->tid;
580 if (req->version != SERVER_PROTOCOL_VERSION)
581 server_protocol_error( "version mismatch %d/%d.\n"
582 "Your %s binary was not upgraded correctly,\n"
583 "or you have an older one somewhere in your PATH.\n",
584 req->version, SERVER_PROTOCOL_VERSION,
585 (req->version > SERVER_PROTOCOL_VERSION) ? "wine" : "wineserver" );
586 if (req->boot) boot_thread_id = teb->tid;
587 else if (boot_thread_id == teb->tid) boot_thread_id = 0;
589 SERVER_START_REQ
591 struct init_thread_request *req = wine_server_alloc_req( sizeof(*req), 0 );
592 req->unix_pid = getpid();
593 req->teb = teb;
594 req->entry = teb->entry_point;
595 ret = wine_server_call( REQ_INIT_THREAD );
597 SERVER_END_REQ;
598 return ret;
601 /***********************************************************************
602 * CLIENT_BootDone
604 * Signal that we have finished booting, and set debug level.
606 int CLIENT_BootDone( int debug_level )
608 int ret;
609 SERVER_START_REQ
611 struct boot_done_request *req = wine_server_alloc_req( sizeof(*req), 0 );
612 req->debug_level = debug_level;
613 ret = server_call( REQ_BOOT_DONE );
615 SERVER_END_REQ;
616 return ret;
620 /***********************************************************************
621 * CLIENT_IsBootThread
623 * Return TRUE if current thread is the boot thread.
625 int CLIENT_IsBootThread(void)
627 return (GetCurrentThreadId() == (DWORD)boot_thread_id);