Registry server LDB backend: Don't make copies of the same type
[tprouty/samba.git] / source3 / lib / signal.c
1 /* 
2    Unix SMB/CIFS implementation.
3    signal handling functions
4
5    Copyright (C) Andrew Tridgell 1998
6    
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.
11    
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.
16    
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/>.
19 */
20
21 #include "includes.h"
22
23 /****************************************************************************
24  Catch child exits and reap the child zombie status.
25 ****************************************************************************/
26
27 static void sig_cld(int signum)
28 {
29         while (sys_waitpid((pid_t)-1,(int *)NULL, WNOHANG) > 0)
30                 ;
31
32         /*
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.
37          */
38
39 #if !defined(HAVE_SIGACTION)
40         CatchSignal(SIGCLD, sig_cld);
41 #endif
42 }
43
44 /****************************************************************************
45 catch child exits - leave status;
46 ****************************************************************************/
47
48 static void sig_cld_leave_status(int signum)
49 {
50         /*
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.
55          */
56
57 #if !defined(HAVE_SIGACTION)
58         CatchSignal(SIGCLD, sig_cld_leave_status);
59 #endif
60 }
61
62 /*******************************************************************
63  Block sigs.
64 ********************************************************************/
65
66 void BlockSignals(bool block,int signum)
67 {
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));
78         }
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;
85         }
86 #endif
87 }
88
89 /*******************************************************************
90  Catch a signal. This should implement the following semantics:
91
92  1) The handler remains installed after being called.
93  2) The signal should be blocked during handler execution.
94 ********************************************************************/
95
96 void (*CatchSignal(int signum,void (*handler)(int )))(int)
97 {
98 #ifdef HAVE_SIGACTION
99         struct sigaction act;
100         struct sigaction oldact;
101
102         ZERO_STRUCT(act);
103
104         act.sa_handler = handler;
105 #ifdef SA_RESTART
106         /*
107          * We *want* SIGALRM to interrupt a system call.
108          */
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
120 }
121
122 /*******************************************************************
123  Ignore SIGCLD via whatever means is necessary for this OS.
124 ********************************************************************/
125
126 void CatchChild(void)
127 {
128         CatchSignal(SIGCLD, sig_cld);
129 }
130
131 /*******************************************************************
132  Catch SIGCLD but leave the child around so it's status can be reaped.
133 ********************************************************************/
134
135 void CatchChildLeaveStatus(void)
136 {
137         CatchSignal(SIGCLD, sig_cld_leave_status);
138 }