wined3d: Use wined3d_texture_get_pitch() in wined3d_surface_map().
[wine.git] / server / request.c
blobdfa44858a5fc1a51a9b37e6064017aaa765dadcb
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_link_name, /* link_name */
108 NULL, /* unlink_name */
109 no_open_file, /* open_file */
110 no_close_handle, /* close_handle */
111 master_socket_destroy /* destroy */
114 static const struct fd_ops master_socket_fd_ops =
116 NULL, /* get_poll_events */
117 master_socket_poll_event, /* poll_event */
118 NULL, /* flush */
119 NULL, /* get_fd_type */
120 NULL, /* ioctl */
121 NULL, /* queue_async */
122 NULL, /* reselect_async */
123 NULL /* cancel_async */
127 struct thread *current = NULL; /* thread handling the current request */
128 unsigned int global_error = 0; /* global error code for when no thread is current */
129 timeout_t server_start_time = 0; /* server startup time */
130 int server_dir_fd = -1; /* file descriptor for the server dir */
131 int config_dir_fd = -1; /* file descriptor for the config dir */
133 static struct master_socket *master_socket; /* the master socket object */
134 static struct timeout_user *master_timeout;
136 /* complain about a protocol error and terminate the client connection */
137 void fatal_protocol_error( struct thread *thread, const char *err, ... )
139 va_list args;
141 va_start( args, err );
142 fprintf( stderr, "Protocol error:%04x: ", thread->id );
143 vfprintf( stderr, err, args );
144 va_end( args );
145 thread->exit_code = 1;
146 kill_thread( thread, 1 );
149 /* die on a fatal error */
150 void fatal_error( const char *err, ... )
152 va_list args;
154 va_start( args, err );
155 fprintf( stderr, "wineserver: " );
156 vfprintf( stderr, err, args );
157 va_end( args );
158 exit(1);
161 /* allocate the reply data */
162 void *set_reply_data_size( data_size_t size )
164 assert( size <= get_reply_max_size() );
165 if (size && !(current->reply_data = mem_alloc( size ))) size = 0;
166 current->reply_size = size;
167 return current->reply_data;
170 /* return object attributes from the current request */
171 const struct object_attributes *get_req_object_attributes( const struct security_descriptor **sd,
172 struct unicode_str *name,
173 struct directory **root )
175 static const struct object_attributes empty_attributes;
176 const struct object_attributes *attr = get_req_data();
177 data_size_t size = get_req_data_size();
179 if (root) *root = NULL;
181 if (!size)
183 *sd = NULL;
184 name->len = 0;
185 return &empty_attributes;
188 if ((size < sizeof(*attr)) || (size - sizeof(*attr) < attr->sd_len) ||
189 (size - sizeof(*attr) - attr->sd_len < attr->name_len))
191 set_error( STATUS_ACCESS_VIOLATION );
192 return NULL;
194 if (attr->sd_len && !sd_is_valid( (const struct security_descriptor *)(attr + 1), attr->sd_len ))
196 set_error( STATUS_INVALID_SECURITY_DESCR );
197 return NULL;
199 if ((attr->name_len & (sizeof(WCHAR) - 1)) || attr->name_len >= 65534)
201 set_error( STATUS_OBJECT_NAME_INVALID );
202 return NULL;
204 if (root && attr->rootdir && attr->name_len)
206 if (!(*root = get_directory_obj( current->process, attr->rootdir, 0 ))) return NULL;
208 *sd = attr->sd_len ? (const struct security_descriptor *)(attr + 1) : NULL;
209 name->len = attr->name_len;
210 name->str = (const WCHAR *)(attr + 1) + attr->sd_len / sizeof(WCHAR);
211 return attr;
214 /* return a pointer to the request data following an object attributes structure */
215 const void *get_req_data_after_objattr( const struct object_attributes *attr, data_size_t *len )
217 const void *ptr = (const WCHAR *)((const struct object_attributes *)get_req_data() + 1) +
218 attr->sd_len / sizeof(WCHAR) + attr->name_len / sizeof(WCHAR);
219 *len = get_req_data_size() - ((const char *)ptr - (const char *)get_req_data());
220 return ptr;
223 /* write the remaining part of the reply */
224 void write_reply( struct thread *thread )
226 int ret;
228 if ((ret = write( get_unix_fd( thread->reply_fd ),
229 (char *)thread->reply_data + thread->reply_size - thread->reply_towrite,
230 thread->reply_towrite )) >= 0)
232 if (!(thread->reply_towrite -= ret))
234 free( thread->reply_data );
235 thread->reply_data = NULL;
236 /* sent everything, can go back to waiting for requests */
237 set_fd_events( thread->request_fd, POLLIN );
238 set_fd_events( thread->reply_fd, 0 );
240 return;
242 if (errno == EPIPE)
243 kill_thread( thread, 0 ); /* normal death */
244 else if (errno != EWOULDBLOCK && errno != EAGAIN)
245 fatal_protocol_error( thread, "reply write: %s\n", strerror( errno ));
248 /* send a reply to the current thread */
249 static void send_reply( union generic_reply *reply )
251 int ret;
253 if (!current->reply_size)
255 if ((ret = write( get_unix_fd( current->reply_fd ),
256 reply, sizeof(*reply) )) != sizeof(*reply)) goto error;
258 else
260 struct iovec vec[2];
262 vec[0].iov_base = (void *)reply;
263 vec[0].iov_len = sizeof(*reply);
264 vec[1].iov_base = current->reply_data;
265 vec[1].iov_len = current->reply_size;
267 if ((ret = writev( get_unix_fd( current->reply_fd ), vec, 2 )) < sizeof(*reply)) goto error;
269 if ((current->reply_towrite = current->reply_size - (ret - sizeof(*reply))))
271 /* couldn't write it all, wait for POLLOUT */
272 set_fd_events( current->reply_fd, POLLOUT );
273 set_fd_events( current->request_fd, 0 );
274 return;
277 free( current->reply_data );
278 current->reply_data = NULL;
279 return;
281 error:
282 if (ret >= 0)
283 fatal_protocol_error( current, "partial write %d\n", ret );
284 else if (errno == EPIPE)
285 kill_thread( current, 0 ); /* normal death */
286 else
287 fatal_protocol_error( current, "reply write: %s\n", strerror( errno ));
290 /* call a request handler */
291 static void call_req_handler( struct thread *thread )
293 union generic_reply reply;
294 enum request req = thread->req.request_header.req;
296 current = thread;
297 current->reply_size = 0;
298 clear_error();
299 memset( &reply, 0, sizeof(reply) );
301 if (debug_level) trace_request();
303 if (req < REQ_NB_REQUESTS)
304 req_handlers[req]( &current->req, &reply );
305 else
306 set_error( STATUS_NOT_IMPLEMENTED );
308 if (current)
310 if (current->reply_fd)
312 reply.reply_header.error = current->error;
313 reply.reply_header.reply_size = current->reply_size;
314 if (debug_level) trace_reply( req, &reply );
315 send_reply( &reply );
317 else
319 current->exit_code = 1;
320 kill_thread( current, 1 ); /* no way to continue without reply fd */
323 current = NULL;
326 /* read a request from a thread */
327 void read_request( struct thread *thread )
329 int ret;
331 if (!thread->req_toread) /* no pending request */
333 if ((ret = read( get_unix_fd( thread->request_fd ), &thread->req,
334 sizeof(thread->req) )) != sizeof(thread->req)) goto error;
335 if (!(thread->req_toread = thread->req.request_header.request_size))
337 /* no data, handle request at once */
338 call_req_handler( thread );
339 return;
341 if (!(thread->req_data = malloc( thread->req_toread )))
343 fatal_protocol_error( thread, "no memory for %u bytes request %d\n",
344 thread->req_toread, thread->req.request_header.req );
345 return;
349 /* read the variable sized data */
350 for (;;)
352 ret = read( get_unix_fd( thread->request_fd ),
353 (char *)thread->req_data + thread->req.request_header.request_size
354 - thread->req_toread,
355 thread->req_toread );
356 if (ret <= 0) break;
357 if (!(thread->req_toread -= ret))
359 call_req_handler( thread );
360 free( thread->req_data );
361 thread->req_data = NULL;
362 return;
366 error:
367 if (!ret) /* closed pipe */
368 kill_thread( thread, 0 );
369 else if (ret > 0)
370 fatal_protocol_error( thread, "partial read %d\n", ret );
371 else if (errno != EWOULDBLOCK && errno != EAGAIN)
372 fatal_protocol_error( thread, "read: %s\n", strerror( errno ));
375 /* receive a file descriptor on the process socket */
376 int receive_fd( struct process *process )
378 struct iovec vec;
379 struct send_fd data;
380 struct msghdr msghdr;
381 int fd = -1, ret;
383 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
384 msghdr.msg_accrightslen = sizeof(int);
385 msghdr.msg_accrights = (void *)&fd;
386 #else /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
387 char cmsg_buffer[256];
388 msghdr.msg_control = cmsg_buffer;
389 msghdr.msg_controllen = sizeof(cmsg_buffer);
390 msghdr.msg_flags = 0;
391 #endif /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
393 msghdr.msg_name = NULL;
394 msghdr.msg_namelen = 0;
395 msghdr.msg_iov = &vec;
396 msghdr.msg_iovlen = 1;
397 vec.iov_base = (void *)&data;
398 vec.iov_len = sizeof(data);
400 ret = recvmsg( get_unix_fd( process->msg_fd ), &msghdr, 0 );
402 #ifndef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
403 if (ret > 0)
405 struct cmsghdr *cmsg;
406 for (cmsg = CMSG_FIRSTHDR( &msghdr ); cmsg; cmsg = CMSG_NXTHDR( &msghdr, cmsg ))
408 if (cmsg->cmsg_level != SOL_SOCKET) continue;
409 if (cmsg->cmsg_type == SCM_RIGHTS) fd = *(int *)CMSG_DATA(cmsg);
412 #endif /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
414 if (ret == sizeof(data))
416 struct thread *thread;
418 if (data.tid) thread = get_thread_from_id( data.tid );
419 else thread = (struct thread *)grab_object( get_process_first_thread( process ));
421 if (!thread || thread->process != process || thread->state == TERMINATED)
423 if (debug_level)
424 fprintf( stderr, "%04x: *fd* %d <- %d bad thread id\n",
425 data.tid, data.fd, fd );
426 close( fd );
428 else
430 if (debug_level)
431 fprintf( stderr, "%04x: *fd* %d <- %d\n",
432 thread->id, data.fd, fd );
433 thread_add_inflight_fd( thread, data.fd, fd );
435 if (thread) release_object( thread );
436 return 0;
439 if (!ret)
441 kill_process( process, 0 );
443 else if (ret > 0)
445 fprintf( stderr, "Protocol error: process %04x: partial recvmsg %d for fd\n",
446 process->id, ret );
447 if (fd != -1) close( fd );
448 kill_process( process, 1 );
450 else
452 if (errno != EWOULDBLOCK && errno != EAGAIN)
454 fprintf( stderr, "Protocol error: process %04x: ", process->id );
455 perror( "recvmsg" );
456 kill_process( process, 1 );
459 return -1;
462 /* send an fd to a client */
463 int send_client_fd( struct process *process, int fd, obj_handle_t handle )
465 struct iovec vec;
466 struct msghdr msghdr;
467 int ret;
469 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
470 msghdr.msg_accrightslen = sizeof(fd);
471 msghdr.msg_accrights = (void *)&fd;
472 #else /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
473 char cmsg_buffer[256];
474 struct cmsghdr *cmsg;
475 msghdr.msg_control = cmsg_buffer;
476 msghdr.msg_controllen = sizeof(cmsg_buffer);
477 msghdr.msg_flags = 0;
478 cmsg = CMSG_FIRSTHDR( &msghdr );
479 cmsg->cmsg_len = CMSG_LEN( sizeof(fd) );
480 cmsg->cmsg_level = SOL_SOCKET;
481 cmsg->cmsg_type = SCM_RIGHTS;
482 *(int *)CMSG_DATA(cmsg) = fd;
483 msghdr.msg_controllen = cmsg->cmsg_len;
484 #endif /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
486 msghdr.msg_name = NULL;
487 msghdr.msg_namelen = 0;
488 msghdr.msg_iov = &vec;
489 msghdr.msg_iovlen = 1;
491 vec.iov_base = (void *)&handle;
492 vec.iov_len = sizeof(handle);
494 if (debug_level)
495 fprintf( stderr, "%04x: *fd* %04x -> %d\n", current ? current->id : process->id, handle, fd );
497 ret = sendmsg( get_unix_fd( process->msg_fd ), &msghdr, 0 );
499 if (ret == sizeof(handle)) return 0;
501 if (ret >= 0)
503 fprintf( stderr, "Protocol error: process %04x: partial sendmsg %d\n", process->id, ret );
504 kill_process( process, 1 );
506 else if (errno == EPIPE)
508 kill_process( process, 0 );
510 else
512 fprintf( stderr, "Protocol error: process %04x: ", process->id );
513 perror( "sendmsg" );
514 kill_process( process, 1 );
516 return -1;
519 /* get current tick count to return to client */
520 unsigned int get_tick_count(void)
522 #ifdef HAVE_CLOCK_GETTIME
523 struct timespec ts;
524 #ifdef CLOCK_MONOTONIC_RAW
525 if (!clock_gettime( CLOCK_MONOTONIC_RAW, &ts ))
526 return ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
527 #endif
528 if (!clock_gettime( CLOCK_MONOTONIC, &ts ))
529 return ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
530 #elif defined(__APPLE__)
531 static mach_timebase_info_data_t timebase;
533 if (!timebase.denom) mach_timebase_info( &timebase );
534 return mach_absolute_time() * timebase.numer / timebase.denom / 1000000;
535 #endif
536 return (current_time - server_start_time) / 10000;
539 static void master_socket_dump( struct object *obj, int verbose )
541 struct master_socket *sock = (struct master_socket *)obj;
542 assert( obj->ops == &master_socket_ops );
543 fprintf( stderr, "Master socket fd=%p\n", sock->fd );
546 static void master_socket_destroy( struct object *obj )
548 struct master_socket *sock = (struct master_socket *)obj;
549 assert( obj->ops == &master_socket_ops );
550 release_object( sock->fd );
553 /* handle a socket event */
554 static void master_socket_poll_event( struct fd *fd, int event )
556 struct master_socket *sock = get_fd_user( fd );
557 assert( master_socket->obj.ops == &master_socket_ops );
559 assert( sock == master_socket ); /* there is only one master socket */
561 if (event & (POLLERR | POLLHUP))
563 /* this is not supposed to happen */
564 fprintf( stderr, "wineserver: Error on master socket\n" );
565 set_fd_events( sock->fd, -1 );
567 else if (event & POLLIN)
569 struct sockaddr_un dummy;
570 socklen_t len = sizeof(dummy);
571 int client = accept( get_unix_fd( master_socket->fd ), (struct sockaddr *) &dummy, &len );
572 if (client == -1) return;
573 fcntl( client, F_SETFL, O_NONBLOCK );
574 create_process( client, NULL, 0 );
578 /* remove the socket upon exit */
579 static void socket_cleanup(void)
581 static int do_it_once;
582 if (!do_it_once++) unlink( server_socket_name );
585 /* create a directory and check its permissions */
586 static void create_dir( const char *name, struct stat *st )
588 if (lstat( name, st ) == -1)
590 if (errno != ENOENT)
591 fatal_error( "lstat %s: %s", name, strerror( errno ));
592 if (mkdir( name, 0700 ) == -1 && errno != EEXIST)
593 fatal_error( "mkdir %s: %s\n", name, strerror( errno ));
594 if (lstat( name, st ) == -1)
595 fatal_error( "lstat %s: %s\n", name, strerror( errno ));
597 if (!S_ISDIR(st->st_mode)) fatal_error( "%s is not a directory\n", name );
598 if (st->st_uid != getuid()) fatal_error( "%s is not owned by you\n", name );
599 if (st->st_mode & 077) fatal_error( "%s must not be accessible by other users\n", name );
602 /* create the server directory and chdir to it */
603 static void create_server_dir( const char *dir )
605 char *p, *server_dir;
606 struct stat st, st2;
608 if (!(server_dir = strdup( dir ))) fatal_error( "out of memory\n" );
610 /* first create the base directory if needed */
612 p = strrchr( server_dir, '/' );
613 *p = 0;
614 create_dir( server_dir, &st );
616 /* now create the server directory */
618 *p = '/';
619 create_dir( server_dir, &st );
621 if (chdir( server_dir ) == -1)
622 fatal_error( "chdir %s: %s\n", server_dir, strerror( errno ));
623 if ((server_dir_fd = open( ".", O_RDONLY )) == -1)
624 fatal_error( "open %s: %s\n", server_dir, strerror( errno ));
625 if (fstat( server_dir_fd, &st2 ) == -1)
626 fatal_error( "stat %s: %s\n", server_dir, strerror( errno ));
627 if (st.st_dev != st2.st_dev || st.st_ino != st2.st_ino)
628 fatal_error( "chdir did not end up in %s\n", server_dir );
630 free( server_dir );
633 /* create the lock file and return its file descriptor */
634 static int create_server_lock(void)
636 struct stat st;
637 int fd;
639 if (lstat( server_lock_name, &st ) == -1)
641 if (errno != ENOENT)
642 fatal_error( "lstat %s/%s: %s", wine_get_server_dir(), server_lock_name, strerror( errno ));
644 else
646 if (!S_ISREG(st.st_mode))
647 fatal_error( "%s/%s is not a regular file\n", wine_get_server_dir(), server_lock_name );
650 if ((fd = open( server_lock_name, O_CREAT|O_TRUNC|O_WRONLY, 0600 )) == -1)
651 fatal_error( "error creating %s/%s: %s", wine_get_server_dir(), server_lock_name, strerror( errno ));
652 return fd;
655 /* wait for the server lock */
656 int wait_for_lock(void)
658 const char *server_dir = wine_get_server_dir();
659 int fd, r;
660 struct flock fl;
662 if (!server_dir) return 0; /* no server dir, so no lock to wait on */
664 create_server_dir( server_dir );
665 fd = create_server_lock();
667 fl.l_type = F_WRLCK;
668 fl.l_whence = SEEK_SET;
669 fl.l_start = 0;
670 fl.l_len = 1;
671 r = fcntl( fd, F_SETLKW, &fl );
672 close(fd);
674 return r;
677 /* kill the wine server holding the lock */
678 int kill_lock_owner( int sig )
680 const char *server_dir = wine_get_server_dir();
681 int fd, i, ret = 0;
682 pid_t pid = 0;
683 struct flock fl;
685 if (!server_dir) return 0; /* no server dir, nothing to do */
687 create_server_dir( server_dir );
688 fd = create_server_lock();
690 for (i = 1; i <= 20; i++)
692 fl.l_type = F_WRLCK;
693 fl.l_whence = SEEK_SET;
694 fl.l_start = 0;
695 fl.l_len = 1;
696 if (fcntl( fd, F_GETLK, &fl ) == -1) goto done;
697 if (fl.l_type != F_WRLCK) goto done; /* the file is not locked */
698 if (!pid) /* first time around */
700 if (!(pid = fl.l_pid)) goto done; /* shouldn't happen */
701 if (sig == -1)
703 if (kill( pid, SIGINT ) == -1) goto done;
704 kill( pid, SIGCONT );
705 ret = 1;
707 else /* just send the specified signal and return */
709 ret = (kill( pid, sig ) != -1);
710 goto done;
713 else if (fl.l_pid != pid) goto done; /* no longer the same process */
714 usleep( 50000 * i );
716 /* waited long enough, now kill it */
717 kill( pid, SIGKILL );
719 done:
720 close( fd );
721 return ret;
724 /* acquire the main server lock */
725 static void acquire_lock(void)
727 struct sockaddr_un addr;
728 struct stat st;
729 struct flock fl;
730 int fd, slen, got_lock = 0;
732 fd = create_server_lock();
734 fl.l_type = F_WRLCK;
735 fl.l_whence = SEEK_SET;
736 fl.l_start = 0;
737 fl.l_len = 1;
738 if (fcntl( fd, F_SETLK, &fl ) != -1)
740 /* check for crashed server */
741 if (stat( server_socket_name, &st ) != -1 && /* there is a leftover socket */
742 stat( "core", &st ) != -1 && st.st_size) /* and there is a non-empty core file */
744 fprintf( stderr,
745 "Warning: a previous instance of the wine server seems to have crashed.\n"
746 "Please run 'gdb %s %s/core',\n"
747 "type 'backtrace' at the gdb prompt and report the results. Thanks.\n\n",
748 server_argv0, wine_get_server_dir() );
750 unlink( server_socket_name ); /* we got the lock, we can safely remove the socket */
751 got_lock = 1;
752 /* in that case we reuse fd without closing it, this ensures
753 * that we hold the lock until the process exits */
755 else
757 switch(errno)
759 case ENOLCK:
760 break;
761 case EACCES:
762 /* check whether locks work at all on this file system */
763 if (fcntl( fd, F_GETLK, &fl ) == -1) break;
764 /* fall through */
765 case EAGAIN:
766 exit(2); /* we didn't get the lock, exit with special status */
767 default:
768 fatal_error( "fcntl %s/%s: %s", wine_get_server_dir(), server_lock_name, strerror( errno ));
770 /* it seems we can't use locks on this fs, so we will use the socket existence as lock */
771 close( fd );
774 if ((fd = socket( AF_UNIX, SOCK_STREAM, 0 )) == -1) fatal_error( "socket: %s\n", strerror( errno ));
775 addr.sun_family = AF_UNIX;
776 strcpy( addr.sun_path, server_socket_name );
777 slen = sizeof(addr) - sizeof(addr.sun_path) + strlen(addr.sun_path) + 1;
778 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
779 addr.sun_len = slen;
780 #endif
781 if (bind( fd, (struct sockaddr *)&addr, slen ) == -1)
783 if ((errno == EEXIST) || (errno == EADDRINUSE))
785 if (got_lock)
786 fatal_error( "couldn't bind to the socket even though we hold the lock\n" );
787 exit(2); /* we didn't get the lock, exit with special status */
789 fatal_error( "bind: %s\n", strerror( errno ));
791 atexit( socket_cleanup );
792 chmod( server_socket_name, 0600 ); /* make sure no other user can connect */
793 if (listen( fd, 5 ) == -1) fatal_error( "listen: %s\n", strerror( errno ));
795 if (!(master_socket = alloc_object( &master_socket_ops )) ||
796 !(master_socket->fd = create_anonymous_fd( &master_socket_fd_ops, fd, &master_socket->obj, 0 )))
797 fatal_error( "out of memory\n" );
798 set_fd_events( master_socket->fd, POLLIN );
799 make_object_static( &master_socket->obj );
802 /* open the master server socket and start waiting for new clients */
803 void open_master_socket(void)
805 const char *server_dir = wine_get_server_dir();
806 const char *config_dir = wine_get_config_dir();
807 int fd, pid, status, sync_pipe[2];
808 char dummy;
810 /* make sure no request is larger than the maximum size */
811 assert( sizeof(union generic_request) == sizeof(struct request_max_size) );
812 assert( sizeof(union generic_reply) == sizeof(struct request_max_size) );
814 /* make sure the stdio fds are open */
815 fd = open( "/dev/null", O_RDWR );
816 while (fd >= 0 && fd <= 2) fd = dup( fd );
818 if (!server_dir)
819 fatal_error( "directory %s cannot be accessed\n", config_dir );
820 if (chdir( config_dir ) == -1)
821 fatal_error( "chdir to %s: %s\n", config_dir, strerror( errno ));
822 if ((config_dir_fd = open( ".", O_RDONLY )) == -1)
823 fatal_error( "open %s: %s\n", config_dir, strerror( errno ));
825 create_server_dir( server_dir );
827 if (!foreground)
829 if (pipe( sync_pipe ) == -1) fatal_error( "pipe: %s\n", strerror( errno ));
830 pid = fork();
831 switch( pid )
833 case 0: /* child */
834 setsid();
835 close( sync_pipe[0] );
837 acquire_lock();
839 /* close stdin and stdout */
840 dup2( fd, 0 );
841 dup2( fd, 1 );
843 /* signal parent */
844 dummy = 0;
845 write( sync_pipe[1], &dummy, 1 );
846 close( sync_pipe[1] );
847 break;
849 case -1:
850 fatal_error( "fork: %s\n", strerror( errno ));
851 break;
853 default: /* parent */
854 close( sync_pipe[1] );
856 /* wait for child to signal us and then exit */
857 if (read( sync_pipe[0], &dummy, 1 ) == 1) _exit(0);
859 /* child terminated, propagate exit status */
860 waitpid( pid, &status, 0 );
861 if (WIFEXITED(status)) _exit( WEXITSTATUS(status) );
862 _exit(1);
865 else /* remain in the foreground */
867 acquire_lock();
870 /* init the process tracing mechanism */
871 init_tracing_mechanism();
872 close( fd );
875 /* master socket timer expiration handler */
876 static void close_socket_timeout( void *arg )
878 master_timeout = NULL;
879 flush_registry();
880 if (debug_level) fprintf( stderr, "wineserver: exiting (pid=%ld)\n", (long) getpid() );
882 #ifdef DEBUG_OBJECTS
883 close_objects(); /* shut down everything properly */
884 #endif
885 exit( 0 );
888 /* close the master socket and stop waiting for new clients */
889 void close_master_socket( timeout_t timeout )
891 if (master_socket)
893 release_object( master_socket );
894 master_socket = NULL;
896 if (master_timeout) /* cancel previous timeout */
897 remove_timeout_user( master_timeout );
899 master_timeout = add_timeout_user( timeout, close_socket_timeout, NULL );