Fixed an error message.
[wine/multimedia.git] / dlls / ntdll / server.c
blob6654df1eec2247e7267c7ef3e36cd58789473a7f
1 /*
2 * Wine 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/pthread.h"
57 #include "wine/server.h"
58 #include "wine/debug.h"
59 #include "winerror.h"
60 #include "ntdll_misc.h"
62 WINE_DEFAULT_DEBUG_CHANNEL(server);
64 /* Some versions of glibc don't define this */
65 #ifndef SCM_RIGHTS
66 #define SCM_RIGHTS 1
67 #endif
69 #define SOCKETNAME "socket" /* name of the socket file */
70 #define LOCKNAME "lock" /* name of the lock file */
72 #ifndef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
73 /* data structure used to pass an fd with sendmsg/recvmsg */
74 struct cmsg_fd
76 int len; /* size of structure */
77 int level; /* SOL_SOCKET */
78 int type; /* SCM_RIGHTS */
79 int fd; /* fd to pass */
81 #endif /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
83 static sigset_t block_set; /* signals to block during server calls */
84 static int fd_socket = -1; /* socket to exchange file descriptors with the server */
86 #ifdef __GNUC__
87 static void fatal_error( const char *err, ... ) __attribute__((noreturn, format(printf,1,2)));
88 static void fatal_perror( const char *err, ... ) __attribute__((noreturn, format(printf,1,2)));
89 static void server_connect_error( const char *serverdir ) __attribute__((noreturn));
90 #endif
92 /* die on a fatal error; use only during initialization */
93 static void fatal_error( const char *err, ... )
95 va_list args;
97 va_start( args, err );
98 fprintf( stderr, "wine: " );
99 vfprintf( stderr, err, args );
100 va_end( args );
101 exit(1);
104 /* die on a fatal error; use only during initialization */
105 static void fatal_perror( const char *err, ... )
107 va_list args;
109 va_start( args, err );
110 fprintf( stderr, "wine: " );
111 vfprintf( stderr, err, args );
112 perror( " " );
113 va_end( args );
114 exit(1);
118 /***********************************************************************
119 * server_abort_thread
121 void server_abort_thread( int status )
123 sigprocmask( SIG_BLOCK, &block_set, NULL );
124 close( NtCurrentTeb()->wait_fd[0] );
125 close( NtCurrentTeb()->wait_fd[1] );
126 close( NtCurrentTeb()->reply_fd );
127 close( NtCurrentTeb()->request_fd );
128 wine_pthread_abort_thread( status );
132 /***********************************************************************
133 * server_protocol_error
135 void server_protocol_error( const char *err, ... )
137 va_list args;
139 va_start( args, err );
140 fprintf( stderr, "wine client error:%lx: ", GetCurrentThreadId() );
141 vfprintf( stderr, err, args );
142 va_end( args );
143 server_abort_thread(1);
147 /***********************************************************************
148 * server_protocol_perror
150 void server_protocol_perror( const char *err )
152 fprintf( stderr, "wine client error:%lx: ", GetCurrentThreadId() );
153 perror( err );
154 server_abort_thread(1);
158 /***********************************************************************
159 * send_request
161 * Send a request to the server.
163 static void send_request( const struct __server_request_info *req )
165 int i, ret;
167 if (!req->u.req.request_header.request_size)
169 if ((ret = write( NtCurrentTeb()->request_fd, &req->u.req,
170 sizeof(req->u.req) )) == sizeof(req->u.req)) return;
173 else
175 struct iovec vec[__SERVER_MAX_DATA+1];
177 vec[0].iov_base = (void *)&req->u.req;
178 vec[0].iov_len = sizeof(req->u.req);
179 for (i = 0; i < req->data_count; i++)
181 vec[i+1].iov_base = (void *)req->data[i].ptr;
182 vec[i+1].iov_len = req->data[i].size;
184 if ((ret = writev( NtCurrentTeb()->request_fd, vec, i+1 )) ==
185 req->u.req.request_header.request_size + sizeof(req->u.req)) return;
188 if (ret >= 0) server_protocol_error( "partial write %d\n", ret );
189 if (errno == EPIPE) server_abort_thread(0);
190 server_protocol_perror( "write" );
194 /***********************************************************************
195 * read_reply_data
197 * Read data from the reply buffer; helper for wait_reply.
199 static void read_reply_data( void *buffer, size_t size )
201 int ret;
203 for (;;)
205 if ((ret = read( NtCurrentTeb()->reply_fd, buffer, size )) > 0)
207 if (!(size -= ret)) return;
208 buffer = (char *)buffer + ret;
209 continue;
211 if (!ret) break;
212 if (errno == EINTR) continue;
213 if (errno == EPIPE) break;
214 server_protocol_perror("read");
216 /* the server closed the connection; time to die... */
217 server_abort_thread(0);
221 /***********************************************************************
222 * wait_reply
224 * Wait for a reply from the server.
226 inline static void wait_reply( struct __server_request_info *req )
228 read_reply_data( &req->u.reply, sizeof(req->u.reply) );
229 if (req->u.reply.reply_header.reply_size)
230 read_reply_data( req->reply_data, req->u.reply.reply_header.reply_size );
234 /***********************************************************************
235 * wine_server_call (NTDLL.@)
237 * Perform a server call.
239 unsigned int wine_server_call( void *req_ptr )
241 struct __server_request_info * const req = req_ptr;
242 sigset_t old_set;
244 sigprocmask( SIG_BLOCK, &block_set, &old_set );
245 send_request( req );
246 wait_reply( req );
247 sigprocmask( SIG_SETMASK, &old_set, NULL );
248 return req->u.reply.reply_header.error;
252 /***********************************************************************
253 * wine_server_send_fd
255 * Send a file descriptor to the server.
257 void wine_server_send_fd( int fd )
259 #ifndef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
260 struct cmsg_fd cmsg;
261 #endif
262 struct send_fd data;
263 struct msghdr msghdr;
264 struct iovec vec;
265 int ret;
267 vec.iov_base = (void *)&data;
268 vec.iov_len = sizeof(data);
270 msghdr.msg_name = NULL;
271 msghdr.msg_namelen = 0;
272 msghdr.msg_iov = &vec;
273 msghdr.msg_iovlen = 1;
275 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
276 msghdr.msg_accrights = (void *)&fd;
277 msghdr.msg_accrightslen = sizeof(fd);
278 #else /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
279 cmsg.len = sizeof(cmsg);
280 cmsg.level = SOL_SOCKET;
281 cmsg.type = SCM_RIGHTS;
282 cmsg.fd = fd;
283 msghdr.msg_control = &cmsg;
284 msghdr.msg_controllen = sizeof(cmsg);
285 msghdr.msg_flags = 0;
286 #endif /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
288 data.tid = GetCurrentThreadId();
289 data.fd = fd;
291 for (;;)
293 if ((ret = sendmsg( fd_socket, &msghdr, 0 )) == sizeof(data)) return;
294 if (ret >= 0) server_protocol_error( "partial write %d\n", ret );
295 if (errno == EINTR) continue;
296 if (errno == EPIPE) server_abort_thread(0);
297 server_protocol_perror( "sendmsg" );
302 /***********************************************************************
303 * receive_fd
305 * Receive a file descriptor passed from the server.
307 static int receive_fd( obj_handle_t *handle )
309 struct iovec vec;
310 int ret, fd;
312 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
313 struct msghdr msghdr;
315 fd = -1;
316 msghdr.msg_accrights = (void *)&fd;
317 msghdr.msg_accrightslen = sizeof(fd);
318 #else /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
319 struct msghdr msghdr;
320 struct cmsg_fd cmsg;
322 cmsg.len = sizeof(cmsg);
323 cmsg.level = SOL_SOCKET;
324 cmsg.type = SCM_RIGHTS;
325 cmsg.fd = -1;
326 msghdr.msg_control = &cmsg;
327 msghdr.msg_controllen = sizeof(cmsg);
328 msghdr.msg_flags = 0;
329 #endif /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
331 msghdr.msg_name = NULL;
332 msghdr.msg_namelen = 0;
333 msghdr.msg_iov = &vec;
334 msghdr.msg_iovlen = 1;
335 vec.iov_base = (void *)handle;
336 vec.iov_len = sizeof(*handle);
338 for (;;)
340 if ((ret = recvmsg( fd_socket, &msghdr, 0 )) > 0)
342 #ifndef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
343 fd = cmsg.fd;
344 #endif
345 if (fd == -1) server_protocol_error( "no fd received for handle %d\n", *handle );
346 fcntl( fd, F_SETFD, 1 ); /* set close on exec flag */
347 return fd;
349 if (!ret) break;
350 if (errno == EINTR) continue;
351 if (errno == EPIPE) break;
352 server_protocol_perror("recvmsg");
354 /* the server closed the connection; time to die... */
355 server_abort_thread(0);
359 /***********************************************************************
360 * store_cached_fd
362 * Store the cached fd value for a given handle back into the server.
363 * Returns the new fd, which can be different if there was already an
364 * fd in the cache for that handle.
366 inline static int store_cached_fd( int *fd, obj_handle_t handle )
368 int ret;
370 SERVER_START_REQ( set_handle_info )
372 req->handle = handle;
373 req->flags = 0;
374 req->mask = 0;
375 req->fd = *fd;
376 if (!(ret = wine_server_call( req )))
378 if (reply->cur_fd != *fd)
380 /* someone was here before us */
381 close( *fd );
382 *fd = reply->cur_fd;
385 else
387 close( *fd );
388 *fd = -1;
391 SERVER_END_REQ;
392 return ret;
396 /***********************************************************************
397 * wine_server_fd_to_handle (NTDLL.@)
399 * Allocate a file handle for a Unix fd.
401 int wine_server_fd_to_handle( int fd, unsigned int access, int inherit, obj_handle_t *handle )
403 int ret;
405 *handle = 0;
406 wine_server_send_fd( fd );
408 SERVER_START_REQ( alloc_file_handle )
410 req->access = access;
411 req->inherit = inherit;
412 req->fd = fd;
413 if (!(ret = wine_server_call( req ))) *handle = reply->handle;
415 SERVER_END_REQ;
416 return ret;
420 /***********************************************************************
421 * wine_server_handle_to_fd (NTDLL.@)
423 * Retrieve the Unix fd corresponding to a file handle.
425 int wine_server_handle_to_fd( obj_handle_t handle, unsigned int access, int *unix_fd,
426 enum fd_type *type, int *flags )
428 obj_handle_t fd_handle;
429 int ret, fd = -1;
431 *unix_fd = -1;
432 for (;;)
434 SERVER_START_REQ( get_handle_fd )
436 req->handle = handle;
437 req->access = access;
438 if (!(ret = wine_server_call( req ))) fd = reply->fd;
439 if (type) *type = reply->type;
440 if (flags) *flags = reply->flags;
442 SERVER_END_REQ;
443 if (ret) return ret;
445 if (fd != -1) break;
447 /* it wasn't in the cache, get it from the server */
448 fd = receive_fd( &fd_handle );
449 /* and store it back into the cache */
450 ret = store_cached_fd( &fd, fd_handle );
451 if (ret) return ret;
453 if (fd_handle == handle) break;
454 /* if we received a different handle this means there was
455 * a race with another thread; we restart everything from
456 * scratch in this case.
460 if ((fd != -1) && ((fd = dup(fd)) == -1)) return STATUS_TOO_MANY_OPENED_FILES;
461 *unix_fd = fd;
462 return STATUS_SUCCESS;
466 /***********************************************************************
467 * wine_server_release_fd (NTDLL.@)
469 * Release the Unix fd returned by wine_server_handle_to_fd.
471 void wine_server_release_fd( obj_handle_t handle, int unix_fd )
473 close( unix_fd );
477 /***********************************************************************
478 * start_server
480 * Start a new wine server.
482 static void start_server( const char *oldcwd )
484 static int started; /* we only try once */
485 char *path, *p;
486 char *argv[3];
488 if (!started)
490 int status;
491 int pid = fork();
492 if (pid == -1) fatal_perror( "fork" );
493 if (!pid)
495 argv[0] = "wineserver";
496 argv[1] = TRACE_ON(server) ? "-d" : NULL;
497 argv[2] = NULL;
498 /* if server is explicitly specified, use this */
499 if ((p = getenv("WINESERVER")))
501 if (p[0] != '/' && oldcwd[0] == '/') /* make it an absolute path */
503 if (!(path = malloc( strlen(oldcwd) + strlen(p) + 1 )))
504 fatal_error( "out of memory\n" );
505 sprintf( path, "%s/%s", oldcwd, p );
506 p = path;
508 argv[0] = p;
509 execv( argv[0], argv );
510 fatal_perror( "could not exec the server '%s'\n"
511 " specified in the WINESERVER environment variable", p );
513 /* now use the standard search strategy */
514 wine_exec_wine_binary( argv[0], argv, NULL );
515 fatal_error( "could not exec wineserver\n" );
517 waitpid( pid, &status, 0 );
518 status = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
519 if (status == 2) return; /* server lock held by someone else, will retry later */
520 if (status) exit(status); /* server failed */
521 started = 1;
526 /***********************************************************************
527 * server_connect_error
529 * Try to display a meaningful explanation of why we couldn't connect
530 * to the server.
532 static void server_connect_error( const char *serverdir )
534 int fd;
535 struct flock fl;
537 if ((fd = open( LOCKNAME, O_WRONLY )) == -1)
538 fatal_error( "for some mysterious reason, the wine server never started.\n" );
540 fl.l_type = F_WRLCK;
541 fl.l_whence = SEEK_SET;
542 fl.l_start = 0;
543 fl.l_len = 1;
544 if (fcntl( fd, F_GETLK, &fl ) != -1)
546 if (fl.l_type == F_WRLCK) /* the file is locked */
547 fatal_error( "a wine server seems to be running, but I cannot connect to it.\n"
548 " You probably need to kill that process (it might be pid %d).\n",
549 (int)fl.l_pid );
550 fatal_error( "for some mysterious reason, the wine server failed to run.\n" );
552 fatal_error( "the file system of '%s' doesn't support locks,\n"
553 " and there is a 'socket' file in that directory that prevents wine from starting.\n"
554 " You should make sure no wine server is running, remove that file and try again.\n",
555 serverdir );
559 /***********************************************************************
560 * server_connect
562 * Attempt to connect to an existing server socket.
563 * We need to be in the server directory already.
565 static int server_connect( const char *oldcwd, const char *serverdir )
567 struct sockaddr_un addr;
568 struct stat st;
569 int s, slen, retry;
571 /* chdir to the server directory */
572 if (chdir( serverdir ) == -1)
574 if (errno != ENOENT) fatal_perror( "chdir to %s", serverdir );
575 start_server( "." );
576 if (chdir( serverdir ) == -1) fatal_perror( "chdir to %s", serverdir );
579 /* make sure we are at the right place */
580 if (stat( ".", &st ) == -1) fatal_perror( "stat %s", serverdir );
581 if (st.st_uid != getuid()) fatal_error( "'%s' is not owned by you\n", serverdir );
582 if (st.st_mode & 077) fatal_error( "'%s' must not be accessible by other users\n", serverdir );
584 for (retry = 0; retry < 6; retry++)
586 /* if not the first try, wait a bit to leave the previous server time to exit */
587 if (retry)
589 usleep( 100000 * retry * retry );
590 start_server( oldcwd );
591 if (lstat( SOCKETNAME, &st ) == -1) continue; /* still no socket, wait a bit more */
593 else if (lstat( SOCKETNAME, &st ) == -1) /* check for an already existing socket */
595 if (errno != ENOENT) fatal_perror( "lstat %s/%s", serverdir, SOCKETNAME );
596 start_server( oldcwd );
597 if (lstat( SOCKETNAME, &st ) == -1) continue; /* still no socket, wait a bit more */
600 /* make sure the socket is sane (ISFIFO needed for Solaris) */
601 if (!S_ISSOCK(st.st_mode) && !S_ISFIFO(st.st_mode))
602 fatal_error( "'%s/%s' is not a socket\n", serverdir, SOCKETNAME );
603 if (st.st_uid != getuid())
604 fatal_error( "'%s/%s' is not owned by you\n", serverdir, SOCKETNAME );
606 /* try to connect to it */
607 addr.sun_family = AF_UNIX;
608 strcpy( addr.sun_path, SOCKETNAME );
609 slen = sizeof(addr) - sizeof(addr.sun_path) + strlen(addr.sun_path) + 1;
610 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
611 addr.sun_len = slen;
612 #endif
613 if ((s = socket( AF_UNIX, SOCK_STREAM, 0 )) == -1) fatal_perror( "socket" );
614 if (connect( s, (struct sockaddr *)&addr, slen ) != -1)
616 fcntl( s, F_SETFD, 1 ); /* set close on exec flag */
617 return s;
619 close( s );
621 server_connect_error( serverdir );
625 /***********************************************************************
626 * server_init_process
628 * Start the server and create the initial socket pair.
630 void server_init_process(void)
632 int size;
633 char *oldcwd;
634 obj_handle_t dummy_handle;
636 /* retrieve the current directory */
637 for (size = 512; ; size *= 2)
639 if (!(oldcwd = malloc( size ))) break;
640 if (getcwd( oldcwd, size )) break;
641 free( oldcwd );
642 if (errno == ERANGE) continue;
643 oldcwd = NULL;
644 break;
647 /* connect to the server */
648 fd_socket = server_connect( oldcwd, wine_get_server_dir() );
650 /* switch back to the starting directory */
651 if (oldcwd)
653 chdir( oldcwd );
654 free( oldcwd );
657 /* setup the signal mask */
658 sigemptyset( &block_set );
659 sigaddset( &block_set, SIGALRM );
660 sigaddset( &block_set, SIGIO );
661 sigaddset( &block_set, SIGINT );
662 sigaddset( &block_set, SIGHUP );
663 sigaddset( &block_set, SIGUSR1 );
664 sigaddset( &block_set, SIGUSR2 );
666 /* receive the first thread request fd on the main socket */
667 NtCurrentTeb()->request_fd = receive_fd( &dummy_handle );
671 /***********************************************************************
672 * server_init_thread
674 * Send an init thread request. Return 0 if OK.
676 void server_init_thread( int unix_pid, int unix_tid, void *entry_point )
678 TEB *teb = NtCurrentTeb();
679 int version, ret;
680 int reply_pipe[2];
681 struct sigaction sig_act;
683 sig_act.sa_handler = SIG_IGN;
684 sig_act.sa_flags = 0;
685 sigemptyset( &sig_act.sa_mask );
687 /* ignore SIGPIPE so that we get a EPIPE error instead */
688 sigaction( SIGPIPE, &sig_act, NULL );
689 /* automatic child reaping to avoid zombies */
690 #ifdef SA_NOCLDWAIT
691 sig_act.sa_flags |= SA_NOCLDWAIT;
692 #endif
693 sigaction( SIGCHLD, &sig_act, NULL );
695 /* create the server->client communication pipes */
696 if (pipe( reply_pipe ) == -1) server_protocol_perror( "pipe" );
697 if (pipe( teb->wait_fd ) == -1) server_protocol_perror( "pipe" );
698 wine_server_send_fd( reply_pipe[1] );
699 wine_server_send_fd( teb->wait_fd[1] );
700 teb->reply_fd = reply_pipe[0];
701 close( reply_pipe[1] );
703 /* set close on exec flag */
704 fcntl( teb->reply_fd, F_SETFD, 1 );
705 fcntl( teb->wait_fd[0], F_SETFD, 1 );
706 fcntl( teb->wait_fd[1], F_SETFD, 1 );
708 SERVER_START_REQ( init_thread )
710 req->unix_pid = unix_pid;
711 req->unix_tid = unix_tid;
712 req->teb = teb;
713 req->entry = entry_point;
714 req->reply_fd = reply_pipe[1];
715 req->wait_fd = teb->wait_fd[1];
716 ret = wine_server_call( req );
717 teb->ClientId.UniqueProcess = (HANDLE)reply->pid;
718 teb->ClientId.UniqueThread = (HANDLE)reply->tid;
719 version = reply->version;
721 SERVER_END_REQ;
723 if (ret) server_protocol_error( "init_thread failed with status %x\n", ret );
724 if (version != SERVER_PROTOCOL_VERSION)
725 server_protocol_error( "version mismatch %d/%d.\n"
726 "Your %s binary was not upgraded correctly,\n"
727 "or you have an older one somewhere in your PATH.\n"
728 "Or maybe the wrong wineserver is still running?\n",
729 version, SERVER_PROTOCOL_VERSION,
730 (version > SERVER_PROTOCOL_VERSION) ? "wine" : "wineserver" );