16 #include "support/lstrings.h"
18 Systemcalls::Systemcalls() {
19 pid = 0; // No child yet
22 Systemcalls::Systemcalls(Starttype how, string const & what, Callbackfct cback)
27 pid = static_cast<pid_t>(0);
32 Systemcalls::~Systemcalls() {
34 // If the child is alive, we have to brutally kill it
36 ::kill(getpid(), SIGKILL);
41 // Start a childprocess
43 // if child runs in background, add information to global controller.
45 int Systemcalls::startscript() {
49 retval = system(command.c_str());
54 if (pid>0) { // Fork succesful. Wait for child
63 // Now integrate into Controller
64 SystemcallsSingletoncontroller::Startcontroller starter;
65 SystemcallsSingletoncontroller * contr = starter.getController();
66 // Add this to controller
67 contr->addCall(*this);
75 void Systemcalls::kill(int /*tolerance*/) {
77 lyxerr << "LyX: Can't kill non-existing process." << endl;
80 int ret = ::kill(getpid(), SIGHUP);
81 bool wait_for_death = true;
84 // The process is already dead!
85 wait_for_death = false;
87 // Something is rotten - maybe we lost permissions?
91 // Here, we should add the PID to a list of
92 // waiting processes to kill if they are not
93 // dead without tolerance seconds
95 #warning Implement this using the timer of the singleton systemcontroller (Asger)
101 // Wait for child process to finish. Returns returncode from child.
102 void Systemcalls::waitForChild() {
103 // We'll pretend that the child returns 1 on all errorconditions.
108 pid_t waitrpid = waitpid(pid, &status, WUNTRACED);
109 if (waitrpid == -1) {
110 lyxerr << "LyX: Error waiting for child:" << strerror(errno) << endl;
112 } else if (WIFEXITED(status)) {
113 // Child exited normally. Update return value.
114 retval = WEXITSTATUS(status);
116 } else if (WIFSIGNALED(status)) {
117 lyxerr << "LyX: Child didn't catch signal "
119 << "and died. Too bad." << endl;
121 } else if (WIFSTOPPED(status)) {
122 lyxerr << "LyX: Child (pid: " << pid
123 << ") stopped on signal "
125 << ". Waiting for child to finish." << endl;
127 lyxerr << "LyX: Something rotten happened while "
128 "waiting for child " << pid << endl;
135 // generate child in background
137 pid_t Systemcalls::fork()
139 pid_t cpid= ::fork();
140 if (cpid == 0) { // child
141 string childcommand(command); // copy
142 string rest = split(command, childcommand, ' ');
143 const int MAX_ARGV = 255;
145 char *argv[MAX_ARGV];
150 syscmd = new char[childcommand.length() + 1];
151 childcommand.copy(syscmd, childcommand.length());
152 syscmd[childcommand.length()] = '\0';
154 char * tmp = new char[childcommand.length() + 1];
155 childcommand.copy(tmp, childcommand.length());
156 tmp[childcommand.length()] = '\0';
159 more = !rest.empty();
161 rest = split(rest, childcommand, ' ');
164 // replace by command. Expand using PATH-environment-var.
165 execvp(syscmd, argv);
166 // If something goes wrong, we end up here:
167 lyxerr << "LyX: execvp failed: " << strerror(errno) << endl;
168 } else if (cpid < 0) { // error
169 lyxerr << "LyX: Could not fork: " << strerror(errno) << endl;
179 int Systemcalls::startscript(Starttype how, string const & what,
185 pid = static_cast<pid_t>(0); // yet no child
187 return startscript();
193 // Mini-Test-environment for script-classes
200 void back(string cmd, int retval)
202 printf("Done: %s gave %d\n", cmd.c_str(), retval);
207 int main(int, char**)
210 SystemcallsSingletoncontroller::Startcontroller starter;
211 SystemcallsSingletoncontroller *contr= starter.GetController();
213 Systemcalls one(Systemcalls::System, "ls -ltag", back);
214 Systemcalls two(Systemcalls::Wait, "ls -ltag", back);
216 Systemcalls three(Systemcalls::DontWait , "ls -ltag", back);
217 // Simulation of timer
218 while (SimulateTimer)