Add shell support for deleting files using the Delete key.
[wine/gsoc_dplay.git] / scheduler / client.c
blob1c022986d6e56dce610797edba43a671e350995c
1 /*
2 * Client part of the client/server communication
4 * Copyright (C) 1998 Alexandre Julliard
5 */
7 #include "config.h"
9 #include <assert.h>
10 #include <ctype.h>
11 #include <errno.h>
12 #include <fcntl.h>
13 #include <pwd.h>
14 #include <signal.h>
15 #include <stdio.h>
16 #include <string.h>
17 #include <sys/types.h>
18 #ifdef HAVE_SYS_SOCKET_H
19 # include <sys/socket.h>
20 #endif
21 #ifdef HAVE_SYS_WAIT_H
22 #include <sys/wait.h>
23 #endif
24 #include <sys/un.h>
25 #ifdef HAVE_SYS_MMAN_H
26 #include <sys/mman.h>
27 #endif
28 #include <sys/stat.h>
29 #include <sys/uio.h>
30 #include <unistd.h>
31 #include <stdarg.h>
33 #include "wine/port.h"
34 #include "thread.h"
35 #include "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 #undef server_alloc_req
50 /* data structure used to pass an fd with sendmsg/recvmsg */
51 struct cmsg_fd
53 int len; /* sizeof structure */
54 int level; /* SOL_SOCKET */
55 int type; /* SCM_RIGHTS */
56 int fd; /* fd to pass */
59 static void *boot_thread_id;
62 /* die on a fatal error; use only during initialization */
63 static void fatal_error( const char *err, ... ) WINE_NORETURN;
64 static void fatal_error( const char *err, ... )
66 va_list args;
68 va_start( args, err );
69 fprintf( stderr, "wine: " );
70 vfprintf( stderr, err, args );
71 va_end( args );
72 exit(1);
75 /* die on a fatal error; use only during initialization */
76 static void fatal_perror( const char *err, ... ) WINE_NORETURN;
77 static void fatal_perror( const char *err, ... )
79 va_list args;
81 va_start( args, err );
82 fprintf( stderr, "wine: " );
83 vfprintf( stderr, err, args );
84 perror( " " );
85 va_end( args );
86 exit(1);
89 /***********************************************************************
90 * server_protocol_error
92 void server_protocol_error( const char *err, ... )
94 va_list args;
96 va_start( args, err );
97 fprintf( stderr, "Client protocol error:%p: ", NtCurrentTeb()->tid );
98 vfprintf( stderr, err, args );
99 va_end( args );
100 SYSDEPS_ExitThread(1);
104 /***********************************************************************
105 * server_protocol_perror
107 void server_protocol_perror( const char *err )
109 fprintf( stderr, "Client protocol error:%p: ", NtCurrentTeb()->tid );
110 perror( err );
111 SYSDEPS_ExitThread(1);
115 /***********************************************************************
116 * __wine_server_exception_handler (NTDLL.@)
118 DWORD __wine_server_exception_handler( PEXCEPTION_RECORD record, EXCEPTION_FRAME *frame,
119 CONTEXT *context, EXCEPTION_FRAME **pdispatcher )
121 struct __server_exception_frame *server_frame = (struct __server_exception_frame *)frame;
122 if ((record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
123 *NtCurrentTeb()->buffer_info = server_frame->info;
124 return ExceptionContinueSearch;
128 /***********************************************************************
129 * wine_server_alloc_req (NTDLL.@)
131 void *wine_server_alloc_req( size_t fixed_size, size_t var_size )
133 unsigned int pos = NtCurrentTeb()->buffer_info->cur_pos;
134 union generic_request *req = (union generic_request *)((char *)NtCurrentTeb()->buffer + pos);
135 size_t size = sizeof(*req) + var_size;
137 assert( fixed_size <= sizeof(*req) );
138 assert( var_size <= REQUEST_MAX_VAR_SIZE );
140 if ((char *)req + size > (char *)NtCurrentTeb()->buffer_info)
141 server_protocol_error( "buffer overflow %d bytes\n",
142 (char *)req + size - (char *)NtCurrentTeb()->buffer_info );
143 NtCurrentTeb()->buffer_info->cur_pos = pos + size;
144 req->header.var_offset = pos + sizeof(*req);
145 req->header.var_size = var_size;
146 return req;
150 /***********************************************************************
151 * send_request
153 * Send a request to the server.
155 static void send_request( enum request req, union generic_request *request )
157 request->header.req = req;
158 NtCurrentTeb()->buffer_info->cur_req = (char *)request - (char *)NtCurrentTeb()->buffer;
159 /* write a single byte; the value is ignored anyway */
160 if (write( NtCurrentTeb()->request_fd, request, 1 ) == -1)
162 if (errno == EPIPE) SYSDEPS_ExitThread(0);
163 server_protocol_perror( "sendmsg" );
167 /***********************************************************************
168 * send_request_fd
170 * Send a request to the server, passing a file descriptor.
172 static void send_request_fd( enum request req, union generic_request *request, int fd )
174 #ifndef HAVE_MSGHDR_ACCRIGHTS
175 struct cmsg_fd cmsg;
176 #endif
177 struct msghdr msghdr;
178 struct iovec vec;
180 /* write a single byte; the value is ignored anyway */
181 vec.iov_base = (void *)request;
182 vec.iov_len = 1;
184 msghdr.msg_name = NULL;
185 msghdr.msg_namelen = 0;
186 msghdr.msg_iov = &vec;
187 msghdr.msg_iovlen = 1;
189 #ifdef HAVE_MSGHDR_ACCRIGHTS
190 msghdr.msg_accrights = (void *)&fd;
191 msghdr.msg_accrightslen = sizeof(fd);
192 #else /* HAVE_MSGHDR_ACCRIGHTS */
193 cmsg.len = sizeof(cmsg);
194 cmsg.level = SOL_SOCKET;
195 cmsg.type = SCM_RIGHTS;
196 cmsg.fd = fd;
197 msghdr.msg_control = &cmsg;
198 msghdr.msg_controllen = sizeof(cmsg);
199 msghdr.msg_flags = 0;
200 #endif /* HAVE_MSGHDR_ACCRIGHTS */
202 request->header.req = req;
204 if (sendmsg( NtCurrentTeb()->socket, &msghdr, 0 ) == -1)
206 if (errno == EPIPE) SYSDEPS_ExitThread(0);
207 server_protocol_perror( "sendmsg" );
211 /***********************************************************************
212 * wait_reply
214 * Wait for a reply from the server.
216 static void wait_reply(void)
218 int ret;
219 char dummy[1];
221 for (;;)
223 if ((ret = read( NtCurrentTeb()->reply_fd, dummy, 1 )) > 0) return;
224 if (!ret) break;
225 if (errno == EINTR) continue;
226 if (errno == EPIPE) break;
227 server_protocol_perror("read");
229 /* the server closed the connection; time to die... */
230 SYSDEPS_ExitThread(0);
234 /***********************************************************************
235 * wine_server_call (NTDLL.@)
237 * Perform a server call.
239 unsigned int wine_server_call( enum request req )
241 union generic_request *req_ptr = NtCurrentTeb()->buffer;
242 send_request( req, req_ptr );
243 wait_reply();
244 return req_ptr->header.error;
248 /***********************************************************************
249 * server_call_fd
251 * Perform a server call, passing a file descriptor.
253 unsigned int server_call_fd( enum request req, int fd_out )
255 unsigned int res;
256 union generic_request *req_ptr = NtCurrentTeb()->buffer;
258 send_request_fd( req, req_ptr, fd_out );
259 wait_reply();
261 if ((res = req_ptr->header.error)) SetLastError( RtlNtStatusToDosError(res) );
262 return res; /* error code */
266 /***********************************************************************
267 * set_handle_fd
269 * Store the fd for a given handle in the server
271 static int set_handle_fd( int handle, int fd )
273 SERVER_START_REQ
275 struct set_handle_info_request *req = wine_server_alloc_req( sizeof(*req), 0 );
276 req->handle = handle;
277 req->flags = 0;
278 req->mask = 0;
279 req->fd = fd;
280 if (!server_call( REQ_SET_HANDLE_INFO ))
282 if (req->cur_fd != fd)
284 /* someone was here before us */
285 close( fd );
286 fd = req->cur_fd;
288 else fcntl( fd, F_SETFD, 1 ); /* set close on exec flag */
290 else
292 close( fd );
293 fd = -1;
296 SERVER_END_REQ;
297 return fd;
301 /***********************************************************************
302 * wine_server_recv_fd
304 * Receive a file descriptor passed from the server.
305 * The file descriptor must be closed after use.
307 int wine_server_recv_fd( int handle, int cache )
309 struct iovec vec;
310 int ret, fd, server_handle;
312 #ifdef HAVE_MSGHDR_ACCRIGHTS
313 struct msghdr msghdr;
315 fd = -1;
316 msghdr.msg_accrights = (void *)&fd;
317 msghdr.msg_accrightslen = sizeof(fd);
318 #else /* HAVE_MSGHDR_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_MSGHDR_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 *)&server_handle;
336 vec.iov_len = sizeof(server_handle);
338 for (;;)
340 if ((ret = recvmsg( NtCurrentTeb()->socket, &msghdr, 0 )) > 0)
342 #ifndef HAVE_MSGHDR_ACCRIGHTS
343 fd = cmsg.fd;
344 #endif
345 if (handle != server_handle)
346 server_protocol_error( "recv_fd: got handle %d, expected %d\n",
347 server_handle, handle );
348 if (cache)
350 fd = set_handle_fd( handle, fd );
351 if (fd != -1) fd = dup(fd);
353 return fd;
355 if (!ret) break;
356 if (errno == EINTR) continue;
357 if (errno == EPIPE) break;
358 server_protocol_perror("recvmsg");
360 /* the server closed the connection; time to die... */
361 SYSDEPS_ExitThread(0);
365 /***********************************************************************
366 * get_config_dir
368 * Return the configuration directory ($WINEPREFIX or $HOME/.wine)
370 const char *get_config_dir(void)
372 static char *confdir;
373 if (!confdir)
375 const char *prefix = getenv( "WINEPREFIX" );
376 if (prefix)
378 int len = strlen(prefix);
379 if (!(confdir = strdup( prefix ))) fatal_error( "out of memory\n" );
380 if (len > 1 && confdir[len-1] == '/') confdir[len-1] = 0;
382 else
384 const char *home = getenv( "HOME" );
385 if (!home)
387 struct passwd *pwd = getpwuid( getuid() );
388 if (!pwd) fatal_error( "could not find your home directory\n" );
389 home = pwd->pw_dir;
391 if (!(confdir = malloc( strlen(home) + strlen(CONFDIR) + 1 )))
392 fatal_error( "out of memory\n" );
393 strcpy( confdir, home );
394 strcat( confdir, CONFDIR );
396 mkdir( confdir, 0755 ); /* just in case */
398 return confdir;
402 /***********************************************************************
403 * start_server
405 * Start a new wine server.
407 static void start_server( const char *oldcwd )
409 static int started; /* we only try once */
410 char *path, *p;
411 if (!started)
413 int status;
414 int pid = fork();
415 if (pid == -1) fatal_perror( "fork" );
416 if (!pid)
418 /* if server is explicitly specified, use this */
419 if ((p = getenv("WINESERVER")))
421 execl( p, "wineserver", NULL );
422 fatal_perror( "could not exec the server '%s'\n"
423 " specified in the WINESERVER environment variable", p );
426 /* first try the installation dir */
427 execl( BINDIR "/wineserver", "wineserver", NULL );
429 /* now try the dir we were launched from */
430 if (full_argv0)
432 if (!(path = malloc( strlen(full_argv0) + 20 )))
433 fatal_error( "out of memory\n" );
434 if ((p = strrchr( strcpy( path, full_argv0 ), '/' )))
436 strcpy( p, "/wineserver" );
437 execl( path, "wineserver", NULL );
438 strcpy( p, "/server/wineserver" );
439 execl( path, "wineserver", NULL );
441 free(path);
444 /* now try the path */
445 execlp( "wineserver", "wineserver", NULL );
447 /* and finally the current dir */
448 if (!(path = malloc( strlen(oldcwd) + 20 )))
449 fatal_error( "out of memory\n" );
450 p = strcpy( path, oldcwd ) + strlen( oldcwd );
451 strcpy( p, "/wineserver" );
452 execl( path, "wineserver", NULL );
453 strcpy( p, "/server/wineserver" );
454 execl( path, "wineserver", NULL );
455 free(path);
456 fatal_error( "could not exec wineserver\n" );
458 started = 1;
459 waitpid( pid, &status, 0 );
460 status = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
461 if (status) exit(status); /* server failed */
465 /***********************************************************************
466 * server_connect
468 * Attempt to connect to an existing server socket.
469 * We need to be in the server directory already.
471 static int server_connect( const char *oldcwd, const char *serverdir )
473 struct sockaddr_un addr;
474 struct stat st;
475 int s, slen, retry;
477 /* chdir to the server directory */
478 if (chdir( serverdir ) == -1)
480 if (errno != ENOENT) fatal_perror( "chdir to %s", serverdir );
481 start_server( "." );
482 if (chdir( serverdir ) == -1) fatal_perror( "chdir to %s", serverdir );
485 /* make sure we are at the right place */
486 if (stat( ".", &st ) == -1) fatal_perror( "stat %s", serverdir );
487 if (st.st_uid != getuid()) fatal_error( "'%s' is not owned by you\n", serverdir );
488 if (st.st_mode & 077) fatal_error( "'%s' must not be accessible by other users\n", serverdir );
490 for (retry = 0; retry < 3; retry++)
492 /* if not the first try, wait a bit to leave the server time to exit */
493 if (retry) usleep( 100000 * retry * retry );
495 /* check for an existing socket */
496 if (lstat( SOCKETNAME, &st ) == -1)
498 if (errno != ENOENT) fatal_perror( "lstat %s/%s", serverdir, SOCKETNAME );
499 start_server( oldcwd );
500 if (lstat( SOCKETNAME, &st ) == -1) fatal_perror( "lstat %s/%s", serverdir, SOCKETNAME );
503 /* make sure the socket is sane (ISFIFO needed for Solaris) */
504 if (!S_ISSOCK(st.st_mode) && !S_ISFIFO(st.st_mode))
505 fatal_error( "'%s/%s' is not a socket\n", serverdir, SOCKETNAME );
506 if (st.st_uid != getuid())
507 fatal_error( "'%s/%s' is not owned by you\n", serverdir, SOCKETNAME );
509 /* try to connect to it */
510 addr.sun_family = AF_UNIX;
511 strcpy( addr.sun_path, SOCKETNAME );
512 slen = sizeof(addr) - sizeof(addr.sun_path) + strlen(addr.sun_path) + 1;
513 #ifdef HAVE_SOCKADDR_SUN_LEN
514 addr.sun_len = slen;
515 #endif
516 if ((s = socket( AF_UNIX, SOCK_STREAM, 0 )) == -1) fatal_perror( "socket" );
517 if (connect( s, (struct sockaddr *)&addr, slen ) != -1)
519 fcntl( s, F_SETFD, 1 ); /* set close on exec flag */
520 return s;
522 close( s );
524 fatal_error( "file '%s/%s' exists,\n"
525 " but I cannot connect to it; maybe the wineserver has crashed?\n"
526 " If this is the case, you should remove this socket file and try again.\n",
527 serverdir, SOCKETNAME );
531 /***********************************************************************
532 * CLIENT_InitServer
534 * Start the server and create the initial socket pair.
536 int CLIENT_InitServer(void)
538 int fd, size;
539 char hostname[64];
540 char *oldcwd, *serverdir;
541 const char *configdir;
543 /* retrieve the current directory */
544 for (size = 512; ; size *= 2)
546 if (!(oldcwd = malloc( size ))) break;
547 if (getcwd( oldcwd, size )) break;
548 free( oldcwd );
549 if (errno == ERANGE) continue;
550 oldcwd = NULL;
551 break;
554 /* if argv[0] is a relative path, make it absolute */
555 full_argv0 = argv0;
556 if (oldcwd && argv0[0] != '/' && strchr( argv0, '/' ))
558 char *new_argv0 = malloc( strlen(oldcwd) + strlen(argv0) + 2 );
559 if (new_argv0)
561 strcpy( new_argv0, oldcwd );
562 strcat( new_argv0, "/" );
563 strcat( new_argv0, argv0 );
564 full_argv0 = new_argv0;
568 /* get the server directory name */
569 if (gethostname( hostname, sizeof(hostname) ) == -1) fatal_perror( "gethostname" );
570 configdir = get_config_dir();
571 serverdir = malloc( strlen(configdir) + strlen(SERVERDIR) + strlen(hostname) + 1 );
572 if (!serverdir) fatal_error( "out of memory\n" );
573 strcpy( serverdir, configdir );
574 strcat( serverdir, SERVERDIR );
575 strcat( serverdir, hostname );
577 /* connect to the server */
578 fd = server_connect( oldcwd, serverdir );
580 /* switch back to the starting directory */
581 if (oldcwd)
583 chdir( oldcwd );
584 free( oldcwd );
586 return fd;
590 /***********************************************************************
591 * CLIENT_InitThread
593 * Send an init thread request. Return 0 if OK.
595 int CLIENT_InitThread(void)
597 struct get_thread_buffer_request *req;
598 TEB *teb = NtCurrentTeb();
599 int fd, ret, size;
601 /* ignore SIGPIPE so that we get a EPIPE error instead */
602 signal( SIGPIPE, SIG_IGN );
604 teb->request_fd = wine_server_recv_fd( 0, 0 );
605 if (teb->request_fd == -1) server_protocol_error( "no request fd passed on first request\n" );
606 fcntl( teb->request_fd, F_SETFD, 1 ); /* set close on exec flag */
608 teb->reply_fd = wine_server_recv_fd( 0, 0 );
609 if (teb->reply_fd == -1) server_protocol_error( "no reply fd passed on first request\n" );
610 fcntl( teb->reply_fd, F_SETFD, 1 ); /* set close on exec flag */
612 fd = wine_server_recv_fd( 0, 0 );
613 if (fd == -1) server_protocol_error( "no fd received for thread buffer\n" );
615 if ((size = lseek( fd, 0, SEEK_END )) == -1) server_protocol_perror( "lseek" );
616 teb->buffer = mmap( 0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 );
617 close( fd );
618 if (teb->buffer == (void*)-1) server_protocol_perror( "mmap" );
619 teb->buffer_info = (struct server_buffer_info *)((char *)teb->buffer + size) - 1;
621 wait_reply();
623 req = (struct get_thread_buffer_request *)teb->buffer;
624 teb->pid = req->pid;
625 teb->tid = req->tid;
626 if (req->version != SERVER_PROTOCOL_VERSION)
627 server_protocol_error( "version mismatch %d/%d.\n"
628 "Your %s binary was not upgraded correctly,\n"
629 "or you have an older one somewhere in your PATH.\nOr maybe wrong wineserver still running ?",
630 req->version, SERVER_PROTOCOL_VERSION,
631 (req->version > SERVER_PROTOCOL_VERSION) ? "wine" : "wineserver" );
632 if (req->boot) boot_thread_id = teb->tid;
633 else if (boot_thread_id == teb->tid) boot_thread_id = 0;
635 SERVER_START_REQ
637 struct init_thread_request *req = wine_server_alloc_req( sizeof(*req), 0 );
638 req->unix_pid = getpid();
639 req->teb = teb;
640 req->entry = teb->entry_point;
641 ret = wine_server_call( REQ_INIT_THREAD );
643 SERVER_END_REQ;
644 return ret;
648 /***********************************************************************
649 * CLIENT_BootDone
651 * Signal that we have finished booting, and set debug level.
653 int CLIENT_BootDone( int debug_level )
655 int ret;
656 SERVER_START_REQ
658 struct boot_done_request *req = wine_server_alloc_req( sizeof(*req), 0 );
659 req->debug_level = debug_level;
660 ret = server_call( REQ_BOOT_DONE );
662 SERVER_END_REQ;
663 return ret;
667 /***********************************************************************
668 * CLIENT_IsBootThread
670 * Return TRUE if current thread is the boot thread.
672 int CLIENT_IsBootThread(void)
674 return (GetCurrentThreadId() == (DWORD)boot_thread_id);