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