This patch initializes the return buffer used in GetPrinterDriverA to
[wine.git] / scheduler / client.c
blobbd097f8c21137b10b843f8f3f3ec86855ba21f5a
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 <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>
28 #include "process.h"
29 #include "thread.h"
30 #include "server.h"
31 #include "winerror.h"
32 #include "options.h"
33 #include "xmalloc.h"
35 /* Some versions of glibc don't define this */
36 #ifndef SCM_RIGHTS
37 #define SCM_RIGHTS 1
38 #endif
40 #define SERVERDIR "/wineserver-" /* server socket directory (hostname appended) */
41 #define SOCKETNAME "socket" /* name of the socket file */
43 /* data structure used to pass an fd with sendmsg/recvmsg */
44 struct cmsg_fd
46 int len; /* sizeof structure */
47 int level; /* SOL_SOCKET */
48 int type; /* SCM_RIGHTS */
49 int fd; /* fd to pass */
52 static void *boot_thread_id;
55 /* die on a fatal error; use only during initialization */
56 static void fatal_error( const char *err, ... )
58 va_list args;
60 va_start( args, err );
61 fprintf( stderr, "wine: " );
62 vfprintf( stderr, err, args );
63 va_end( args );
64 exit(1);
67 /* die on a fatal error; use only during initialization */
68 static void fatal_perror( const char *err, ... )
70 va_list args;
72 va_start( args, err );
73 fprintf( stderr, "wine: " );
74 vfprintf( stderr, err, args );
75 perror( " " );
76 va_end( args );
77 exit(1);
80 /***********************************************************************
81 * CLIENT_Die
83 * Die on protocol errors or socket close
85 static void CLIENT_Die(void)
87 close( NtCurrentTeb()->socket );
88 SYSDEPS_ExitThread();
91 /***********************************************************************
92 * server_protocol_error
94 void server_protocol_error( const char *err, ... )
96 va_list args;
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();
106 /***********************************************************************
107 * server_perror
109 static void server_perror( const char *err )
111 fprintf( stderr, "Client protocol error:%p: ", NtCurrentTeb()->tid );
112 perror( err );
113 CLIENT_Die();
117 /***********************************************************************
118 * send_request
120 * Send a request to the server.
122 static void send_request( enum request req )
124 int ret;
125 if ((ret = write( NtCurrentTeb()->socket, &req, sizeof(req) )) == sizeof(req))
126 return;
127 if (ret == -1)
129 if (errno == EPIPE) CLIENT_Die();
130 server_perror( "sendmsg" );
132 server_protocol_error( "partial msg sent %d/%d\n", ret, sizeof(req) );
135 /***********************************************************************
136 * send_request_fd
138 * Send a request to the server, passing a file descriptor.
140 static void send_request_fd( enum request req, int fd )
142 int ret;
143 #ifndef HAVE_MSGHDR_ACCRIGHTS
144 struct cmsg_fd cmsg;
145 #endif
146 struct msghdr msghdr;
147 struct iovec vec;
149 vec.iov_base = (void *)&req;
150 vec.iov_len = sizeof(req);
152 msghdr.msg_name = NULL;
153 msghdr.msg_namelen = 0;
154 msghdr.msg_iov = &vec;
155 msghdr.msg_iovlen = 1;
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 */
170 if ((ret = sendmsg( NtCurrentTeb()->socket, &msghdr, 0 )) == sizeof(req)) return;
171 if (ret == -1)
173 if (errno == EPIPE) CLIENT_Die();
174 server_perror( "sendmsg" );
176 server_protocol_error( "partial msg sent %d/%d\n", ret, sizeof(req) );
179 /***********************************************************************
180 * wait_reply
182 * Wait for a reply from the server.
184 static unsigned int wait_reply(void)
186 int ret;
187 unsigned int res;
189 for (;;)
191 if ((ret = read( NtCurrentTeb()->socket, &res, sizeof(res) )) == sizeof(res))
192 return res;
193 if (ret == -1)
195 if (errno == EINTR) continue;
196 if (errno == EPIPE) CLIENT_Die();
197 server_perror("read");
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) );
205 /***********************************************************************
206 * wait_reply_fd
208 * Wait for a reply from the server, when a file descriptor is passed.
210 static unsigned int wait_reply_fd( int *fd )
212 struct iovec vec;
213 int ret;
214 unsigned int res;
216 #ifdef HAVE_MSGHDR_ACCRIGHTS
217 struct msghdr msghdr;
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;
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 */
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);
242 for (;;)
244 if ((ret = recvmsg( NtCurrentTeb()->socket, &msghdr, 0 )) == sizeof(res))
246 #ifndef HAVE_MSGHDR_ACCRIGHTS
247 *fd = cmsg.fd;
248 #endif
249 return res;
251 if (ret == -1)
253 if (errno == EINTR) continue;
254 if (errno == EPIPE) CLIENT_Die();
255 server_perror("recvmsg");
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) );
263 /***********************************************************************
264 * server_call_noerr
266 * Perform a server call.
268 unsigned int server_call_noerr( enum request req )
270 send_request( req );
271 return wait_reply();
275 /***********************************************************************
276 * server_call_fd
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.
282 unsigned int server_call_fd( enum request req, int fd_out, int *fd_in )
284 unsigned int res;
286 if (fd_out == -1) send_request( req );
287 else send_request_fd( req, fd_out );
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 */
296 /***********************************************************************
297 * start_server
299 * Start a new wine server.
301 static void start_server( const char *oldcwd )
303 static int started; /* we only try once */
304 if (!started)
306 int pid = fork();
307 if (pid == -1) fatal_perror( "fork" );
308 if (!pid)
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 ), '/' )))
318 strcpy( p, "/wineserver" );
319 execl( path, "wineserver", NULL );
320 strcpy( p, "/server/wineserver" );
321 execl( path, "wineserver", NULL );
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" );
329 started = 1;
333 /***********************************************************************
334 * server_connect
336 * Attempt to connect to an existing server socket.
337 * We need to be in the server directory already.
339 static int server_connect( const char *oldcwd, const char *serverdir )
341 struct sockaddr_un addr;
342 struct stat st;
343 int s, slen;
345 if (chdir( serverdir ) == -1)
347 if (errno != ENOENT) fatal_perror( "chdir to %s", serverdir );
348 start_server( NULL );
349 return -1;
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 );
355 if (lstat( SOCKETNAME, &st ) == -1)
357 if (errno != ENOENT) fatal_perror( "lstat %s/%s", serverdir, SOCKETNAME );
358 start_server( oldcwd );
359 return -1;
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 );
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)
375 close( s );
376 return -2;
378 fcntl( s, F_SETFD, 1 ); /* set close on exec flag */
379 return s;
383 /***********************************************************************
384 * CLIENT_InitServer
386 * Start the server and create the initial socket pair.
388 int CLIENT_InitServer(void)
390 int delay, fd, size;
391 const char *env_fd;
392 char hostname[64];
393 char *oldcwd, *serverdir;
394 const char *configdir;
396 /* first check if we inherited the socket fd */
397 if ((env_fd = getenv( "__WINE_FD" )) && isdigit(env_fd[0]))
399 fd = atoi( env_fd );
400 if (fcntl( fd, F_GETFL, 0 ) != -1) return fd;
403 /* retrieve the current directory */
404 for (size = 512; ; size *= 2)
406 oldcwd = xmalloc( size );
407 if (getcwd( oldcwd, size )) break;
408 free( oldcwd );
409 if (errno == ERANGE) continue;
410 oldcwd = NULL;
411 break;
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 );
422 /* try to connect, leaving some time for the server to start up */
423 for (delay = 10000; delay < 500000; delay += delay / 2)
425 if ((fd = server_connect( oldcwd, serverdir )) >= 0) goto done;
426 usleep( delay );
429 if (fd == -2)
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 );
436 fatal_error( "could not start wineserver, giving up\n" );
438 done:
439 /* switch back to the starting directory */
440 if (oldcwd)
442 chdir( oldcwd );
443 free( oldcwd );
445 return fd;
449 /***********************************************************************
450 * CLIENT_InitThread
452 * Send an init thread request. Return 0 if OK.
454 int CLIENT_InitThread(void)
456 struct init_thread_request *req;
457 TEB *teb = NtCurrentTeb();
458 int fd;
460 if (wait_reply_fd( &fd ) || (fd == -1))
461 server_protocol_error( "no fd passed on first request\n" );
462 if ((teb->buffer_size = lseek( fd, 0, SEEK_END )) == -1) server_perror( "lseek" );
463 teb->buffer = mmap( 0, teb->buffer_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 );
464 close( fd );
465 if (teb->buffer == (void*)-1) server_perror( "mmap" );
467 req = get_req_buffer();
468 req->unix_pid = getpid();
469 req->teb = teb;
470 if (server_call( REQ_INIT_THREAD )) return -1;
471 teb->process->server_pid = req->pid;
472 teb->tid = req->tid;
473 if (req->boot) boot_thread_id = req->tid;
474 else if (boot_thread_id == req->tid) boot_thread_id = 0;
475 return 0;
478 /***********************************************************************
479 * CLIENT_BootDone
481 * Signal that we have finished booting, and set debug level.
483 int CLIENT_BootDone( int debug_level )
485 struct boot_done_request *req = get_req_buffer();
486 req->debug_level = debug_level;
487 return server_call( REQ_BOOT_DONE );
491 /***********************************************************************
492 * CLIENT_IsBootThread
494 * Return TRUE if current thread is the boot thread.
496 int CLIENT_IsBootThread(void)
498 return (GetCurrentThreadId() == (DWORD)boot_thread_id);
501 /***********************************************************************
502 * CLIENT_DebuggerRequest
504 * Send a debugger support request. Return 0 if OK.
506 int CLIENT_DebuggerRequest( int op )
508 struct debugger_request *req = get_req_buffer();
509 req->op = op;
510 return server_call( REQ_DEBUGGER );