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