Kid.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2017 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 #include "SquidConfig.h"
15 
16 #include <ctime>
17 #if HAVE_SYS_WAIT_H
18 #include <sys/wait.h>
19 #endif
20 
22 
24  badFailures(0),
25  pid(-1),
26  startTime(0),
27  isRunning(false),
28  status(0)
29 {
30 }
31 
32 Kid::Kid(const String& kid_name):
33  theName(kid_name),
34  badFailures(0),
35  pid(-1),
36  startTime(0),
37  isRunning(false),
38  status(0)
39 {
40 }
41 
43 void Kid::start(pid_t cpid)
44 {
45  assert(!running());
46  assert(cpid > 0);
47 
48  isRunning = true;
49  stopTime = 0;
50  pid = cpid;
52 }
53 
55 void
56 Kid::stop(PidStatus const theExitStatus)
57 {
58  assert(running());
59  assert(startTime != 0);
60 
61  isRunning = false;
63  status = theExitStatus;
64 
66  ++badFailures;
67  else
68  badFailures = 0; // the failures are not "frequent" [any more]
69 
70  reportStopped(); // after all state changes
71 }
72 
74 void
76 {
77  if (calledExit()) {
78  syslog(LOG_NOTICE,
79  "Squid Parent: %s process %d exited with status %d",
81  } else if (signaled()) {
82  syslog(LOG_NOTICE,
83  "Squid Parent: %s process %d exited due to signal %d with status %d",
85  } else {
86  syslog(LOG_NOTICE, "Squid Parent: %s process %d exited",
87  theName.termedBuf(), pid);
88  }
89 
91  syslog(LOG_NOTICE, "Squid Parent: %s process %d will not be restarted for %ld "
92  "seconds due to repeated, frequent failures",
94  }
95 }
96 
98 bool Kid::running() const
99 {
100  return isRunning;
101 }
102 
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 
116 pid_t Kid::getPid() const
117 {
118  assert(pid > 0);
119  return pid;
120 }
121 
123 bool Kid::hopeless() const
124 {
125  return badFailures > badFailureLimit;
126 }
127 
129 bool Kid::calledExit() const
130 {
131  return (pid > 0) && !running() && WIFEXITED(status);
132 }
133 
135 int Kid::exitStatus() const
136 {
137  return WEXITSTATUS(status);
138 }
139 
141 bool Kid::calledExit(int code) const
142 {
143  return calledExit() && (exitStatus() == code);
144 }
145 
147 bool Kid::exitedHappy() const
148 {
149  return calledExit(0);
150 }
151 
153 bool Kid::signaled() const
154 {
155  return (pid > 0) && !running() && WIFSIGNALED(status);
156 }
157 
159 int Kid::termSignal() const
160 {
161  return WTERMSIG(status);
162 }
163 
165 bool Kid::signaled(int sgnl) const
166 {
167  return signaled() && (termSignal() == sgnl);
168 }
169 
171 const String& Kid::name() const
172 {
173  return theName;
174 }
175 
176 time_t
178 {
179  return squid_curtime > stopTime ? squid_curtime - stopTime : 0;
180 }
181 
bool calledExit() const
returns true if the process terminated normally
Definition: Kid.cc:129
int PidStatus
Definition: tools.h:94
#define assert(EX)
Definition: assert.h:17
void reportStopped() const
describes a recently stopped kid
Definition: Kid.cc:75
void stop(PidStatus const exitStatus)
called when kid terminates, sets exiting status
Definition: Kid.cc:56
pid_t pid
current (for a running kid) or last (for stopped kid) PID
Definition: Kid.h:88
const String & name() const
returns kid name
Definition: Kid.cc:171
String theName
process name
Definition: Kid.h:84
PidStatus status
exit status of a stopped kid
Definition: Kid.h:92
time_t squid_curtime
Definition: stub_time.cc:17
bool hopeless() const
whether the failures are "repeated and frequent"
Definition: Kid.cc:123
int termSignal() const
returns the number of the signal that caused the kid to terminate
Definition: Kid.cc:159
int shutting_down
Definition: testAddress.cc:36
char const * termedBuf() const
Definition: SquidString.h:90
pid_t getPid() const
returns current pid for a running kid and last pid for a stopped kid
Definition: Kid.cc:116
void start(pid_t cpid)
called when this kid got started, records PID
Definition: Kid.cc:43
bool running() const
returns true if tracking of kid is stopped
Definition: Kid.cc:98
unsigned char code
Definition: html_quote.c:20
time_t deathDuration() const
Definition: Kid.cc:177
we do not know or do not care
Definition: Kid.h:99
bool shouldRestart() const
returns true if master should restart this kid
Definition: Kid.cc:104
static pid_t pid
Definition: IcmpSquid.cc:35
Kid()
Definition: Kid.cc:23
time_t hopelessKidRevivalDelay
hopeless_kid_revival_delay
Definition: SquidConfig.h:99
int TheProcessKind
ProcessKind for the current process.
Definition: Kid.cc:21
bool isRunning
whether the kid is assumed to be alive
Definition: Kid.h:91
bool signaled() const
returns true if the kid was terminated by a signal
Definition: Kid.cc:153
bool exitedHappy() const
whether the process exited with code 0
Definition: Kid.cc:147
time_t startTime
last start time
Definition: Kid.h:89
class SquidConfig Config
Definition: SquidConfig.cc:12
time_t stopTime
last termination time
Definition: Kid.h:90
int badFailures
number of "repeated frequent" failures
Definition: Kid.h:85
#define false
Definition: GnuRegex.c:233
int exitStatus() const
returns the exit status of the process
Definition: Kid.cc:135

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors