| 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 2 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, write to the Free Software | 
|---|
| 19 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | 
|---|
| 20 | */ | 
|---|
| 21 |  | 
|---|
| 22 | #include "includes.h" | 
|---|
| 23 |  | 
|---|
| 24 | /**************************************************************************** | 
|---|
| 25 | Catch child exits and reap the child zombie status. | 
|---|
| 26 | ****************************************************************************/ | 
|---|
| 27 |  | 
|---|
| 28 | static void sig_cld(int signum) | 
|---|
| 29 | { | 
|---|
| 30 | while (sys_waitpid((pid_t)-1,(int *)NULL, WNOHANG) > 0) | 
|---|
| 31 | ; | 
|---|
| 32 |  | 
|---|
| 33 | /* | 
|---|
| 34 | * Turns out it's *really* important not to | 
|---|
| 35 | * restore the signal handler here if we have real POSIX | 
|---|
| 36 | * signal handling. If we do, then we get the signal re-delivered | 
|---|
| 37 | * immediately - hey presto - instant loop ! JRA. | 
|---|
| 38 | */ | 
|---|
| 39 |  | 
|---|
| 40 | #if !defined(HAVE_SIGACTION) | 
|---|
| 41 | CatchSignal(SIGCLD, sig_cld); | 
|---|
| 42 | #endif | 
|---|
| 43 | } | 
|---|
| 44 |  | 
|---|
| 45 | /**************************************************************************** | 
|---|
| 46 | catch child exits - leave status; | 
|---|
| 47 | ****************************************************************************/ | 
|---|
| 48 |  | 
|---|
| 49 | static void sig_cld_leave_status(int signum) | 
|---|
| 50 | { | 
|---|
| 51 | /* | 
|---|
| 52 | * Turns out it's *really* important not to | 
|---|
| 53 | * restore the signal handler here if we have real POSIX | 
|---|
| 54 | * signal handling. If we do, then we get the signal re-delivered | 
|---|
| 55 | * immediately - hey presto - instant loop ! JRA. | 
|---|
| 56 | */ | 
|---|
| 57 |  | 
|---|
| 58 | #if !defined(HAVE_SIGACTION) | 
|---|
| 59 | CatchSignal(SIGCLD, sig_cld_leave_status); | 
|---|
| 60 | #endif | 
|---|
| 61 | } | 
|---|
| 62 |  | 
|---|
| 63 | /******************************************************************* | 
|---|
| 64 | Block sigs. | 
|---|
| 65 | ********************************************************************/ | 
|---|
| 66 |  | 
|---|
| 67 | void BlockSignals(BOOL block,int signum) | 
|---|
| 68 | { | 
|---|
| 69 | #ifdef HAVE_SIGPROCMASK | 
|---|
| 70 | sigset_t set; | 
|---|
| 71 | sigemptyset(&set); | 
|---|
| 72 | sigaddset(&set,signum); | 
|---|
| 73 | sigprocmask(block?SIG_BLOCK:SIG_UNBLOCK,&set,NULL); | 
|---|
| 74 | #elif defined(HAVE_SIGBLOCK) | 
|---|
| 75 | if (block) { | 
|---|
| 76 | sigblock(sigmask(signum)); | 
|---|
| 77 | } else { | 
|---|
| 78 | sigsetmask(siggetmask() & ~sigmask(signum)); | 
|---|
| 79 | } | 
|---|
| 80 | #else | 
|---|
| 81 | /* yikes! This platform can't block signals? */ | 
|---|
| 82 | static int done; | 
|---|
| 83 | if (!done) { | 
|---|
| 84 | DEBUG(0,("WARNING: No signal blocking available\n")); | 
|---|
| 85 | done=1; | 
|---|
| 86 | } | 
|---|
| 87 | #endif | 
|---|
| 88 | } | 
|---|
| 89 |  | 
|---|
| 90 | /******************************************************************* | 
|---|
| 91 | Catch a signal. This should implement the following semantics: | 
|---|
| 92 |  | 
|---|
| 93 | 1) The handler remains installed after being called. | 
|---|
| 94 | 2) The signal should be blocked during handler execution. | 
|---|
| 95 | ********************************************************************/ | 
|---|
| 96 |  | 
|---|
| 97 | void (*CatchSignal(int signum,void (*handler)(int )))(int) | 
|---|
| 98 | { | 
|---|
| 99 | #ifdef HAVE_SIGACTION | 
|---|
| 100 | struct sigaction act; | 
|---|
| 101 | struct sigaction oldact; | 
|---|
| 102 |  | 
|---|
| 103 | ZERO_STRUCT(act); | 
|---|
| 104 |  | 
|---|
| 105 | act.sa_handler = handler; | 
|---|
| 106 | #ifdef SA_RESTART | 
|---|
| 107 | /* | 
|---|
| 108 | * We *want* SIGALRM to interrupt a system call. | 
|---|
| 109 | */ | 
|---|
| 110 | if(signum != SIGALRM) | 
|---|
| 111 | act.sa_flags = SA_RESTART; | 
|---|
| 112 | #endif | 
|---|
| 113 | sigemptyset(&act.sa_mask); | 
|---|
| 114 | sigaddset(&act.sa_mask,signum); | 
|---|
| 115 | sigaction(signum,&act,&oldact); | 
|---|
| 116 | return oldact.sa_handler; | 
|---|
| 117 | #else /* !HAVE_SIGACTION */ | 
|---|
| 118 | /* FIXME: need to handle sigvec and systems with broken signal() */ | 
|---|
| 119 | return signal(signum, handler); | 
|---|
| 120 | #endif | 
|---|
| 121 | } | 
|---|
| 122 |  | 
|---|
| 123 | /******************************************************************* | 
|---|
| 124 | Ignore SIGCLD via whatever means is necessary for this OS. | 
|---|
| 125 | ********************************************************************/ | 
|---|
| 126 |  | 
|---|
| 127 | void CatchChild(void) | 
|---|
| 128 | { | 
|---|
| 129 | CatchSignal(SIGCLD, sig_cld); | 
|---|
| 130 | } | 
|---|
| 131 |  | 
|---|
| 132 | /******************************************************************* | 
|---|
| 133 | Catch SIGCLD but leave the child around so it's status can be reaped. | 
|---|
| 134 | ********************************************************************/ | 
|---|
| 135 |  | 
|---|
| 136 | void CatchChildLeaveStatus(void) | 
|---|
| 137 | { | 
|---|
| 138 | CatchSignal(SIGCLD, sig_cld_leave_status); | 
|---|
| 139 | } | 
|---|