No longer directly accessing debuggee memory.
[wine] / scheduler / client.c
1 /*
2  * Client part of the client/server communication
3  *
4  * Copyright (C) 1998 Alexandre Julliard
5  */
6
7 #include "config.h"
8
9 #include <assert.h>
10 #include <ctype.h>
11 #include <errno.h>
12 #include <fcntl.h>
13 #include <stdio.h>
14 #include <string.h>
15 #include <sys/types.h>
16 #ifdef HAVE_SYS_SOCKET_H
17 # include <sys/socket.h>
18 #endif
19 #include <sys/un.h>
20 #ifdef HAVE_SYS_MMAN_H
21 #include <sys/mman.h>
22 #endif
23 #include <sys/stat.h>
24 #include <sys/uio.h>
25 #include <unistd.h>
26 #include <stdarg.h>
27
28 #include "process.h"
29 #include "thread.h"
30 #include "server.h"
31 #include "winerror.h"
32 #include "options.h"
33 #include "xmalloc.h"
34
35 /* Some versions of glibc don't define this */
36 #ifndef SCM_RIGHTS
37 #define SCM_RIGHTS 1
38 #endif
39
40 #define SERVERDIR  "/wineserver-"  /* server socket directory (hostname appended) */
41 #define SOCKETNAME "socket"        /* name of the socket file */
42
43 /* data structure used to pass an fd with sendmsg/recvmsg */
44 struct cmsg_fd
45 {
46     int len;   /* sizeof structure */
47     int level; /* SOL_SOCKET */
48     int type;  /* SCM_RIGHTS */
49     int fd;    /* fd to pass */
50 };
51
52 static void *boot_thread_id;
53
54
55 /* die on a fatal error; use only during initialization */
56 static void fatal_error( const char *err, ... )
57 {
58     va_list args;
59
60     va_start( args, err );
61     fprintf( stderr, "wine: " );
62     vfprintf( stderr, err, args );
63     va_end( args );
64     exit(1);
65 }
66
67 /* die on a fatal error; use only during initialization */
68 static void fatal_perror( const char *err, ... )
69 {
70     va_list args;
71
72     va_start( args, err );
73     fprintf( stderr, "wine: " );
74     vfprintf( stderr, err, args );
75     perror( " " );
76     va_end( args );
77     exit(1);
78 }
79
80 /***********************************************************************
81  *           CLIENT_Die
82  *
83  * Die on protocol errors or socket close
84  */
85 static void CLIENT_Die(void)
86 {
87     close( NtCurrentTeb()->socket );
88     SYSDEPS_ExitThread();
89 }
90
91 /***********************************************************************
92  *           server_protocol_error
93  */
94 void server_protocol_error( const char *err, ... )
95 {
96     va_list args;
97
98     va_start( args, err );
99     fprintf( stderr, "Client protocol error:%p: ", NtCurrentTeb()->tid );
100     vfprintf( stderr, err, args );
101     va_end( args );
102     CLIENT_Die();
103 }
104
105
106 /***********************************************************************
107  *           server_perror
108  */
109 static void server_perror( const char *err )
110 {
111     fprintf( stderr, "Client protocol error:%p: ", NtCurrentTeb()->tid );
112     perror( err );
113     CLIENT_Die();
114 }
115
116
117 /***********************************************************************
118  *           send_request
119  *
120  * Send a request to the server.
121  */
122 static void send_request( enum request req )
123 {
124     int ret;
125     if ((ret = write( NtCurrentTeb()->socket, &req, sizeof(req) )) == sizeof(req))
126         return;
127     if (ret == -1)
128     {
129         if (errno == EPIPE) CLIENT_Die();
130         server_perror( "sendmsg" );
131     }
132     server_protocol_error( "partial msg sent %d/%d\n", ret, sizeof(req) );
133 }
134
135 /***********************************************************************
136  *           send_request_fd
137  *
138  * Send a request to the server, passing a file descriptor.
139  */
140 static void send_request_fd( enum request req, int fd )
141 {
142     int ret;
143 #ifndef HAVE_MSGHDR_ACCRIGHTS
144     struct cmsg_fd cmsg;
145 #endif
146     struct msghdr msghdr;
147     struct iovec vec;
148
149     vec.iov_base = (void *)&req;
150     vec.iov_len  = sizeof(req);
151
152     msghdr.msg_name    = NULL;
153     msghdr.msg_namelen = 0;
154     msghdr.msg_iov     = &vec;
155     msghdr.msg_iovlen  = 1;
156
157 #ifdef HAVE_MSGHDR_ACCRIGHTS
158     msghdr.msg_accrights    = (void *)&fd;
159     msghdr.msg_accrightslen = sizeof(fd);
160 #else  /* HAVE_MSGHDR_ACCRIGHTS */
161     cmsg.len   = sizeof(cmsg);
162     cmsg.level = SOL_SOCKET;
163     cmsg.type  = SCM_RIGHTS;
164     cmsg.fd    = fd;
165     msghdr.msg_control    = &cmsg;
166     msghdr.msg_controllen = sizeof(cmsg);
167     msghdr.msg_flags      = 0;
168 #endif  /* HAVE_MSGHDR_ACCRIGHTS */
169
170     if ((ret = sendmsg( NtCurrentTeb()->socket, &msghdr, 0 )) == sizeof(req)) return;
171     if (ret == -1)
172     {
173         if (errno == EPIPE) CLIENT_Die();
174         server_perror( "sendmsg" );
175     }
176     server_protocol_error( "partial msg sent %d/%d\n", ret, sizeof(req) );
177 }
178
179 /***********************************************************************
180  *           wait_reply
181  *
182  * Wait for a reply from the server.
183  */
184 static unsigned int wait_reply(void)
185 {
186     int ret;
187     unsigned int res;
188
189     for (;;)
190     {
191         if ((ret = read( NtCurrentTeb()->socket, &res, sizeof(res) )) == sizeof(res))
192             return res;
193         if (ret == -1)
194         {
195             if (errno == EINTR) continue;
196             if (errno == EPIPE) CLIENT_Die();
197             server_perror("read");
198         }
199         if (!ret) CLIENT_Die(); /* the server closed the connection; time to die... */
200         server_protocol_error( "partial msg received %d/%d\n", ret, sizeof(res) );
201     }
202 }
203
204
205 /***********************************************************************
206  *           wait_reply_fd
207  *
208  * Wait for a reply from the server, when a file descriptor is passed.
209  */
210 static unsigned int wait_reply_fd( int *fd )
211 {
212     struct iovec vec;
213     int ret;
214     unsigned int res;
215
216 #ifdef HAVE_MSGHDR_ACCRIGHTS
217     struct msghdr msghdr;
218
219     *fd = -1;
220     msghdr.msg_accrights    = (void *)fd;
221     msghdr.msg_accrightslen = sizeof(*fd);
222 #else  /* HAVE_MSGHDR_ACCRIGHTS */
223     struct msghdr msghdr;
224     struct cmsg_fd cmsg;
225
226     cmsg.len   = sizeof(cmsg);
227     cmsg.level = SOL_SOCKET;
228     cmsg.type  = SCM_RIGHTS;
229     cmsg.fd    = -1;
230     msghdr.msg_control    = &cmsg;
231     msghdr.msg_controllen = sizeof(cmsg);
232     msghdr.msg_flags      = 0;
233 #endif  /* HAVE_MSGHDR_ACCRIGHTS */
234
235     msghdr.msg_name    = NULL;
236     msghdr.msg_namelen = 0;
237     msghdr.msg_iov     = &vec;
238     msghdr.msg_iovlen  = 1;
239     vec.iov_base = (void *)&res;
240     vec.iov_len  = sizeof(res);
241
242     for (;;)
243     {
244         if ((ret = recvmsg( NtCurrentTeb()->socket, &msghdr, 0 )) == sizeof(res))
245         {
246 #ifndef HAVE_MSGHDR_ACCRIGHTS
247             *fd = cmsg.fd;
248 #endif
249             return res;
250         }
251         if (ret == -1)
252         {
253             if (errno == EINTR) continue;
254             if (errno == EPIPE) CLIENT_Die();
255             server_perror("recvmsg");
256         }
257         if (!ret) CLIENT_Die(); /* the server closed the connection; time to die... */
258         server_protocol_error( "partial seq received %d/%d\n", ret, sizeof(res) );
259     }
260 }
261
262
263 /***********************************************************************
264  *           server_call_noerr
265  *
266  * Perform a server call.
267  */
268 unsigned int server_call_noerr( enum request req )
269 {
270     send_request( req );
271     return wait_reply();
272 }
273
274
275 /***********************************************************************
276  *           server_call_fd
277  *
278  * Perform a server call, passing a file descriptor.
279  * If *fd is != -1, it will be passed to the server.
280  * If the server passes an fd, it will be stored into *fd.
281  */
282 unsigned int server_call_fd( enum request req, int fd_out, int *fd_in )
283 {
284     unsigned int res;
285
286     if (fd_out == -1) send_request( req );
287     else send_request_fd( req, fd_out );
288
289     if (fd_in) res = wait_reply_fd( fd_in );
290     else res = wait_reply();
291     if (res) SetLastError( RtlNtStatusToDosError(res) );
292     return res;  /* error code */
293 }
294
295
296 /***********************************************************************
297  *           start_server
298  *
299  * Start a new wine server.
300  */
301 static void start_server( const char *oldcwd )
302 {
303     static int started;  /* we only try once */
304     if (!started)
305     {
306         int pid = fork();
307         if (pid == -1) fatal_perror( "fork" );
308         if (!pid)
309         {
310             char *path, *p;
311             /* first try the installation dir */
312             execl( BINDIR "/wineserver", "wineserver", NULL );
313             if (oldcwd) chdir( oldcwd );
314             /* now try the dir we were launched from */
315             path = xmalloc( strlen(argv0) + 20 );
316             if ((p = strrchr( strcpy( path, argv0 ), '/' )))
317             {
318                 strcpy( p, "/wineserver" );
319                 execl( path, "wineserver", NULL );
320                 strcpy( p, "/server/wineserver" );
321                 execl( path, "wineserver", NULL );
322             }
323             /* now try the path */
324             execlp( "wineserver", "wineserver", NULL );
325             /* and finally the current dir */
326             execl( "./server/wineserver", "wineserver", NULL );
327             fatal_error( "could not exec wineserver\n" );
328         }
329         started = 1;
330     }
331 }
332
333 /***********************************************************************
334  *           server_connect
335  *
336  * Attempt to connect to an existing server socket.
337  * We need to be in the server directory already.
338  */
339 static int server_connect( const char *oldcwd, const char *serverdir )
340 {
341     struct sockaddr_un addr;
342     struct stat st;
343     int s, slen;
344
345     if (chdir( serverdir ) == -1)
346     {
347         if (errno != ENOENT) fatal_perror( "chdir to %s", serverdir );
348         start_server( NULL );
349         return -1;
350     }
351     if (stat( ".", &st ) == -1) fatal_perror( "stat %s", serverdir );
352     if (st.st_uid != getuid()) fatal_error( "'%s' is not owned by you\n", serverdir );
353     if (st.st_mode & 077) fatal_error( "'%s' must not be accessible by other users\n", serverdir );
354
355     if (lstat( SOCKETNAME, &st ) == -1)
356     {
357         if (errno != ENOENT) fatal_perror( "lstat %s/%s", serverdir, SOCKETNAME );
358         start_server( oldcwd );
359         return -1;
360     }
361     if (!S_ISSOCK(st.st_mode))
362         fatal_error( "'%s/%s' is not a socket\n", serverdir, SOCKETNAME );
363     if (st.st_uid != getuid())
364         fatal_error( "'%s/%s' is not owned by you\n", serverdir, SOCKETNAME );
365
366     if ((s = socket( AF_UNIX, SOCK_STREAM, 0 )) == -1) fatal_perror( "socket" );
367     addr.sun_family = AF_UNIX;
368     strcpy( addr.sun_path, SOCKETNAME );
369     slen = sizeof(addr) - sizeof(addr.sun_path) + strlen(addr.sun_path) + 1;
370 #ifdef HAVE_SOCKADDR_SUN_LEN
371     addr.sun_len = slen;
372 #endif
373     if (connect( s, (struct sockaddr *)&addr, slen ) == -1)
374     {
375         close( s );
376         return -2;
377     }
378     fcntl( s, F_SETFD, 1 ); /* set close on exec flag */
379     return s;
380 }
381
382
383 /***********************************************************************
384  *           CLIENT_InitServer
385  *
386  * Start the server and create the initial socket pair.
387  */
388 int CLIENT_InitServer(void)
389 {
390     int delay, fd, size;
391     const char *env_fd;
392     char hostname[64];
393     char *oldcwd, *serverdir;
394     const char *configdir;
395
396     /* first check if we inherited the socket fd */
397     if ((env_fd = getenv( "__WINE_FD" )) && isdigit(env_fd[0]))
398     {
399         fd = atoi( env_fd );
400         if (fcntl( fd, F_GETFL, 0 ) != -1) return fd;
401     }
402
403     /* retrieve the current directory */
404     for (size = 512; ; size *= 2)
405     {
406         oldcwd = xmalloc( size );
407         if (getcwd( oldcwd, size )) break;
408         free( oldcwd );
409         if (errno == ERANGE) continue;
410         oldcwd = NULL;
411         break;
412     }
413
414     /* get the server directory name */
415     if (gethostname( hostname, sizeof(hostname) ) == -1) fatal_perror( "gethostname" );
416     configdir = PROFILE_GetConfigDir();
417     serverdir = xmalloc( strlen(configdir) + strlen(SERVERDIR) + strlen(hostname) + 1 );
418     strcpy( serverdir, configdir );
419     strcat( serverdir, SERVERDIR );
420     strcat( serverdir, hostname );
421
422     /* try to connect, leaving some time for the server to start up */
423     for (delay = 10000; delay < 500000; delay += delay / 2)
424     {
425         if ((fd = server_connect( oldcwd, serverdir )) >= 0) goto done;
426         usleep( delay );
427     }
428
429     if (fd == -2)
430     {
431         fatal_error( "'%s/%s' exists,\n"
432                      "   but I cannot connect to it; maybe the server has crashed?\n"
433                      "   If this is the case, you should remove the socket file and try again.\n",
434                      serverdir, SOCKETNAME );
435     }
436     fatal_error( "could not start wineserver, giving up\n" );
437
438  done:
439     /* switch back to the starting directory */
440     if (oldcwd)
441     {
442         chdir( oldcwd );
443         free( oldcwd );
444     }
445     return fd;
446 }
447
448
449 /***********************************************************************
450  *           CLIENT_InitThread
451  *
452  * Send an init thread request. Return 0 if OK.
453  */
454 int CLIENT_InitThread(void)
455 {
456     struct get_thread_buffer_request *first_req;
457     struct init_thread_request *req;
458     TEB *teb = NtCurrentTeb();
459     int fd;
460
461     if (wait_reply_fd( &fd ) || (fd == -1))
462         server_protocol_error( "no fd passed on first request\n" );
463     if ((teb->buffer_size = lseek( fd, 0, SEEK_END )) == -1) server_perror( "lseek" );
464     teb->buffer = mmap( 0, teb->buffer_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 );
465     close( fd );
466     if (teb->buffer == (void*)-1) server_perror( "mmap" );
467     first_req = teb->buffer;
468     teb->process->server_pid = first_req->pid;
469     teb->tid = first_req->tid;
470     if (first_req->boot) boot_thread_id = teb->tid;
471     else if (boot_thread_id == teb->tid) boot_thread_id = 0;
472
473     req = teb->buffer;
474     req->unix_pid = getpid();
475     req->teb      = teb;
476     req->entry    = teb->entry_point;
477     return server_call_noerr( REQ_INIT_THREAD );
478 }
479
480 /***********************************************************************
481  *           CLIENT_BootDone
482  *
483  * Signal that we have finished booting, and set debug level.
484  */
485 int CLIENT_BootDone( int debug_level )
486 {
487     struct boot_done_request *req = get_req_buffer();
488     req->debug_level = debug_level;
489     return server_call( REQ_BOOT_DONE );
490 }
491
492
493 /***********************************************************************
494  *           CLIENT_IsBootThread
495  *
496  * Return TRUE if current thread is the boot thread.
497  */
498 int CLIENT_IsBootThread(void)
499 {
500     return (GetCurrentThreadId() == (DWORD)boot_thread_id);
501 }
502
503 /***********************************************************************
504  *           CLIENT_DebuggerRequest
505  *
506  * Send a debugger support request. Return 0 if OK.
507  */
508 int CLIENT_DebuggerRequest( int op )
509 {
510     struct debugger_request *req = get_req_buffer();
511     req->op = op;
512     return server_call( REQ_DEBUGGER );
513 }