16 #include "support/lstrings.h"
17 #include "support/lyxlib.h"
18 #include "support/filetools.h"
19 #include "support/os.h"
24 Systemcalls::Systemcalls() {
25 pid = 0; // No child yet
28 Systemcalls::Systemcalls(Starttype how, string const & what, Callbackfct cback)
30 startscript(how, what, cback);
33 Systemcalls::~Systemcalls() {
35 // If the child is alive, we have to brutally kill it
37 lyx::kill(getpid(), SIGKILL);
42 // Start a childprocess
44 // if child runs in background, add information to global controller.
46 int Systemcalls::startscript() {
51 retval = ::system(command.c_str());
56 if (pid>0) { // Fork succesful. Wait for child
65 // Now integrate into Controller
66 SystemcallsSingletoncontroller::Startcontroller starter;
67 SystemcallsSingletoncontroller * contr = starter.getController();
68 // Add this to controller
69 contr->addCall(*this);
77 void Systemcalls::kill(int /*tolerance*/) {
79 lyxerr << "LyX: Can't kill non-existing process." << endl;
82 int ret = lyx::kill(getpid(), SIGHUP);
83 bool wait_for_death = true;
86 // The process is already dead!
87 wait_for_death = false;
89 // Something is rotten - maybe we lost permissions?
93 // Here, we should add the PID to a list of
94 // waiting processes to kill if they are not
95 // dead within tolerance seconds
97 // CHECK Implement this using the timer of
98 // the singleton systemcontroller (Asger)
104 // Wait for child process to finish. Returns returncode from child.
105 void Systemcalls::waitForChild() {
106 // We'll pretend that the child returns 1 on all errorconditions.
111 pid_t waitrpid = waitpid(pid, &status, WUNTRACED);
112 if (waitrpid == -1) {
113 lyxerr << "LyX: Error waiting for child:" << strerror(errno) << endl;
115 } else if (WIFEXITED(status)) {
116 // Child exited normally. Update return value.
117 retval = WEXITSTATUS(status);
119 } else if (WIFSIGNALED(status)) {
120 lyxerr << "LyX: Child didn't catch signal "
122 << "and died. Too bad." << endl;
124 } else if (WIFSTOPPED(status)) {
125 lyxerr << "LyX: Child (pid: " << pid
126 << ") stopped on signal "
128 << ". Waiting for child to finish." << endl;
130 lyxerr << "LyX: Something rotten happened while "
131 "waiting for child " << pid << endl;
138 // generate child in background
140 pid_t Systemcalls::fork()
143 pid_t cpid= ::fork();
144 if (cpid == 0) { // child
146 // TODO: Consider doing all of this before the fork, otherwise me
147 // might have troubles with multi-threaded access. (Baruch 20010228)
148 string childcommand(command); // copy
149 string rest = split(command, childcommand, ' ');
150 const int MAX_ARGV = 255;
152 char *argv[MAX_ARGV];
156 childcommand = frontStrip(childcommand);
158 syscmd = new char[childcommand.length() + 1];
159 childcommand.copy(syscmd, childcommand.length());
160 syscmd[childcommand.length()] = '\0';
162 if (!childcommand.empty()) {
163 char * tmp = new char[childcommand.length() + 1];
164 childcommand.copy(tmp, childcommand.length());
165 tmp[childcommand.length()] = '\0';
170 more = !rest.empty();
172 rest = split(rest, childcommand, ' ');
175 // replace by command. Expand using PATH-environment-var.
177 execvp(syscmd, argv);
178 // If something goes wrong, we end up here:
179 lyxerr << "LyX: execvp failed: " << strerror(errno) << endl;
180 } else if (cpid < 0) { // error
182 pid_t cpid = spawnvp(P_SESSION|P_DEFAULT|P_MINIMIZE|P_BACKGROUND, syscmd, argv);
183 if (cpid < 0) { // error
185 lyxerr << "LyX: Could not fork: " << strerror(errno) << endl;
195 int Systemcalls::startscript(Starttype how, string const & what,
201 pid = static_cast<pid_t>(0); // yet no child
204 if (how == SystemDontWait) {
205 (os::shell() == os::UNIX) ? command += " &"
206 : command = "start /min/n " + command;
209 return startscript();
215 // Mini-Test-environment for script-classes
222 void back(string cmd, int retval)
224 ::printf("Done: %s gave %d\n", cmd.c_str(), retval);
229 int main(int, char **)
232 SystemcallsSingletoncontroller::Startcontroller starter;
233 SystemcallsSingletoncontroller *contr= starter.GetController();
235 Systemcalls one(Systemcalls::System, "ls -ltag", back);
236 Systemcalls two(Systemcalls::Wait, "ls -ltag", back);
238 Systemcalls three(Systemcalls::DontWait , "ls -ltag", back);
239 // Simulation of timer
240 while (SimulateTimer)