16 #include "support/lstrings.h"
17 #include "support/lyxlib.h"
22 Systemcalls::Systemcalls() {
23 pid = 0; // No child yet
26 Systemcalls::Systemcalls(Starttype how, string const & what, Callbackfct cback)
31 pid = static_cast<pid_t>(0);
36 Systemcalls::~Systemcalls() {
38 // If the child is alive, we have to brutally kill it
40 lyx::kill(getpid(), SIGKILL);
45 // Start a childprocess
47 // if child runs in background, add information to global controller.
49 int Systemcalls::startscript() {
53 retval = system(command.c_str());
58 if (pid>0) { // Fork succesful. Wait for child
67 // Now integrate into Controller
68 SystemcallsSingletoncontroller::Startcontroller starter;
69 SystemcallsSingletoncontroller * contr = starter.getController();
70 // Add this to controller
71 contr->addCall(*this);
79 void Systemcalls::kill(int /*tolerance*/) {
81 lyxerr << "LyX: Can't kill non-existing process." << endl;
84 int ret = lyx::kill(getpid(), SIGHUP);
85 bool wait_for_death = true;
88 // The process is already dead!
89 wait_for_death = false;
91 // Something is rotten - maybe we lost permissions?
95 // Here, we should add the PID to a list of
96 // waiting processes to kill if they are not
97 // dead without tolerance seconds
99 #warning Implement this using the timer of the singleton systemcontroller (Asger)
105 // Wait for child process to finish. Returns returncode from child.
106 void Systemcalls::waitForChild() {
107 // We'll pretend that the child returns 1 on all errorconditions.
112 pid_t waitrpid = waitpid(pid, &status, WUNTRACED);
113 if (waitrpid == -1) {
114 lyxerr << "LyX: Error waiting for child:" << strerror(errno) << endl;
116 } else if (WIFEXITED(status)) {
117 // Child exited normally. Update return value.
118 retval = WEXITSTATUS(status);
120 } else if (WIFSIGNALED(status)) {
121 lyxerr << "LyX: Child didn't catch signal "
123 << "and died. Too bad." << endl;
125 } else if (WIFSTOPPED(status)) {
126 lyxerr << "LyX: Child (pid: " << pid
127 << ") stopped on signal "
129 << ". Waiting for child to finish." << endl;
131 lyxerr << "LyX: Something rotten happened while "
132 "waiting for child " << pid << endl;
139 // generate child in background
141 pid_t Systemcalls::fork()
143 pid_t cpid= ::fork();
144 if (cpid == 0) { // child
145 string childcommand(command); // copy
146 string rest = split(command, childcommand, ' ');
147 const int MAX_ARGV = 255;
149 char *argv[MAX_ARGV];
154 syscmd = new char[childcommand.length() + 1];
155 childcommand.copy(syscmd, childcommand.length());
156 syscmd[childcommand.length()] = '\0';
158 char * tmp = new char[childcommand.length() + 1];
159 childcommand.copy(tmp, childcommand.length());
160 tmp[childcommand.length()] = '\0';
163 more = !rest.empty();
165 rest = split(rest, childcommand, ' ');
168 // replace by command. Expand using PATH-environment-var.
169 execvp(syscmd, argv);
170 // If something goes wrong, we end up here:
171 lyxerr << "LyX: execvp failed: " << strerror(errno) << endl;
172 } else if (cpid < 0) { // error
173 lyxerr << "LyX: Could not fork: " << strerror(errno) << endl;
183 int Systemcalls::startscript(Starttype how, string const & what,
189 pid = static_cast<pid_t>(0); // yet no child
191 return startscript();
197 // Mini-Test-environment for script-classes
204 void back(string cmd, int retval)
206 printf("Done: %s gave %d\n", cmd.c_str(), retval);
211 int main(int, char **)
214 SystemcallsSingletoncontroller::Startcontroller starter;
215 SystemcallsSingletoncontroller *contr= starter.GetController();
217 Systemcalls one(Systemcalls::System, "ls -ltag", back);
218 Systemcalls two(Systemcalls::Wait, "ls -ltag", back);
220 Systemcalls three(Systemcalls::DontWait , "ls -ltag", back);
221 // Simulation of timer
222 while (SimulateTimer)