signal.c 8.5 KB

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