- fix the "int format, HANDLE arg" type of warnings for comctl32
[wine/hacks.git] / server / request.c
blob888841ebeeb93d72c57c2924a6d65e6e30610123
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 || thread->state == TERMINATED)
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* %p -> %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 /* create the lock file and return its file descriptor */
517 static int create_server_lock(void)
519 struct stat st;
520 int fd;
522 if (lstat( server_lock_name, &st ) == -1)
524 if (errno != ENOENT)
525 fatal_perror( "lstat %s/%s", wine_get_server_dir(), server_lock_name );
527 else
529 if (!S_ISREG(st.st_mode))
530 fatal_error( "%s/%s is not a regular file\n", wine_get_server_dir(), server_lock_name );
533 if ((fd = open( server_lock_name, O_CREAT|O_TRUNC|O_WRONLY, 0600 )) == -1)
534 fatal_perror( "error creating %s/%s", wine_get_server_dir(), server_lock_name );
535 return fd;
538 /* wait for the server lock */
539 int wait_for_lock(void)
541 int fd, r;
542 struct flock fl;
544 create_server_dir();
545 fd = create_server_lock();
547 fl.l_type = F_WRLCK;
548 fl.l_whence = SEEK_SET;
549 fl.l_start = 0;
550 fl.l_len = 1;
551 r = fcntl( fd, F_SETLKW, &fl );
552 close(fd);
554 return r;
557 /* kill the wine server holding the lock */
558 int kill_lock_owner( int sig )
560 int fd, i, ret = 0;
561 pid_t pid = 0;
562 struct flock fl;
564 create_server_dir();
565 fd = create_server_lock();
567 for (i = 0; i < 10; i++)
569 fl.l_type = F_WRLCK;
570 fl.l_whence = SEEK_SET;
571 fl.l_start = 0;
572 fl.l_len = 1;
573 if (fcntl( fd, F_GETLK, &fl ) == -1) goto done;
574 if (fl.l_type != F_WRLCK) goto done; /* the file is not locked */
575 if (!pid) /* first time around */
577 if (!(pid = fl.l_pid)) goto done; /* shouldn't happen */
578 if (sig == -1)
580 if (kill( pid, SIGINT ) == -1) goto done;
581 kill( pid, SIGCONT );
582 ret = 1;
584 else /* just send the specified signal and return */
586 ret = (kill( pid, sig ) != -1);
587 goto done;
590 else if (fl.l_pid != pid) goto done; /* no longer the same process */
591 sleep( 1 );
593 /* waited long enough, now kill it */
594 kill( pid, SIGKILL );
596 done:
597 close( fd );
598 return ret;
601 /* acquire the main server lock */
602 static void acquire_lock(void)
604 struct sockaddr_un addr;
605 struct stat st;
606 struct flock fl;
607 int fd, slen, got_lock = 0;
609 fd = create_server_lock();
611 fl.l_type = F_WRLCK;
612 fl.l_whence = SEEK_SET;
613 fl.l_start = 0;
614 fl.l_len = 1;
615 if (fcntl( fd, F_SETLK, &fl ) != -1)
617 /* check for crashed server */
618 if (stat( server_socket_name, &st ) != -1 && /* there is a leftover socket */
619 stat( "core", &st ) != -1 && st.st_size) /* and there is a non-empty core file */
621 fprintf( stderr,
622 "Warning: a previous instance of the wine server seems to have crashed.\n"
623 "Please run 'gdb %s %s/core',\n"
624 "type 'backtrace' at the gdb prompt and report the results. Thanks.\n\n",
625 server_argv0, wine_get_server_dir() );
627 unlink( server_socket_name ); /* we got the lock, we can safely remove the socket */
628 got_lock = 1;
629 /* in that case we reuse fd without closing it, this ensures
630 * that we hold the lock until the process exits */
632 else
634 switch(errno)
636 case ENOLCK:
637 break;
638 case EACCES:
639 /* check whether locks work at all on this file system */
640 if (fcntl( fd, F_GETLK, &fl ) == -1) break;
641 /* fall through */
642 case EAGAIN:
643 exit(2); /* we didn't get the lock, exit with special status */
644 default:
645 fatal_perror( "fcntl %s/%s", wine_get_server_dir(), server_lock_name );
647 /* it seems we can't use locks on this fs, so we will use the socket existence as lock */
648 close( fd );
651 if ((fd = socket( AF_UNIX, SOCK_STREAM, 0 )) == -1) fatal_perror( "socket" );
652 addr.sun_family = AF_UNIX;
653 strcpy( addr.sun_path, server_socket_name );
654 slen = sizeof(addr) - sizeof(addr.sun_path) + strlen(addr.sun_path) + 1;
655 #ifdef HAVE_SOCKADDR_SUN_LEN
656 addr.sun_len = slen;
657 #endif
658 if (bind( fd, (struct sockaddr *)&addr, slen ) == -1)
660 if ((errno == EEXIST) || (errno == EADDRINUSE))
662 if (got_lock)
663 fatal_error( "couldn't bind to the socket even though we hold the lock\n" );
664 exit(2); /* we didn't get the lock, exit with special status */
666 fatal_perror( "bind" );
668 atexit( socket_cleanup );
669 chmod( server_socket_name, 0600 ); /* make sure no other user can connect */
670 if (listen( fd, 5 ) == -1) fatal_perror( "listen" );
672 if (!(master_socket = alloc_object( &master_socket_ops, fd )))
673 fatal_error( "out of memory\n" );
674 master_socket->timeout = NULL;
675 set_select_events( &master_socket->obj, POLLIN );
678 /* open the master server socket and start waiting for new clients */
679 void open_master_socket(void)
681 int pid, status, sync_pipe[2];
682 char dummy;
684 /* make sure no request is larger than the maximum size */
685 assert( sizeof(union generic_request) == sizeof(struct request_max_size) );
686 assert( sizeof(union generic_reply) == sizeof(struct request_max_size) );
688 create_server_dir();
689 if (pipe( sync_pipe ) == -1) fatal_perror( "pipe" );
691 pid = fork();
692 switch( pid )
694 case 0: /* child */
695 setsid();
696 close( sync_pipe[0] );
698 acquire_lock();
700 /* signal parent */
701 write( sync_pipe[1], &dummy, 1 );
702 close( sync_pipe[1] );
703 break;
705 case -1:
706 fatal_perror( "fork" );
707 break;
709 default: /* parent */
710 close( sync_pipe[1] );
712 /* wait for child to signal us and then exit */
713 if (read( sync_pipe[0], &dummy, 1 ) == 1) _exit(0);
715 /* child terminated, propagate exit status */
716 wait4( pid, &status, 0, NULL );
717 if (WIFEXITED(status)) _exit( WEXITSTATUS(status) );
718 _exit(1);
721 /* setup msghdr structure constant fields */
722 msghdr.msg_name = NULL;
723 msghdr.msg_namelen = 0;
724 msghdr.msg_iov = &myiovec;
725 msghdr.msg_iovlen = 1;
727 /* init startup ticks */
728 server_start_ticks = get_tick_count();
731 /* master socket timer expiration handler */
732 static void close_socket_timeout( void *arg )
734 master_socket->timeout = NULL;
735 flush_registry();
737 /* if a new client is waiting, we keep on running */
738 if (check_select_events( master_socket->obj.fd, POLLIN )) return;
740 if (debug_level) fprintf( stderr, "wineserver: exiting (pid=%ld)\n", (long) getpid() );
742 #ifdef DEBUG_OBJECTS
743 /* shut down everything properly */
744 release_object( master_socket );
745 close_global_handles();
746 close_registry();
747 close_atom_table();
748 #else
749 exit(0);
750 #endif
753 /* close the master socket and stop waiting for new clients */
754 void close_master_socket(void)
756 struct timeval when;
758 if (master_socket_timeout == -1) return; /* just keep running forever */
760 if (master_socket_timeout)
762 gettimeofday( &when, 0 );
763 add_timeout( &when, master_socket_timeout * 1000 );
764 master_socket->timeout = add_timeout_user( &when, close_socket_timeout, NULL );
766 else close_socket_timeout( NULL ); /* close it right away */
769 /* lock/unlock the master socket to stop accepting new clients */
770 void lock_master_socket( int locked )
772 set_select_events( &master_socket->obj, locked ? 0 : POLLIN );