wined3d: Don't setup FBO and draw buffers in wined3d_context_gl_apply_blit_state().
[wine.git] / server / request.c
blob7021741c765aad696a8b65095dccee07ec9477fe
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"
23 #include <assert.h>
24 #include <errno.h>
25 #include <fcntl.h>
26 #ifdef HAVE_PWD_H
27 #include <pwd.h>
28 #endif
29 #include <signal.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <stdarg.h>
33 #include <string.h>
34 #include <sys/stat.h>
35 #include <sys/time.h>
36 #include <sys/types.h>
37 #include <sys/socket.h>
38 #include <sys/wait.h>
39 #ifdef HAVE_SYS_UIO_H
40 #include <sys/uio.h>
41 #endif
42 #ifdef HAVE_SYS_UN_H
43 #include <sys/un.h>
44 #endif
45 #include <unistd.h>
46 #include <poll.h>
47 #ifdef __APPLE__
48 # include <mach/mach_time.h>
49 #endif
51 #include "ntstatus.h"
52 #define WIN32_NO_STATUS
53 #include "windef.h"
54 #include "winbase.h"
55 #include "wincon.h"
56 #include "winternl.h"
58 #include "file.h"
59 #include "process.h"
60 #include "thread.h"
61 #include "security.h"
62 #include "handle.h"
63 #define WANT_REQUEST_HANDLERS
64 #include "request.h"
66 /* Some versions of glibc don't define this */
67 #ifndef SCM_RIGHTS
68 #define SCM_RIGHTS 1
69 #endif
71 /* path names for server master Unix socket */
72 static const char * const server_socket_name = "socket"; /* name of the socket file */
73 static const char * const server_lock_name = "lock"; /* name of the server lock file */
75 struct master_socket
77 struct object obj; /* object header */
78 struct fd *fd; /* file descriptor of the master socket */
81 static void master_socket_dump( struct object *obj, int verbose );
82 static void master_socket_destroy( struct object *obj );
83 static void master_socket_poll_event( struct fd *fd, int event );
85 static const struct object_ops master_socket_ops =
87 sizeof(struct master_socket), /* size */
88 &no_type, /* type */
89 master_socket_dump, /* dump */
90 no_add_queue, /* add_queue */
91 NULL, /* remove_queue */
92 NULL, /* signaled */
93 NULL, /* satisfied */
94 no_signal, /* signal */
95 no_get_fd, /* get_fd */
96 default_map_access, /* map_access */
97 default_get_sd, /* get_sd */
98 default_set_sd, /* set_sd */
99 no_get_full_name, /* get_full_name */
100 no_lookup_name, /* lookup_name */
101 no_link_name, /* link_name */
102 NULL, /* unlink_name */
103 no_open_file, /* open_file */
104 no_kernel_obj_list, /* get_kernel_obj_list */
105 no_close_handle, /* close_handle */
106 master_socket_destroy /* destroy */
109 static const struct fd_ops master_socket_fd_ops =
111 NULL, /* get_poll_events */
112 master_socket_poll_event, /* poll_event */
113 NULL, /* flush */
114 NULL, /* get_fd_type */
115 NULL, /* ioctl */
116 NULL, /* queue_async */
117 NULL /* reselect_async */
121 struct thread *current = NULL; /* thread handling the current request */
122 unsigned int global_error = 0; /* global error code for when no thread is current */
123 timeout_t server_start_time = 0; /* server startup time */
124 char *server_dir = NULL; /* server directory */
125 int server_dir_fd = -1; /* file descriptor for the server dir */
126 int config_dir_fd = -1; /* file descriptor for the config dir */
128 static struct master_socket *master_socket; /* the master socket object */
129 static struct timeout_user *master_timeout;
131 /* complain about a protocol error and terminate the client connection */
132 void fatal_protocol_error( struct thread *thread, const char *err, ... )
134 va_list args;
136 va_start( args, err );
137 fprintf( stderr, "Protocol error:%04x: ", thread->id );
138 vfprintf( stderr, err, args );
139 va_end( args );
140 thread->exit_code = 1;
141 kill_thread( thread, 1 );
144 /* die on a fatal error */
145 void fatal_error( const char *err, ... )
147 va_list args;
149 va_start( args, err );
150 fprintf( stderr, "wineserver: " );
151 vfprintf( stderr, err, args );
152 va_end( args );
153 exit(1);
156 /* allocate the reply data */
157 void *set_reply_data_size( data_size_t size )
159 assert( size <= get_reply_max_size() );
160 if (size && !(current->reply_data = mem_alloc( size ))) size = 0;
161 current->reply_size = size;
162 return current->reply_data;
165 static const struct object_attributes empty_attributes;
167 /* return object attributes from the current request */
168 const struct object_attributes *get_req_object_attributes( const struct security_descriptor **sd,
169 struct unicode_str *name,
170 struct object **root )
172 const struct object_attributes *attr = get_req_data();
173 data_size_t size = get_req_data_size();
175 if (root) *root = NULL;
177 if (!size)
179 *sd = NULL;
180 name->len = 0;
181 return &empty_attributes;
184 if ((size < sizeof(*attr)) || (size - sizeof(*attr) < attr->sd_len) ||
185 (size - sizeof(*attr) - attr->sd_len < attr->name_len))
187 set_error( STATUS_ACCESS_VIOLATION );
188 return NULL;
190 if (attr->sd_len && !sd_is_valid( (const struct security_descriptor *)(attr + 1), attr->sd_len ))
192 set_error( STATUS_INVALID_SECURITY_DESCR );
193 return NULL;
195 if ((attr->name_len & (sizeof(WCHAR) - 1)) || attr->name_len >= 65534)
197 set_error( STATUS_OBJECT_NAME_INVALID );
198 return NULL;
200 if (root && attr->rootdir && attr->name_len)
202 if (!(*root = get_handle_obj( current->process, attr->rootdir, 0, NULL ))) return NULL;
204 *sd = attr->sd_len ? (const struct security_descriptor *)(attr + 1) : NULL;
205 name->len = attr->name_len;
206 name->str = (const WCHAR *)(attr + 1) + attr->sd_len / sizeof(WCHAR);
207 return attr;
210 /* return a pointer to the request data following an object attributes structure */
211 const void *get_req_data_after_objattr( const struct object_attributes *attr, data_size_t *len )
213 data_size_t size = (sizeof(*attr) + (attr->sd_len & ~1) + (attr->name_len & ~1) + 3) & ~3;
215 if (attr == &empty_attributes || size >= get_req_data_size())
217 *len = 0;
218 return NULL;
220 *len = get_req_data_size() - size;
221 return (const char *)get_req_data() + size;
224 /* write the remaining part of the reply */
225 void write_reply( struct thread *thread )
227 int ret;
229 if ((ret = write( get_unix_fd( thread->reply_fd ),
230 (char *)thread->reply_data + thread->reply_size - thread->reply_towrite,
231 thread->reply_towrite )) >= 0)
233 if (!(thread->reply_towrite -= ret))
235 free( thread->reply_data );
236 thread->reply_data = NULL;
237 /* sent everything, can go back to waiting for requests */
238 set_fd_events( thread->request_fd, POLLIN );
239 set_fd_events( thread->reply_fd, 0 );
241 return;
243 if (errno == EPIPE)
244 kill_thread( thread, 0 ); /* normal death */
245 else if (errno != EWOULDBLOCK && (EWOULDBLOCK == EAGAIN || errno != EAGAIN))
246 fatal_protocol_error( thread, "reply write: %s\n", strerror( errno ));
249 /* send a reply to the current thread */
250 static void send_reply( union generic_reply *reply )
252 int ret;
254 if (!current->reply_size)
256 if ((ret = write( get_unix_fd( current->reply_fd ),
257 reply, sizeof(*reply) )) != sizeof(*reply)) goto error;
259 else
261 struct iovec vec[2];
263 vec[0].iov_base = (void *)reply;
264 vec[0].iov_len = sizeof(*reply);
265 vec[1].iov_base = current->reply_data;
266 vec[1].iov_len = current->reply_size;
268 if ((ret = writev( get_unix_fd( current->reply_fd ), vec, 2 )) < sizeof(*reply)) goto error;
270 if ((current->reply_towrite = current->reply_size - (ret - sizeof(*reply))))
272 /* couldn't write it all, wait for POLLOUT */
273 set_fd_events( current->reply_fd, POLLOUT );
274 set_fd_events( current->request_fd, 0 );
275 return;
278 free( current->reply_data );
279 current->reply_data = NULL;
280 return;
282 error:
283 if (ret >= 0)
284 fatal_protocol_error( current, "partial write %d\n", ret );
285 else if (errno == EPIPE)
286 kill_thread( current, 0 ); /* normal death */
287 else
288 fatal_protocol_error( current, "reply write: %s\n", strerror( errno ));
291 /* call a request handler */
292 static void call_req_handler( struct thread *thread )
294 union generic_reply reply;
295 enum request req = thread->req.request_header.req;
297 current = thread;
298 current->reply_size = 0;
299 clear_error();
300 memset( &reply, 0, sizeof(reply) );
302 if (debug_level) trace_request();
304 if (req < REQ_NB_REQUESTS)
305 req_handlers[req]( &current->req, &reply );
306 else
307 set_error( STATUS_NOT_IMPLEMENTED );
309 if (current)
311 if (current->reply_fd)
313 reply.reply_header.error = current->error;
314 reply.reply_header.reply_size = current->reply_size;
315 if (debug_level) trace_reply( req, &reply );
316 send_reply( &reply );
318 else
320 current->exit_code = 1;
321 kill_thread( current, 1 ); /* no way to continue without reply fd */
324 current = NULL;
327 /* read a request from a thread */
328 void read_request( struct thread *thread )
330 int ret;
332 if (!thread->req_toread) /* no pending request */
334 if ((ret = read( get_unix_fd( thread->request_fd ), &thread->req,
335 sizeof(thread->req) )) != sizeof(thread->req)) goto error;
336 if (!(thread->req_toread = thread->req.request_header.request_size))
338 /* no data, handle request at once */
339 call_req_handler( thread );
340 return;
342 if (!(thread->req_data = malloc( thread->req_toread )))
344 fatal_protocol_error( thread, "no memory for %u bytes request %d\n",
345 thread->req_toread, thread->req.request_header.req );
346 return;
350 /* read the variable sized data */
351 for (;;)
353 ret = read( get_unix_fd( thread->request_fd ),
354 (char *)thread->req_data + thread->req.request_header.request_size
355 - thread->req_toread,
356 thread->req_toread );
357 if (ret <= 0) break;
358 if (!(thread->req_toread -= ret))
360 call_req_handler( thread );
361 free( thread->req_data );
362 thread->req_data = NULL;
363 return;
367 error:
368 if (!ret) /* closed pipe */
369 kill_thread( thread, 0 );
370 else if (ret > 0)
371 fatal_protocol_error( thread, "partial read %d\n", ret );
372 else if (errno != EWOULDBLOCK && (EWOULDBLOCK == EAGAIN || errno != EAGAIN))
373 fatal_protocol_error( thread, "read: %s\n", strerror( errno ));
376 /* receive a file descriptor on the process socket */
377 int receive_fd( struct process *process )
379 struct iovec vec;
380 struct send_fd data;
381 struct msghdr msghdr;
382 int fd = -1, ret;
384 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
385 msghdr.msg_accrightslen = sizeof(int);
386 msghdr.msg_accrights = (void *)&fd;
387 #else /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
388 char cmsg_buffer[256];
389 msghdr.msg_control = cmsg_buffer;
390 msghdr.msg_controllen = sizeof(cmsg_buffer);
391 msghdr.msg_flags = 0;
392 #endif /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
394 msghdr.msg_name = NULL;
395 msghdr.msg_namelen = 0;
396 msghdr.msg_iov = &vec;
397 msghdr.msg_iovlen = 1;
398 vec.iov_base = (void *)&data;
399 vec.iov_len = sizeof(data);
401 ret = recvmsg( get_unix_fd( process->msg_fd ), &msghdr, 0 );
403 #ifndef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
404 if (ret > 0)
406 struct cmsghdr *cmsg;
407 for (cmsg = CMSG_FIRSTHDR( &msghdr ); cmsg; cmsg = CMSG_NXTHDR( &msghdr, cmsg ))
409 if (cmsg->cmsg_level != SOL_SOCKET) continue;
410 if (cmsg->cmsg_type == SCM_RIGHTS) fd = *(int *)CMSG_DATA(cmsg);
413 #endif /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
415 if (ret == sizeof(data))
417 struct thread *thread;
419 if (data.tid) thread = get_thread_from_id( data.tid );
420 else thread = (struct thread *)grab_object( get_process_first_thread( process ));
422 if (!thread || thread->process != process || thread->state == TERMINATED)
424 if (debug_level)
425 fprintf( stderr, "%04x: *fd* %d <- %d bad thread id\n",
426 data.tid, data.fd, fd );
427 close( fd );
429 else
431 if (debug_level)
432 fprintf( stderr, "%04x: *fd* %d <- %d\n",
433 thread->id, data.fd, fd );
434 thread_add_inflight_fd( thread, data.fd, fd );
436 if (thread) release_object( thread );
437 return 0;
440 if (!ret)
442 kill_process( process, 0 );
444 else if (ret > 0)
446 fprintf( stderr, "Protocol error: process %04x: partial recvmsg %d for fd\n",
447 process->id, ret );
448 if (fd != -1) close( fd );
449 kill_process( process, 1 );
451 else
453 if (errno != EWOULDBLOCK && (EWOULDBLOCK == EAGAIN || errno != EAGAIN))
455 fprintf( stderr, "Protocol error: process %04x: ", process->id );
456 perror( "recvmsg" );
457 kill_process( process, 1 );
460 return -1;
463 /* send an fd to a client */
464 int send_client_fd( struct process *process, int fd, obj_handle_t handle )
466 struct iovec vec;
467 struct msghdr msghdr;
468 int ret;
470 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
471 msghdr.msg_accrightslen = sizeof(fd);
472 msghdr.msg_accrights = (void *)&fd;
473 #else /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
474 char cmsg_buffer[256];
475 struct cmsghdr *cmsg;
476 msghdr.msg_control = cmsg_buffer;
477 msghdr.msg_controllen = sizeof(cmsg_buffer);
478 msghdr.msg_flags = 0;
479 cmsg = CMSG_FIRSTHDR( &msghdr );
480 cmsg->cmsg_len = CMSG_LEN( sizeof(fd) );
481 cmsg->cmsg_level = SOL_SOCKET;
482 cmsg->cmsg_type = SCM_RIGHTS;
483 *(int *)CMSG_DATA(cmsg) = fd;
484 msghdr.msg_controllen = cmsg->cmsg_len;
485 #endif /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
487 msghdr.msg_name = NULL;
488 msghdr.msg_namelen = 0;
489 msghdr.msg_iov = &vec;
490 msghdr.msg_iovlen = 1;
492 vec.iov_base = (void *)&handle;
493 vec.iov_len = sizeof(handle);
495 if (debug_level)
496 fprintf( stderr, "%04x: *fd* %04x -> %d\n", current ? current->id : process->id, handle, fd );
498 ret = sendmsg( get_unix_fd( process->msg_fd ), &msghdr, 0 );
500 if (ret == sizeof(handle)) return 0;
502 if (ret >= 0)
504 fprintf( stderr, "Protocol error: process %04x: partial sendmsg %d\n", process->id, ret );
505 kill_process( process, 1 );
507 else if (errno == EPIPE)
509 kill_process( process, 0 );
511 else
513 fprintf( stderr, "Protocol error: process %04x: ", process->id );
514 perror( "sendmsg" );
515 kill_process( process, 1 );
517 return -1;
520 /* return a monotonic time counter */
521 timeout_t monotonic_counter(void)
523 #ifdef __APPLE__
524 static mach_timebase_info_data_t timebase;
526 if (!timebase.denom) mach_timebase_info( &timebase );
527 #ifdef HAVE_MACH_CONTINUOUS_TIME
528 if (&mach_continuous_time != NULL)
529 return mach_continuous_time() * timebase.numer / timebase.denom / 100;
530 #endif
531 return mach_absolute_time() * timebase.numer / timebase.denom / 100;
532 #elif defined(HAVE_CLOCK_GETTIME)
533 struct timespec ts;
534 #ifdef CLOCK_MONOTONIC_RAW
535 if (!clock_gettime( CLOCK_MONOTONIC_RAW, &ts ))
536 return (timeout_t)ts.tv_sec * TICKS_PER_SEC + ts.tv_nsec / 100;
537 #endif
538 if (!clock_gettime( CLOCK_MONOTONIC, &ts ))
539 return (timeout_t)ts.tv_sec * TICKS_PER_SEC + ts.tv_nsec / 100;
540 #endif
541 return current_time - server_start_time;
544 static void master_socket_dump( struct object *obj, int verbose )
546 struct master_socket *sock = (struct master_socket *)obj;
547 assert( obj->ops == &master_socket_ops );
548 fprintf( stderr, "Master socket fd=%p\n", sock->fd );
551 static void master_socket_destroy( struct object *obj )
553 struct master_socket *sock = (struct master_socket *)obj;
554 assert( obj->ops == &master_socket_ops );
555 release_object( sock->fd );
558 /* handle a socket event */
559 static void master_socket_poll_event( struct fd *fd, int event )
561 struct master_socket *sock = get_fd_user( fd );
562 assert( master_socket->obj.ops == &master_socket_ops );
564 assert( sock == master_socket ); /* there is only one master socket */
566 if (event & (POLLERR | POLLHUP))
568 /* this is not supposed to happen */
569 fprintf( stderr, "wineserver: Error on master socket\n" );
570 set_fd_events( sock->fd, -1 );
572 else if (event & POLLIN)
574 struct process *process;
575 struct sockaddr_un dummy;
576 socklen_t len = sizeof(dummy);
577 int client = accept( get_unix_fd( master_socket->fd ), (struct sockaddr *) &dummy, &len );
578 if (client == -1) return;
579 fcntl( client, F_SETFL, O_NONBLOCK );
580 if ((process = create_process( client, NULL, 0, NULL, NULL, NULL, 0, NULL )))
582 create_thread( -1, process, NULL );
583 release_object( process );
588 /* remove the socket upon exit */
589 static void socket_cleanup(void)
591 static int do_it_once;
592 if (!do_it_once++) unlink( server_socket_name );
595 /* create a directory and check its permissions */
596 static void create_dir( const char *name, struct stat *st )
598 if (lstat( name, st ) == -1)
600 if (errno != ENOENT)
601 fatal_error( "lstat %s: %s\n", name, strerror( errno ));
602 if (mkdir( name, 0700 ) == -1 && errno != EEXIST)
603 fatal_error( "mkdir %s: %s\n", name, strerror( errno ));
604 if (lstat( name, st ) == -1)
605 fatal_error( "lstat %s: %s\n", name, strerror( errno ));
607 if (!S_ISDIR(st->st_mode)) fatal_error( "%s is not a directory\n", name );
608 if (st->st_uid != getuid()) fatal_error( "%s is not owned by you\n", name );
609 if (st->st_mode & 077) fatal_error( "%s must not be accessible by other users\n", name );
612 /* create the server directory and chdir to it */
613 static char *create_server_dir( int force )
615 const char *prefix = getenv( "WINEPREFIX" );
616 char *p, *config_dir;
617 struct stat st, st2;
618 size_t len = sizeof("/server-") + 2 * sizeof(st.st_dev) + 2 * sizeof(st.st_ino) + 2;
620 /* open the configuration directory */
622 if (prefix)
624 if (!(config_dir = strdup( prefix ))) fatal_error( "out of memory\n" );
625 for (p = config_dir + strlen(config_dir); p > config_dir; p--) if (p[-1] != '/') break;
626 if (p > config_dir) *p = 0;
627 if (config_dir[0] != '/')
628 fatal_error( "invalid directory %s in WINEPREFIX: not an absolute path\n", prefix );
630 else
632 const char *home = getenv( "HOME" );
633 if (!home)
635 struct passwd *pwd = getpwuid( getuid() );
636 if (pwd) home = pwd->pw_dir;
638 if (!home) fatal_error( "could not determine your home directory\n" );
639 if (home[0] != '/') fatal_error( "your home directory %s is not an absolute path\n", home );
640 if (!(config_dir = malloc( strlen(home) + sizeof("/.wine") ))) fatal_error( "out of memory\n" );
641 strcpy( config_dir, home );
642 for (p = config_dir + strlen(config_dir); p > config_dir; p--) if (p[-1] != '/') break;
643 strcpy( p, "/.wine" );
646 if (chdir( config_dir ) == -1)
648 if (errno != ENOENT || force) fatal_error( "chdir to %s: %s\n", config_dir, strerror( errno ));
649 return NULL;
651 if ((config_dir_fd = open( ".", O_RDONLY )) == -1)
652 fatal_error( "open %s: %s\n", config_dir, strerror( errno ));
653 if (fstat( config_dir_fd, &st ) == -1)
654 fatal_error( "stat %s: %s\n", config_dir, strerror( errno ));
655 if (st.st_uid != getuid())
656 fatal_error( "%s is not owned by you\n", config_dir );
658 /* create the base directory if needed */
660 #ifdef __ANDROID__ /* there's no /tmp dir on Android */
661 len += strlen( config_dir ) + sizeof("/.wineserver");
662 if (!(server_dir = malloc( len ))) fatal_error( "out of memory\n" );
663 strcpy( server_dir, config_dir );
664 strcat( server_dir, "/.wineserver" );
665 #else
666 len += sizeof("/tmp/.wine-") + 12;
667 if (!(server_dir = malloc( len ))) fatal_error( "out of memory\n" );
668 sprintf( server_dir, "/tmp/.wine-%u", getuid() );
669 #endif
670 create_dir( server_dir, &st2 );
672 /* now create the server directory */
674 strcat( server_dir, "/server-" );
675 p = server_dir + strlen(server_dir);
677 if (st.st_dev != (unsigned long)st.st_dev)
678 p += sprintf( p, "%lx%08lx-", (unsigned long)((unsigned long long)st.st_dev >> 32),
679 (unsigned long)st.st_dev );
680 else
681 p += sprintf( p, "%lx-", (unsigned long)st.st_dev );
683 if (st.st_ino != (unsigned long)st.st_ino)
684 sprintf( p, "%lx%08lx", (unsigned long)((unsigned long long)st.st_ino >> 32),
685 (unsigned long)st.st_ino );
686 else
687 sprintf( p, "%lx", (unsigned long)st.st_ino );
689 create_dir( server_dir, &st );
691 if (chdir( server_dir ) == -1)
692 fatal_error( "chdir %s: %s\n", server_dir, strerror( errno ));
693 if ((server_dir_fd = open( ".", O_RDONLY )) == -1)
694 fatal_error( "open %s: %s\n", server_dir, strerror( errno ));
695 if (fstat( server_dir_fd, &st2 ) == -1)
696 fatal_error( "stat %s: %s\n", server_dir, strerror( errno ));
697 if (st.st_dev != st2.st_dev || st.st_ino != st2.st_ino)
698 fatal_error( "chdir did not end up in %s\n", server_dir );
700 free( config_dir );
701 return server_dir;
704 /* create the lock file and return its file descriptor */
705 static int create_server_lock(void)
707 struct stat st;
708 int fd;
710 if (lstat( server_lock_name, &st ) == -1)
712 if (errno != ENOENT)
713 fatal_error( "lstat %s/%s: %s\n", server_dir, server_lock_name, strerror( errno ));
715 else
717 if (!S_ISREG(st.st_mode))
718 fatal_error( "%s/%s is not a regular file\n", server_dir, server_lock_name );
721 if ((fd = open( server_lock_name, O_CREAT|O_TRUNC|O_WRONLY, 0600 )) == -1)
722 fatal_error( "error creating %s/%s: %s\n", server_dir, server_lock_name, strerror( errno ));
723 return fd;
726 /* wait for the server lock */
727 int wait_for_lock(void)
729 int fd, r;
730 struct flock fl;
732 server_dir = create_server_dir( 0 );
733 if (!server_dir) return 0; /* no server dir, so no lock to wait on */
735 fd = create_server_lock();
737 fl.l_type = F_WRLCK;
738 fl.l_whence = SEEK_SET;
739 fl.l_start = 0;
740 fl.l_len = 1;
741 r = fcntl( fd, F_SETLKW, &fl );
742 close(fd);
744 return r;
747 /* kill the wine server holding the lock */
748 int kill_lock_owner( int sig )
750 int fd, i, ret = 0;
751 pid_t pid = 0;
752 struct flock fl;
754 server_dir = create_server_dir( 0 );
755 if (!server_dir) return 0; /* no server dir, nothing to do */
757 fd = create_server_lock();
759 for (i = 1; i <= 20; i++)
761 fl.l_type = F_WRLCK;
762 fl.l_whence = SEEK_SET;
763 fl.l_start = 0;
764 fl.l_len = 1;
765 if (fcntl( fd, F_GETLK, &fl ) == -1) goto done;
766 if (fl.l_type != F_WRLCK) goto done; /* the file is not locked */
767 if (!pid) /* first time around */
769 if (!(pid = fl.l_pid)) goto done; /* shouldn't happen */
770 if (sig == -1)
772 if (kill( pid, SIGINT ) == -1) goto done;
773 kill( pid, SIGCONT );
774 ret = 1;
776 else /* just send the specified signal and return */
778 ret = (kill( pid, sig ) != -1);
779 goto done;
782 else if (fl.l_pid != pid) goto done; /* no longer the same process */
783 usleep( 50000 * i );
785 /* waited long enough, now kill it */
786 kill( pid, SIGKILL );
788 done:
789 close( fd );
790 return ret;
793 /* acquire the main server lock */
794 static void acquire_lock(void)
796 struct sockaddr_un addr;
797 struct stat st;
798 struct flock fl;
799 int fd, slen, got_lock = 0;
801 fd = create_server_lock();
803 fl.l_type = F_WRLCK;
804 fl.l_whence = SEEK_SET;
805 fl.l_start = 0;
806 fl.l_len = 1;
807 if (fcntl( fd, F_SETLK, &fl ) != -1)
809 /* check for crashed server */
810 if (stat( server_socket_name, &st ) != -1 && /* there is a leftover socket */
811 stat( "core", &st ) != -1 && st.st_size) /* and there is a non-empty core file */
813 fprintf( stderr,
814 "Warning: a previous instance of the wine server seems to have crashed.\n"
815 "Please run 'gdb %s %s/core',\n"
816 "type 'backtrace' at the gdb prompt and report the results. Thanks.\n\n",
817 server_argv0, server_dir );
819 unlink( server_socket_name ); /* we got the lock, we can safely remove the socket */
820 got_lock = 1;
821 /* in that case we reuse fd without closing it, this ensures
822 * that we hold the lock until the process exits */
824 else
826 switch(errno)
828 case ENOLCK:
829 break;
830 case EACCES:
831 /* check whether locks work at all on this file system */
832 if (fcntl( fd, F_GETLK, &fl ) == -1) break;
833 /* fall through */
834 case EAGAIN:
835 exit(2); /* we didn't get the lock, exit with special status */
836 default:
837 fatal_error( "fcntl %s/%s: %s\n", server_dir, server_lock_name, strerror( errno ));
839 /* it seems we can't use locks on this fs, so we will use the socket existence as lock */
840 close( fd );
843 if ((fd = socket( AF_UNIX, SOCK_STREAM, 0 )) == -1) fatal_error( "socket: %s\n", strerror( errno ));
844 addr.sun_family = AF_UNIX;
845 strcpy( addr.sun_path, server_socket_name );
846 slen = sizeof(addr) - sizeof(addr.sun_path) + strlen(addr.sun_path) + 1;
847 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
848 addr.sun_len = slen;
849 #endif
850 if (bind( fd, (struct sockaddr *)&addr, slen ) == -1)
852 if ((errno == EEXIST) || (errno == EADDRINUSE))
854 if (got_lock)
855 fatal_error( "couldn't bind to the socket even though we hold the lock\n" );
856 exit(2); /* we didn't get the lock, exit with special status */
858 fatal_error( "bind: %s\n", strerror( errno ));
860 atexit( socket_cleanup );
861 chmod( server_socket_name, 0600 ); /* make sure no other user can connect */
862 if (listen( fd, 5 ) == -1) fatal_error( "listen: %s\n", strerror( errno ));
864 if (!(master_socket = alloc_object( &master_socket_ops )) ||
865 !(master_socket->fd = create_anonymous_fd( &master_socket_fd_ops, fd, &master_socket->obj, 0 )))
866 fatal_error( "out of memory\n" );
867 set_fd_events( master_socket->fd, POLLIN );
868 make_object_permanent( &master_socket->obj );
871 /* open the master server socket and start waiting for new clients */
872 void open_master_socket(void)
874 int fd, pid, status, sync_pipe[2];
875 char dummy;
877 /* make sure no request is larger than the maximum size */
878 assert( sizeof(union generic_request) == sizeof(struct request_max_size) );
879 assert( sizeof(union generic_reply) == sizeof(struct request_max_size) );
881 /* make sure the stdio fds are open */
882 fd = open( "/dev/null", O_RDWR );
883 while (fd >= 0 && fd <= 2) fd = dup( fd );
885 server_dir = create_server_dir( 1 );
887 if (!foreground)
889 if (pipe( sync_pipe ) == -1) fatal_error( "pipe: %s\n", strerror( errno ));
890 pid = fork();
891 switch( pid )
893 case 0: /* child */
894 setsid();
895 close( sync_pipe[0] );
897 acquire_lock();
899 /* close stdin and stdout */
900 dup2( fd, 0 );
901 dup2( fd, 1 );
903 /* signal parent */
904 dummy = 0;
905 write( sync_pipe[1], &dummy, 1 );
906 close( sync_pipe[1] );
907 break;
909 case -1:
910 fatal_error( "fork: %s\n", strerror( errno ));
911 break;
913 default: /* parent */
914 close( sync_pipe[1] );
916 /* wait for child to signal us and then exit */
917 if (read( sync_pipe[0], &dummy, 1 ) == 1) _exit(0);
919 /* child terminated, propagate exit status */
920 waitpid( pid, &status, 0 );
921 if (WIFEXITED(status)) _exit( WEXITSTATUS(status) );
922 _exit(1);
925 else /* remain in the foreground */
927 acquire_lock();
930 /* init the process tracing mechanism */
931 init_tracing_mechanism();
932 close( fd );
935 /* master socket timer expiration handler */
936 static void close_socket_timeout( void *arg )
938 master_timeout = NULL;
939 flush_registry();
940 if (debug_level) fprintf( stderr, "wineserver: exiting (pid=%ld)\n", (long) getpid() );
942 #ifdef DEBUG_OBJECTS
943 close_objects(); /* shut down everything properly */
944 #endif
945 exit( 0 );
948 /* close the master socket and stop waiting for new clients */
949 void close_master_socket( timeout_t timeout )
951 if (master_socket)
953 release_object( master_socket );
954 master_socket = NULL;
956 if (master_timeout) /* cancel previous timeout */
957 remove_timeout_user( master_timeout );
959 master_timeout = add_timeout_user( timeout, close_socket_timeout, NULL );