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:"
114 << std::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()
144 pid_t cpid= ::fork();
145 if (cpid == 0) { // child
147 // TODO: Consider doing all of this before the fork, otherwise me
148 // might have troubles with multi-threaded access. (Baruch 20010228)
149 string childcommand(command); // copy
150 string rest = split(command, childcommand, ' ');
151 const int MAX_ARGV = 255;
153 char *argv[MAX_ARGV];
157 childcommand = frontStrip(childcommand);
159 syscmd = new char[childcommand.length() + 1];
160 childcommand.copy(syscmd, childcommand.length());
161 syscmd[childcommand.length()] = '\0';
163 if (!childcommand.empty()) {
164 char * tmp = new char[childcommand.length() + 1];
165 childcommand.copy(tmp, childcommand.length());
166 tmp[childcommand.length()] = '\0';
171 more = !rest.empty();
173 rest = split(rest, childcommand, ' ');
176 // replace by command. Expand using PATH-environment-var.
178 execvp(syscmd, argv);
179 // If something goes wrong, we end up here:
180 lyxerr << "LyX: execvp failed: "
181 << std::strerror(errno) << endl;
182 } else if (cpid < 0) { // error
184 pid_t cpid = spawnvp(P_SESSION|P_DEFAULT|P_MINIMIZE|P_BACKGROUND, syscmd, argv);
185 if (cpid < 0) { // error
187 lyxerr << "LyX: Could not fork: "
188 << std::strerror(errno) << endl;
198 int Systemcalls::startscript(Starttype how, string const & what,
204 pid = static_cast<pid_t>(0); // yet no child
207 if (how == SystemDontWait) {
208 (os::shell() == os::UNIX) ? command += " &"
209 : command = "start /min/n " + command;
212 return startscript();
218 // Mini-Test-environment for script-classes
225 void back(string cmd, int retval)
227 ::printf("Done: %s gave %d\n", cmd.c_str(), retval);
232 int main(int, char **)
235 SystemcallsSingletoncontroller::Startcontroller starter;
236 SystemcallsSingletoncontroller *contr= starter.GetController();
238 Systemcalls one(Systemcalls::System, "ls -ltag", back);
239 Systemcalls two(Systemcalls::Wait, "ls -ltag", back);
241 Systemcalls three(Systemcalls::DontWait , "ls -ltag", back);
242 // Simulation of timer
243 while (SimulateTimer)