]> git.lyx.org Git - lyx.git/blob - src/support/forkedcontr.C
Strip bogus continue
[lyx.git] / src / support / forkedcontr.C
1 /**
2  * \file forkedcontr.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Asger Alstrup Nielsen
7  * \author Angus Leeming
8  *
9  * Full author contact details are available in file CREDITS
10  *
11  * A class for the control of child processes launched using
12  * fork() and execvp().
13  */
14
15 #include <config.h>
16
17 #include "forkedcontr.h"
18 #include "forkedcall.h"
19 #include "lyxfunctional.h"
20 #include "debug.h"
21
22 #include "frontends/Timeout.h"
23
24 #include <boost/bind.hpp>
25
26 #include <cerrno>
27 #include <cstdlib>
28 #include <unistd.h>
29 #include <sys/wait.h>
30
31 using std::vector;
32 using std::endl;
33 using std::find_if;
34
35 #ifndef CXX_GLOBAL_CSTD
36 using std::strerror;
37 #endif
38
39 // Ensure, that only one controller exists inside process
40 ForkedcallsController & ForkedcallsController::get()
41 {
42         static ForkedcallsController singleton;
43         return singleton;
44 }
45
46
47 ForkedcallsController::ForkedcallsController()
48 {
49         timeout_ = new Timeout(100, Timeout::ONETIME);
50
51         timeout_->timeout
52                 .connect(boost::bind(&ForkedcallsController::timer, this));
53 }
54
55
56 // open question: should we stop childs here?
57 // Asger says no: I like to have my xdvi open after closing LyX. Maybe
58 // I want to print or something.
59 ForkedcallsController::~ForkedcallsController()
60 {
61         for (ListType::iterator it = forkedCalls.begin();
62              it != forkedCalls.end(); ++it) {
63                 delete *it;
64         }
65
66         delete timeout_;
67 }
68
69
70 void ForkedcallsController::addCall(ForkedProcess const & newcall)
71 {
72         if (!timeout_->running())
73                 timeout_->start();
74
75         forkedCalls.push_back(newcall.clone());
76         childrenChanged();
77 }
78
79
80 // Timer-call
81 // Check the list and, if there is a stopped child, emit the signal.
82 void ForkedcallsController::timer()
83 {
84         ListType::size_type start_size = forkedCalls.size();
85
86         ListType::iterator it  = forkedCalls.begin();
87         ListType::iterator end = forkedCalls.end();
88         while (it != end) {
89                 ForkedProcess * actCall = *it;
90
91                 pid_t pid = actCall->pid();
92                 int stat_loc;
93                 pid_t const waitrpid = waitpid(pid, &stat_loc, WNOHANG);
94                 bool remove_it = false;
95
96                 if (waitrpid == -1) {
97                         lyxerr << "LyX: Error waiting for child: "
98                                << strerror(errno) << endl;
99
100                         // Child died, so pretend it returned 1
101                         actCall->setRetValue(1);
102                         remove_it = true;
103
104                 } else if (waitrpid == 0) {
105                         // Still running. Move on to the next child.
106
107                 } else if (WIFEXITED(stat_loc)) {
108                         // Ok, the return value goes into retval.
109                         actCall->setRetValue(WEXITSTATUS(stat_loc));
110                         remove_it = true;
111
112                 } else if (WIFSIGNALED(stat_loc)) {
113                         // Child died, so pretend it returned 1
114                         actCall->setRetValue(1);
115                         remove_it = true;
116
117                 } else if (WIFSTOPPED(stat_loc)) {
118                         lyxerr << "LyX: Child (pid: " << pid
119                                << ") stopped on signal "
120                                << WSTOPSIG(stat_loc)
121                                << ". Waiting for child to finish." << endl;
122
123                 } else {
124                         lyxerr << "LyX: Something rotten happened while "
125                                 "waiting for child " << pid << endl;
126
127                         // Child died, so pretend it returned 1
128                         actCall->setRetValue(1);
129                         remove_it = true;
130                 }
131
132                 if (remove_it) {
133                         forkedCalls.erase(it);
134
135                         actCall->emitSignal();
136                         delete actCall;
137
138                         /* start all over: emiting the signal can result
139                          * in changing the list (Ab)
140                          */
141                         it = forkedCalls.begin();
142                 } else {
143                         ++it;
144                 }
145         }
146
147         if (!forkedCalls.empty() && !timeout_->running()) {
148                 timeout_->start();
149         }
150
151         if (start_size != forkedCalls.size())
152                 childrenChanged();
153 }
154
155
156 // Return a vector of the pids of all the controlled processes.
157 vector<pid_t> const ForkedcallsController::getPIDs() const
158 {
159         vector<pid_t> pids;
160
161         if (forkedCalls.empty())
162                 return pids;
163
164         pids.resize(forkedCalls.size());
165
166         vector<pid_t>::iterator vit = pids.begin();
167         for (ListType::const_iterator lit = forkedCalls.begin();
168              lit != forkedCalls.end(); ++lit, ++vit) {
169                 *vit = (*lit)->pid();
170         }
171
172         std::sort(pids.begin(), pids.end());
173         return pids;
174 }
175
176
177 // Get the command string of the process.
178 string const ForkedcallsController::getCommand(pid_t pid) const
179 {
180         ListType::const_iterator it =
181                 find_if(forkedCalls.begin(), forkedCalls.end(),
182                         lyx::compare_memfun(&Forkedcall::pid, pid));
183
184         if (it == forkedCalls.end())
185                 return string();
186
187         return (*it)->command();
188 }
189
190
191 // Kill the process prematurely and remove it from the list
192 // within tolerance secs
193 void ForkedcallsController::kill(pid_t pid, int tolerance)
194 {
195         ListType::iterator it =
196                 find_if(forkedCalls.begin(), forkedCalls.end(),
197                         lyx::compare_memfun(&Forkedcall::pid, pid));
198
199         if (it == forkedCalls.end())
200                 return;
201
202         (*it)->kill(tolerance);
203         forkedCalls.erase(it);
204
205         if (forkedCalls.empty()) {
206                 timeout_->stop();
207         }
208 }