Add dumping of window styles at the window creation time.
[wine/hacks.git] / scheduler / client.c
bloba8cb4dd3e71675e4489ec07dacaed265d689e067
1 /*
2 * Client part of the client/server communication
4 * Copyright (C) 1998 Alexandre Julliard
5 */
7 #include "config.h"
8 #include "wine/port.h"
10 #include <assert.h>
11 #include <ctype.h>
12 #include <errno.h>
13 #include <fcntl.h>
14 #include <pwd.h>
15 #include <signal.h>
16 #include <stdio.h>
17 #include <string.h>
18 #include <sys/types.h>
19 #ifdef HAVE_SYS_SOCKET_H
20 # include <sys/socket.h>
21 #endif
22 #ifdef HAVE_SYS_WAIT_H
23 #include <sys/wait.h>
24 #endif
25 #include <sys/un.h>
26 #ifdef HAVE_SYS_MMAN_H
27 #include <sys/mman.h>
28 #endif
29 #include <sys/stat.h>
30 #include <sys/uio.h>
31 #include <unistd.h>
32 #include <stdarg.h>
34 #include "thread.h"
35 #include "wine/server.h"
36 #include "winerror.h"
37 #include "options.h"
39 /* Some versions of glibc don't define this */
40 #ifndef SCM_RIGHTS
41 #define SCM_RIGHTS 1
42 #endif
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 #ifndef HAVE_MSGHDR_ACCRIGHTS
49 /* data structure used to pass an fd with sendmsg/recvmsg */
50 struct cmsg_fd
52 int len; /* sizeof structure */
53 int level; /* SOL_SOCKET */
54 int type; /* SCM_RIGHTS */
55 int fd; /* fd to pass */
57 #endif /* HAVE_MSGHDR_ACCRIGHTS */
59 static void *boot_thread_id;
60 static sigset_t block_set; /* signals to block during server calls */
61 static int fd_socket; /* socket to exchange file descriptors with the server */
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, "wine client error:%p: ", NtCurrentTeb()->tid );
99 vfprintf( stderr, err, args );
100 va_end( args );
101 SYSDEPS_ExitThread(1);
105 /***********************************************************************
106 * server_protocol_perror
108 void server_protocol_perror( const char *err )
110 fprintf( stderr, "wine client error:%p: ", NtCurrentTeb()->tid );
111 perror( err );
112 SYSDEPS_ExitThread(1);
116 /***********************************************************************
117 * __wine_server_exception_handler (NTDLL.@)
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_pos = server_frame->buffer_pos;
125 return ExceptionContinueSearch;
129 /***********************************************************************
130 * wine_server_alloc_req (NTDLL.@)
132 void wine_server_alloc_req( union generic_request *req, size_t size )
134 unsigned int pos = NtCurrentTeb()->buffer_pos;
136 assert( size <= REQUEST_MAX_VAR_SIZE );
138 if (pos + size > NtCurrentTeb()->buffer_size)
139 server_protocol_error( "buffer overflow %d bytes\n",
140 pos + size - NtCurrentTeb()->buffer_pos );
142 NtCurrentTeb()->buffer_pos = pos + size;
143 req->header.var_offset = pos;
144 req->header.var_size = size;
148 /***********************************************************************
149 * send_request
151 * Send a request to the server.
153 static void send_request( union generic_request *request )
155 int ret;
157 if ((ret = write( NtCurrentTeb()->request_fd, request, sizeof(*request) )) == sizeof(*request))
158 return;
159 if (ret >= 0) server_protocol_error( "partial write %d\n", ret );
160 if (errno == EPIPE) SYSDEPS_ExitThread(0);
161 server_protocol_perror( "sendmsg" );
164 /***********************************************************************
165 * wait_reply
167 * Wait for a reply from the server.
169 static void wait_reply( union generic_request *req )
171 int ret;
173 for (;;)
175 if ((ret = read( NtCurrentTeb()->reply_fd, req, sizeof(*req) )) == sizeof(*req))
176 return;
177 if (!ret) break;
178 if (ret > 0) server_protocol_error( "partial read %d\n", ret );
179 if (errno == EINTR) continue;
180 if (errno == EPIPE) break;
181 server_protocol_perror("read");
183 /* the server closed the connection; time to die... */
184 SYSDEPS_ExitThread(0);
188 /***********************************************************************
189 * wine_server_call (NTDLL.@)
191 * Perform a server call.
193 unsigned int wine_server_call( union generic_request *req, size_t size )
195 sigset_t old_set;
197 memset( (char *)req + size, 0, sizeof(*req) - size );
198 sigprocmask( SIG_BLOCK, &block_set, &old_set );
199 send_request( req );
200 wait_reply( req );
201 sigprocmask( SIG_SETMASK, &old_set, NULL );
202 return req->header.error;
206 /***********************************************************************
207 * wine_server_send_fd
209 * Send a file descriptor to the server.
211 void wine_server_send_fd( int fd )
213 #ifndef HAVE_MSGHDR_ACCRIGHTS
214 struct cmsg_fd cmsg;
215 #endif
216 struct send_fd data;
217 struct msghdr msghdr;
218 struct iovec vec;
219 int ret;
221 vec.iov_base = (void *)&data;
222 vec.iov_len = sizeof(data);
224 msghdr.msg_name = NULL;
225 msghdr.msg_namelen = 0;
226 msghdr.msg_iov = &vec;
227 msghdr.msg_iovlen = 1;
229 #ifdef HAVE_MSGHDR_ACCRIGHTS
230 msghdr.msg_accrights = (void *)&fd;
231 msghdr.msg_accrightslen = sizeof(fd);
232 #else /* HAVE_MSGHDR_ACCRIGHTS */
233 cmsg.len = sizeof(cmsg);
234 cmsg.level = SOL_SOCKET;
235 cmsg.type = SCM_RIGHTS;
236 cmsg.fd = fd;
237 msghdr.msg_control = &cmsg;
238 msghdr.msg_controllen = sizeof(cmsg);
239 msghdr.msg_flags = 0;
240 #endif /* HAVE_MSGHDR_ACCRIGHTS */
242 data.tid = (void *)GetCurrentThreadId();
243 data.fd = fd;
245 for (;;)
247 if ((ret = sendmsg( fd_socket, &msghdr, 0 )) == sizeof(data)) return;
248 if (ret >= 0) server_protocol_error( "partial write %d\n", ret );
249 if (errno == EINTR) continue;
250 if (errno == EPIPE) SYSDEPS_ExitThread(0);
251 server_protocol_perror( "sendmsg" );
256 /***********************************************************************
257 * receive_fd
259 * Receive a file descriptor passed from the server.
261 static int receive_fd( handle_t *handle )
263 struct iovec vec;
264 int ret, fd;
266 #ifdef HAVE_MSGHDR_ACCRIGHTS
267 struct msghdr msghdr;
269 fd = -1;
270 msghdr.msg_accrights = (void *)&fd;
271 msghdr.msg_accrightslen = sizeof(fd);
272 #else /* HAVE_MSGHDR_ACCRIGHTS */
273 struct msghdr msghdr;
274 struct cmsg_fd cmsg;
276 cmsg.len = sizeof(cmsg);
277 cmsg.level = SOL_SOCKET;
278 cmsg.type = SCM_RIGHTS;
279 cmsg.fd = -1;
280 msghdr.msg_control = &cmsg;
281 msghdr.msg_controllen = sizeof(cmsg);
282 msghdr.msg_flags = 0;
283 #endif /* HAVE_MSGHDR_ACCRIGHTS */
285 msghdr.msg_name = NULL;
286 msghdr.msg_namelen = 0;
287 msghdr.msg_iov = &vec;
288 msghdr.msg_iovlen = 1;
289 vec.iov_base = (void *)handle;
290 vec.iov_len = sizeof(*handle);
292 for (;;)
294 if ((ret = recvmsg( fd_socket, &msghdr, 0 )) > 0)
296 #ifndef HAVE_MSGHDR_ACCRIGHTS
297 fd = cmsg.fd;
298 #endif
299 if (fd == -1) server_protocol_error( "no fd received for handle %d\n", *handle );
300 fcntl( fd, F_SETFD, 1 ); /* set close on exec flag */
301 return fd;
303 if (!ret) break;
304 if (errno == EINTR) continue;
305 if (errno == EPIPE) break;
306 server_protocol_perror("recvmsg");
308 /* the server closed the connection; time to die... */
309 SYSDEPS_ExitThread(0);
313 /***********************************************************************
314 * wine_server_recv_fd
316 * Receive a file descriptor passed from the server.
317 * The file descriptor must not be closed.
318 * Return -2 if a race condition stole our file descriptor.
320 int wine_server_recv_fd( handle_t handle )
322 handle_t fd_handle;
324 int fd = receive_fd( &fd_handle );
326 /* now store it in the server fd cache for this handle */
328 SERVER_START_REQ( set_handle_info )
330 req->handle = fd_handle;
331 req->flags = 0;
332 req->mask = 0;
333 req->fd = fd;
334 if (!SERVER_CALL())
336 if (req->cur_fd != fd)
338 /* someone was here before us */
339 close( fd );
340 fd = req->cur_fd;
343 else
345 close( fd );
346 fd = -1;
349 SERVER_END_REQ;
351 if (handle != fd_handle) fd = -2; /* not the one we expected */
352 return fd;
356 /***********************************************************************
357 * get_config_dir
359 * Return the configuration directory ($WINEPREFIX or $HOME/.wine)
361 const char *get_config_dir(void)
363 static char *confdir;
364 if (!confdir)
366 const char *prefix = getenv( "WINEPREFIX" );
367 if (prefix)
369 int len = strlen(prefix);
370 if (!(confdir = strdup( prefix ))) fatal_error( "out of memory\n" );
371 if (len > 1 && confdir[len-1] == '/') confdir[len-1] = 0;
373 else
375 const char *home = getenv( "HOME" );
376 if (!home)
378 struct passwd *pwd = getpwuid( getuid() );
379 if (!pwd) fatal_error( "could not find your home directory\n" );
380 home = pwd->pw_dir;
382 if (!(confdir = malloc( strlen(home) + strlen(CONFDIR) + 1 )))
383 fatal_error( "out of memory\n" );
384 strcpy( confdir, home );
385 strcat( confdir, CONFDIR );
388 return confdir;
392 /***********************************************************************
393 * start_server
395 * Start a new wine server.
397 static void start_server( const char *oldcwd )
399 static int started; /* we only try once */
400 char *path, *p;
401 if (!started)
403 int status;
404 int pid = fork();
405 if (pid == -1) fatal_perror( "fork" );
406 if (!pid)
408 /* if server is explicitly specified, use this */
409 if ((p = getenv("WINESERVER")))
411 if (p[0] != '/' && oldcwd[0] == '/') /* make it an absolute path */
413 if (!(path = malloc( strlen(oldcwd) + strlen(p) + 1 )))
414 fatal_error( "out of memory\n" );
415 sprintf( path, "%s/%s", oldcwd, p );
416 p = path;
418 execl( p, "wineserver", NULL );
419 fatal_perror( "could not exec the server '%s'\n"
420 " specified in the WINESERVER environment variable", p );
423 /* first try the installation dir */
424 execl( BINDIR "/wineserver", "wineserver", NULL );
426 /* now try the dir we were launched from */
427 if (full_argv0)
429 if (!(path = malloc( strlen(full_argv0) + 20 )))
430 fatal_error( "out of memory\n" );
431 if ((p = strrchr( strcpy( path, full_argv0 ), '/' )))
433 strcpy( p, "/wineserver" );
434 execl( path, "wineserver", NULL );
435 strcpy( p, "/server/wineserver" );
436 execl( path, "wineserver", NULL );
438 free(path);
441 /* now try the path */
442 execlp( "wineserver", "wineserver", NULL );
444 /* and finally the current dir */
445 if (!(path = malloc( strlen(oldcwd) + 20 )))
446 fatal_error( "out of memory\n" );
447 p = strcpy( path, oldcwd ) + strlen( oldcwd );
448 strcpy( p, "/wineserver" );
449 execl( path, "wineserver", NULL );
450 strcpy( p, "/server/wineserver" );
451 execl( path, "wineserver", NULL );
452 free(path);
453 fatal_error( "could not exec wineserver\n" );
455 started = 1;
456 waitpid( pid, &status, 0 );
457 status = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
458 if (status) exit(status); /* server failed */
462 /***********************************************************************
463 * server_connect
465 * Attempt to connect to an existing server socket.
466 * We need to be in the server directory already.
468 static int server_connect( const char *oldcwd, const char *serverdir )
470 struct sockaddr_un addr;
471 struct stat st;
472 int s, slen, retry;
474 /* chdir to the server directory */
475 if (chdir( serverdir ) == -1)
477 if (errno != ENOENT) fatal_perror( "chdir to %s", serverdir );
478 start_server( "." );
479 if (chdir( serverdir ) == -1) fatal_perror( "chdir to %s", serverdir );
482 /* make sure we are at the right place */
483 if (stat( ".", &st ) == -1) fatal_perror( "stat %s", serverdir );
484 if (st.st_uid != getuid()) fatal_error( "'%s' is not owned by you\n", serverdir );
485 if (st.st_mode & 077) fatal_error( "'%s' must not be accessible by other users\n", serverdir );
487 for (retry = 0; retry < 3; retry++)
489 /* if not the first try, wait a bit to leave the server time to exit */
490 if (retry) usleep( 100000 * retry * retry );
492 /* check for an existing socket */
493 if (lstat( SOCKETNAME, &st ) == -1)
495 if (errno != ENOENT) fatal_perror( "lstat %s/%s", serverdir, SOCKETNAME );
496 start_server( oldcwd );
497 if (lstat( SOCKETNAME, &st ) == -1) fatal_perror( "lstat %s/%s", serverdir, SOCKETNAME );
500 /* make sure the socket is sane (ISFIFO needed for Solaris) */
501 if (!S_ISSOCK(st.st_mode) && !S_ISFIFO(st.st_mode))
502 fatal_error( "'%s/%s' is not a socket\n", serverdir, SOCKETNAME );
503 if (st.st_uid != getuid())
504 fatal_error( "'%s/%s' is not owned by you\n", serverdir, SOCKETNAME );
506 /* try to connect to it */
507 addr.sun_family = AF_UNIX;
508 strcpy( addr.sun_path, SOCKETNAME );
509 slen = sizeof(addr) - sizeof(addr.sun_path) + strlen(addr.sun_path) + 1;
510 #ifdef HAVE_SOCKADDR_SUN_LEN
511 addr.sun_len = slen;
512 #endif
513 if ((s = socket( AF_UNIX, SOCK_STREAM, 0 )) == -1) fatal_perror( "socket" );
514 if (connect( s, (struct sockaddr *)&addr, slen ) != -1)
516 fcntl( s, F_SETFD, 1 ); /* set close on exec flag */
517 return s;
519 close( s );
521 fatal_error( "file '%s/%s' exists,\n"
522 " but I cannot connect to it; maybe the wineserver has crashed?\n"
523 " If this is the case, you should remove this socket file and try again.\n",
524 serverdir, SOCKETNAME );
528 /***********************************************************************
529 * CLIENT_InitServer
531 * Start the server and create the initial socket pair.
533 void CLIENT_InitServer(void)
535 int size;
536 char hostname[64];
537 char *oldcwd, *serverdir;
538 const char *configdir;
539 handle_t dummy_handle;
541 /* retrieve the current directory */
542 for (size = 512; ; size *= 2)
544 if (!(oldcwd = malloc( size ))) break;
545 if (getcwd( oldcwd, size )) break;
546 free( oldcwd );
547 if (errno == ERANGE) continue;
548 oldcwd = NULL;
549 break;
552 /* if argv[0] is a relative path, make it absolute */
553 full_argv0 = argv0;
554 if (oldcwd && argv0[0] != '/' && strchr( argv0, '/' ))
556 char *new_argv0 = malloc( strlen(oldcwd) + strlen(argv0) + 2 );
557 if (new_argv0)
559 strcpy( new_argv0, oldcwd );
560 strcat( new_argv0, "/" );
561 strcat( new_argv0, argv0 );
562 full_argv0 = new_argv0;
566 /* get the server directory name */
567 if (gethostname( hostname, sizeof(hostname) ) == -1) fatal_perror( "gethostname" );
568 configdir = get_config_dir();
569 serverdir = malloc( strlen(configdir) + strlen(SERVERDIR) + strlen(hostname) + 1 );
570 if (!serverdir) fatal_error( "out of memory\n" );
571 strcpy( serverdir, configdir );
572 strcat( serverdir, SERVERDIR );
573 strcat( serverdir, hostname );
575 /* connect to the server */
576 fd_socket = server_connect( oldcwd, serverdir );
578 /* switch back to the starting directory */
579 if (oldcwd)
581 chdir( oldcwd );
582 free( oldcwd );
585 /* setup the signal mask */
586 sigemptyset( &block_set );
587 sigaddset( &block_set, SIGALRM );
588 sigaddset( &block_set, SIGIO );
589 sigaddset( &block_set, SIGINT );
590 sigaddset( &block_set, SIGHUP );
592 /* receive the first thread request fd on the main socket */
593 NtCurrentTeb()->request_fd = receive_fd( &dummy_handle );
595 CLIENT_InitThread();
599 /***********************************************************************
600 * set_request_buffer
602 inline static void set_request_buffer(void)
604 char *name;
605 int fd, ret;
606 unsigned int offset, size;
608 /* create a temporary file */
611 if (!(name = tmpnam(NULL))) server_protocol_perror( "tmpnam" );
612 fd = open( name, O_CREAT | O_EXCL | O_RDWR, 0600 );
613 } while ((fd == -1) && (errno == EEXIST));
615 if (fd == -1) server_protocol_perror( "create" );
616 unlink( name );
618 wine_server_send_fd( fd );
620 SERVER_START_REQ( set_thread_buffer )
622 req->fd = fd;
623 ret = SERVER_CALL();
624 offset = req->offset;
625 size = req->size;
627 SERVER_END_REQ;
628 if (ret) server_protocol_error( "set_thread_buffer failed with status %x\n", ret );
630 if ((NtCurrentTeb()->buffer = mmap( 0, size, PROT_READ | PROT_WRITE,
631 MAP_SHARED, fd, offset )) == (void*)-1)
632 server_protocol_perror( "mmap" );
634 close( fd );
635 NtCurrentTeb()->buffer_pos = 0;
636 NtCurrentTeb()->buffer_size = size;
640 /***********************************************************************
641 * CLIENT_InitThread
643 * Send an init thread request. Return 0 if OK.
645 void CLIENT_InitThread(void)
647 TEB *teb = NtCurrentTeb();
648 int version, ret;
649 int reply_pipe[2];
651 /* ignore SIGPIPE so that we get a EPIPE error instead */
652 signal( SIGPIPE, SIG_IGN );
654 /* create the server->client communication pipes */
655 if (pipe( reply_pipe ) == -1) server_protocol_perror( "pipe" );
656 if (pipe( teb->wait_fd ) == -1) server_protocol_perror( "pipe" );
657 wine_server_send_fd( reply_pipe[1] );
658 wine_server_send_fd( teb->wait_fd[1] );
659 teb->reply_fd = reply_pipe[0];
661 /* set close on exec flag */
662 fcntl( teb->reply_fd, F_SETFD, 1 );
663 fcntl( teb->wait_fd[0], F_SETFD, 1 );
664 fcntl( teb->wait_fd[1], F_SETFD, 1 );
666 SERVER_START_REQ( init_thread )
668 req->unix_pid = getpid();
669 req->teb = teb;
670 req->entry = teb->entry_point;
671 req->reply_fd = reply_pipe[1];
672 req->wait_fd = teb->wait_fd[1];
673 ret = SERVER_CALL();
674 teb->pid = req->pid;
675 teb->tid = req->tid;
676 version = req->version;
677 if (req->boot) boot_thread_id = teb->tid;
678 else if (boot_thread_id == teb->tid) boot_thread_id = 0;
679 close( reply_pipe[1] );
681 SERVER_END_REQ;
683 if (ret) server_protocol_error( "init_thread failed with status %x\n", ret );
684 if (version != SERVER_PROTOCOL_VERSION)
685 server_protocol_error( "version mismatch %d/%d.\n"
686 "Your %s binary was not upgraded correctly,\n"
687 "or you have an older one somewhere in your PATH.\n"
688 "Or maybe the wrong wineserver is still running?\n",
689 version, SERVER_PROTOCOL_VERSION,
690 (version > SERVER_PROTOCOL_VERSION) ? "wine" : "wineserver" );
691 set_request_buffer();
695 /***********************************************************************
696 * CLIENT_BootDone
698 * Signal that we have finished booting, and set debug level.
700 void CLIENT_BootDone( int debug_level )
702 SERVER_START_REQ( boot_done )
704 req->debug_level = debug_level;
705 SERVER_CALL();
707 SERVER_END_REQ;
711 /***********************************************************************
712 * CLIENT_IsBootThread
714 * Return TRUE if current thread is the boot thread.
716 int CLIENT_IsBootThread(void)
718 return (GetCurrentThreadId() == (DWORD)boot_thread_id);