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
22 #include "wine/port.h"
37 #include <sys/types.h>
38 #ifdef HAVE_SYS_SOCKET_H
39 # include <sys/socket.h>
41 #ifdef HAVE_SYS_WAIT_H
42 # include <sys/wait.h>
56 #define WIN32_NO_STATUS
61 #include "wine/library.h"
65 #define WANT_REQUEST_HANDLERS
68 /* Some versions of glibc don't define this */
73 /* path names for server master Unix socket */
74 static const char * const server_socket_name
= "socket"; /* name of the socket file */
75 static const char * const server_lock_name
= "lock"; /* name of the server lock file */
79 struct object obj
; /* object header */
80 struct fd
*fd
; /* file descriptor of the master socket */
83 static void master_socket_dump( struct object
*obj
, int verbose
);
84 static void master_socket_destroy( struct object
*obj
);
85 static void master_socket_poll_event( struct fd
*fd
, int event
);
87 static const struct object_ops master_socket_ops
=
89 sizeof(struct master_socket
), /* size */
90 master_socket_dump
, /* dump */
91 no_get_type
, /* get_type */
92 no_add_queue
, /* add_queue */
93 NULL
, /* remove_queue */
96 no_signal
, /* signal */
97 no_get_fd
, /* get_fd */
98 no_map_access
, /* map_access */
99 default_get_sd
, /* get_sd */
100 default_set_sd
, /* set_sd */
101 no_lookup_name
, /* lookup_name */
102 no_open_file
, /* open_file */
103 no_close_handle
, /* close_handle */
104 master_socket_destroy
/* destroy */
107 static const struct fd_ops master_socket_fd_ops
=
109 NULL
, /* get_poll_events */
110 master_socket_poll_event
, /* poll_event */
112 NULL
, /* get_fd_type */
114 NULL
, /* queue_async */
115 NULL
, /* reselect_async */
116 NULL
/* cancel_async */
120 struct thread
*current
= NULL
; /* thread handling the current request */
121 unsigned int global_error
= 0; /* global error code for when no thread is current */
122 timeout_t server_start_time
= 0; /* server startup time */
123 int server_dir_fd
= -1; /* file descriptor for the server dir */
124 int config_dir_fd
= -1; /* file descriptor for the config dir */
126 static struct master_socket
*master_socket
; /* the master socket object */
127 static struct timeout_user
*master_timeout
;
129 /* socket communication static structures */
130 static struct iovec myiovec
;
131 static struct msghdr msghdr
;
132 #ifndef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
137 size_t len
; /* size of structure */
138 int level
; /* SOL_SOCKET */
139 int type
; /* SCM_RIGHTS */
141 int fd
; /* fd to pass */
143 static struct cmsg_fd cmsg
= { { sizeof(cmsg
.header
) + sizeof(cmsg
.fd
), SOL_SOCKET
, SCM_RIGHTS
}, -1 };
144 #endif /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
146 /* complain about a protocol error and terminate the client connection */
147 void fatal_protocol_error( struct thread
*thread
, const char *err
, ... )
151 va_start( args
, err
);
152 fprintf( stderr
, "Protocol error:%04x: ", thread
->id
);
153 vfprintf( stderr
, err
, args
);
155 thread
->exit_code
= 1;
156 kill_thread( thread
, 1 );
159 /* complain about a protocol error and terminate the client connection */
160 void fatal_protocol_perror( struct thread
*thread
, const char *err
, ... )
164 va_start( args
, err
);
165 fprintf( stderr
, "Protocol error:%04x: ", thread
->id
);
166 vfprintf( stderr
, err
, args
);
169 thread
->exit_code
= 1;
170 kill_thread( thread
, 1 );
173 /* die on a fatal error */
174 void fatal_error( const char *err
, ... )
178 va_start( args
, err
);
179 fprintf( stderr
, "wineserver: " );
180 vfprintf( stderr
, err
, args
);
185 /* die on a fatal error */
186 void fatal_perror( const char *err
, ... )
190 va_start( args
, err
);
191 fprintf( stderr
, "wineserver: " );
192 vfprintf( stderr
, err
, args
);
198 /* allocate the reply data */
199 void *set_reply_data_size( data_size_t size
)
201 assert( size
<= get_reply_max_size() );
202 if (size
&& !(current
->reply_data
= mem_alloc( size
))) size
= 0;
203 current
->reply_size
= size
;
204 return current
->reply_data
;
207 /* write the remaining part of the reply */
208 void write_reply( struct thread
*thread
)
212 if ((ret
= write( get_unix_fd( thread
->reply_fd
),
213 (char *)thread
->reply_data
+ thread
->reply_size
- thread
->reply_towrite
,
214 thread
->reply_towrite
)) >= 0)
216 if (!(thread
->reply_towrite
-= ret
))
218 free( thread
->reply_data
);
219 thread
->reply_data
= NULL
;
220 /* sent everything, can go back to waiting for requests */
221 set_fd_events( thread
->request_fd
, POLLIN
);
222 set_fd_events( thread
->reply_fd
, 0 );
227 kill_thread( thread
, 0 ); /* normal death */
228 else if (errno
!= EWOULDBLOCK
&& errno
!= EAGAIN
)
229 fatal_protocol_perror( thread
, "reply write" );
232 /* send a reply to the current thread */
233 static void send_reply( union generic_reply
*reply
)
237 if (!current
->reply_size
)
239 if ((ret
= write( get_unix_fd( current
->reply_fd
),
240 reply
, sizeof(*reply
) )) != sizeof(*reply
)) goto error
;
246 vec
[0].iov_base
= (void *)reply
;
247 vec
[0].iov_len
= sizeof(*reply
);
248 vec
[1].iov_base
= current
->reply_data
;
249 vec
[1].iov_len
= current
->reply_size
;
251 if ((ret
= writev( get_unix_fd( current
->reply_fd
), vec
, 2 )) < sizeof(*reply
)) goto error
;
253 if ((current
->reply_towrite
= current
->reply_size
- (ret
- sizeof(*reply
))))
255 /* couldn't write it all, wait for POLLOUT */
256 set_fd_events( current
->reply_fd
, POLLOUT
);
257 set_fd_events( current
->request_fd
, 0 );
261 free( current
->reply_data
);
262 current
->reply_data
= NULL
;
267 fatal_protocol_error( current
, "partial write %d\n", ret
);
268 else if (errno
== EPIPE
)
269 kill_thread( current
, 0 ); /* normal death */
271 fatal_protocol_perror( current
, "reply write" );
274 /* call a request handler */
275 static void call_req_handler( struct thread
*thread
)
277 union generic_reply reply
;
278 enum request req
= thread
->req
.request_header
.req
;
281 current
->reply_size
= 0;
283 memset( &reply
, 0, sizeof(reply
) );
285 if (debug_level
) trace_request();
287 if (req
< REQ_NB_REQUESTS
)
288 req_handlers
[req
]( ¤t
->req
, &reply
);
290 set_error( STATUS_NOT_IMPLEMENTED
);
294 if (current
->reply_fd
)
296 reply
.reply_header
.error
= current
->error
;
297 reply
.reply_header
.reply_size
= current
->reply_size
;
298 if (debug_level
) trace_reply( req
, &reply
);
299 send_reply( &reply
);
303 current
->exit_code
= 1;
304 kill_thread( current
, 1 ); /* no way to continue without reply fd */
310 /* read a request from a thread */
311 void read_request( struct thread
*thread
)
315 if (!thread
->req_toread
) /* no pending request */
317 ret
= read( get_unix_fd( thread
->request_fd
), &thread
->req
, sizeof(thread
->req
) );
318 if (sizeof(thread
->req
) == REQ_SIZE_64
&& ret
== REQ_SIZE_32
)
320 /* Uh oh, we received a partial read, not an error per se, if
321 * we were connecting a 32-bit client to a 64-bit server
323 if (thread
->req
.request_header
.req
== REQ_init_thread
&&
324 thread
->req
.init_thread_request
.pointer_bytes
!= sizeof(void *))
326 /* Do nothing, this is a 32-bits client */
331 if (!(thread
->req_toread
= thread
->req
.request_header
.request_size
))
333 /* no data, handle request at once */
334 call_req_handler( thread
);
337 if (!(thread
->req_data
= malloc( thread
->req_toread
)))
339 fatal_protocol_error( thread
, "no memory for %u bytes request %d\n",
340 thread
->req_toread
, thread
->req
.request_header
.req
);
345 /* read the variable sized data */
348 ret
= read( get_unix_fd( thread
->request_fd
),
349 (char *)thread
->req_data
+ thread
->req
.request_header
.request_size
350 - thread
->req_toread
,
351 thread
->req_toread
);
353 if (!(thread
->req_toread
-= ret
))
355 call_req_handler( thread
);
356 free( thread
->req_data
);
357 thread
->req_data
= NULL
;
363 if (!ret
) /* closed pipe */
364 kill_thread( thread
, 0 );
366 fatal_protocol_error( thread
, "partial read %d\n", ret
);
367 else if (errno
!= EWOULDBLOCK
&& errno
!= EAGAIN
)
368 fatal_protocol_perror( thread
, "read" );
371 /* receive a file descriptor on the process socket */
372 int receive_fd( struct process
*process
)
377 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
378 msghdr
.msg_accrightslen
= sizeof(int);
379 msghdr
.msg_accrights
= (void *)&fd
;
380 #else /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
381 msghdr
.msg_control
= &cmsg
;
382 msghdr
.msg_controllen
= sizeof(cmsg
.header
) + sizeof(fd
);
384 #endif /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
386 myiovec
.iov_base
= (void *)&data
;
387 myiovec
.iov_len
= sizeof(data
);
389 ret
= recvmsg( get_unix_fd( process
->msg_fd
), &msghdr
, 0 );
390 #ifndef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
394 if (ret
== sizeof(data
))
396 struct thread
*thread
;
398 if (data
.tid
) thread
= get_thread_from_id( data
.tid
);
399 else thread
= (struct thread
*)grab_object( get_process_first_thread( process
));
401 if (!thread
|| thread
->process
!= process
|| thread
->state
== TERMINATED
)
404 fprintf( stderr
, "%04x: *fd* %d <- %d bad thread id\n",
405 data
.tid
, data
.fd
, fd
);
411 fprintf( stderr
, "%04x: *fd* %d <- %d\n",
412 thread
->id
, data
.fd
, fd
);
413 thread_add_inflight_fd( thread
, data
.fd
, fd
);
415 if (thread
) release_object( thread
);
421 kill_process( process
, 0 );
425 fprintf( stderr
, "Protocol error: process %04x: partial recvmsg %d for fd\n",
427 kill_process( process
, 1 );
431 if (errno
!= EWOULDBLOCK
&& errno
!= EAGAIN
)
433 fprintf( stderr
, "Protocol error: process %04x: ", process
->id
);
435 kill_process( process
, 1 );
441 /* send an fd to a client */
442 int send_client_fd( struct process
*process
, int fd
, obj_handle_t handle
)
447 fprintf( stderr
, "%04x: *fd* %04x -> %d\n",
448 current
? current
->id
: process
->id
, handle
, fd
);
450 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
451 msghdr
.msg_accrightslen
= sizeof(fd
);
452 msghdr
.msg_accrights
= (void *)&fd
;
453 #else /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
454 msghdr
.msg_control
= &cmsg
;
455 msghdr
.msg_controllen
= sizeof(cmsg
.header
) + sizeof(fd
);
457 #endif /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
459 myiovec
.iov_base
= (void *)&handle
;
460 myiovec
.iov_len
= sizeof(handle
);
462 ret
= sendmsg( get_unix_fd( process
->msg_fd
), &msghdr
, 0 );
464 if (ret
== sizeof(handle
)) return 0;
468 fprintf( stderr
, "Protocol error: process %04x: partial sendmsg %d\n", process
->id
, ret
);
469 kill_process( process
, 1 );
471 else if (errno
== EPIPE
)
473 kill_process( process
, 0 );
477 fprintf( stderr
, "Protocol error: process %04x: ", process
->id
);
479 kill_process( process
, 1 );
484 /* get current tick count to return to client */
485 unsigned int get_tick_count(void)
487 return (current_time
- server_start_time
) / 10000;
490 static void master_socket_dump( struct object
*obj
, int verbose
)
492 struct master_socket
*sock
= (struct master_socket
*)obj
;
493 assert( obj
->ops
== &master_socket_ops
);
494 fprintf( stderr
, "Master socket fd=%p\n", sock
->fd
);
497 static void master_socket_destroy( struct object
*obj
)
499 struct master_socket
*sock
= (struct master_socket
*)obj
;
500 assert( obj
->ops
== &master_socket_ops
);
501 release_object( sock
->fd
);
504 /* handle a socket event */
505 static void master_socket_poll_event( struct fd
*fd
, int event
)
507 struct master_socket
*sock
= get_fd_user( fd
);
508 assert( master_socket
->obj
.ops
== &master_socket_ops
);
510 assert( sock
== master_socket
); /* there is only one master socket */
512 if (event
& (POLLERR
| POLLHUP
))
514 /* this is not supposed to happen */
515 fprintf( stderr
, "wineserver: Error on master socket\n" );
516 set_fd_events( sock
->fd
, -1 );
518 else if (event
& POLLIN
)
520 struct sockaddr_un dummy
;
521 unsigned int len
= sizeof(dummy
);
522 int client
= accept( get_unix_fd( master_socket
->fd
), (struct sockaddr
*) &dummy
, &len
);
523 if (client
== -1) return;
524 fcntl( client
, F_SETFL
, O_NONBLOCK
);
525 create_process( client
, NULL
, 0 );
529 /* remove the socket upon exit */
530 static void socket_cleanup(void)
532 static int do_it_once
;
533 if (!do_it_once
++) unlink( server_socket_name
);
536 /* create a directory and check its permissions */
537 static void create_dir( const char *name
, struct stat
*st
)
539 if (lstat( name
, st
) == -1)
541 if (errno
!= ENOENT
) fatal_perror( "lstat %s", name
);
542 if (mkdir( name
, 0700 ) == -1 && errno
!= EEXIST
) fatal_perror( "mkdir %s", name
);
543 if (lstat( name
, st
) == -1) fatal_perror( "lstat %s", name
);
545 if (!S_ISDIR(st
->st_mode
)) fatal_error( "%s is not a directory\n", name
);
546 if (st
->st_uid
!= getuid()) fatal_error( "%s is not owned by you\n", name
);
547 if (st
->st_mode
& 077) fatal_error( "%s must not be accessible by other users\n", name
);
550 /* create the server directory and chdir to it */
551 static void create_server_dir( const char *dir
)
553 char *p
, *server_dir
;
556 if (!(server_dir
= strdup( dir
))) fatal_error( "out of memory\n" );
558 /* first create the base directory if needed */
560 p
= strrchr( server_dir
, '/' );
562 create_dir( server_dir
, &st
);
564 /* now create the server directory */
567 create_dir( server_dir
, &st
);
569 if (chdir( server_dir
) == -1) fatal_perror( "chdir %s", server_dir
);
570 if ((server_dir_fd
= open( ".", O_RDONLY
)) == -1) fatal_perror( "open %s", server_dir
);
571 if (fstat( server_dir_fd
, &st2
) == -1) fatal_perror( "stat %s", server_dir
);
572 if (st
.st_dev
!= st2
.st_dev
|| st
.st_ino
!= st2
.st_ino
)
573 fatal_error( "chdir did not end up in %s\n", server_dir
);
578 /* create the lock file and return its file descriptor */
579 static int create_server_lock(void)
584 if (lstat( server_lock_name
, &st
) == -1)
587 fatal_perror( "lstat %s/%s", wine_get_server_dir(), server_lock_name
);
591 if (!S_ISREG(st
.st_mode
))
592 fatal_error( "%s/%s is not a regular file\n", wine_get_server_dir(), server_lock_name
);
595 if ((fd
= open( server_lock_name
, O_CREAT
|O_TRUNC
|O_WRONLY
, 0600 )) == -1)
596 fatal_perror( "error creating %s/%s", wine_get_server_dir(), server_lock_name
);
600 /* wait for the server lock */
601 int wait_for_lock(void)
603 const char *server_dir
= wine_get_server_dir();
607 if (!server_dir
) return 0; /* no server dir, so no lock to wait on */
609 create_server_dir( server_dir
);
610 fd
= create_server_lock();
613 fl
.l_whence
= SEEK_SET
;
616 r
= fcntl( fd
, F_SETLKW
, &fl
);
622 /* kill the wine server holding the lock */
623 int kill_lock_owner( int sig
)
625 const char *server_dir
= wine_get_server_dir();
630 if (!server_dir
) return 0; /* no server dir, nothing to do */
632 create_server_dir( server_dir
);
633 fd
= create_server_lock();
635 for (i
= 1; i
<= 20; i
++)
638 fl
.l_whence
= SEEK_SET
;
641 if (fcntl( fd
, F_GETLK
, &fl
) == -1) goto done
;
642 if (fl
.l_type
!= F_WRLCK
) goto done
; /* the file is not locked */
643 if (!pid
) /* first time around */
645 if (!(pid
= fl
.l_pid
)) goto done
; /* shouldn't happen */
648 if (kill( pid
, SIGINT
) == -1) goto done
;
649 kill( pid
, SIGCONT
);
652 else /* just send the specified signal and return */
654 ret
= (kill( pid
, sig
) != -1);
658 else if (fl
.l_pid
!= pid
) goto done
; /* no longer the same process */
661 /* waited long enough, now kill it */
662 kill( pid
, SIGKILL
);
669 /* acquire the main server lock */
670 static void acquire_lock(void)
672 struct sockaddr_un addr
;
675 int fd
, slen
, got_lock
= 0;
677 fd
= create_server_lock();
680 fl
.l_whence
= SEEK_SET
;
683 if (fcntl( fd
, F_SETLK
, &fl
) != -1)
685 /* check for crashed server */
686 if (stat( server_socket_name
, &st
) != -1 && /* there is a leftover socket */
687 stat( "core", &st
) != -1 && st
.st_size
) /* and there is a non-empty core file */
690 "Warning: a previous instance of the wine server seems to have crashed.\n"
691 "Please run 'gdb %s %s/core',\n"
692 "type 'backtrace' at the gdb prompt and report the results. Thanks.\n\n",
693 server_argv0
, wine_get_server_dir() );
695 unlink( server_socket_name
); /* we got the lock, we can safely remove the socket */
697 /* in that case we reuse fd without closing it, this ensures
698 * that we hold the lock until the process exits */
707 /* check whether locks work at all on this file system */
708 if (fcntl( fd
, F_GETLK
, &fl
) == -1) break;
711 exit(2); /* we didn't get the lock, exit with special status */
713 fatal_perror( "fcntl %s/%s", wine_get_server_dir(), server_lock_name
);
715 /* it seems we can't use locks on this fs, so we will use the socket existence as lock */
719 if ((fd
= socket( AF_UNIX
, SOCK_STREAM
, 0 )) == -1) fatal_perror( "socket" );
720 addr
.sun_family
= AF_UNIX
;
721 strcpy( addr
.sun_path
, server_socket_name
);
722 slen
= sizeof(addr
) - sizeof(addr
.sun_path
) + strlen(addr
.sun_path
) + 1;
723 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
726 if (bind( fd
, (struct sockaddr
*)&addr
, slen
) == -1)
728 if ((errno
== EEXIST
) || (errno
== EADDRINUSE
))
731 fatal_error( "couldn't bind to the socket even though we hold the lock\n" );
732 exit(2); /* we didn't get the lock, exit with special status */
734 fatal_perror( "bind" );
736 atexit( socket_cleanup
);
737 chmod( server_socket_name
, 0600 ); /* make sure no other user can connect */
738 if (listen( fd
, 5 ) == -1) fatal_perror( "listen" );
740 if (!(master_socket
= alloc_object( &master_socket_ops
)) ||
741 !(master_socket
->fd
= create_anonymous_fd( &master_socket_fd_ops
, fd
, &master_socket
->obj
, 0 )))
742 fatal_error( "out of memory\n" );
743 set_fd_events( master_socket
->fd
, POLLIN
);
744 make_object_static( &master_socket
->obj
);
747 /* open the master server socket and start waiting for new clients */
748 void open_master_socket(void)
750 const char *server_dir
= wine_get_server_dir();
751 const char *config_dir
= wine_get_config_dir();
752 int fd
, pid
, status
, sync_pipe
[2];
755 /* make sure no request is larger than the maximum size */
756 assert( sizeof(union generic_request
) == sizeof(struct request_max_size
) );
757 assert( sizeof(union generic_reply
) == sizeof(struct request_max_size
) );
759 /* Make sure REQ_SIZE_32 and REQ_SIZE_64 are of correct size */
760 if (sizeof(void *) == sizeof(int))
761 assert(sizeof(struct request_max_size
) == REQ_SIZE_32
);
762 else if (sizeof(void *) == sizeof(__int64
))
763 assert(sizeof(struct request_max_size
) == REQ_SIZE_64
);
767 if (!server_dir
) fatal_error( "directory %s cannot be accessed\n", config_dir
);
768 if (chdir( config_dir
) == -1) fatal_perror( "chdir to %s", config_dir
);
769 if ((config_dir_fd
= open( ".", O_RDONLY
)) == -1) fatal_perror( "open %s", config_dir
);
771 create_server_dir( server_dir
);
775 if (pipe( sync_pipe
) == -1) fatal_perror( "pipe" );
781 close( sync_pipe
[0] );
785 /* close stdin and stdout */
786 if ((fd
= open( "/dev/null", O_RDWR
)) != -1)
795 write( sync_pipe
[1], &dummy
, 1 );
796 close( sync_pipe
[1] );
800 fatal_perror( "fork" );
803 default: /* parent */
804 close( sync_pipe
[1] );
806 /* wait for child to signal us and then exit */
807 if (read( sync_pipe
[0], &dummy
, 1 ) == 1) _exit(0);
809 /* child terminated, propagate exit status */
810 wait4( pid
, &status
, 0, NULL
);
811 if (WIFEXITED(status
)) _exit( WEXITSTATUS(status
) );
815 else /* remain in the foreground */
820 /* setup msghdr structure constant fields */
821 msghdr
.msg_name
= NULL
;
822 msghdr
.msg_namelen
= 0;
823 msghdr
.msg_iov
= &myiovec
;
824 msghdr
.msg_iovlen
= 1;
826 /* init the process tracing mechanism */
827 init_tracing_mechanism();
830 /* master socket timer expiration handler */
831 static void close_socket_timeout( void *arg
)
833 master_timeout
= NULL
;
835 if (debug_level
) fprintf( stderr
, "wineserver: exiting (pid=%ld)\n", (long) getpid() );
838 close_objects(); /* shut down everything properly */
843 /* close the master socket and stop waiting for new clients */
844 void close_master_socket( timeout_t timeout
)
848 release_object( master_socket
);
849 master_socket
= NULL
;
851 if (master_timeout
) /* cancel previous timeout */
852 remove_timeout_user( master_timeout
);
854 master_timeout
= add_timeout_user( timeout
, close_socket_timeout
, NULL
);