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