]> git.ipfire.org Git - thirdparty/squid.git/blame - tools/purge/signal.cc
Source Format Enforcement (#963)
[thirdparty/squid.git] / tools / purge / signal.cc
CommitLineData
5f623035 1/*
bf95c10a 2 * Copyright (C) 1996-2022 The Squid Software Foundation and contributors
5f623035
AJ
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
0b96a9b3 9// Author: Jens-S. V?ckler <voeckler@rvs.uni-hannover.de>
eb1f6bfa
AJ
10// File: signal.cc
11// Date: Sat Feb 28 1998
12// Compiler: gcc 2.7.2.x series
feec68a0 13//
eb1f6bfa
AJ
14// Books: W. Richard Steven, "Advanced Programming in the UNIX Environment",
15// Addison-Wesley, 1992.
feec68a0 16//
eb1f6bfa 17// (c) 1998 Lehrgebiet Rechnernetze und Verteilte Systeme
0b96a9b3 18// Universit?t Hannover, Germany
eb1f6bfa
AJ
19//
20// Permission to use, copy, modify, distribute, and sell this software
21// and its documentation for any purpose is hereby granted without fee,
22// provided that (i) the above copyright notices and this permission
23// notice appear in all copies of the software and related documentation,
24// and (ii) the names of the Lehrgebiet Rechnernetze und Verteilte
25// Systeme and the University of Hannover may not be used in any
26// advertising or publicity relating to the software without the
27// specific, prior written permission of Lehrgebiet Rechnernetze und
28// Verteilte Systeme and the University of Hannover.
29//
30// THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
31// EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
32// WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
33//
34// IN NO EVENT SHALL THE LEHRGEBIET RECHNERNETZE UND VERTEILTE SYSTEME OR
35// THE UNIVERSITY OF HANNOVER BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
36// INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES
37// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT
38// ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY,
39// ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
40// SOFTWARE.
41//
eb1f6bfa
AJ
42// Revision 1.3 1999/01/19 13:11:52 cached
43// adaptations necessary for AIX.
44//
45// Revision 1.2 1999/01/19 11:00:50 voeckler
46// added psignal(int,const char*) compatibility function.
47//
48// Revision 1.1 1998/08/13 21:51:58 voeckler
49// Initial revision
50//
5f623035 51
f7f3304a 52#include "squid.h"
2ccf2eb2
AJ
53#include "signal.hh"
54
074d6a40
AJ
55#include <cerrno>
56#include <cstring>
eb1f6bfa 57#include <memory.h>
eb1f6bfa
AJ
58#include <unistd.h>
59#include <sys/wait.h>
eb1f6bfa
AJ
60
61SigFunc*
62Signal( int signo, SigFunc* newhandler, bool doInterrupt )
feec68a0
A
63// purpose: install reliable signals
64// paramtr: signo (IN): signal for which a handler is to be installed
65// newhandler (IN): function pointer to the signal handler
66// doInterrupt (IN): interrupted system calls wanted!
67// returns: the old signal handler, or SIG_ERR in case of error.
eb1f6bfa 68{
feec68a0 69 struct sigaction action, old;
eb1f6bfa 70
feec68a0
A
71 memset( &old, 0, sizeof(old) );
72 memset( &action, 0, sizeof(action) );
eb1f6bfa 73
feec68a0
A
74 // action.sa_handler = newhandler; I HATE TYPE-OVERCORRECTNESS !
75 memmove( &action.sa_handler, &newhandler, sizeof(SigFunc*) );
76 sigemptyset( &action.sa_mask );
eb1f6bfa 77
feec68a0
A
78 if ( signo == SIGCHLD ) {
79 action.sa_flags |= SA_NOCLDSTOP;
eb1f6bfa
AJ
80
81#ifdef SA_NODEFER
feec68a0 82 action.sa_flags |= SA_NODEFER; // SYSV: don't block current signal
eb1f6bfa 83#endif
feec68a0 84 }
eb1f6bfa 85
feec68a0 86 if ( signo == SIGALRM || doInterrupt ) {
eb1f6bfa 87#ifdef SA_INTERRUPT
feec68a0 88 action.sa_flags |= SA_INTERRUPT; // SunOS, obsoleted by POSIX
eb1f6bfa 89#endif
feec68a0 90 } else {
eb1f6bfa 91#ifdef SA_RESTART
feec68a0 92 action.sa_flags |= SA_RESTART; // BSD, SVR4
eb1f6bfa 93#endif
feec68a0 94 }
eb1f6bfa 95
feec68a0
A
96 return ( sigaction( signo, &action, &old ) < 0 ) ?
97 (SigFunc*) SIG_ERR :
98 (SigFunc*) old.sa_handler;
eb1f6bfa
AJ
99}
100
53521734 101void
eb1f6bfa 102sigChild( int signo )
feec68a0
A
103// purpose: supply ad hoc child handler with output on stderr
104// paramtr: signo (IN): == SIGCHLD
105// returns: only if OS uses a return type for signal handler
106// seealso: Stevens, UNP, figure 5.11 *and* Stevens, APUE, figure 8.3
eb1f6bfa 107{
feec68a0
A
108 pid_t pid;
109 int status = signo; // to stop GNU from complaining...
feec68a0
A
110
111 int saveerr = errno;
112 while ( (pid = waitpid( -1, &status, WNOHANG )) > 0 ) {
113 if ( WIFEXITED(status) ) {
ec3c3187 114 fprintf( stderr, "child (pid=%ld) reaped, status %d\n%c",
8978bd9d 115 (long) pid, WEXITSTATUS(status), 0 );
feec68a0 116 } else if ( WIFSIGNALED(status) ) {
ec3c3187 117 fprintf( stderr, "child (pid=%ld) died on signal %d%s\n%c",
8978bd9d 118 (long) pid, WTERMSIG(status),
eb1f6bfa 119#ifdef WCOREDUMP
8978bd9d 120 WCOREDUMP(status) ? " (core generated)" : "",
eb1f6bfa 121#else
8978bd9d 122 "",
eb1f6bfa 123#endif
8978bd9d 124 0 );
feec68a0 125 } else {
ec3c3187 126 fprintf( stderr, "detected dead child (pid=%ld), status %d\n%c",
8978bd9d 127 (long) pid, status, 0 );
feec68a0 128 }
eb1f6bfa 129 }
feec68a0 130 errno = saveerr;
eb1f6bfa 131}
f53969cc 132