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>
55 # include <mach/mach_time.h>
59 #define WIN32_NO_STATUS
64 #include "wine/library.h"
70 #define WANT_REQUEST_HANDLERS
73 /* Some versions of glibc don't define this */
78 /* path names for server master Unix socket */
79 static const char * const server_socket_name
= "socket"; /* name of the socket file */
80 static const char * const server_lock_name
= "lock"; /* name of the server lock file */
84 struct object obj
; /* object header */
85 struct fd
*fd
; /* file descriptor of the master socket */
88 static void master_socket_dump( struct object
*obj
, int verbose
);
89 static void master_socket_destroy( struct object
*obj
);
90 static void master_socket_poll_event( struct fd
*fd
, int event
);
92 static const struct object_ops master_socket_ops
=
94 sizeof(struct master_socket
), /* size */
95 master_socket_dump
, /* dump */
96 no_get_type
, /* get_type */
97 no_add_queue
, /* add_queue */
98 NULL
, /* remove_queue */
100 NULL
, /* satisfied */
101 no_signal
, /* signal */
102 no_get_fd
, /* get_fd */
103 no_map_access
, /* map_access */
104 default_get_sd
, /* get_sd */
105 default_set_sd
, /* set_sd */
106 no_lookup_name
, /* lookup_name */
107 no_link_name
, /* link_name */
108 NULL
, /* unlink_name */
109 no_open_file
, /* open_file */
110 no_close_handle
, /* close_handle */
111 master_socket_destroy
/* destroy */
114 static const struct fd_ops master_socket_fd_ops
=
116 NULL
, /* get_poll_events */
117 master_socket_poll_event
, /* poll_event */
119 NULL
, /* get_fd_type */
121 NULL
, /* queue_async */
122 NULL
/* reselect_async */
126 struct thread
*current
= NULL
; /* thread handling the current request */
127 unsigned int global_error
= 0; /* global error code for when no thread is current */
128 timeout_t server_start_time
= 0; /* server startup time */
129 int server_dir_fd
= -1; /* file descriptor for the server dir */
130 int config_dir_fd
= -1; /* file descriptor for the config dir */
132 static struct master_socket
*master_socket
; /* the master socket object */
133 static struct timeout_user
*master_timeout
;
135 /* complain about a protocol error and terminate the client connection */
136 void fatal_protocol_error( struct thread
*thread
, const char *err
, ... )
140 va_start( args
, err
);
141 fprintf( stderr
, "Protocol error:%04x: ", thread
->id
);
142 vfprintf( stderr
, err
, args
);
144 thread
->exit_code
= 1;
145 kill_thread( thread
, 1 );
148 /* die on a fatal error */
149 void fatal_error( const char *err
, ... )
153 va_start( args
, err
);
154 fprintf( stderr
, "wineserver: " );
155 vfprintf( stderr
, err
, args
);
160 /* allocate the reply data */
161 void *set_reply_data_size( data_size_t size
)
163 assert( size
<= get_reply_max_size() );
164 if (size
&& !(current
->reply_data
= mem_alloc( size
))) size
= 0;
165 current
->reply_size
= size
;
166 return current
->reply_data
;
169 /* return object attributes from the current request */
170 const struct object_attributes
*get_req_object_attributes( const struct security_descriptor
**sd
,
171 struct unicode_str
*name
,
172 struct object
**root
)
174 static const struct object_attributes empty_attributes
;
175 const struct object_attributes
*attr
= get_req_data();
176 data_size_t size
= get_req_data_size();
178 if (root
) *root
= NULL
;
184 return &empty_attributes
;
187 if ((size
< sizeof(*attr
)) || (size
- sizeof(*attr
) < attr
->sd_len
) ||
188 (size
- sizeof(*attr
) - attr
->sd_len
< attr
->name_len
))
190 set_error( STATUS_ACCESS_VIOLATION
);
193 if (attr
->sd_len
&& !sd_is_valid( (const struct security_descriptor
*)(attr
+ 1), attr
->sd_len
))
195 set_error( STATUS_INVALID_SECURITY_DESCR
);
198 if ((attr
->name_len
& (sizeof(WCHAR
) - 1)) || attr
->name_len
>= 65534)
200 set_error( STATUS_OBJECT_NAME_INVALID
);
203 if (root
&& attr
->rootdir
&& attr
->name_len
)
205 if (!(*root
= get_directory_obj( current
->process
, attr
->rootdir
))) return NULL
;
207 *sd
= attr
->sd_len
? (const struct security_descriptor
*)(attr
+ 1) : NULL
;
208 name
->len
= attr
->name_len
;
209 name
->str
= (const WCHAR
*)(attr
+ 1) + attr
->sd_len
/ sizeof(WCHAR
);
213 /* return a pointer to the request data following an object attributes structure */
214 const void *get_req_data_after_objattr( const struct object_attributes
*attr
, data_size_t
*len
)
216 const void *ptr
= (const WCHAR
*)((const struct object_attributes
*)get_req_data() + 1) +
217 attr
->sd_len
/ sizeof(WCHAR
) + attr
->name_len
/ sizeof(WCHAR
);
218 *len
= get_req_data_size() - ((const char *)ptr
- (const char *)get_req_data());
222 /* write the remaining part of the reply */
223 void write_reply( struct thread
*thread
)
227 if ((ret
= write( get_unix_fd( thread
->reply_fd
),
228 (char *)thread
->reply_data
+ thread
->reply_size
- thread
->reply_towrite
,
229 thread
->reply_towrite
)) >= 0)
231 if (!(thread
->reply_towrite
-= ret
))
233 free( thread
->reply_data
);
234 thread
->reply_data
= NULL
;
235 /* sent everything, can go back to waiting for requests */
236 set_fd_events( thread
->request_fd
, POLLIN
);
237 set_fd_events( thread
->reply_fd
, 0 );
242 kill_thread( thread
, 0 ); /* normal death */
243 else if (errno
!= EWOULDBLOCK
&& (EWOULDBLOCK
== EAGAIN
|| errno
!= EAGAIN
))
244 fatal_protocol_error( thread
, "reply write: %s\n", strerror( errno
));
247 /* send a reply to the current thread */
248 static void send_reply( union generic_reply
*reply
)
252 if (!current
->reply_size
)
254 if ((ret
= write( get_unix_fd( current
->reply_fd
),
255 reply
, sizeof(*reply
) )) != sizeof(*reply
)) goto error
;
261 vec
[0].iov_base
= (void *)reply
;
262 vec
[0].iov_len
= sizeof(*reply
);
263 vec
[1].iov_base
= current
->reply_data
;
264 vec
[1].iov_len
= current
->reply_size
;
266 if ((ret
= writev( get_unix_fd( current
->reply_fd
), vec
, 2 )) < sizeof(*reply
)) goto error
;
268 if ((current
->reply_towrite
= current
->reply_size
- (ret
- sizeof(*reply
))))
270 /* couldn't write it all, wait for POLLOUT */
271 set_fd_events( current
->reply_fd
, POLLOUT
);
272 set_fd_events( current
->request_fd
, 0 );
276 free( current
->reply_data
);
277 current
->reply_data
= NULL
;
282 fatal_protocol_error( current
, "partial write %d\n", ret
);
283 else if (errno
== EPIPE
)
284 kill_thread( current
, 0 ); /* normal death */
286 fatal_protocol_error( current
, "reply write: %s\n", strerror( errno
));
289 /* call a request handler */
290 static void call_req_handler( struct thread
*thread
)
292 union generic_reply reply
;
293 enum request req
= thread
->req
.request_header
.req
;
296 current
->reply_size
= 0;
298 memset( &reply
, 0, sizeof(reply
) );
300 if (debug_level
) trace_request();
302 if (req
< REQ_NB_REQUESTS
)
303 req_handlers
[req
]( ¤t
->req
, &reply
);
305 set_error( STATUS_NOT_IMPLEMENTED
);
309 if (current
->reply_fd
)
311 reply
.reply_header
.error
= current
->error
;
312 reply
.reply_header
.reply_size
= current
->reply_size
;
313 if (debug_level
) trace_reply( req
, &reply
);
314 send_reply( &reply
);
318 current
->exit_code
= 1;
319 kill_thread( current
, 1 ); /* no way to continue without reply fd */
325 /* read a request from a thread */
326 void read_request( struct thread
*thread
)
330 if (!thread
->req_toread
) /* no pending request */
332 if ((ret
= read( get_unix_fd( thread
->request_fd
), &thread
->req
,
333 sizeof(thread
->req
) )) != sizeof(thread
->req
)) goto error
;
334 if (!(thread
->req_toread
= thread
->req
.request_header
.request_size
))
336 /* no data, handle request at once */
337 call_req_handler( thread
);
340 if (!(thread
->req_data
= malloc( thread
->req_toread
)))
342 fatal_protocol_error( thread
, "no memory for %u bytes request %d\n",
343 thread
->req_toread
, thread
->req
.request_header
.req
);
348 /* read the variable sized data */
351 ret
= read( get_unix_fd( thread
->request_fd
),
352 (char *)thread
->req_data
+ thread
->req
.request_header
.request_size
353 - thread
->req_toread
,
354 thread
->req_toread
);
356 if (!(thread
->req_toread
-= ret
))
358 call_req_handler( thread
);
359 free( thread
->req_data
);
360 thread
->req_data
= NULL
;
366 if (!ret
) /* closed pipe */
367 kill_thread( thread
, 0 );
369 fatal_protocol_error( thread
, "partial read %d\n", ret
);
370 else if (errno
!= EWOULDBLOCK
&& (EWOULDBLOCK
== EAGAIN
|| errno
!= EAGAIN
))
371 fatal_protocol_error( thread
, "read: %s\n", strerror( errno
));
374 /* receive a file descriptor on the process socket */
375 int receive_fd( struct process
*process
)
379 struct msghdr msghdr
;
382 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
383 msghdr
.msg_accrightslen
= sizeof(int);
384 msghdr
.msg_accrights
= (void *)&fd
;
385 #else /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
386 char cmsg_buffer
[256];
387 msghdr
.msg_control
= cmsg_buffer
;
388 msghdr
.msg_controllen
= sizeof(cmsg_buffer
);
389 msghdr
.msg_flags
= 0;
390 #endif /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
392 msghdr
.msg_name
= NULL
;
393 msghdr
.msg_namelen
= 0;
394 msghdr
.msg_iov
= &vec
;
395 msghdr
.msg_iovlen
= 1;
396 vec
.iov_base
= (void *)&data
;
397 vec
.iov_len
= sizeof(data
);
399 ret
= recvmsg( get_unix_fd( process
->msg_fd
), &msghdr
, 0 );
401 #ifndef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
404 struct cmsghdr
*cmsg
;
405 for (cmsg
= CMSG_FIRSTHDR( &msghdr
); cmsg
; cmsg
= CMSG_NXTHDR( &msghdr
, cmsg
))
407 if (cmsg
->cmsg_level
!= SOL_SOCKET
) continue;
408 if (cmsg
->cmsg_type
== SCM_RIGHTS
) fd
= *(int *)CMSG_DATA(cmsg
);
411 #endif /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
413 if (ret
== sizeof(data
))
415 struct thread
*thread
;
417 if (data
.tid
) thread
= get_thread_from_id( data
.tid
);
418 else thread
= (struct thread
*)grab_object( get_process_first_thread( process
));
420 if (!thread
|| thread
->process
!= process
|| thread
->state
== TERMINATED
)
423 fprintf( stderr
, "%04x: *fd* %d <- %d bad thread id\n",
424 data
.tid
, data
.fd
, fd
);
430 fprintf( stderr
, "%04x: *fd* %d <- %d\n",
431 thread
->id
, data
.fd
, fd
);
432 thread_add_inflight_fd( thread
, data
.fd
, fd
);
434 if (thread
) release_object( thread
);
440 kill_process( process
, 0 );
444 fprintf( stderr
, "Protocol error: process %04x: partial recvmsg %d for fd\n",
446 if (fd
!= -1) close( fd
);
447 kill_process( process
, 1 );
451 if (errno
!= EWOULDBLOCK
&& (EWOULDBLOCK
== EAGAIN
|| errno
!= EAGAIN
))
453 fprintf( stderr
, "Protocol error: process %04x: ", process
->id
);
455 kill_process( process
, 1 );
461 /* send an fd to a client */
462 int send_client_fd( struct process
*process
, int fd
, obj_handle_t handle
)
465 struct msghdr msghdr
;
468 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
469 msghdr
.msg_accrightslen
= sizeof(fd
);
470 msghdr
.msg_accrights
= (void *)&fd
;
471 #else /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
472 char cmsg_buffer
[256];
473 struct cmsghdr
*cmsg
;
474 msghdr
.msg_control
= cmsg_buffer
;
475 msghdr
.msg_controllen
= sizeof(cmsg_buffer
);
476 msghdr
.msg_flags
= 0;
477 cmsg
= CMSG_FIRSTHDR( &msghdr
);
478 cmsg
->cmsg_len
= CMSG_LEN( sizeof(fd
) );
479 cmsg
->cmsg_level
= SOL_SOCKET
;
480 cmsg
->cmsg_type
= SCM_RIGHTS
;
481 *(int *)CMSG_DATA(cmsg
) = fd
;
482 msghdr
.msg_controllen
= cmsg
->cmsg_len
;
483 #endif /* HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS */
485 msghdr
.msg_name
= NULL
;
486 msghdr
.msg_namelen
= 0;
487 msghdr
.msg_iov
= &vec
;
488 msghdr
.msg_iovlen
= 1;
490 vec
.iov_base
= (void *)&handle
;
491 vec
.iov_len
= sizeof(handle
);
494 fprintf( stderr
, "%04x: *fd* %04x -> %d\n", current
? current
->id
: process
->id
, handle
, fd
);
496 ret
= sendmsg( get_unix_fd( process
->msg_fd
), &msghdr
, 0 );
498 if (ret
== sizeof(handle
)) return 0;
502 fprintf( stderr
, "Protocol error: process %04x: partial sendmsg %d\n", process
->id
, ret
);
503 kill_process( process
, 1 );
505 else if (errno
== EPIPE
)
507 kill_process( process
, 0 );
511 fprintf( stderr
, "Protocol error: process %04x: ", process
->id
);
513 kill_process( process
, 1 );
518 /* get current tick count to return to client */
519 unsigned int get_tick_count(void)
522 static mach_timebase_info_data_t timebase
;
524 if (!timebase
.denom
) mach_timebase_info( &timebase
);
525 return mach_absolute_time() * timebase
.numer
/ timebase
.denom
/ 1000000;
526 #elif defined(HAVE_CLOCK_GETTIME)
528 #ifdef CLOCK_MONOTONIC_RAW
529 if (!clock_gettime( CLOCK_MONOTONIC_RAW
, &ts
))
530 return ts
.tv_sec
* 1000 + ts
.tv_nsec
/ 1000000;
532 if (!clock_gettime( CLOCK_MONOTONIC
, &ts
))
533 return ts
.tv_sec
* 1000 + ts
.tv_nsec
/ 1000000;
535 return (current_time
- server_start_time
) / 10000;
538 static void master_socket_dump( struct object
*obj
, int verbose
)
540 struct master_socket
*sock
= (struct master_socket
*)obj
;
541 assert( obj
->ops
== &master_socket_ops
);
542 fprintf( stderr
, "Master socket fd=%p\n", sock
->fd
);
545 static void master_socket_destroy( struct object
*obj
)
547 struct master_socket
*sock
= (struct master_socket
*)obj
;
548 assert( obj
->ops
== &master_socket_ops
);
549 release_object( sock
->fd
);
552 /* handle a socket event */
553 static void master_socket_poll_event( struct fd
*fd
, int event
)
555 struct master_socket
*sock
= get_fd_user( fd
);
556 assert( master_socket
->obj
.ops
== &master_socket_ops
);
558 assert( sock
== master_socket
); /* there is only one master socket */
560 if (event
& (POLLERR
| POLLHUP
))
562 /* this is not supposed to happen */
563 fprintf( stderr
, "wineserver: Error on master socket\n" );
564 set_fd_events( sock
->fd
, -1 );
566 else if (event
& POLLIN
)
568 struct sockaddr_un dummy
;
569 socklen_t len
= sizeof(dummy
);
570 int client
= accept( get_unix_fd( master_socket
->fd
), (struct sockaddr
*) &dummy
, &len
);
571 if (client
== -1) return;
572 fcntl( client
, F_SETFL
, O_NONBLOCK
);
573 create_process( client
, NULL
, 0 );
577 /* remove the socket upon exit */
578 static void socket_cleanup(void)
580 static int do_it_once
;
581 if (!do_it_once
++) unlink( server_socket_name
);
584 /* create a directory and check its permissions */
585 static void create_dir( const char *name
, struct stat
*st
)
587 if (lstat( name
, st
) == -1)
590 fatal_error( "lstat %s: %s", name
, strerror( errno
));
591 if (mkdir( name
, 0700 ) == -1 && errno
!= EEXIST
)
592 fatal_error( "mkdir %s: %s\n", name
, strerror( errno
));
593 if (lstat( name
, st
) == -1)
594 fatal_error( "lstat %s: %s\n", name
, strerror( errno
));
596 if (!S_ISDIR(st
->st_mode
)) fatal_error( "%s is not a directory\n", name
);
597 if (st
->st_uid
!= getuid()) fatal_error( "%s is not owned by you\n", name
);
598 if (st
->st_mode
& 077) fatal_error( "%s must not be accessible by other users\n", name
);
601 /* create the server directory and chdir to it */
602 static void create_server_dir( const char *dir
)
604 char *p
, *server_dir
;
607 if (!(server_dir
= strdup( dir
))) fatal_error( "out of memory\n" );
609 /* first create the base directory if needed */
611 p
= strrchr( server_dir
, '/' );
613 create_dir( server_dir
, &st
);
615 /* now create the server directory */
618 create_dir( server_dir
, &st
);
620 if (chdir( server_dir
) == -1)
621 fatal_error( "chdir %s: %s\n", server_dir
, strerror( errno
));
622 if ((server_dir_fd
= open( ".", O_RDONLY
)) == -1)
623 fatal_error( "open %s: %s\n", server_dir
, strerror( errno
));
624 if (fstat( server_dir_fd
, &st2
) == -1)
625 fatal_error( "stat %s: %s\n", server_dir
, strerror( errno
));
626 if (st
.st_dev
!= st2
.st_dev
|| st
.st_ino
!= st2
.st_ino
)
627 fatal_error( "chdir did not end up in %s\n", server_dir
);
632 /* create the lock file and return its file descriptor */
633 static int create_server_lock(void)
638 if (lstat( server_lock_name
, &st
) == -1)
641 fatal_error( "lstat %s/%s: %s", wine_get_server_dir(), server_lock_name
, strerror( errno
));
645 if (!S_ISREG(st
.st_mode
))
646 fatal_error( "%s/%s is not a regular file\n", wine_get_server_dir(), server_lock_name
);
649 if ((fd
= open( server_lock_name
, O_CREAT
|O_TRUNC
|O_WRONLY
, 0600 )) == -1)
650 fatal_error( "error creating %s/%s: %s", wine_get_server_dir(), server_lock_name
, strerror( errno
));
654 /* wait for the server lock */
655 int wait_for_lock(void)
657 const char *server_dir
= wine_get_server_dir();
661 if (!server_dir
) return 0; /* no server dir, so no lock to wait on */
663 create_server_dir( server_dir
);
664 fd
= create_server_lock();
667 fl
.l_whence
= SEEK_SET
;
670 r
= fcntl( fd
, F_SETLKW
, &fl
);
676 /* kill the wine server holding the lock */
677 int kill_lock_owner( int sig
)
679 const char *server_dir
= wine_get_server_dir();
684 if (!server_dir
) return 0; /* no server dir, nothing to do */
686 create_server_dir( server_dir
);
687 fd
= create_server_lock();
689 for (i
= 1; i
<= 20; i
++)
692 fl
.l_whence
= SEEK_SET
;
695 if (fcntl( fd
, F_GETLK
, &fl
) == -1) goto done
;
696 if (fl
.l_type
!= F_WRLCK
) goto done
; /* the file is not locked */
697 if (!pid
) /* first time around */
699 if (!(pid
= fl
.l_pid
)) goto done
; /* shouldn't happen */
702 if (kill( pid
, SIGINT
) == -1) goto done
;
703 kill( pid
, SIGCONT
);
706 else /* just send the specified signal and return */
708 ret
= (kill( pid
, sig
) != -1);
712 else if (fl
.l_pid
!= pid
) goto done
; /* no longer the same process */
715 /* waited long enough, now kill it */
716 kill( pid
, SIGKILL
);
723 /* acquire the main server lock */
724 static void acquire_lock(void)
726 struct sockaddr_un addr
;
729 int fd
, slen
, got_lock
= 0;
731 fd
= create_server_lock();
734 fl
.l_whence
= SEEK_SET
;
737 if (fcntl( fd
, F_SETLK
, &fl
) != -1)
739 /* check for crashed server */
740 if (stat( server_socket_name
, &st
) != -1 && /* there is a leftover socket */
741 stat( "core", &st
) != -1 && st
.st_size
) /* and there is a non-empty core file */
744 "Warning: a previous instance of the wine server seems to have crashed.\n"
745 "Please run 'gdb %s %s/core',\n"
746 "type 'backtrace' at the gdb prompt and report the results. Thanks.\n\n",
747 server_argv0
, wine_get_server_dir() );
749 unlink( server_socket_name
); /* we got the lock, we can safely remove the socket */
751 /* in that case we reuse fd without closing it, this ensures
752 * that we hold the lock until the process exits */
761 /* check whether locks work at all on this file system */
762 if (fcntl( fd
, F_GETLK
, &fl
) == -1) break;
765 exit(2); /* we didn't get the lock, exit with special status */
767 fatal_error( "fcntl %s/%s: %s", wine_get_server_dir(), server_lock_name
, strerror( errno
));
769 /* it seems we can't use locks on this fs, so we will use the socket existence as lock */
773 if ((fd
= socket( AF_UNIX
, SOCK_STREAM
, 0 )) == -1) fatal_error( "socket: %s\n", strerror( errno
));
774 addr
.sun_family
= AF_UNIX
;
775 strcpy( addr
.sun_path
, server_socket_name
);
776 slen
= sizeof(addr
) - sizeof(addr
.sun_path
) + strlen(addr
.sun_path
) + 1;
777 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
780 if (bind( fd
, (struct sockaddr
*)&addr
, slen
) == -1)
782 if ((errno
== EEXIST
) || (errno
== EADDRINUSE
))
785 fatal_error( "couldn't bind to the socket even though we hold the lock\n" );
786 exit(2); /* we didn't get the lock, exit with special status */
788 fatal_error( "bind: %s\n", strerror( errno
));
790 atexit( socket_cleanup
);
791 chmod( server_socket_name
, 0600 ); /* make sure no other user can connect */
792 if (listen( fd
, 5 ) == -1) fatal_error( "listen: %s\n", strerror( errno
));
794 if (!(master_socket
= alloc_object( &master_socket_ops
)) ||
795 !(master_socket
->fd
= create_anonymous_fd( &master_socket_fd_ops
, fd
, &master_socket
->obj
, 0 )))
796 fatal_error( "out of memory\n" );
797 set_fd_events( master_socket
->fd
, POLLIN
);
798 make_object_static( &master_socket
->obj
);
801 /* open the master server socket and start waiting for new clients */
802 void open_master_socket(void)
804 const char *server_dir
= wine_get_server_dir();
805 const char *config_dir
= wine_get_config_dir();
806 int fd
, pid
, status
, sync_pipe
[2];
809 /* make sure no request is larger than the maximum size */
810 assert( sizeof(union generic_request
) == sizeof(struct request_max_size
) );
811 assert( sizeof(union generic_reply
) == sizeof(struct request_max_size
) );
813 /* make sure the stdio fds are open */
814 fd
= open( "/dev/null", O_RDWR
);
815 while (fd
>= 0 && fd
<= 2) fd
= dup( fd
);
818 fatal_error( "directory %s cannot be accessed\n", config_dir
);
819 if (chdir( config_dir
) == -1)
820 fatal_error( "chdir to %s: %s\n", config_dir
, strerror( errno
));
821 if ((config_dir_fd
= open( ".", O_RDONLY
)) == -1)
822 fatal_error( "open %s: %s\n", config_dir
, strerror( errno
));
824 create_server_dir( server_dir
);
828 if (pipe( sync_pipe
) == -1) fatal_error( "pipe: %s\n", strerror( errno
));
834 close( sync_pipe
[0] );
838 /* close stdin and stdout */
844 write( sync_pipe
[1], &dummy
, 1 );
845 close( sync_pipe
[1] );
849 fatal_error( "fork: %s\n", strerror( errno
));
852 default: /* parent */
853 close( sync_pipe
[1] );
855 /* wait for child to signal us and then exit */
856 if (read( sync_pipe
[0], &dummy
, 1 ) == 1) _exit(0);
858 /* child terminated, propagate exit status */
859 waitpid( pid
, &status
, 0 );
860 if (WIFEXITED(status
)) _exit( WEXITSTATUS(status
) );
864 else /* remain in the foreground */
869 /* init the process tracing mechanism */
870 init_tracing_mechanism();
874 /* master socket timer expiration handler */
875 static void close_socket_timeout( void *arg
)
877 master_timeout
= NULL
;
879 if (debug_level
) fprintf( stderr
, "wineserver: exiting (pid=%ld)\n", (long) getpid() );
882 close_objects(); /* shut down everything properly */
887 /* close the master socket and stop waiting for new clients */
888 void close_master_socket( timeout_t timeout
)
892 release_object( master_socket
);
893 master_socket
= NULL
;
895 if (master_timeout
) /* cancel previous timeout */
896 remove_timeout_user( master_timeout
);
898 master_timeout
= add_timeout_user( timeout
, close_socket_timeout
, NULL
);