mciqtz32: Support MCI_DGV_PUT_DESTINATION.
[wine.git] / server / request.c
blob2debf6dbb7201c58e99e18cf38bce62a6b4cfa8a
1 /*
2 * Server-side request handling
4 * Copyright (C) 1998 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #ifdef HAVE_PWD_H
28 #include <pwd.h>
29 #endif
30 #include <signal.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <stdarg.h>
34 #include <string.h>
35 #include <sys/stat.h>
36 #include <sys/time.h>
37 #include <sys/types.h>
38 #ifdef HAVE_SYS_SOCKET_H
39 # include <sys/socket.h>
40 #endif
41 #ifdef HAVE_SYS_WAIT_H
42 # include <sys/wait.h>
43 #endif
44 #ifdef HAVE_SYS_UIO_H
45 #include <sys/uio.h>
46 #endif
47 #ifdef HAVE_SYS_UN_H
48 #include <sys/un.h>
49 #endif
50 #include <unistd.h>
51 #ifdef HAVE_POLL_H
52 #include <poll.h>
53 #endif
54 #ifdef __APPLE__
55 # include <mach/mach_time.h>
56 #endif
58 #include "ntstatus.h"
59 #define WIN32_NO_STATUS
60 #include "windef.h"
61 #include "winbase.h"
62 #include "wincon.h"
63 #include "winternl.h"
64 #include "wine/library.h"
66 #include "file.h"
67 #include "process.h"
68 #include "thread.h"
69 #include "security.h"
70 #define WANT_REQUEST_HANDLERS
71 #include "request.h"
73 /* Some versions of glibc don't define this */
74 #ifndef SCM_RIGHTS
75 #define SCM_RIGHTS 1
76 #endif
78 /* path names for server master Unix socket */
79 static const char * const server_socket_name = "socket"; /* name of the socket file */
80 static const char * const server_lock_name = "lock"; /* name of the server lock file */
82 struct master_socket
84 struct object obj; /* object header */
85 struct fd *fd; /* file descriptor of the master socket */
88 static void master_socket_dump( struct object *obj, int verbose );
89 static void master_socket_destroy( struct object *obj );
90 static void master_socket_poll_event( struct fd *fd, int event );
92 static const struct object_ops master_socket_ops =
94 sizeof(struct master_socket), /* size */
95 master_socket_dump, /* dump */
96 no_get_type, /* get_type */
97 no_add_queue, /* add_queue */
98 NULL, /* remove_queue */
99 NULL, /* signaled */
100 NULL, /* satisfied */
101 no_signal, /* signal */
102 no_get_fd, /* get_fd */
103 no_map_access, /* map_access */
104 default_get_sd, /* get_sd */
105 default_set_sd, /* set_sd */
106 no_lookup_name, /* lookup_name */
107 no_open_file, /* open_file */
108 no_close_handle, /* close_handle */
109 master_socket_destroy /* destroy */
112 static const struct fd_ops master_socket_fd_ops =
114 NULL, /* get_poll_events */
115 master_socket_poll_event, /* poll_event */
116 NULL, /* flush */
117 NULL, /* get_fd_type */
118 NULL, /* ioctl */
119 NULL, /* queue_async */
120 NULL, /* reselect_async */
121 NULL /* cancel_async */
125 struct thread *current = NULL; /* thread handling the current request */
126 unsigned int global_error = 0; /* global error code for when no thread is current */
127 timeout_t server_start_time = 0; /* server startup time */
128 int server_dir_fd = -1; /* file descriptor for the server dir */
129 int config_dir_fd = -1; /* file descriptor for the config dir */
131 static struct master_socket *master_socket; /* the master socket object */
132 static struct timeout_user *master_timeout;
134 /* complain about a protocol error and terminate the client connection */
135 void fatal_protocol_error( struct thread *thread, const char *err, ... )
137 va_list args;
139 va_start( args, err );
140 fprintf( stderr, "Protocol error:%04x: ", thread->id );
141 vfprintf( stderr, err, args );
142 va_end( args );
143 thread->exit_code = 1;
144 kill_thread( thread, 1 );
147 /* die on a fatal error */
148 void fatal_error( const char *err, ... )
150 va_list args;
152 va_start( args, err );
153 fprintf( stderr, "wineserver: " );
154 vfprintf( stderr, err, args );
155 va_end( args );
156 exit(1);
159 /* allocate the reply data */
160 void *set_reply_data_size( data_size_t size )
162 assert( size <= get_reply_max_size() );
163 if (size && !(current->reply_data = mem_alloc( size ))) size = 0;
164 current->reply_size = size;
165 return current->reply_data;
168 /* return object attributes from the current request */
169 const struct object_attributes *get_req_object_attributes( const struct security_descriptor **sd,
170 struct unicode_str *name )
172 static const struct object_attributes empty_attributes;
173 const struct object_attributes *attr = get_req_data();
174 data_size_t size = get_req_data_size();
176 if (!size)
178 *sd = NULL;
179 name->len = 0;
180 return &empty_attributes;
183 if ((size < sizeof(*attr)) || (size - sizeof(*attr) < attr->sd_len) ||
184 (size - sizeof(*attr) - attr->sd_len < attr->name_len))
186 set_error( STATUS_ACCESS_VIOLATION );
187 return NULL;
189 if (attr->sd_len && !sd_is_valid( (const struct security_descriptor *)(attr + 1), attr->sd_len ))
191 set_error( STATUS_INVALID_SECURITY_DESCR );
192 return NULL;
195 *sd = attr->sd_len ? (const struct security_descriptor *)(attr + 1) : NULL;
196 name->len = (attr->name_len / sizeof(WCHAR)) * sizeof(WCHAR);
197 name->str = (const WCHAR *)(attr + 1) + attr->sd_len / sizeof(WCHAR);
198 return attr;
201 /* write the remaining part of the reply */
202 void write_reply( struct thread *thread )
204 int ret;
206 if ((ret = write( get_unix_fd( thread->reply_fd ),
207 (char *)thread->reply_data + thread->reply_size - thread->reply_towrite,
208 thread->reply_towrite )) >= 0)
210 if (!(thread->reply_towrite -= ret))
212 free( thread->reply_data );
213 thread->reply_data = NULL;
214 /* sent everything, can go back to waiting for requests */
215 set_fd_events( thread->request_fd, POLLIN );
216 set_fd_events( thread->reply_fd, 0 );
218 return;
220 if (errno == EPIPE)
221 kill_thread( thread, 0 ); /* normal death */
222 else if (errno != EWOULDBLOCK && errno != EAGAIN)
223 fatal_protocol_error( thread, "reply write: %s\n", strerror( errno ));
226 /* send a reply to the current thread */
227 static void send_reply( union generic_reply *reply )
229 int ret;
231 if (!current->reply_size)
233 if ((ret = write( get_unix_fd( current->reply_fd ),
234 reply, sizeof(*reply) )) != sizeof(*reply)) goto error;
236 else
238 struct iovec vec[2];
240 vec[0].iov_base = (void *)reply;
241 vec[0].iov_len = sizeof(*reply);
242 vec[1].iov_base = current->reply_data;
243 vec[1].iov_len = current->reply_size;
245 if ((ret = writev( get_unix_fd( current->reply_fd ), vec, 2 )) < sizeof(*reply)) goto error;
247 if ((current->reply_towrite = current->reply_size - (ret - sizeof(*reply))))
249 /* couldn't write it all, wait for POLLOUT */
250 set_fd_events( current->reply_fd, POLLOUT );
251 set_fd_events( current->request_fd, 0 );
252 return;
255 free( current->reply_data );
256 current->reply_data = NULL;
257 return;
259 error:
260 if (ret >= 0)
261 fatal_protocol_error( current, "partial write %d\n", ret );
262 else if (errno == EPIPE)
263 kill_thread( current, 0 ); /* normal death */
264 else
265 fatal_protocol_error( current, "reply write: %s\n", strerror( errno ));
268 /* call a request handler */
269 static void call_req_handler( struct thread *thread )
271 union generic_reply reply;
272 enum request req = thread->req.request_header.req;
274 current = thread;
275 current->reply_size = 0;
276 clear_error();
277 memset( &reply, 0, sizeof(reply) );
279 if (debug_level) trace_request();
281 if (req < REQ_NB_REQUESTS)
282 req_handlers[req]( &current->req, &reply );
283 else
284 set_error( STATUS_NOT_IMPLEMENTED );
286 if (current)
288 if (current->reply_fd)
290 reply.reply_header.error = current->error;
291 reply.reply_header.reply_size = current->reply_size;
292 if (debug_level) trace_reply( req, &reply );
293 send_reply( &reply );
295 else
297 current->exit_code = 1;
298 kill_thread( current, 1 ); /* no way to continue without reply fd */
301 current = NULL;
304 /* read a request from a thread */
305 void read_request( struct thread *thread )
307 int ret;
309 if (!thread->req_toread) /* no pending request */
311 if ((ret = read( get_unix_fd( thread->request_fd ), &thread->req,
312 sizeof(thread->req) )) != sizeof(thread->req)) goto error;
313 if (!(thread->req_toread = thread->req.request_header.request_size))
315 /* no data, handle request at once */
316 call_req_handler( thread );
317 return;
319 if (!(thread->req_data = malloc( thread->req_toread )))
321 fatal_protocol_error( thread, "no memory for %u bytes request %d\n",
322 thread->req_toread, thread->req.request_header.req );
323 return;
327 /* read the variable sized data */
328 for (;;)
330 ret = read( get_unix_fd( thread->request_fd ),
331 (char *)thread->req_data + thread->req.request_header.request_size
332 - thread->req_toread,
333 thread->req_toread );
334 if (ret <= 0) break;
335 if (!(thread->req_toread -= ret))
337 call_req_handler( thread );
338 free( thread->req_data );
339 thread->req_data = NULL;
340 return;
344 error:
345 if (!ret) /* closed pipe */
346 kill_thread( thread, 0 );
347 else if (ret > 0)
348 fatal_protocol_error( thread, "partial read %d\n", ret );
349 else if (errno != EWOULDBLOCK && errno != EAGAIN)
350 fatal_protocol_error( thread, "read: %s\n", strerror( errno ));
353 /* receive a file descriptor on the process socket */
354 int receive_fd( struct process *process )
356 struct iovec vec;
357 struct send_fd data;
358 struct msghdr msghdr;
359 int fd = -1, ret;
361 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
362 msghdr.msg_accrightslen = sizeof(int);
363 msghdr.msg_accrights = (void *)&fd;
364 #else /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
365 char cmsg_buffer[256];
366 msghdr.msg_control = cmsg_buffer;
367 msghdr.msg_controllen = sizeof(cmsg_buffer);
368 msghdr.msg_flags = 0;
369 #endif /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
371 msghdr.msg_name = NULL;
372 msghdr.msg_namelen = 0;
373 msghdr.msg_iov = &vec;
374 msghdr.msg_iovlen = 1;
375 vec.iov_base = (void *)&data;
376 vec.iov_len = sizeof(data);
378 ret = recvmsg( get_unix_fd( process->msg_fd ), &msghdr, 0 );
380 #ifndef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
381 if (ret > 0)
383 struct cmsghdr *cmsg;
384 for (cmsg = CMSG_FIRSTHDR( &msghdr ); cmsg; cmsg = CMSG_NXTHDR( &msghdr, cmsg ))
386 if (cmsg->cmsg_level != SOL_SOCKET) continue;
387 if (cmsg->cmsg_type == SCM_RIGHTS) fd = *(int *)CMSG_DATA(cmsg);
390 #endif /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
392 if (ret == sizeof(data))
394 struct thread *thread;
396 if (data.tid) thread = get_thread_from_id( data.tid );
397 else thread = (struct thread *)grab_object( get_process_first_thread( process ));
399 if (!thread || thread->process != process || thread->state == TERMINATED)
401 if (debug_level)
402 fprintf( stderr, "%04x: *fd* %d <- %d bad thread id\n",
403 data.tid, data.fd, fd );
404 close( fd );
406 else
408 if (debug_level)
409 fprintf( stderr, "%04x: *fd* %d <- %d\n",
410 thread->id, data.fd, fd );
411 thread_add_inflight_fd( thread, data.fd, fd );
413 if (thread) release_object( thread );
414 return 0;
417 if (!ret)
419 kill_process( process, 0 );
421 else if (ret > 0)
423 fprintf( stderr, "Protocol error: process %04x: partial recvmsg %d for fd\n",
424 process->id, ret );
425 if (fd != -1) close( fd );
426 kill_process( process, 1 );
428 else
430 if (errno != EWOULDBLOCK && errno != EAGAIN)
432 fprintf( stderr, "Protocol error: process %04x: ", process->id );
433 perror( "recvmsg" );
434 kill_process( process, 1 );
437 return -1;
440 /* send an fd to a client */
441 int send_client_fd( struct process *process, int fd, obj_handle_t handle )
443 struct iovec vec;
444 struct msghdr msghdr;
445 int ret;
447 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
448 msghdr.msg_accrightslen = sizeof(fd);
449 msghdr.msg_accrights = (void *)&fd;
450 #else /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
451 char cmsg_buffer[256];
452 struct cmsghdr *cmsg;
453 msghdr.msg_control = cmsg_buffer;
454 msghdr.msg_controllen = sizeof(cmsg_buffer);
455 msghdr.msg_flags = 0;
456 cmsg = CMSG_FIRSTHDR( &msghdr );
457 cmsg->cmsg_len = CMSG_LEN( sizeof(fd) );
458 cmsg->cmsg_level = SOL_SOCKET;
459 cmsg->cmsg_type = SCM_RIGHTS;
460 *(int *)CMSG_DATA(cmsg) = fd;
461 msghdr.msg_controllen = cmsg->cmsg_len;
462 #endif /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
464 msghdr.msg_name = NULL;
465 msghdr.msg_namelen = 0;
466 msghdr.msg_iov = &vec;
467 msghdr.msg_iovlen = 1;
469 vec.iov_base = (void *)&handle;
470 vec.iov_len = sizeof(handle);
472 if (debug_level)
473 fprintf( stderr, "%04x: *fd* %04x -> %d\n", current ? current->id : process->id, handle, fd );
475 ret = sendmsg( get_unix_fd( process->msg_fd ), &msghdr, 0 );
477 if (ret == sizeof(handle)) return 0;
479 if (ret >= 0)
481 fprintf( stderr, "Protocol error: process %04x: partial sendmsg %d\n", process->id, ret );
482 kill_process( process, 1 );
484 else if (errno == EPIPE)
486 kill_process( process, 0 );
488 else
490 fprintf( stderr, "Protocol error: process %04x: ", process->id );
491 perror( "sendmsg" );
492 kill_process( process, 1 );
494 return -1;
497 /* get current tick count to return to client */
498 unsigned int get_tick_count(void)
500 #ifdef HAVE_CLOCK_GETTIME
501 struct timespec ts;
502 #ifdef CLOCK_MONOTONIC_RAW
503 if (!clock_gettime( CLOCK_MONOTONIC_RAW, &ts ))
504 return ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
505 #endif
506 if (!clock_gettime( CLOCK_MONOTONIC, &ts ))
507 return ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
508 #elif defined(__APPLE__)
509 static mach_timebase_info_data_t timebase;
511 if (!timebase.denom) mach_timebase_info( &timebase );
512 return mach_absolute_time() * timebase.numer / timebase.denom / 1000000;
513 #endif
514 return (current_time - server_start_time) / 10000;
517 static void master_socket_dump( struct object *obj, int verbose )
519 struct master_socket *sock = (struct master_socket *)obj;
520 assert( obj->ops == &master_socket_ops );
521 fprintf( stderr, "Master socket fd=%p\n", sock->fd );
524 static void master_socket_destroy( struct object *obj )
526 struct master_socket *sock = (struct master_socket *)obj;
527 assert( obj->ops == &master_socket_ops );
528 release_object( sock->fd );
531 /* handle a socket event */
532 static void master_socket_poll_event( struct fd *fd, int event )
534 struct master_socket *sock = get_fd_user( fd );
535 assert( master_socket->obj.ops == &master_socket_ops );
537 assert( sock == master_socket ); /* there is only one master socket */
539 if (event & (POLLERR | POLLHUP))
541 /* this is not supposed to happen */
542 fprintf( stderr, "wineserver: Error on master socket\n" );
543 set_fd_events( sock->fd, -1 );
545 else if (event & POLLIN)
547 struct sockaddr_un dummy;
548 socklen_t len = sizeof(dummy);
549 int client = accept( get_unix_fd( master_socket->fd ), (struct sockaddr *) &dummy, &len );
550 if (client == -1) return;
551 fcntl( client, F_SETFL, O_NONBLOCK );
552 create_process( client, NULL, 0 );
556 /* remove the socket upon exit */
557 static void socket_cleanup(void)
559 static int do_it_once;
560 if (!do_it_once++) unlink( server_socket_name );
563 /* create a directory and check its permissions */
564 static void create_dir( const char *name, struct stat *st )
566 if (lstat( name, st ) == -1)
568 if (errno != ENOENT)
569 fatal_error( "lstat %s: %s", name, strerror( errno ));
570 if (mkdir( name, 0700 ) == -1 && errno != EEXIST)
571 fatal_error( "mkdir %s: %s\n", name, strerror( errno ));
572 if (lstat( name, st ) == -1)
573 fatal_error( "lstat %s: %s\n", name, strerror( errno ));
575 if (!S_ISDIR(st->st_mode)) fatal_error( "%s is not a directory\n", name );
576 if (st->st_uid != getuid()) fatal_error( "%s is not owned by you\n", name );
577 if (st->st_mode & 077) fatal_error( "%s must not be accessible by other users\n", name );
580 /* create the server directory and chdir to it */
581 static void create_server_dir( const char *dir )
583 char *p, *server_dir;
584 struct stat st, st2;
586 if (!(server_dir = strdup( dir ))) fatal_error( "out of memory\n" );
588 /* first create the base directory if needed */
590 p = strrchr( server_dir, '/' );
591 *p = 0;
592 create_dir( server_dir, &st );
594 /* now create the server directory */
596 *p = '/';
597 create_dir( server_dir, &st );
599 if (chdir( server_dir ) == -1)
600 fatal_error( "chdir %s: %s\n", server_dir, strerror( errno ));
601 if ((server_dir_fd = open( ".", O_RDONLY )) == -1)
602 fatal_error( "open %s: %s\n", server_dir, strerror( errno ));
603 if (fstat( server_dir_fd, &st2 ) == -1)
604 fatal_error( "stat %s: %s\n", server_dir, strerror( errno ));
605 if (st.st_dev != st2.st_dev || st.st_ino != st2.st_ino)
606 fatal_error( "chdir did not end up in %s\n", server_dir );
608 free( server_dir );
611 /* create the lock file and return its file descriptor */
612 static int create_server_lock(void)
614 struct stat st;
615 int fd;
617 if (lstat( server_lock_name, &st ) == -1)
619 if (errno != ENOENT)
620 fatal_error( "lstat %s/%s: %s", wine_get_server_dir(), server_lock_name, strerror( errno ));
622 else
624 if (!S_ISREG(st.st_mode))
625 fatal_error( "%s/%s is not a regular file\n", wine_get_server_dir(), server_lock_name );
628 if ((fd = open( server_lock_name, O_CREAT|O_TRUNC|O_WRONLY, 0600 )) == -1)
629 fatal_error( "error creating %s/%s: %s", wine_get_server_dir(), server_lock_name, strerror( errno ));
630 return fd;
633 /* wait for the server lock */
634 int wait_for_lock(void)
636 const char *server_dir = wine_get_server_dir();
637 int fd, r;
638 struct flock fl;
640 if (!server_dir) return 0; /* no server dir, so no lock to wait on */
642 create_server_dir( server_dir );
643 fd = create_server_lock();
645 fl.l_type = F_WRLCK;
646 fl.l_whence = SEEK_SET;
647 fl.l_start = 0;
648 fl.l_len = 1;
649 r = fcntl( fd, F_SETLKW, &fl );
650 close(fd);
652 return r;
655 /* kill the wine server holding the lock */
656 int kill_lock_owner( int sig )
658 const char *server_dir = wine_get_server_dir();
659 int fd, i, ret = 0;
660 pid_t pid = 0;
661 struct flock fl;
663 if (!server_dir) return 0; /* no server dir, nothing to do */
665 create_server_dir( server_dir );
666 fd = create_server_lock();
668 for (i = 1; i <= 20; i++)
670 fl.l_type = F_WRLCK;
671 fl.l_whence = SEEK_SET;
672 fl.l_start = 0;
673 fl.l_len = 1;
674 if (fcntl( fd, F_GETLK, &fl ) == -1) goto done;
675 if (fl.l_type != F_WRLCK) goto done; /* the file is not locked */
676 if (!pid) /* first time around */
678 if (!(pid = fl.l_pid)) goto done; /* shouldn't happen */
679 if (sig == -1)
681 if (kill( pid, SIGINT ) == -1) goto done;
682 kill( pid, SIGCONT );
683 ret = 1;
685 else /* just send the specified signal and return */
687 ret = (kill( pid, sig ) != -1);
688 goto done;
691 else if (fl.l_pid != pid) goto done; /* no longer the same process */
692 usleep( 50000 * i );
694 /* waited long enough, now kill it */
695 kill( pid, SIGKILL );
697 done:
698 close( fd );
699 return ret;
702 /* acquire the main server lock */
703 static void acquire_lock(void)
705 struct sockaddr_un addr;
706 struct stat st;
707 struct flock fl;
708 int fd, slen, got_lock = 0;
710 fd = create_server_lock();
712 fl.l_type = F_WRLCK;
713 fl.l_whence = SEEK_SET;
714 fl.l_start = 0;
715 fl.l_len = 1;
716 if (fcntl( fd, F_SETLK, &fl ) != -1)
718 /* check for crashed server */
719 if (stat( server_socket_name, &st ) != -1 && /* there is a leftover socket */
720 stat( "core", &st ) != -1 && st.st_size) /* and there is a non-empty core file */
722 fprintf( stderr,
723 "Warning: a previous instance of the wine server seems to have crashed.\n"
724 "Please run 'gdb %s %s/core',\n"
725 "type 'backtrace' at the gdb prompt and report the results. Thanks.\n\n",
726 server_argv0, wine_get_server_dir() );
728 unlink( server_socket_name ); /* we got the lock, we can safely remove the socket */
729 got_lock = 1;
730 /* in that case we reuse fd without closing it, this ensures
731 * that we hold the lock until the process exits */
733 else
735 switch(errno)
737 case ENOLCK:
738 break;
739 case EACCES:
740 /* check whether locks work at all on this file system */
741 if (fcntl( fd, F_GETLK, &fl ) == -1) break;
742 /* fall through */
743 case EAGAIN:
744 exit(2); /* we didn't get the lock, exit with special status */
745 default:
746 fatal_error( "fcntl %s/%s: %s", wine_get_server_dir(), server_lock_name, strerror( errno ));
748 /* it seems we can't use locks on this fs, so we will use the socket existence as lock */
749 close( fd );
752 if ((fd = socket( AF_UNIX, SOCK_STREAM, 0 )) == -1) fatal_error( "socket: %s\n", strerror( errno ));
753 addr.sun_family = AF_UNIX;
754 strcpy( addr.sun_path, server_socket_name );
755 slen = sizeof(addr) - sizeof(addr.sun_path) + strlen(addr.sun_path) + 1;
756 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
757 addr.sun_len = slen;
758 #endif
759 if (bind( fd, (struct sockaddr *)&addr, slen ) == -1)
761 if ((errno == EEXIST) || (errno == EADDRINUSE))
763 if (got_lock)
764 fatal_error( "couldn't bind to the socket even though we hold the lock\n" );
765 exit(2); /* we didn't get the lock, exit with special status */
767 fatal_error( "bind: %s\n", strerror( errno ));
769 atexit( socket_cleanup );
770 chmod( server_socket_name, 0600 ); /* make sure no other user can connect */
771 if (listen( fd, 5 ) == -1) fatal_error( "listen: %s\n", strerror( errno ));
773 if (!(master_socket = alloc_object( &master_socket_ops )) ||
774 !(master_socket->fd = create_anonymous_fd( &master_socket_fd_ops, fd, &master_socket->obj, 0 )))
775 fatal_error( "out of memory\n" );
776 set_fd_events( master_socket->fd, POLLIN );
777 make_object_static( &master_socket->obj );
780 /* open the master server socket and start waiting for new clients */
781 void open_master_socket(void)
783 const char *server_dir = wine_get_server_dir();
784 const char *config_dir = wine_get_config_dir();
785 int fd, pid, status, sync_pipe[2];
786 char dummy;
788 /* make sure no request is larger than the maximum size */
789 assert( sizeof(union generic_request) == sizeof(struct request_max_size) );
790 assert( sizeof(union generic_reply) == sizeof(struct request_max_size) );
792 /* make sure the stdio fds are open */
793 fd = open( "/dev/null", O_RDWR );
794 while (fd >= 0 && fd <= 2) fd = dup( fd );
796 if (!server_dir)
797 fatal_error( "directory %s cannot be accessed\n", config_dir );
798 if (chdir( config_dir ) == -1)
799 fatal_error( "chdir to %s: %s\n", config_dir, strerror( errno ));
800 if ((config_dir_fd = open( ".", O_RDONLY )) == -1)
801 fatal_error( "open %s: %s\n", config_dir, strerror( errno ));
803 create_server_dir( server_dir );
805 if (!foreground)
807 if (pipe( sync_pipe ) == -1) fatal_error( "pipe: %s\n", strerror( errno ));
808 pid = fork();
809 switch( pid )
811 case 0: /* child */
812 setsid();
813 close( sync_pipe[0] );
815 acquire_lock();
817 /* close stdin and stdout */
818 dup2( fd, 0 );
819 dup2( fd, 1 );
821 /* signal parent */
822 dummy = 0;
823 write( sync_pipe[1], &dummy, 1 );
824 close( sync_pipe[1] );
825 break;
827 case -1:
828 fatal_error( "fork: %s\n", strerror( errno ));
829 break;
831 default: /* parent */
832 close( sync_pipe[1] );
834 /* wait for child to signal us and then exit */
835 if (read( sync_pipe[0], &dummy, 1 ) == 1) _exit(0);
837 /* child terminated, propagate exit status */
838 waitpid( pid, &status, 0 );
839 if (WIFEXITED(status)) _exit( WEXITSTATUS(status) );
840 _exit(1);
843 else /* remain in the foreground */
845 acquire_lock();
848 /* init the process tracing mechanism */
849 init_tracing_mechanism();
850 close( fd );
853 /* master socket timer expiration handler */
854 static void close_socket_timeout( void *arg )
856 master_timeout = NULL;
857 flush_registry();
858 if (debug_level) fprintf( stderr, "wineserver: exiting (pid=%ld)\n", (long) getpid() );
860 #ifdef DEBUG_OBJECTS
861 close_objects(); /* shut down everything properly */
862 #endif
863 exit( 0 );
866 /* close the master socket and stop waiting for new clients */
867 void close_master_socket( timeout_t timeout )
869 if (master_socket)
871 release_object( master_socket );
872 master_socket = NULL;
874 if (master_timeout) /* cancel previous timeout */
875 remove_timeout_user( master_timeout );
877 master_timeout = add_timeout_user( timeout, close_socket_timeout, NULL );