server: Fix checks for a valid directory in object attributes.
[wine.git] / server / request.c
blob021878a92315d480e539e0a44e5c47a412c4319b
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,
171 struct directory **root )
173 static const struct object_attributes empty_attributes;
174 const struct object_attributes *attr = get_req_data();
175 data_size_t size = get_req_data_size();
177 if (root) *root = NULL;
179 if (!size)
181 *sd = NULL;
182 name->len = 0;
183 return &empty_attributes;
186 if ((size < sizeof(*attr)) || (size - sizeof(*attr) < attr->sd_len) ||
187 (size - sizeof(*attr) - attr->sd_len < attr->name_len))
189 set_error( STATUS_ACCESS_VIOLATION );
190 return NULL;
192 if (attr->sd_len && !sd_is_valid( (const struct security_descriptor *)(attr + 1), attr->sd_len ))
194 set_error( STATUS_INVALID_SECURITY_DESCR );
195 return NULL;
197 if ((attr->name_len & (sizeof(WCHAR) - 1)) || attr->name_len >= 65534)
199 set_error( STATUS_OBJECT_NAME_INVALID );
200 return NULL;
202 if (root && attr->rootdir && attr->name_len)
204 if (!(*root = get_directory_obj( current->process, attr->rootdir, 0 ))) return NULL;
206 *sd = attr->sd_len ? (const struct security_descriptor *)(attr + 1) : NULL;
207 name->len = attr->name_len;
208 name->str = (const WCHAR *)(attr + 1) + attr->sd_len / sizeof(WCHAR);
209 return attr;
212 /* return a pointer to the request data following an object attributes structure */
213 const void *get_req_data_after_objattr( const struct object_attributes *attr, data_size_t *len )
215 const void *ptr = (const WCHAR *)((const struct object_attributes *)get_req_data() + 1) +
216 attr->sd_len / sizeof(WCHAR) + attr->name_len / sizeof(WCHAR);
217 *len = get_req_data_size() - ((const char *)ptr - (const char *)get_req_data());
218 return ptr;
221 /* write the remaining part of the reply */
222 void write_reply( struct thread *thread )
224 int ret;
226 if ((ret = write( get_unix_fd( thread->reply_fd ),
227 (char *)thread->reply_data + thread->reply_size - thread->reply_towrite,
228 thread->reply_towrite )) >= 0)
230 if (!(thread->reply_towrite -= ret))
232 free( thread->reply_data );
233 thread->reply_data = NULL;
234 /* sent everything, can go back to waiting for requests */
235 set_fd_events( thread->request_fd, POLLIN );
236 set_fd_events( thread->reply_fd, 0 );
238 return;
240 if (errno == EPIPE)
241 kill_thread( thread, 0 ); /* normal death */
242 else if (errno != EWOULDBLOCK && errno != EAGAIN)
243 fatal_protocol_error( thread, "reply write: %s\n", strerror( errno ));
246 /* send a reply to the current thread */
247 static void send_reply( union generic_reply *reply )
249 int ret;
251 if (!current->reply_size)
253 if ((ret = write( get_unix_fd( current->reply_fd ),
254 reply, sizeof(*reply) )) != sizeof(*reply)) goto error;
256 else
258 struct iovec vec[2];
260 vec[0].iov_base = (void *)reply;
261 vec[0].iov_len = sizeof(*reply);
262 vec[1].iov_base = current->reply_data;
263 vec[1].iov_len = current->reply_size;
265 if ((ret = writev( get_unix_fd( current->reply_fd ), vec, 2 )) < sizeof(*reply)) goto error;
267 if ((current->reply_towrite = current->reply_size - (ret - sizeof(*reply))))
269 /* couldn't write it all, wait for POLLOUT */
270 set_fd_events( current->reply_fd, POLLOUT );
271 set_fd_events( current->request_fd, 0 );
272 return;
275 free( current->reply_data );
276 current->reply_data = NULL;
277 return;
279 error:
280 if (ret >= 0)
281 fatal_protocol_error( current, "partial write %d\n", ret );
282 else if (errno == EPIPE)
283 kill_thread( current, 0 ); /* normal death */
284 else
285 fatal_protocol_error( current, "reply write: %s\n", strerror( errno ));
288 /* call a request handler */
289 static void call_req_handler( struct thread *thread )
291 union generic_reply reply;
292 enum request req = thread->req.request_header.req;
294 current = thread;
295 current->reply_size = 0;
296 clear_error();
297 memset( &reply, 0, sizeof(reply) );
299 if (debug_level) trace_request();
301 if (req < REQ_NB_REQUESTS)
302 req_handlers[req]( &current->req, &reply );
303 else
304 set_error( STATUS_NOT_IMPLEMENTED );
306 if (current)
308 if (current->reply_fd)
310 reply.reply_header.error = current->error;
311 reply.reply_header.reply_size = current->reply_size;
312 if (debug_level) trace_reply( req, &reply );
313 send_reply( &reply );
315 else
317 current->exit_code = 1;
318 kill_thread( current, 1 ); /* no way to continue without reply fd */
321 current = NULL;
324 /* read a request from a thread */
325 void read_request( struct thread *thread )
327 int ret;
329 if (!thread->req_toread) /* no pending request */
331 if ((ret = read( get_unix_fd( thread->request_fd ), &thread->req,
332 sizeof(thread->req) )) != sizeof(thread->req)) goto error;
333 if (!(thread->req_toread = thread->req.request_header.request_size))
335 /* no data, handle request at once */
336 call_req_handler( thread );
337 return;
339 if (!(thread->req_data = malloc( thread->req_toread )))
341 fatal_protocol_error( thread, "no memory for %u bytes request %d\n",
342 thread->req_toread, thread->req.request_header.req );
343 return;
347 /* read the variable sized data */
348 for (;;)
350 ret = read( get_unix_fd( thread->request_fd ),
351 (char *)thread->req_data + thread->req.request_header.request_size
352 - thread->req_toread,
353 thread->req_toread );
354 if (ret <= 0) break;
355 if (!(thread->req_toread -= ret))
357 call_req_handler( thread );
358 free( thread->req_data );
359 thread->req_data = NULL;
360 return;
364 error:
365 if (!ret) /* closed pipe */
366 kill_thread( thread, 0 );
367 else if (ret > 0)
368 fatal_protocol_error( thread, "partial read %d\n", ret );
369 else if (errno != EWOULDBLOCK && errno != EAGAIN)
370 fatal_protocol_error( thread, "read: %s\n", strerror( errno ));
373 /* receive a file descriptor on the process socket */
374 int receive_fd( struct process *process )
376 struct iovec vec;
377 struct send_fd data;
378 struct msghdr msghdr;
379 int fd = -1, ret;
381 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
382 msghdr.msg_accrightslen = sizeof(int);
383 msghdr.msg_accrights = (void *)&fd;
384 #else /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
385 char cmsg_buffer[256];
386 msghdr.msg_control = cmsg_buffer;
387 msghdr.msg_controllen = sizeof(cmsg_buffer);
388 msghdr.msg_flags = 0;
389 #endif /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
391 msghdr.msg_name = NULL;
392 msghdr.msg_namelen = 0;
393 msghdr.msg_iov = &vec;
394 msghdr.msg_iovlen = 1;
395 vec.iov_base = (void *)&data;
396 vec.iov_len = sizeof(data);
398 ret = recvmsg( get_unix_fd( process->msg_fd ), &msghdr, 0 );
400 #ifndef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
401 if (ret > 0)
403 struct cmsghdr *cmsg;
404 for (cmsg = CMSG_FIRSTHDR( &msghdr ); cmsg; cmsg = CMSG_NXTHDR( &msghdr, cmsg ))
406 if (cmsg->cmsg_level != SOL_SOCKET) continue;
407 if (cmsg->cmsg_type == SCM_RIGHTS) fd = *(int *)CMSG_DATA(cmsg);
410 #endif /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
412 if (ret == sizeof(data))
414 struct thread *thread;
416 if (data.tid) thread = get_thread_from_id( data.tid );
417 else thread = (struct thread *)grab_object( get_process_first_thread( process ));
419 if (!thread || thread->process != process || thread->state == TERMINATED)
421 if (debug_level)
422 fprintf( stderr, "%04x: *fd* %d <- %d bad thread id\n",
423 data.tid, data.fd, fd );
424 close( fd );
426 else
428 if (debug_level)
429 fprintf( stderr, "%04x: *fd* %d <- %d\n",
430 thread->id, data.fd, fd );
431 thread_add_inflight_fd( thread, data.fd, fd );
433 if (thread) release_object( thread );
434 return 0;
437 if (!ret)
439 kill_process( process, 0 );
441 else if (ret > 0)
443 fprintf( stderr, "Protocol error: process %04x: partial recvmsg %d for fd\n",
444 process->id, ret );
445 if (fd != -1) close( fd );
446 kill_process( process, 1 );
448 else
450 if (errno != EWOULDBLOCK && errno != EAGAIN)
452 fprintf( stderr, "Protocol error: process %04x: ", process->id );
453 perror( "recvmsg" );
454 kill_process( process, 1 );
457 return -1;
460 /* send an fd to a client */
461 int send_client_fd( struct process *process, int fd, obj_handle_t handle )
463 struct iovec vec;
464 struct msghdr msghdr;
465 int ret;
467 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
468 msghdr.msg_accrightslen = sizeof(fd);
469 msghdr.msg_accrights = (void *)&fd;
470 #else /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
471 char cmsg_buffer[256];
472 struct cmsghdr *cmsg;
473 msghdr.msg_control = cmsg_buffer;
474 msghdr.msg_controllen = sizeof(cmsg_buffer);
475 msghdr.msg_flags = 0;
476 cmsg = CMSG_FIRSTHDR( &msghdr );
477 cmsg->cmsg_len = CMSG_LEN( sizeof(fd) );
478 cmsg->cmsg_level = SOL_SOCKET;
479 cmsg->cmsg_type = SCM_RIGHTS;
480 *(int *)CMSG_DATA(cmsg) = fd;
481 msghdr.msg_controllen = cmsg->cmsg_len;
482 #endif /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
484 msghdr.msg_name = NULL;
485 msghdr.msg_namelen = 0;
486 msghdr.msg_iov = &vec;
487 msghdr.msg_iovlen = 1;
489 vec.iov_base = (void *)&handle;
490 vec.iov_len = sizeof(handle);
492 if (debug_level)
493 fprintf( stderr, "%04x: *fd* %04x -> %d\n", current ? current->id : process->id, handle, fd );
495 ret = sendmsg( get_unix_fd( process->msg_fd ), &msghdr, 0 );
497 if (ret == sizeof(handle)) return 0;
499 if (ret >= 0)
501 fprintf( stderr, "Protocol error: process %04x: partial sendmsg %d\n", process->id, ret );
502 kill_process( process, 1 );
504 else if (errno == EPIPE)
506 kill_process( process, 0 );
508 else
510 fprintf( stderr, "Protocol error: process %04x: ", process->id );
511 perror( "sendmsg" );
512 kill_process( process, 1 );
514 return -1;
517 /* get current tick count to return to client */
518 unsigned int get_tick_count(void)
520 #ifdef HAVE_CLOCK_GETTIME
521 struct timespec ts;
522 #ifdef CLOCK_MONOTONIC_RAW
523 if (!clock_gettime( CLOCK_MONOTONIC_RAW, &ts ))
524 return ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
525 #endif
526 if (!clock_gettime( CLOCK_MONOTONIC, &ts ))
527 return ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
528 #elif defined(__APPLE__)
529 static mach_timebase_info_data_t timebase;
531 if (!timebase.denom) mach_timebase_info( &timebase );
532 return mach_absolute_time() * timebase.numer / timebase.denom / 1000000;
533 #endif
534 return (current_time - server_start_time) / 10000;
537 static void master_socket_dump( struct object *obj, int verbose )
539 struct master_socket *sock = (struct master_socket *)obj;
540 assert( obj->ops == &master_socket_ops );
541 fprintf( stderr, "Master socket fd=%p\n", sock->fd );
544 static void master_socket_destroy( struct object *obj )
546 struct master_socket *sock = (struct master_socket *)obj;
547 assert( obj->ops == &master_socket_ops );
548 release_object( sock->fd );
551 /* handle a socket event */
552 static void master_socket_poll_event( struct fd *fd, int event )
554 struct master_socket *sock = get_fd_user( fd );
555 assert( master_socket->obj.ops == &master_socket_ops );
557 assert( sock == master_socket ); /* there is only one master socket */
559 if (event & (POLLERR | POLLHUP))
561 /* this is not supposed to happen */
562 fprintf( stderr, "wineserver: Error on master socket\n" );
563 set_fd_events( sock->fd, -1 );
565 else if (event & POLLIN)
567 struct sockaddr_un dummy;
568 socklen_t len = sizeof(dummy);
569 int client = accept( get_unix_fd( master_socket->fd ), (struct sockaddr *) &dummy, &len );
570 if (client == -1) return;
571 fcntl( client, F_SETFL, O_NONBLOCK );
572 create_process( client, NULL, 0 );
576 /* remove the socket upon exit */
577 static void socket_cleanup(void)
579 static int do_it_once;
580 if (!do_it_once++) unlink( server_socket_name );
583 /* create a directory and check its permissions */
584 static void create_dir( const char *name, struct stat *st )
586 if (lstat( name, st ) == -1)
588 if (errno != ENOENT)
589 fatal_error( "lstat %s: %s", name, strerror( errno ));
590 if (mkdir( name, 0700 ) == -1 && errno != EEXIST)
591 fatal_error( "mkdir %s: %s\n", name, strerror( errno ));
592 if (lstat( name, st ) == -1)
593 fatal_error( "lstat %s: %s\n", name, strerror( errno ));
595 if (!S_ISDIR(st->st_mode)) fatal_error( "%s is not a directory\n", name );
596 if (st->st_uid != getuid()) fatal_error( "%s is not owned by you\n", name );
597 if (st->st_mode & 077) fatal_error( "%s must not be accessible by other users\n", name );
600 /* create the server directory and chdir to it */
601 static void create_server_dir( const char *dir )
603 char *p, *server_dir;
604 struct stat st, st2;
606 if (!(server_dir = strdup( dir ))) fatal_error( "out of memory\n" );
608 /* first create the base directory if needed */
610 p = strrchr( server_dir, '/' );
611 *p = 0;
612 create_dir( server_dir, &st );
614 /* now create the server directory */
616 *p = '/';
617 create_dir( server_dir, &st );
619 if (chdir( server_dir ) == -1)
620 fatal_error( "chdir %s: %s\n", server_dir, strerror( errno ));
621 if ((server_dir_fd = open( ".", O_RDONLY )) == -1)
622 fatal_error( "open %s: %s\n", server_dir, strerror( errno ));
623 if (fstat( server_dir_fd, &st2 ) == -1)
624 fatal_error( "stat %s: %s\n", server_dir, strerror( errno ));
625 if (st.st_dev != st2.st_dev || st.st_ino != st2.st_ino)
626 fatal_error( "chdir did not end up in %s\n", server_dir );
628 free( server_dir );
631 /* create the lock file and return its file descriptor */
632 static int create_server_lock(void)
634 struct stat st;
635 int fd;
637 if (lstat( server_lock_name, &st ) == -1)
639 if (errno != ENOENT)
640 fatal_error( "lstat %s/%s: %s", wine_get_server_dir(), server_lock_name, strerror( errno ));
642 else
644 if (!S_ISREG(st.st_mode))
645 fatal_error( "%s/%s is not a regular file\n", wine_get_server_dir(), server_lock_name );
648 if ((fd = open( server_lock_name, O_CREAT|O_TRUNC|O_WRONLY, 0600 )) == -1)
649 fatal_error( "error creating %s/%s: %s", wine_get_server_dir(), server_lock_name, strerror( errno ));
650 return fd;
653 /* wait for the server lock */
654 int wait_for_lock(void)
656 const char *server_dir = wine_get_server_dir();
657 int fd, r;
658 struct flock fl;
660 if (!server_dir) return 0; /* no server dir, so no lock to wait on */
662 create_server_dir( server_dir );
663 fd = create_server_lock();
665 fl.l_type = F_WRLCK;
666 fl.l_whence = SEEK_SET;
667 fl.l_start = 0;
668 fl.l_len = 1;
669 r = fcntl( fd, F_SETLKW, &fl );
670 close(fd);
672 return r;
675 /* kill the wine server holding the lock */
676 int kill_lock_owner( int sig )
678 const char *server_dir = wine_get_server_dir();
679 int fd, i, ret = 0;
680 pid_t pid = 0;
681 struct flock fl;
683 if (!server_dir) return 0; /* no server dir, nothing to do */
685 create_server_dir( server_dir );
686 fd = create_server_lock();
688 for (i = 1; i <= 20; i++)
690 fl.l_type = F_WRLCK;
691 fl.l_whence = SEEK_SET;
692 fl.l_start = 0;
693 fl.l_len = 1;
694 if (fcntl( fd, F_GETLK, &fl ) == -1) goto done;
695 if (fl.l_type != F_WRLCK) goto done; /* the file is not locked */
696 if (!pid) /* first time around */
698 if (!(pid = fl.l_pid)) goto done; /* shouldn't happen */
699 if (sig == -1)
701 if (kill( pid, SIGINT ) == -1) goto done;
702 kill( pid, SIGCONT );
703 ret = 1;
705 else /* just send the specified signal and return */
707 ret = (kill( pid, sig ) != -1);
708 goto done;
711 else if (fl.l_pid != pid) goto done; /* no longer the same process */
712 usleep( 50000 * i );
714 /* waited long enough, now kill it */
715 kill( pid, SIGKILL );
717 done:
718 close( fd );
719 return ret;
722 /* acquire the main server lock */
723 static void acquire_lock(void)
725 struct sockaddr_un addr;
726 struct stat st;
727 struct flock fl;
728 int fd, slen, got_lock = 0;
730 fd = create_server_lock();
732 fl.l_type = F_WRLCK;
733 fl.l_whence = SEEK_SET;
734 fl.l_start = 0;
735 fl.l_len = 1;
736 if (fcntl( fd, F_SETLK, &fl ) != -1)
738 /* check for crashed server */
739 if (stat( server_socket_name, &st ) != -1 && /* there is a leftover socket */
740 stat( "core", &st ) != -1 && st.st_size) /* and there is a non-empty core file */
742 fprintf( stderr,
743 "Warning: a previous instance of the wine server seems to have crashed.\n"
744 "Please run 'gdb %s %s/core',\n"
745 "type 'backtrace' at the gdb prompt and report the results. Thanks.\n\n",
746 server_argv0, wine_get_server_dir() );
748 unlink( server_socket_name ); /* we got the lock, we can safely remove the socket */
749 got_lock = 1;
750 /* in that case we reuse fd without closing it, this ensures
751 * that we hold the lock until the process exits */
753 else
755 switch(errno)
757 case ENOLCK:
758 break;
759 case EACCES:
760 /* check whether locks work at all on this file system */
761 if (fcntl( fd, F_GETLK, &fl ) == -1) break;
762 /* fall through */
763 case EAGAIN:
764 exit(2); /* we didn't get the lock, exit with special status */
765 default:
766 fatal_error( "fcntl %s/%s: %s", wine_get_server_dir(), server_lock_name, strerror( errno ));
768 /* it seems we can't use locks on this fs, so we will use the socket existence as lock */
769 close( fd );
772 if ((fd = socket( AF_UNIX, SOCK_STREAM, 0 )) == -1) fatal_error( "socket: %s\n", strerror( errno ));
773 addr.sun_family = AF_UNIX;
774 strcpy( addr.sun_path, server_socket_name );
775 slen = sizeof(addr) - sizeof(addr.sun_path) + strlen(addr.sun_path) + 1;
776 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
777 addr.sun_len = slen;
778 #endif
779 if (bind( fd, (struct sockaddr *)&addr, slen ) == -1)
781 if ((errno == EEXIST) || (errno == EADDRINUSE))
783 if (got_lock)
784 fatal_error( "couldn't bind to the socket even though we hold the lock\n" );
785 exit(2); /* we didn't get the lock, exit with special status */
787 fatal_error( "bind: %s\n", strerror( errno ));
789 atexit( socket_cleanup );
790 chmod( server_socket_name, 0600 ); /* make sure no other user can connect */
791 if (listen( fd, 5 ) == -1) fatal_error( "listen: %s\n", strerror( errno ));
793 if (!(master_socket = alloc_object( &master_socket_ops )) ||
794 !(master_socket->fd = create_anonymous_fd( &master_socket_fd_ops, fd, &master_socket->obj, 0 )))
795 fatal_error( "out of memory\n" );
796 set_fd_events( master_socket->fd, POLLIN );
797 make_object_static( &master_socket->obj );
800 /* open the master server socket and start waiting for new clients */
801 void open_master_socket(void)
803 const char *server_dir = wine_get_server_dir();
804 const char *config_dir = wine_get_config_dir();
805 int fd, pid, status, sync_pipe[2];
806 char dummy;
808 /* make sure no request is larger than the maximum size */
809 assert( sizeof(union generic_request) == sizeof(struct request_max_size) );
810 assert( sizeof(union generic_reply) == sizeof(struct request_max_size) );
812 /* make sure the stdio fds are open */
813 fd = open( "/dev/null", O_RDWR );
814 while (fd >= 0 && fd <= 2) fd = dup( fd );
816 if (!server_dir)
817 fatal_error( "directory %s cannot be accessed\n", config_dir );
818 if (chdir( config_dir ) == -1)
819 fatal_error( "chdir to %s: %s\n", config_dir, strerror( errno ));
820 if ((config_dir_fd = open( ".", O_RDONLY )) == -1)
821 fatal_error( "open %s: %s\n", config_dir, strerror( errno ));
823 create_server_dir( server_dir );
825 if (!foreground)
827 if (pipe( sync_pipe ) == -1) fatal_error( "pipe: %s\n", strerror( errno ));
828 pid = fork();
829 switch( pid )
831 case 0: /* child */
832 setsid();
833 close( sync_pipe[0] );
835 acquire_lock();
837 /* close stdin and stdout */
838 dup2( fd, 0 );
839 dup2( fd, 1 );
841 /* signal parent */
842 dummy = 0;
843 write( sync_pipe[1], &dummy, 1 );
844 close( sync_pipe[1] );
845 break;
847 case -1:
848 fatal_error( "fork: %s\n", strerror( errno ));
849 break;
851 default: /* parent */
852 close( sync_pipe[1] );
854 /* wait for child to signal us and then exit */
855 if (read( sync_pipe[0], &dummy, 1 ) == 1) _exit(0);
857 /* child terminated, propagate exit status */
858 waitpid( pid, &status, 0 );
859 if (WIFEXITED(status)) _exit( WEXITSTATUS(status) );
860 _exit(1);
863 else /* remain in the foreground */
865 acquire_lock();
868 /* init the process tracing mechanism */
869 init_tracing_mechanism();
870 close( fd );
873 /* master socket timer expiration handler */
874 static void close_socket_timeout( void *arg )
876 master_timeout = NULL;
877 flush_registry();
878 if (debug_level) fprintf( stderr, "wineserver: exiting (pid=%ld)\n", (long) getpid() );
880 #ifdef DEBUG_OBJECTS
881 close_objects(); /* shut down everything properly */
882 #endif
883 exit( 0 );
886 /* close the master socket and stop waiting for new clients */
887 void close_master_socket( timeout_t timeout )
889 if (master_socket)
891 release_object( master_socket );
892 master_socket = NULL;
894 if (master_timeout) /* cancel previous timeout */
895 remove_timeout_user( master_timeout );
897 master_timeout = add_timeout_user( timeout, close_socket_timeout, NULL );