xaudio2: Use an HRESULT return code.
[wine.git] / server / request.c
blob62e2b0304ff46b721cc0f78a40ea0009feed9d0c
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;
194 if ((attr->name_len & (sizeof(WCHAR) - 1)) || attr->name_len >= 65534)
196 set_error( STATUS_OBJECT_NAME_INVALID );
197 return NULL;
199 *sd = attr->sd_len ? (const struct security_descriptor *)(attr + 1) : NULL;
200 name->len = attr->name_len;
201 name->str = (const WCHAR *)(attr + 1) + attr->sd_len / sizeof(WCHAR);
202 return attr;
205 /* return a pointer to the request data following an object attributes structure */
206 const void *get_req_data_after_objattr( const struct object_attributes *attr, data_size_t *len )
208 const void *ptr = (const WCHAR *)((const struct object_attributes *)get_req_data() + 1) +
209 attr->sd_len / sizeof(WCHAR) + attr->name_len / sizeof(WCHAR);
210 *len = get_req_data_size() - ((const char *)ptr - (const char *)get_req_data());
211 return ptr;
214 /* write the remaining part of the reply */
215 void write_reply( struct thread *thread )
217 int ret;
219 if ((ret = write( get_unix_fd( thread->reply_fd ),
220 (char *)thread->reply_data + thread->reply_size - thread->reply_towrite,
221 thread->reply_towrite )) >= 0)
223 if (!(thread->reply_towrite -= ret))
225 free( thread->reply_data );
226 thread->reply_data = NULL;
227 /* sent everything, can go back to waiting for requests */
228 set_fd_events( thread->request_fd, POLLIN );
229 set_fd_events( thread->reply_fd, 0 );
231 return;
233 if (errno == EPIPE)
234 kill_thread( thread, 0 ); /* normal death */
235 else if (errno != EWOULDBLOCK && errno != EAGAIN)
236 fatal_protocol_error( thread, "reply write: %s\n", strerror( errno ));
239 /* send a reply to the current thread */
240 static void send_reply( union generic_reply *reply )
242 int ret;
244 if (!current->reply_size)
246 if ((ret = write( get_unix_fd( current->reply_fd ),
247 reply, sizeof(*reply) )) != sizeof(*reply)) goto error;
249 else
251 struct iovec vec[2];
253 vec[0].iov_base = (void *)reply;
254 vec[0].iov_len = sizeof(*reply);
255 vec[1].iov_base = current->reply_data;
256 vec[1].iov_len = current->reply_size;
258 if ((ret = writev( get_unix_fd( current->reply_fd ), vec, 2 )) < sizeof(*reply)) goto error;
260 if ((current->reply_towrite = current->reply_size - (ret - sizeof(*reply))))
262 /* couldn't write it all, wait for POLLOUT */
263 set_fd_events( current->reply_fd, POLLOUT );
264 set_fd_events( current->request_fd, 0 );
265 return;
268 free( current->reply_data );
269 current->reply_data = NULL;
270 return;
272 error:
273 if (ret >= 0)
274 fatal_protocol_error( current, "partial write %d\n", ret );
275 else if (errno == EPIPE)
276 kill_thread( current, 0 ); /* normal death */
277 else
278 fatal_protocol_error( current, "reply write: %s\n", strerror( errno ));
281 /* call a request handler */
282 static void call_req_handler( struct thread *thread )
284 union generic_reply reply;
285 enum request req = thread->req.request_header.req;
287 current = thread;
288 current->reply_size = 0;
289 clear_error();
290 memset( &reply, 0, sizeof(reply) );
292 if (debug_level) trace_request();
294 if (req < REQ_NB_REQUESTS)
295 req_handlers[req]( &current->req, &reply );
296 else
297 set_error( STATUS_NOT_IMPLEMENTED );
299 if (current)
301 if (current->reply_fd)
303 reply.reply_header.error = current->error;
304 reply.reply_header.reply_size = current->reply_size;
305 if (debug_level) trace_reply( req, &reply );
306 send_reply( &reply );
308 else
310 current->exit_code = 1;
311 kill_thread( current, 1 ); /* no way to continue without reply fd */
314 current = NULL;
317 /* read a request from a thread */
318 void read_request( struct thread *thread )
320 int ret;
322 if (!thread->req_toread) /* no pending request */
324 if ((ret = read( get_unix_fd( thread->request_fd ), &thread->req,
325 sizeof(thread->req) )) != sizeof(thread->req)) goto error;
326 if (!(thread->req_toread = thread->req.request_header.request_size))
328 /* no data, handle request at once */
329 call_req_handler( thread );
330 return;
332 if (!(thread->req_data = malloc( thread->req_toread )))
334 fatal_protocol_error( thread, "no memory for %u bytes request %d\n",
335 thread->req_toread, thread->req.request_header.req );
336 return;
340 /* read the variable sized data */
341 for (;;)
343 ret = read( get_unix_fd( thread->request_fd ),
344 (char *)thread->req_data + thread->req.request_header.request_size
345 - thread->req_toread,
346 thread->req_toread );
347 if (ret <= 0) break;
348 if (!(thread->req_toread -= ret))
350 call_req_handler( thread );
351 free( thread->req_data );
352 thread->req_data = NULL;
353 return;
357 error:
358 if (!ret) /* closed pipe */
359 kill_thread( thread, 0 );
360 else if (ret > 0)
361 fatal_protocol_error( thread, "partial read %d\n", ret );
362 else if (errno != EWOULDBLOCK && errno != EAGAIN)
363 fatal_protocol_error( thread, "read: %s\n", strerror( errno ));
366 /* receive a file descriptor on the process socket */
367 int receive_fd( struct process *process )
369 struct iovec vec;
370 struct send_fd data;
371 struct msghdr msghdr;
372 int fd = -1, ret;
374 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
375 msghdr.msg_accrightslen = sizeof(int);
376 msghdr.msg_accrights = (void *)&fd;
377 #else /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
378 char cmsg_buffer[256];
379 msghdr.msg_control = cmsg_buffer;
380 msghdr.msg_controllen = sizeof(cmsg_buffer);
381 msghdr.msg_flags = 0;
382 #endif /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
384 msghdr.msg_name = NULL;
385 msghdr.msg_namelen = 0;
386 msghdr.msg_iov = &vec;
387 msghdr.msg_iovlen = 1;
388 vec.iov_base = (void *)&data;
389 vec.iov_len = sizeof(data);
391 ret = recvmsg( get_unix_fd( process->msg_fd ), &msghdr, 0 );
393 #ifndef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
394 if (ret > 0)
396 struct cmsghdr *cmsg;
397 for (cmsg = CMSG_FIRSTHDR( &msghdr ); cmsg; cmsg = CMSG_NXTHDR( &msghdr, cmsg ))
399 if (cmsg->cmsg_level != SOL_SOCKET) continue;
400 if (cmsg->cmsg_type == SCM_RIGHTS) fd = *(int *)CMSG_DATA(cmsg);
403 #endif /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
405 if (ret == sizeof(data))
407 struct thread *thread;
409 if (data.tid) thread = get_thread_from_id( data.tid );
410 else thread = (struct thread *)grab_object( get_process_first_thread( process ));
412 if (!thread || thread->process != process || thread->state == TERMINATED)
414 if (debug_level)
415 fprintf( stderr, "%04x: *fd* %d <- %d bad thread id\n",
416 data.tid, data.fd, fd );
417 close( fd );
419 else
421 if (debug_level)
422 fprintf( stderr, "%04x: *fd* %d <- %d\n",
423 thread->id, data.fd, fd );
424 thread_add_inflight_fd( thread, data.fd, fd );
426 if (thread) release_object( thread );
427 return 0;
430 if (!ret)
432 kill_process( process, 0 );
434 else if (ret > 0)
436 fprintf( stderr, "Protocol error: process %04x: partial recvmsg %d for fd\n",
437 process->id, ret );
438 if (fd != -1) close( fd );
439 kill_process( process, 1 );
441 else
443 if (errno != EWOULDBLOCK && errno != EAGAIN)
445 fprintf( stderr, "Protocol error: process %04x: ", process->id );
446 perror( "recvmsg" );
447 kill_process( process, 1 );
450 return -1;
453 /* send an fd to a client */
454 int send_client_fd( struct process *process, int fd, obj_handle_t handle )
456 struct iovec vec;
457 struct msghdr msghdr;
458 int ret;
460 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
461 msghdr.msg_accrightslen = sizeof(fd);
462 msghdr.msg_accrights = (void *)&fd;
463 #else /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
464 char cmsg_buffer[256];
465 struct cmsghdr *cmsg;
466 msghdr.msg_control = cmsg_buffer;
467 msghdr.msg_controllen = sizeof(cmsg_buffer);
468 msghdr.msg_flags = 0;
469 cmsg = CMSG_FIRSTHDR( &msghdr );
470 cmsg->cmsg_len = CMSG_LEN( sizeof(fd) );
471 cmsg->cmsg_level = SOL_SOCKET;
472 cmsg->cmsg_type = SCM_RIGHTS;
473 *(int *)CMSG_DATA(cmsg) = fd;
474 msghdr.msg_controllen = cmsg->cmsg_len;
475 #endif /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
477 msghdr.msg_name = NULL;
478 msghdr.msg_namelen = 0;
479 msghdr.msg_iov = &vec;
480 msghdr.msg_iovlen = 1;
482 vec.iov_base = (void *)&handle;
483 vec.iov_len = sizeof(handle);
485 if (debug_level)
486 fprintf( stderr, "%04x: *fd* %04x -> %d\n", current ? current->id : process->id, handle, fd );
488 ret = sendmsg( get_unix_fd( process->msg_fd ), &msghdr, 0 );
490 if (ret == sizeof(handle)) return 0;
492 if (ret >= 0)
494 fprintf( stderr, "Protocol error: process %04x: partial sendmsg %d\n", process->id, ret );
495 kill_process( process, 1 );
497 else if (errno == EPIPE)
499 kill_process( process, 0 );
501 else
503 fprintf( stderr, "Protocol error: process %04x: ", process->id );
504 perror( "sendmsg" );
505 kill_process( process, 1 );
507 return -1;
510 /* get current tick count to return to client */
511 unsigned int get_tick_count(void)
513 #ifdef HAVE_CLOCK_GETTIME
514 struct timespec ts;
515 #ifdef CLOCK_MONOTONIC_RAW
516 if (!clock_gettime( CLOCK_MONOTONIC_RAW, &ts ))
517 return ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
518 #endif
519 if (!clock_gettime( CLOCK_MONOTONIC, &ts ))
520 return ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
521 #elif defined(__APPLE__)
522 static mach_timebase_info_data_t timebase;
524 if (!timebase.denom) mach_timebase_info( &timebase );
525 return mach_absolute_time() * timebase.numer / timebase.denom / 1000000;
526 #endif
527 return (current_time - server_start_time) / 10000;
530 static void master_socket_dump( struct object *obj, int verbose )
532 struct master_socket *sock = (struct master_socket *)obj;
533 assert( obj->ops == &master_socket_ops );
534 fprintf( stderr, "Master socket fd=%p\n", sock->fd );
537 static void master_socket_destroy( struct object *obj )
539 struct master_socket *sock = (struct master_socket *)obj;
540 assert( obj->ops == &master_socket_ops );
541 release_object( sock->fd );
544 /* handle a socket event */
545 static void master_socket_poll_event( struct fd *fd, int event )
547 struct master_socket *sock = get_fd_user( fd );
548 assert( master_socket->obj.ops == &master_socket_ops );
550 assert( sock == master_socket ); /* there is only one master socket */
552 if (event & (POLLERR | POLLHUP))
554 /* this is not supposed to happen */
555 fprintf( stderr, "wineserver: Error on master socket\n" );
556 set_fd_events( sock->fd, -1 );
558 else if (event & POLLIN)
560 struct sockaddr_un dummy;
561 socklen_t len = sizeof(dummy);
562 int client = accept( get_unix_fd( master_socket->fd ), (struct sockaddr *) &dummy, &len );
563 if (client == -1) return;
564 fcntl( client, F_SETFL, O_NONBLOCK );
565 create_process( client, NULL, 0 );
569 /* remove the socket upon exit */
570 static void socket_cleanup(void)
572 static int do_it_once;
573 if (!do_it_once++) unlink( server_socket_name );
576 /* create a directory and check its permissions */
577 static void create_dir( const char *name, struct stat *st )
579 if (lstat( name, st ) == -1)
581 if (errno != ENOENT)
582 fatal_error( "lstat %s: %s", name, strerror( errno ));
583 if (mkdir( name, 0700 ) == -1 && errno != EEXIST)
584 fatal_error( "mkdir %s: %s\n", name, strerror( errno ));
585 if (lstat( name, st ) == -1)
586 fatal_error( "lstat %s: %s\n", name, strerror( errno ));
588 if (!S_ISDIR(st->st_mode)) fatal_error( "%s is not a directory\n", name );
589 if (st->st_uid != getuid()) fatal_error( "%s is not owned by you\n", name );
590 if (st->st_mode & 077) fatal_error( "%s must not be accessible by other users\n", name );
593 /* create the server directory and chdir to it */
594 static void create_server_dir( const char *dir )
596 char *p, *server_dir;
597 struct stat st, st2;
599 if (!(server_dir = strdup( dir ))) fatal_error( "out of memory\n" );
601 /* first create the base directory if needed */
603 p = strrchr( server_dir, '/' );
604 *p = 0;
605 create_dir( server_dir, &st );
607 /* now create the server directory */
609 *p = '/';
610 create_dir( server_dir, &st );
612 if (chdir( server_dir ) == -1)
613 fatal_error( "chdir %s: %s\n", server_dir, strerror( errno ));
614 if ((server_dir_fd = open( ".", O_RDONLY )) == -1)
615 fatal_error( "open %s: %s\n", server_dir, strerror( errno ));
616 if (fstat( server_dir_fd, &st2 ) == -1)
617 fatal_error( "stat %s: %s\n", server_dir, strerror( errno ));
618 if (st.st_dev != st2.st_dev || st.st_ino != st2.st_ino)
619 fatal_error( "chdir did not end up in %s\n", server_dir );
621 free( server_dir );
624 /* create the lock file and return its file descriptor */
625 static int create_server_lock(void)
627 struct stat st;
628 int fd;
630 if (lstat( server_lock_name, &st ) == -1)
632 if (errno != ENOENT)
633 fatal_error( "lstat %s/%s: %s", wine_get_server_dir(), server_lock_name, strerror( errno ));
635 else
637 if (!S_ISREG(st.st_mode))
638 fatal_error( "%s/%s is not a regular file\n", wine_get_server_dir(), server_lock_name );
641 if ((fd = open( server_lock_name, O_CREAT|O_TRUNC|O_WRONLY, 0600 )) == -1)
642 fatal_error( "error creating %s/%s: %s", wine_get_server_dir(), server_lock_name, strerror( errno ));
643 return fd;
646 /* wait for the server lock */
647 int wait_for_lock(void)
649 const char *server_dir = wine_get_server_dir();
650 int fd, r;
651 struct flock fl;
653 if (!server_dir) return 0; /* no server dir, so no lock to wait on */
655 create_server_dir( server_dir );
656 fd = create_server_lock();
658 fl.l_type = F_WRLCK;
659 fl.l_whence = SEEK_SET;
660 fl.l_start = 0;
661 fl.l_len = 1;
662 r = fcntl( fd, F_SETLKW, &fl );
663 close(fd);
665 return r;
668 /* kill the wine server holding the lock */
669 int kill_lock_owner( int sig )
671 const char *server_dir = wine_get_server_dir();
672 int fd, i, ret = 0;
673 pid_t pid = 0;
674 struct flock fl;
676 if (!server_dir) return 0; /* no server dir, nothing to do */
678 create_server_dir( server_dir );
679 fd = create_server_lock();
681 for (i = 1; i <= 20; i++)
683 fl.l_type = F_WRLCK;
684 fl.l_whence = SEEK_SET;
685 fl.l_start = 0;
686 fl.l_len = 1;
687 if (fcntl( fd, F_GETLK, &fl ) == -1) goto done;
688 if (fl.l_type != F_WRLCK) goto done; /* the file is not locked */
689 if (!pid) /* first time around */
691 if (!(pid = fl.l_pid)) goto done; /* shouldn't happen */
692 if (sig == -1)
694 if (kill( pid, SIGINT ) == -1) goto done;
695 kill( pid, SIGCONT );
696 ret = 1;
698 else /* just send the specified signal and return */
700 ret = (kill( pid, sig ) != -1);
701 goto done;
704 else if (fl.l_pid != pid) goto done; /* no longer the same process */
705 usleep( 50000 * i );
707 /* waited long enough, now kill it */
708 kill( pid, SIGKILL );
710 done:
711 close( fd );
712 return ret;
715 /* acquire the main server lock */
716 static void acquire_lock(void)
718 struct sockaddr_un addr;
719 struct stat st;
720 struct flock fl;
721 int fd, slen, got_lock = 0;
723 fd = create_server_lock();
725 fl.l_type = F_WRLCK;
726 fl.l_whence = SEEK_SET;
727 fl.l_start = 0;
728 fl.l_len = 1;
729 if (fcntl( fd, F_SETLK, &fl ) != -1)
731 /* check for crashed server */
732 if (stat( server_socket_name, &st ) != -1 && /* there is a leftover socket */
733 stat( "core", &st ) != -1 && st.st_size) /* and there is a non-empty core file */
735 fprintf( stderr,
736 "Warning: a previous instance of the wine server seems to have crashed.\n"
737 "Please run 'gdb %s %s/core',\n"
738 "type 'backtrace' at the gdb prompt and report the results. Thanks.\n\n",
739 server_argv0, wine_get_server_dir() );
741 unlink( server_socket_name ); /* we got the lock, we can safely remove the socket */
742 got_lock = 1;
743 /* in that case we reuse fd without closing it, this ensures
744 * that we hold the lock until the process exits */
746 else
748 switch(errno)
750 case ENOLCK:
751 break;
752 case EACCES:
753 /* check whether locks work at all on this file system */
754 if (fcntl( fd, F_GETLK, &fl ) == -1) break;
755 /* fall through */
756 case EAGAIN:
757 exit(2); /* we didn't get the lock, exit with special status */
758 default:
759 fatal_error( "fcntl %s/%s: %s", wine_get_server_dir(), server_lock_name, strerror( errno ));
761 /* it seems we can't use locks on this fs, so we will use the socket existence as lock */
762 close( fd );
765 if ((fd = socket( AF_UNIX, SOCK_STREAM, 0 )) == -1) fatal_error( "socket: %s\n", strerror( errno ));
766 addr.sun_family = AF_UNIX;
767 strcpy( addr.sun_path, server_socket_name );
768 slen = sizeof(addr) - sizeof(addr.sun_path) + strlen(addr.sun_path) + 1;
769 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
770 addr.sun_len = slen;
771 #endif
772 if (bind( fd, (struct sockaddr *)&addr, slen ) == -1)
774 if ((errno == EEXIST) || (errno == EADDRINUSE))
776 if (got_lock)
777 fatal_error( "couldn't bind to the socket even though we hold the lock\n" );
778 exit(2); /* we didn't get the lock, exit with special status */
780 fatal_error( "bind: %s\n", strerror( errno ));
782 atexit( socket_cleanup );
783 chmod( server_socket_name, 0600 ); /* make sure no other user can connect */
784 if (listen( fd, 5 ) == -1) fatal_error( "listen: %s\n", strerror( errno ));
786 if (!(master_socket = alloc_object( &master_socket_ops )) ||
787 !(master_socket->fd = create_anonymous_fd( &master_socket_fd_ops, fd, &master_socket->obj, 0 )))
788 fatal_error( "out of memory\n" );
789 set_fd_events( master_socket->fd, POLLIN );
790 make_object_static( &master_socket->obj );
793 /* open the master server socket and start waiting for new clients */
794 void open_master_socket(void)
796 const char *server_dir = wine_get_server_dir();
797 const char *config_dir = wine_get_config_dir();
798 int fd, pid, status, sync_pipe[2];
799 char dummy;
801 /* make sure no request is larger than the maximum size */
802 assert( sizeof(union generic_request) == sizeof(struct request_max_size) );
803 assert( sizeof(union generic_reply) == sizeof(struct request_max_size) );
805 /* make sure the stdio fds are open */
806 fd = open( "/dev/null", O_RDWR );
807 while (fd >= 0 && fd <= 2) fd = dup( fd );
809 if (!server_dir)
810 fatal_error( "directory %s cannot be accessed\n", config_dir );
811 if (chdir( config_dir ) == -1)
812 fatal_error( "chdir to %s: %s\n", config_dir, strerror( errno ));
813 if ((config_dir_fd = open( ".", O_RDONLY )) == -1)
814 fatal_error( "open %s: %s\n", config_dir, strerror( errno ));
816 create_server_dir( server_dir );
818 if (!foreground)
820 if (pipe( sync_pipe ) == -1) fatal_error( "pipe: %s\n", strerror( errno ));
821 pid = fork();
822 switch( pid )
824 case 0: /* child */
825 setsid();
826 close( sync_pipe[0] );
828 acquire_lock();
830 /* close stdin and stdout */
831 dup2( fd, 0 );
832 dup2( fd, 1 );
834 /* signal parent */
835 dummy = 0;
836 write( sync_pipe[1], &dummy, 1 );
837 close( sync_pipe[1] );
838 break;
840 case -1:
841 fatal_error( "fork: %s\n", strerror( errno ));
842 break;
844 default: /* parent */
845 close( sync_pipe[1] );
847 /* wait for child to signal us and then exit */
848 if (read( sync_pipe[0], &dummy, 1 ) == 1) _exit(0);
850 /* child terminated, propagate exit status */
851 waitpid( pid, &status, 0 );
852 if (WIFEXITED(status)) _exit( WEXITSTATUS(status) );
853 _exit(1);
856 else /* remain in the foreground */
858 acquire_lock();
861 /* init the process tracing mechanism */
862 init_tracing_mechanism();
863 close( fd );
866 /* master socket timer expiration handler */
867 static void close_socket_timeout( void *arg )
869 master_timeout = NULL;
870 flush_registry();
871 if (debug_level) fprintf( stderr, "wineserver: exiting (pid=%ld)\n", (long) getpid() );
873 #ifdef DEBUG_OBJECTS
874 close_objects(); /* shut down everything properly */
875 #endif
876 exit( 0 );
879 /* close the master socket and stop waiting for new clients */
880 void close_master_socket( timeout_t timeout )
882 if (master_socket)
884 release_object( master_socket );
885 master_socket = NULL;
887 if (master_timeout) /* cancel previous timeout */
888 remove_timeout_user( master_timeout );
890 master_timeout = add_timeout_user( timeout, close_socket_timeout, NULL );