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
36 #include <sys/types.h>
37 #include <sys/socket.h>
48 # include <mach/mach_time.h>
52 #define WIN32_NO_STATUS
63 #define WANT_REQUEST_HANDLERS
66 /* Some versions of glibc don't define this */
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 */
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 */
89 master_socket_dump
, /* dump */
90 no_add_queue
, /* add_queue */
91 NULL
, /* remove_queue */
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 */
114 NULL
, /* get_fd_type */
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
, ... )
136 va_start( args
, err
);
137 fprintf( stderr
, "Protocol error:%04x: ", thread
->id
);
138 vfprintf( stderr
, err
, args
);
140 thread
->exit_code
= 1;
141 kill_thread( thread
, 1 );
144 /* die on a fatal error */
145 void fatal_error( const char *err
, ... )
149 va_start( args
, err
);
150 fprintf( stderr
, "wineserver: " );
151 vfprintf( stderr
, err
, args
);
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
;
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
);
190 if (attr
->sd_len
&& !sd_is_valid( (const struct security_descriptor
*)(attr
+ 1), attr
->sd_len
))
192 set_error( STATUS_INVALID_SECURITY_DESCR
);
195 if ((attr
->name_len
& (sizeof(WCHAR
) - 1)) || attr
->name_len
>= 65534)
197 set_error( STATUS_OBJECT_NAME_INVALID
);
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
);
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())
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
)
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 );
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
)
254 if (!current
->reply_size
)
256 if ((ret
= write( get_unix_fd( current
->reply_fd
),
257 reply
, sizeof(*reply
) )) != sizeof(*reply
)) goto error
;
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 );
278 free( current
->reply_data
);
279 current
->reply_data
= NULL
;
284 fatal_protocol_error( current
, "partial write %d\n", ret
);
285 else if (errno
== EPIPE
)
286 kill_thread( current
, 0 ); /* normal death */
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
;
298 current
->reply_size
= 0;
300 memset( &reply
, 0, sizeof(reply
) );
302 if (debug_level
) trace_request();
304 if (req
< REQ_NB_REQUESTS
)
305 req_handlers
[req
]( ¤t
->req
, &reply
);
307 set_error( STATUS_NOT_IMPLEMENTED
);
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
);
320 current
->exit_code
= 1;
321 kill_thread( current
, 1 ); /* no way to continue without reply fd */
327 /* read a request from a thread */
328 void read_request( struct thread
*thread
)
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
);
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
);
350 /* read the variable sized data */
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
);
358 if (!(thread
->req_toread
-= ret
))
360 call_req_handler( thread
);
361 free( thread
->req_data
);
362 thread
->req_data
= NULL
;
368 if (!ret
) /* closed pipe */
369 kill_thread( thread
, 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
)
381 struct msghdr msghdr
;
382 char cmsg_buffer
[256];
385 msghdr
.msg_name
= NULL
;
386 msghdr
.msg_namelen
= 0;
387 msghdr
.msg_iov
= &vec
;
388 msghdr
.msg_iovlen
= 1;
389 msghdr
.msg_control
= cmsg_buffer
;
390 msghdr
.msg_controllen
= sizeof(cmsg_buffer
);
391 msghdr
.msg_flags
= 0;
393 vec
.iov_base
= (void *)&data
;
394 vec
.iov_len
= sizeof(data
);
396 ret
= recvmsg( get_unix_fd( process
->msg_fd
), &msghdr
, 0 );
400 struct cmsghdr
*cmsg
;
401 for (cmsg
= CMSG_FIRSTHDR( &msghdr
); cmsg
; cmsg
= CMSG_NXTHDR( &msghdr
, cmsg
))
403 if (cmsg
->cmsg_level
!= SOL_SOCKET
) continue;
404 if (cmsg
->cmsg_type
== SCM_RIGHTS
) fd
= *(int *)CMSG_DATA(cmsg
);
408 if (ret
== sizeof(data
))
410 struct thread
*thread
;
412 if (data
.tid
) thread
= get_thread_from_id( data
.tid
);
413 else thread
= (struct thread
*)grab_object( get_process_first_thread( process
));
415 if (!thread
|| thread
->process
!= process
|| thread
->state
== TERMINATED
)
418 fprintf( stderr
, "%04x: *fd* %d <- %d bad thread id\n",
419 data
.tid
, data
.fd
, fd
);
425 fprintf( stderr
, "%04x: *fd* %d <- %d\n",
426 thread
->id
, data
.fd
, fd
);
427 thread_add_inflight_fd( thread
, data
.fd
, fd
);
429 if (thread
) release_object( thread
);
435 kill_process( process
, 0 );
439 fprintf( stderr
, "Protocol error: process %04x: partial recvmsg %d for fd\n",
441 if (fd
!= -1) close( fd
);
442 kill_process( process
, 1 );
446 if (errno
!= EWOULDBLOCK
&& (EWOULDBLOCK
== EAGAIN
|| errno
!= EAGAIN
))
448 fprintf( stderr
, "Protocol error: process %04x: ", process
->id
);
450 kill_process( process
, 1 );
456 /* send an fd to a client */
457 int send_client_fd( struct process
*process
, int fd
, obj_handle_t handle
)
460 struct msghdr msghdr
;
461 char cmsg_buffer
[256];
462 struct cmsghdr
*cmsg
;
465 msghdr
.msg_name
= NULL
;
466 msghdr
.msg_namelen
= 0;
467 msghdr
.msg_iov
= &vec
;
468 msghdr
.msg_iovlen
= 1;
469 msghdr
.msg_control
= cmsg_buffer
;
470 msghdr
.msg_controllen
= sizeof(cmsg_buffer
);
471 msghdr
.msg_flags
= 0;
473 vec
.iov_base
= (void *)&handle
;
474 vec
.iov_len
= sizeof(handle
);
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
;
484 fprintf( stderr
, "%04x: *fd* %04x -> %d\n", current
? current
->id
: process
->id
, handle
, fd
);
486 ret
= sendmsg( get_unix_fd( process
->msg_fd
), &msghdr
, 0 );
488 if (ret
== sizeof(handle
)) return 0;
492 fprintf( stderr
, "Protocol error: process %04x: partial sendmsg %d\n", process
->id
, ret
);
493 kill_process( process
, 1 );
495 else if (errno
== EPIPE
)
497 kill_process( process
, 0 );
501 fprintf( stderr
, "Protocol error: process %04x: ", process
->id
);
503 kill_process( process
, 1 );
508 /* return a monotonic time counter */
509 timeout_t
monotonic_counter(void)
512 static mach_timebase_info_data_t timebase
;
514 if (!timebase
.denom
) mach_timebase_info( &timebase
);
515 #ifdef HAVE_MACH_CONTINUOUS_TIME
516 if (&mach_continuous_time
!= NULL
)
517 return mach_continuous_time() * timebase
.numer
/ timebase
.denom
/ 100;
519 return mach_absolute_time() * timebase
.numer
/ timebase
.denom
/ 100;
520 #elif defined(HAVE_CLOCK_GETTIME)
522 #ifdef CLOCK_MONOTONIC_RAW
523 if (!clock_gettime( CLOCK_MONOTONIC_RAW
, &ts
))
524 return (timeout_t
)ts
.tv_sec
* TICKS_PER_SEC
+ ts
.tv_nsec
/ 100;
526 if (!clock_gettime( CLOCK_MONOTONIC
, &ts
))
527 return (timeout_t
)ts
.tv_sec
* TICKS_PER_SEC
+ ts
.tv_nsec
/ 100;
529 return current_time
- server_start_time
;
532 static void master_socket_dump( struct object
*obj
, int verbose
)
534 struct master_socket
*sock
= (struct master_socket
*)obj
;
535 assert( obj
->ops
== &master_socket_ops
);
536 fprintf( stderr
, "Master socket fd=%p\n", sock
->fd
);
539 static void master_socket_destroy( struct object
*obj
)
541 struct master_socket
*sock
= (struct master_socket
*)obj
;
542 assert( obj
->ops
== &master_socket_ops
);
543 release_object( sock
->fd
);
546 /* handle a socket event */
547 static void master_socket_poll_event( struct fd
*fd
, int event
)
549 struct master_socket
*sock
= get_fd_user( fd
);
550 assert( master_socket
->obj
.ops
== &master_socket_ops
);
552 assert( sock
== master_socket
); /* there is only one master socket */
554 if (event
& (POLLERR
| POLLHUP
))
556 /* this is not supposed to happen */
557 fprintf( stderr
, "wineserver: Error on master socket\n" );
558 set_fd_events( sock
->fd
, -1 );
560 else if (event
& POLLIN
)
562 struct process
*process
;
563 struct sockaddr_un dummy
;
564 socklen_t len
= sizeof(dummy
);
565 int client
= accept( get_unix_fd( master_socket
->fd
), (struct sockaddr
*) &dummy
, &len
);
566 if (client
== -1) return;
567 fcntl( client
, F_SETFL
, O_NONBLOCK
);
568 if ((process
= create_process( client
, NULL
, 0, NULL
, NULL
, NULL
, 0, NULL
)))
570 create_thread( -1, process
, NULL
);
571 release_object( process
);
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)
589 fatal_error( "lstat %s: %s\n", 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 char *create_server_dir( int force
)
603 const char *prefix
= getenv( "WINEPREFIX" );
604 char *p
, *config_dir
;
606 size_t len
= sizeof("/server-") + 2 * sizeof(st
.st_dev
) + 2 * sizeof(st
.st_ino
) + 2;
608 /* open the configuration directory */
612 if (!(config_dir
= strdup( prefix
))) fatal_error( "out of memory\n" );
613 for (p
= config_dir
+ strlen(config_dir
); p
> config_dir
; p
--) if (p
[-1] != '/') break;
614 if (p
> config_dir
) *p
= 0;
615 if (config_dir
[0] != '/')
616 fatal_error( "invalid directory %s in WINEPREFIX: not an absolute path\n", prefix
);
620 const char *home
= getenv( "HOME" );
623 struct passwd
*pwd
= getpwuid( getuid() );
624 if (pwd
) home
= pwd
->pw_dir
;
626 if (!home
) fatal_error( "could not determine your home directory\n" );
627 if (home
[0] != '/') fatal_error( "your home directory %s is not an absolute path\n", home
);
628 if (!(config_dir
= malloc( strlen(home
) + sizeof("/.wine") ))) fatal_error( "out of memory\n" );
629 strcpy( config_dir
, home
);
630 for (p
= config_dir
+ strlen(config_dir
); p
> config_dir
; p
--) if (p
[-1] != '/') break;
631 strcpy( p
, "/.wine" );
634 if (chdir( config_dir
) == -1)
636 if (errno
!= ENOENT
|| force
) fatal_error( "chdir to %s: %s\n", config_dir
, strerror( errno
));
639 if ((config_dir_fd
= open( ".", O_RDONLY
)) == -1)
640 fatal_error( "open %s: %s\n", config_dir
, strerror( errno
));
641 if (fstat( config_dir_fd
, &st
) == -1)
642 fatal_error( "stat %s: %s\n", config_dir
, strerror( errno
));
643 if (st
.st_uid
!= getuid())
644 fatal_error( "%s is not owned by you\n", config_dir
);
646 /* create the base directory if needed */
648 #ifdef __ANDROID__ /* there's no /tmp dir on Android */
649 len
+= strlen( config_dir
) + sizeof("/.wineserver");
650 if (!(server_dir
= malloc( len
))) fatal_error( "out of memory\n" );
651 strcpy( server_dir
, config_dir
);
652 strcat( server_dir
, "/.wineserver" );
654 len
+= sizeof("/tmp/.wine-") + 12;
655 if (!(server_dir
= malloc( len
))) fatal_error( "out of memory\n" );
656 sprintf( server_dir
, "/tmp/.wine-%u", getuid() );
658 create_dir( server_dir
, &st2
);
660 /* now create the server directory */
662 strcat( server_dir
, "/server-" );
663 p
= server_dir
+ strlen(server_dir
);
665 if (st
.st_dev
!= (unsigned long)st
.st_dev
)
666 p
+= sprintf( p
, "%lx%08lx-", (unsigned long)((unsigned long long)st
.st_dev
>> 32),
667 (unsigned long)st
.st_dev
);
669 p
+= sprintf( p
, "%lx-", (unsigned long)st
.st_dev
);
671 if (st
.st_ino
!= (unsigned long)st
.st_ino
)
672 sprintf( p
, "%lx%08lx", (unsigned long)((unsigned long long)st
.st_ino
>> 32),
673 (unsigned long)st
.st_ino
);
675 sprintf( p
, "%lx", (unsigned long)st
.st_ino
);
677 create_dir( server_dir
, &st
);
679 if (chdir( server_dir
) == -1)
680 fatal_error( "chdir %s: %s\n", server_dir
, strerror( errno
));
681 if ((server_dir_fd
= open( ".", O_RDONLY
)) == -1)
682 fatal_error( "open %s: %s\n", server_dir
, strerror( errno
));
683 if (fstat( server_dir_fd
, &st2
) == -1)
684 fatal_error( "stat %s: %s\n", server_dir
, strerror( errno
));
685 if (st
.st_dev
!= st2
.st_dev
|| st
.st_ino
!= st2
.st_ino
)
686 fatal_error( "chdir did not end up in %s\n", server_dir
);
692 /* create the lock file and return its file descriptor */
693 static int create_server_lock(void)
698 if (lstat( server_lock_name
, &st
) == -1)
701 fatal_error( "lstat %s/%s: %s\n", server_dir
, server_lock_name
, strerror( errno
));
705 if (!S_ISREG(st
.st_mode
))
706 fatal_error( "%s/%s is not a regular file\n", server_dir
, server_lock_name
);
709 if ((fd
= open( server_lock_name
, O_CREAT
|O_TRUNC
|O_WRONLY
, 0600 )) == -1)
710 fatal_error( "error creating %s/%s: %s\n", server_dir
, server_lock_name
, strerror( errno
));
714 /* wait for the server lock */
715 int wait_for_lock(void)
720 server_dir
= create_server_dir( 0 );
721 if (!server_dir
) return 0; /* no server dir, so no lock to wait on */
723 fd
= create_server_lock();
726 fl
.l_whence
= SEEK_SET
;
729 r
= fcntl( fd
, F_SETLKW
, &fl
);
735 /* kill the wine server holding the lock */
736 int kill_lock_owner( int sig
)
742 server_dir
= create_server_dir( 0 );
743 if (!server_dir
) return 0; /* no server dir, nothing to do */
745 fd
= create_server_lock();
747 for (i
= 1; i
<= 20; i
++)
750 fl
.l_whence
= SEEK_SET
;
753 if (fcntl( fd
, F_GETLK
, &fl
) == -1) goto done
;
754 if (fl
.l_type
!= F_WRLCK
) goto done
; /* the file is not locked */
755 if (!pid
) /* first time around */
757 if (!(pid
= fl
.l_pid
)) goto done
; /* shouldn't happen */
760 if (kill( pid
, SIGINT
) == -1) goto done
;
761 kill( pid
, SIGCONT
);
764 else /* just send the specified signal and return */
766 ret
= (kill( pid
, sig
) != -1);
770 else if (fl
.l_pid
!= pid
) goto done
; /* no longer the same process */
773 /* waited long enough, now kill it */
774 kill( pid
, SIGKILL
);
781 /* acquire the main server lock */
782 static void acquire_lock(void)
784 struct sockaddr_un addr
;
787 int fd
, slen
, got_lock
= 0;
789 fd
= create_server_lock();
792 fl
.l_whence
= SEEK_SET
;
795 if (fcntl( fd
, F_SETLK
, &fl
) != -1)
797 /* check for crashed server */
798 if (stat( server_socket_name
, &st
) != -1 && /* there is a leftover socket */
799 stat( "core", &st
) != -1 && st
.st_size
) /* and there is a non-empty core file */
802 "Warning: a previous instance of the wine server seems to have crashed.\n"
803 "Please run 'gdb %s %s/core',\n"
804 "type 'backtrace' at the gdb prompt and report the results. Thanks.\n\n",
805 server_argv0
, server_dir
);
807 unlink( server_socket_name
); /* we got the lock, we can safely remove the socket */
809 /* in that case we reuse fd without closing it, this ensures
810 * that we hold the lock until the process exits */
819 /* check whether locks work at all on this file system */
820 if (fcntl( fd
, F_GETLK
, &fl
) == -1) break;
823 exit(2); /* we didn't get the lock, exit with special status */
825 fatal_error( "fcntl %s/%s: %s\n", server_dir
, server_lock_name
, strerror( errno
));
827 /* it seems we can't use locks on this fs, so we will use the socket existence as lock */
831 if ((fd
= socket( AF_UNIX
, SOCK_STREAM
, 0 )) == -1) fatal_error( "socket: %s\n", strerror( errno
));
832 addr
.sun_family
= AF_UNIX
;
833 strcpy( addr
.sun_path
, server_socket_name
);
834 slen
= sizeof(addr
) - sizeof(addr
.sun_path
) + strlen(addr
.sun_path
) + 1;
835 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
838 if (bind( fd
, (struct sockaddr
*)&addr
, slen
) == -1)
840 if ((errno
== EEXIST
) || (errno
== EADDRINUSE
))
843 fatal_error( "couldn't bind to the socket even though we hold the lock\n" );
844 exit(2); /* we didn't get the lock, exit with special status */
846 fatal_error( "bind: %s\n", strerror( errno
));
848 atexit( socket_cleanup
);
849 chmod( server_socket_name
, 0600 ); /* make sure no other user can connect */
850 if (listen( fd
, 5 ) == -1) fatal_error( "listen: %s\n", strerror( errno
));
852 if (!(master_socket
= alloc_object( &master_socket_ops
)) ||
853 !(master_socket
->fd
= create_anonymous_fd( &master_socket_fd_ops
, fd
, &master_socket
->obj
, 0 )))
854 fatal_error( "out of memory\n" );
855 set_fd_events( master_socket
->fd
, POLLIN
);
856 make_object_permanent( &master_socket
->obj
);
859 /* open the master server socket and start waiting for new clients */
860 void open_master_socket(void)
862 int fd
, pid
, status
, sync_pipe
[2];
865 /* make sure no request is larger than the maximum size */
866 assert( sizeof(union generic_request
) == sizeof(struct request_max_size
) );
867 assert( sizeof(union generic_reply
) == sizeof(struct request_max_size
) );
869 /* make sure the stdio fds are open */
870 fd
= open( "/dev/null", O_RDWR
);
871 while (fd
>= 0 && fd
<= 2) fd
= dup( fd
);
873 server_dir
= create_server_dir( 1 );
877 if (pipe( sync_pipe
) == -1) fatal_error( "pipe: %s\n", strerror( errno
));
883 close( sync_pipe
[0] );
887 /* close stdin and stdout */
893 write( sync_pipe
[1], &dummy
, 1 );
894 close( sync_pipe
[1] );
898 fatal_error( "fork: %s\n", strerror( errno
));
901 default: /* parent */
902 close( sync_pipe
[1] );
904 /* wait for child to signal us and then exit */
905 if (read( sync_pipe
[0], &dummy
, 1 ) == 1) _exit(0);
907 /* child terminated, propagate exit status */
908 waitpid( pid
, &status
, 0 );
909 if (WIFEXITED(status
)) _exit( WEXITSTATUS(status
) );
913 else /* remain in the foreground */
918 /* init the process tracing mechanism */
919 init_tracing_mechanism();
923 /* master socket timer expiration handler */
924 static void close_socket_timeout( void *arg
)
926 master_timeout
= NULL
;
928 if (debug_level
) fprintf( stderr
, "wineserver: exiting (pid=%ld)\n", (long) getpid() );
931 close_objects(); /* shut down everything properly */
936 /* close the master socket and stop waiting for new clients */
937 void close_master_socket( timeout_t timeout
)
941 release_object( master_socket
);
942 master_socket
= NULL
;
944 if (master_timeout
) /* cancel previous timeout */
945 remove_timeout_user( master_timeout
);
947 master_timeout
= add_timeout_user( timeout
, close_socket_timeout
, NULL
);