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 */
81 struct timeout_user
*timeout
; /* timeout on last process exit */
84 static void master_socket_dump( struct object
*obj
, int verbose
);
85 static void master_socket_destroy( struct object
*obj
);
86 static void master_socket_poll_event( struct fd
*fd
, int event
);
88 static const struct object_ops master_socket_ops
=
90 sizeof(struct master_socket
), /* size */
91 master_socket_dump
, /* dump */
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 no_lookup_name
, /* lookup_name */
100 no_open_file
, /* open_file */
101 no_close_handle
, /* close_handle */
102 master_socket_destroy
/* destroy */
105 static const struct fd_ops master_socket_fd_ops
=
107 NULL
, /* get_poll_events */
108 master_socket_poll_event
, /* poll_event */
110 NULL
, /* get_fd_type */
112 NULL
, /* queue_async */
113 NULL
, /* reselect_async */
114 NULL
/* cancel_async */
118 struct thread
*current
= NULL
; /* thread handling the current request */
119 unsigned int global_error
= 0; /* global error code for when no thread is current */
120 timeout_t server_start_time
= 0; /* server startup time */
122 static struct master_socket
*master_socket
; /* the master socket object */
123 static int force_shutdown
;
125 /* socket communication static structures */
126 static struct iovec myiovec
;
127 static struct msghdr msghdr
;
128 #ifndef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
133 size_t len
; /* size of structure */
134 int level
; /* SOL_SOCKET */
135 int type
; /* SCM_RIGHTS */
137 int fd
; /* fd to pass */
139 static struct cmsg_fd cmsg
= { { sizeof(cmsg
.header
) + sizeof(cmsg
.fd
), SOL_SOCKET
, SCM_RIGHTS
}, -1 };
140 #endif /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
142 /* complain about a protocol error and terminate the client connection */
143 void fatal_protocol_error( struct thread
*thread
, const char *err
, ... )
147 va_start( args
, err
);
148 fprintf( stderr
, "Protocol error:%04x: ", thread
->id
);
149 vfprintf( stderr
, err
, args
);
151 thread
->exit_code
= 1;
152 kill_thread( thread
, 1 );
155 /* complain about a protocol error and terminate the client connection */
156 void fatal_protocol_perror( struct thread
*thread
, const char *err
, ... )
160 va_start( args
, err
);
161 fprintf( stderr
, "Protocol error:%04x: ", thread
->id
);
162 vfprintf( stderr
, err
, args
);
165 thread
->exit_code
= 1;
166 kill_thread( thread
, 1 );
169 /* die on a fatal error */
170 void fatal_error( const char *err
, ... )
174 va_start( args
, err
);
175 fprintf( stderr
, "wineserver: " );
176 vfprintf( stderr
, err
, args
);
181 /* die on a fatal error */
182 void fatal_perror( const char *err
, ... )
186 va_start( args
, err
);
187 fprintf( stderr
, "wineserver: " );
188 vfprintf( stderr
, err
, args
);
194 /* allocate the reply data */
195 void *set_reply_data_size( data_size_t size
)
197 assert( size
<= get_reply_max_size() );
198 if (size
&& !(current
->reply_data
= mem_alloc( size
))) size
= 0;
199 current
->reply_size
= size
;
200 return current
->reply_data
;
203 /* write the remaining part of the reply */
204 void write_reply( struct thread
*thread
)
208 if ((ret
= write( get_unix_fd( thread
->reply_fd
),
209 (char *)thread
->reply_data
+ thread
->reply_size
- thread
->reply_towrite
,
210 thread
->reply_towrite
)) >= 0)
212 if (!(thread
->reply_towrite
-= ret
))
214 free( thread
->reply_data
);
215 thread
->reply_data
= NULL
;
216 /* sent everything, can go back to waiting for requests */
217 set_fd_events( thread
->request_fd
, POLLIN
);
218 set_fd_events( thread
->reply_fd
, 0 );
223 kill_thread( thread
, 0 ); /* normal death */
224 else if (errno
!= EWOULDBLOCK
&& errno
!= EAGAIN
)
225 fatal_protocol_perror( thread
, "reply write" );
228 /* send a reply to the current thread */
229 static void send_reply( union generic_reply
*reply
)
233 if (!current
->reply_size
)
235 if ((ret
= write( get_unix_fd( current
->reply_fd
),
236 reply
, sizeof(*reply
) )) != sizeof(*reply
)) goto error
;
242 vec
[0].iov_base
= (void *)reply
;
243 vec
[0].iov_len
= sizeof(*reply
);
244 vec
[1].iov_base
= current
->reply_data
;
245 vec
[1].iov_len
= current
->reply_size
;
247 if ((ret
= writev( get_unix_fd( current
->reply_fd
), vec
, 2 )) < sizeof(*reply
)) goto error
;
249 if ((current
->reply_towrite
= current
->reply_size
- (ret
- sizeof(*reply
))))
251 /* couldn't write it all, wait for POLLOUT */
252 set_fd_events( current
->reply_fd
, POLLOUT
);
253 set_fd_events( current
->request_fd
, 0 );
257 free( current
->reply_data
);
258 current
->reply_data
= NULL
;
263 fatal_protocol_error( current
, "partial write %d\n", ret
);
264 else if (errno
== EPIPE
)
265 kill_thread( current
, 0 ); /* normal death */
267 fatal_protocol_perror( current
, "reply write" );
270 /* call a request handler */
271 static void call_req_handler( struct thread
*thread
)
273 union generic_reply reply
;
274 enum request req
= thread
->req
.request_header
.req
;
277 current
->reply_size
= 0;
279 memset( &reply
, 0, sizeof(reply
) );
281 if (debug_level
) trace_request();
283 if (req
< REQ_NB_REQUESTS
)
284 req_handlers
[req
]( ¤t
->req
, &reply
);
286 set_error( STATUS_NOT_IMPLEMENTED
);
290 if (current
->reply_fd
)
292 reply
.reply_header
.error
= current
->error
;
293 reply
.reply_header
.reply_size
= current
->reply_size
;
294 if (debug_level
) trace_reply( req
, &reply
);
295 send_reply( &reply
);
299 current
->exit_code
= 1;
300 kill_thread( current
, 1 ); /* no way to continue without reply fd */
306 /* read a request from a thread */
307 void read_request( struct thread
*thread
)
311 if (!thread
->req_toread
) /* no pending request */
313 if ((ret
= read( get_unix_fd( thread
->request_fd
), &thread
->req
,
314 sizeof(thread
->req
) )) != sizeof(thread
->req
)) goto error
;
315 if (!(thread
->req_toread
= thread
->req
.request_header
.request_size
))
317 /* no data, handle request at once */
318 call_req_handler( thread
);
321 if (!(thread
->req_data
= malloc( thread
->req_toread
)))
323 fatal_protocol_error( thread
, "no memory for %u bytes request %d\n",
324 thread
->req_toread
, thread
->req
.request_header
.req
);
329 /* read the variable sized data */
332 ret
= read( get_unix_fd( thread
->request_fd
),
333 (char *)thread
->req_data
+ thread
->req
.request_header
.request_size
334 - thread
->req_toread
,
335 thread
->req_toread
);
337 if (!(thread
->req_toread
-= ret
))
339 call_req_handler( thread
);
340 free( thread
->req_data
);
341 thread
->req_data
= NULL
;
347 if (!ret
) /* closed pipe */
348 kill_thread( thread
, 0 );
350 fatal_protocol_error( thread
, "partial read %d\n", ret
);
351 else if (errno
!= EWOULDBLOCK
&& errno
!= EAGAIN
)
352 fatal_protocol_perror( thread
, "read" );
355 /* receive a file descriptor on the process socket */
356 int receive_fd( struct process
*process
)
361 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
362 msghdr
.msg_accrightslen
= sizeof(int);
363 msghdr
.msg_accrights
= (void *)&fd
;
364 #else /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
365 msghdr
.msg_control
= &cmsg
;
366 msghdr
.msg_controllen
= sizeof(cmsg
.header
) + sizeof(fd
);
368 #endif /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
370 myiovec
.iov_base
= (void *)&data
;
371 myiovec
.iov_len
= sizeof(data
);
373 ret
= recvmsg( get_unix_fd( process
->msg_fd
), &msghdr
, 0 );
374 #ifndef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
378 if (ret
== sizeof(data
))
380 struct thread
*thread
;
382 if (data
.tid
) thread
= get_thread_from_id( data
.tid
);
383 else thread
= (struct thread
*)grab_object( get_process_first_thread( process
));
385 if (!thread
|| thread
->process
!= process
|| thread
->state
== TERMINATED
)
388 fprintf( stderr
, "%04x: *fd* %d <- %d bad thread id\n",
389 data
.tid
, data
.fd
, fd
);
395 fprintf( stderr
, "%04x: *fd* %d <- %d\n",
396 thread
->id
, data
.fd
, fd
);
397 thread_add_inflight_fd( thread
, data
.fd
, fd
);
399 if (thread
) release_object( thread
);
405 kill_process( process
, 0 );
409 fprintf( stderr
, "Protocol error: process %04x: partial recvmsg %d for fd\n",
411 kill_process( process
, 1 );
415 if (errno
!= EWOULDBLOCK
&& errno
!= EAGAIN
)
417 fprintf( stderr
, "Protocol error: process %04x: ", process
->id
);
419 kill_process( process
, 1 );
425 /* send an fd to a client */
426 int send_client_fd( struct process
*process
, int fd
, obj_handle_t handle
)
431 fprintf( stderr
, "%04x: *fd* %p -> %d\n",
432 current
? current
->id
: process
->id
, handle
, fd
);
434 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
435 msghdr
.msg_accrightslen
= sizeof(fd
);
436 msghdr
.msg_accrights
= (void *)&fd
;
437 #else /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
438 msghdr
.msg_control
= &cmsg
;
439 msghdr
.msg_controllen
= sizeof(cmsg
.header
) + sizeof(fd
);
441 #endif /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
443 myiovec
.iov_base
= (void *)&handle
;
444 myiovec
.iov_len
= sizeof(handle
);
446 ret
= sendmsg( get_unix_fd( process
->msg_fd
), &msghdr
, 0 );
448 if (ret
== sizeof(handle
)) return 0;
452 fprintf( stderr
, "Protocol error: process %04x: partial sendmsg %d\n", process
->id
, ret
);
453 kill_process( process
, 1 );
455 else if (errno
== EPIPE
)
457 kill_process( process
, 0 );
461 fprintf( stderr
, "Protocol error: process %04x: ", process
->id
);
463 kill_process( process
, 1 );
468 /* get current tick count to return to client */
469 unsigned int get_tick_count(void)
471 return (current_time
- server_start_time
) / 10000;
474 static void master_socket_dump( struct object
*obj
, int verbose
)
476 struct master_socket
*sock
= (struct master_socket
*)obj
;
477 assert( obj
->ops
== &master_socket_ops
);
478 fprintf( stderr
, "Master socket fd=%p\n", sock
->fd
);
481 static void master_socket_destroy( struct object
*obj
)
483 struct master_socket
*sock
= (struct master_socket
*)obj
;
484 assert( obj
->ops
== &master_socket_ops
);
485 release_object( sock
->fd
);
488 /* handle a socket event */
489 static void master_socket_poll_event( struct fd
*fd
, int event
)
491 struct master_socket
*sock
= get_fd_user( fd
);
492 assert( master_socket
->obj
.ops
== &master_socket_ops
);
494 assert( sock
== master_socket
); /* there is only one master socket */
496 if (event
& (POLLERR
| POLLHUP
))
498 /* this is not supposed to happen */
499 fprintf( stderr
, "wineserver: Error on master socket\n" );
500 set_fd_events( sock
->fd
, -1 );
502 else if (event
& POLLIN
)
504 struct sockaddr_un dummy
;
505 unsigned int len
= sizeof(dummy
);
506 int client
= accept( get_unix_fd( master_socket
->fd
), (struct sockaddr
*) &dummy
, &len
);
507 if (client
== -1) return;
510 remove_timeout_user( sock
->timeout
);
511 sock
->timeout
= NULL
;
513 fcntl( client
, F_SETFL
, O_NONBLOCK
);
514 create_process( client
, NULL
, 0 );
518 /* remove the socket upon exit */
519 static void socket_cleanup(void)
521 static int do_it_once
;
522 if (!do_it_once
++) unlink( server_socket_name
);
525 /* create a directory and check its permissions */
526 static void create_dir( const char *name
, struct stat
*st
)
528 if (lstat( name
, st
) == -1)
530 if (errno
!= ENOENT
) fatal_perror( "lstat %s", name
);
531 if (mkdir( name
, 0700 ) == -1 && errno
!= EEXIST
) fatal_perror( "mkdir %s", name
);
532 if (lstat( name
, st
) == -1) fatal_perror( "lstat %s", name
);
534 if (!S_ISDIR(st
->st_mode
)) fatal_error( "%s is not a directory\n", name
);
535 if (st
->st_uid
!= getuid()) fatal_error( "%s is not owned by you\n", name
);
536 if (st
->st_mode
& 077) fatal_error( "%s must not be accessible by other users\n", name
);
539 /* create the server directory and chdir to it */
540 static void create_server_dir( const char *dir
)
542 char *p
, *server_dir
;
545 if (!(server_dir
= strdup( dir
))) fatal_error( "out of memory\n" );
547 /* first create the base directory if needed */
549 p
= strrchr( server_dir
, '/' );
551 create_dir( server_dir
, &st
);
553 /* now create the server directory */
556 create_dir( server_dir
, &st
);
558 if (chdir( server_dir
) == -1) fatal_perror( "chdir %s", server_dir
);
559 if (stat( ".", &st2
) == -1) fatal_perror( "stat %s", server_dir
);
560 if (st
.st_dev
!= st2
.st_dev
|| st
.st_ino
!= st2
.st_ino
)
561 fatal_error( "chdir did not end up in %s\n", server_dir
);
566 /* create the lock file and return its file descriptor */
567 static int create_server_lock(void)
572 if (lstat( server_lock_name
, &st
) == -1)
575 fatal_perror( "lstat %s/%s", wine_get_server_dir(), server_lock_name
);
579 if (!S_ISREG(st
.st_mode
))
580 fatal_error( "%s/%s is not a regular file\n", wine_get_server_dir(), server_lock_name
);
583 if ((fd
= open( server_lock_name
, O_CREAT
|O_TRUNC
|O_WRONLY
, 0600 )) == -1)
584 fatal_perror( "error creating %s/%s", wine_get_server_dir(), server_lock_name
);
588 /* wait for the server lock */
589 int wait_for_lock(void)
591 const char *server_dir
= wine_get_server_dir();
595 if (!server_dir
) return 0; /* no server dir, so no lock to wait on */
597 create_server_dir( server_dir
);
598 fd
= create_server_lock();
601 fl
.l_whence
= SEEK_SET
;
604 r
= fcntl( fd
, F_SETLKW
, &fl
);
610 /* kill the wine server holding the lock */
611 int kill_lock_owner( int sig
)
613 const char *server_dir
= wine_get_server_dir();
618 if (!server_dir
) return 0; /* no server dir, nothing to do */
620 create_server_dir( server_dir
);
621 fd
= create_server_lock();
623 for (i
= 0; i
< 10; i
++)
626 fl
.l_whence
= SEEK_SET
;
629 if (fcntl( fd
, F_GETLK
, &fl
) == -1) goto done
;
630 if (fl
.l_type
!= F_WRLCK
) goto done
; /* the file is not locked */
631 if (!pid
) /* first time around */
633 if (!(pid
= fl
.l_pid
)) goto done
; /* shouldn't happen */
636 if (kill( pid
, SIGINT
) == -1) goto done
;
637 kill( pid
, SIGCONT
);
640 else /* just send the specified signal and return */
642 ret
= (kill( pid
, sig
) != -1);
646 else if (fl
.l_pid
!= pid
) goto done
; /* no longer the same process */
649 /* waited long enough, now kill it */
650 kill( pid
, SIGKILL
);
657 /* acquire the main server lock */
658 static void acquire_lock(void)
660 struct sockaddr_un addr
;
663 int fd
, slen
, got_lock
= 0;
665 fd
= create_server_lock();
668 fl
.l_whence
= SEEK_SET
;
671 if (fcntl( fd
, F_SETLK
, &fl
) != -1)
673 /* check for crashed server */
674 if (stat( server_socket_name
, &st
) != -1 && /* there is a leftover socket */
675 stat( "core", &st
) != -1 && st
.st_size
) /* and there is a non-empty core file */
678 "Warning: a previous instance of the wine server seems to have crashed.\n"
679 "Please run 'gdb %s %s/core',\n"
680 "type 'backtrace' at the gdb prompt and report the results. Thanks.\n\n",
681 server_argv0
, wine_get_server_dir() );
683 unlink( server_socket_name
); /* we got the lock, we can safely remove the socket */
685 /* in that case we reuse fd without closing it, this ensures
686 * that we hold the lock until the process exits */
695 /* check whether locks work at all on this file system */
696 if (fcntl( fd
, F_GETLK
, &fl
) == -1) break;
699 exit(2); /* we didn't get the lock, exit with special status */
701 fatal_perror( "fcntl %s/%s", wine_get_server_dir(), server_lock_name
);
703 /* it seems we can't use locks on this fs, so we will use the socket existence as lock */
707 if ((fd
= socket( AF_UNIX
, SOCK_STREAM
, 0 )) == -1) fatal_perror( "socket" );
708 addr
.sun_family
= AF_UNIX
;
709 strcpy( addr
.sun_path
, server_socket_name
);
710 slen
= sizeof(addr
) - sizeof(addr
.sun_path
) + strlen(addr
.sun_path
) + 1;
711 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
714 if (bind( fd
, (struct sockaddr
*)&addr
, slen
) == -1)
716 if ((errno
== EEXIST
) || (errno
== EADDRINUSE
))
719 fatal_error( "couldn't bind to the socket even though we hold the lock\n" );
720 exit(2); /* we didn't get the lock, exit with special status */
722 fatal_perror( "bind" );
724 atexit( socket_cleanup
);
725 chmod( server_socket_name
, 0600 ); /* make sure no other user can connect */
726 if (listen( fd
, 5 ) == -1) fatal_perror( "listen" );
728 if (!(master_socket
= alloc_object( &master_socket_ops
)) ||
729 !(master_socket
->fd
= create_anonymous_fd( &master_socket_fd_ops
, fd
, &master_socket
->obj
, 0 )))
730 fatal_error( "out of memory\n" );
731 master_socket
->timeout
= NULL
;
732 set_fd_events( master_socket
->fd
, POLLIN
);
733 make_object_static( &master_socket
->obj
);
736 /* open the master server socket and start waiting for new clients */
737 void open_master_socket(void)
739 const char *server_dir
= wine_get_server_dir();
740 int fd
, pid
, status
, sync_pipe
[2];
743 /* make sure no request is larger than the maximum size */
744 assert( sizeof(union generic_request
) == sizeof(struct request_max_size
) );
745 assert( sizeof(union generic_reply
) == sizeof(struct request_max_size
) );
747 if (!server_dir
) fatal_error( "directory %s cannot be accessed\n", wine_get_config_dir() );
748 create_server_dir( server_dir
);
752 if (pipe( sync_pipe
) == -1) fatal_perror( "pipe" );
758 close( sync_pipe
[0] );
762 /* close stdin and stdout */
763 if ((fd
= open( "/dev/null", O_RDWR
)) != -1)
772 write( sync_pipe
[1], &dummy
, 1 );
773 close( sync_pipe
[1] );
777 fatal_perror( "fork" );
780 default: /* parent */
781 close( sync_pipe
[1] );
783 /* wait for child to signal us and then exit */
784 if (read( sync_pipe
[0], &dummy
, 1 ) == 1) _exit(0);
786 /* child terminated, propagate exit status */
787 wait4( pid
, &status
, 0, NULL
);
788 if (WIFEXITED(status
)) _exit( WEXITSTATUS(status
) );
792 else /* remain in the foreground */
797 /* setup msghdr structure constant fields */
798 msghdr
.msg_name
= NULL
;
799 msghdr
.msg_namelen
= 0;
800 msghdr
.msg_iov
= &myiovec
;
801 msghdr
.msg_iovlen
= 1;
803 /* init the process tracing mechanism */
804 init_tracing_mechanism();
807 /* master socket timer expiration handler */
808 static void close_socket_timeout( void *arg
)
810 master_socket
->timeout
= NULL
;
813 /* if a new client is waiting, we keep on running */
814 if (!force_shutdown
&& check_fd_events( master_socket
->fd
, POLLIN
)) return;
816 if (debug_level
) fprintf( stderr
, "wineserver: exiting (pid=%ld)\n", (long) getpid() );
819 close_objects(); /* shut down everything properly */
821 exit( force_shutdown
);
824 /* close the master socket and stop waiting for new clients */
825 void close_master_socket(void)
827 if (master_socket_timeout
== TIMEOUT_INFINITE
) return; /* just keep running forever */
829 if (master_socket_timeout
)
830 master_socket
->timeout
= add_timeout_user( master_socket_timeout
, close_socket_timeout
, NULL
);
832 close_socket_timeout( NULL
); /* close it right away */
835 /* forced shutdown, used for wineserver -k */
836 void shutdown_master_socket(void)
839 master_socket_timeout
= 0;
840 if (master_socket
->timeout
)
842 remove_timeout_user( master_socket
->timeout
);
843 close_socket_timeout( NULL
);
845 set_fd_events( master_socket
->fd
, -1 ); /* stop waiting for new clients */