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