1999-09-27 18:44:28 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
1999-12-13 00:05:34 +00:00
|
|
|
#include <cerrno>
|
|
|
|
#include <cstdlib>
|
1999-09-27 18:44:28 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include "syscontr.h"
|
|
|
|
#include "syscall.h"
|
1999-10-12 21:37:10 +00:00
|
|
|
#include "debug.h"
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
#ifdef __GNUG__
|
|
|
|
#pragma implementation
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2000-03-28 02:18:55 +00:00
|
|
|
using std::endl;
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
//
|
1999-10-12 21:37:10 +00:00
|
|
|
// Default constructor
|
1999-09-27 18:44:28 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
SystemcallsSingletoncontroller::SystemcallsSingletoncontroller()
|
|
|
|
{
|
1999-10-12 21:37:10 +00:00
|
|
|
sysCalls = 0;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
1999-10-12 21:37:10 +00:00
|
|
|
// Destructor
|
1999-09-27 18:44:28 +00:00
|
|
|
//
|
|
|
|
// destroy structs for leaving program
|
1999-10-12 21:37:10 +00:00
|
|
|
// open question: should we stop childs here?
|
1999-09-27 18:44:28 +00:00
|
|
|
// Asger says no: I like to have my xdvi open after closing LyX. Maybe
|
|
|
|
// I want to print or something.
|
|
|
|
|
|
|
|
SystemcallsSingletoncontroller::~SystemcallsSingletoncontroller()
|
|
|
|
{
|
|
|
|
ControlledCalls *next;
|
1999-10-12 21:37:10 +00:00
|
|
|
while (sysCalls)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
1999-10-12 21:37:10 +00:00
|
|
|
next = sysCalls->next;
|
|
|
|
delete sysCalls;
|
|
|
|
sysCalls = next;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
1999-10-12 21:37:10 +00:00
|
|
|
// Add child process information into controlled list
|
1999-09-27 18:44:28 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
void
|
1999-10-12 21:37:10 +00:00
|
|
|
SystemcallsSingletoncontroller::addCall(Systemcalls const &newcall) {
|
|
|
|
ControlledCalls * newCall = new ControlledCalls;
|
1999-10-02 16:21:10 +00:00
|
|
|
if (newCall == 0) // sorry, no idea
|
1999-09-27 18:44:28 +00:00
|
|
|
return;
|
1999-10-12 21:37:10 +00:00
|
|
|
newCall->next = sysCalls;
|
1999-09-27 18:44:28 +00:00
|
|
|
newCall->call = new Systemcalls(newcall);
|
1999-10-12 21:37:10 +00:00
|
|
|
sysCalls = newCall;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Timer-call
|
|
|
|
//
|
|
|
|
// Check list, if there is a stopped child. If yes, call-back.
|
|
|
|
//
|
|
|
|
|
|
|
|
void
|
1999-10-12 21:37:10 +00:00
|
|
|
SystemcallsSingletoncontroller::timer() {
|
1999-12-19 22:35:36 +00:00
|
|
|
lyxerr << "Tick" << endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
// check each entry of our list, if it's finished
|
1999-10-02 16:21:10 +00:00
|
|
|
ControlledCalls *prev = 0;
|
1999-11-15 10:54:16 +00:00
|
|
|
for (ControlledCalls *actCall= sysCalls; actCall; actCall= actCall->next)
|
1999-10-12 21:37:10 +00:00
|
|
|
{
|
1999-11-15 10:54:16 +00:00
|
|
|
pid_t pid= actCall->call->getpid();
|
1999-10-12 21:37:10 +00:00
|
|
|
int stat_loc;
|
|
|
|
int waitrpid = waitpid(pid, &stat_loc, WNOHANG);
|
|
|
|
if (waitrpid == -1) {
|
|
|
|
lyxerr << "LyX: Error waiting for child:"
|
2001-05-31 02:23:46 +00:00
|
|
|
<< std::strerror(errno) << endl;
|
1999-10-12 21:37:10 +00:00
|
|
|
} else if (WIFEXITED(stat_loc) || WIFSIGNALED(stat_loc)) {
|
|
|
|
if (WIFEXITED(stat_loc)) {
|
1999-09-27 18:44:28 +00:00
|
|
|
// Ok, the return value goes into retval.
|
1999-10-12 21:37:10 +00:00
|
|
|
actCall->call->setRetValue(WEXITSTATUS(stat_loc));
|
|
|
|
} else {
|
|
|
|
// Child died, so pretend it returned 1
|
|
|
|
actCall->call->setRetValue(1);
|
|
|
|
}
|
|
|
|
// Callback and release
|
|
|
|
actCall->call->callback();
|
|
|
|
if (actCall == sysCalls) {
|
|
|
|
sysCalls = actCall->next;
|
|
|
|
} else {
|
|
|
|
prev->next = actCall->next;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
1999-10-12 21:37:10 +00:00
|
|
|
delete actCall;
|
|
|
|
actCall = prev;
|
|
|
|
} else if (WIFSTOPPED(stat_loc)) {
|
|
|
|
lyxerr << "LyX: Child (pid: " << pid
|
|
|
|
<< ") stopped on signal "
|
|
|
|
<< WSTOPSIG(stat_loc)
|
|
|
|
<< ". Waiting for child to finish." << endl;
|
|
|
|
} else {
|
|
|
|
lyxerr << "LyX: Something rotten happened while "
|
|
|
|
"waiting for child " << pid << endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
1999-10-12 21:37:10 +00:00
|
|
|
prev = actCall;
|
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|