request.c 30 KB

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