Add comment explaining the previous fix.
[Samba.git] / source / lib / signal.c
blob4b1c95eb77b35ad3bf6bfc40550c8600bab37a23
1 /*
2 Unix SMB/CIFS implementation.
3 signal handling functions
5 Copyright (C) Andrew Tridgell 1998
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
23 /****************************************************************************
24 Catch child exits and reap the child zombie status.
25 ****************************************************************************/
27 static void sig_cld(int signum)
29 while (sys_waitpid((pid_t)-1,(int *)NULL, WNOHANG) > 0)
33 * Turns out it's *really* important not to
34 * restore the signal handler here if we have real POSIX
35 * signal handling. If we do, then we get the signal re-delivered
36 * immediately - hey presto - instant loop ! JRA.
39 #if !defined(HAVE_SIGACTION)
40 CatchSignal(SIGCLD, sig_cld);
41 #endif
44 /****************************************************************************
45 catch child exits - leave status;
46 ****************************************************************************/
48 static void sig_cld_leave_status(int signum)
51 * Turns out it's *really* important not to
52 * restore the signal handler here if we have real POSIX
53 * signal handling. If we do, then we get the signal re-delivered
54 * immediately - hey presto - instant loop ! JRA.
57 #if !defined(HAVE_SIGACTION)
58 CatchSignal(SIGCLD, sig_cld_leave_status);
59 #endif
62 /*******************************************************************
63 Block sigs.
64 ********************************************************************/
66 void BlockSignals(bool block,int signum)
68 #ifdef HAVE_SIGPROCMASK
69 sigset_t set;
70 sigemptyset(&set);
71 sigaddset(&set,signum);
72 sigprocmask(block?SIG_BLOCK:SIG_UNBLOCK,&set,NULL);
73 #elif defined(HAVE_SIGBLOCK)
74 if (block) {
75 sigblock(sigmask(signum));
76 } else {
77 sigsetmask(siggetmask() & ~sigmask(signum));
79 #else
80 /* yikes! This platform can't block signals? */
81 static int done;
82 if (!done) {
83 DEBUG(0,("WARNING: No signal blocking available\n"));
84 done=1;
86 #endif
89 /*******************************************************************
90 Catch a signal. This should implement the following semantics:
92 1) The handler remains installed after being called.
93 2) The signal should be blocked during handler execution.
94 ********************************************************************/
96 void (*CatchSignal(int signum,void (*handler)(int )))(int)
98 #ifdef HAVE_SIGACTION
99 struct sigaction act;
100 struct sigaction oldact;
102 ZERO_STRUCT(act);
104 act.sa_handler = handler;
105 #ifdef SA_RESTART
107 * We *want* SIGALRM to interrupt a system call.
109 if(signum != SIGALRM)
110 act.sa_flags = SA_RESTART;
111 #endif
112 sigemptyset(&act.sa_mask);
113 sigaddset(&act.sa_mask,signum);
114 sigaction(signum,&act,&oldact);
115 return oldact.sa_handler;
116 #else /* !HAVE_SIGACTION */
117 /* FIXME: need to handle sigvec and systems with broken signal() */
118 return signal(signum, handler);
119 #endif
122 /*******************************************************************
123 Ignore SIGCLD via whatever means is necessary for this OS.
124 ********************************************************************/
126 void CatchChild(void)
128 CatchSignal(SIGCLD, sig_cld);
131 /*******************************************************************
132 Catch SIGCLD but leave the child around so it's status can be reaped.
133 ********************************************************************/
135 void CatchChildLeaveStatus(void)
137 CatchSignal(SIGCLD, sig_cld_leave_status);