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