In SHFileOperation() when requested to copy files, figure out when the
[wine/dcerpc.git] / scheduler / client.c
blob0aeb9f0a6155389e60134d40bb988f25d358e77b
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_AbortThread(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_AbortThread(1);
116 /***********************************************************************
117 * send_request
119 * Send a request to the server.
121 static void send_request( const struct __server_request_info *req )
123 int i, ret;
125 if (!req->u.req.request_header.request_size)
127 if ((ret = write( NtCurrentTeb()->request_fd, &req->u.req,
128 sizeof(req->u.req) )) == sizeof(req->u.req)) return;
131 else
133 struct iovec vec[__SERVER_MAX_DATA+1];
135 vec[0].iov_base = (void *)&req->u.req;
136 vec[0].iov_len = sizeof(req->u.req);
137 for (i = 0; i < req->data_count; i++)
139 vec[i+1].iov_base = (void *)req->data[i].ptr;
140 vec[i+1].iov_len = req->data[i].size;
142 if ((ret = writev( NtCurrentTeb()->request_fd, vec, i+1 )) ==
143 req->u.req.request_header.request_size + sizeof(req->u.req)) return;
146 if (ret >= 0) server_protocol_error( "partial write %d\n", ret );
147 if (errno == EPIPE) SYSDEPS_AbortThread(0);
148 server_protocol_perror( "sendmsg" );
152 /***********************************************************************
153 * read_reply_data
155 * Read data from the reply buffer; helper for wait_reply.
157 static void read_reply_data( void *buffer, size_t size )
159 int ret;
161 for (;;)
163 if ((ret = read( NtCurrentTeb()->reply_fd, buffer, size )) > 0)
165 if (!(size -= ret)) return;
166 buffer = (char *)buffer + ret;
167 continue;
169 if (!ret) break;
170 if (errno == EINTR) continue;
171 if (errno == EPIPE) break;
172 server_protocol_perror("read");
174 /* the server closed the connection; time to die... */
175 SYSDEPS_AbortThread(0);
179 /***********************************************************************
180 * wait_reply
182 * Wait for a reply from the server.
184 inline static void wait_reply( struct __server_request_info *req )
186 read_reply_data( &req->u.reply, sizeof(req->u.reply) );
187 if (req->u.reply.reply_header.reply_size)
188 read_reply_data( req->reply_data, req->u.reply.reply_header.reply_size );
192 /***********************************************************************
193 * wine_server_call (NTDLL.@)
195 * Perform a server call.
197 unsigned int wine_server_call( void *req_ptr )
199 struct __server_request_info * const req = req_ptr;
200 sigset_t old_set;
202 memset( (char *)&req->u.req + req->size, 0, sizeof(req->u.req) - req->size );
203 sigprocmask( SIG_BLOCK, &block_set, &old_set );
204 send_request( req );
205 wait_reply( req );
206 sigprocmask( SIG_SETMASK, &old_set, NULL );
207 return req->u.reply.reply_header.error;
211 /***********************************************************************
212 * wine_server_send_fd
214 * Send a file descriptor to the server.
216 void wine_server_send_fd( int fd )
218 #ifndef HAVE_MSGHDR_ACCRIGHTS
219 struct cmsg_fd cmsg;
220 #endif
221 struct send_fd data;
222 struct msghdr msghdr;
223 struct iovec vec;
224 int ret;
226 vec.iov_base = (void *)&data;
227 vec.iov_len = sizeof(data);
229 msghdr.msg_name = NULL;
230 msghdr.msg_namelen = 0;
231 msghdr.msg_iov = &vec;
232 msghdr.msg_iovlen = 1;
234 #ifdef HAVE_MSGHDR_ACCRIGHTS
235 msghdr.msg_accrights = (void *)&fd;
236 msghdr.msg_accrightslen = sizeof(fd);
237 #else /* HAVE_MSGHDR_ACCRIGHTS */
238 cmsg.len = sizeof(cmsg);
239 cmsg.level = SOL_SOCKET;
240 cmsg.type = SCM_RIGHTS;
241 cmsg.fd = fd;
242 msghdr.msg_control = &cmsg;
243 msghdr.msg_controllen = sizeof(cmsg);
244 msghdr.msg_flags = 0;
245 #endif /* HAVE_MSGHDR_ACCRIGHTS */
247 data.tid = (void *)GetCurrentThreadId();
248 data.fd = fd;
250 for (;;)
252 if ((ret = sendmsg( fd_socket, &msghdr, 0 )) == sizeof(data)) return;
253 if (ret >= 0) server_protocol_error( "partial write %d\n", ret );
254 if (errno == EINTR) continue;
255 if (errno == EPIPE) SYSDEPS_AbortThread(0);
256 server_protocol_perror( "sendmsg" );
261 /***********************************************************************
262 * receive_fd
264 * Receive a file descriptor passed from the server.
266 static int receive_fd( handle_t *handle )
268 struct iovec vec;
269 int ret, fd;
271 #ifdef HAVE_MSGHDR_ACCRIGHTS
272 struct msghdr msghdr;
274 fd = -1;
275 msghdr.msg_accrights = (void *)&fd;
276 msghdr.msg_accrightslen = sizeof(fd);
277 #else /* HAVE_MSGHDR_ACCRIGHTS */
278 struct msghdr msghdr;
279 struct cmsg_fd cmsg;
281 cmsg.len = sizeof(cmsg);
282 cmsg.level = SOL_SOCKET;
283 cmsg.type = SCM_RIGHTS;
284 cmsg.fd = -1;
285 msghdr.msg_control = &cmsg;
286 msghdr.msg_controllen = sizeof(cmsg);
287 msghdr.msg_flags = 0;
288 #endif /* HAVE_MSGHDR_ACCRIGHTS */
290 msghdr.msg_name = NULL;
291 msghdr.msg_namelen = 0;
292 msghdr.msg_iov = &vec;
293 msghdr.msg_iovlen = 1;
294 vec.iov_base = (void *)handle;
295 vec.iov_len = sizeof(*handle);
297 for (;;)
299 if ((ret = recvmsg( fd_socket, &msghdr, 0 )) > 0)
301 #ifndef HAVE_MSGHDR_ACCRIGHTS
302 fd = cmsg.fd;
303 #endif
304 if (fd == -1) server_protocol_error( "no fd received for handle %d\n", *handle );
305 fcntl( fd, F_SETFD, 1 ); /* set close on exec flag */
306 return fd;
308 if (!ret) break;
309 if (errno == EINTR) continue;
310 if (errno == EPIPE) break;
311 server_protocol_perror("recvmsg");
313 /* the server closed the connection; time to die... */
314 SYSDEPS_AbortThread(0);
318 /***********************************************************************
319 * wine_server_recv_fd
321 * Receive a file descriptor passed from the server.
322 * The file descriptor must not be closed.
323 * Return -2 if a race condition stole our file descriptor.
325 int wine_server_recv_fd( handle_t handle )
327 handle_t fd_handle;
329 int fd = receive_fd( &fd_handle );
331 /* now store it in the server fd cache for this handle */
333 SERVER_START_REQ( set_handle_info )
335 req->handle = fd_handle;
336 req->flags = 0;
337 req->mask = 0;
338 req->fd = fd;
339 if (!wine_server_call( req ))
341 if (reply->cur_fd != fd)
343 /* someone was here before us */
344 close( fd );
345 fd = reply->cur_fd;
348 else
350 close( fd );
351 fd = -1;
354 SERVER_END_REQ;
356 if (handle != fd_handle) fd = -2; /* not the one we expected */
357 return fd;
361 /***********************************************************************
362 * get_config_dir
364 * Return the configuration directory ($WINEPREFIX or $HOME/.wine)
366 const char *get_config_dir(void)
368 static char *confdir;
369 if (!confdir)
371 const char *prefix = getenv( "WINEPREFIX" );
372 if (prefix)
374 int len = strlen(prefix);
375 if (!(confdir = strdup( prefix ))) fatal_error( "out of memory\n" );
376 if (len > 1 && confdir[len-1] == '/') confdir[len-1] = 0;
378 else
380 const char *home = getenv( "HOME" );
381 if (!home)
383 struct passwd *pwd = getpwuid( getuid() );
384 if (!pwd) fatal_error( "could not find your home directory\n" );
385 home = pwd->pw_dir;
387 if (!(confdir = malloc( strlen(home) + strlen(CONFDIR) + 1 )))
388 fatal_error( "out of memory\n" );
389 strcpy( confdir, home );
390 strcat( confdir, CONFDIR );
393 return confdir;
397 /***********************************************************************
398 * start_server
400 * Start a new wine server.
402 static void start_server( const char *oldcwd )
404 static int started; /* we only try once */
405 char *path, *p;
406 if (!started)
408 int status;
409 int pid = fork();
410 if (pid == -1) fatal_perror( "fork" );
411 if (!pid)
413 /* if server is explicitly specified, use this */
414 if ((p = getenv("WINESERVER")))
416 if (p[0] != '/' && oldcwd[0] == '/') /* make it an absolute path */
418 if (!(path = malloc( strlen(oldcwd) + strlen(p) + 1 )))
419 fatal_error( "out of memory\n" );
420 sprintf( path, "%s/%s", oldcwd, p );
421 p = path;
423 execl( p, "wineserver", NULL );
424 fatal_perror( "could not exec the server '%s'\n"
425 " specified in the WINESERVER environment variable", p );
428 /* first try the installation dir */
429 execl( BINDIR "/wineserver", "wineserver", NULL );
431 /* now try the dir we were launched from */
432 if (full_argv0)
434 if (!(path = malloc( strlen(full_argv0) + 20 )))
435 fatal_error( "out of memory\n" );
436 if ((p = strrchr( strcpy( path, full_argv0 ), '/' )))
438 strcpy( p, "/wineserver" );
439 execl( path, "wineserver", NULL );
440 strcpy( p, "/server/wineserver" );
441 execl( path, "wineserver", NULL );
443 free(path);
446 /* now try the path */
447 execlp( "wineserver", "wineserver", NULL );
449 /* and finally the current dir */
450 if (!(path = malloc( strlen(oldcwd) + 20 )))
451 fatal_error( "out of memory\n" );
452 p = strcpy( path, oldcwd ) + strlen( oldcwd );
453 strcpy( p, "/wineserver" );
454 execl( path, "wineserver", NULL );
455 strcpy( p, "/server/wineserver" );
456 execl( path, "wineserver", NULL );
457 free(path);
458 fatal_error( "could not exec wineserver\n" );
460 started = 1;
461 waitpid( pid, &status, 0 );
462 status = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
463 if (status) exit(status); /* server failed */
467 /***********************************************************************
468 * server_connect
470 * Attempt to connect to an existing server socket.
471 * We need to be in the server directory already.
473 static int server_connect( const char *oldcwd, const char *serverdir )
475 struct sockaddr_un addr;
476 struct stat st;
477 int s, slen, retry;
479 /* chdir to the server directory */
480 if (chdir( serverdir ) == -1)
482 if (errno != ENOENT) fatal_perror( "chdir to %s", serverdir );
483 start_server( "." );
484 if (chdir( serverdir ) == -1) fatal_perror( "chdir to %s", serverdir );
487 /* make sure we are at the right place */
488 if (stat( ".", &st ) == -1) fatal_perror( "stat %s", serverdir );
489 if (st.st_uid != getuid()) fatal_error( "'%s' is not owned by you\n", serverdir );
490 if (st.st_mode & 077) fatal_error( "'%s' must not be accessible by other users\n", serverdir );
492 for (retry = 0; retry < 3; retry++)
494 /* if not the first try, wait a bit to leave the server time to exit */
495 if (retry) usleep( 100000 * retry * retry );
497 /* check for an existing socket */
498 if (lstat( SOCKETNAME, &st ) == -1)
500 if (errno != ENOENT) fatal_perror( "lstat %s/%s", serverdir, SOCKETNAME );
501 start_server( oldcwd );
502 if (lstat( SOCKETNAME, &st ) == -1) fatal_perror( "lstat %s/%s", serverdir, SOCKETNAME );
505 /* make sure the socket is sane (ISFIFO needed for Solaris) */
506 if (!S_ISSOCK(st.st_mode) && !S_ISFIFO(st.st_mode))
507 fatal_error( "'%s/%s' is not a socket\n", serverdir, SOCKETNAME );
508 if (st.st_uid != getuid())
509 fatal_error( "'%s/%s' is not owned by you\n", serverdir, SOCKETNAME );
511 /* try to connect to it */
512 addr.sun_family = AF_UNIX;
513 strcpy( addr.sun_path, SOCKETNAME );
514 slen = sizeof(addr) - sizeof(addr.sun_path) + strlen(addr.sun_path) + 1;
515 #ifdef HAVE_SOCKADDR_SUN_LEN
516 addr.sun_len = slen;
517 #endif
518 if ((s = socket( AF_UNIX, SOCK_STREAM, 0 )) == -1) fatal_perror( "socket" );
519 if (connect( s, (struct sockaddr *)&addr, slen ) != -1)
521 fcntl( s, F_SETFD, 1 ); /* set close on exec flag */
522 return s;
524 close( s );
526 fatal_error( "file '%s/%s' exists,\n"
527 " but I cannot connect to it; maybe the wineserver has crashed?\n"
528 " If this is the case, you should remove this socket file and try again.\n",
529 serverdir, SOCKETNAME );
533 /***********************************************************************
534 * CLIENT_InitServer
536 * Start the server and create the initial socket pair.
538 void CLIENT_InitServer(void)
540 int size;
541 char hostname[64];
542 char *oldcwd, *serverdir;
543 const char *configdir;
544 handle_t dummy_handle;
546 /* retrieve the current directory */
547 for (size = 512; ; size *= 2)
549 if (!(oldcwd = malloc( size ))) break;
550 if (getcwd( oldcwd, size )) break;
551 free( oldcwd );
552 if (errno == ERANGE) continue;
553 oldcwd = NULL;
554 break;
557 /* if argv[0] is a relative path, make it absolute */
558 full_argv0 = argv0;
559 if (oldcwd && argv0[0] != '/' && strchr( argv0, '/' ))
561 char *new_argv0 = malloc( strlen(oldcwd) + strlen(argv0) + 2 );
562 if (new_argv0)
564 strcpy( new_argv0, oldcwd );
565 strcat( new_argv0, "/" );
566 strcat( new_argv0, argv0 );
567 full_argv0 = new_argv0;
571 /* get the server directory name */
572 if (gethostname( hostname, sizeof(hostname) ) == -1) fatal_perror( "gethostname" );
573 configdir = get_config_dir();
574 serverdir = malloc( strlen(configdir) + strlen(SERVERDIR) + strlen(hostname) + 1 );
575 if (!serverdir) fatal_error( "out of memory\n" );
576 strcpy( serverdir, configdir );
577 strcat( serverdir, SERVERDIR );
578 strcat( serverdir, hostname );
580 /* connect to the server */
581 fd_socket = server_connect( oldcwd, serverdir );
583 /* switch back to the starting directory */
584 if (oldcwd)
586 chdir( oldcwd );
587 free( oldcwd );
590 /* setup the signal mask */
591 sigemptyset( &block_set );
592 sigaddset( &block_set, SIGALRM );
593 sigaddset( &block_set, SIGIO );
594 sigaddset( &block_set, SIGINT );
595 sigaddset( &block_set, SIGHUP );
597 /* receive the first thread request fd on the main socket */
598 NtCurrentTeb()->request_fd = receive_fd( &dummy_handle );
600 CLIENT_InitThread();
604 /***********************************************************************
605 * CLIENT_InitThread
607 * Send an init thread request. Return 0 if OK.
609 void CLIENT_InitThread(void)
611 TEB *teb = NtCurrentTeb();
612 int version, ret;
613 int reply_pipe[2];
615 /* ignore SIGPIPE so that we get a EPIPE error instead */
616 signal( SIGPIPE, SIG_IGN );
618 /* create the server->client communication pipes */
619 if (pipe( reply_pipe ) == -1) server_protocol_perror( "pipe" );
620 if (pipe( teb->wait_fd ) == -1) server_protocol_perror( "pipe" );
621 wine_server_send_fd( reply_pipe[1] );
622 wine_server_send_fd( teb->wait_fd[1] );
623 teb->reply_fd = reply_pipe[0];
625 /* set close on exec flag */
626 fcntl( teb->reply_fd, F_SETFD, 1 );
627 fcntl( teb->wait_fd[0], F_SETFD, 1 );
628 fcntl( teb->wait_fd[1], F_SETFD, 1 );
630 SERVER_START_REQ( init_thread )
632 req->unix_pid = getpid();
633 req->teb = teb;
634 req->entry = teb->entry_point;
635 req->reply_fd = reply_pipe[1];
636 req->wait_fd = teb->wait_fd[1];
637 ret = wine_server_call( req );
638 teb->pid = reply->pid;
639 teb->tid = reply->tid;
640 version = reply->version;
641 if (reply->boot) boot_thread_id = teb->tid;
642 else if (boot_thread_id == teb->tid) boot_thread_id = 0;
643 close( reply_pipe[1] );
645 SERVER_END_REQ;
647 if (ret) server_protocol_error( "init_thread failed with status %x\n", ret );
648 if (version != SERVER_PROTOCOL_VERSION)
649 server_protocol_error( "version mismatch %d/%d.\n"
650 "Your %s binary was not upgraded correctly,\n"
651 "or you have an older one somewhere in your PATH.\n"
652 "Or maybe the wrong wineserver is still running?\n",
653 version, SERVER_PROTOCOL_VERSION,
654 (version > SERVER_PROTOCOL_VERSION) ? "wine" : "wineserver" );
658 /***********************************************************************
659 * CLIENT_BootDone
661 * Signal that we have finished booting, and set debug level.
663 void CLIENT_BootDone( int debug_level )
665 SERVER_START_REQ( boot_done )
667 req->debug_level = debug_level;
668 wine_server_call( req );
670 SERVER_END_REQ;
674 /***********************************************************************
675 * CLIENT_IsBootThread
677 * Return TRUE if current thread is the boot thread.
679 int CLIENT_IsBootThread(void)
681 return (GetCurrentThreadId() == (DWORD)boot_thread_id);