dwrite: Fixed NULL ptr deref before check (Coverity).
[wine.git] / server / request.c
blobf78026a061d71b648245307ea2cbe93ba6f804fa
1 /*
2 * Server-side request handling
4 * Copyright (C) 1998 Alexandre Julliard
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.
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.
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
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #ifdef HAVE_PWD_H
28 #include <pwd.h>
29 #endif
30 #include <signal.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <stdarg.h>
34 #include <string.h>
35 #include <sys/stat.h>
36 #include <sys/time.h>
37 #include <sys/types.h>
38 #ifdef HAVE_SYS_SOCKET_H
39 # include <sys/socket.h>
40 #endif
41 #ifdef HAVE_SYS_WAIT_H
42 # include <sys/wait.h>
43 #endif
44 #ifdef HAVE_SYS_UIO_H
45 #include <sys/uio.h>
46 #endif
47 #ifdef HAVE_SYS_UN_H
48 #include <sys/un.h>
49 #endif
50 #include <unistd.h>
51 #ifdef HAVE_POLL_H
52 #include <poll.h>
53 #endif
54 #ifdef __APPLE__
55 # include <mach/mach_time.h>
56 #endif
58 #include "ntstatus.h"
59 #define WIN32_NO_STATUS
60 #include "windef.h"
61 #include "winbase.h"
62 #include "wincon.h"
63 #include "winternl.h"
64 #include "wine/library.h"
66 #include "file.h"
67 #include "process.h"
68 #define WANT_REQUEST_HANDLERS
69 #include "request.h"
71 /* Some versions of glibc don't define this */
72 #ifndef SCM_RIGHTS
73 #define SCM_RIGHTS 1
74 #endif
76 /* path names for server master Unix socket */
77 static const char * const server_socket_name = "socket"; /* name of the socket file */
78 static const char * const server_lock_name = "lock"; /* name of the server lock file */
80 struct master_socket
82 struct object obj; /* object header */
83 struct fd *fd; /* file descriptor of the master socket */
86 static void master_socket_dump( struct object *obj, int verbose );
87 static void master_socket_destroy( struct object *obj );
88 static void master_socket_poll_event( struct fd *fd, int event );
90 static const struct object_ops master_socket_ops =
92 sizeof(struct master_socket), /* size */
93 master_socket_dump, /* dump */
94 no_get_type, /* get_type */
95 no_add_queue, /* add_queue */
96 NULL, /* remove_queue */
97 NULL, /* signaled */
98 NULL, /* satisfied */
99 no_signal, /* signal */
100 no_get_fd, /* get_fd */
101 no_map_access, /* map_access */
102 default_get_sd, /* get_sd */
103 default_set_sd, /* set_sd */
104 no_lookup_name, /* lookup_name */
105 no_open_file, /* open_file */
106 no_close_handle, /* close_handle */
107 master_socket_destroy /* destroy */
110 static const struct fd_ops master_socket_fd_ops =
112 NULL, /* get_poll_events */
113 master_socket_poll_event, /* poll_event */
114 NULL, /* flush */
115 NULL, /* get_fd_type */
116 NULL, /* ioctl */
117 NULL, /* queue_async */
118 NULL, /* reselect_async */
119 NULL /* cancel_async */
123 struct thread *current = NULL; /* thread handling the current request */
124 unsigned int global_error = 0; /* global error code for when no thread is current */
125 timeout_t server_start_time = 0; /* server startup time */
126 int server_dir_fd = -1; /* file descriptor for the server dir */
127 int config_dir_fd = -1; /* file descriptor for the config dir */
129 static struct master_socket *master_socket; /* the master socket object */
130 static struct timeout_user *master_timeout;
132 /* complain about a protocol error and terminate the client connection */
133 void fatal_protocol_error( struct thread *thread, const char *err, ... )
135 va_list args;
137 va_start( args, err );
138 fprintf( stderr, "Protocol error:%04x: ", thread->id );
139 vfprintf( stderr, err, args );
140 va_end( args );
141 thread->exit_code = 1;
142 kill_thread( thread, 1 );
145 /* die on a fatal error */
146 void fatal_error( const char *err, ... )
148 va_list args;
150 va_start( args, err );
151 fprintf( stderr, "wineserver: " );
152 vfprintf( stderr, err, args );
153 va_end( args );
154 exit(1);
157 /* allocate the reply data */
158 void *set_reply_data_size( data_size_t size )
160 assert( size <= get_reply_max_size() );
161 if (size && !(current->reply_data = mem_alloc( size ))) size = 0;
162 current->reply_size = size;
163 return current->reply_data;
166 /* write the remaining part of the reply */
167 void write_reply( struct thread *thread )
169 int ret;
171 if ((ret = write( get_unix_fd( thread->reply_fd ),
172 (char *)thread->reply_data + thread->reply_size - thread->reply_towrite,
173 thread->reply_towrite )) >= 0)
175 if (!(thread->reply_towrite -= ret))
177 free( thread->reply_data );
178 thread->reply_data = NULL;
179 /* sent everything, can go back to waiting for requests */
180 set_fd_events( thread->request_fd, POLLIN );
181 set_fd_events( thread->reply_fd, 0 );
183 return;
185 if (errno == EPIPE)
186 kill_thread( thread, 0 ); /* normal death */
187 else if (errno != EWOULDBLOCK && errno != EAGAIN)
188 fatal_protocol_error( thread, "reply write: %s\n", strerror( errno ));
191 /* send a reply to the current thread */
192 static void send_reply( union generic_reply *reply )
194 int ret;
196 if (!current->reply_size)
198 if ((ret = write( get_unix_fd( current->reply_fd ),
199 reply, sizeof(*reply) )) != sizeof(*reply)) goto error;
201 else
203 struct iovec vec[2];
205 vec[0].iov_base = (void *)reply;
206 vec[0].iov_len = sizeof(*reply);
207 vec[1].iov_base = current->reply_data;
208 vec[1].iov_len = current->reply_size;
210 if ((ret = writev( get_unix_fd( current->reply_fd ), vec, 2 )) < sizeof(*reply)) goto error;
212 if ((current->reply_towrite = current->reply_size - (ret - sizeof(*reply))))
214 /* couldn't write it all, wait for POLLOUT */
215 set_fd_events( current->reply_fd, POLLOUT );
216 set_fd_events( current->request_fd, 0 );
217 return;
220 free( current->reply_data );
221 current->reply_data = NULL;
222 return;
224 error:
225 if (ret >= 0)
226 fatal_protocol_error( current, "partial write %d\n", ret );
227 else if (errno == EPIPE)
228 kill_thread( current, 0 ); /* normal death */
229 else
230 fatal_protocol_error( current, "reply write: %s\n", strerror( errno ));
233 /* call a request handler */
234 static void call_req_handler( struct thread *thread )
236 union generic_reply reply;
237 enum request req = thread->req.request_header.req;
239 current = thread;
240 current->reply_size = 0;
241 clear_error();
242 memset( &reply, 0, sizeof(reply) );
244 if (debug_level) trace_request();
246 if (req < REQ_NB_REQUESTS)
247 req_handlers[req]( &current->req, &reply );
248 else
249 set_error( STATUS_NOT_IMPLEMENTED );
251 if (current)
253 if (current->reply_fd)
255 reply.reply_header.error = current->error;
256 reply.reply_header.reply_size = current->reply_size;
257 if (debug_level) trace_reply( req, &reply );
258 send_reply( &reply );
260 else
262 current->exit_code = 1;
263 kill_thread( current, 1 ); /* no way to continue without reply fd */
266 current = NULL;
269 /* read a request from a thread */
270 void read_request( struct thread *thread )
272 int ret;
274 if (!thread->req_toread) /* no pending request */
276 if ((ret = read( get_unix_fd( thread->request_fd ), &thread->req,
277 sizeof(thread->req) )) != sizeof(thread->req)) goto error;
278 if (!(thread->req_toread = thread->req.request_header.request_size))
280 /* no data, handle request at once */
281 call_req_handler( thread );
282 return;
284 if (!(thread->req_data = malloc( thread->req_toread )))
286 fatal_protocol_error( thread, "no memory for %u bytes request %d\n",
287 thread->req_toread, thread->req.request_header.req );
288 return;
292 /* read the variable sized data */
293 for (;;)
295 ret = read( get_unix_fd( thread->request_fd ),
296 (char *)thread->req_data + thread->req.request_header.request_size
297 - thread->req_toread,
298 thread->req_toread );
299 if (ret <= 0) break;
300 if (!(thread->req_toread -= ret))
302 call_req_handler( thread );
303 free( thread->req_data );
304 thread->req_data = NULL;
305 return;
309 error:
310 if (!ret) /* closed pipe */
311 kill_thread( thread, 0 );
312 else if (ret > 0)
313 fatal_protocol_error( thread, "partial read %d\n", ret );
314 else if (errno != EWOULDBLOCK && errno != EAGAIN)
315 fatal_protocol_error( thread, "read: %s\n", strerror( errno ));
318 /* receive a file descriptor on the process socket */
319 int receive_fd( struct process *process )
321 struct iovec vec;
322 struct send_fd data;
323 struct msghdr msghdr;
324 int fd = -1, ret;
326 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
327 msghdr.msg_accrightslen = sizeof(int);
328 msghdr.msg_accrights = (void *)&fd;
329 #else /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
330 char cmsg_buffer[256];
331 msghdr.msg_control = cmsg_buffer;
332 msghdr.msg_controllen = sizeof(cmsg_buffer);
333 msghdr.msg_flags = 0;
334 #endif /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
336 msghdr.msg_name = NULL;
337 msghdr.msg_namelen = 0;
338 msghdr.msg_iov = &vec;
339 msghdr.msg_iovlen = 1;
340 vec.iov_base = (void *)&data;
341 vec.iov_len = sizeof(data);
343 ret = recvmsg( get_unix_fd( process->msg_fd ), &msghdr, 0 );
345 #ifndef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
346 if (ret > 0)
348 struct cmsghdr *cmsg;
349 for (cmsg = CMSG_FIRSTHDR( &msghdr ); cmsg; cmsg = CMSG_NXTHDR( &msghdr, cmsg ))
351 if (cmsg->cmsg_level != SOL_SOCKET) continue;
352 if (cmsg->cmsg_type == SCM_RIGHTS) fd = *(int *)CMSG_DATA(cmsg);
355 #endif /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
357 if (ret == sizeof(data))
359 struct thread *thread;
361 if (data.tid) thread = get_thread_from_id( data.tid );
362 else thread = (struct thread *)grab_object( get_process_first_thread( process ));
364 if (!thread || thread->process != process || thread->state == TERMINATED)
366 if (debug_level)
367 fprintf( stderr, "%04x: *fd* %d <- %d bad thread id\n",
368 data.tid, data.fd, fd );
369 close( fd );
371 else
373 if (debug_level)
374 fprintf( stderr, "%04x: *fd* %d <- %d\n",
375 thread->id, data.fd, fd );
376 thread_add_inflight_fd( thread, data.fd, fd );
378 if (thread) release_object( thread );
379 return 0;
382 if (!ret)
384 kill_process( process, 0 );
386 else if (ret > 0)
388 fprintf( stderr, "Protocol error: process %04x: partial recvmsg %d for fd\n",
389 process->id, ret );
390 if (fd != -1) close( fd );
391 kill_process( process, 1 );
393 else
395 if (errno != EWOULDBLOCK && errno != EAGAIN)
397 fprintf( stderr, "Protocol error: process %04x: ", process->id );
398 perror( "recvmsg" );
399 kill_process( process, 1 );
402 return -1;
405 /* send an fd to a client */
406 int send_client_fd( struct process *process, int fd, obj_handle_t handle )
408 struct iovec vec;
409 struct msghdr msghdr;
410 int ret;
412 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
413 msghdr.msg_accrightslen = sizeof(fd);
414 msghdr.msg_accrights = (void *)&fd;
415 #else /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
416 char cmsg_buffer[256];
417 struct cmsghdr *cmsg;
418 msghdr.msg_control = cmsg_buffer;
419 msghdr.msg_controllen = sizeof(cmsg_buffer);
420 msghdr.msg_flags = 0;
421 cmsg = CMSG_FIRSTHDR( &msghdr );
422 cmsg->cmsg_len = CMSG_LEN( sizeof(fd) );
423 cmsg->cmsg_level = SOL_SOCKET;
424 cmsg->cmsg_type = SCM_RIGHTS;
425 *(int *)CMSG_DATA(cmsg) = fd;
426 msghdr.msg_controllen = cmsg->cmsg_len;
427 #endif /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
429 msghdr.msg_name = NULL;
430 msghdr.msg_namelen = 0;
431 msghdr.msg_iov = &vec;
432 msghdr.msg_iovlen = 1;
434 vec.iov_base = (void *)&handle;
435 vec.iov_len = sizeof(handle);
437 if (debug_level)
438 fprintf( stderr, "%04x: *fd* %04x -> %d\n", current ? current->id : process->id, handle, fd );
440 ret = sendmsg( get_unix_fd( process->msg_fd ), &msghdr, 0 );
442 if (ret == sizeof(handle)) return 0;
444 if (ret >= 0)
446 fprintf( stderr, "Protocol error: process %04x: partial sendmsg %d\n", process->id, ret );
447 kill_process( process, 1 );
449 else if (errno == EPIPE)
451 kill_process( process, 0 );
453 else
455 fprintf( stderr, "Protocol error: process %04x: ", process->id );
456 perror( "sendmsg" );
457 kill_process( process, 1 );
459 return -1;
462 /* get current tick count to return to client */
463 unsigned int get_tick_count(void)
465 #ifdef HAVE_CLOCK_GETTIME
466 struct timespec ts;
467 #ifdef CLOCK_MONOTONIC_RAW
468 if (!clock_gettime( CLOCK_MONOTONIC_RAW, &ts ))
469 return ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
470 #endif
471 if (!clock_gettime( CLOCK_MONOTONIC, &ts ))
472 return ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
473 #elif defined(__APPLE__)
474 static mach_timebase_info_data_t timebase;
476 if (!timebase.denom) mach_timebase_info( &timebase );
477 return mach_absolute_time() * timebase.numer / timebase.denom / 1000000;
478 #endif
479 return (current_time - server_start_time) / 10000;
482 static void master_socket_dump( struct object *obj, int verbose )
484 struct master_socket *sock = (struct master_socket *)obj;
485 assert( obj->ops == &master_socket_ops );
486 fprintf( stderr, "Master socket fd=%p\n", sock->fd );
489 static void master_socket_destroy( struct object *obj )
491 struct master_socket *sock = (struct master_socket *)obj;
492 assert( obj->ops == &master_socket_ops );
493 release_object( sock->fd );
496 /* handle a socket event */
497 static void master_socket_poll_event( struct fd *fd, int event )
499 struct master_socket *sock = get_fd_user( fd );
500 assert( master_socket->obj.ops == &master_socket_ops );
502 assert( sock == master_socket ); /* there is only one master socket */
504 if (event & (POLLERR | POLLHUP))
506 /* this is not supposed to happen */
507 fprintf( stderr, "wineserver: Error on master socket\n" );
508 set_fd_events( sock->fd, -1 );
510 else if (event & POLLIN)
512 struct sockaddr_un dummy;
513 socklen_t len = sizeof(dummy);
514 int client = accept( get_unix_fd( master_socket->fd ), (struct sockaddr *) &dummy, &len );
515 if (client == -1) return;
516 fcntl( client, F_SETFL, O_NONBLOCK );
517 create_process( client, NULL, 0 );
521 /* remove the socket upon exit */
522 static void socket_cleanup(void)
524 static int do_it_once;
525 if (!do_it_once++) unlink( server_socket_name );
528 /* create a directory and check its permissions */
529 static void create_dir( const char *name, struct stat *st )
531 if (lstat( name, st ) == -1)
533 if (errno != ENOENT)
534 fatal_error( "lstat %s: %s", name, strerror( errno ));
535 if (mkdir( name, 0700 ) == -1 && errno != EEXIST)
536 fatal_error( "mkdir %s: %s\n", name, strerror( errno ));
537 if (lstat( name, st ) == -1)
538 fatal_error( "lstat %s: %s\n", name, strerror( errno ));
540 if (!S_ISDIR(st->st_mode)) fatal_error( "%s is not a directory\n", name );
541 if (st->st_uid != getuid()) fatal_error( "%s is not owned by you\n", name );
542 if (st->st_mode & 077) fatal_error( "%s must not be accessible by other users\n", name );
545 /* create the server directory and chdir to it */
546 static void create_server_dir( const char *dir )
548 char *p, *server_dir;
549 struct stat st, st2;
551 if (!(server_dir = strdup( dir ))) fatal_error( "out of memory\n" );
553 /* first create the base directory if needed */
555 p = strrchr( server_dir, '/' );
556 *p = 0;
557 create_dir( server_dir, &st );
559 /* now create the server directory */
561 *p = '/';
562 create_dir( server_dir, &st );
564 if (chdir( server_dir ) == -1)
565 fatal_error( "chdir %s: %s\n", server_dir, strerror( errno ));
566 if ((server_dir_fd = open( ".", O_RDONLY )) == -1)
567 fatal_error( "open %s: %s\n", server_dir, strerror( errno ));
568 if (fstat( server_dir_fd, &st2 ) == -1)
569 fatal_error( "stat %s: %s\n", server_dir, strerror( errno ));
570 if (st.st_dev != st2.st_dev || st.st_ino != st2.st_ino)
571 fatal_error( "chdir did not end up in %s\n", server_dir );
573 free( server_dir );
576 /* create the lock file and return its file descriptor */
577 static int create_server_lock(void)
579 struct stat st;
580 int fd;
582 if (lstat( server_lock_name, &st ) == -1)
584 if (errno != ENOENT)
585 fatal_error( "lstat %s/%s: %s", wine_get_server_dir(), server_lock_name, strerror( errno ));
587 else
589 if (!S_ISREG(st.st_mode))
590 fatal_error( "%s/%s is not a regular file\n", wine_get_server_dir(), server_lock_name );
593 if ((fd = open( server_lock_name, O_CREAT|O_TRUNC|O_WRONLY, 0600 )) == -1)
594 fatal_error( "error creating %s/%s: %s", wine_get_server_dir(), server_lock_name, strerror( errno ));
595 return fd;
598 /* wait for the server lock */
599 int wait_for_lock(void)
601 const char *server_dir = wine_get_server_dir();
602 int fd, r;
603 struct flock fl;
605 if (!server_dir) return 0; /* no server dir, so no lock to wait on */
607 create_server_dir( server_dir );
608 fd = create_server_lock();
610 fl.l_type = F_WRLCK;
611 fl.l_whence = SEEK_SET;
612 fl.l_start = 0;
613 fl.l_len = 1;
614 r = fcntl( fd, F_SETLKW, &fl );
615 close(fd);
617 return r;
620 /* kill the wine server holding the lock */
621 int kill_lock_owner( int sig )
623 const char *server_dir = wine_get_server_dir();
624 int fd, i, ret = 0;
625 pid_t pid = 0;
626 struct flock fl;
628 if (!server_dir) return 0; /* no server dir, nothing to do */
630 create_server_dir( server_dir );
631 fd = create_server_lock();
633 for (i = 1; i <= 20; i++)
635 fl.l_type = F_WRLCK;
636 fl.l_whence = SEEK_SET;
637 fl.l_start = 0;
638 fl.l_len = 1;
639 if (fcntl( fd, F_GETLK, &fl ) == -1) goto done;
640 if (fl.l_type != F_WRLCK) goto done; /* the file is not locked */
641 if (!pid) /* first time around */
643 if (!(pid = fl.l_pid)) goto done; /* shouldn't happen */
644 if (sig == -1)
646 if (kill( pid, SIGINT ) == -1) goto done;
647 kill( pid, SIGCONT );
648 ret = 1;
650 else /* just send the specified signal and return */
652 ret = (kill( pid, sig ) != -1);
653 goto done;
656 else if (fl.l_pid != pid) goto done; /* no longer the same process */
657 usleep( 50000 * i );
659 /* waited long enough, now kill it */
660 kill( pid, SIGKILL );
662 done:
663 close( fd );
664 return ret;
667 /* acquire the main server lock */
668 static void acquire_lock(void)
670 struct sockaddr_un addr;
671 struct stat st;
672 struct flock fl;
673 int fd, slen, got_lock = 0;
675 fd = create_server_lock();
677 fl.l_type = F_WRLCK;
678 fl.l_whence = SEEK_SET;
679 fl.l_start = 0;
680 fl.l_len = 1;
681 if (fcntl( fd, F_SETLK, &fl ) != -1)
683 /* check for crashed server */
684 if (stat( server_socket_name, &st ) != -1 && /* there is a leftover socket */
685 stat( "core", &st ) != -1 && st.st_size) /* and there is a non-empty core file */
687 fprintf( stderr,
688 "Warning: a previous instance of the wine server seems to have crashed.\n"
689 "Please run 'gdb %s %s/core',\n"
690 "type 'backtrace' at the gdb prompt and report the results. Thanks.\n\n",
691 server_argv0, wine_get_server_dir() );
693 unlink( server_socket_name ); /* we got the lock, we can safely remove the socket */
694 got_lock = 1;
695 /* in that case we reuse fd without closing it, this ensures
696 * that we hold the lock until the process exits */
698 else
700 switch(errno)
702 case ENOLCK:
703 break;
704 case EACCES:
705 /* check whether locks work at all on this file system */
706 if (fcntl( fd, F_GETLK, &fl ) == -1) break;
707 /* fall through */
708 case EAGAIN:
709 exit(2); /* we didn't get the lock, exit with special status */
710 default:
711 fatal_error( "fcntl %s/%s: %s", wine_get_server_dir(), server_lock_name, strerror( errno ));
713 /* it seems we can't use locks on this fs, so we will use the socket existence as lock */
714 close( fd );
717 if ((fd = socket( AF_UNIX, SOCK_STREAM, 0 )) == -1) fatal_error( "socket: %s\n", strerror( errno ));
718 addr.sun_family = AF_UNIX;
719 strcpy( addr.sun_path, server_socket_name );
720 slen = sizeof(addr) - sizeof(addr.sun_path) + strlen(addr.sun_path) + 1;
721 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
722 addr.sun_len = slen;
723 #endif
724 if (bind( fd, (struct sockaddr *)&addr, slen ) == -1)
726 if ((errno == EEXIST) || (errno == EADDRINUSE))
728 if (got_lock)
729 fatal_error( "couldn't bind to the socket even though we hold the lock\n" );
730 exit(2); /* we didn't get the lock, exit with special status */
732 fatal_error( "bind: %s\n", strerror( errno ));
734 atexit( socket_cleanup );
735 chmod( server_socket_name, 0600 ); /* make sure no other user can connect */
736 if (listen( fd, 5 ) == -1) fatal_error( "listen: %s\n", strerror( errno ));
738 if (!(master_socket = alloc_object( &master_socket_ops )) ||
739 !(master_socket->fd = create_anonymous_fd( &master_socket_fd_ops, fd, &master_socket->obj, 0 )))
740 fatal_error( "out of memory\n" );
741 set_fd_events( master_socket->fd, POLLIN );
742 make_object_static( &master_socket->obj );
745 /* open the master server socket and start waiting for new clients */
746 void open_master_socket(void)
748 const char *server_dir = wine_get_server_dir();
749 const char *config_dir = wine_get_config_dir();
750 int fd, pid, status, sync_pipe[2];
751 char dummy;
753 /* make sure no request is larger than the maximum size */
754 assert( sizeof(union generic_request) == sizeof(struct request_max_size) );
755 assert( sizeof(union generic_reply) == sizeof(struct request_max_size) );
757 /* make sure the stdio fds are open */
758 fd = open( "/dev/null", O_RDWR );
759 while (fd >= 0 && fd <= 2) fd = dup( fd );
761 if (!server_dir)
762 fatal_error( "directory %s cannot be accessed\n", config_dir );
763 if (chdir( config_dir ) == -1)
764 fatal_error( "chdir to %s: %s\n", config_dir, strerror( errno ));
765 if ((config_dir_fd = open( ".", O_RDONLY )) == -1)
766 fatal_error( "open %s: %s\n", config_dir, strerror( errno ));
768 create_server_dir( server_dir );
770 if (!foreground)
772 if (pipe( sync_pipe ) == -1) fatal_error( "pipe: %s\n", strerror( errno ));
773 pid = fork();
774 switch( pid )
776 case 0: /* child */
777 setsid();
778 close( sync_pipe[0] );
780 acquire_lock();
782 /* close stdin and stdout */
783 dup2( fd, 0 );
784 dup2( fd, 1 );
786 /* signal parent */
787 dummy = 0;
788 write( sync_pipe[1], &dummy, 1 );
789 close( sync_pipe[1] );
790 break;
792 case -1:
793 fatal_error( "fork: %s\n", strerror( errno ));
794 break;
796 default: /* parent */
797 close( sync_pipe[1] );
799 /* wait for child to signal us and then exit */
800 if (read( sync_pipe[0], &dummy, 1 ) == 1) _exit(0);
802 /* child terminated, propagate exit status */
803 waitpid( pid, &status, 0 );
804 if (WIFEXITED(status)) _exit( WEXITSTATUS(status) );
805 _exit(1);
808 else /* remain in the foreground */
810 acquire_lock();
813 /* init the process tracing mechanism */
814 init_tracing_mechanism();
815 close( fd );
818 /* master socket timer expiration handler */
819 static void close_socket_timeout( void *arg )
821 master_timeout = NULL;
822 flush_registry();
823 if (debug_level) fprintf( stderr, "wineserver: exiting (pid=%ld)\n", (long) getpid() );
825 #ifdef DEBUG_OBJECTS
826 close_objects(); /* shut down everything properly */
827 #endif
828 exit( 0 );
831 /* close the master socket and stop waiting for new clients */
832 void close_master_socket( timeout_t timeout )
834 if (master_socket)
836 release_object( master_socket );
837 master_socket = NULL;
839 if (master_timeout) /* cancel previous timeout */
840 remove_timeout_user( master_timeout );
842 master_timeout = add_timeout_user( timeout, close_socket_timeout, NULL );