blob: cea23ef7d549466ec5043eb23f1506e9c9bf052b [file] [log] [blame]
Alexandre Julliard767e6f61998-08-09 12:47:43 +00001/*
2 * Server-side request handling
3 *
4 * Copyright (C) 1998 Alexandre Julliard
Alexandre Julliard0799c1a2002-03-09 23:29:33 +00005 *
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.
10 *
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.
15 *
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
Jonathan Ernst360a3f92006-05-18 14:49:52 +020018 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
Alexandre Julliard767e6f61998-08-09 12:47:43 +000019 */
20
François Gouget14259412001-11-06 20:57:11 +000021#include "config.h"
Francois Gouget386cf6e2001-10-14 16:25:47 +000022#include "wine/port.h"
Alexandre Julliard1dca5e22000-01-01 00:56:27 +000023
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +000024#include <assert.h>
Alexandre Julliard1dca5e22000-01-01 00:56:27 +000025#include <errno.h>
26#include <fcntl.h>
Steven Edwards037c8a12003-02-11 22:27:13 +000027#ifdef HAVE_PWD_H
Alexandre Julliard2fe57772000-01-25 01:40:27 +000028#include <pwd.h>
Steven Edwards037c8a12003-02-11 22:27:13 +000029#endif
Alexandre Julliard2fe57772000-01-25 01:40:27 +000030#include <signal.h>
Alexandre Julliard767e6f61998-08-09 12:47:43 +000031#include <stdio.h>
32#include <stdlib.h>
Alexandre Julliard1dca5e22000-01-01 00:56:27 +000033#include <stdarg.h>
34#include <string.h>
Alexandre Julliard2fe57772000-01-25 01:40:27 +000035#include <sys/stat.h>
Alexandre Julliard1dca5e22000-01-01 00:56:27 +000036#include <sys/time.h>
Alexandre Julliard85ed45e1998-08-22 19:03:56 +000037#include <sys/types.h>
Alexandre Julliard1dca5e22000-01-01 00:56:27 +000038#ifdef HAVE_SYS_SOCKET_H
39# include <sys/socket.h>
40#endif
Alexandre Julliard4144b5b2002-06-20 23:21:27 +000041#ifdef HAVE_SYS_WAIT_H
42# include <sys/wait.h>
43#endif
Steven Edwards037c8a12003-02-11 22:27:13 +000044#ifdef HAVE_SYS_UIO_H
Alexandre Julliard767e6f61998-08-09 12:47:43 +000045#include <sys/uio.h>
Steven Edwards037c8a12003-02-11 22:27:13 +000046#endif
47#ifdef HAVE_SYS_UN_H
Alexandre Julliard2fe57772000-01-25 01:40:27 +000048#include <sys/un.h>
Steven Edwards037c8a12003-02-11 22:27:13 +000049#endif
Alexandre Julliard767e6f61998-08-09 12:47:43 +000050#include <unistd.h>
Steven Edwards57279182005-03-04 12:38:36 +000051#ifdef HAVE_POLL_H
52#include <poll.h>
53#endif
Alexandre Julliard767e6f61998-08-09 12:47:43 +000054
Alexandre Julliard4ec9e112006-05-28 18:27:02 +020055#include "ntstatus.h"
56#define WIN32_NO_STATUS
Alexandre Julliard435e2e62002-12-10 22:56:43 +000057#include "windef.h"
Alexandre Julliard767e6f61998-08-09 12:47:43 +000058#include "winbase.h"
Alexandre Julliard4b461121999-01-31 19:04:30 +000059#include "wincon.h"
Alexandre Julliard4ec9e112006-05-28 18:27:02 +020060#include "winternl.h"
Alexandre Julliard4144b5b2002-06-20 23:21:27 +000061#include "wine/library.h"
62
Alexandre Julliard863637b2003-01-30 00:26:44 +000063#include "file.h"
Alexandre Julliard2fe57772000-01-25 01:40:27 +000064#include "process.h"
Alexandre Julliard5bc78081999-06-22 17:26:53 +000065#define WANT_REQUEST_HANDLERS
66#include "request.h"
Alexandre Julliard1dca5e22000-01-01 00:56:27 +000067
68/* Some versions of glibc don't define this */
69#ifndef SCM_RIGHTS
70#define SCM_RIGHTS 1
71#endif
72
Alexandre Julliard4144b5b2002-06-20 23:21:27 +000073/* path names for server master Unix socket */
74static const char * const server_socket_name = "socket"; /* name of the socket file */
75static const char * const server_lock_name = "lock"; /* name of the server lock file */
Alexandre Julliard2fe57772000-01-25 01:40:27 +000076
77struct master_socket
78{
Alexandre Julliarde66207e2003-02-19 00:33:32 +000079 struct object obj; /* object header */
80 struct fd *fd; /* file descriptor of the master socket */
Alexandre Julliard2fe57772000-01-25 01:40:27 +000081};
82
83static void master_socket_dump( struct object *obj, int verbose );
Alexandre Julliarde66207e2003-02-19 00:33:32 +000084static void master_socket_destroy( struct object *obj );
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +000085static void master_socket_poll_event( struct fd *fd, int event );
Alexandre Julliard2fe57772000-01-25 01:40:27 +000086
87static const struct object_ops master_socket_ops =
88{
89 sizeof(struct master_socket), /* size */
90 master_socket_dump, /* dump */
Alexandre Julliard8382eb02007-12-05 18:16:42 +010091 no_get_type, /* get_type */
Alexandre Julliard2fe57772000-01-25 01:40:27 +000092 no_add_queue, /* add_queue */
93 NULL, /* remove_queue */
94 NULL, /* signaled */
95 NULL, /* satisfied */
Mike McCormackf92fff62005-04-24 17:35:52 +000096 no_signal, /* signal */
Alexandre Julliard863637b2003-01-30 00:26:44 +000097 no_get_fd, /* get_fd */
Alexandre Julliard28beba32005-12-12 14:57:40 +010098 no_map_access, /* map_access */
Rob Shearmanc1707d82007-10-03 13:10:37 +010099 default_get_sd, /* get_sd */
100 default_set_sd, /* set_sd */
Vitaliy Margolenbaffcb92005-11-22 14:55:42 +0000101 no_lookup_name, /* lookup_name */
Alexandre Julliard7e71c1d2007-03-22 11:44:29 +0100102 no_open_file, /* open_file */
Alexandre Julliardb9b1ea92005-06-09 15:39:52 +0000103 no_close_handle, /* close_handle */
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000104 master_socket_destroy /* destroy */
Alexandre Julliard863637b2003-01-30 00:26:44 +0000105};
106
107static const struct fd_ops master_socket_fd_ops =
108{
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000109 NULL, /* get_poll_events */
110 master_socket_poll_event, /* poll_event */
Alexandre Julliard3f057592007-04-12 20:21:53 +0200111 NULL, /* flush */
112 NULL, /* get_fd_type */
Alexandre Julliard63571432007-04-16 14:45:03 +0200113 NULL, /* ioctl */
Alexandre Julliard3f057592007-04-12 20:21:53 +0200114 NULL, /* queue_async */
Alexandre Julliard72bff2e2007-04-10 17:07:27 +0200115 NULL, /* reselect_async */
Alexandre Julliard3f057592007-04-12 20:21:53 +0200116 NULL /* cancel_async */
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000117};
118
119
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000120struct thread *current = NULL; /* thread handling the current request */
Alexandre Julliard908464d2000-11-01 03:11:12 +0000121unsigned int global_error = 0; /* global error code for when no thread is current */
Alexandre Julliardaaf477f2007-04-17 20:08:59 +0200122timeout_t server_start_time = 0; /* server startup time */
Alexandre Julliard161160f2008-04-17 12:41:34 +0200123int server_dir_fd = -1; /* file descriptor for the server dir */
124int config_dir_fd = -1; /* file descriptor for the config dir */
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000125
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000126static struct master_socket *master_socket; /* the master socket object */
Alexandre Julliardaf268c62008-01-02 16:16:00 +0100127static struct timeout_user *master_timeout;
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000128
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000129/* complain about a protocol error and terminate the client connection */
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000130void fatal_protocol_error( struct thread *thread, const char *err, ... )
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000131{
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000132 va_list args;
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000133
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000134 va_start( args, err );
Alexandre Julliard202ef692006-06-07 14:20:38 +0200135 fprintf( stderr, "Protocol error:%04x: ", thread->id );
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000136 vfprintf( stderr, err, args );
137 va_end( args );
Alexandre Julliard12f29b52000-03-17 15:16:57 +0000138 thread->exit_code = 1;
139 kill_thread( thread, 1 );
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000140}
141
Alexandre Julliardd90e9642001-02-21 04:21:50 +0000142/* complain about a protocol error and terminate the client connection */
143void fatal_protocol_perror( struct thread *thread, const char *err, ... )
144{
145 va_list args;
146
147 va_start( args, err );
Alexandre Julliard202ef692006-06-07 14:20:38 +0200148 fprintf( stderr, "Protocol error:%04x: ", thread->id );
Alexandre Julliardd90e9642001-02-21 04:21:50 +0000149 vfprintf( stderr, err, args );
150 perror( " " );
151 va_end( args );
152 thread->exit_code = 1;
153 kill_thread( thread, 1 );
154}
155
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000156/* die on a fatal error */
Alexandre Julliard6c8d9172000-08-26 04:40:07 +0000157void fatal_error( const char *err, ... )
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000158{
159 va_list args;
160
161 va_start( args, err );
162 fprintf( stderr, "wineserver: " );
163 vfprintf( stderr, err, args );
164 va_end( args );
165 exit(1);
166}
167
168/* die on a fatal error */
Alexandre Julliard6c8d9172000-08-26 04:40:07 +0000169void fatal_perror( const char *err, ... )
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000170{
171 va_list args;
172
173 va_start( args, err );
174 fprintf( stderr, "wineserver: " );
175 vfprintf( stderr, err, args );
176 perror( " " );
177 va_end( args );
178 exit(1);
179}
180
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000181/* allocate the reply data */
Alexandre Julliard0f273c12006-07-26 10:43:25 +0200182void *set_reply_data_size( data_size_t size )
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000183{
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000184 assert( size <= get_reply_max_size() );
185 if (size && !(current->reply_data = mem_alloc( size ))) size = 0;
186 current->reply_size = size;
187 return current->reply_data;
188}
Alexandre Julliardd90e9642001-02-21 04:21:50 +0000189
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000190/* write the remaining part of the reply */
191void write_reply( struct thread *thread )
192{
193 int ret;
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000194
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000195 if ((ret = write( get_unix_fd( thread->reply_fd ),
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000196 (char *)thread->reply_data + thread->reply_size - thread->reply_towrite,
197 thread->reply_towrite )) >= 0)
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000198 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000199 if (!(thread->reply_towrite -= ret))
Alexandre Julliard67a74992001-02-27 02:09:16 +0000200 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000201 free( thread->reply_data );
202 thread->reply_data = NULL;
203 /* sent everything, can go back to waiting for requests */
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000204 set_fd_events( thread->request_fd, POLLIN );
205 set_fd_events( thread->reply_fd, 0 );
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000206 }
207 return;
208 }
209 if (errno == EPIPE)
210 kill_thread( thread, 0 ); /* normal death */
211 else if (errno != EWOULDBLOCK && errno != EAGAIN)
212 fatal_protocol_perror( thread, "reply write" );
213}
214
215/* send a reply to the current thread */
216static void send_reply( union generic_reply *reply )
217{
218 int ret;
219
220 if (!current->reply_size)
221 {
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000222 if ((ret = write( get_unix_fd( current->reply_fd ),
223 reply, sizeof(*reply) )) != sizeof(*reply)) goto error;
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000224 }
225 else
226 {
227 struct iovec vec[2];
228
Francois Gouget0763abf2002-04-01 21:08:16 +0000229 vec[0].iov_base = (void *)reply;
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000230 vec[0].iov_len = sizeof(*reply);
231 vec[1].iov_base = current->reply_data;
232 vec[1].iov_len = current->reply_size;
233
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000234 if ((ret = writev( get_unix_fd( current->reply_fd ), vec, 2 )) < sizeof(*reply)) goto error;
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000235
236 if ((current->reply_towrite = current->reply_size - (ret - sizeof(*reply))))
237 {
238 /* couldn't write it all, wait for POLLOUT */
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000239 set_fd_events( current->reply_fd, POLLOUT );
240 set_fd_events( current->request_fd, 0 );
Alexandre Julliard67a74992001-02-27 02:09:16 +0000241 return;
242 }
Alexandre Julliardd90e9642001-02-21 04:21:50 +0000243 }
Michael Stefaniuc5ceccec2006-10-09 23:34:36 +0200244 free( current->reply_data );
245 current->reply_data = NULL;
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000246 return;
247
248 error:
249 if (ret >= 0)
250 fatal_protocol_error( current, "partial write %d\n", ret );
251 else if (errno == EPIPE)
252 kill_thread( current, 0 ); /* normal death */
253 else
254 fatal_protocol_perror( current, "reply write" );
255}
256
257/* call a request handler */
258static void call_req_handler( struct thread *thread )
259{
260 union generic_reply reply;
261 enum request req = thread->req.request_header.req;
262
263 current = thread;
264 current->reply_size = 0;
265 clear_error();
266 memset( &reply, 0, sizeof(reply) );
267
268 if (debug_level) trace_request();
Alexandre Julliard67a74992001-02-27 02:09:16 +0000269
270 if (req < REQ_NB_REQUESTS)
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000271 req_handlers[req]( &current->req, &reply );
Alexandre Julliard4ec9e112006-05-28 18:27:02 +0200272 else
273 set_error( STATUS_NOT_IMPLEMENTED );
274
275 if (current)
276 {
277 if (current->reply_fd)
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000278 {
Alexandre Julliard4ec9e112006-05-28 18:27:02 +0200279 reply.reply_header.error = current->error;
280 reply.reply_header.reply_size = current->reply_size;
281 if (debug_level) trace_reply( req, &reply );
282 send_reply( &reply );
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000283 }
Alexandre Julliard55586522006-06-08 10:26:02 +0200284 else
285 {
286 current->exit_code = 1;
287 kill_thread( current, 1 ); /* no way to continue without reply fd */
288 }
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000289 }
Alexandre Julliard4ec9e112006-05-28 18:27:02 +0200290 current = NULL;
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000291}
292
Alexandre Julliard8859d772001-03-01 22:13:49 +0000293/* read a request from a thread */
294void read_request( struct thread *thread )
295{
Alexandre Julliard8859d772001-03-01 22:13:49 +0000296 int ret;
297
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000298 if (!thread->req_toread) /* no pending request */
Alexandre Julliard8859d772001-03-01 22:13:49 +0000299 {
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000300 if ((ret = read( get_unix_fd( thread->request_fd ), &thread->req,
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000301 sizeof(thread->req) )) != sizeof(thread->req)) goto error;
302 if (!(thread->req_toread = thread->req.request_header.request_size))
303 {
304 /* no data, handle request at once */
305 call_req_handler( thread );
306 return;
307 }
308 if (!(thread->req_data = malloc( thread->req_toread )))
Alexandre Julliard55224462006-07-27 14:39:33 +0200309 {
310 fatal_protocol_error( thread, "no memory for %u bytes request %d\n",
311 thread->req_toread, thread->req.request_header.req );
312 return;
313 }
Alexandre Julliard8859d772001-03-01 22:13:49 +0000314 }
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000315
316 /* read the variable sized data */
317 for (;;)
318 {
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000319 ret = read( get_unix_fd( thread->request_fd ),
320 (char *)thread->req_data + thread->req.request_header.request_size
321 - thread->req_toread,
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000322 thread->req_toread );
323 if (ret <= 0) break;
324 if (!(thread->req_toread -= ret))
325 {
326 call_req_handler( thread );
327 free( thread->req_data );
328 thread->req_data = NULL;
329 return;
330 }
331 }
332
333error:
Alexandre Julliard8859d772001-03-01 22:13:49 +0000334 if (!ret) /* closed pipe */
335 kill_thread( thread, 0 );
336 else if (ret > 0)
337 fatal_protocol_error( thread, "partial read %d\n", ret );
Alexandre Julliard4b29d662001-06-19 18:23:13 +0000338 else if (errno != EWOULDBLOCK && errno != EAGAIN)
Alexandre Julliard8859d772001-03-01 22:13:49 +0000339 fatal_protocol_perror( thread, "read" );
340}
341
Alexandre Julliardf5242402001-02-28 21:45:23 +0000342/* receive a file descriptor on the process socket */
343int receive_fd( struct process *process )
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000344{
Alexandre Julliard17971522010-09-30 11:53:07 +0200345 struct iovec vec;
Alexandre Julliardf5242402001-02-28 21:45:23 +0000346 struct send_fd data;
Alexandre Julliard17971522010-09-30 11:53:07 +0200347 struct msghdr msghdr;
348 int fd = -1, ret;
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000349
Alexandre Julliard5537dbb2003-03-28 00:36:12 +0000350#ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000351 msghdr.msg_accrightslen = sizeof(int);
Alexandre Julliardf5242402001-02-28 21:45:23 +0000352 msghdr.msg_accrights = (void *)&fd;
Alexandre Julliard5537dbb2003-03-28 00:36:12 +0000353#else /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
Alexandre Julliard17971522010-09-30 11:53:07 +0200354 char cmsg_buffer[256];
355 msghdr.msg_control = cmsg_buffer;
356 msghdr.msg_controllen = sizeof(cmsg_buffer);
357 msghdr.msg_flags = 0;
Alexandre Julliard5537dbb2003-03-28 00:36:12 +0000358#endif /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000359
Alexandre Julliard17971522010-09-30 11:53:07 +0200360 msghdr.msg_name = NULL;
361 msghdr.msg_namelen = 0;
362 msghdr.msg_iov = &vec;
363 msghdr.msg_iovlen = 1;
364 vec.iov_base = (void *)&data;
365 vec.iov_len = sizeof(data);
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000366
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000367 ret = recvmsg( get_unix_fd( process->msg_fd ), &msghdr, 0 );
Alexandre Julliard17971522010-09-30 11:53:07 +0200368
Alexandre Julliard5537dbb2003-03-28 00:36:12 +0000369#ifndef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
Alexandre Julliard17971522010-09-30 11:53:07 +0200370 if (ret > 0)
371 {
372 struct cmsghdr *cmsg;
373 for (cmsg = CMSG_FIRSTHDR( &msghdr ); cmsg; cmsg = CMSG_NXTHDR( &msghdr, cmsg ))
374 {
375 if (cmsg->cmsg_level != SOL_SOCKET) continue;
376 if (cmsg->cmsg_type == SCM_RIGHTS) fd = *(int *)CMSG_DATA(cmsg);
377 }
378 }
379#endif /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000380
Alexandre Julliardf5242402001-02-28 21:45:23 +0000381 if (ret == sizeof(data))
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000382 {
Alexandre Julliard8859d772001-03-01 22:13:49 +0000383 struct thread *thread;
384
385 if (data.tid) thread = get_thread_from_id( data.tid );
Alexandre Julliard05026382005-03-01 10:56:18 +0000386 else thread = (struct thread *)grab_object( get_process_first_thread( process ));
Alexandre Julliard8859d772001-03-01 22:13:49 +0000387
Alexandre Julliard714156d2002-08-13 18:24:27 +0000388 if (!thread || thread->process != process || thread->state == TERMINATED)
Alexandre Julliardf5242402001-02-28 21:45:23 +0000389 {
390 if (debug_level)
Alexandre Julliard91befe12003-02-01 01:38:40 +0000391 fprintf( stderr, "%04x: *fd* %d <- %d bad thread id\n",
392 data.tid, data.fd, fd );
Alexandre Julliardf5242402001-02-28 21:45:23 +0000393 close( fd );
394 }
395 else
396 {
397 if (debug_level)
Alexandre Julliard91befe12003-02-01 01:38:40 +0000398 fprintf( stderr, "%04x: *fd* %d <- %d\n",
399 thread->id, data.fd, fd );
Alexandre Julliardf5242402001-02-28 21:45:23 +0000400 thread_add_inflight_fd( thread, data.fd, fd );
401 }
Alexandre Julliard8859d772001-03-01 22:13:49 +0000402 if (thread) release_object( thread );
Alexandre Julliardf5242402001-02-28 21:45:23 +0000403 return 0;
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000404 }
Alexandre Julliardf5242402001-02-28 21:45:23 +0000405
Alexandre Julliard0a364622006-03-27 12:14:24 +0200406 if (!ret)
Alexandre Julliardf5242402001-02-28 21:45:23 +0000407 {
Alexandre Julliard81977b72006-09-21 11:14:45 +0200408 kill_process( process, 0 );
Alexandre Julliard0a364622006-03-27 12:14:24 +0200409 }
410 else if (ret > 0)
411 {
Alexandre Julliard202ef692006-06-07 14:20:38 +0200412 fprintf( stderr, "Protocol error: process %04x: partial recvmsg %d for fd\n",
413 process->id, ret );
Alexandre Julliard17971522010-09-30 11:53:07 +0200414 if (fd != -1) close( fd );
Alexandre Julliard81977b72006-09-21 11:14:45 +0200415 kill_process( process, 1 );
Alexandre Julliardf5242402001-02-28 21:45:23 +0000416 }
Alexandre Julliard8859d772001-03-01 22:13:49 +0000417 else
Alexandre Julliardf5242402001-02-28 21:45:23 +0000418 {
419 if (errno != EWOULDBLOCK && errno != EAGAIN)
420 {
Alexandre Julliard202ef692006-06-07 14:20:38 +0200421 fprintf( stderr, "Protocol error: process %04x: ", process->id );
Alexandre Julliardf5242402001-02-28 21:45:23 +0000422 perror( "recvmsg" );
Alexandre Julliard81977b72006-09-21 11:14:45 +0200423 kill_process( process, 1 );
Alexandre Julliardf5242402001-02-28 21:45:23 +0000424 }
425 }
426 return -1;
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000427}
428
Alexandre Julliardd549f692000-12-22 02:04:15 +0000429/* send an fd to a client */
Alexandre Julliard51885742002-05-30 20:12:58 +0000430int send_client_fd( struct process *process, int fd, obj_handle_t handle )
Alexandre Julliardd549f692000-12-22 02:04:15 +0000431{
Alexandre Julliard17971522010-09-30 11:53:07 +0200432 struct iovec vec;
433 struct msghdr msghdr;
Alexandre Julliardd549f692000-12-22 02:04:15 +0000434 int ret;
435
Alexandre Julliard5537dbb2003-03-28 00:36:12 +0000436#ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
Alexandre Julliardd549f692000-12-22 02:04:15 +0000437 msghdr.msg_accrightslen = sizeof(fd);
438 msghdr.msg_accrights = (void *)&fd;
Alexandre Julliard5537dbb2003-03-28 00:36:12 +0000439#else /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
Alexandre Julliard17971522010-09-30 11:53:07 +0200440 char cmsg_buffer[256];
441 struct cmsghdr *cmsg;
442 msghdr.msg_control = cmsg_buffer;
443 msghdr.msg_controllen = sizeof(cmsg_buffer);
444 msghdr.msg_flags = 0;
445 cmsg = CMSG_FIRSTHDR( &msghdr );
446 cmsg->cmsg_len = CMSG_LEN( sizeof(fd) );
447 cmsg->cmsg_level = SOL_SOCKET;
448 cmsg->cmsg_type = SCM_RIGHTS;
449 *(int *)CMSG_DATA(cmsg) = fd;
450 msghdr.msg_controllen = cmsg->cmsg_len;
Alexandre Julliard5537dbb2003-03-28 00:36:12 +0000451#endif /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
Alexandre Julliardd549f692000-12-22 02:04:15 +0000452
Alexandre Julliard17971522010-09-30 11:53:07 +0200453 msghdr.msg_name = NULL;
454 msghdr.msg_namelen = 0;
455 msghdr.msg_iov = &vec;
456 msghdr.msg_iovlen = 1;
457
458 vec.iov_base = (void *)&handle;
459 vec.iov_len = sizeof(handle);
460
461 if (debug_level)
462 fprintf( stderr, "%04x: *fd* %04x -> %d\n", current ? current->id : process->id, handle, fd );
Alexandre Julliardd549f692000-12-22 02:04:15 +0000463
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000464 ret = sendmsg( get_unix_fd( process->msg_fd ), &msghdr, 0 );
Alexandre Julliardd549f692000-12-22 02:04:15 +0000465
Alexandre Julliard8859d772001-03-01 22:13:49 +0000466 if (ret == sizeof(handle)) return 0;
467
468 if (ret >= 0)
Alexandre Julliardd549f692000-12-22 02:04:15 +0000469 {
Alexandre Julliard202ef692006-06-07 14:20:38 +0200470 fprintf( stderr, "Protocol error: process %04x: partial sendmsg %d\n", process->id, ret );
Alexandre Julliard81977b72006-09-21 11:14:45 +0200471 kill_process( process, 1 );
Alexandre Julliardd549f692000-12-22 02:04:15 +0000472 }
Alexandre Julliard4144b5b2002-06-20 23:21:27 +0000473 else if (errno == EPIPE)
474 {
Alexandre Julliard81977b72006-09-21 11:14:45 +0200475 kill_process( process, 0 );
Alexandre Julliard4144b5b2002-06-20 23:21:27 +0000476 }
Alexandre Julliardd549f692000-12-22 02:04:15 +0000477 else
478 {
Alexandre Julliard202ef692006-06-07 14:20:38 +0200479 fprintf( stderr, "Protocol error: process %04x: ", process->id );
Alexandre Julliard8859d772001-03-01 22:13:49 +0000480 perror( "sendmsg" );
Alexandre Julliard81977b72006-09-21 11:14:45 +0200481 kill_process( process, 1 );
Alexandre Julliardd549f692000-12-22 02:04:15 +0000482 }
483 return -1;
484}
485
Alexandre Julliard516e40e2001-10-17 17:48:49 +0000486/* get current tick count to return to client */
487unsigned int get_tick_count(void)
488{
Alexandre Julliardaaf477f2007-04-17 20:08:59 +0200489 return (current_time - server_start_time) / 10000;
Alexandre Julliard516e40e2001-10-17 17:48:49 +0000490}
491
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000492static void master_socket_dump( struct object *obj, int verbose )
Alexandre Julliard338e7571998-12-27 15:28:54 +0000493{
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000494 struct master_socket *sock = (struct master_socket *)obj;
495 assert( obj->ops == &master_socket_ops );
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000496 fprintf( stderr, "Master socket fd=%p\n", sock->fd );
497}
498
499static void master_socket_destroy( struct object *obj )
500{
501 struct master_socket *sock = (struct master_socket *)obj;
502 assert( obj->ops == &master_socket_ops );
503 release_object( sock->fd );
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000504}
505
506/* handle a socket event */
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000507static void master_socket_poll_event( struct fd *fd, int event )
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000508{
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000509 struct master_socket *sock = get_fd_user( fd );
510 assert( master_socket->obj.ops == &master_socket_ops );
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000511
512 assert( sock == master_socket ); /* there is only one master socket */
513
514 if (event & (POLLERR | POLLHUP))
515 {
516 /* this is not supposed to happen */
517 fprintf( stderr, "wineserver: Error on master socket\n" );
Alexandre Julliard3a4c04d2006-08-14 20:40:31 +0200518 set_fd_events( sock->fd, -1 );
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000519 }
520 else if (event & POLLIN)
521 {
522 struct sockaddr_un dummy;
Mike McCormacke659f1e2005-08-09 10:37:50 +0000523 unsigned int len = sizeof(dummy);
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000524 int client = accept( get_unix_fd( master_socket->fd ), (struct sockaddr *) &dummy, &len );
Alexandre Julliard09cc91d2001-04-04 00:11:13 +0000525 if (client == -1) return;
526 fcntl( client, F_SETFL, O_NONBLOCK );
Alexandre Julliardc316f0e2006-07-19 14:00:10 +0200527 create_process( client, NULL, 0 );
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000528 }
529}
530
531/* remove the socket upon exit */
532static void socket_cleanup(void)
533{
Alexandre Julliardc10c9ef2000-08-11 21:16:53 +0000534 static int do_it_once;
Alexandre Julliard4144b5b2002-06-20 23:21:27 +0000535 if (!do_it_once++) unlink( server_socket_name );
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000536}
537
Alexandre Julliard4144b5b2002-06-20 23:21:27 +0000538/* create a directory and check its permissions */
539static void create_dir( const char *name, struct stat *st )
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000540{
Alexandre Julliard4144b5b2002-06-20 23:21:27 +0000541 if (lstat( name, st ) == -1)
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000542 {
Alexandre Julliard4144b5b2002-06-20 23:21:27 +0000543 if (errno != ENOENT) fatal_perror( "lstat %s", name );
Francois Gouget10e3d962005-03-01 11:00:06 +0000544 if (mkdir( name, 0700 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", name );
Alexandre Julliard4144b5b2002-06-20 23:21:27 +0000545 if (lstat( name, st ) == -1) fatal_perror( "lstat %s", name );
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000546 }
Alexandre Julliard4144b5b2002-06-20 23:21:27 +0000547 if (!S_ISDIR(st->st_mode)) fatal_error( "%s is not a directory\n", name );
548 if (st->st_uid != getuid()) fatal_error( "%s is not owned by you\n", name );
549 if (st->st_mode & 077) fatal_error( "%s must not be accessible by other users\n", name );
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000550}
551
552/* create the server directory and chdir to it */
Alexandre Julliardc5c599d2006-03-31 13:06:04 +0200553static void create_server_dir( const char *dir )
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000554{
Alexandre Julliard4144b5b2002-06-20 23:21:27 +0000555 char *p, *server_dir;
556 struct stat st, st2;
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000557
Alexandre Julliardc5c599d2006-03-31 13:06:04 +0200558 if (!(server_dir = strdup( dir ))) fatal_error( "out of memory\n" );
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000559
Alexandre Julliard4144b5b2002-06-20 23:21:27 +0000560 /* first create the base directory if needed */
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000561
Alexandre Julliard4144b5b2002-06-20 23:21:27 +0000562 p = strrchr( server_dir, '/' );
563 *p = 0;
564 create_dir( server_dir, &st );
Alexandre Julliardb8dd37d2001-08-09 21:22:33 +0000565
Alexandre Julliard4144b5b2002-06-20 23:21:27 +0000566 /* now create the server directory */
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000567
Alexandre Julliard4144b5b2002-06-20 23:21:27 +0000568 *p = '/';
569 create_dir( server_dir, &st );
570
571 if (chdir( server_dir ) == -1) fatal_perror( "chdir %s", server_dir );
Alexandre Julliard161160f2008-04-17 12:41:34 +0200572 if ((server_dir_fd = open( ".", O_RDONLY )) == -1) fatal_perror( "open %s", server_dir );
573 if (fstat( server_dir_fd, &st2 ) == -1) fatal_perror( "stat %s", server_dir );
Alexandre Julliard4144b5b2002-06-20 23:21:27 +0000574 if (st.st_dev != st2.st_dev || st.st_ino != st2.st_ino)
575 fatal_error( "chdir did not end up in %s\n", server_dir );
576
577 free( server_dir );
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000578}
579
Alexandre Julliard40043ed2002-08-16 20:02:15 +0000580/* create the lock file and return its file descriptor */
581static int create_server_lock(void)
582{
583 struct stat st;
584 int fd;
585
586 if (lstat( server_lock_name, &st ) == -1)
587 {
588 if (errno != ENOENT)
589 fatal_perror( "lstat %s/%s", wine_get_server_dir(), server_lock_name );
590 }
591 else
592 {
593 if (!S_ISREG(st.st_mode))
594 fatal_error( "%s/%s is not a regular file\n", wine_get_server_dir(), server_lock_name );
595 }
596
597 if ((fd = open( server_lock_name, O_CREAT|O_TRUNC|O_WRONLY, 0600 )) == -1)
598 fatal_perror( "error creating %s/%s", wine_get_server_dir(), server_lock_name );
599 return fd;
600}
601
Alexandre Julliard4144b5b2002-06-20 23:21:27 +0000602/* wait for the server lock */
603int wait_for_lock(void)
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000604{
Alexandre Julliardc5c599d2006-03-31 13:06:04 +0200605 const char *server_dir = wine_get_server_dir();
Alexandre Julliard4144b5b2002-06-20 23:21:27 +0000606 int fd, r;
607 struct flock fl;
Alexandre Julliard0b6a79c2000-12-15 20:57:00 +0000608
Alexandre Julliardc5c599d2006-03-31 13:06:04 +0200609 if (!server_dir) return 0; /* no server dir, so no lock to wait on */
610
611 create_server_dir( server_dir );
Alexandre Julliard40043ed2002-08-16 20:02:15 +0000612 fd = create_server_lock();
Alexandre Julliard4144b5b2002-06-20 23:21:27 +0000613
614 fl.l_type = F_WRLCK;
615 fl.l_whence = SEEK_SET;
616 fl.l_start = 0;
617 fl.l_len = 1;
618 r = fcntl( fd, F_SETLKW, &fl );
619 close(fd);
620
621 return r;
622}
623
Alexandre Julliard40043ed2002-08-16 20:02:15 +0000624/* kill the wine server holding the lock */
625int kill_lock_owner( int sig )
626{
Alexandre Julliardc5c599d2006-03-31 13:06:04 +0200627 const char *server_dir = wine_get_server_dir();
Alexandre Julliard40043ed2002-08-16 20:02:15 +0000628 int fd, i, ret = 0;
629 pid_t pid = 0;
630 struct flock fl;
631
Alexandre Julliardc5c599d2006-03-31 13:06:04 +0200632 if (!server_dir) return 0; /* no server dir, nothing to do */
633
634 create_server_dir( server_dir );
Alexandre Julliard40043ed2002-08-16 20:02:15 +0000635 fd = create_server_lock();
636
Alexandre Julliard307cb092008-01-02 16:13:08 +0100637 for (i = 1; i <= 20; i++)
Alexandre Julliard40043ed2002-08-16 20:02:15 +0000638 {
639 fl.l_type = F_WRLCK;
640 fl.l_whence = SEEK_SET;
641 fl.l_start = 0;
642 fl.l_len = 1;
643 if (fcntl( fd, F_GETLK, &fl ) == -1) goto done;
644 if (fl.l_type != F_WRLCK) goto done; /* the file is not locked */
645 if (!pid) /* first time around */
646 {
647 if (!(pid = fl.l_pid)) goto done; /* shouldn't happen */
648 if (sig == -1)
649 {
650 if (kill( pid, SIGINT ) == -1) goto done;
651 kill( pid, SIGCONT );
652 ret = 1;
653 }
654 else /* just send the specified signal and return */
655 {
656 ret = (kill( pid, sig ) != -1);
657 goto done;
658 }
659 }
660 else if (fl.l_pid != pid) goto done; /* no longer the same process */
Alexandre Julliard307cb092008-01-02 16:13:08 +0100661 usleep( 50000 * i );
Alexandre Julliard40043ed2002-08-16 20:02:15 +0000662 }
663 /* waited long enough, now kill it */
664 kill( pid, SIGKILL );
665
666 done:
667 close( fd );
668 return ret;
669}
670
Alexandre Julliard4144b5b2002-06-20 23:21:27 +0000671/* acquire the main server lock */
672static void acquire_lock(void)
673{
Alexandre Julliard4144b5b2002-06-20 23:21:27 +0000674 struct sockaddr_un addr;
675 struct stat st;
676 struct flock fl;
677 int fd, slen, got_lock = 0;
678
Alexandre Julliard40043ed2002-08-16 20:02:15 +0000679 fd = create_server_lock();
Alexandre Julliard4144b5b2002-06-20 23:21:27 +0000680
681 fl.l_type = F_WRLCK;
682 fl.l_whence = SEEK_SET;
683 fl.l_start = 0;
684 fl.l_len = 1;
685 if (fcntl( fd, F_SETLK, &fl ) != -1)
686 {
687 /* check for crashed server */
688 if (stat( server_socket_name, &st ) != -1 && /* there is a leftover socket */
689 stat( "core", &st ) != -1 && st.st_size) /* and there is a non-empty core file */
690 {
691 fprintf( stderr,
692 "Warning: a previous instance of the wine server seems to have crashed.\n"
693 "Please run 'gdb %s %s/core',\n"
694 "type 'backtrace' at the gdb prompt and report the results. Thanks.\n\n",
Alexandre Julliard40043ed2002-08-16 20:02:15 +0000695 server_argv0, wine_get_server_dir() );
Alexandre Julliard4144b5b2002-06-20 23:21:27 +0000696 }
697 unlink( server_socket_name ); /* we got the lock, we can safely remove the socket */
698 got_lock = 1;
699 /* in that case we reuse fd without closing it, this ensures
700 * that we hold the lock until the process exits */
701 }
702 else
703 {
704 switch(errno)
705 {
706 case ENOLCK:
707 break;
708 case EACCES:
709 /* check whether locks work at all on this file system */
710 if (fcntl( fd, F_GETLK, &fl ) == -1) break;
711 /* fall through */
712 case EAGAIN:
713 exit(2); /* we didn't get the lock, exit with special status */
714 default:
Alexandre Julliard40043ed2002-08-16 20:02:15 +0000715 fatal_perror( "fcntl %s/%s", wine_get_server_dir(), server_lock_name );
Alexandre Julliard4144b5b2002-06-20 23:21:27 +0000716 }
717 /* it seems we can't use locks on this fs, so we will use the socket existence as lock */
718 close( fd );
719 }
720
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000721 if ((fd = socket( AF_UNIX, SOCK_STREAM, 0 )) == -1) fatal_perror( "socket" );
722 addr.sun_family = AF_UNIX;
Alexandre Julliard4144b5b2002-06-20 23:21:27 +0000723 strcpy( addr.sun_path, server_socket_name );
Juergen Lock2d33ab92000-02-13 16:03:29 +0000724 slen = sizeof(addr) - sizeof(addr.sun_path) + strlen(addr.sun_path) + 1;
Alexandre Julliard5537dbb2003-03-28 00:36:12 +0000725#ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
Juergen Lock2d33ab92000-02-13 16:03:29 +0000726 addr.sun_len = slen;
727#endif
728 if (bind( fd, (struct sockaddr *)&addr, slen ) == -1)
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000729 {
730 if ((errno == EEXIST) || (errno == EADDRINUSE))
Alexandre Julliard4144b5b2002-06-20 23:21:27 +0000731 {
732 if (got_lock)
733 fatal_error( "couldn't bind to the socket even though we hold the lock\n" );
734 exit(2); /* we didn't get the lock, exit with special status */
735 }
736 fatal_perror( "bind" );
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000737 }
738 atexit( socket_cleanup );
Alexandre Julliard4144b5b2002-06-20 23:21:27 +0000739 chmod( server_socket_name, 0600 ); /* make sure no other user can connect */
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000740 if (listen( fd, 5 ) == -1) fatal_perror( "listen" );
741
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000742 if (!(master_socket = alloc_object( &master_socket_ops )) ||
Alexandre Julliardf85437c2007-04-10 22:25:07 +0200743 !(master_socket->fd = create_anonymous_fd( &master_socket_fd_ops, fd, &master_socket->obj, 0 )))
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000744 fatal_error( "out of memory\n" );
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000745 set_fd_events( master_socket->fd, POLLIN );
Alexandre Julliardb00fb172006-03-22 20:32:04 +0100746 make_object_static( &master_socket->obj );
Alexandre Julliard4144b5b2002-06-20 23:21:27 +0000747}
748
749/* open the master server socket and start waiting for new clients */
750void open_master_socket(void)
751{
Alexandre Julliardc5c599d2006-03-31 13:06:04 +0200752 const char *server_dir = wine_get_server_dir();
Alexandre Julliard161160f2008-04-17 12:41:34 +0200753 const char *config_dir = wine_get_config_dir();
Alexandre Julliard7ad5be92003-03-14 04:08:42 +0000754 int fd, pid, status, sync_pipe[2];
Alexandre Julliard4144b5b2002-06-20 23:21:27 +0000755 char dummy;
756
757 /* make sure no request is larger than the maximum size */
758 assert( sizeof(union generic_request) == sizeof(struct request_max_size) );
759 assert( sizeof(union generic_reply) == sizeof(struct request_max_size) );
760
Alexandre Julliard161160f2008-04-17 12:41:34 +0200761 if (!server_dir) fatal_error( "directory %s cannot be accessed\n", config_dir );
762 if (chdir( config_dir ) == -1) fatal_perror( "chdir to %s", config_dir );
763 if ((config_dir_fd = open( ".", O_RDONLY )) == -1) fatal_perror( "open %s", config_dir );
764
Alexandre Julliardc5c599d2006-03-31 13:06:04 +0200765 create_server_dir( server_dir );
Alexandre Julliard4144b5b2002-06-20 23:21:27 +0000766
Alexandre Julliard7ad5be92003-03-14 04:08:42 +0000767 if (!foreground)
Alexandre Julliard4144b5b2002-06-20 23:21:27 +0000768 {
Alexandre Julliard7ad5be92003-03-14 04:08:42 +0000769 if (pipe( sync_pipe ) == -1) fatal_perror( "pipe" );
770 pid = fork();
771 switch( pid )
772 {
773 case 0: /* child */
774 setsid();
775 close( sync_pipe[0] );
Alexandre Julliard4144b5b2002-06-20 23:21:27 +0000776
Alexandre Julliard7ad5be92003-03-14 04:08:42 +0000777 acquire_lock();
778
779 /* close stdin and stdout */
780 if ((fd = open( "/dev/null", O_RDWR )) != -1)
781 {
782 dup2( fd, 0 );
783 dup2( fd, 1 );
784 close( fd );
785 }
786
787 /* signal parent */
Eric Pouech2e0b5332006-01-27 16:17:51 +0100788 dummy = 0;
Alexandre Julliard7ad5be92003-03-14 04:08:42 +0000789 write( sync_pipe[1], &dummy, 1 );
790 close( sync_pipe[1] );
791 break;
792
793 case -1:
794 fatal_perror( "fork" );
795 break;
796
797 default: /* parent */
798 close( sync_pipe[1] );
799
800 /* wait for child to signal us and then exit */
801 if (read( sync_pipe[0], &dummy, 1 ) == 1) _exit(0);
802
803 /* child terminated, propagate exit status */
804 wait4( pid, &status, 0, NULL );
805 if (WIFEXITED(status)) _exit( WEXITSTATUS(status) );
806 _exit(1);
807 }
808 }
809 else /* remain in the foreground */
810 {
Alexandre Julliard4144b5b2002-06-20 23:21:27 +0000811 acquire_lock();
Alexandre Julliard4144b5b2002-06-20 23:21:27 +0000812 }
Alexandre Julliardd8041112000-04-14 14:42:41 +0000813
Alexandre Julliardcd1c7fc2006-12-29 16:56:11 +0100814 /* init the process tracing mechanism */
815 init_tracing_mechanism();
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000816}
817
Alexandre Julliardf5e0f0c2002-04-03 22:51:18 +0000818/* master socket timer expiration handler */
819static void close_socket_timeout( void *arg )
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000820{
Alexandre Julliardaf268c62008-01-02 16:16:00 +0100821 master_timeout = NULL;
Alexandre Julliard4144b5b2002-06-20 23:21:27 +0000822 flush_registry();
Alexandre Julliard4144b5b2002-06-20 23:21:27 +0000823 if (debug_level) fprintf( stderr, "wineserver: exiting (pid=%ld)\n", (long) getpid() );
824
825#ifdef DEBUG_OBJECTS
Alexandre Julliardb00fb172006-03-22 20:32:04 +0100826 close_objects(); /* shut down everything properly */
Alexandre Julliard4144b5b2002-06-20 23:21:27 +0000827#endif
Alexandre Julliardaf268c62008-01-02 16:16:00 +0100828 exit( 0 );
Alexandre Julliard2fe57772000-01-25 01:40:27 +0000829}
830
Alexandre Julliardf5e0f0c2002-04-03 22:51:18 +0000831/* close the master socket and stop waiting for new clients */
Alexandre Julliardaf268c62008-01-02 16:16:00 +0100832void close_master_socket( timeout_t timeout )
Alexandre Julliardf5e0f0c2002-04-03 22:51:18 +0000833{
Alexandre Julliardaf268c62008-01-02 16:16:00 +0100834 if (master_socket)
Alexandre Julliard3a4c04d2006-08-14 20:40:31 +0200835 {
Alexandre Julliardaf268c62008-01-02 16:16:00 +0100836 release_object( master_socket );
837 master_socket = NULL;
Alexandre Julliard3a4c04d2006-08-14 20:40:31 +0200838 }
Alexandre Julliardaf268c62008-01-02 16:16:00 +0100839 if (master_timeout) /* cancel previous timeout */
840 remove_timeout_user( master_timeout );
841
Alexandre Julliard2f1e73c2008-01-07 17:19:05 +0100842 master_timeout = add_timeout_user( timeout, close_socket_timeout, NULL );
Alexandre Julliard338e7571998-12-27 15:28:54 +0000843}