signals.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #define INCL_DOSEXCEPTIONS /* for OS2 */
  17. #include "fspr_arch_threadproc.h"
  18. #include "fspr_private.h"
  19. #include "fspr_pools.h"
  20. #include "fspr_signal.h"
  21. #include "fspr_strings.h"
  22. #include <assert.h>
  23. #if APR_HAS_THREADS && APR_HAVE_PTHREAD_H
  24. #include <pthread.h>
  25. #endif
  26. #ifdef SIGWAIT_TAKES_ONE_ARG
  27. #define fspr_sigwait(a,b) ((*(b)=sigwait((a)))<0?-1:0)
  28. #else
  29. #define fspr_sigwait(a,b) sigwait((a),(b))
  30. #endif
  31. APR_DECLARE(fspr_status_t) fspr_proc_kill(fspr_proc_t *proc, int signum)
  32. {
  33. #ifdef OS2
  34. /* SIGTERM's don't work too well in OS/2 (only affects other EMX
  35. * programs). CGIs may not be, esp. REXX scripts, so use a native
  36. * call instead
  37. */
  38. if (signum == SIGTERM) {
  39. return APR_OS2_STATUS(DosSendSignalException(proc->pid,
  40. XCPT_SIGNAL_BREAK));
  41. }
  42. #endif /* OS2 */
  43. if (kill(proc->pid, signum) == -1) {
  44. return errno;
  45. }
  46. return APR_SUCCESS;
  47. }
  48. #if APR_HAVE_SIGACTION
  49. #if defined(__NetBSD__) || defined(DARWIN)
  50. static void avoid_zombies(int signo)
  51. {
  52. int exit_status;
  53. while (waitpid(-1, &exit_status, WNOHANG) > 0) {
  54. /* do nothing */
  55. }
  56. }
  57. #endif /* DARWIN */
  58. /*
  59. * Replace standard signal() with the more reliable sigaction equivalent
  60. * from W. Richard Stevens' "Advanced Programming in the UNIX Environment"
  61. * (the version that does not automatically restart system calls).
  62. */
  63. APR_DECLARE(fspr_sigfunc_t *) fspr_signal(int signo, fspr_sigfunc_t * func)
  64. {
  65. struct sigaction act, oact;
  66. act.sa_handler = func;
  67. sigemptyset(&act.sa_mask);
  68. act.sa_flags = 0;
  69. #ifdef SA_INTERRUPT /* SunOS */
  70. act.sa_flags |= SA_INTERRUPT;
  71. #endif
  72. #if defined(__osf__) && defined(__alpha)
  73. /* XXX jeff thinks this should be enabled whenever SA_NOCLDWAIT is defined */
  74. /* this is required on Tru64 to cause child processes to
  75. * disappear gracefully - XPG4 compatible
  76. */
  77. if ((signo == SIGCHLD) && (func == SIG_IGN)) {
  78. act.sa_flags |= SA_NOCLDWAIT;
  79. }
  80. #endif
  81. #if defined(__NetBSD__) || defined(DARWIN)
  82. /* ignoring SIGCHLD or leaving the default disposition doesn't avoid zombies,
  83. * and there is no SA_NOCLDWAIT flag, so catch the signal and reap status in
  84. * the handler to avoid zombies
  85. */
  86. if ((signo == SIGCHLD) && (func == SIG_IGN)) {
  87. act.sa_handler = avoid_zombies;
  88. }
  89. #endif
  90. if (sigaction(signo, &act, &oact) < 0)
  91. return SIG_ERR;
  92. return oact.sa_handler;
  93. }
  94. #endif /* HAVE_SIGACTION */
  95. /* AC_DECL_SYS_SIGLIST defines either of these symbols depending
  96. * on the version of autoconf used. */
  97. #if defined(SYS_SIGLIST_DECLARED) || HAVE_DECL_SYS_SIGLIST
  98. void fspr_signal_init(fspr_pool_t *pglobal)
  99. {
  100. }
  101. const char *fspr_signal_description_get(int signum)
  102. {
  103. return sys_siglist[signum];
  104. }
  105. #else /* !(SYS_SIGLIST_DECLARED || HAVE_DECL_SYS_SIGLIST) */
  106. /* we need to roll our own signal description stuff */
  107. #if defined(NSIG)
  108. #define APR_NUMSIG NSIG
  109. #elif defined(_NSIG)
  110. #define APR_NUMSIG _NSIG
  111. #elif defined(__NSIG)
  112. #define APR_NUMSIG __NSIG
  113. #else
  114. #define APR_NUMSIG 33 /* breaks on OS/390 with < 33; 32 is o.k. for most */
  115. #endif
  116. static const char *signal_description[APR_NUMSIG];
  117. #define store_desc(index, string) \
  118. do { \
  119. if (index >= APR_NUMSIG) { \
  120. assert(index < APR_NUMSIG); \
  121. } \
  122. else { \
  123. signal_description[index] = string; \
  124. } \
  125. } while (0)
  126. void fspr_signal_init(fspr_pool_t *pglobal)
  127. {
  128. int sig;
  129. store_desc(0, "Signal 0");
  130. #ifdef SIGHUP
  131. store_desc(SIGHUP, "Hangup");
  132. #endif
  133. #ifdef SIGINT
  134. store_desc(SIGINT, "Interrupt");
  135. #endif
  136. #ifdef SIGQUIT
  137. store_desc(SIGQUIT, "Quit");
  138. #endif
  139. #ifdef SIGILL
  140. store_desc(SIGILL, "Illegal instruction");
  141. #endif
  142. #ifdef SIGTRAP
  143. store_desc(SIGTRAP, "Trace/BPT trap");
  144. #endif
  145. #ifdef SIGIOT
  146. store_desc(SIGIOT, "IOT instruction");
  147. #endif
  148. #ifdef SIGABRT
  149. store_desc(SIGABRT, "Abort");
  150. #endif
  151. #ifdef SIGEMT
  152. store_desc(SIGEMT, "Emulator trap");
  153. #endif
  154. #ifdef SIGFPE
  155. store_desc(SIGFPE, "Arithmetic exception");
  156. #endif
  157. #ifdef SIGKILL
  158. store_desc(SIGKILL, "Killed");
  159. #endif
  160. #ifdef SIGBUS
  161. store_desc(SIGBUS, "Bus error");
  162. #endif
  163. #ifdef SIGSEGV
  164. store_desc(SIGSEGV, "Segmentation fault");
  165. #endif
  166. #ifdef SIGSYS
  167. store_desc(SIGSYS, "Bad system call");
  168. #endif
  169. #ifdef SIGPIPE
  170. store_desc(SIGPIPE, "Broken pipe");
  171. #endif
  172. #ifdef SIGALRM
  173. store_desc(SIGALRM, "Alarm clock");
  174. #endif
  175. #ifdef SIGTERM
  176. store_desc(SIGTERM, "Terminated");
  177. #endif
  178. #ifdef SIGUSR1
  179. store_desc(SIGUSR1, "User defined signal 1");
  180. #endif
  181. #ifdef SIGUSR2
  182. store_desc(SIGUSR2, "User defined signal 2");
  183. #endif
  184. #ifdef SIGCLD
  185. store_desc(SIGCLD, "Child status change");
  186. #endif
  187. #ifdef SIGCHLD
  188. store_desc(SIGCHLD, "Child status change");
  189. #endif
  190. #ifdef SIGPWR
  191. store_desc(SIGPWR, "Power-fail restart");
  192. #endif
  193. #ifdef SIGWINCH
  194. store_desc(SIGWINCH, "Window changed");
  195. #endif
  196. #ifdef SIGURG
  197. store_desc(SIGURG, "urgent socket condition");
  198. #endif
  199. #ifdef SIGPOLL
  200. store_desc(SIGPOLL, "Pollable event occurred");
  201. #endif
  202. #ifdef SIGIO
  203. store_desc(SIGIO, "socket I/O possible");
  204. #endif
  205. #ifdef SIGSTOP
  206. store_desc(SIGSTOP, "Stopped (signal)");
  207. #endif
  208. #ifdef SIGTSTP
  209. store_desc(SIGTSTP, "Stopped");
  210. #endif
  211. #ifdef SIGCONT
  212. store_desc(SIGCONT, "Continued");
  213. #endif
  214. #ifdef SIGTTIN
  215. store_desc(SIGTTIN, "Stopped (tty input)");
  216. #endif
  217. #ifdef SIGTTOU
  218. store_desc(SIGTTOU, "Stopped (tty output)");
  219. #endif
  220. #ifdef SIGVTALRM
  221. store_desc(SIGVTALRM, "virtual timer expired");
  222. #endif
  223. #ifdef SIGPROF
  224. store_desc(SIGPROF, "profiling timer expired");
  225. #endif
  226. #ifdef SIGXCPU
  227. store_desc(SIGXCPU, "exceeded cpu limit");
  228. #endif
  229. #ifdef SIGXFSZ
  230. store_desc(SIGXFSZ, "exceeded file size limit");
  231. #endif
  232. for (sig = 0; sig < APR_NUMSIG; ++sig)
  233. if (signal_description[sig] == NULL)
  234. signal_description[sig] = fspr_psprintf(pglobal, "signal #%d", sig);
  235. }
  236. const char *fspr_signal_description_get(int signum)
  237. {
  238. return
  239. signum < APR_NUMSIG
  240. ? signal_description[signum]
  241. : "unknown signal (number)";
  242. }
  243. #endif /* SYS_SIGLIST_DECLARED || HAVE_DECL_SYS_SIGLIST */
  244. #if APR_HAS_THREADS && (HAVE_SIGSUSPEND || APR_HAVE_SIGWAIT) && !defined(OS2)
  245. static void remove_sync_sigs(sigset_t *sig_mask)
  246. {
  247. #ifdef SIGABRT
  248. sigdelset(sig_mask, SIGABRT);
  249. #endif
  250. #ifdef SIGBUS
  251. sigdelset(sig_mask, SIGBUS);
  252. #endif
  253. #ifdef SIGEMT
  254. sigdelset(sig_mask, SIGEMT);
  255. #endif
  256. #ifdef SIGFPE
  257. sigdelset(sig_mask, SIGFPE);
  258. #endif
  259. #ifdef SIGILL
  260. sigdelset(sig_mask, SIGILL);
  261. #endif
  262. #ifdef SIGIOT
  263. sigdelset(sig_mask, SIGIOT);
  264. #endif
  265. #ifdef SIGPIPE
  266. sigdelset(sig_mask, SIGPIPE);
  267. #endif
  268. #ifdef SIGSEGV
  269. sigdelset(sig_mask, SIGSEGV);
  270. #endif
  271. #ifdef SIGSYS
  272. sigdelset(sig_mask, SIGSYS);
  273. #endif
  274. #ifdef SIGTRAP
  275. sigdelset(sig_mask, SIGTRAP);
  276. #endif
  277. /* the rest of the signals removed from the mask in this function
  278. * absolutely must be removed; you cannot block synchronous signals
  279. * (requirement of pthreads API)
  280. *
  281. * SIGUSR2 is being removed from the mask for the convenience of
  282. * Purify users (Solaris, HP-UX, SGI) since Purify uses SIGUSR2
  283. */
  284. #ifdef SIGUSR2
  285. sigdelset(sig_mask, SIGUSR2);
  286. #endif
  287. }
  288. APR_DECLARE(fspr_status_t) fspr_signal_thread(int(*signal_handler)(int signum))
  289. {
  290. sigset_t sig_mask;
  291. #if APR_HAVE_SIGWAIT
  292. int (*sig_func)(int signum) = (int (*)(int))signal_handler;
  293. #endif
  294. /* This thread will be the one responsible for handling signals */
  295. sigfillset(&sig_mask);
  296. /* On certain platforms, sigwait() returns EINVAL if any of various
  297. * unblockable signals are included in the mask. This was first
  298. * observed on AIX and Tru64.
  299. */
  300. #ifdef SIGKILL
  301. sigdelset(&sig_mask, SIGKILL);
  302. #endif
  303. #ifdef SIGSTOP
  304. sigdelset(&sig_mask, SIGSTOP);
  305. #endif
  306. #ifdef SIGCONT
  307. sigdelset(&sig_mask, SIGCONT);
  308. #endif
  309. #ifdef SIGWAITING
  310. sigdelset(&sig_mask, SIGWAITING);
  311. #endif
  312. /* no synchronous signals should be in the mask passed to sigwait() */
  313. remove_sync_sigs(&sig_mask);
  314. /* On AIX (4.3.3, at least), sigwait() won't wake up if the high-
  315. * order bit of the second word of flags is turned on. sigdelset()
  316. * returns an error when trying to turn this off, so we'll turn it
  317. * off manually.
  318. *
  319. * Note that the private fields differ between 32-bit and 64-bit
  320. * and even between _ALL_SOURCE and !_ALL_SOURCE. Except that on
  321. * AIX 4.3 32-bit builds and 64-bit builds use the same definition.
  322. *
  323. * Applicable AIX fixes such that this is no longer needed:
  324. *
  325. * APAR IY23096 for AIX 51B, fix included in AIX 51C, and
  326. * APAR IY24162 for 43X.
  327. */
  328. #if defined(_AIX)
  329. #if defined(__64BIT__) && defined(_AIXVERSION_510)
  330. #ifdef _ALL_SOURCE
  331. sig_mask.ss_set[3] &= 0x7FFFFFFF;
  332. #else /* not _ALL_SOURCE */
  333. sig_mask.__ss_set[3] &= 0x7FFFFFFF;
  334. #endif
  335. #else /* not 64-bit build, or 64-bit build on 4.3 */
  336. #ifdef _ALL_SOURCE
  337. sig_mask.hisigs &= 0x7FFFFFFF;
  338. #else /* not _ALL_SOURCE */
  339. sig_mask.__hisigs &= 0x7FFFFFFF;
  340. #endif
  341. #endif
  342. #endif /* _AIX */
  343. while (1) {
  344. #if APR_HAVE_SIGWAIT
  345. int signal_received;
  346. if (fspr_sigwait(&sig_mask, &signal_received) != 0)
  347. {
  348. /* handle sigwait() error here */
  349. }
  350. if (sig_func(signal_received) == 1) {
  351. return APR_SUCCESS;
  352. }
  353. #elif HAVE_SIGSUSPEND
  354. sigsuspend(&sig_mask);
  355. #else
  356. #error No fspr_sigwait() and no sigsuspend()
  357. #endif
  358. }
  359. }
  360. APR_DECLARE(fspr_status_t) fspr_setup_signal_thread(void)
  361. {
  362. sigset_t sig_mask;
  363. int rv;
  364. /* All threads should mask out signals to be handled by
  365. * the thread doing sigwait().
  366. *
  367. * No thread should ever block synchronous signals.
  368. * See the Solaris man page for pthread_sigmask() for
  369. * some information. Solaris chooses to knock out such
  370. * processes when a blocked synchronous signal is
  371. * delivered, skipping any registered signal handler.
  372. * AIX doesn't call a signal handler either. At least
  373. * one level of linux+glibc does call the handler even
  374. * when the synchronous signal is blocked.
  375. */
  376. sigfillset(&sig_mask);
  377. remove_sync_sigs(&sig_mask);
  378. #if defined(SIGPROCMASK_SETS_THREAD_MASK) || ! APR_HAS_THREADS
  379. if ((rv = sigprocmask(SIG_SETMASK, &sig_mask, NULL)) != 0) {
  380. rv = errno;
  381. }
  382. #else
  383. if ((rv = pthread_sigmask(SIG_SETMASK, &sig_mask, NULL)) != 0) {
  384. #ifdef PTHREAD_SETS_ERRNO
  385. rv = errno;
  386. #endif
  387. }
  388. #endif
  389. return rv;
  390. }
  391. #endif /* APR_HAS_THREADS && ... */
  392. APR_DECLARE(fspr_status_t) fspr_signal_block(int signum)
  393. {
  394. #if APR_HAVE_SIGACTION
  395. sigset_t sig_mask;
  396. int rv;
  397. sigemptyset(&sig_mask);
  398. sigaddset(&sig_mask, signum);
  399. #if defined(SIGPROCMASK_SETS_THREAD_MASK) || ! APR_HAS_THREADS
  400. if ((rv = sigprocmask(SIG_BLOCK, &sig_mask, NULL)) != 0) {
  401. rv = errno;
  402. }
  403. #else
  404. if ((rv = pthread_sigmask(SIG_BLOCK, &sig_mask, NULL)) != 0) {
  405. #ifdef PTHREAD_SETS_ERRNO
  406. rv = errno;
  407. #endif
  408. }
  409. #endif
  410. return rv;
  411. #else
  412. return APR_ENOTIMPL;
  413. #endif
  414. }
  415. APR_DECLARE(fspr_status_t) fspr_signal_unblock(int signum)
  416. {
  417. #if APR_HAVE_SIGACTION
  418. sigset_t sig_mask;
  419. int rv;
  420. sigemptyset(&sig_mask);
  421. sigaddset(&sig_mask, signum);
  422. #if defined(SIGPROCMASK_SETS_THREAD_MASK) || ! APR_HAS_THREADS
  423. if ((rv = sigprocmask(SIG_UNBLOCK, &sig_mask, NULL)) != 0) {
  424. rv = errno;
  425. }
  426. #else
  427. if ((rv = pthread_sigmask(SIG_UNBLOCK, &sig_mask, NULL)) != 0) {
  428. #ifdef PTHREAD_SETS_ERRNO
  429. rv = errno;
  430. #endif
  431. }
  432. #endif
  433. return rv;
  434. #else
  435. return APR_ENOTIMPL;
  436. #endif
  437. }