Some more RPC definitions.
[wine.git] / server / request.c
blob855e877560828975520d4111871128b69cf110dd
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <pwd.h>
28 #include <signal.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <stdarg.h>
32 #include <string.h>
33 #include <sys/stat.h>
34 #include <sys/time.h>
35 #include <sys/types.h>
36 #ifdef HAVE_SYS_SOCKET_H
37 # include <sys/socket.h>
38 #endif
39 #ifdef HAVE_SYS_WAIT_H
40 # include <sys/wait.h>
41 #endif
42 #include <sys/uio.h>
43 #include <sys/un.h>
44 #include <unistd.h>
46 #include "winnt.h"
47 #include "winbase.h"
48 #include "wincon.h"
49 #include "wine/library.h"
51 #include "handle.h"
52 #include "thread.h"
53 #include "process.h"
54 #define WANT_REQUEST_HANDLERS
55 #include "request.h"
57 /* Some versions of glibc don't define this */
58 #ifndef SCM_RIGHTS
59 #define SCM_RIGHTS 1
60 #endif
62 /* path names for server master Unix socket */
63 static const char * const server_socket_name = "socket"; /* name of the socket file */
64 static const char * const server_lock_name = "lock"; /* name of the server lock file */
66 struct master_socket
68 struct object obj; /* object header */
69 struct timeout_user *timeout; /* timeout on last process exit */
72 static void master_socket_dump( struct object *obj, int verbose );
73 static void master_socket_poll_event( struct object *obj, int event );
75 static const struct object_ops master_socket_ops =
77 sizeof(struct master_socket), /* size */
78 master_socket_dump, /* dump */
79 no_add_queue, /* add_queue */
80 NULL, /* remove_queue */
81 NULL, /* signaled */
82 NULL, /* satisfied */
83 NULL, /* get_poll_events */
84 master_socket_poll_event, /* poll_event */
85 no_get_fd, /* get_fd */
86 no_flush, /* flush */
87 no_get_file_info, /* get_file_info */
88 NULL, /* queue_async */
89 no_destroy /* destroy */
93 struct thread *current = NULL; /* thread handling the current request */
94 unsigned int global_error = 0; /* global error code for when no thread is current */
95 unsigned int server_start_ticks = 0; /* tick count offset from server startup */
97 static struct master_socket *master_socket; /* the master socket object */
99 /* socket communication static structures */
100 static struct iovec myiovec;
101 static struct msghdr msghdr;
102 #ifndef HAVE_MSGHDR_ACCRIGHTS
103 struct cmsg_fd
105 int len; /* sizeof structure */
106 int level; /* SOL_SOCKET */
107 int type; /* SCM_RIGHTS */
108 int fd; /* fd to pass */
110 static struct cmsg_fd cmsg = { sizeof(cmsg), SOL_SOCKET, SCM_RIGHTS, -1 };
111 #endif /* HAVE_MSGHDR_ACCRIGHTS */
113 /* complain about a protocol error and terminate the client connection */
114 void fatal_protocol_error( struct thread *thread, const char *err, ... )
116 va_list args;
118 va_start( args, err );
119 fprintf( stderr, "Protocol error:%p: ", thread );
120 vfprintf( stderr, err, args );
121 va_end( args );
122 thread->exit_code = 1;
123 kill_thread( thread, 1 );
126 /* complain about a protocol error and terminate the client connection */
127 void fatal_protocol_perror( struct thread *thread, const char *err, ... )
129 va_list args;
131 va_start( args, err );
132 fprintf( stderr, "Protocol error:%p: ", thread );
133 vfprintf( stderr, err, args );
134 perror( " " );
135 va_end( args );
136 thread->exit_code = 1;
137 kill_thread( thread, 1 );
140 /* die on a fatal error */
141 void fatal_error( const char *err, ... )
143 va_list args;
145 va_start( args, err );
146 fprintf( stderr, "wineserver: " );
147 vfprintf( stderr, err, args );
148 va_end( args );
149 exit(1);
152 /* die on a fatal error */
153 void fatal_perror( const char *err, ... )
155 va_list args;
157 va_start( args, err );
158 fprintf( stderr, "wineserver: " );
159 vfprintf( stderr, err, args );
160 perror( " " );
161 va_end( args );
162 exit(1);
165 /* allocate the reply data */
166 void *set_reply_data_size( size_t size )
168 assert( size <= get_reply_max_size() );
169 if (size && !(current->reply_data = mem_alloc( size ))) size = 0;
170 current->reply_size = size;
171 return current->reply_data;
174 /* write the remaining part of the reply */
175 void write_reply( struct thread *thread )
177 int ret;
179 if ((ret = write( thread->reply_fd,
180 (char *)thread->reply_data + thread->reply_size - thread->reply_towrite,
181 thread->reply_towrite )) >= 0)
183 if (!(thread->reply_towrite -= ret))
185 free( thread->reply_data );
186 thread->reply_data = NULL;
187 /* sent everything, can go back to waiting for requests */
188 change_select_fd( &thread->obj, thread->request_fd, POLLIN );
190 return;
192 if (errno == EPIPE)
193 kill_thread( thread, 0 ); /* normal death */
194 else if (errno != EWOULDBLOCK && errno != EAGAIN)
195 fatal_protocol_perror( thread, "reply write" );
198 /* send a reply to the current thread */
199 static void send_reply( union generic_reply *reply )
201 int ret;
203 if (!current->reply_size)
205 if ((ret = write( current->reply_fd, reply, sizeof(*reply) )) != sizeof(*reply)) goto error;
207 else
209 struct iovec vec[2];
211 vec[0].iov_base = (void *)reply;
212 vec[0].iov_len = sizeof(*reply);
213 vec[1].iov_base = current->reply_data;
214 vec[1].iov_len = current->reply_size;
216 if ((ret = writev( current->reply_fd, vec, 2 )) < sizeof(*reply)) goto error;
218 if ((current->reply_towrite = current->reply_size - (ret - sizeof(*reply))))
220 /* couldn't write it all, wait for POLLOUT */
221 change_select_fd( &current->obj, current->reply_fd, POLLOUT );
222 return;
225 if (current->reply_data)
227 free( current->reply_data );
228 current->reply_data = NULL;
230 return;
232 error:
233 if (ret >= 0)
234 fatal_protocol_error( current, "partial write %d\n", ret );
235 else if (errno == EPIPE)
236 kill_thread( current, 0 ); /* normal death */
237 else
238 fatal_protocol_perror( current, "reply write" );
241 /* call a request handler */
242 static void call_req_handler( struct thread *thread )
244 union generic_reply reply;
245 enum request req = thread->req.request_header.req;
247 current = thread;
248 current->reply_size = 0;
249 clear_error();
250 memset( &reply, 0, sizeof(reply) );
252 if (debug_level) trace_request();
254 if (req < REQ_NB_REQUESTS)
256 req_handlers[req]( &current->req, &reply );
257 if (current)
259 reply.reply_header.error = current->error;
260 reply.reply_header.reply_size = current->reply_size;
261 if (debug_level) trace_reply( req, &reply );
262 send_reply( &reply );
264 current = NULL;
265 return;
267 fatal_protocol_error( current, "bad request %d\n", req );
270 /* read a request from a thread */
271 void read_request( struct thread *thread )
273 int ret;
275 if (!thread->req_toread) /* no pending request */
277 if ((ret = read( thread->obj.fd, &thread->req,
278 sizeof(thread->req) )) != sizeof(thread->req)) goto error;
279 if (!(thread->req_toread = thread->req.request_header.request_size))
281 /* no data, handle request at once */
282 call_req_handler( thread );
283 return;
285 if (!(thread->req_data = malloc( thread->req_toread )))
286 fatal_protocol_error( thread, "no memory for %d bytes request\n", thread->req_toread );
289 /* read the variable sized data */
290 for (;;)
292 ret = read( thread->obj.fd, ((char *)thread->req_data +
293 thread->req.request_header.request_size - thread->req_toread),
294 thread->req_toread );
295 if (ret <= 0) break;
296 if (!(thread->req_toread -= ret))
298 call_req_handler( thread );
299 free( thread->req_data );
300 thread->req_data = NULL;
301 return;
305 error:
306 if (!ret) /* closed pipe */
307 kill_thread( thread, 0 );
308 else if (ret > 0)
309 fatal_protocol_error( thread, "partial read %d\n", ret );
310 else if (errno != EWOULDBLOCK && errno != EAGAIN)
311 fatal_protocol_perror( thread, "read" );
314 /* receive a file descriptor on the process socket */
315 int receive_fd( struct process *process )
317 struct send_fd data;
318 int fd, ret;
320 #ifdef HAVE_MSGHDR_ACCRIGHTS
321 msghdr.msg_accrightslen = sizeof(int);
322 msghdr.msg_accrights = (void *)&fd;
323 #else /* HAVE_MSGHDR_ACCRIGHTS */
324 msghdr.msg_control = &cmsg;
325 msghdr.msg_controllen = sizeof(cmsg);
326 cmsg.fd = -1;
327 #endif /* HAVE_MSGHDR_ACCRIGHTS */
329 myiovec.iov_base = (void *)&data;
330 myiovec.iov_len = sizeof(data);
332 ret = recvmsg( process->obj.fd, &msghdr, 0 );
333 #ifndef HAVE_MSGHDR_ACCRIGHTS
334 fd = cmsg.fd;
335 #endif
337 if (ret == sizeof(data))
339 struct thread *thread;
341 if (data.tid) thread = get_thread_from_id( data.tid );
342 else thread = (struct thread *)grab_object( process->thread_list );
344 if (!thread || thread->process != process)
346 if (debug_level)
347 fprintf( stderr, "%08x: *fd* %d <- %d bad thread id\n",
348 (unsigned int)data.tid, data.fd, fd );
349 close( fd );
351 else
353 if (debug_level)
354 fprintf( stderr, "%08x: *fd* %d <- %d\n",
355 (unsigned int)thread, data.fd, fd );
356 thread_add_inflight_fd( thread, data.fd, fd );
358 if (thread) release_object( thread );
359 return 0;
362 if (ret >= 0)
364 if (ret > 0)
365 fprintf( stderr, "Protocol error: process %p: partial recvmsg %d for fd\n",
366 process, ret );
367 kill_process( process, NULL, 1 );
369 else
371 if (errno != EWOULDBLOCK && errno != EAGAIN)
373 fprintf( stderr, "Protocol error: process %p: ", process );
374 perror( "recvmsg" );
375 kill_process( process, NULL, 1 );
378 return -1;
381 /* send an fd to a client */
382 int send_client_fd( struct process *process, int fd, obj_handle_t handle )
384 int ret;
386 if (debug_level)
387 fprintf( stderr, "%08x: *fd* %d -> %d\n", (unsigned int)current, handle, fd );
389 #ifdef HAVE_MSGHDR_ACCRIGHTS
390 msghdr.msg_accrightslen = sizeof(fd);
391 msghdr.msg_accrights = (void *)&fd;
392 #else /* HAVE_MSGHDR_ACCRIGHTS */
393 msghdr.msg_control = &cmsg;
394 msghdr.msg_controllen = sizeof(cmsg);
395 cmsg.fd = fd;
396 #endif /* HAVE_MSGHDR_ACCRIGHTS */
398 myiovec.iov_base = (void *)&handle;
399 myiovec.iov_len = sizeof(handle);
401 ret = sendmsg( process->obj.fd, &msghdr, 0 );
403 if (ret == sizeof(handle)) return 0;
405 if (ret >= 0)
407 fprintf( stderr, "Protocol error: process %p: partial sendmsg %d\n", process, ret );
408 kill_process( process, NULL, 1 );
410 else if (errno == EPIPE)
412 kill_process( process, NULL, 0 );
414 else
416 fprintf( stderr, "Protocol error: process %p: ", process );
417 perror( "sendmsg" );
418 kill_process( process, NULL, 1 );
420 return -1;
423 /* get current tick count to return to client */
424 unsigned int get_tick_count(void)
426 struct timeval t;
427 gettimeofday( &t, NULL );
428 return (t.tv_sec * 1000) + (t.tv_usec / 1000) - server_start_ticks;
431 static void master_socket_dump( struct object *obj, int verbose )
433 struct master_socket *sock = (struct master_socket *)obj;
434 assert( obj->ops == &master_socket_ops );
435 fprintf( stderr, "Master socket fd=%d\n", sock->obj.fd );
438 /* handle a socket event */
439 static void master_socket_poll_event( struct object *obj, int event )
441 struct master_socket *sock = (struct master_socket *)obj;
442 assert( obj->ops == &master_socket_ops );
444 assert( sock == master_socket ); /* there is only one master socket */
446 if (event & (POLLERR | POLLHUP))
448 /* this is not supposed to happen */
449 fprintf( stderr, "wineserver: Error on master socket\n" );
450 release_object( obj );
452 else if (event & POLLIN)
454 struct sockaddr_un dummy;
455 int len = sizeof(dummy);
456 int client = accept( master_socket->obj.fd, (struct sockaddr *) &dummy, &len );
457 if (client == -1) return;
458 if (sock->timeout)
460 remove_timeout_user( sock->timeout );
461 sock->timeout = NULL;
463 fcntl( client, F_SETFL, O_NONBLOCK );
464 create_process( client );
468 /* remove the socket upon exit */
469 static void socket_cleanup(void)
471 static int do_it_once;
472 if (!do_it_once++) unlink( server_socket_name );
475 /* create a directory and check its permissions */
476 static void create_dir( const char *name, struct stat *st )
478 if (lstat( name, st ) == -1)
480 if (errno != ENOENT) fatal_perror( "lstat %s", name );
481 if (mkdir( name, 0700 ) == -1) fatal_perror( "mkdir %s", name );
482 if (lstat( name, st ) == -1) fatal_perror( "lstat %s", name );
484 if (!S_ISDIR(st->st_mode)) fatal_error( "%s is not a directory\n", name );
485 if (st->st_uid != getuid()) fatal_error( "%s is not owned by you\n", name );
486 if (st->st_mode & 077) fatal_error( "%s must not be accessible by other users\n", name );
489 /* create the server directory and chdir to it */
490 static void create_server_dir(void)
492 char *p, *server_dir;
493 struct stat st, st2;
495 if (!(server_dir = strdup( wine_get_server_dir() ))) fatal_error( "out of memory\n" );
497 /* first create the base directory if needed */
499 p = strrchr( server_dir, '/' );
500 *p = 0;
501 create_dir( server_dir, &st );
503 /* now create the server directory */
505 *p = '/';
506 create_dir( server_dir, &st );
508 if (chdir( server_dir ) == -1) fatal_perror( "chdir %s", server_dir );
509 if (stat( ".", &st2 ) == -1) fatal_perror( "stat %s", server_dir );
510 if (st.st_dev != st2.st_dev || st.st_ino != st2.st_ino)
511 fatal_error( "chdir did not end up in %s\n", server_dir );
513 free( server_dir );
516 /* wait for the server lock */
517 int wait_for_lock(void)
519 int fd, r;
520 struct flock fl;
522 create_server_dir();
523 if ((fd = open( server_lock_name, O_TRUNC|O_WRONLY, 0600 )) == -1)
524 return -1;
526 fl.l_type = F_WRLCK;
527 fl.l_whence = SEEK_SET;
528 fl.l_start = 0;
529 fl.l_len = 1;
530 r = fcntl( fd, F_SETLKW, &fl );
531 close(fd);
533 return r;
536 /* acquire the main server lock */
537 static void acquire_lock(void)
539 const char *server_dir_name = wine_get_server_dir();
540 struct sockaddr_un addr;
541 struct stat st;
542 struct flock fl;
543 int fd, slen, got_lock = 0;
545 if (lstat( server_lock_name, &st ) == -1)
547 if (errno != ENOENT)
548 fatal_perror( "lstat %s/%s", server_dir_name, server_lock_name );
550 else
552 if (!S_ISREG(st.st_mode))
553 fatal_error( "%s/%s is not a regular file\n", server_dir_name, server_lock_name );
556 if ((fd = open( server_lock_name, O_CREAT|O_TRUNC|O_WRONLY, 0600 )) == -1)
557 fatal_perror( "error creating %s/%s", server_dir_name, server_lock_name );
559 fl.l_type = F_WRLCK;
560 fl.l_whence = SEEK_SET;
561 fl.l_start = 0;
562 fl.l_len = 1;
563 if (fcntl( fd, F_SETLK, &fl ) != -1)
565 /* check for crashed server */
566 if (stat( server_socket_name, &st ) != -1 && /* there is a leftover socket */
567 stat( "core", &st ) != -1 && st.st_size) /* and there is a non-empty core file */
569 fprintf( stderr,
570 "Warning: a previous instance of the wine server seems to have crashed.\n"
571 "Please run 'gdb %s %s/core',\n"
572 "type 'backtrace' at the gdb prompt and report the results. Thanks.\n\n",
573 server_argv0, server_dir_name );
575 unlink( server_socket_name ); /* we got the lock, we can safely remove the socket */
576 got_lock = 1;
577 /* in that case we reuse fd without closing it, this ensures
578 * that we hold the lock until the process exits */
580 else
582 switch(errno)
584 case ENOLCK:
585 break;
586 case EACCES:
587 /* check whether locks work at all on this file system */
588 if (fcntl( fd, F_GETLK, &fl ) == -1) break;
589 /* fall through */
590 case EAGAIN:
591 exit(2); /* we didn't get the lock, exit with special status */
592 default:
593 fatal_perror( "fcntl %s/%s", server_dir_name, server_lock_name );
595 /* it seems we can't use locks on this fs, so we will use the socket existence as lock */
596 close( fd );
599 if ((fd = socket( AF_UNIX, SOCK_STREAM, 0 )) == -1) fatal_perror( "socket" );
600 addr.sun_family = AF_UNIX;
601 strcpy( addr.sun_path, server_socket_name );
602 slen = sizeof(addr) - sizeof(addr.sun_path) + strlen(addr.sun_path) + 1;
603 #ifdef HAVE_SOCKADDR_SUN_LEN
604 addr.sun_len = slen;
605 #endif
606 if (bind( fd, (struct sockaddr *)&addr, slen ) == -1)
608 if ((errno == EEXIST) || (errno == EADDRINUSE))
610 if (got_lock)
611 fatal_error( "couldn't bind to the socket even though we hold the lock\n" );
612 exit(2); /* we didn't get the lock, exit with special status */
614 fatal_perror( "bind" );
616 atexit( socket_cleanup );
617 chmod( server_socket_name, 0600 ); /* make sure no other user can connect */
618 if (listen( fd, 5 ) == -1) fatal_perror( "listen" );
620 if (!(master_socket = alloc_object( &master_socket_ops, fd )))
621 fatal_error( "out of memory\n" );
622 master_socket->timeout = NULL;
623 set_select_events( &master_socket->obj, POLLIN );
626 /* open the master server socket and start waiting for new clients */
627 void open_master_socket(void)
629 int pid, status, sync_pipe[2];
630 char dummy;
632 /* make sure no request is larger than the maximum size */
633 assert( sizeof(union generic_request) == sizeof(struct request_max_size) );
634 assert( sizeof(union generic_reply) == sizeof(struct request_max_size) );
636 create_server_dir();
637 if (pipe( sync_pipe ) == -1) fatal_perror( "pipe" );
639 pid = fork();
640 switch( pid )
642 case 0: /* child */
643 setsid();
644 close( sync_pipe[0] );
646 acquire_lock();
648 /* signal parent */
649 write( sync_pipe[1], &dummy, 1 );
650 close( sync_pipe[1] );
651 break;
653 case -1:
654 fatal_perror( "fork" );
655 break;
657 default: /* parent */
658 close( sync_pipe[1] );
660 /* wait for child to signal us and then exit */
661 if (read( sync_pipe[0], &dummy, 1 ) == 1) _exit(0);
663 /* child terminated, propagate exit status */
664 wait4( pid, &status, 0, NULL );
665 if (WIFEXITED(status)) _exit( WEXITSTATUS(status) );
666 _exit(1);
669 /* setup msghdr structure constant fields */
670 msghdr.msg_name = NULL;
671 msghdr.msg_namelen = 0;
672 msghdr.msg_iov = &myiovec;
673 msghdr.msg_iovlen = 1;
675 /* init startup ticks */
676 server_start_ticks = get_tick_count();
679 /* master socket timer expiration handler */
680 static void close_socket_timeout( void *arg )
682 master_socket->timeout = NULL;
683 flush_registry();
685 /* if a new client is waiting, we keep on running */
686 if (check_select_events( master_socket->obj.fd, POLLIN )) return;
688 if (debug_level) fprintf( stderr, "wineserver: exiting (pid=%ld)\n", (long) getpid() );
690 #ifdef DEBUG_OBJECTS
691 /* shut down everything properly */
692 release_object( master_socket );
693 close_global_handles();
694 close_registry();
695 close_atom_table();
696 #else
697 exit(0);
698 #endif
701 /* close the master socket and stop waiting for new clients */
702 void close_master_socket(void)
704 struct timeval when;
706 if (master_socket_timeout == -1) return; /* just keep running forever */
708 if (master_socket_timeout)
710 gettimeofday( &when, 0 );
711 add_timeout( &when, master_socket_timeout * 1000 );
712 master_socket->timeout = add_timeout_user( &when, close_socket_timeout, NULL );
714 else close_socket_timeout( NULL ); /* close it right away */
717 /* lock/unlock the master socket to stop accepting new clients */
718 void lock_master_socket( int locked )
720 set_select_events( &master_socket->obj, locked ? 0 : POLLIN );