Changed fd operations to take a struct fd instead of a struct object.
[wine] / server / request.c
1 /*
2  * Server-side request handling
3  *
4  * Copyright (C) 1998 Alexandre Julliard
5  *
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
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
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
52 #include "windef.h"
53 #include "winbase.h"
54 #include "wincon.h"
55 #include "wine/library.h"
56
57 #include "file.h"
58 #include "handle.h"
59 #include "thread.h"
60 #include "process.h"
61 #include "user.h"
62 #define WANT_REQUEST_HANDLERS
63 #include "request.h"
64
65 /* Some versions of glibc don't define this */
66 #ifndef SCM_RIGHTS
67 #define SCM_RIGHTS 1
68 #endif
69
70 /* path names for server master Unix socket */
71 static const char * const server_socket_name = "socket";   /* name of the socket file */
72 static const char * const server_lock_name = "lock";       /* name of the server lock file */
73
74 struct master_socket
75 {
76     struct object        obj;         /* object header */
77     struct timeout_user *timeout;    /* timeout on last process exit */
78 };
79
80 static void master_socket_dump( struct object *obj, int verbose );
81 static void master_socket_poll_event( struct fd *fd, int event );
82
83 static const struct object_ops master_socket_ops =
84 {
85     sizeof(struct master_socket),  /* size */
86     master_socket_dump,            /* dump */
87     no_add_queue,                  /* add_queue */
88     NULL,                          /* remove_queue */
89     NULL,                          /* signaled */
90     NULL,                          /* satisfied */
91     no_get_fd,                     /* get_fd */
92     no_destroy                     /* destroy */
93 };
94
95 static const struct fd_ops master_socket_fd_ops =
96 {
97     NULL,                          /* get_poll_events */
98     master_socket_poll_event,      /* poll_event */
99     no_flush,                      /* flush */
100     no_get_file_info,              /* get_file_info */
101     no_queue_async                 /* queue_async */
102 };
103
104
105 struct thread *current = NULL;  /* thread handling the current request */
106 unsigned int global_error = 0;  /* global error code for when no thread is current */
107 unsigned int server_start_ticks = 0;  /* tick count offset from server startup */
108
109 static struct master_socket *master_socket;  /* the master socket object */
110
111 /* socket communication static structures */
112 static struct iovec myiovec;
113 static struct msghdr msghdr;
114 #ifndef HAVE_MSGHDR_ACCRIGHTS
115 struct cmsg_fd
116 {
117     int len;   /* sizeof structure */
118     int level; /* SOL_SOCKET */
119     int type;  /* SCM_RIGHTS */
120     int fd;    /* fd to pass */
121 };
122 static struct cmsg_fd cmsg = { sizeof(cmsg), SOL_SOCKET, SCM_RIGHTS, -1 };
123 #endif  /* HAVE_MSGHDR_ACCRIGHTS */
124
125 /* complain about a protocol error and terminate the client connection */
126 void fatal_protocol_error( struct thread *thread, const char *err, ... )
127 {
128     va_list args;
129
130     va_start( args, err );
131     fprintf( stderr, "Protocol error:%p: ", thread );
132     vfprintf( stderr, err, args );
133     va_end( args );
134     thread->exit_code = 1;
135     kill_thread( thread, 1 );
136 }
137
138 /* complain about a protocol error and terminate the client connection */
139 void fatal_protocol_perror( struct thread *thread, const char *err, ... )
140 {
141     va_list args;
142
143     va_start( args, err );
144     fprintf( stderr, "Protocol error:%p: ", thread );
145     vfprintf( stderr, err, args );
146     perror( " " );
147     va_end( args );
148     thread->exit_code = 1;
149     kill_thread( thread, 1 );
150 }
151
152 /* die on a fatal error */
153 void fatal_error( const char *err, ... )
154 {
155     va_list args;
156
157     va_start( args, err );
158     fprintf( stderr, "wineserver: " );
159     vfprintf( stderr, err, args );
160     va_end( args );
161     exit(1);
162 }
163
164 /* die on a fatal error */
165 void fatal_perror( const char *err, ... )
166 {
167     va_list args;
168
169     va_start( args, err );
170     fprintf( stderr, "wineserver: " );
171     vfprintf( stderr, err, args );
172     perror( " " );
173     va_end( args );
174     exit(1);
175 }
176
177 /* allocate the reply data */
178 void *set_reply_data_size( size_t size )
179 {
180     assert( size <= get_reply_max_size() );
181     if (size && !(current->reply_data = mem_alloc( size ))) size = 0;
182     current->reply_size = size;
183     return current->reply_data;
184 }
185
186 /* write the remaining part of the reply */
187 void write_reply( struct thread *thread )
188 {
189     int ret;
190
191     if ((ret = write( thread->reply_fd,
192                       (char *)thread->reply_data + thread->reply_size - thread->reply_towrite,
193                       thread->reply_towrite )) >= 0)
194     {
195         if (!(thread->reply_towrite -= ret))
196         {
197             free( thread->reply_data );
198             thread->reply_data = NULL;
199             /* sent everything, can go back to waiting for requests */
200             change_select_fd( &thread->obj, thread->request_fd, POLLIN );
201         }
202         return;
203     }
204     if (errno == EPIPE)
205         kill_thread( thread, 0 );  /* normal death */
206     else if (errno != EWOULDBLOCK && errno != EAGAIN)
207         fatal_protocol_perror( thread, "reply write" );
208 }
209
210 /* send a reply to the current thread */
211 static void send_reply( union generic_reply *reply )
212 {
213     int ret;
214
215     if (!current->reply_size)
216     {
217         if ((ret = write( current->reply_fd, reply, sizeof(*reply) )) != sizeof(*reply)) goto error;
218     }
219     else
220     {
221         struct iovec vec[2];
222
223         vec[0].iov_base = (void *)reply;
224         vec[0].iov_len  = sizeof(*reply);
225         vec[1].iov_base = current->reply_data;
226         vec[1].iov_len  = current->reply_size;
227
228         if ((ret = writev( current->reply_fd, vec, 2 )) < sizeof(*reply)) goto error;
229
230         if ((current->reply_towrite = current->reply_size - (ret - sizeof(*reply))))
231         {
232             /* couldn't write it all, wait for POLLOUT */
233             change_select_fd( &current->obj, current->reply_fd, POLLOUT );
234             return;
235         }
236     }
237     if (current->reply_data)
238     {
239         free( current->reply_data );
240         current->reply_data = NULL;
241     }
242     return;
243
244  error:
245     if (ret >= 0)
246         fatal_protocol_error( current, "partial write %d\n", ret );
247     else if (errno == EPIPE)
248         kill_thread( current, 0 );  /* normal death */
249     else
250         fatal_protocol_perror( current, "reply write" );
251 }
252
253 /* call a request handler */
254 static void call_req_handler( struct thread *thread )
255 {
256     union generic_reply reply;
257     enum request req = thread->req.request_header.req;
258
259     current = thread;
260     current->reply_size = 0;
261     clear_error();
262     memset( &reply, 0, sizeof(reply) );
263
264     if (debug_level) trace_request();
265
266     if (req < REQ_NB_REQUESTS)
267     {
268         req_handlers[req]( &current->req, &reply );
269         if (current)
270         {
271             reply.reply_header.error = current->error;
272             reply.reply_header.reply_size = current->reply_size;
273             if (debug_level) trace_reply( req, &reply );
274             send_reply( &reply );
275         }
276         current = NULL;
277         return;
278     }
279     fatal_protocol_error( current, "bad request %d\n", req );
280 }
281
282 /* read a request from a thread */
283 void read_request( struct thread *thread )
284 {
285     int ret;
286
287     if (!thread->req_toread)  /* no pending request */
288     {
289         if ((ret = read( thread->obj.fd, &thread->req,
290                          sizeof(thread->req) )) != sizeof(thread->req)) goto error;
291         if (!(thread->req_toread = thread->req.request_header.request_size))
292         {
293             /* no data, handle request at once */
294             call_req_handler( thread );
295             return;
296         }
297         if (!(thread->req_data = malloc( thread->req_toread )))
298             fatal_protocol_error( thread, "no memory for %d bytes request\n", thread->req_toread );
299     }
300
301     /* read the variable sized data */
302     for (;;)
303     {
304         ret = read( thread->obj.fd, ((char *)thread->req_data +
305                                      thread->req.request_header.request_size - thread->req_toread),
306                     thread->req_toread );
307         if (ret <= 0) break;
308         if (!(thread->req_toread -= ret))
309         {
310             call_req_handler( thread );
311             free( thread->req_data );
312             thread->req_data = NULL;
313             return;
314         }
315     }
316
317 error:
318     if (!ret)  /* closed pipe */
319         kill_thread( thread, 0 );
320     else if (ret > 0)
321         fatal_protocol_error( thread, "partial read %d\n", ret );
322     else if (errno != EWOULDBLOCK && errno != EAGAIN)
323         fatal_protocol_perror( thread, "read" );
324 }
325
326 /* receive a file descriptor on the process socket */
327 int receive_fd( struct process *process )
328 {
329     struct send_fd data;
330     int fd, ret;
331
332 #ifdef HAVE_MSGHDR_ACCRIGHTS
333     msghdr.msg_accrightslen = sizeof(int);
334     msghdr.msg_accrights = (void *)&fd;
335 #else  /* HAVE_MSGHDR_ACCRIGHTS */
336     msghdr.msg_control    = &cmsg;
337     msghdr.msg_controllen = sizeof(cmsg);
338     cmsg.fd = -1;
339 #endif  /* HAVE_MSGHDR_ACCRIGHTS */
340
341     myiovec.iov_base = (void *)&data;
342     myiovec.iov_len  = sizeof(data);
343
344     ret = recvmsg( process->obj.fd, &msghdr, 0 );
345 #ifndef HAVE_MSGHDR_ACCRIGHTS
346     fd = cmsg.fd;
347 #endif
348
349     if (ret == sizeof(data))
350     {
351         struct thread *thread;
352
353         if (data.tid) thread = get_thread_from_id( data.tid );
354         else thread = (struct thread *)grab_object( process->thread_list );
355
356         if (!thread || thread->process != process || thread->state == TERMINATED)
357         {
358             if (debug_level)
359                 fprintf( stderr, "%04x: *fd* %d <- %d bad thread id\n",
360                          data.tid, data.fd, fd );
361             close( fd );
362         }
363         else
364         {
365             if (debug_level)
366                 fprintf( stderr, "%04x: *fd* %d <- %d\n",
367                          thread->id, data.fd, fd );
368             thread_add_inflight_fd( thread, data.fd, fd );
369         }
370         if (thread) release_object( thread );
371         return 0;
372     }
373
374     if (ret >= 0)
375     {
376         if (ret > 0)
377             fprintf( stderr, "Protocol error: process %p: partial recvmsg %d for fd\n",
378                      process, ret );
379         kill_process( process, NULL, 1 );
380     }
381     else
382     {
383         if (errno != EWOULDBLOCK && errno != EAGAIN)
384         {
385             fprintf( stderr, "Protocol error: process %p: ", process );
386             perror( "recvmsg" );
387             kill_process( process, NULL, 1 );
388         }
389     }
390     return -1;
391 }
392
393 /* send an fd to a client */
394 int send_client_fd( struct process *process, int fd, obj_handle_t handle )
395 {
396     int ret;
397
398     if (debug_level)
399         fprintf( stderr, "%04x: *fd* %p -> %d\n",
400                  current ? current->id : process->id, handle, fd );
401
402 #ifdef HAVE_MSGHDR_ACCRIGHTS
403     msghdr.msg_accrightslen = sizeof(fd);
404     msghdr.msg_accrights = (void *)&fd;
405 #else  /* HAVE_MSGHDR_ACCRIGHTS */
406     msghdr.msg_control    = &cmsg;
407     msghdr.msg_controllen = sizeof(cmsg);
408     cmsg.fd = fd;
409 #endif  /* HAVE_MSGHDR_ACCRIGHTS */
410
411     myiovec.iov_base = (void *)&handle;
412     myiovec.iov_len  = sizeof(handle);
413
414     ret = sendmsg( process->obj.fd, &msghdr, 0 );
415
416     if (ret == sizeof(handle)) return 0;
417
418     if (ret >= 0)
419     {
420         fprintf( stderr, "Protocol error: process %p: partial sendmsg %d\n", process, ret );
421         kill_process( process, NULL, 1 );
422     }
423     else if (errno == EPIPE)
424     {
425         kill_process( process, NULL, 0 );
426     }
427     else
428     {
429         fprintf( stderr, "Protocol error: process %p: ", process );
430         perror( "sendmsg" );
431         kill_process( process, NULL, 1 );
432     }
433     return -1;
434 }
435
436 /* get current tick count to return to client */
437 unsigned int get_tick_count(void)
438 {
439     struct timeval t;
440     gettimeofday( &t, NULL );
441     return (t.tv_sec * 1000) + (t.tv_usec / 1000) - server_start_ticks;
442 }
443
444 static void master_socket_dump( struct object *obj, int verbose )
445 {
446     struct master_socket *sock = (struct master_socket *)obj;
447     assert( obj->ops == &master_socket_ops );
448     fprintf( stderr, "Master socket fd=%p\n", sock->obj.fd_obj );
449 }
450
451 /* handle a socket event */
452 static void master_socket_poll_event( struct fd *fd, int event )
453 {
454     struct master_socket *sock = get_fd_user( fd );
455     assert( master_socket->obj.ops == &master_socket_ops );
456
457     assert( sock == master_socket );  /* there is only one master socket */
458
459     if (event & (POLLERR | POLLHUP))
460     {
461         /* this is not supposed to happen */
462         fprintf( stderr, "wineserver: Error on master socket\n" );
463         release_object( sock );
464     }
465     else if (event & POLLIN)
466     {
467         struct sockaddr_un dummy;
468         int len = sizeof(dummy);
469         int client = accept( master_socket->obj.fd, (struct sockaddr *) &dummy, &len );
470         if (client == -1) return;
471         if (sock->timeout)
472         {
473             remove_timeout_user( sock->timeout );
474             sock->timeout = NULL;
475         }
476         fcntl( client, F_SETFL, O_NONBLOCK );
477         create_process( client );
478     }
479 }
480
481 /* remove the socket upon exit */
482 static void socket_cleanup(void)
483 {
484     static int do_it_once;
485     if (!do_it_once++) unlink( server_socket_name );
486 }
487
488 /* create a directory and check its permissions */
489 static void create_dir( const char *name, struct stat *st )
490 {
491     if (lstat( name, st ) == -1)
492     {
493         if (errno != ENOENT) fatal_perror( "lstat %s", name );
494         if (mkdir( name, 0700 ) == -1) fatal_perror( "mkdir %s", name );
495         if (lstat( name, st ) == -1) fatal_perror( "lstat %s", name );
496     }
497     if (!S_ISDIR(st->st_mode)) fatal_error( "%s is not a directory\n", name );
498     if (st->st_uid != getuid()) fatal_error( "%s is not owned by you\n", name );
499     if (st->st_mode & 077) fatal_error( "%s must not be accessible by other users\n", name );
500 }
501
502 /* create the server directory and chdir to it */
503 static void create_server_dir(void)
504 {
505     char *p, *server_dir;
506     struct stat st, st2;
507
508     if (!(server_dir = strdup( wine_get_server_dir() ))) fatal_error( "out of memory\n" );
509
510     /* first create the base directory if needed */
511
512     p = strrchr( server_dir, '/' );
513     *p = 0;
514     create_dir( server_dir, &st );
515
516     /* now create the server directory */
517
518     *p = '/';
519     create_dir( server_dir, &st );
520
521     if (chdir( server_dir ) == -1) fatal_perror( "chdir %s", server_dir );
522     if (stat( ".", &st2 ) == -1) fatal_perror( "stat %s", server_dir );
523     if (st.st_dev != st2.st_dev || st.st_ino != st2.st_ino)
524         fatal_error( "chdir did not end up in %s\n", server_dir );
525
526     free( server_dir );
527 }
528
529 /* create the lock file and return its file descriptor */
530 static int create_server_lock(void)
531 {
532     struct stat st;
533     int fd;
534
535     if (lstat( server_lock_name, &st ) == -1)
536     {
537         if (errno != ENOENT)
538             fatal_perror( "lstat %s/%s", wine_get_server_dir(), server_lock_name );
539     }
540     else
541     {
542         if (!S_ISREG(st.st_mode))
543             fatal_error( "%s/%s is not a regular file\n", wine_get_server_dir(), server_lock_name );
544     }
545
546     if ((fd = open( server_lock_name, O_CREAT|O_TRUNC|O_WRONLY, 0600 )) == -1)
547         fatal_perror( "error creating %s/%s", wine_get_server_dir(), server_lock_name );
548     return fd;
549 }
550
551 /* wait for the server lock */
552 int wait_for_lock(void)
553 {
554     int fd, r;
555     struct flock fl;
556
557     create_server_dir();
558     fd = create_server_lock();
559
560     fl.l_type   = F_WRLCK;
561     fl.l_whence = SEEK_SET;
562     fl.l_start  = 0;
563     fl.l_len    = 1;
564     r = fcntl( fd, F_SETLKW, &fl );
565     close(fd);
566
567     return r;
568 }
569
570 /* kill the wine server holding the lock */
571 int kill_lock_owner( int sig )
572 {
573     int fd, i, ret = 0;
574     pid_t pid = 0;
575     struct flock fl;
576
577     create_server_dir();
578     fd = create_server_lock();
579
580     for (i = 0; i < 10; i++)
581     {
582         fl.l_type   = F_WRLCK;
583         fl.l_whence = SEEK_SET;
584         fl.l_start  = 0;
585         fl.l_len    = 1;
586         if (fcntl( fd, F_GETLK, &fl ) == -1) goto done;
587         if (fl.l_type != F_WRLCK) goto done;  /* the file is not locked */
588         if (!pid)  /* first time around */
589         {
590             if (!(pid = fl.l_pid)) goto done;  /* shouldn't happen */
591             if (sig == -1)
592             {
593                 if (kill( pid, SIGINT ) == -1) goto done;
594                 kill( pid, SIGCONT );
595                 ret = 1;
596             }
597             else  /* just send the specified signal and return */
598             {
599                 ret = (kill( pid, sig ) != -1);
600                 goto done;
601             }
602         }
603         else if (fl.l_pid != pid) goto done;  /* no longer the same process */
604         sleep( 1 );
605     }
606     /* waited long enough, now kill it */
607     kill( pid, SIGKILL );
608
609  done:
610     close( fd );
611     return ret;
612 }
613
614 /* acquire the main server lock */
615 static void acquire_lock(void)
616 {
617     struct sockaddr_un addr;
618     struct stat st;
619     struct flock fl;
620     int fd, slen, got_lock = 0;
621
622     fd = create_server_lock();
623
624     fl.l_type   = F_WRLCK;
625     fl.l_whence = SEEK_SET;
626     fl.l_start  = 0;
627     fl.l_len    = 1;
628     if (fcntl( fd, F_SETLK, &fl ) != -1)
629     {
630         /* check for crashed server */
631         if (stat( server_socket_name, &st ) != -1 &&   /* there is a leftover socket */
632             stat( "core", &st ) != -1 && st.st_size)   /* and there is a non-empty core file */
633         {
634             fprintf( stderr,
635                      "Warning: a previous instance of the wine server seems to have crashed.\n"
636                      "Please run 'gdb %s %s/core',\n"
637                      "type 'backtrace' at the gdb prompt and report the results. Thanks.\n\n",
638                      server_argv0, wine_get_server_dir() );
639         }
640         unlink( server_socket_name ); /* we got the lock, we can safely remove the socket */
641         got_lock = 1;
642         /* in that case we reuse fd without closing it, this ensures
643          * that we hold the lock until the process exits */
644     }
645     else
646     {
647         switch(errno)
648         {
649         case ENOLCK:
650             break;
651         case EACCES:
652             /* check whether locks work at all on this file system */
653             if (fcntl( fd, F_GETLK, &fl ) == -1) break;
654             /* fall through */
655         case EAGAIN:
656             exit(2); /* we didn't get the lock, exit with special status */
657         default:
658             fatal_perror( "fcntl %s/%s", wine_get_server_dir(), server_lock_name );
659         }
660         /* it seems we can't use locks on this fs, so we will use the socket existence as lock */
661         close( fd );
662     }
663
664     if ((fd = socket( AF_UNIX, SOCK_STREAM, 0 )) == -1) fatal_perror( "socket" );
665     addr.sun_family = AF_UNIX;
666     strcpy( addr.sun_path, server_socket_name );
667     slen = sizeof(addr) - sizeof(addr.sun_path) + strlen(addr.sun_path) + 1;
668 #ifdef HAVE_SOCKADDR_SUN_LEN
669     addr.sun_len = slen;
670 #endif
671     if (bind( fd, (struct sockaddr *)&addr, slen ) == -1)
672     {
673         if ((errno == EEXIST) || (errno == EADDRINUSE))
674         {
675             if (got_lock)
676                 fatal_error( "couldn't bind to the socket even though we hold the lock\n" );
677             exit(2); /* we didn't get the lock, exit with special status */
678         }
679         fatal_perror( "bind" );
680     }
681     atexit( socket_cleanup );
682     chmod( server_socket_name, 0600 );  /* make sure no other user can connect */
683     if (listen( fd, 5 ) == -1) fatal_perror( "listen" );
684
685     if (!(master_socket = alloc_fd_object( &master_socket_ops, &master_socket_fd_ops, fd )))
686         fatal_error( "out of memory\n" );
687     master_socket->timeout = NULL;
688     set_select_events( &master_socket->obj, POLLIN );
689 }
690
691 /* open the master server socket and start waiting for new clients */
692 void open_master_socket(void)
693 {
694     int pid, status, sync_pipe[2];
695     char dummy;
696
697     /* make sure no request is larger than the maximum size */
698     assert( sizeof(union generic_request) == sizeof(struct request_max_size) );
699     assert( sizeof(union generic_reply) == sizeof(struct request_max_size) );
700
701     create_server_dir();
702     if (pipe( sync_pipe ) == -1) fatal_perror( "pipe" );
703
704     pid = fork();
705     switch( pid )
706     {
707     case 0:  /* child */
708         setsid();
709         close( sync_pipe[0] );
710
711         acquire_lock();
712
713         /* signal parent */
714         write( sync_pipe[1], &dummy, 1 );
715         close( sync_pipe[1] );
716         break;
717
718     case -1:
719         fatal_perror( "fork" );
720         break;
721
722     default:  /* parent */
723         close( sync_pipe[1] );
724
725         /* wait for child to signal us and then exit */
726         if (read( sync_pipe[0], &dummy, 1 ) == 1) _exit(0);
727
728         /* child terminated, propagate exit status */
729         wait4( pid, &status, 0, NULL );
730         if (WIFEXITED(status)) _exit( WEXITSTATUS(status) );
731         _exit(1);
732     }
733
734     /* setup msghdr structure constant fields */
735     msghdr.msg_name    = NULL;
736     msghdr.msg_namelen = 0;
737     msghdr.msg_iov     = &myiovec;
738     msghdr.msg_iovlen  = 1;
739
740     /* init startup ticks */
741     server_start_ticks = get_tick_count();
742 }
743
744 /* master socket timer expiration handler */
745 static void close_socket_timeout( void *arg )
746 {
747     master_socket->timeout = NULL;
748     flush_registry();
749
750     /* if a new client is waiting, we keep on running */
751     if (check_fd_events( master_socket->obj.fd_obj, POLLIN )) return;
752
753     if (debug_level) fprintf( stderr, "wineserver: exiting (pid=%ld)\n", (long) getpid() );
754
755 #ifdef DEBUG_OBJECTS
756     /* shut down everything properly */
757     release_object( master_socket );
758     close_global_hooks();
759     close_global_handles();
760     close_registry();
761     close_atom_table();
762 #else
763     exit(0);
764 #endif
765 }
766
767 /* close the master socket and stop waiting for new clients */
768 void close_master_socket(void)
769 {
770     struct timeval when;
771
772     if (master_socket_timeout == -1) return;  /* just keep running forever */
773
774     if (master_socket_timeout)
775     {
776         gettimeofday( &when, 0 );
777         add_timeout( &when, master_socket_timeout * 1000 );
778         master_socket->timeout = add_timeout_user( &when, close_socket_timeout, NULL );
779     }
780     else close_socket_timeout( NULL );  /* close it right away */
781 }
782
783 /* lock/unlock the master socket to stop accepting new clients */
784 void lock_master_socket( int locked )
785 {
786     set_select_events( &master_socket->obj, locked ? 0 : POLLIN );
787 }