]> git.ipfire.org Git - thirdparty/squid.git/blob - src/ipc/Kid.cc
If a worker process crashes during shutdown, dump core and prevent restarts.
[thirdparty/squid.git] / src / ipc / Kid.cc
1 /*
2 * $Id$
3 *
4 * DEBUG: section 54 Interprocess Communication
5 *
6 */
7
8 #include "config.h"
9 #include "globals.h"
10 #include "ipc/Kid.h"
11
12 #if HAVE_SYS_WAIT_H
13 #include <sys/wait.h>
14 #endif
15
16 Kid::Kid():
17 badFailures(0),
18 pid(-1),
19 startTime(0),
20 isRunning(false)
21 {
22 }
23
24 Kid::Kid(const String& kid_name):
25 theName(kid_name),
26 badFailures(0),
27 pid(-1),
28 startTime(0),
29 isRunning(false)
30 {
31 }
32
33 /// called when this kid got started, records PID
34 void Kid::start(pid_t cpid)
35 {
36 assert(!running());
37 assert(cpid > 0);
38
39 isRunning = true;
40 pid = cpid;
41 time(&startTime);
42 }
43
44 /// called when kid terminates, sets exiting status
45 void Kid::stop(status_type exitStatus)
46 {
47 assert(running());
48 assert(startTime != 0);
49
50 isRunning = false;
51
52 time_t stop_time;
53 time(&stop_time);
54 if ((stop_time - startTime) < fastFailureTimeLimit)
55 badFailures++;
56 else
57 badFailures = 0; // the failures are not "frequent" [any more]
58
59 status = exitStatus;
60 }
61
62 /// returns true if tracking of kid is stopped
63 bool Kid::running() const
64 {
65 return isRunning;
66 }
67
68 /// returns true if master process should restart this kid
69 bool Kid::shouldRestart() const
70 {
71 return !(running() ||
72 exitedHappy() ||
73 hopeless() ||
74 shutting_down ||
75 signaled(SIGKILL) || // squid -k kill
76 signaled(SIGINT) || // unexpected forced shutdown
77 signaled(SIGTERM)); // unexpected forced shutdown
78 }
79
80 /// returns current pid for a running kid and last pid for a stopped kid
81 pid_t Kid::getPid() const
82 {
83 assert(pid > 0);
84 return pid;
85 }
86
87 /// whether the failures are "repeated and frequent"
88 bool Kid::hopeless() const
89 {
90 return badFailures > badFailureLimit;
91 }
92
93 /// returns true if the process terminated normally
94 bool Kid::calledExit() const
95 {
96 return (pid > 0) && !running() && WIFEXITED(status);
97 }
98
99 /// returns the exit status of the process
100 int Kid::exitStatus() const
101 {
102 return WEXITSTATUS(status);
103 }
104
105 /// whether the process exited with a given exit status code
106 bool Kid::calledExit(int code) const
107 {
108 return calledExit() && (exitStatus() == code);
109 }
110
111 /// whether the process exited with code 0
112 bool Kid::exitedHappy() const
113 {
114 return calledExit(0);
115 }
116
117 /// returns true if the kid was terminated by a signal
118 bool Kid::signaled() const
119 {
120 return (pid > 0) && !running() && WIFSIGNALED(status);
121 }
122
123 /// returns the number of the signal that caused the kid to terminate
124 int Kid::termSignal() const
125 {
126 return WTERMSIG(status);
127 }
128
129 /// whether the process was terminated by a given signal
130 bool Kid::signaled(int sgnl) const
131 {
132 return signaled() && (termSignal() == sgnl);
133 }
134
135 /// returns kid name
136 const String& Kid::name() const
137 {
138 return theName;
139 }