]>
Commit | Line | Data |
---|---|---|
40daaeb8 | 1 | /* |
bbc27441 | 2 | * Copyright (C) 1996-2014 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(): |
5667a628 AR |
23 | badFailures(0), |
24 | pid(-1), | |
25 | startTime(0), | |
fad2588a FC |
26 | isRunning(false), |
27 | status(0) | |
40daaeb8 AR |
28 | { |
29 | } | |
30 | ||
31 | Kid::Kid(const String& kid_name): | |
5667a628 AR |
32 | theName(kid_name), |
33 | badFailures(0), | |
34 | pid(-1), | |
35 | startTime(0), | |
fad2588a FC |
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 | |
9dca980d | 53 | void Kid::stop(status_type theExitStatus) |
40daaeb8 AR |
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) | |
d7ae3534 | 63 | ++badFailures; |
40daaeb8 AR |
64 | else |
65 | badFailures = 0; // the failures are not "frequent" [any more] | |
66 | ||
9dca980d | 67 | status = theExitStatus; |
40daaeb8 AR |
68 | } |
69 | ||
70 | /// returns true if tracking of kid is stopped | |
71 | bool Kid::running() const | |
72 | { | |
73 | return isRunning; | |
74 | } | |
75 | ||
ca4b9ee6 DK |
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 | ||
40daaeb8 AR |
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 | } |