- Fix some spelling problems.
[wine/multimedia.git] / scheduler / client.c
blob494430357963b377a00fc9903f0041f205a9c141
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/library.h"
50 #include "wine/server.h"
51 #include "winerror.h"
52 #include "options.h"
54 /* Some versions of glibc don't define this */
55 #ifndef SCM_RIGHTS
56 #define SCM_RIGHTS 1
57 #endif
59 #define SOCKETNAME "socket" /* name of the socket file */
60 #define LOCKNAME "lock" /* name of the lock 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 #ifdef __GNUC__
78 static void fatal_error( const char *err, ... ) __attribute__((noreturn, format(printf,1,2)));
79 static void fatal_perror( const char *err, ... ) __attribute__((noreturn, format(printf,1,2)));
80 static void server_connect_error( const char *serverdir ) __attribute__((noreturn));
81 #endif
83 /* die on a fatal error; use only during initialization */
84 static void fatal_error( const char *err, ... )
86 va_list args;
88 va_start( args, err );
89 fprintf( stderr, "wine: " );
90 vfprintf( stderr, err, args );
91 va_end( args );
92 exit(1);
95 /* die on a fatal error; use only during initialization */
96 static void fatal_perror( const char *err, ... )
98 va_list args;
100 va_start( args, err );
101 fprintf( stderr, "wine: " );
102 vfprintf( stderr, err, args );
103 perror( " " );
104 va_end( args );
105 exit(1);
108 /***********************************************************************
109 * server_protocol_error
111 void server_protocol_error( const char *err, ... )
113 va_list args;
115 va_start( args, err );
116 fprintf( stderr, "wine client error:%p: ", NtCurrentTeb()->tid );
117 vfprintf( stderr, err, args );
118 va_end( args );
119 SYSDEPS_AbortThread(1);
123 /***********************************************************************
124 * server_protocol_perror
126 void server_protocol_perror( const char *err )
128 fprintf( stderr, "wine client error:%p: ", NtCurrentTeb()->tid );
129 perror( err );
130 SYSDEPS_AbortThread(1);
134 /***********************************************************************
135 * send_request
137 * Send a request to the server.
139 static void send_request( const struct __server_request_info *req )
141 int i, ret;
143 if (!req->u.req.request_header.request_size)
145 if ((ret = write( NtCurrentTeb()->request_fd, &req->u.req,
146 sizeof(req->u.req) )) == sizeof(req->u.req)) return;
149 else
151 struct iovec vec[__SERVER_MAX_DATA+1];
153 vec[0].iov_base = (void *)&req->u.req;
154 vec[0].iov_len = sizeof(req->u.req);
155 for (i = 0; i < req->data_count; i++)
157 vec[i+1].iov_base = (void *)req->data[i].ptr;
158 vec[i+1].iov_len = req->data[i].size;
160 if ((ret = writev( NtCurrentTeb()->request_fd, vec, i+1 )) ==
161 req->u.req.request_header.request_size + sizeof(req->u.req)) return;
164 if (ret >= 0) server_protocol_error( "partial write %d\n", ret );
165 if (errno == EPIPE) SYSDEPS_AbortThread(0);
166 server_protocol_perror( "sendmsg" );
170 /***********************************************************************
171 * read_reply_data
173 * Read data from the reply buffer; helper for wait_reply.
175 static void read_reply_data( void *buffer, size_t size )
177 int ret;
179 for (;;)
181 if ((ret = read( NtCurrentTeb()->reply_fd, buffer, size )) > 0)
183 if (!(size -= ret)) return;
184 buffer = (char *)buffer + ret;
185 continue;
187 if (!ret) break;
188 if (errno == EINTR) continue;
189 if (errno == EPIPE) break;
190 server_protocol_perror("read");
192 /* the server closed the connection; time to die... */
193 SYSDEPS_AbortThread(0);
197 /***********************************************************************
198 * wait_reply
200 * Wait for a reply from the server.
202 inline static void wait_reply( struct __server_request_info *req )
204 read_reply_data( &req->u.reply, sizeof(req->u.reply) );
205 if (req->u.reply.reply_header.reply_size)
206 read_reply_data( req->reply_data, req->u.reply.reply_header.reply_size );
210 /***********************************************************************
211 * wine_server_call (NTDLL.@)
213 * Perform a server call.
215 unsigned int wine_server_call( void *req_ptr )
217 struct __server_request_info * const req = req_ptr;
218 sigset_t old_set;
220 memset( (char *)&req->u.req + req->size, 0, sizeof(req->u.req) - req->size );
221 sigprocmask( SIG_BLOCK, &block_set, &old_set );
222 send_request( req );
223 wait_reply( req );
224 sigprocmask( SIG_SETMASK, &old_set, NULL );
225 return req->u.reply.reply_header.error;
229 /***********************************************************************
230 * wine_server_send_fd
232 * Send a file descriptor to the server.
234 void wine_server_send_fd( int fd )
236 #ifndef HAVE_MSGHDR_ACCRIGHTS
237 struct cmsg_fd cmsg;
238 #endif
239 struct send_fd data;
240 struct msghdr msghdr;
241 struct iovec vec;
242 int ret;
244 vec.iov_base = (void *)&data;
245 vec.iov_len = sizeof(data);
247 msghdr.msg_name = NULL;
248 msghdr.msg_namelen = 0;
249 msghdr.msg_iov = &vec;
250 msghdr.msg_iovlen = 1;
252 #ifdef HAVE_MSGHDR_ACCRIGHTS
253 msghdr.msg_accrights = (void *)&fd;
254 msghdr.msg_accrightslen = sizeof(fd);
255 #else /* HAVE_MSGHDR_ACCRIGHTS */
256 cmsg.len = sizeof(cmsg);
257 cmsg.level = SOL_SOCKET;
258 cmsg.type = SCM_RIGHTS;
259 cmsg.fd = fd;
260 msghdr.msg_control = &cmsg;
261 msghdr.msg_controllen = sizeof(cmsg);
262 msghdr.msg_flags = 0;
263 #endif /* HAVE_MSGHDR_ACCRIGHTS */
265 data.tid = (void *)GetCurrentThreadId();
266 data.fd = fd;
268 for (;;)
270 if ((ret = sendmsg( fd_socket, &msghdr, 0 )) == sizeof(data)) return;
271 if (ret >= 0) server_protocol_error( "partial write %d\n", ret );
272 if (errno == EINTR) continue;
273 if (errno == EPIPE) SYSDEPS_AbortThread(0);
274 server_protocol_perror( "sendmsg" );
279 /***********************************************************************
280 * receive_fd
282 * Receive a file descriptor passed from the server.
284 static int receive_fd( obj_handle_t *handle )
286 struct iovec vec;
287 int ret, fd;
289 #ifdef HAVE_MSGHDR_ACCRIGHTS
290 struct msghdr msghdr;
292 fd = -1;
293 msghdr.msg_accrights = (void *)&fd;
294 msghdr.msg_accrightslen = sizeof(fd);
295 #else /* HAVE_MSGHDR_ACCRIGHTS */
296 struct msghdr msghdr;
297 struct cmsg_fd cmsg;
299 cmsg.len = sizeof(cmsg);
300 cmsg.level = SOL_SOCKET;
301 cmsg.type = SCM_RIGHTS;
302 cmsg.fd = -1;
303 msghdr.msg_control = &cmsg;
304 msghdr.msg_controllen = sizeof(cmsg);
305 msghdr.msg_flags = 0;
306 #endif /* HAVE_MSGHDR_ACCRIGHTS */
308 msghdr.msg_name = NULL;
309 msghdr.msg_namelen = 0;
310 msghdr.msg_iov = &vec;
311 msghdr.msg_iovlen = 1;
312 vec.iov_base = (void *)handle;
313 vec.iov_len = sizeof(*handle);
315 for (;;)
317 if ((ret = recvmsg( fd_socket, &msghdr, 0 )) > 0)
319 #ifndef HAVE_MSGHDR_ACCRIGHTS
320 fd = cmsg.fd;
321 #endif
322 if (fd == -1) server_protocol_error( "no fd received for handle %d\n", *handle );
323 fcntl( fd, F_SETFD, 1 ); /* set close on exec flag */
324 return fd;
326 if (!ret) break;
327 if (errno == EINTR) continue;
328 if (errno == EPIPE) break;
329 server_protocol_perror("recvmsg");
331 /* the server closed the connection; time to die... */
332 SYSDEPS_AbortThread(0);
336 /***********************************************************************
337 * store_cached_fd
339 * Store the cached fd value for a given handle back into the server.
340 * Returns the new fd, which can be different if there was already an
341 * fd in the cache for that handle.
343 inline static int store_cached_fd( int fd, obj_handle_t handle )
345 SERVER_START_REQ( set_handle_info )
347 req->handle = handle;
348 req->flags = 0;
349 req->mask = 0;
350 req->fd = fd;
351 if (!wine_server_call( req ))
353 if (reply->cur_fd != fd)
355 /* someone was here before us */
356 close( fd );
357 fd = reply->cur_fd;
360 else
362 close( fd );
363 fd = -1;
366 SERVER_END_REQ;
367 return fd;
371 /***********************************************************************
372 * wine_server_fd_to_handle (NTDLL.@)
374 * Allocate a file handle for a Unix fd.
376 int wine_server_fd_to_handle( int fd, unsigned int access, int inherit, obj_handle_t *handle )
378 int ret;
380 *handle = 0;
381 wine_server_send_fd( fd );
383 SERVER_START_REQ( alloc_file_handle )
385 req->access = access;
386 req->inherit = inherit;
387 req->fd = fd;
388 if (!(ret = wine_server_call( req ))) *handle = reply->handle;
390 SERVER_END_REQ;
391 return ret;
395 /***********************************************************************
396 * wine_server_handle_to_fd (NTDLL.@)
398 * Retrieve the Unix fd corresponding to a file handle.
400 int wine_server_handle_to_fd( obj_handle_t handle, unsigned int access, int *unix_fd,
401 enum fd_type *type, int *flags )
403 obj_handle_t fd_handle;
404 int ret, fd = -1;
406 *unix_fd = -1;
407 for (;;)
409 SERVER_START_REQ( get_handle_fd )
411 req->handle = handle;
412 req->access = access;
413 if (!(ret = wine_server_call( req ))) fd = reply->fd;
414 if (type) *type = reply->type;
415 if (flags) *flags = reply->flags;
417 SERVER_END_REQ;
418 if (ret) return ret;
420 if (fd != -1) break;
422 /* it wasn't in the cache, get it from the server */
423 fd = receive_fd( &fd_handle );
424 /* and store it back into the cache */
425 fd = store_cached_fd( fd, fd_handle );
427 if (fd_handle == handle) break;
428 /* if we received a different handle this means there was
429 * a race with another thread; we restart everything from
430 * scratch in this case.
434 if ((fd != -1) && ((fd = dup(fd)) == -1)) return STATUS_TOO_MANY_OPENED_FILES;
435 *unix_fd = fd;
436 return STATUS_SUCCESS;
440 /***********************************************************************
441 * start_server
443 * Start a new wine server.
445 static void start_server( const char *oldcwd )
447 static int started; /* we only try once */
448 char *path, *p;
449 if (!started)
451 int status;
452 int pid = fork();
453 if (pid == -1) fatal_perror( "fork" );
454 if (!pid)
456 /* if server is explicitly specified, use this */
457 if ((p = getenv("WINESERVER")))
459 if (p[0] != '/' && oldcwd[0] == '/') /* make it an absolute path */
461 if (!(path = malloc( strlen(oldcwd) + strlen(p) + 1 )))
462 fatal_error( "out of memory\n" );
463 sprintf( path, "%s/%s", oldcwd, p );
464 p = path;
466 execl( p, p, NULL );
467 fatal_perror( "could not exec the server '%s'\n"
468 " specified in the WINESERVER environment variable", p );
471 /* first try the installation dir */
472 execl( BINDIR "/wineserver", "wineserver", NULL );
474 /* now try the dir we were launched from */
475 if (full_argv0)
477 if (!(path = malloc( strlen(full_argv0) + 20 )))
478 fatal_error( "out of memory\n" );
479 if ((p = strrchr( strcpy( path, full_argv0 ), '/' )))
481 strcpy( p, "/wineserver" );
482 execl( path, path, NULL );
484 free(path);
487 /* finally try the path */
488 execlp( "wineserver", "wineserver", NULL );
489 fatal_error( "could not exec wineserver\n" );
491 waitpid( pid, &status, 0 );
492 status = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
493 if (status == 2) return; /* server lock held by someone else, will retry later */
494 if (status) exit(status); /* server failed */
495 started = 1;
500 /***********************************************************************
501 * server_connect_error
503 * Try to display a meaningful explanation of why we couldn't connect
504 * to the server.
506 static void server_connect_error( const char *serverdir )
508 int fd;
509 struct flock fl;
511 if ((fd = open( LOCKNAME, O_WRONLY )) == -1)
512 fatal_error( "for some mysterious reason, the wine server never started.\n" );
514 fl.l_type = F_WRLCK;
515 fl.l_whence = SEEK_SET;
516 fl.l_start = 0;
517 fl.l_len = 1;
518 if (fcntl( fd, F_GETLK, &fl ) != -1)
520 if (fl.l_type == F_WRLCK) /* the file is locked */
521 fatal_error( "a wine server seems to be running, but I cannot connect to it.\n"
522 " You probably need to kill that process (it might be pid %d).\n",
523 (int)fl.l_pid );
524 fatal_error( "for some mysterious reason, the wine server failed to run.\n" );
526 fatal_error( "the file system of '%s' doesn't support locks,\n"
527 " and there is a 'socket' file in that directory that prevents wine from starting.\n"
528 " You should make sure no wine server is running, remove that file and try again.\n",
529 serverdir );
533 /***********************************************************************
534 * server_connect
536 * Attempt to connect to an existing server socket.
537 * We need to be in the server directory already.
539 static int server_connect( const char *oldcwd, const char *serverdir )
541 struct sockaddr_un addr;
542 struct stat st;
543 int s, slen, retry;
545 /* chdir to the server directory */
546 if (chdir( serverdir ) == -1)
548 if (errno != ENOENT) fatal_perror( "chdir to %s", serverdir );
549 start_server( "." );
550 if (chdir( serverdir ) == -1) fatal_perror( "chdir to %s", serverdir );
553 /* make sure we are at the right place */
554 if (stat( ".", &st ) == -1) fatal_perror( "stat %s", serverdir );
555 if (st.st_uid != getuid()) fatal_error( "'%s' is not owned by you\n", serverdir );
556 if (st.st_mode & 077) fatal_error( "'%s' must not be accessible by other users\n", serverdir );
558 for (retry = 0; retry < 6; retry++)
560 /* if not the first try, wait a bit to leave the previous server time to exit */
561 if (retry)
563 usleep( 100000 * retry * retry );
564 start_server( oldcwd );
565 if (lstat( SOCKETNAME, &st ) == -1) continue; /* still no socket, wait a bit more */
567 else if (lstat( SOCKETNAME, &st ) == -1) /* check for an already existing socket */
569 if (errno != ENOENT) fatal_perror( "lstat %s/%s", serverdir, SOCKETNAME );
570 start_server( oldcwd );
571 if (lstat( SOCKETNAME, &st ) == -1) continue; /* still no socket, wait a bit more */
574 /* make sure the socket is sane (ISFIFO needed for Solaris) */
575 if (!S_ISSOCK(st.st_mode) && !S_ISFIFO(st.st_mode))
576 fatal_error( "'%s/%s' is not a socket\n", serverdir, SOCKETNAME );
577 if (st.st_uid != getuid())
578 fatal_error( "'%s/%s' is not owned by you\n", serverdir, SOCKETNAME );
580 /* try to connect to it */
581 addr.sun_family = AF_UNIX;
582 strcpy( addr.sun_path, SOCKETNAME );
583 slen = sizeof(addr) - sizeof(addr.sun_path) + strlen(addr.sun_path) + 1;
584 #ifdef HAVE_SOCKADDR_SUN_LEN
585 addr.sun_len = slen;
586 #endif
587 if ((s = socket( AF_UNIX, SOCK_STREAM, 0 )) == -1) fatal_perror( "socket" );
588 if (connect( s, (struct sockaddr *)&addr, slen ) != -1)
590 fcntl( s, F_SETFD, 1 ); /* set close on exec flag */
591 return s;
593 close( s );
595 server_connect_error( serverdir );
599 /***********************************************************************
600 * CLIENT_InitServer
602 * Start the server and create the initial socket pair.
604 void CLIENT_InitServer(void)
606 int size;
607 char *oldcwd;
608 obj_handle_t dummy_handle;
610 /* retrieve the current directory */
611 for (size = 512; ; size *= 2)
613 if (!(oldcwd = malloc( size ))) break;
614 if (getcwd( oldcwd, size )) break;
615 free( oldcwd );
616 if (errno == ERANGE) continue;
617 oldcwd = NULL;
618 break;
621 /* if argv[0] is a relative path, make it absolute */
622 full_argv0 = argv0;
623 if (oldcwd && argv0[0] != '/' && strchr( argv0, '/' ))
625 char *new_argv0 = malloc( strlen(oldcwd) + strlen(argv0) + 2 );
626 if (new_argv0)
628 strcpy( new_argv0, oldcwd );
629 strcat( new_argv0, "/" );
630 strcat( new_argv0, argv0 );
631 full_argv0 = new_argv0;
635 /* connect to the server */
636 fd_socket = server_connect( oldcwd, wine_get_server_dir() );
638 /* switch back to the starting directory */
639 if (oldcwd)
641 chdir( oldcwd );
642 free( oldcwd );
645 /* setup the signal mask */
646 sigemptyset( &block_set );
647 sigaddset( &block_set, SIGALRM );
648 sigaddset( &block_set, SIGIO );
649 sigaddset( &block_set, SIGINT );
650 sigaddset( &block_set, SIGHUP );
652 /* receive the first thread request fd on the main socket */
653 NtCurrentTeb()->request_fd = receive_fd( &dummy_handle );
655 CLIENT_InitThread();
659 /***********************************************************************
660 * CLIENT_InitThread
662 * Send an init thread request. Return 0 if OK.
664 void CLIENT_InitThread(void)
666 TEB *teb = NtCurrentTeb();
667 int version, ret;
668 int reply_pipe[2];
670 /* ignore SIGPIPE so that we get a EPIPE error instead */
671 signal( SIGPIPE, SIG_IGN );
672 /* automatic child reaping to avoid zombies */
673 signal( SIGCHLD, SIG_IGN );
675 /* create the server->client communication pipes */
676 if (pipe( reply_pipe ) == -1) server_protocol_perror( "pipe" );
677 if (pipe( teb->wait_fd ) == -1) server_protocol_perror( "pipe" );
678 wine_server_send_fd( reply_pipe[1] );
679 wine_server_send_fd( teb->wait_fd[1] );
680 teb->reply_fd = reply_pipe[0];
682 /* set close on exec flag */
683 fcntl( teb->reply_fd, F_SETFD, 1 );
684 fcntl( teb->wait_fd[0], F_SETFD, 1 );
685 fcntl( teb->wait_fd[1], F_SETFD, 1 );
687 SERVER_START_REQ( init_thread )
689 req->unix_pid = getpid();
690 req->teb = teb;
691 req->entry = teb->entry_point;
692 req->reply_fd = reply_pipe[1];
693 req->wait_fd = teb->wait_fd[1];
694 ret = wine_server_call( req );
695 teb->pid = reply->pid;
696 teb->tid = reply->tid;
697 version = reply->version;
698 if (reply->boot) boot_thread_id = teb->tid;
699 else if (boot_thread_id == teb->tid) boot_thread_id = 0;
700 close( reply_pipe[1] );
702 SERVER_END_REQ;
704 if (ret) server_protocol_error( "init_thread failed with status %x\n", ret );
705 if (version != SERVER_PROTOCOL_VERSION)
706 server_protocol_error( "version mismatch %d/%d.\n"
707 "Your %s binary was not upgraded correctly,\n"
708 "or you have an older one somewhere in your PATH.\n"
709 "Or maybe the wrong wineserver is still running?\n",
710 version, SERVER_PROTOCOL_VERSION,
711 (version > SERVER_PROTOCOL_VERSION) ? "wine" : "wineserver" );
715 /***********************************************************************
716 * CLIENT_BootDone
718 * Signal that we have finished booting, and set debug level.
720 void CLIENT_BootDone( int debug_level )
722 SERVER_START_REQ( boot_done )
724 req->debug_level = debug_level;
725 wine_server_call( req );
727 SERVER_END_REQ;
731 /***********************************************************************
732 * CLIENT_IsBootThread
734 * Return TRUE if current thread is the boot thread.
736 int CLIENT_IsBootThread(void)
738 return (GetCurrentThreadId() == (DWORD)boot_thread_id);