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/>.
22 #include "system/wait.h"
24 #include "lib/util/signal.h" /* Avoid /usr/include/signal.h */
28 * @brief Signal handling
31 /****************************************************************************
32 Catch child exits and reap the child zombie status.
33 ****************************************************************************/
35 static void sig_cld(int signum
)
37 while (waitpid((pid_t
)-1,(int *)NULL
, WNOHANG
) > 0)
41 * Turns out it's *really* important not to
42 * restore the signal handler here if we have real POSIX
43 * signal handling. If we do, then we get the signal re-delivered
44 * immediately - hey presto - instant loop ! JRA.
47 #if !defined(HAVE_SIGACTION)
48 CatchSignal(SIGCLD
, sig_cld
);
52 /****************************************************************************
53 catch child exits - leave status;
54 ****************************************************************************/
56 static void sig_cld_leave_status(int signum
)
59 * Turns out it's *really* important not to
60 * restore the signal handler here if we have real POSIX
61 * signal handling. If we do, then we get the signal re-delivered
62 * immediately - hey presto - instant loop ! JRA.
65 #if !defined(HAVE_SIGACTION)
66 CatchSignal(SIGCLD
, sig_cld_leave_status
);
74 void BlockSignals(bool block
, int signum
)
76 #ifdef HAVE_SIGPROCMASK
79 sigaddset(&set
,signum
);
80 sigprocmask(block
?SIG_BLOCK
:SIG_UNBLOCK
,&set
,NULL
);
81 #elif defined(HAVE_SIGBLOCK)
83 sigblock(sigmask(signum
));
85 sigsetmask(siggetmask() & ~sigmask(signum
));
88 /* yikes! This platform can't block signals? */
91 DEBUG(0,("WARNING: No signal blocking available\n"));
98 Catch a signal. This should implement the following semantics:
100 1) The handler remains installed after being called.
101 2) The signal should be blocked during handler execution.
104 void (*CatchSignal(int signum
,void (*handler
)(int )))(int)
106 #ifdef HAVE_SIGACTION
107 struct sigaction act
;
108 struct sigaction oldact
;
112 act
.sa_handler
= handler
;
115 * We *want* SIGALRM to interrupt a system call.
117 if(signum
!= SIGALRM
)
118 act
.sa_flags
= SA_RESTART
;
120 sigemptyset(&act
.sa_mask
);
121 sigaddset(&act
.sa_mask
,signum
);
122 sigaction(signum
,&act
,&oldact
);
123 return oldact
.sa_handler
;
124 #else /* !HAVE_SIGACTION */
125 /* FIXME: need to handle sigvec and systems with broken signal() */
126 return signal(signum
, handler
);
131 Ignore SIGCLD via whatever means is necessary for this OS.
134 void (*CatchChild(void))(int)
136 return CatchSignal(SIGCLD
, sig_cld
);
140 Catch SIGCLD but leave the child around so it's status can be reaped.
143 void (*CatchChildLeaveStatus(void))(int)
145 return CatchSignal(SIGCLD
, sig_cld_leave_status
);