signal.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * Server signal handling
  3. *
  4. * Copyright (C) 2003 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 <signal.h>
  22. #include <stdio.h>
  23. #include <sys/time.h>
  24. #ifdef HAVE_POLL_H
  25. #include <poll.h>
  26. #endif
  27. #ifdef HAVE_SYS_POLL_H
  28. #include <sys/poll.h>
  29. #endif
  30. #ifdef HAVE_SYS_RESOURCE_H
  31. #include <sys/resource.h>
  32. #endif
  33. #include <unistd.h>
  34. #include "file.h"
  35. #include "object.h"
  36. #include "process.h"
  37. #include "thread.h"
  38. #include "request.h"
  39. #if defined(linux) && defined(__SIGRTMIN)
  40. /* the signal used by linuxthreads as exit signal for clone() threads */
  41. # define SIG_PTHREAD_CANCEL (__SIGRTMIN+1)
  42. #endif
  43. typedef void (*signal_callback)(void);
  44. struct handler
  45. {
  46. struct object obj; /* object header */
  47. struct fd *fd; /* file descriptor for the pipe side */
  48. int pipe_write; /* unix fd for the pipe write side */
  49. volatile int pending; /* is signal pending? */
  50. signal_callback callback; /* callback function */
  51. };
  52. static void handler_dump( struct object *obj, int verbose );
  53. static void handler_destroy( struct object *obj );
  54. static const struct object_ops handler_ops =
  55. {
  56. sizeof(struct handler), /* size */
  57. &no_type, /* type */
  58. handler_dump, /* dump */
  59. no_add_queue, /* add_queue */
  60. NULL, /* remove_queue */
  61. NULL, /* signaled */
  62. NULL, /* satisfied */
  63. no_signal, /* signal */
  64. no_get_fd, /* get_fd */
  65. default_map_access, /* map_access */
  66. default_get_sd, /* get_sd */
  67. default_set_sd, /* set_sd */
  68. no_get_full_name, /* get_full_name */
  69. no_lookup_name, /* lookup_name */
  70. no_link_name, /* link_name */
  71. NULL, /* unlink_name */
  72. no_open_file, /* open_file */
  73. no_kernel_obj_list, /* get_kernel_obj_list */
  74. no_close_handle, /* close_handle */
  75. handler_destroy /* destroy */
  76. };
  77. static void handler_poll_event( struct fd *fd, int event );
  78. static const struct fd_ops handler_fd_ops =
  79. {
  80. NULL, /* get_poll_events */
  81. handler_poll_event, /* poll_event */
  82. NULL, /* flush */
  83. NULL, /* get_fd_type */
  84. NULL, /* ioctl */
  85. NULL, /* queue_async */
  86. NULL /* reselect_async */
  87. };
  88. static struct handler *handler_sighup;
  89. static struct handler *handler_sigterm;
  90. static struct handler *handler_sigint;
  91. static struct handler *handler_sigchld;
  92. static struct handler *handler_sigio;
  93. static int watchdog;
  94. /* create a signal handler */
  95. static struct handler *create_handler( signal_callback callback )
  96. {
  97. struct handler *handler;
  98. int fd[2];
  99. if (pipe( fd ) == -1) return NULL;
  100. if (!(handler = alloc_object( &handler_ops )))
  101. {
  102. close( fd[0] );
  103. close( fd[1] );
  104. return NULL;
  105. }
  106. handler->pipe_write = fd[1];
  107. handler->pending = 0;
  108. handler->callback = callback;
  109. if (!(handler->fd = create_anonymous_fd( &handler_fd_ops, fd[0], &handler->obj, 0 )))
  110. {
  111. release_object( handler );
  112. return NULL;
  113. }
  114. set_fd_events( handler->fd, POLLIN );
  115. make_object_permanent( &handler->obj );
  116. return handler;
  117. }
  118. /* handle a signal received for a given handler */
  119. static void do_signal( struct handler *handler )
  120. {
  121. if (!handler->pending)
  122. {
  123. char dummy = 0;
  124. handler->pending = 1;
  125. write( handler->pipe_write, &dummy, 1 );
  126. }
  127. }
  128. static void handler_dump( struct object *obj, int verbose )
  129. {
  130. struct handler *handler = (struct handler *)obj;
  131. fprintf( stderr, "Signal handler fd=%p\n", handler->fd );
  132. }
  133. static void handler_destroy( struct object *obj )
  134. {
  135. struct handler *handler = (struct handler *)obj;
  136. if (handler->fd) release_object( handler->fd );
  137. close( handler->pipe_write );
  138. }
  139. static void handler_poll_event( struct fd *fd, int event )
  140. {
  141. struct handler *handler = get_fd_user( fd );
  142. if (event & (POLLERR | POLLHUP))
  143. {
  144. /* this is not supposed to happen */
  145. fprintf( stderr, "wineserver: Error on signal handler pipe\n" );
  146. release_object( handler );
  147. }
  148. else if (event & POLLIN)
  149. {
  150. char dummy;
  151. handler->pending = 0;
  152. read( get_unix_fd( handler->fd ), &dummy, 1 );
  153. handler->callback();
  154. }
  155. }
  156. /* SIGHUP callback */
  157. static void sighup_callback(void)
  158. {
  159. #ifdef DEBUG_OBJECTS
  160. dump_objects();
  161. #endif
  162. }
  163. /* SIGTERM callback */
  164. static void sigterm_callback(void)
  165. {
  166. flush_registry();
  167. exit(1);
  168. }
  169. /* SIGINT callback */
  170. static void sigint_callback(void)
  171. {
  172. shutdown_master_socket();
  173. }
  174. /* SIGHUP handler */
  175. static void do_sighup( int signum )
  176. {
  177. do_signal( handler_sighup );
  178. }
  179. /* SIGTERM handler */
  180. static void do_sigterm( int signum )
  181. {
  182. do_signal( handler_sigterm );
  183. }
  184. /* SIGINT handler */
  185. static void do_sigint( int signum )
  186. {
  187. do_signal( handler_sigint );
  188. }
  189. /* SIGALRM handler */
  190. static void do_sigalrm( int signum )
  191. {
  192. watchdog = 1;
  193. }
  194. /* SIGCHLD handler */
  195. static void do_sigchld( int signum )
  196. {
  197. do_signal( handler_sigchld );
  198. }
  199. /* SIGSEGV handler */
  200. static void do_sigsegv( int signum )
  201. {
  202. fprintf( stderr, "wineserver crashed, please enable coredumps (ulimit -c unlimited) and restart.\n");
  203. abort();
  204. }
  205. /* SIGIO handler */
  206. #ifdef HAVE_SIGINFO_T_SI_FD
  207. static void do_sigio( int signum, siginfo_t *si, void *x )
  208. {
  209. do_signal( handler_sigio );
  210. do_change_notify( si->si_fd );
  211. }
  212. #endif
  213. void start_watchdog(void)
  214. {
  215. alarm( 3 );
  216. watchdog = 0;
  217. }
  218. void stop_watchdog(void)
  219. {
  220. alarm( 0 );
  221. watchdog = 0;
  222. }
  223. int watchdog_triggered(void)
  224. {
  225. return watchdog != 0;
  226. }
  227. static int core_dump_disabled( void )
  228. {
  229. int r = 0;
  230. #ifdef RLIMIT_CORE
  231. struct rlimit lim;
  232. r = !getrlimit(RLIMIT_CORE, &lim) && (lim.rlim_cur == 0);
  233. #endif
  234. return r;
  235. }
  236. void init_signals(void)
  237. {
  238. struct sigaction action;
  239. sigset_t blocked_sigset;
  240. if (!(handler_sighup = create_handler( sighup_callback ))) goto error;
  241. if (!(handler_sigterm = create_handler( sigterm_callback ))) goto error;
  242. if (!(handler_sigint = create_handler( sigint_callback ))) goto error;
  243. if (!(handler_sigchld = create_handler( sigchld_callback ))) goto error;
  244. if (!(handler_sigio = create_handler( sigio_callback ))) goto error;
  245. sigemptyset( &blocked_sigset );
  246. sigaddset( &blocked_sigset, SIGCHLD );
  247. sigaddset( &blocked_sigset, SIGHUP );
  248. sigaddset( &blocked_sigset, SIGINT );
  249. sigaddset( &blocked_sigset, SIGALRM );
  250. sigaddset( &blocked_sigset, SIGIO );
  251. sigaddset( &blocked_sigset, SIGQUIT );
  252. sigaddset( &blocked_sigset, SIGTERM );
  253. #ifdef SIG_PTHREAD_CANCEL
  254. sigaddset( &blocked_sigset, SIG_PTHREAD_CANCEL );
  255. #endif
  256. action.sa_mask = blocked_sigset;
  257. action.sa_flags = 0;
  258. action.sa_handler = do_sigchld;
  259. sigaction( SIGCHLD, &action, NULL );
  260. #ifdef SIG_PTHREAD_CANCEL
  261. sigaction( SIG_PTHREAD_CANCEL, &action, NULL );
  262. #endif
  263. action.sa_handler = do_sighup;
  264. sigaction( SIGHUP, &action, NULL );
  265. action.sa_handler = do_sigint;
  266. sigaction( SIGINT, &action, NULL );
  267. action.sa_handler = do_sigalrm;
  268. sigaction( SIGALRM, &action, NULL );
  269. action.sa_handler = do_sigterm;
  270. sigaction( SIGQUIT, &action, NULL );
  271. sigaction( SIGTERM, &action, NULL );
  272. if (core_dump_disabled())
  273. {
  274. action.sa_handler = do_sigsegv;
  275. sigaction( SIGSEGV, &action, NULL );
  276. }
  277. action.sa_handler = SIG_IGN;
  278. sigaction( SIGXFSZ, &action, NULL );
  279. #ifdef HAVE_SIGINFO_T_SI_FD
  280. action.sa_sigaction = do_sigio;
  281. action.sa_flags = SA_SIGINFO;
  282. sigaction( SIGIO, &action, NULL );
  283. #endif
  284. return;
  285. error:
  286. fprintf( stderr, "failed to initialize signal handlers\n" );
  287. exit(1);
  288. }