]> git.ipfire.org Git - thirdparty/squid.git/blame - src/ipc/Kid.cc
Docs: Copyright updates for 2018 (#114)
[thirdparty/squid.git] / src / ipc / Kid.cc
CommitLineData
40daaeb8 1/*
5b74111a 2 * Copyright (C) 1996-2018 The Squid Software Foundation and contributors
40daaeb8 3 *
bbc27441
AJ
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.
40daaeb8
AR
7 */
8
bbc27441
AJ
9/* DEBUG: section 54 Interprocess Communication */
10
f7f3304a 11#include "squid.h"
ca4b9ee6 12#include "globals.h"
40daaeb8 13#include "ipc/Kid.h"
00e2479d 14#include "SquidConfig.h"
40daaeb8 15
074d6a40 16#include <ctime>
541b39f3
AJ
17#if HAVE_SYS_WAIT_H
18#include <sys/wait.h>
19#endif
20
095ec2b1
AR
21int TheProcessKind = pkOther;
22
40daaeb8 23Kid::Kid():
f53969cc
SM
24 badFailures(0),
25 pid(-1),
26 startTime(0),
27 isRunning(false),
28 status(0)
40daaeb8
AR
29{
30}
31
32Kid::Kid(const String& kid_name):
f53969cc
SM
33 theName(kid_name),
34 badFailures(0),
35 pid(-1),
36 startTime(0),
37 isRunning(false),
38 status(0)
40daaeb8
AR
39{
40}
41
42/// called when this kid got started, records PID
43void Kid::start(pid_t cpid)
44{
45 assert(!running());
46 assert(cpid > 0);
47
48 isRunning = true;
00e2479d 49 stopTime = 0;
40daaeb8 50 pid = cpid;
00e2479d 51 startTime = squid_curtime;
40daaeb8
AR
52}
53
54/// called when kid terminates, sets exiting status
dbf55289
CT
55void
56Kid::stop(PidStatus const theExitStatus)
40daaeb8
AR
57{
58 assert(running());
59 assert(startTime != 0);
60
61 isRunning = false;
00e2479d
AR
62 stopTime = squid_curtime;
63 status = theExitStatus;
40daaeb8 64
00e2479d 65 if ((stopTime - startTime) < fastFailureTimeLimit)
d7ae3534 66 ++badFailures;
40daaeb8
AR
67 else
68 badFailures = 0; // the failures are not "frequent" [any more]
69
00e2479d
AR
70 reportStopped(); // after all state changes
71}
72
73/// describes a recently stopped kid
74void
75Kid::reportStopped() const
76{
77 if (calledExit()) {
78 syslog(LOG_NOTICE,
79 "Squid Parent: %s process %d exited with status %d",
80 theName.termedBuf(), pid, exitStatus());
81 } else if (signaled()) {
82 syslog(LOG_NOTICE,
83 "Squid Parent: %s process %d exited due to signal %d with status %d",
84 theName.termedBuf(), pid, termSignal(), exitStatus());
85 } else {
86 syslog(LOG_NOTICE, "Squid Parent: %s process %d exited",
87 theName.termedBuf(), pid);
88 }
89
90 if (hopeless() && Config.hopelessKidRevivalDelay) {
91 syslog(LOG_NOTICE, "Squid Parent: %s process %d will not be restarted for %ld "
92 "seconds due to repeated, frequent failures",
93 theName.termedBuf(), pid, Config.hopelessKidRevivalDelay);
94 }
40daaeb8
AR
95}
96
97/// returns true if tracking of kid is stopped
98bool Kid::running() const
99{
100 return isRunning;
101}
102
ca4b9ee6
DK
103/// returns true if master process should restart this kid
104bool Kid::shouldRestart() const
105{
106 return !(running() ||
107 exitedHappy() ||
108 hopeless() ||
109 shutting_down ||
110 signaled(SIGKILL) || // squid -k kill
111 signaled(SIGINT) || // unexpected forced shutdown
112 signaled(SIGTERM)); // unexpected forced shutdown
113}
114
40daaeb8
AR
115/// returns current pid for a running kid and last pid for a stopped kid
116pid_t Kid::getPid() const
117{
118 assert(pid > 0);
119 return pid;
120}
121
122/// whether the failures are "repeated and frequent"
123bool Kid::hopeless() const
124{
125 return badFailures > badFailureLimit;
126}
127
128/// returns true if the process terminated normally
129bool Kid::calledExit() const
130{
131 return (pid > 0) && !running() && WIFEXITED(status);
132}
133
134/// returns the exit status of the process
135int Kid::exitStatus() const
136{
137 return WEXITSTATUS(status);
138}
139
140/// whether the process exited with a given exit status code
141bool Kid::calledExit(int code) const
142{
143 return calledExit() && (exitStatus() == code);
144}
145
146/// whether the process exited with code 0
147bool Kid::exitedHappy() const
148{
149 return calledExit(0);
150}
151
152/// returns true if the kid was terminated by a signal
153bool Kid::signaled() const
154{
155 return (pid > 0) && !running() && WIFSIGNALED(status);
156}
157
158/// returns the number of the signal that caused the kid to terminate
159int Kid::termSignal() const
160{
161 return WTERMSIG(status);
162}
163
164/// whether the process was terminated by a given signal
165bool Kid::signaled(int sgnl) const
166{
167 return signaled() && (termSignal() == sgnl);
168}
169
170/// returns kid name
171const String& Kid::name() const
172{
173 return theName;
174}
f53969cc 175
00e2479d
AR
176time_t
177Kid::deathDuration() const
178{
179 return squid_curtime > stopTime ? squid_curtime - stopTime : 0;
180}
181