request.c 30 KB

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