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
;
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
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
)
425 fprintf( stderr
, "%04x: *fd* %d <- %d bad thread id\n",
426 data
.tid
, data
.fd
, fd
);
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
);
442 kill_process( process
, 0 );
446 fprintf( stderr
, "Protocol error: process %04x: partial recvmsg %d for fd\n",
448 if (fd
!= -1) close( fd
);
449 kill_process( process
, 1 );
453 if (errno
!= EWOULDBLOCK
&& (EWOULDBLOCK
== EAGAIN
|| errno
!= EAGAIN
))
455 fprintf( stderr
, "Protocol error: process %04x: ", process
->id
);
457 kill_process( process
, 1 );
463 /* send an fd to a client */
464 int send_client_fd( struct process
*process
, int fd
, obj_handle_t handle
)
467 struct msghdr msghdr
;
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
);
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;
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 );
513 fprintf( stderr
, "Protocol error: process %04x: ", process
->id
);
515 kill_process( process
, 1 );
520 /* return a monotonic time counter */
521 timeout_t
monotonic_counter(void)
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;
531 return mach_absolute_time() * timebase
.numer
/ timebase
.denom
/ 100;
532 #elif defined(HAVE_CLOCK_GETTIME)
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;
538 if (!clock_gettime( CLOCK_MONOTONIC
, &ts
))
539 return (timeout_t
)ts
.tv_sec
* TICKS_PER_SEC
+ ts
.tv_nsec
/ 100;
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)
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
;
618 size_t len
= sizeof("/server-") + 2 * sizeof(st
.st_dev
) + 2 * sizeof(st
.st_ino
) + 2;
620 /* open the configuration directory */
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
);
632 const char *home
= getenv( "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
));
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" );
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() );
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
);
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
);
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
);
704 /* create the lock file and return its file descriptor */
705 static int create_server_lock(void)
710 if (lstat( server_lock_name
, &st
) == -1)
713 fatal_error( "lstat %s/%s: %s\n", server_dir
, server_lock_name
, strerror( errno
));
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
));
726 /* wait for the server lock */
727 int wait_for_lock(void)
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();
738 fl
.l_whence
= SEEK_SET
;
741 r
= fcntl( fd
, F_SETLKW
, &fl
);
747 /* kill the wine server holding the lock */
748 int kill_lock_owner( int sig
)
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
++)
762 fl
.l_whence
= SEEK_SET
;
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 */
772 if (kill( pid
, SIGINT
) == -1) goto done
;
773 kill( pid
, SIGCONT
);
776 else /* just send the specified signal and return */
778 ret
= (kill( pid
, sig
) != -1);
782 else if (fl
.l_pid
!= pid
) goto done
; /* no longer the same process */
785 /* waited long enough, now kill it */
786 kill( pid
, SIGKILL
);
793 /* acquire the main server lock */
794 static void acquire_lock(void)
796 struct sockaddr_un addr
;
799 int fd
, slen
, got_lock
= 0;
801 fd
= create_server_lock();
804 fl
.l_whence
= SEEK_SET
;
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 */
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 */
821 /* in that case we reuse fd without closing it, this ensures
822 * that we hold the lock until the process exits */
831 /* check whether locks work at all on this file system */
832 if (fcntl( fd
, F_GETLK
, &fl
) == -1) break;
835 exit(2); /* we didn't get the lock, exit with special status */
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 */
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
850 if (bind( fd
, (struct sockaddr
*)&addr
, slen
) == -1)
852 if ((errno
== EEXIST
) || (errno
== EADDRINUSE
))
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];
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 );
889 if (pipe( sync_pipe
) == -1) fatal_error( "pipe: %s\n", strerror( errno
));
895 close( sync_pipe
[0] );
899 /* close stdin and stdout */
905 write( sync_pipe
[1], &dummy
, 1 );
906 close( sync_pipe
[1] );
910 fatal_error( "fork: %s\n", strerror( errno
));
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
) );
925 else /* remain in the foreground */
930 /* init the process tracing mechanism */
931 init_tracing_mechanism();
935 /* master socket timer expiration handler */
936 static void close_socket_timeout( void *arg
)
938 master_timeout
= NULL
;
940 if (debug_level
) fprintf( stderr
, "wineserver: exiting (pid=%ld)\n", (long) getpid() );
943 close_objects(); /* shut down everything properly */
948 /* close the master socket and stop waiting for new clients */
949 void close_master_socket( timeout_t timeout
)
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
);