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