]> git.lyx.org Git - lyx.git/blob - src/support/forkedcontr.C
dont use pragma impementation and interface anymore
[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         for (ListType::iterator it = forkedCalls.begin();
87              it != forkedCalls.end(); ++it) {
88                 ForkedProcess * actCall = *it;
89
90                 pid_t pid = actCall->pid();
91                 int stat_loc;
92                 pid_t const waitrpid = waitpid(pid, &stat_loc, WNOHANG);
93                 bool remove_it = false;
94
95                 if (waitrpid == -1) {
96                         lyxerr << "LyX: Error waiting for child: "
97                                << strerror(errno) << endl;
98
99                         // Child died, so pretend it returned 1
100                         actCall->setRetValue(1);
101                         remove_it = true;
102
103                 } else if (waitrpid == 0) {
104                         // Still running. Move on to the next child.
105                         continue;
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                         // Emit signal and remove the item from the list
134                         actCall->emitSignal();
135                         delete actCall;
136                         // erase returns the next iterator, so decrement it
137                         // to continue the loop.
138                         ListType::iterator prev = it;
139                         --prev;
140                         forkedCalls.erase(it);
141                         it = prev;
142                 }
143         }
144
145         if (!forkedCalls.empty()) {
146                 timeout_->start();
147         }
148
149         if (start_size != forkedCalls.size())
150                 childrenChanged();
151 }
152
153
154 // Return a vector of the pids of all the controlled processes.
155 vector<pid_t> const ForkedcallsController::getPIDs() const
156 {
157         vector<pid_t> pids;
158
159         if (forkedCalls.empty())
160                 return pids;
161
162         pids.resize(forkedCalls.size());
163
164         vector<pid_t>::iterator vit = pids.begin();
165         for (ListType::const_iterator lit = forkedCalls.begin();
166              lit != forkedCalls.end(); ++lit, ++vit) {
167                 *vit = (*lit)->pid();
168         }
169
170         std::sort(pids.begin(), pids.end());
171         return pids;
172 }
173
174
175 // Get the command string of the process.
176 string const ForkedcallsController::getCommand(pid_t pid) const
177 {
178         ListType::const_iterator it =
179                 find_if(forkedCalls.begin(), forkedCalls.end(),
180                         lyx::compare_memfun(&Forkedcall::pid, pid));
181
182         if (it == forkedCalls.end())
183                 return string();
184
185         return (*it)->command();
186 }
187
188
189 // Kill the process prematurely and remove it from the list
190 // within tolerance secs
191 void ForkedcallsController::kill(pid_t pid, int tolerance)
192 {
193         ListType::iterator it =
194                 find_if(forkedCalls.begin(), forkedCalls.end(),
195                         lyx::compare_memfun(&Forkedcall::pid, pid));
196
197         if (it == forkedCalls.end())
198                 return;
199
200         (*it)->kill(tolerance);
201         forkedCalls.erase(it);
202
203         if (forkedCalls.empty()) {
204                 timeout_->stop();
205         }
206 }