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