request.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  1. /*
  2. * Server-side request handling
  3. *
  4. * Copyright (C) 1998 Alexandre Julliard
  5. *
  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.
  10. *
  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.
  15. *
  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
  19. */
  20. #include "config.h"
  21. #include <assert.h>
  22. #include <errno.h>
  23. #include <fcntl.h>
  24. #ifdef HAVE_PWD_H
  25. #include <pwd.h>
  26. #endif
  27. #include <signal.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <stdarg.h>
  31. #include <string.h>
  32. #include <sys/stat.h>
  33. #include <sys/time.h>
  34. #include <sys/types.h>
  35. #include <sys/socket.h>
  36. #include <sys/wait.h>
  37. #ifdef HAVE_SYS_UIO_H
  38. #include <sys/uio.h>
  39. #endif
  40. #ifdef HAVE_SYS_UN_H
  41. #include <sys/un.h>
  42. #endif
  43. #include <unistd.h>
  44. #include <poll.h>
  45. #ifdef __APPLE__
  46. # include <mach/mach_time.h>
  47. #endif
  48. #include "ntstatus.h"
  49. #define WIN32_NO_STATUS
  50. #include "windef.h"
  51. #include "winbase.h"
  52. #include "wincon.h"
  53. #include "winternl.h"
  54. #include "file.h"
  55. #include "process.h"
  56. #include "thread.h"
  57. #include "security.h"
  58. #include "handle.h"
  59. #define WANT_REQUEST_HANDLERS
  60. #include "request.h"
  61. /* Some versions of glibc don't define this */
  62. #ifndef SCM_RIGHTS
  63. #define SCM_RIGHTS 1
  64. #endif
  65. /* path names for server master Unix socket */
  66. static const char * const server_socket_name = "socket"; /* name of the socket file */
  67. static const char * const server_lock_name = "lock"; /* name of the server lock file */
  68. struct master_socket
  69. {
  70. struct object obj; /* object header */
  71. struct fd *fd; /* file descriptor of the master socket */
  72. };
  73. static void master_socket_dump( struct object *obj, int verbose );
  74. static void master_socket_destroy( struct object *obj );
  75. static void master_socket_poll_event( struct fd *fd, int event );
  76. static const struct object_ops master_socket_ops =
  77. {
  78. sizeof(struct master_socket), /* size */
  79. &no_type, /* type */
  80. master_socket_dump, /* dump */
  81. no_add_queue, /* add_queue */
  82. NULL, /* remove_queue */
  83. NULL, /* signaled */
  84. NULL, /* satisfied */
  85. no_signal, /* signal */
  86. no_get_fd, /* get_fd */
  87. default_map_access, /* map_access */
  88. default_get_sd, /* get_sd */
  89. default_set_sd, /* set_sd */
  90. no_get_full_name, /* get_full_name */
  91. no_lookup_name, /* lookup_name */
  92. no_link_name, /* link_name */
  93. NULL, /* unlink_name */
  94. no_open_file, /* open_file */
  95. no_kernel_obj_list, /* get_kernel_obj_list */
  96. no_get_fast_sync, /* get_fast_sync */
  97. no_close_handle, /* close_handle */
  98. master_socket_destroy /* destroy */
  99. };
  100. static const struct fd_ops master_socket_fd_ops =
  101. {
  102. NULL, /* get_poll_events */
  103. master_socket_poll_event, /* poll_event */
  104. NULL, /* flush */
  105. NULL, /* get_fd_type */
  106. NULL, /* ioctl */
  107. NULL, /* queue_async */
  108. NULL /* reselect_async */
  109. };
  110. struct thread *current = NULL; /* thread handling the current request */
  111. unsigned int global_error = 0; /* global error code for when no thread is current */
  112. timeout_t server_start_time = 0; /* server startup time */
  113. char *server_dir = NULL; /* server directory */
  114. int server_dir_fd = -1; /* file descriptor for the server dir */
  115. int config_dir_fd = -1; /* file descriptor for the config dir */
  116. static struct master_socket *master_socket; /* the master socket object */
  117. static struct timeout_user *master_timeout;
  118. /* complain about a protocol error and terminate the client connection */
  119. void fatal_protocol_error( struct thread *thread, const char *err, ... )
  120. {
  121. va_list args;
  122. va_start( args, err );
  123. fprintf( stderr, "Protocol error:%04x: ", thread->id );
  124. vfprintf( stderr, err, args );
  125. va_end( args );
  126. thread->exit_code = 1;
  127. kill_thread( thread, 1 );
  128. }
  129. /* die on a fatal error */
  130. void fatal_error( const char *err, ... )
  131. {
  132. va_list args;
  133. va_start( args, err );
  134. fprintf( stderr, "wineserver: " );
  135. vfprintf( stderr, err, args );
  136. va_end( args );
  137. exit(1);
  138. }
  139. /* allocate the reply data */
  140. void *set_reply_data_size( data_size_t size )
  141. {
  142. assert( size <= get_reply_max_size() );
  143. if (size && !(current->reply_data = mem_alloc( size ))) size = 0;
  144. current->reply_size = size;
  145. return current->reply_data;
  146. }
  147. static const struct object_attributes empty_attributes;
  148. /* return object attributes from the current request */
  149. const struct object_attributes *get_req_object_attributes( const struct security_descriptor **sd,
  150. struct unicode_str *name,
  151. struct object **root )
  152. {
  153. const struct object_attributes *attr = get_req_data();
  154. data_size_t size = get_req_data_size();
  155. if (root) *root = NULL;
  156. if (!size)
  157. {
  158. *sd = NULL;
  159. name->len = 0;
  160. return &empty_attributes;
  161. }
  162. if ((size < sizeof(*attr)) || (size - sizeof(*attr) < attr->sd_len) ||
  163. (size - sizeof(*attr) - attr->sd_len < attr->name_len))
  164. {
  165. set_error( STATUS_ACCESS_VIOLATION );
  166. return NULL;
  167. }
  168. if (attr->sd_len && !sd_is_valid( (const struct security_descriptor *)(attr + 1), attr->sd_len ))
  169. {
  170. set_error( STATUS_INVALID_SECURITY_DESCR );
  171. return NULL;
  172. }
  173. if ((attr->name_len & (sizeof(WCHAR) - 1)) || attr->name_len >= 65534)
  174. {
  175. set_error( STATUS_OBJECT_NAME_INVALID );
  176. return NULL;
  177. }
  178. if (root && attr->rootdir && attr->name_len)
  179. {
  180. if (!(*root = get_handle_obj( current->process, attr->rootdir, 0, NULL ))) return NULL;
  181. }
  182. *sd = attr->sd_len ? (const struct security_descriptor *)(attr + 1) : NULL;
  183. name->len = attr->name_len;
  184. name->str = (const WCHAR *)(attr + 1) + attr->sd_len / sizeof(WCHAR);
  185. return attr;
  186. }
  187. /* return a pointer to the request data following an object attributes structure */
  188. const void *get_req_data_after_objattr( const struct object_attributes *attr, data_size_t *len )
  189. {
  190. data_size_t size = (sizeof(*attr) + (attr->sd_len & ~1) + (attr->name_len & ~1) + 3) & ~3;
  191. if (attr == &empty_attributes || size >= get_req_data_size())
  192. {
  193. *len = 0;
  194. return NULL;
  195. }
  196. *len = get_req_data_size() - size;
  197. return (const char *)get_req_data() + size;
  198. }
  199. /* write the remaining part of the reply */
  200. void write_reply( struct thread *thread )
  201. {
  202. int ret;
  203. if ((ret = write( get_unix_fd( thread->reply_fd ),
  204. (char *)thread->reply_data + thread->reply_size - thread->reply_towrite,
  205. thread->reply_towrite )) >= 0)
  206. {
  207. if (!(thread->reply_towrite -= ret))
  208. {
  209. free( thread->reply_data );
  210. thread->reply_data = NULL;
  211. /* sent everything, can go back to waiting for requests */
  212. set_fd_events( thread->request_fd, POLLIN );
  213. set_fd_events( thread->reply_fd, 0 );
  214. }
  215. return;
  216. }
  217. if (errno == EPIPE)
  218. kill_thread( thread, 0 ); /* normal death */
  219. else if (errno != EWOULDBLOCK && (EWOULDBLOCK == EAGAIN || errno != EAGAIN))
  220. fatal_protocol_error( thread, "reply write: %s\n", strerror( errno ));
  221. }
  222. /* send a reply to the current thread */
  223. static void send_reply( union generic_reply *reply )
  224. {
  225. int ret;
  226. if (!current->reply_size)
  227. {
  228. if ((ret = write( get_unix_fd( current->reply_fd ),
  229. reply, sizeof(*reply) )) != sizeof(*reply)) goto error;
  230. }
  231. else
  232. {
  233. struct iovec vec[2];
  234. vec[0].iov_base = (void *)reply;
  235. vec[0].iov_len = sizeof(*reply);
  236. vec[1].iov_base = current->reply_data;
  237. vec[1].iov_len = current->reply_size;
  238. if ((ret = writev( get_unix_fd( current->reply_fd ), vec, 2 )) < sizeof(*reply)) goto error;
  239. if ((current->reply_towrite = current->reply_size - (ret - sizeof(*reply))))
  240. {
  241. /* couldn't write it all, wait for POLLOUT */
  242. set_fd_events( current->reply_fd, POLLOUT );
  243. set_fd_events( current->request_fd, 0 );
  244. return;
  245. }
  246. }
  247. free( current->reply_data );
  248. current->reply_data = NULL;
  249. return;
  250. error:
  251. if (ret >= 0)
  252. fatal_protocol_error( current, "partial write %d\n", ret );
  253. else if (errno == EPIPE)
  254. kill_thread( current, 0 ); /* normal death */
  255. else
  256. fatal_protocol_error( current, "reply write: %s\n", strerror( errno ));
  257. }
  258. /* call a request handler */
  259. static void call_req_handler( struct thread *thread )
  260. {
  261. union generic_reply reply;
  262. enum request req = thread->req.request_header.req;
  263. current = thread;
  264. current->reply_size = 0;
  265. clear_error();
  266. memset( &reply, 0, sizeof(reply) );
  267. if (debug_level) trace_request();
  268. if (req < REQ_NB_REQUESTS)
  269. req_handlers[req]( &current->req, &reply );
  270. else
  271. set_error( STATUS_NOT_IMPLEMENTED );
  272. if (current)
  273. {
  274. if (current->reply_fd)
  275. {
  276. reply.reply_header.error = current->error;
  277. reply.reply_header.reply_size = current->reply_size;
  278. if (debug_level) trace_reply( req, &reply );
  279. send_reply( &reply );
  280. }
  281. else
  282. {
  283. current->exit_code = 1;
  284. kill_thread( current, 1 ); /* no way to continue without reply fd */
  285. }
  286. }
  287. current = NULL;
  288. }
  289. /* read a request from a thread */
  290. void read_request( struct thread *thread )
  291. {
  292. int ret;
  293. if (!thread->req_toread) /* no pending request */
  294. {
  295. if ((ret = read( get_unix_fd( thread->request_fd ), &thread->req,
  296. sizeof(thread->req) )) != sizeof(thread->req)) goto error;
  297. if (!(thread->req_toread = thread->req.request_header.request_size))
  298. {
  299. /* no data, handle request at once */
  300. call_req_handler( thread );
  301. return;
  302. }
  303. if (!(thread->req_data = malloc( thread->req_toread )))
  304. {
  305. fatal_protocol_error( thread, "no memory for %u bytes request %d\n",
  306. thread->req_toread, thread->req.request_header.req );
  307. return;
  308. }
  309. }
  310. /* read the variable sized data */
  311. for (;;)
  312. {
  313. ret = read( get_unix_fd( thread->request_fd ),
  314. (char *)thread->req_data + thread->req.request_header.request_size
  315. - thread->req_toread,
  316. thread->req_toread );
  317. if (ret <= 0) break;
  318. if (!(thread->req_toread -= ret))
  319. {
  320. call_req_handler( thread );
  321. free( thread->req_data );
  322. thread->req_data = NULL;
  323. return;
  324. }
  325. }
  326. error:
  327. if (!ret) /* closed pipe */
  328. kill_thread( thread, 0 );
  329. else if (ret > 0)
  330. fatal_protocol_error( thread, "partial read %d\n", ret );
  331. else if (errno != EWOULDBLOCK && (EWOULDBLOCK == EAGAIN || errno != EAGAIN))
  332. fatal_protocol_error( thread, "read: %s\n", strerror( errno ));
  333. }
  334. /* receive a file descriptor on the process socket */
  335. int receive_fd( struct process *process )
  336. {
  337. struct iovec vec;
  338. struct send_fd data;
  339. struct msghdr msghdr;
  340. char cmsg_buffer[256];
  341. int fd = -1, ret;
  342. msghdr.msg_name = NULL;
  343. msghdr.msg_namelen = 0;
  344. msghdr.msg_iov = &vec;
  345. msghdr.msg_iovlen = 1;
  346. msghdr.msg_control = cmsg_buffer;
  347. msghdr.msg_controllen = sizeof(cmsg_buffer);
  348. msghdr.msg_flags = 0;
  349. vec.iov_base = (void *)&data;
  350. vec.iov_len = sizeof(data);
  351. ret = recvmsg( get_unix_fd( process->msg_fd ), &msghdr, 0 );
  352. if (ret > 0)
  353. {
  354. struct cmsghdr *cmsg;
  355. for (cmsg = CMSG_FIRSTHDR( &msghdr ); cmsg; cmsg = CMSG_NXTHDR( &msghdr, cmsg ))
  356. {
  357. if (cmsg->cmsg_level != SOL_SOCKET) continue;
  358. if (cmsg->cmsg_type == SCM_RIGHTS) fd = *(int *)CMSG_DATA(cmsg);
  359. }
  360. }
  361. if (ret == sizeof(data))
  362. {
  363. struct thread *thread;
  364. if (data.tid) thread = get_thread_from_id( data.tid );
  365. else thread = (struct thread *)grab_object( get_process_first_thread( process ));
  366. if (!thread || thread->process != process || thread->state == TERMINATED)
  367. {
  368. if (debug_level)
  369. fprintf( stderr, "%04x: *fd* %d <- %d bad thread id\n",
  370. data.tid, data.fd, fd );
  371. close( fd );
  372. }
  373. else
  374. {
  375. if (debug_level)
  376. fprintf( stderr, "%04x: *fd* %d <- %d\n",
  377. thread->id, data.fd, fd );
  378. thread_add_inflight_fd( thread, data.fd, fd );
  379. }
  380. if (thread) release_object( thread );
  381. return 0;
  382. }
  383. if (!ret)
  384. {
  385. kill_process( process, 0 );
  386. }
  387. else if (ret > 0)
  388. {
  389. fprintf( stderr, "Protocol error: process %04x: partial recvmsg %d for fd\n",
  390. process->id, ret );
  391. if (fd != -1) close( fd );
  392. kill_process( process, 1 );
  393. }
  394. else
  395. {
  396. if (errno != EWOULDBLOCK && (EWOULDBLOCK == EAGAIN || errno != EAGAIN))
  397. {
  398. fprintf( stderr, "Protocol error: process %04x: ", process->id );
  399. perror( "recvmsg" );
  400. kill_process( process, 1 );
  401. }
  402. }
  403. return -1;
  404. }
  405. /* send an fd to a client */
  406. int send_client_fd( struct process *process, int fd, obj_handle_t handle )
  407. {
  408. struct iovec vec;
  409. struct msghdr msghdr;
  410. char cmsg_buffer[256];
  411. struct cmsghdr *cmsg;
  412. int ret;
  413. msghdr.msg_name = NULL;
  414. msghdr.msg_namelen = 0;
  415. msghdr.msg_iov = &vec;
  416. msghdr.msg_iovlen = 1;
  417. msghdr.msg_control = cmsg_buffer;
  418. msghdr.msg_controllen = sizeof(cmsg_buffer);
  419. msghdr.msg_flags = 0;
  420. vec.iov_base = (void *)&handle;
  421. vec.iov_len = sizeof(handle);
  422. cmsg = CMSG_FIRSTHDR( &msghdr );
  423. cmsg->cmsg_len = CMSG_LEN( sizeof(fd) );
  424. cmsg->cmsg_level = SOL_SOCKET;
  425. cmsg->cmsg_type = SCM_RIGHTS;
  426. *(int *)CMSG_DATA(cmsg) = fd;
  427. msghdr.msg_controllen = cmsg->cmsg_len;
  428. if (debug_level)
  429. fprintf( stderr, "%04x: *fd* %04x -> %d\n", current ? current->id : process->id, handle, fd );
  430. ret = sendmsg( get_unix_fd( process->msg_fd ), &msghdr, 0 );
  431. if (ret == sizeof(handle)) return 0;
  432. if (ret >= 0)
  433. {
  434. fprintf( stderr, "Protocol error: process %04x: partial sendmsg %d\n", process->id, ret );
  435. kill_process( process, 1 );
  436. }
  437. else if (errno == EPIPE)
  438. {
  439. kill_process( process, 0 );
  440. }
  441. else
  442. {
  443. fprintf( stderr, "Protocol error: process %04x: ", process->id );
  444. perror( "sendmsg" );
  445. kill_process( process, 1 );
  446. }
  447. return -1;
  448. }
  449. /* return a monotonic time counter */
  450. timeout_t monotonic_counter(void)
  451. {
  452. #ifdef __APPLE__
  453. static mach_timebase_info_data_t timebase;
  454. if (!timebase.denom) mach_timebase_info( &timebase );
  455. #ifdef HAVE_MACH_CONTINUOUS_TIME
  456. if (&mach_continuous_time != NULL)
  457. return mach_continuous_time() * timebase.numer / timebase.denom / 100;
  458. #endif
  459. return mach_absolute_time() * timebase.numer / timebase.denom / 100;
  460. #elif defined(HAVE_CLOCK_GETTIME)
  461. struct timespec ts;
  462. #ifdef CLOCK_MONOTONIC_RAW
  463. if (!clock_gettime( CLOCK_MONOTONIC_RAW, &ts ))
  464. return (timeout_t)ts.tv_sec * TICKS_PER_SEC + ts.tv_nsec / 100;
  465. #endif
  466. if (!clock_gettime( CLOCK_MONOTONIC, &ts ))
  467. return (timeout_t)ts.tv_sec * TICKS_PER_SEC + ts.tv_nsec / 100;
  468. #endif
  469. return current_time - server_start_time;
  470. }
  471. static void master_socket_dump( struct object *obj, int verbose )
  472. {
  473. struct master_socket *sock = (struct master_socket *)obj;
  474. assert( obj->ops == &master_socket_ops );
  475. fprintf( stderr, "Master socket fd=%p\n", sock->fd );
  476. }
  477. static void master_socket_destroy( struct object *obj )
  478. {
  479. struct master_socket *sock = (struct master_socket *)obj;
  480. assert( obj->ops == &master_socket_ops );
  481. release_object( sock->fd );
  482. }
  483. /* handle a socket event */
  484. static void master_socket_poll_event( struct fd *fd, int event )
  485. {
  486. struct master_socket *sock = get_fd_user( fd );
  487. assert( master_socket->obj.ops == &master_socket_ops );
  488. assert( sock == master_socket ); /* there is only one master socket */
  489. if (event & (POLLERR | POLLHUP))
  490. {
  491. /* this is not supposed to happen */
  492. fprintf( stderr, "wineserver: Error on master socket\n" );
  493. set_fd_events( sock->fd, -1 );
  494. }
  495. else if (event & POLLIN)
  496. {
  497. struct process *process;
  498. struct sockaddr_un dummy;
  499. socklen_t len = sizeof(dummy);
  500. int client = accept( get_unix_fd( master_socket->fd ), (struct sockaddr *) &dummy, &len );
  501. if (client == -1) return;
  502. fcntl( client, F_SETFL, O_NONBLOCK );
  503. if ((process = create_process( client, NULL, 0, NULL, NULL, NULL, 0, NULL )))
  504. {
  505. create_thread( -1, process, NULL );
  506. release_object( process );
  507. }
  508. }
  509. }
  510. /* remove the socket upon exit */
  511. static void socket_cleanup(void)
  512. {
  513. static int do_it_once;
  514. if (!do_it_once++) unlink( server_socket_name );
  515. }
  516. /* create a directory and check its permissions */
  517. static void create_dir( const char *name, struct stat *st )
  518. {
  519. if (lstat( name, st ) == -1)
  520. {
  521. if (errno != ENOENT)
  522. fatal_error( "lstat %s: %s\n", name, strerror( errno ));
  523. if (mkdir( name, 0700 ) == -1 && errno != EEXIST)
  524. fatal_error( "mkdir %s: %s\n", name, strerror( errno ));
  525. if (lstat( name, st ) == -1)
  526. fatal_error( "lstat %s: %s\n", name, strerror( errno ));
  527. }
  528. if (!S_ISDIR(st->st_mode)) fatal_error( "%s is not a directory\n", name );
  529. if (st->st_uid != getuid()) fatal_error( "%s is not owned by you\n", name );
  530. if (st->st_mode & 077) fatal_error( "%s must not be accessible by other users\n", name );
  531. }
  532. /* create the server directory and chdir to it */
  533. static char *create_server_dir( int force )
  534. {
  535. const char *prefix = getenv( "WINEPREFIX" );
  536. char *p, *config_dir, *base_dir;
  537. struct stat st, st2;
  538. /* open the configuration directory */
  539. if (prefix)
  540. {
  541. if (!(config_dir = strdup( prefix ))) fatal_error( "out of memory\n" );
  542. for (p = config_dir + strlen(config_dir); p > config_dir; p--) if (p[-1] != '/') break;
  543. if (p > config_dir) *p = 0;
  544. if (config_dir[0] != '/')
  545. fatal_error( "invalid directory %s in WINEPREFIX: not an absolute path\n", prefix );
  546. }
  547. else
  548. {
  549. const char *home = getenv( "HOME" );
  550. if (!home)
  551. {
  552. struct passwd *pwd = getpwuid( getuid() );
  553. if (pwd) home = pwd->pw_dir;
  554. }
  555. if (!home) fatal_error( "could not determine your home directory\n" );
  556. if (home[0] != '/') fatal_error( "your home directory %s is not an absolute path\n", home );
  557. if (!(config_dir = malloc( strlen(home) + sizeof("/.wine") ))) fatal_error( "out of memory\n" );
  558. strcpy( config_dir, home );
  559. for (p = config_dir + strlen(config_dir); p > config_dir; p--) if (p[-1] != '/') break;
  560. strcpy( p, "/.wine" );
  561. }
  562. if (chdir( config_dir ) == -1)
  563. {
  564. if (errno != ENOENT || force) fatal_error( "chdir to %s: %s\n", config_dir, strerror( errno ));
  565. return NULL;
  566. }
  567. if ((config_dir_fd = open( ".", O_RDONLY )) == -1)
  568. fatal_error( "open %s: %s\n", config_dir, strerror( errno ));
  569. if (fstat( config_dir_fd, &st ) == -1)
  570. fatal_error( "stat %s: %s\n", config_dir, strerror( errno ));
  571. if (st.st_uid != getuid())
  572. fatal_error( "%s is not owned by you\n", config_dir );
  573. /* create the base directory if needed */
  574. #ifdef __ANDROID__ /* there's no /tmp dir on Android */
  575. if (asprintf( &base_dir, "%s/.wineserver", config_dir ) == -1)
  576. fatal_error( "out of memory\n" );
  577. #else
  578. if (asprintf( &base_dir, "/tmp/.wine-%u", getuid() ) == -1)
  579. fatal_error( "out of memory\n" );
  580. #endif
  581. create_dir( base_dir, &st2 );
  582. /* now create the server directory */
  583. if (asprintf( &server_dir, "%s/server-%llx-%llx", base_dir,
  584. (unsigned long long)st.st_dev, (unsigned long long)st.st_ino ) == -1)
  585. fatal_error( "out of memory\n" );
  586. create_dir( server_dir, &st );
  587. if (chdir( server_dir ) == -1)
  588. fatal_error( "chdir %s: %s\n", server_dir, strerror( errno ));
  589. if ((server_dir_fd = open( ".", O_RDONLY )) == -1)
  590. fatal_error( "open %s: %s\n", server_dir, strerror( errno ));
  591. if (fstat( server_dir_fd, &st2 ) == -1)
  592. fatal_error( "stat %s: %s\n", server_dir, strerror( errno ));
  593. if (st.st_dev != st2.st_dev || st.st_ino != st2.st_ino)
  594. fatal_error( "chdir did not end up in %s\n", server_dir );
  595. free( base_dir );
  596. free( config_dir );
  597. return server_dir;
  598. }
  599. /* create the lock file and return its file descriptor */
  600. static int create_server_lock(void)
  601. {
  602. struct stat st;
  603. int fd;
  604. if (lstat( server_lock_name, &st ) == -1)
  605. {
  606. if (errno != ENOENT)
  607. fatal_error( "lstat %s/%s: %s\n", server_dir, server_lock_name, strerror( errno ));
  608. }
  609. else
  610. {
  611. if (!S_ISREG(st.st_mode))
  612. fatal_error( "%s/%s is not a regular file\n", server_dir, server_lock_name );
  613. }
  614. if ((fd = open( server_lock_name, O_CREAT|O_TRUNC|O_WRONLY, 0600 )) == -1)
  615. fatal_error( "error creating %s/%s: %s\n", server_dir, server_lock_name, strerror( errno ));
  616. return fd;
  617. }
  618. /* wait for the server lock */
  619. int wait_for_lock(void)
  620. {
  621. int fd, r;
  622. struct flock fl;
  623. server_dir = create_server_dir( 0 );
  624. if (!server_dir) return 0; /* no server dir, so no lock to wait on */
  625. fd = create_server_lock();
  626. fl.l_type = F_WRLCK;
  627. fl.l_whence = SEEK_SET;
  628. fl.l_start = 0;
  629. fl.l_len = 1;
  630. r = fcntl( fd, F_SETLKW, &fl );
  631. close(fd);
  632. return r;
  633. }
  634. /* kill the wine server holding the lock */
  635. int kill_lock_owner( int sig )
  636. {
  637. int fd, i, ret = 0;
  638. pid_t pid = 0;
  639. struct flock fl;
  640. server_dir = create_server_dir( 0 );
  641. if (!server_dir) return 0; /* no server dir, nothing to do */
  642. fd = create_server_lock();
  643. for (i = 1; i <= 20; i++)
  644. {
  645. fl.l_type = F_WRLCK;
  646. fl.l_whence = SEEK_SET;
  647. fl.l_start = 0;
  648. fl.l_len = 1;
  649. if (fcntl( fd, F_GETLK, &fl ) == -1) goto done;
  650. if (fl.l_type != F_WRLCK) goto done; /* the file is not locked */
  651. if (!pid) /* first time around */
  652. {
  653. if (!(pid = fl.l_pid)) goto done; /* shouldn't happen */
  654. if (sig == -1)
  655. {
  656. if (kill( pid, SIGINT ) == -1) goto done;
  657. kill( pid, SIGCONT );
  658. ret = 1;
  659. }
  660. else /* just send the specified signal and return */
  661. {
  662. ret = (kill( pid, sig ) != -1);
  663. goto done;
  664. }
  665. }
  666. else if (fl.l_pid != pid) goto done; /* no longer the same process */
  667. usleep( 50000 * i );
  668. }
  669. /* waited long enough, now kill it */
  670. kill( pid, SIGKILL );
  671. done:
  672. close( fd );
  673. return ret;
  674. }
  675. /* acquire the main server lock */
  676. static void acquire_lock(void)
  677. {
  678. struct sockaddr_un addr;
  679. struct stat st;
  680. struct flock fl;
  681. int fd, slen, got_lock = 0;
  682. fd = create_server_lock();
  683. fl.l_type = F_WRLCK;
  684. fl.l_whence = SEEK_SET;
  685. fl.l_start = 0;
  686. fl.l_len = 1;
  687. if (fcntl( fd, F_SETLK, &fl ) != -1)
  688. {
  689. /* check for crashed server */
  690. if (stat( server_socket_name, &st ) != -1 && /* there is a leftover socket */
  691. stat( "core", &st ) != -1 && st.st_size) /* and there is a non-empty core file */
  692. {
  693. fprintf( stderr,
  694. "Warning: a previous instance of the wine server seems to have crashed.\n"
  695. "Please run 'gdb %s %s/core',\n"
  696. "type 'backtrace' at the gdb prompt and report the results. Thanks.\n\n",
  697. server_argv0, server_dir );
  698. }
  699. unlink( server_socket_name ); /* we got the lock, we can safely remove the socket */
  700. got_lock = 1;
  701. /* in that case we reuse fd without closing it, this ensures
  702. * that we hold the lock until the process exits */
  703. }
  704. else
  705. {
  706. switch(errno)
  707. {
  708. case ENOLCK:
  709. break;
  710. case EACCES:
  711. /* check whether locks work at all on this file system */
  712. if (fcntl( fd, F_GETLK, &fl ) == -1) break;
  713. /* fall through */
  714. case EAGAIN:
  715. exit(2); /* we didn't get the lock, exit with special status */
  716. default:
  717. fatal_error( "fcntl %s/%s: %s\n", server_dir, server_lock_name, strerror( errno ));
  718. }
  719. /* it seems we can't use locks on this fs, so we will use the socket existence as lock */
  720. close( fd );
  721. }
  722. if ((fd = socket( AF_UNIX, SOCK_STREAM, 0 )) == -1) fatal_error( "socket: %s\n", strerror( errno ));
  723. addr.sun_family = AF_UNIX;
  724. strcpy( addr.sun_path, server_socket_name );
  725. slen = sizeof(addr) - sizeof(addr.sun_path) + strlen(addr.sun_path) + 1;
  726. #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
  727. addr.sun_len = slen;
  728. #endif
  729. if (bind( fd, (struct sockaddr *)&addr, slen ) == -1)
  730. {
  731. if ((errno == EEXIST) || (errno == EADDRINUSE))
  732. {
  733. if (got_lock)
  734. fatal_error( "couldn't bind to the socket even though we hold the lock\n" );
  735. exit(2); /* we didn't get the lock, exit with special status */
  736. }
  737. fatal_error( "bind: %s\n", strerror( errno ));
  738. }
  739. atexit( socket_cleanup );
  740. chmod( server_socket_name, 0600 ); /* make sure no other user can connect */
  741. if (listen( fd, 5 ) == -1) fatal_error( "listen: %s\n", strerror( errno ));
  742. if (!(master_socket = alloc_object( &master_socket_ops )) ||
  743. !(master_socket->fd = create_anonymous_fd( &master_socket_fd_ops, fd, &master_socket->obj, 0 )))
  744. fatal_error( "out of memory\n" );
  745. set_fd_events( master_socket->fd, POLLIN );
  746. make_object_permanent( &master_socket->obj );
  747. }
  748. /* open the master server socket and start waiting for new clients */
  749. void open_master_socket(void)
  750. {
  751. int fd, pid, status, sync_pipe[2];
  752. char dummy;
  753. /* make sure no request is larger than the maximum size */
  754. assert( sizeof(union generic_request) == sizeof(struct request_max_size) );
  755. assert( sizeof(union generic_reply) == sizeof(struct request_max_size) );
  756. /* make sure the stdio fds are open */
  757. fd = open( "/dev/null", O_RDWR );
  758. while (fd >= 0 && fd <= 2) fd = dup( fd );
  759. server_dir = create_server_dir( 1 );
  760. if (!foreground)
  761. {
  762. if (pipe( sync_pipe ) == -1) fatal_error( "pipe: %s\n", strerror( errno ));
  763. pid = fork();
  764. switch( pid )
  765. {
  766. case 0: /* child */
  767. setsid();
  768. close( sync_pipe[0] );
  769. acquire_lock();
  770. /* close stdin and stdout */
  771. dup2( fd, 0 );
  772. dup2( fd, 1 );
  773. /* signal parent */
  774. dummy = 0;
  775. write( sync_pipe[1], &dummy, 1 );
  776. close( sync_pipe[1] );
  777. break;
  778. case -1:
  779. fatal_error( "fork: %s\n", strerror( errno ));
  780. break;
  781. default: /* parent */
  782. 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);
  785. /* child terminated, propagate exit status */
  786. waitpid( pid, &status, 0 );
  787. if (WIFEXITED(status)) _exit( WEXITSTATUS(status) );
  788. _exit(1);
  789. }
  790. }
  791. else /* remain in the foreground */
  792. {
  793. acquire_lock();
  794. }
  795. /* init the process tracing mechanism */
  796. init_tracing_mechanism();
  797. close( fd );
  798. }
  799. /* master socket timer expiration handler */
  800. static void close_socket_timeout( void *arg )
  801. {
  802. master_timeout = NULL;
  803. flush_registry();
  804. if (debug_level) fprintf( stderr, "wineserver: exiting (pid=%ld)\n", (long) getpid() );
  805. #ifdef DEBUG_OBJECTS
  806. close_objects(); /* shut down everything properly */
  807. #endif
  808. exit( 0 );
  809. }
  810. /* close the master socket and stop waiting for new clients */
  811. void close_master_socket( timeout_t timeout )
  812. {
  813. if (master_socket)
  814. {
  815. release_object( master_socket );
  816. master_socket = NULL;
  817. }
  818. if (master_timeout) /* cancel previous timeout */
  819. remove_timeout_user( master_timeout );
  820. master_timeout = add_timeout_user( timeout, close_socket_timeout, NULL );
  821. }