16 #include "support/lstrings.h"
17 #include "support/lyxlib.h"
19 Systemcalls::Systemcalls() {
20 pid = 0; // No child yet
23 Systemcalls::Systemcalls(Starttype how, string const & what, Callbackfct cback)
28 pid = static_cast<pid_t>(0);
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() {
50 retval = system(command.c_str());
55 if (pid>0) { // Fork succesful. Wait for child
64 // Now integrate into Controller
65 SystemcallsSingletoncontroller::Startcontroller starter;
66 SystemcallsSingletoncontroller * contr = starter.getController();
67 // Add this to controller
68 contr->addCall(*this);
76 void Systemcalls::kill(int /*tolerance*/) {
78 lyxerr << "LyX: Can't kill non-existing process." << endl;
81 int ret = lyx::kill(getpid(), SIGHUP);
82 bool wait_for_death = true;
85 // The process is already dead!
86 wait_for_death = false;
88 // Something is rotten - maybe we lost permissions?
92 // Here, we should add the PID to a list of
93 // waiting processes to kill if they are not
94 // dead without tolerance seconds
96 #warning Implement this using the timer of the singleton systemcontroller (Asger)
102 // Wait for child process to finish. Returns returncode from child.
103 void Systemcalls::waitForChild() {
104 // We'll pretend that the child returns 1 on all errorconditions.
109 pid_t waitrpid = waitpid(pid, &status, WUNTRACED);
110 if (waitrpid == -1) {
111 lyxerr << "LyX: Error waiting for child:" << strerror(errno) << endl;
113 } else if (WIFEXITED(status)) {
114 // Child exited normally. Update return value.
115 retval = WEXITSTATUS(status);
117 } else if (WIFSIGNALED(status)) {
118 lyxerr << "LyX: Child didn't catch signal "
120 << "and died. Too bad." << endl;
122 } else if (WIFSTOPPED(status)) {
123 lyxerr << "LyX: Child (pid: " << pid
124 << ") stopped on signal "
126 << ". Waiting for child to finish." << endl;
128 lyxerr << "LyX: Something rotten happened while "
129 "waiting for child " << pid << endl;
136 // generate child in background
138 pid_t Systemcalls::fork()
140 pid_t cpid= ::fork();
141 if (cpid == 0) { // child
142 string childcommand(command); // copy
143 string rest = split(command, childcommand, ' ');
144 const int MAX_ARGV = 255;
146 char *argv[MAX_ARGV];
151 syscmd = new char[childcommand.length() + 1];
152 childcommand.copy(syscmd, childcommand.length());
153 syscmd[childcommand.length()] = '\0';
155 char * tmp = new char[childcommand.length() + 1];
156 childcommand.copy(tmp, childcommand.length());
157 tmp[childcommand.length()] = '\0';
160 more = !rest.empty();
162 rest = split(rest, childcommand, ' ');
165 // replace by command. Expand using PATH-environment-var.
166 execvp(syscmd, argv);
167 // If something goes wrong, we end up here:
168 lyxerr << "LyX: execvp failed: " << strerror(errno) << endl;
169 } else if (cpid < 0) { // error
170 lyxerr << "LyX: Could not fork: " << strerror(errno) << endl;
180 int Systemcalls::startscript(Starttype how, string const & what,
186 pid = static_cast<pid_t>(0); // yet no child
188 return startscript();
194 // Mini-Test-environment for script-classes
201 void back(string cmd, int retval)
203 printf("Done: %s gave %d\n", cmd.c_str(), retval);
208 int main(int, char**)
211 SystemcallsSingletoncontroller::Startcontroller starter;
212 SystemcallsSingletoncontroller *contr= starter.GetController();
214 Systemcalls one(Systemcalls::System, "ls -ltag", back);
215 Systemcalls two(Systemcalls::Wait, "ls -ltag", back);
217 Systemcalls three(Systemcalls::DontWait , "ls -ltag", back);
218 // Simulation of timer
219 while (SimulateTimer)