signal.c 8.4 KB

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