]>
Commit | Line | Data |
---|---|---|
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 |
21 | int TheProcessKind = pkOther; |
22 | ||
40daaeb8 | 23 | Kid::Kid(): |
f53969cc SM |
24 | badFailures(0), |
25 | pid(-1), | |
26 | startTime(0), | |
27 | isRunning(false), | |
28 | status(0) | |
40daaeb8 AR |
29 | { |
30 | } | |
31 | ||
32 | Kid::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 | |
43 | void 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 |
55 | void |
56 | Kid::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 | |
74 | void | |
75 | Kid::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 | |
98 | bool Kid::running() const | |
99 | { | |
100 | return isRunning; | |
101 | } | |
102 | ||
ca4b9ee6 DK |
103 | /// returns true if master process should restart this kid |
104 | bool 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 |
116 | pid_t Kid::getPid() const | |
117 | { | |
118 | assert(pid > 0); | |
119 | return pid; | |
120 | } | |
121 | ||
122 | /// whether the failures are "repeated and frequent" | |
123 | bool Kid::hopeless() const | |
124 | { | |
125 | return badFailures > badFailureLimit; | |
126 | } | |
127 | ||
128 | /// returns true if the process terminated normally | |
129 | bool Kid::calledExit() const | |
130 | { | |
131 | return (pid > 0) && !running() && WIFEXITED(status); | |
132 | } | |
133 | ||
134 | /// returns the exit status of the process | |
135 | int Kid::exitStatus() const | |
136 | { | |
137 | return WEXITSTATUS(status); | |
138 | } | |
139 | ||
140 | /// whether the process exited with a given exit status code | |
141 | bool Kid::calledExit(int code) const | |
142 | { | |
143 | return calledExit() && (exitStatus() == code); | |
144 | } | |
145 | ||
146 | /// whether the process exited with code 0 | |
147 | bool Kid::exitedHappy() const | |
148 | { | |
149 | return calledExit(0); | |
150 | } | |
151 | ||
152 | /// returns true if the kid was terminated by a signal | |
153 | bool 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 | |
159 | int Kid::termSignal() const | |
160 | { | |
161 | return WTERMSIG(status); | |
162 | } | |
163 | ||
164 | /// whether the process was terminated by a given signal | |
165 | bool Kid::signaled(int sgnl) const | |
166 | { | |
167 | return signaled() && (termSignal() == sgnl); | |
168 | } | |
169 | ||
170 | /// returns kid name | |
171 | const String& Kid::name() const | |
172 | { | |
173 | return theName; | |
174 | } | |
f53969cc | 175 | |
00e2479d AR |
176 | time_t |
177 | Kid::deathDuration() const | |
178 | { | |
179 | return squid_curtime > stopTime ? squid_curtime - stopTime : 0; | |
180 | } | |
181 |