bb1c6fe189b5d98ea3d9bb554b16b6370593b494
[kai/samba.git] / source3 / lib / signal.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    signal handling functions
5
6    Copyright (C) Andrew Tridgell 1998
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24
25
26 /****************************************************************************
27 catch child exits
28 ****************************************************************************/
29 static void sig_cld(int signum)
30 {
31         while (sys_waitpid((pid_t)-1,(int *)NULL, WNOHANG) > 0) ;
32
33         CatchSignal(SIGCLD, sig_cld);
34 }
35
36
37
38 /*******************************************************************
39 block sigs
40 ********************************************************************/
41 void BlockSignals(BOOL block,int signum)
42 {
43 #ifdef HAVE_SIGPROCMASK
44         sigset_t set;
45         sigemptyset(&set);
46         sigaddset(&set,signum);
47         sigprocmask(block?SIG_BLOCK:SIG_UNBLOCK,&set,NULL);
48 #elif defined(HAVE_SIGBLOCK)
49         int block_mask = sigmask(signum);
50         static int oldmask = 0;
51         if (block) {
52                 oldmask = sigblock(block_mask);
53         } else {
54                 sigsetmask(oldmask);
55         }
56 #else
57         /* yikes! This platform can't block signals? */
58         static int done;
59         if (!done) {
60                 DEBUG(0,("WARNING: No signal blocking available\n"));
61                 done=1;
62         }
63 #endif
64 }
65
66
67
68 /*******************************************************************
69 catch a signal. This should implement the following semantics:
70
71 1) the handler remains installed after being called
72 2) the signal should be blocked during handler execution
73 ********************************************************************/
74 void CatchSignal(int signum,void (*handler)(int ))
75 {
76 #ifdef HAVE_SIGACTION
77         struct sigaction act;
78
79         ZERO_STRUCT(act);
80
81         act.sa_handler = handler;
82 #ifdef SA_RESTART
83         act.sa_flags = SA_RESTART;
84 #endif
85         sigemptyset(&act.sa_mask);
86         sigaddset(&act.sa_mask,signum);
87         sigaction(signum,&act,NULL);
88 #else
89         /* FIXME: need to handle sigvec and systems with broken signal() */
90         signal(signum, handler);
91 #endif
92 }
93
94
95
96 /*******************************************************************
97 ignore SIGCLD via whatever means is necessary for this OS
98 ********************************************************************/
99 void CatchChild(void)
100 {
101         CatchSignal(SIGCLD, sig_cld);
102 }