Moved a couple more functions to dlls/kernel.
[wine.git] / scheduler / client.c
blobec62534a8f814ebd70cbe7ce158a00dcd03060f4
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 <signal.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <sys/types.h>
32 #ifdef HAVE_SYS_SOCKET_H
33 # include <sys/socket.h>
34 #endif
35 #ifdef HAVE_SYS_WAIT_H
36 #include <sys/wait.h>
37 #endif
38 #ifdef HAVE_SYS_UN_H
39 #include <sys/un.h>
40 #endif
41 #ifdef HAVE_SYS_MMAN_H
42 #include <sys/mman.h>
43 #endif
44 #include <sys/stat.h>
45 #ifdef HAVE_SYS_UIO_H
46 #include <sys/uio.h>
47 #endif
48 #ifdef HAVE_UNISTD_H
49 # include <unistd.h>
50 #endif
51 #include <stdarg.h>
53 #include "ntstatus.h"
54 #include "thread.h"
55 #include "wine/library.h"
56 #include "wine/server.h"
57 #include "winerror.h"
58 #include "options.h"
60 /* Some versions of glibc don't define this */
61 #ifndef SCM_RIGHTS
62 #define SCM_RIGHTS 1
63 #endif
65 #define SOCKETNAME "socket" /* name of the socket file */
66 #define LOCKNAME "lock" /* name of the lock file */
68 #ifndef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
69 /* data structure used to pass an fd with sendmsg/recvmsg */
70 struct cmsg_fd
72 int len; /* size of structure */
73 int level; /* SOL_SOCKET */
74 int type; /* SCM_RIGHTS */
75 int fd; /* fd to pass */
77 #endif /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
79 static HANDLE boot_thread_id;
80 static sigset_t block_set; /* signals to block during server calls */
81 static int fd_socket; /* socket to exchange file descriptors with the server */
83 #ifdef __GNUC__
84 static void fatal_error( const char *err, ... ) __attribute__((noreturn, format(printf,1,2)));
85 static void fatal_perror( const char *err, ... ) __attribute__((noreturn, format(printf,1,2)));
86 static void server_connect_error( const char *serverdir ) __attribute__((noreturn));
87 #endif
89 /* die on a fatal error; use only during initialization */
90 static void fatal_error( const char *err, ... )
92 va_list args;
94 va_start( args, err );
95 fprintf( stderr, "wine: " );
96 vfprintf( stderr, err, args );
97 va_end( args );
98 exit(1);
101 /* die on a fatal error; use only during initialization */
102 static void fatal_perror( const char *err, ... )
104 va_list args;
106 va_start( args, err );
107 fprintf( stderr, "wine: " );
108 vfprintf( stderr, err, args );
109 perror( " " );
110 va_end( args );
111 exit(1);
114 /***********************************************************************
115 * server_protocol_error
117 void server_protocol_error( const char *err, ... )
119 va_list args;
121 va_start( args, err );
122 fprintf( stderr, "wine client error:%lx: ", GetCurrentThreadId() );
123 vfprintf( stderr, err, args );
124 va_end( args );
125 SYSDEPS_AbortThread(1);
129 /***********************************************************************
130 * server_protocol_perror
132 void server_protocol_perror( const char *err )
134 fprintf( stderr, "wine client error:%lx: ", GetCurrentThreadId() );
135 perror( err );
136 SYSDEPS_AbortThread(1);
140 /***********************************************************************
141 * send_request
143 * Send a request to the server.
145 static void send_request( const struct __server_request_info *req )
147 int i, ret;
149 if (!req->u.req.request_header.request_size)
151 if ((ret = write( NtCurrentTeb()->request_fd, &req->u.req,
152 sizeof(req->u.req) )) == sizeof(req->u.req)) return;
155 else
157 struct iovec vec[__SERVER_MAX_DATA+1];
159 vec[0].iov_base = (void *)&req->u.req;
160 vec[0].iov_len = sizeof(req->u.req);
161 for (i = 0; i < req->data_count; i++)
163 vec[i+1].iov_base = (void *)req->data[i].ptr;
164 vec[i+1].iov_len = req->data[i].size;
166 if ((ret = writev( NtCurrentTeb()->request_fd, vec, i+1 )) ==
167 req->u.req.request_header.request_size + sizeof(req->u.req)) return;
170 if (ret >= 0) server_protocol_error( "partial write %d\n", ret );
171 if (errno == EPIPE) SYSDEPS_AbortThread(0);
172 server_protocol_perror( "sendmsg" );
176 /***********************************************************************
177 * read_reply_data
179 * Read data from the reply buffer; helper for wait_reply.
181 static void read_reply_data( void *buffer, size_t size )
183 int ret;
185 for (;;)
187 if ((ret = read( NtCurrentTeb()->reply_fd, buffer, size )) > 0)
189 if (!(size -= ret)) return;
190 buffer = (char *)buffer + ret;
191 continue;
193 if (!ret) break;
194 if (errno == EINTR) continue;
195 if (errno == EPIPE) break;
196 server_protocol_perror("read");
198 /* the server closed the connection; time to die... */
199 SYSDEPS_AbortThread(0);
203 /***********************************************************************
204 * wait_reply
206 * Wait for a reply from the server.
208 inline static void wait_reply( struct __server_request_info *req )
210 read_reply_data( &req->u.reply, sizeof(req->u.reply) );
211 if (req->u.reply.reply_header.reply_size)
212 read_reply_data( req->reply_data, req->u.reply.reply_header.reply_size );
216 /***********************************************************************
217 * wine_server_call (NTDLL.@)
219 * Perform a server call.
221 unsigned int wine_server_call( void *req_ptr )
223 struct __server_request_info * const req = req_ptr;
224 sigset_t old_set;
226 sigprocmask( SIG_BLOCK, &block_set, &old_set );
227 send_request( req );
228 wait_reply( req );
229 sigprocmask( SIG_SETMASK, &old_set, NULL );
230 return req->u.reply.reply_header.error;
234 /***********************************************************************
235 * wine_server_send_fd
237 * Send a file descriptor to the server.
239 void wine_server_send_fd( int fd )
241 #ifndef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
242 struct cmsg_fd cmsg;
243 #endif
244 struct send_fd data;
245 struct msghdr msghdr;
246 struct iovec vec;
247 int ret;
249 vec.iov_base = (void *)&data;
250 vec.iov_len = sizeof(data);
252 msghdr.msg_name = NULL;
253 msghdr.msg_namelen = 0;
254 msghdr.msg_iov = &vec;
255 msghdr.msg_iovlen = 1;
257 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
258 msghdr.msg_accrights = (void *)&fd;
259 msghdr.msg_accrightslen = sizeof(fd);
260 #else /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
261 cmsg.len = sizeof(cmsg);
262 cmsg.level = SOL_SOCKET;
263 cmsg.type = SCM_RIGHTS;
264 cmsg.fd = fd;
265 msghdr.msg_control = &cmsg;
266 msghdr.msg_controllen = sizeof(cmsg);
267 msghdr.msg_flags = 0;
268 #endif /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
270 data.tid = GetCurrentThreadId();
271 data.fd = fd;
273 for (;;)
275 if ((ret = sendmsg( fd_socket, &msghdr, 0 )) == sizeof(data)) return;
276 if (ret >= 0) server_protocol_error( "partial write %d\n", ret );
277 if (errno == EINTR) continue;
278 if (errno == EPIPE) SYSDEPS_AbortThread(0);
279 server_protocol_perror( "sendmsg" );
284 /***********************************************************************
285 * receive_fd
287 * Receive a file descriptor passed from the server.
289 static int receive_fd( obj_handle_t *handle )
291 struct iovec vec;
292 int ret, fd;
294 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
295 struct msghdr msghdr;
297 fd = -1;
298 msghdr.msg_accrights = (void *)&fd;
299 msghdr.msg_accrightslen = sizeof(fd);
300 #else /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
301 struct msghdr msghdr;
302 struct cmsg_fd cmsg;
304 cmsg.len = sizeof(cmsg);
305 cmsg.level = SOL_SOCKET;
306 cmsg.type = SCM_RIGHTS;
307 cmsg.fd = -1;
308 msghdr.msg_control = &cmsg;
309 msghdr.msg_controllen = sizeof(cmsg);
310 msghdr.msg_flags = 0;
311 #endif /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
313 msghdr.msg_name = NULL;
314 msghdr.msg_namelen = 0;
315 msghdr.msg_iov = &vec;
316 msghdr.msg_iovlen = 1;
317 vec.iov_base = (void *)handle;
318 vec.iov_len = sizeof(*handle);
320 for (;;)
322 if ((ret = recvmsg( fd_socket, &msghdr, 0 )) > 0)
324 #ifndef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
325 fd = cmsg.fd;
326 #endif
327 if (fd == -1) server_protocol_error( "no fd received for handle %d\n", *handle );
328 fcntl( fd, F_SETFD, 1 ); /* set close on exec flag */
329 return fd;
331 if (!ret) break;
332 if (errno == EINTR) continue;
333 if (errno == EPIPE) break;
334 server_protocol_perror("recvmsg");
336 /* the server closed the connection; time to die... */
337 SYSDEPS_AbortThread(0);
341 /***********************************************************************
342 * store_cached_fd
344 * Store the cached fd value for a given handle back into the server.
345 * Returns the new fd, which can be different if there was already an
346 * fd in the cache for that handle.
348 inline static int store_cached_fd( int fd, obj_handle_t handle )
350 SERVER_START_REQ( set_handle_info )
352 req->handle = handle;
353 req->flags = 0;
354 req->mask = 0;
355 req->fd = fd;
356 if (!wine_server_call( req ))
358 if (reply->cur_fd != fd)
360 /* someone was here before us */
361 close( fd );
362 fd = reply->cur_fd;
365 else
367 close( fd );
368 fd = -1;
371 SERVER_END_REQ;
372 return fd;
376 /***********************************************************************
377 * wine_server_fd_to_handle (NTDLL.@)
379 * Allocate a file handle for a Unix fd.
381 int wine_server_fd_to_handle( int fd, unsigned int access, int inherit, obj_handle_t *handle )
383 int ret;
385 *handle = 0;
386 wine_server_send_fd( fd );
388 SERVER_START_REQ( alloc_file_handle )
390 req->access = access;
391 req->inherit = inherit;
392 req->fd = fd;
393 if (!(ret = wine_server_call( req ))) *handle = reply->handle;
395 SERVER_END_REQ;
396 return ret;
400 /***********************************************************************
401 * wine_server_handle_to_fd (NTDLL.@)
403 * Retrieve the Unix fd corresponding to a file handle.
405 int wine_server_handle_to_fd( obj_handle_t handle, unsigned int access, int *unix_fd,
406 enum fd_type *type, int *flags )
408 obj_handle_t fd_handle;
409 int ret, fd = -1;
411 *unix_fd = -1;
412 for (;;)
414 SERVER_START_REQ( get_handle_fd )
416 req->handle = handle;
417 req->access = access;
418 if (!(ret = wine_server_call( req ))) fd = reply->fd;
419 if (type) *type = reply->type;
420 if (flags) *flags = reply->flags;
422 SERVER_END_REQ;
423 if (ret) return ret;
425 if (fd != -1) break;
427 /* it wasn't in the cache, get it from the server */
428 fd = receive_fd( &fd_handle );
429 /* and store it back into the cache */
430 fd = store_cached_fd( fd, fd_handle );
432 if (fd_handle == handle) break;
433 /* if we received a different handle this means there was
434 * a race with another thread; we restart everything from
435 * scratch in this case.
439 if ((fd != -1) && ((fd = dup(fd)) == -1)) return STATUS_TOO_MANY_OPENED_FILES;
440 *unix_fd = fd;
441 return STATUS_SUCCESS;
445 /***********************************************************************
446 * start_server
448 * Start a new wine server.
450 static void start_server( const char *oldcwd )
452 static int started; /* we only try once */
453 char *path, *p;
454 if (!started)
456 int status;
457 int pid = fork();
458 if (pid == -1) fatal_perror( "fork" );
459 if (!pid)
461 /* if server is explicitly specified, use this */
462 if ((p = getenv("WINESERVER")))
464 if (p[0] != '/' && oldcwd[0] == '/') /* make it an absolute path */
466 if (!(path = malloc( strlen(oldcwd) + strlen(p) + 1 )))
467 fatal_error( "out of memory\n" );
468 sprintf( path, "%s/%s", oldcwd, p );
469 p = path;
471 execl( p, p, NULL );
472 fatal_perror( "could not exec the server '%s'\n"
473 " specified in the WINESERVER environment variable", p );
476 /* first try the installation dir */
477 execl( BINDIR "/wineserver", "wineserver", NULL );
479 /* now try the dir we were launched from */
480 if (full_argv0)
482 if (!(path = malloc( strlen(full_argv0) + 20 )))
483 fatal_error( "out of memory\n" );
484 if ((p = strrchr( strcpy( path, full_argv0 ), '/' )))
486 strcpy( p, "/wineserver" );
487 execl( path, path, NULL );
489 free(path);
492 /* finally try the path */
493 execlp( "wineserver", "wineserver", NULL );
494 fatal_error( "could not exec wineserver\n" );
496 waitpid( pid, &status, 0 );
497 status = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
498 if (status == 2) return; /* server lock held by someone else, will retry later */
499 if (status) exit(status); /* server failed */
500 started = 1;
505 /***********************************************************************
506 * server_connect_error
508 * Try to display a meaningful explanation of why we couldn't connect
509 * to the server.
511 static void server_connect_error( const char *serverdir )
513 int fd;
514 struct flock fl;
516 if ((fd = open( LOCKNAME, O_WRONLY )) == -1)
517 fatal_error( "for some mysterious reason, the wine server never started.\n" );
519 fl.l_type = F_WRLCK;
520 fl.l_whence = SEEK_SET;
521 fl.l_start = 0;
522 fl.l_len = 1;
523 if (fcntl( fd, F_GETLK, &fl ) != -1)
525 if (fl.l_type == F_WRLCK) /* the file is locked */
526 fatal_error( "a wine server seems to be running, but I cannot connect to it.\n"
527 " You probably need to kill that process (it might be pid %d).\n",
528 (int)fl.l_pid );
529 fatal_error( "for some mysterious reason, the wine server failed to run.\n" );
531 fatal_error( "the file system of '%s' doesn't support locks,\n"
532 " and there is a 'socket' file in that directory that prevents wine from starting.\n"
533 " You should make sure no wine server is running, remove that file and try again.\n",
534 serverdir );
538 /***********************************************************************
539 * server_connect
541 * Attempt to connect to an existing server socket.
542 * We need to be in the server directory already.
544 static int server_connect( const char *oldcwd, const char *serverdir )
546 struct sockaddr_un addr;
547 struct stat st;
548 int s, slen, retry;
550 /* chdir to the server directory */
551 if (chdir( serverdir ) == -1)
553 if (errno != ENOENT) fatal_perror( "chdir to %s", serverdir );
554 start_server( "." );
555 if (chdir( serverdir ) == -1) fatal_perror( "chdir to %s", serverdir );
558 /* make sure we are at the right place */
559 if (stat( ".", &st ) == -1) fatal_perror( "stat %s", serverdir );
560 if (st.st_uid != getuid()) fatal_error( "'%s' is not owned by you\n", serverdir );
561 if (st.st_mode & 077) fatal_error( "'%s' must not be accessible by other users\n", serverdir );
563 for (retry = 0; retry < 6; retry++)
565 /* if not the first try, wait a bit to leave the previous server time to exit */
566 if (retry)
568 usleep( 100000 * retry * retry );
569 start_server( oldcwd );
570 if (lstat( SOCKETNAME, &st ) == -1) continue; /* still no socket, wait a bit more */
572 else if (lstat( SOCKETNAME, &st ) == -1) /* check for an already existing socket */
574 if (errno != ENOENT) fatal_perror( "lstat %s/%s", serverdir, SOCKETNAME );
575 start_server( oldcwd );
576 if (lstat( SOCKETNAME, &st ) == -1) continue; /* still no socket, wait a bit more */
579 /* make sure the socket is sane (ISFIFO needed for Solaris) */
580 if (!S_ISSOCK(st.st_mode) && !S_ISFIFO(st.st_mode))
581 fatal_error( "'%s/%s' is not a socket\n", serverdir, SOCKETNAME );
582 if (st.st_uid != getuid())
583 fatal_error( "'%s/%s' is not owned by you\n", serverdir, SOCKETNAME );
585 /* try to connect to it */
586 addr.sun_family = AF_UNIX;
587 strcpy( addr.sun_path, SOCKETNAME );
588 slen = sizeof(addr) - sizeof(addr.sun_path) + strlen(addr.sun_path) + 1;
589 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
590 addr.sun_len = slen;
591 #endif
592 if ((s = socket( AF_UNIX, SOCK_STREAM, 0 )) == -1) fatal_perror( "socket" );
593 if (connect( s, (struct sockaddr *)&addr, slen ) != -1)
595 fcntl( s, F_SETFD, 1 ); /* set close on exec flag */
596 return s;
598 close( s );
600 server_connect_error( serverdir );
604 /***********************************************************************
605 * CLIENT_InitServer
607 * Start the server and create the initial socket pair.
609 void CLIENT_InitServer(void)
611 int size;
612 char *oldcwd;
613 obj_handle_t dummy_handle;
615 /* retrieve the current directory */
616 for (size = 512; ; size *= 2)
618 if (!(oldcwd = malloc( size ))) break;
619 if (getcwd( oldcwd, size )) break;
620 free( oldcwd );
621 if (errno == ERANGE) continue;
622 oldcwd = NULL;
623 break;
626 /* if argv[0] is a relative path, make it absolute */
627 full_argv0 = argv0;
628 if (oldcwd && argv0[0] != '/' && strchr( argv0, '/' ))
630 char *new_argv0 = malloc( strlen(oldcwd) + strlen(argv0) + 2 );
631 if (new_argv0)
633 strcpy( new_argv0, oldcwd );
634 strcat( new_argv0, "/" );
635 strcat( new_argv0, argv0 );
636 full_argv0 = new_argv0;
640 /* connect to the server */
641 fd_socket = server_connect( oldcwd, wine_get_server_dir() );
643 /* switch back to the starting directory */
644 if (oldcwd)
646 chdir( oldcwd );
647 free( oldcwd );
650 /* setup the signal mask */
651 sigemptyset( &block_set );
652 sigaddset( &block_set, SIGALRM );
653 sigaddset( &block_set, SIGIO );
654 sigaddset( &block_set, SIGINT );
655 sigaddset( &block_set, SIGHUP );
656 sigaddset( &block_set, SIGUSR1 );
657 sigaddset( &block_set, SIGUSR2 );
659 /* receive the first thread request fd on the main socket */
660 NtCurrentTeb()->request_fd = receive_fd( &dummy_handle );
662 CLIENT_InitThread();
666 /***********************************************************************
667 * CLIENT_InitThread
669 * Send an init thread request. Return 0 if OK.
671 void CLIENT_InitThread(void)
673 TEB *teb = NtCurrentTeb();
674 int version, ret;
675 int reply_pipe[2];
676 struct sigaction sig_act;
678 sig_act.sa_handler = SIG_IGN;
679 sig_act.sa_flags = 0;
680 sigemptyset( &sig_act.sa_mask );
682 /* ignore SIGPIPE so that we get a EPIPE error instead */
683 sigaction( SIGPIPE, &sig_act, NULL );
684 /* automatic child reaping to avoid zombies */
685 #ifdef SA_NOCLDWAIT
686 sig_act.sa_flags |= SA_NOCLDWAIT;
687 #endif
688 sigaction( SIGCHLD, &sig_act, NULL );
690 /* create the server->client communication pipes */
691 if (pipe( reply_pipe ) == -1) server_protocol_perror( "pipe" );
692 if (pipe( teb->wait_fd ) == -1) server_protocol_perror( "pipe" );
693 wine_server_send_fd( reply_pipe[1] );
694 wine_server_send_fd( teb->wait_fd[1] );
695 teb->reply_fd = reply_pipe[0];
696 close( reply_pipe[1] );
698 /* set close on exec flag */
699 fcntl( teb->reply_fd, F_SETFD, 1 );
700 fcntl( teb->wait_fd[0], F_SETFD, 1 );
701 fcntl( teb->wait_fd[1], F_SETFD, 1 );
703 SERVER_START_REQ( init_thread )
705 req->unix_pid = getpid();
706 req->unix_tid = SYSDEPS_GetUnixTid();
707 req->teb = teb;
708 req->entry = teb->entry_point;
709 req->reply_fd = reply_pipe[1];
710 req->wait_fd = teb->wait_fd[1];
711 ret = wine_server_call( req );
712 teb->ClientId.UniqueProcess = (HANDLE)reply->pid;
713 teb->ClientId.UniqueThread = (HANDLE)reply->tid;
714 version = reply->version;
715 if (reply->boot) boot_thread_id = teb->ClientId.UniqueThread;
716 else if (boot_thread_id == teb->ClientId.UniqueThread) boot_thread_id = 0;
718 SERVER_END_REQ;
720 if (ret) server_protocol_error( "init_thread failed with status %x\n", ret );
721 if (version != SERVER_PROTOCOL_VERSION)
722 server_protocol_error( "version mismatch %d/%d.\n"
723 "Your %s binary was not upgraded correctly,\n"
724 "or you have an older one somewhere in your PATH.\n"
725 "Or maybe the wrong wineserver is still running?\n",
726 version, SERVER_PROTOCOL_VERSION,
727 (version > SERVER_PROTOCOL_VERSION) ? "wine" : "wineserver" );
731 /***********************************************************************
732 * CLIENT_BootDone
734 * Signal that we have finished booting, and set debug level.
736 void CLIENT_BootDone( int debug_level )
738 SERVER_START_REQ( boot_done )
740 req->debug_level = debug_level;
741 wine_server_call( req );
743 SERVER_END_REQ;
747 /***********************************************************************
748 * CLIENT_IsBootThread
750 * Return TRUE if current thread is the boot thread.
752 int CLIENT_IsBootThread(void)
754 return (GetCurrentThreadId() == (DWORD)boot_thread_id);