DIB Engine: implement AlphaBlend
[wine/hacks.git] / server / request.c
blob61b76a4a4c8ff408f94fa66ef16b5cd29d3fe48d
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
55 #include "ntstatus.h"
56 #define WIN32_NO_STATUS
57 #include "windef.h"
58 #include "winbase.h"
59 #include "wincon.h"
60 #include "winternl.h"
61 #include "wine/library.h"
63 #include "file.h"
64 #include "process.h"
65 #define WANT_REQUEST_HANDLERS
66 #include "request.h"
68 /* Some versions of glibc don't define this */
69 #ifndef SCM_RIGHTS
70 #define SCM_RIGHTS 1
71 #endif
73 /* path names for server master Unix socket */
74 static const char * const server_socket_name = "socket"; /* name of the socket file */
75 static const char * const server_lock_name = "lock"; /* name of the server lock file */
77 struct master_socket
79 struct object obj; /* object header */
80 struct fd *fd; /* file descriptor of the master socket */
83 static void master_socket_dump( struct object *obj, int verbose );
84 static void master_socket_destroy( struct object *obj );
85 static void master_socket_poll_event( struct fd *fd, int event );
87 static const struct object_ops master_socket_ops =
89 sizeof(struct master_socket), /* size */
90 master_socket_dump, /* dump */
91 no_get_type, /* get_type */
92 no_add_queue, /* add_queue */
93 NULL, /* remove_queue */
94 NULL, /* signaled */
95 NULL, /* satisfied */
96 no_signal, /* signal */
97 no_get_fd, /* get_fd */
98 no_map_access, /* map_access */
99 default_get_sd, /* get_sd */
100 default_set_sd, /* set_sd */
101 no_lookup_name, /* lookup_name */
102 no_open_file, /* open_file */
103 no_close_handle, /* close_handle */
104 master_socket_destroy /* destroy */
107 static const struct fd_ops master_socket_fd_ops =
109 NULL, /* get_poll_events */
110 master_socket_poll_event, /* poll_event */
111 NULL, /* flush */
112 NULL, /* get_fd_type */
113 NULL, /* removable */
114 NULL, /* ioctl */
115 NULL, /* queue_async */
116 NULL, /* async_event */
117 NULL, /* async_terminated */
118 NULL /* cancel_async */
122 struct thread *current = NULL; /* thread handling the current request */
123 unsigned int global_error = 0; /* global error code for when no thread is current */
124 timeout_t server_start_time = 0; /* server startup time */
125 int server_dir_fd = -1; /* file descriptor for the server dir */
126 int config_dir_fd = -1; /* file descriptor for the config dir */
128 static struct master_socket *master_socket; /* the master socket object */
129 static struct timeout_user *master_timeout;
131 /* socket communication static structures */
132 static struct iovec myiovec;
133 static struct msghdr msghdr;
134 #ifndef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
135 struct cmsg_fd
137 struct
139 size_t len; /* size of structure */
140 int level; /* SOL_SOCKET */
141 int type; /* SCM_RIGHTS */
142 } header;
143 int fd; /* fd to pass */
145 static struct cmsg_fd cmsg = { { sizeof(cmsg.header) + sizeof(cmsg.fd), SOL_SOCKET, SCM_RIGHTS }, -1 };
146 #endif /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
148 /* complain about a protocol error and terminate the client connection */
149 void fatal_protocol_error( struct thread *thread, const char *err, ... )
151 va_list args;
153 va_start( args, err );
154 fprintf( stderr, "Protocol error:%04x: ", thread->id );
155 vfprintf( stderr, err, args );
156 va_end( args );
157 thread->exit_code = 1;
158 kill_thread( thread, 1 );
161 /* complain about a protocol error and terminate the client connection */
162 void fatal_protocol_perror( struct thread *thread, const char *err, ... )
164 va_list args;
166 va_start( args, err );
167 fprintf( stderr, "Protocol error:%04x: ", thread->id );
168 vfprintf( stderr, err, args );
169 perror( " " );
170 va_end( args );
171 thread->exit_code = 1;
172 kill_thread( thread, 1 );
175 /* die on a fatal error */
176 void fatal_error( const char *err, ... )
178 va_list args;
180 va_start( args, err );
181 fprintf( stderr, "wineserver: " );
182 vfprintf( stderr, err, args );
183 va_end( args );
184 exit(1);
187 /* die on a fatal error */
188 void fatal_perror( const char *err, ... )
190 va_list args;
192 va_start( args, err );
193 fprintf( stderr, "wineserver: " );
194 vfprintf( stderr, err, args );
195 perror( " " );
196 va_end( args );
197 exit(1);
200 /* allocate the reply data */
201 void *set_reply_data_size( data_size_t size )
203 assert( size <= get_reply_max_size() );
204 if (size && !(current->reply_data = mem_alloc( size ))) size = 0;
205 current->reply_size = size;
206 return current->reply_data;
209 /* write the remaining part of the reply */
210 void write_reply( struct thread *thread )
212 int ret;
214 if ((ret = write( get_unix_fd( thread->reply_fd ),
215 (char *)thread->reply_data + thread->reply_size - thread->reply_towrite,
216 thread->reply_towrite )) >= 0)
218 if (!(thread->reply_towrite -= ret))
220 free( thread->reply_data );
221 thread->reply_data = NULL;
222 /* sent everything, can go back to waiting for requests */
223 set_fd_events( thread->request_fd, POLLIN );
224 set_fd_events( thread->reply_fd, 0 );
226 return;
228 if (errno == EPIPE)
229 kill_thread( thread, 0 ); /* normal death */
230 else if (errno != EWOULDBLOCK && errno != EAGAIN)
231 fatal_protocol_perror( thread, "reply write" );
234 /* send a reply to the current thread */
235 static void send_reply( union generic_reply *reply )
237 int ret;
239 if (!current->reply_size)
241 if ((ret = write( get_unix_fd( current->reply_fd ),
242 reply, sizeof(*reply) )) != sizeof(*reply)) goto error;
244 else
246 struct iovec vec[2];
248 vec[0].iov_base = (void *)reply;
249 vec[0].iov_len = sizeof(*reply);
250 vec[1].iov_base = current->reply_data;
251 vec[1].iov_len = current->reply_size;
253 if ((ret = writev( get_unix_fd( current->reply_fd ), vec, 2 )) < sizeof(*reply)) goto error;
255 if ((current->reply_towrite = current->reply_size - (ret - sizeof(*reply))))
257 /* couldn't write it all, wait for POLLOUT */
258 set_fd_events( current->reply_fd, POLLOUT );
259 set_fd_events( current->request_fd, 0 );
260 return;
263 free( current->reply_data );
264 current->reply_data = NULL;
265 return;
267 error:
268 if (ret >= 0)
269 fatal_protocol_error( current, "partial write %d\n", ret );
270 else if (errno == EPIPE)
271 kill_thread( current, 0 ); /* normal death */
272 else
273 fatal_protocol_perror( current, "reply write" );
276 /* call a request handler */
277 static void call_req_handler( struct thread *thread )
279 union generic_reply reply;
280 enum request req = thread->req.request_header.req;
282 current = thread;
283 current->reply_size = 0;
284 clear_error();
285 memset( &reply, 0, sizeof(reply) );
287 if (debug_level) trace_request();
289 if (req < REQ_NB_REQUESTS)
290 req_handlers[req]( &current->req, &reply );
291 else
292 set_error( STATUS_NOT_IMPLEMENTED );
294 if (current)
296 if (current->reply_fd)
298 reply.reply_header.error = current->error;
299 reply.reply_header.reply_size = current->reply_size;
300 if (debug_level) trace_reply( req, &reply );
301 send_reply( &reply );
303 else
305 current->exit_code = 1;
306 kill_thread( current, 1 ); /* no way to continue without reply fd */
309 current = NULL;
312 /* read a request from a thread */
313 void read_request( struct thread *thread )
315 int ret;
317 if (!thread->req_toread) /* no pending request */
319 if ((ret = read( get_unix_fd( thread->request_fd ), &thread->req,
320 sizeof(thread->req) )) != sizeof(thread->req)) goto error;
321 if (!(thread->req_toread = thread->req.request_header.request_size))
323 /* no data, handle request at once */
324 call_req_handler( thread );
325 return;
327 if (!(thread->req_data = malloc( thread->req_toread )))
329 fatal_protocol_error( thread, "no memory for %u bytes request %d\n",
330 thread->req_toread, thread->req.request_header.req );
331 return;
335 /* read the variable sized data */
336 for (;;)
338 ret = read( get_unix_fd( thread->request_fd ),
339 (char *)thread->req_data + thread->req.request_header.request_size
340 - thread->req_toread,
341 thread->req_toread );
342 if (ret <= 0) break;
343 if (!(thread->req_toread -= ret))
345 call_req_handler( thread );
346 free( thread->req_data );
347 thread->req_data = NULL;
348 return;
352 error:
353 if (!ret) /* closed pipe */
354 kill_thread( thread, 0 );
355 else if (ret > 0)
356 fatal_protocol_error( thread, "partial read %d\n", ret );
357 else if (errno != EWOULDBLOCK && errno != EAGAIN)
358 fatal_protocol_perror( thread, "read" );
361 /* receive a file descriptor on the process socket */
362 int receive_fd( struct process *process )
364 struct send_fd data;
365 int fd, ret;
367 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
368 msghdr.msg_accrightslen = sizeof(int);
369 msghdr.msg_accrights = (void *)&fd;
370 #else /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
371 msghdr.msg_control = &cmsg;
372 msghdr.msg_controllen = sizeof(cmsg.header) + sizeof(fd);
373 cmsg.fd = -1;
374 #endif /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
376 myiovec.iov_base = (void *)&data;
377 myiovec.iov_len = sizeof(data);
379 ret = recvmsg( get_unix_fd( process->msg_fd ), &msghdr, 0 );
380 #ifndef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
381 fd = cmsg.fd;
382 #endif
384 if (ret == sizeof(data))
386 struct thread *thread;
388 if (data.tid) thread = get_thread_from_id( data.tid );
389 else thread = (struct thread *)grab_object( get_process_first_thread( process ));
391 if (!thread || thread->process != process || thread->state == TERMINATED)
393 if (debug_level)
394 fprintf( stderr, "%04x: *fd* %d <- %d bad thread id\n",
395 data.tid, data.fd, fd );
396 close( fd );
398 else
400 if (debug_level)
401 fprintf( stderr, "%04x: *fd* %d <- %d\n",
402 thread->id, data.fd, fd );
403 thread_add_inflight_fd( thread, data.fd, fd );
405 if (thread) release_object( thread );
406 return 0;
409 if (!ret)
411 kill_process( process, 0 );
413 else if (ret > 0)
415 fprintf( stderr, "Protocol error: process %04x: partial recvmsg %d for fd\n",
416 process->id, ret );
417 kill_process( process, 1 );
419 else
421 if (errno != EWOULDBLOCK && errno != EAGAIN)
423 fprintf( stderr, "Protocol error: process %04x: ", process->id );
424 perror( "recvmsg" );
425 kill_process( process, 1 );
428 return -1;
431 /* send an fd to a client */
432 int send_client_fd( struct process *process, int fd, obj_handle_t handle )
434 int ret;
436 if (debug_level)
437 fprintf( stderr, "%04x: *fd* %04x -> %d\n",
438 current ? current->id : process->id, handle, fd );
440 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
441 msghdr.msg_accrightslen = sizeof(fd);
442 msghdr.msg_accrights = (void *)&fd;
443 #else /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
444 msghdr.msg_control = &cmsg;
445 msghdr.msg_controllen = sizeof(cmsg.header) + sizeof(fd);
446 cmsg.fd = fd;
447 #endif /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
449 myiovec.iov_base = (void *)&handle;
450 myiovec.iov_len = sizeof(handle);
452 ret = sendmsg( get_unix_fd( process->msg_fd ), &msghdr, 0 );
454 if (ret == sizeof(handle)) return 0;
456 if (ret >= 0)
458 fprintf( stderr, "Protocol error: process %04x: partial sendmsg %d\n", process->id, ret );
459 kill_process( process, 1 );
461 else if (errno == EPIPE)
463 kill_process( process, 0 );
465 else
467 fprintf( stderr, "Protocol error: process %04x: ", process->id );
468 perror( "sendmsg" );
469 kill_process( process, 1 );
471 return -1;
474 /* get current tick count to return to client */
475 unsigned int get_tick_count(void)
477 return (current_time - server_start_time) / 10000;
480 static void master_socket_dump( struct object *obj, int verbose )
482 struct master_socket *sock = (struct master_socket *)obj;
483 assert( obj->ops == &master_socket_ops );
484 fprintf( stderr, "Master socket fd=%p\n", sock->fd );
487 static void master_socket_destroy( struct object *obj )
489 struct master_socket *sock = (struct master_socket *)obj;
490 assert( obj->ops == &master_socket_ops );
491 release_object( sock->fd );
494 /* handle a socket event */
495 static void master_socket_poll_event( struct fd *fd, int event )
497 struct master_socket *sock = get_fd_user( fd );
498 assert( master_socket->obj.ops == &master_socket_ops );
500 assert( sock == master_socket ); /* there is only one master socket */
502 if (event & (POLLERR | POLLHUP))
504 /* this is not supposed to happen */
505 fprintf( stderr, "wineserver: Error on master socket\n" );
506 set_fd_events( sock->fd, -1 );
508 else if (event & POLLIN)
510 struct sockaddr_un dummy;
511 unsigned int len = sizeof(dummy);
512 int client = accept( get_unix_fd( master_socket->fd ), (struct sockaddr *) &dummy, &len );
513 if (client == -1) return;
514 fcntl( client, F_SETFL, O_NONBLOCK );
515 create_process( client, NULL, 0 );
519 /* remove the socket upon exit */
520 static void socket_cleanup(void)
522 static int do_it_once;
523 if (!do_it_once++) unlink( server_socket_name );
526 /* create a directory and check its permissions */
527 static void create_dir( const char *name, struct stat *st )
529 if (lstat( name, st ) == -1)
531 if (errno != ENOENT) fatal_perror( "lstat %s", name );
532 if (mkdir( name, 0700 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", name );
533 if (lstat( name, st ) == -1) fatal_perror( "lstat %s", name );
535 if (!S_ISDIR(st->st_mode)) fatal_error( "%s is not a directory\n", name );
536 if (st->st_uid != getuid()) fatal_error( "%s is not owned by you\n", name );
537 if (st->st_mode & 077) fatal_error( "%s must not be accessible by other users\n", name );
540 /* create the server directory and chdir to it */
541 static void create_server_dir( const char *dir )
543 char *p, *server_dir;
544 struct stat st, st2;
546 if (!(server_dir = strdup( dir ))) fatal_error( "out of memory\n" );
548 /* first create the base directory if needed */
550 p = strrchr( server_dir, '/' );
551 *p = 0;
552 create_dir( server_dir, &st );
554 /* now create the server directory */
556 *p = '/';
557 create_dir( server_dir, &st );
559 if (chdir( server_dir ) == -1) fatal_perror( "chdir %s", server_dir );
560 if ((server_dir_fd = open( ".", O_RDONLY )) == -1) fatal_perror( "open %s", server_dir );
561 if (fstat( server_dir_fd, &st2 ) == -1) fatal_perror( "stat %s", server_dir );
562 if (st.st_dev != st2.st_dev || st.st_ino != st2.st_ino)
563 fatal_error( "chdir did not end up in %s\n", server_dir );
565 free( server_dir );
568 /* create the lock file and return its file descriptor */
569 static int create_server_lock(void)
571 struct stat st;
572 int fd;
574 if (lstat( server_lock_name, &st ) == -1)
576 if (errno != ENOENT)
577 fatal_perror( "lstat %s/%s", wine_get_server_dir(), server_lock_name );
579 else
581 if (!S_ISREG(st.st_mode))
582 fatal_error( "%s/%s is not a regular file\n", wine_get_server_dir(), server_lock_name );
585 if ((fd = open( server_lock_name, O_CREAT|O_TRUNC|O_WRONLY, 0600 )) == -1)
586 fatal_perror( "error creating %s/%s", wine_get_server_dir(), server_lock_name );
587 return fd;
590 /* wait for the server lock */
591 int wait_for_lock(void)
593 const char *server_dir = wine_get_server_dir();
594 int fd, r;
595 struct flock fl;
597 if (!server_dir) return 0; /* no server dir, so no lock to wait on */
599 create_server_dir( server_dir );
600 fd = create_server_lock();
602 fl.l_type = F_WRLCK;
603 fl.l_whence = SEEK_SET;
604 fl.l_start = 0;
605 fl.l_len = 1;
606 r = fcntl( fd, F_SETLKW, &fl );
607 close(fd);
609 return r;
612 /* kill the wine server holding the lock */
613 int kill_lock_owner( int sig )
615 const char *server_dir = wine_get_server_dir();
616 int fd, i, ret = 0;
617 pid_t pid = 0;
618 struct flock fl;
620 if (!server_dir) return 0; /* no server dir, nothing to do */
622 create_server_dir( server_dir );
623 fd = create_server_lock();
625 for (i = 1; i <= 20; i++)
627 fl.l_type = F_WRLCK;
628 fl.l_whence = SEEK_SET;
629 fl.l_start = 0;
630 fl.l_len = 1;
631 if (fcntl( fd, F_GETLK, &fl ) == -1) goto done;
632 if (fl.l_type != F_WRLCK) goto done; /* the file is not locked */
633 if (!pid) /* first time around */
635 if (!(pid = fl.l_pid)) goto done; /* shouldn't happen */
636 if (sig == -1)
638 if (kill( pid, SIGINT ) == -1) goto done;
639 kill( pid, SIGCONT );
640 ret = 1;
642 else /* just send the specified signal and return */
644 ret = (kill( pid, sig ) != -1);
645 goto done;
648 else if (fl.l_pid != pid) goto done; /* no longer the same process */
649 usleep( 50000 * i );
651 /* waited long enough, now kill it */
652 kill( pid, SIGKILL );
654 done:
655 close( fd );
656 return ret;
659 /* acquire the main server lock */
660 static void acquire_lock(void)
662 struct sockaddr_un addr;
663 struct stat st;
664 struct flock fl;
665 int fd, slen, got_lock = 0;
667 fd = create_server_lock();
669 fl.l_type = F_WRLCK;
670 fl.l_whence = SEEK_SET;
671 fl.l_start = 0;
672 fl.l_len = 1;
673 if (fcntl( fd, F_SETLK, &fl ) != -1)
675 /* check for crashed server */
676 if (stat( server_socket_name, &st ) != -1 && /* there is a leftover socket */
677 stat( "core", &st ) != -1 && st.st_size) /* and there is a non-empty core file */
679 fprintf( stderr,
680 "Warning: a previous instance of the wine server seems to have crashed.\n"
681 "Please run 'gdb %s %s/core',\n"
682 "type 'backtrace' at the gdb prompt and report the results. Thanks.\n\n",
683 server_argv0, wine_get_server_dir() );
685 unlink( server_socket_name ); /* we got the lock, we can safely remove the socket */
686 got_lock = 1;
687 /* in that case we reuse fd without closing it, this ensures
688 * that we hold the lock until the process exits */
690 else
692 switch(errno)
694 case ENOLCK:
695 break;
696 case EACCES:
697 /* check whether locks work at all on this file system */
698 if (fcntl( fd, F_GETLK, &fl ) == -1) break;
699 /* fall through */
700 case EAGAIN:
701 exit(2); /* we didn't get the lock, exit with special status */
702 default:
703 fatal_perror( "fcntl %s/%s", wine_get_server_dir(), server_lock_name );
705 /* it seems we can't use locks on this fs, so we will use the socket existence as lock */
706 close( fd );
709 if ((fd = socket( AF_UNIX, SOCK_STREAM, 0 )) == -1) fatal_perror( "socket" );
710 addr.sun_family = AF_UNIX;
711 strcpy( addr.sun_path, server_socket_name );
712 slen = sizeof(addr) - sizeof(addr.sun_path) + strlen(addr.sun_path) + 1;
713 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
714 addr.sun_len = slen;
715 #endif
716 if (bind( fd, (struct sockaddr *)&addr, slen ) == -1)
718 if ((errno == EEXIST) || (errno == EADDRINUSE))
720 if (got_lock)
721 fatal_error( "couldn't bind to the socket even though we hold the lock\n" );
722 exit(2); /* we didn't get the lock, exit with special status */
724 fatal_perror( "bind" );
726 atexit( socket_cleanup );
727 chmod( server_socket_name, 0600 ); /* make sure no other user can connect */
728 if (listen( fd, 5 ) == -1) fatal_perror( "listen" );
730 if (!(master_socket = alloc_object( &master_socket_ops )) ||
731 !(master_socket->fd = create_anonymous_fd( &master_socket_fd_ops, fd, &master_socket->obj, 0 )))
732 fatal_error( "out of memory\n" );
733 set_fd_events( master_socket->fd, POLLIN );
734 make_object_static( &master_socket->obj );
737 /* open the master server socket and start waiting for new clients */
738 void open_master_socket(void)
740 const char *server_dir = wine_get_server_dir();
741 const char *config_dir = wine_get_config_dir();
742 int fd, pid, status, sync_pipe[2];
743 char dummy;
745 /* make sure no request is larger than the maximum size */
746 assert( sizeof(union generic_request) == sizeof(struct request_max_size) );
747 assert( sizeof(union generic_reply) == sizeof(struct request_max_size) );
749 if (!server_dir) fatal_error( "directory %s cannot be accessed\n", config_dir );
750 if (chdir( config_dir ) == -1) fatal_perror( "chdir to %s", config_dir );
751 if ((config_dir_fd = open( ".", O_RDONLY )) == -1) fatal_perror( "open %s", config_dir );
753 create_server_dir( server_dir );
755 if (!foreground)
757 if (pipe( sync_pipe ) == -1) fatal_perror( "pipe" );
758 pid = fork();
759 switch( pid )
761 case 0: /* child */
762 setsid();
763 close( sync_pipe[0] );
765 acquire_lock();
767 /* close stdin and stdout */
768 if ((fd = open( "/dev/null", O_RDWR )) != -1)
770 dup2( fd, 0 );
771 dup2( fd, 1 );
772 close( fd );
775 /* signal parent */
776 dummy = 0;
777 write( sync_pipe[1], &dummy, 1 );
778 close( sync_pipe[1] );
779 break;
781 case -1:
782 fatal_perror( "fork" );
783 break;
785 default: /* parent */
786 close( sync_pipe[1] );
788 /* wait for child to signal us and then exit */
789 if (read( sync_pipe[0], &dummy, 1 ) == 1) _exit(0);
791 /* child terminated, propagate exit status */
792 wait4( pid, &status, 0, NULL );
793 if (WIFEXITED(status)) _exit( WEXITSTATUS(status) );
794 _exit(1);
797 else /* remain in the foreground */
799 acquire_lock();
802 /* setup msghdr structure constant fields */
803 msghdr.msg_name = NULL;
804 msghdr.msg_namelen = 0;
805 msghdr.msg_iov = &myiovec;
806 msghdr.msg_iovlen = 1;
808 /* init the process tracing mechanism */
809 init_tracing_mechanism();
812 /* master socket timer expiration handler */
813 static void close_socket_timeout( void *arg )
815 master_timeout = NULL;
816 flush_registry();
817 if (debug_level) fprintf( stderr, "wineserver: exiting (pid=%ld)\n", (long) getpid() );
819 #ifdef DEBUG_OBJECTS
820 close_objects(); /* shut down everything properly */
821 #endif
822 exit( 0 );
825 /* close the master socket and stop waiting for new clients */
826 void close_master_socket( timeout_t timeout )
828 if (master_socket)
830 release_object( master_socket );
831 master_socket = NULL;
833 if (master_timeout) /* cancel previous timeout */
834 remove_timeout_user( master_timeout );
836 master_timeout = add_timeout_user( timeout, close_socket_timeout, NULL );