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