Fixed another regression in PlaySound.
[wine.git] / scheduler / client.c
blobde1da8ced174a1b5a7ee76733fd9d260d654b5b0
1 /*
2 * Client part of the client/server communication
4 * Copyright (C) 1998 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <ctype.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <pwd.h>
29 #include <signal.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <sys/types.h>
33 #ifdef HAVE_SYS_SOCKET_H
34 # include <sys/socket.h>
35 #endif
36 #ifdef HAVE_SYS_WAIT_H
37 #include <sys/wait.h>
38 #endif
39 #include <sys/un.h>
40 #ifdef HAVE_SYS_MMAN_H
41 #include <sys/mman.h>
42 #endif
43 #include <sys/stat.h>
44 #include <sys/uio.h>
45 #include <unistd.h>
46 #include <stdarg.h>
48 #include "thread.h"
49 #include "wine/server.h"
50 #include "winerror.h"
51 #include "options.h"
53 /* Some versions of glibc don't define this */
54 #ifndef SCM_RIGHTS
55 #define SCM_RIGHTS 1
56 #endif
58 #define CONFDIR "/.wine" /* directory for Wine config relative to $HOME */
59 #define SERVERDIR "/wineserver-" /* server socket directory (hostname appended) */
60 #define SOCKETNAME "socket" /* name of the socket file */
62 #ifndef HAVE_MSGHDR_ACCRIGHTS
63 /* data structure used to pass an fd with sendmsg/recvmsg */
64 struct cmsg_fd
66 int len; /* sizeof structure */
67 int level; /* SOL_SOCKET */
68 int type; /* SCM_RIGHTS */
69 int fd; /* fd to pass */
71 #endif /* HAVE_MSGHDR_ACCRIGHTS */
73 static void *boot_thread_id;
74 static sigset_t block_set; /* signals to block during server calls */
75 static int fd_socket; /* socket to exchange file descriptors with the server */
77 /* die on a fatal error; use only during initialization */
78 static void fatal_error( const char *err, ... ) WINE_NORETURN;
79 static void fatal_error( const char *err, ... )
81 va_list args;
83 va_start( args, err );
84 fprintf( stderr, "wine: " );
85 vfprintf( stderr, err, args );
86 va_end( args );
87 exit(1);
90 /* die on a fatal error; use only during initialization */
91 static void fatal_perror( const char *err, ... ) WINE_NORETURN;
92 static void fatal_perror( const char *err, ... )
94 va_list args;
96 va_start( args, err );
97 fprintf( stderr, "wine: " );
98 vfprintf( stderr, err, args );
99 perror( " " );
100 va_end( args );
101 exit(1);
104 /***********************************************************************
105 * server_protocol_error
107 void server_protocol_error( const char *err, ... )
109 va_list args;
111 va_start( args, err );
112 fprintf( stderr, "wine client error:%p: ", NtCurrentTeb()->tid );
113 vfprintf( stderr, err, args );
114 va_end( args );
115 SYSDEPS_AbortThread(1);
119 /***********************************************************************
120 * server_protocol_perror
122 void server_protocol_perror( const char *err )
124 fprintf( stderr, "wine client error:%p: ", NtCurrentTeb()->tid );
125 perror( err );
126 SYSDEPS_AbortThread(1);
130 /***********************************************************************
131 * send_request
133 * Send a request to the server.
135 static void send_request( const struct __server_request_info *req )
137 int i, ret;
139 if (!req->u.req.request_header.request_size)
141 if ((ret = write( NtCurrentTeb()->request_fd, &req->u.req,
142 sizeof(req->u.req) )) == sizeof(req->u.req)) return;
145 else
147 struct iovec vec[__SERVER_MAX_DATA+1];
149 vec[0].iov_base = (void *)&req->u.req;
150 vec[0].iov_len = sizeof(req->u.req);
151 for (i = 0; i < req->data_count; i++)
153 vec[i+1].iov_base = (void *)req->data[i].ptr;
154 vec[i+1].iov_len = req->data[i].size;
156 if ((ret = writev( NtCurrentTeb()->request_fd, vec, i+1 )) ==
157 req->u.req.request_header.request_size + sizeof(req->u.req)) return;
160 if (ret >= 0) server_protocol_error( "partial write %d\n", ret );
161 if (errno == EPIPE) SYSDEPS_AbortThread(0);
162 server_protocol_perror( "sendmsg" );
166 /***********************************************************************
167 * read_reply_data
169 * Read data from the reply buffer; helper for wait_reply.
171 static void read_reply_data( void *buffer, size_t size )
173 int ret;
175 for (;;)
177 if ((ret = read( NtCurrentTeb()->reply_fd, buffer, size )) > 0)
179 if (!(size -= ret)) return;
180 buffer = (char *)buffer + ret;
181 continue;
183 if (!ret) break;
184 if (errno == EINTR) continue;
185 if (errno == EPIPE) break;
186 server_protocol_perror("read");
188 /* the server closed the connection; time to die... */
189 SYSDEPS_AbortThread(0);
193 /***********************************************************************
194 * wait_reply
196 * Wait for a reply from the server.
198 inline static void wait_reply( struct __server_request_info *req )
200 read_reply_data( &req->u.reply, sizeof(req->u.reply) );
201 if (req->u.reply.reply_header.reply_size)
202 read_reply_data( req->reply_data, req->u.reply.reply_header.reply_size );
206 /***********************************************************************
207 * wine_server_call (NTDLL.@)
209 * Perform a server call.
211 unsigned int wine_server_call( void *req_ptr )
213 struct __server_request_info * const req = req_ptr;
214 sigset_t old_set;
216 memset( (char *)&req->u.req + req->size, 0, sizeof(req->u.req) - req->size );
217 sigprocmask( SIG_BLOCK, &block_set, &old_set );
218 send_request( req );
219 wait_reply( req );
220 sigprocmask( SIG_SETMASK, &old_set, NULL );
221 return req->u.reply.reply_header.error;
225 /***********************************************************************
226 * wine_server_send_fd
228 * Send a file descriptor to the server.
230 void wine_server_send_fd( int fd )
232 #ifndef HAVE_MSGHDR_ACCRIGHTS
233 struct cmsg_fd cmsg;
234 #endif
235 struct send_fd data;
236 struct msghdr msghdr;
237 struct iovec vec;
238 int ret;
240 vec.iov_base = (void *)&data;
241 vec.iov_len = sizeof(data);
243 msghdr.msg_name = NULL;
244 msghdr.msg_namelen = 0;
245 msghdr.msg_iov = &vec;
246 msghdr.msg_iovlen = 1;
248 #ifdef HAVE_MSGHDR_ACCRIGHTS
249 msghdr.msg_accrights = (void *)&fd;
250 msghdr.msg_accrightslen = sizeof(fd);
251 #else /* HAVE_MSGHDR_ACCRIGHTS */
252 cmsg.len = sizeof(cmsg);
253 cmsg.level = SOL_SOCKET;
254 cmsg.type = SCM_RIGHTS;
255 cmsg.fd = fd;
256 msghdr.msg_control = &cmsg;
257 msghdr.msg_controllen = sizeof(cmsg);
258 msghdr.msg_flags = 0;
259 #endif /* HAVE_MSGHDR_ACCRIGHTS */
261 data.tid = (void *)GetCurrentThreadId();
262 data.fd = fd;
264 for (;;)
266 if ((ret = sendmsg( fd_socket, &msghdr, 0 )) == sizeof(data)) return;
267 if (ret >= 0) server_protocol_error( "partial write %d\n", ret );
268 if (errno == EINTR) continue;
269 if (errno == EPIPE) SYSDEPS_AbortThread(0);
270 server_protocol_perror( "sendmsg" );
275 /***********************************************************************
276 * receive_fd
278 * Receive a file descriptor passed from the server.
280 static int receive_fd( obj_handle_t *handle )
282 struct iovec vec;
283 int ret, fd;
285 #ifdef HAVE_MSGHDR_ACCRIGHTS
286 struct msghdr msghdr;
288 fd = -1;
289 msghdr.msg_accrights = (void *)&fd;
290 msghdr.msg_accrightslen = sizeof(fd);
291 #else /* HAVE_MSGHDR_ACCRIGHTS */
292 struct msghdr msghdr;
293 struct cmsg_fd cmsg;
295 cmsg.len = sizeof(cmsg);
296 cmsg.level = SOL_SOCKET;
297 cmsg.type = SCM_RIGHTS;
298 cmsg.fd = -1;
299 msghdr.msg_control = &cmsg;
300 msghdr.msg_controllen = sizeof(cmsg);
301 msghdr.msg_flags = 0;
302 #endif /* HAVE_MSGHDR_ACCRIGHTS */
304 msghdr.msg_name = NULL;
305 msghdr.msg_namelen = 0;
306 msghdr.msg_iov = &vec;
307 msghdr.msg_iovlen = 1;
308 vec.iov_base = (void *)handle;
309 vec.iov_len = sizeof(*handle);
311 for (;;)
313 if ((ret = recvmsg( fd_socket, &msghdr, 0 )) > 0)
315 #ifndef HAVE_MSGHDR_ACCRIGHTS
316 fd = cmsg.fd;
317 #endif
318 if (fd == -1) server_protocol_error( "no fd received for handle %d\n", *handle );
319 fcntl( fd, F_SETFD, 1 ); /* set close on exec flag */
320 return fd;
322 if (!ret) break;
323 if (errno == EINTR) continue;
324 if (errno == EPIPE) break;
325 server_protocol_perror("recvmsg");
327 /* the server closed the connection; time to die... */
328 SYSDEPS_AbortThread(0);
332 /***********************************************************************
333 * store_cached_fd
335 * Store the cached fd value for a given handle back into the server.
336 * Returns the new fd, which can be different if there was already an
337 * fd in the cache for that handle.
339 inline static int store_cached_fd( int fd, obj_handle_t handle )
341 SERVER_START_REQ( set_handle_info )
343 req->handle = handle;
344 req->flags = 0;
345 req->mask = 0;
346 req->fd = fd;
347 if (!wine_server_call( req ))
349 if (reply->cur_fd != fd)
351 /* someone was here before us */
352 close( fd );
353 fd = reply->cur_fd;
356 else
358 close( fd );
359 fd = -1;
362 SERVER_END_REQ;
363 return fd;
367 /***********************************************************************
368 * wine_server_fd_to_handle (NTDLL.@)
370 * Allocate a file handle for a Unix fd.
372 int wine_server_fd_to_handle( int fd, unsigned int access, int inherit, obj_handle_t *handle )
374 int ret;
376 *handle = 0;
377 wine_server_send_fd( fd );
379 SERVER_START_REQ( alloc_file_handle )
381 req->access = access;
382 req->inherit = inherit;
383 req->fd = fd;
384 if (!(ret = wine_server_call( req ))) *handle = reply->handle;
386 SERVER_END_REQ;
387 return ret;
391 /***********************************************************************
392 * wine_server_handle_to_fd (NTDLL.@)
394 * Retrieve the Unix fd corresponding to a file handle.
396 int wine_server_handle_to_fd( obj_handle_t handle, unsigned int access, int *unix_fd,
397 enum fd_type *type, int *flags )
399 obj_handle_t fd_handle;
400 int ret, fd = -1;
402 *unix_fd = -1;
403 for (;;)
405 SERVER_START_REQ( get_handle_fd )
407 req->handle = handle;
408 req->access = access;
409 if (!(ret = wine_server_call( req ))) fd = reply->fd;
410 if (type) *type = reply->type;
411 if (flags) *flags = reply->flags;
413 SERVER_END_REQ;
414 if (ret) return ret;
416 if (fd != -1) break;
418 /* it wasn't in the cache, get it from the server */
419 fd = receive_fd( &fd_handle );
420 /* and store it back into the cache */
421 fd = store_cached_fd( fd, fd_handle );
423 if (fd_handle == handle) break;
424 /* if we received a different handle this means there was
425 * a race with another thread; we restart everything from
426 * scratch in this case.
430 if ((fd != -1) && ((fd = dup(fd)) == -1)) return STATUS_TOO_MANY_OPENED_FILES;
431 *unix_fd = fd;
432 return STATUS_SUCCESS;
436 /***********************************************************************
437 * get_config_dir
439 * Return the configuration directory ($WINEPREFIX or $HOME/.wine)
441 const char *get_config_dir(void)
443 static char *confdir;
444 if (!confdir)
446 const char *prefix = getenv( "WINEPREFIX" );
447 if (prefix)
449 int len = strlen(prefix);
450 if (!(confdir = strdup( prefix ))) fatal_error( "out of memory\n" );
451 if (len > 1 && confdir[len-1] == '/') confdir[len-1] = 0;
453 else
455 const char *home = getenv( "HOME" );
456 if (!home)
458 struct passwd *pwd = getpwuid( getuid() );
459 if (!pwd) fatal_error( "could not find your home directory\n" );
460 home = pwd->pw_dir;
462 if (!(confdir = malloc( strlen(home) + strlen(CONFDIR) + 1 )))
463 fatal_error( "out of memory\n" );
464 strcpy( confdir, home );
465 strcat( confdir, CONFDIR );
468 return confdir;
472 /***********************************************************************
473 * start_server
475 * Start a new wine server.
477 static void start_server( const char *oldcwd )
479 static int started; /* we only try once */
480 char *path, *p;
481 if (!started)
483 int status;
484 int pid = fork();
485 if (pid == -1) fatal_perror( "fork" );
486 if (!pid)
488 /* if server is explicitly specified, use this */
489 if ((p = getenv("WINESERVER")))
491 if (p[0] != '/' && oldcwd[0] == '/') /* make it an absolute path */
493 if (!(path = malloc( strlen(oldcwd) + strlen(p) + 1 )))
494 fatal_error( "out of memory\n" );
495 sprintf( path, "%s/%s", oldcwd, p );
496 p = path;
498 execl( p, "wineserver", NULL );
499 fatal_perror( "could not exec the server '%s'\n"
500 " specified in the WINESERVER environment variable", p );
503 /* first try the installation dir */
504 execl( BINDIR "/wineserver", "wineserver", NULL );
506 /* now try the dir we were launched from */
507 if (full_argv0)
509 if (!(path = malloc( strlen(full_argv0) + 20 )))
510 fatal_error( "out of memory\n" );
511 if ((p = strrchr( strcpy( path, full_argv0 ), '/' )))
513 strcpy( p, "/wineserver" );
514 execl( path, "wineserver", NULL );
516 free(path);
519 /* finally try the path */
520 execlp( "wineserver", "wineserver", NULL );
521 fatal_error( "could not exec wineserver\n" );
523 started = 1;
524 waitpid( pid, &status, 0 );
525 status = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
526 if (status) exit(status); /* server failed */
530 /***********************************************************************
531 * server_connect
533 * Attempt to connect to an existing server socket.
534 * We need to be in the server directory already.
536 static int server_connect( const char *oldcwd, const char *serverdir )
538 struct sockaddr_un addr;
539 struct stat st;
540 int s, slen, retry;
542 /* chdir to the server directory */
543 if (chdir( serverdir ) == -1)
545 if (errno != ENOENT) fatal_perror( "chdir to %s", serverdir );
546 start_server( "." );
547 if (chdir( serverdir ) == -1) fatal_perror( "chdir to %s", serverdir );
550 /* make sure we are at the right place */
551 if (stat( ".", &st ) == -1) fatal_perror( "stat %s", serverdir );
552 if (st.st_uid != getuid()) fatal_error( "'%s' is not owned by you\n", serverdir );
553 if (st.st_mode & 077) fatal_error( "'%s' must not be accessible by other users\n", serverdir );
555 for (retry = 0; retry < 3; retry++)
557 /* if not the first try, wait a bit to leave the server time to exit */
558 if (retry) usleep( 100000 * retry * retry );
560 /* check for an existing socket */
561 if (lstat( SOCKETNAME, &st ) == -1)
563 if (errno != ENOENT) fatal_perror( "lstat %s/%s", serverdir, SOCKETNAME );
564 start_server( oldcwd );
565 if (lstat( SOCKETNAME, &st ) == -1) fatal_perror( "lstat %s/%s", serverdir, SOCKETNAME );
568 /* make sure the socket is sane (ISFIFO needed for Solaris) */
569 if (!S_ISSOCK(st.st_mode) && !S_ISFIFO(st.st_mode))
570 fatal_error( "'%s/%s' is not a socket\n", serverdir, SOCKETNAME );
571 if (st.st_uid != getuid())
572 fatal_error( "'%s/%s' is not owned by you\n", serverdir, SOCKETNAME );
574 /* try to connect to it */
575 addr.sun_family = AF_UNIX;
576 strcpy( addr.sun_path, SOCKETNAME );
577 slen = sizeof(addr) - sizeof(addr.sun_path) + strlen(addr.sun_path) + 1;
578 #ifdef HAVE_SOCKADDR_SUN_LEN
579 addr.sun_len = slen;
580 #endif
581 if ((s = socket( AF_UNIX, SOCK_STREAM, 0 )) == -1) fatal_perror( "socket" );
582 if (connect( s, (struct sockaddr *)&addr, slen ) != -1)
584 fcntl( s, F_SETFD, 1 ); /* set close on exec flag */
585 return s;
587 close( s );
589 fatal_error( "file '%s/%s' exists,\n"
590 " but I cannot connect to it; maybe the wineserver has crashed?\n"
591 " If this is the case, you should remove this socket file and try again.\n",
592 serverdir, SOCKETNAME );
596 /***********************************************************************
597 * CLIENT_InitServer
599 * Start the server and create the initial socket pair.
601 void CLIENT_InitServer(void)
603 int size;
604 char hostname[64];
605 char *oldcwd, *serverdir;
606 const char *configdir;
607 obj_handle_t dummy_handle;
609 /* retrieve the current directory */
610 for (size = 512; ; size *= 2)
612 if (!(oldcwd = malloc( size ))) break;
613 if (getcwd( oldcwd, size )) break;
614 free( oldcwd );
615 if (errno == ERANGE) continue;
616 oldcwd = NULL;
617 break;
620 /* if argv[0] is a relative path, make it absolute */
621 full_argv0 = argv0;
622 if (oldcwd && argv0[0] != '/' && strchr( argv0, '/' ))
624 char *new_argv0 = malloc( strlen(oldcwd) + strlen(argv0) + 2 );
625 if (new_argv0)
627 strcpy( new_argv0, oldcwd );
628 strcat( new_argv0, "/" );
629 strcat( new_argv0, argv0 );
630 full_argv0 = new_argv0;
634 /* get the server directory name */
635 if (gethostname( hostname, sizeof(hostname) ) == -1) fatal_perror( "gethostname" );
636 configdir = get_config_dir();
637 serverdir = malloc( strlen(configdir) + strlen(SERVERDIR) + strlen(hostname) + 1 );
638 if (!serverdir) fatal_error( "out of memory\n" );
639 strcpy( serverdir, configdir );
640 strcat( serverdir, SERVERDIR );
641 strcat( serverdir, hostname );
643 /* connect to the server */
644 fd_socket = server_connect( oldcwd, serverdir );
646 /* switch back to the starting directory */
647 if (oldcwd)
649 chdir( oldcwd );
650 free( oldcwd );
653 /* setup the signal mask */
654 sigemptyset( &block_set );
655 sigaddset( &block_set, SIGALRM );
656 sigaddset( &block_set, SIGIO );
657 sigaddset( &block_set, SIGINT );
658 sigaddset( &block_set, SIGHUP );
660 /* receive the first thread request fd on the main socket */
661 NtCurrentTeb()->request_fd = receive_fd( &dummy_handle );
663 CLIENT_InitThread();
667 /***********************************************************************
668 * CLIENT_InitThread
670 * Send an init thread request. Return 0 if OK.
672 void CLIENT_InitThread(void)
674 TEB *teb = NtCurrentTeb();
675 int version, ret;
676 int reply_pipe[2];
678 /* ignore SIGPIPE so that we get a EPIPE error instead */
679 signal( SIGPIPE, SIG_IGN );
681 /* create the server->client communication pipes */
682 if (pipe( reply_pipe ) == -1) server_protocol_perror( "pipe" );
683 if (pipe( teb->wait_fd ) == -1) server_protocol_perror( "pipe" );
684 wine_server_send_fd( reply_pipe[1] );
685 wine_server_send_fd( teb->wait_fd[1] );
686 teb->reply_fd = reply_pipe[0];
688 /* set close on exec flag */
689 fcntl( teb->reply_fd, F_SETFD, 1 );
690 fcntl( teb->wait_fd[0], F_SETFD, 1 );
691 fcntl( teb->wait_fd[1], F_SETFD, 1 );
693 SERVER_START_REQ( init_thread )
695 req->unix_pid = getpid();
696 req->teb = teb;
697 req->entry = teb->entry_point;
698 req->reply_fd = reply_pipe[1];
699 req->wait_fd = teb->wait_fd[1];
700 ret = wine_server_call( req );
701 teb->pid = reply->pid;
702 teb->tid = reply->tid;
703 version = reply->version;
704 if (reply->boot) boot_thread_id = teb->tid;
705 else if (boot_thread_id == teb->tid) boot_thread_id = 0;
706 close( reply_pipe[1] );
708 SERVER_END_REQ;
710 if (ret) server_protocol_error( "init_thread failed with status %x\n", ret );
711 if (version != SERVER_PROTOCOL_VERSION)
712 server_protocol_error( "version mismatch %d/%d.\n"
713 "Your %s binary was not upgraded correctly,\n"
714 "or you have an older one somewhere in your PATH.\n"
715 "Or maybe the wrong wineserver is still running?\n",
716 version, SERVER_PROTOCOL_VERSION,
717 (version > SERVER_PROTOCOL_VERSION) ? "wine" : "wineserver" );
721 /***********************************************************************
722 * CLIENT_BootDone
724 * Signal that we have finished booting, and set debug level.
726 void CLIENT_BootDone( int debug_level )
728 SERVER_START_REQ( boot_done )
730 req->debug_level = debug_level;
731 wine_server_call( req );
733 SERVER_END_REQ;
737 /***********************************************************************
738 * CLIENT_IsBootThread
740 * Return TRUE if current thread is the boot thread.
742 int CLIENT_IsBootThread(void)
744 return (GetCurrentThreadId() == (DWORD)boot_thread_id);