]> git.lyx.org Git - lyx.git/blob - src/support/syscall.C
use more explicit on constructors use the pimpl idom to reduce size with about 500k
[lyx.git] / src / support / syscall.C
1 #include <config.h>
2
3 #ifdef __GNUG__
4 #pragma implementation
5 #endif
6
7 #include <cerrno>
8 #include <sys/types.h>
9 #include <sys/wait.h>
10 #include <csignal>
11 #include <cstdlib>
12 #include <unistd.h>
13 #include "debug.h"
14 #include "syscall.h"
15 #include "syscontr.h"
16 #include "support/lstrings.h"
17 #include "support/lyxlib.h"
18
19 using std::endl;
20
21
22 Systemcalls::Systemcalls() {
23         pid = 0; // No child yet
24 }
25
26 Systemcalls::Systemcalls(Starttype how, string const & what, Callbackfct cback)
27 {
28         start   = how;
29         command = what;
30         cbk     = cback;
31         pid     = static_cast<pid_t>(0);
32         retval  = 0;
33         startscript();
34 }
35
36 Systemcalls::~Systemcalls() {
37 #if 0
38         // If the child is alive, we have to brutally kill it
39         if (getpid() != 0) {
40                 lyx::kill(getpid(), SIGKILL);
41         }
42 #endif
43 }
44
45 // Start a childprocess
46 // 
47 // if child runs in background, add information to global controller.
48
49 int Systemcalls::startscript() {
50         retval = 0;
51         switch (start) {
52         case System: 
53                 retval = system(command.c_str());
54                 callback();
55                 break;
56         case Wait:   
57                 pid = fork();
58                 if (pid>0) { // Fork succesful. Wait for child
59                         waitForChild();
60                         callback();
61                 } else
62                         retval = 1;
63                 break;
64         case DontWait:
65                 pid = fork();
66                 if (pid>0) {
67                         // Now integrate into Controller
68                         SystemcallsSingletoncontroller::Startcontroller starter;
69                         SystemcallsSingletoncontroller * contr = starter.getController();
70                         // Add this to controller
71                         contr->addCall(*this);
72                 } else
73                         retval = 1;
74                 break;
75         }
76         return retval;
77 }
78
79 void Systemcalls::kill(int /*tolerance*/) {
80         if (getpid() == 0) {
81                 lyxerr << "LyX: Can't kill non-existing process." << endl;
82                 return;
83         }
84         int ret = lyx::kill(getpid(), SIGHUP);
85         bool wait_for_death = true;
86         if (ret != 0) {
87                 if (errno == ESRCH) {
88                         // The process is already dead!
89                         wait_for_death = false;
90                 } else {
91                         // Something is rotten - maybe we lost permissions?
92                 }
93         }
94         if (wait_for_death) {
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
98 #ifdef WITH_WARNINGS
99 #warning Implement this using the timer of the singleton systemcontroller (Asger)
100 #endif
101         }
102 }
103
104
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.
108         retval = 1;
109         int status;
110         bool wait = true;
111         while (wait) {
112                 pid_t waitrpid = waitpid(pid, &status, WUNTRACED);
113                 if (waitrpid == -1) {
114                         lyxerr << "LyX: Error waiting for child:" << strerror(errno) << endl;
115                         wait = false;
116                 } else if (WIFEXITED(status)) {
117                         // Child exited normally. Update return value.
118                         retval = WEXITSTATUS(status);
119                         wait = false;
120                 } else if (WIFSIGNALED(status)) {
121                         lyxerr << "LyX: Child didn't catch signal "
122                                << WTERMSIG(status) 
123                                << "and died. Too bad." << endl;
124                         wait = false;
125                 } else if (WIFSTOPPED(status)) {
126                         lyxerr << "LyX: Child (pid: " << pid 
127                                << ") stopped on signal "
128                                << WSTOPSIG(status) 
129                                << ". Waiting for child to finish." << endl;
130                 } else {
131                         lyxerr << "LyX: Something rotten happened while "
132                                 "waiting for child " << pid << endl;
133                         wait = false;
134                 }
135         }
136 }
137
138
139 // generate child in background
140
141 pid_t Systemcalls::fork()
142 {
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;
148                 char *syscmd = 0; 
149                 char *argv[MAX_ARGV];
150                 int  index = 0;
151                 bool more;
152                 do {
153                         if (syscmd == 0) {
154                                 syscmd = new char[childcommand.length() + 1];
155                                 childcommand.copy(syscmd, childcommand.length());
156                                 syscmd[childcommand.length()] = '\0';
157                         }
158                         char * tmp = new char[childcommand.length() + 1];
159                         childcommand.copy(tmp, childcommand.length());
160                         tmp[childcommand.length()] = '\0';
161                         argv[index++] = tmp;
162                         // reinit
163                         more = !rest.empty();
164                         if (more) 
165                                 rest = split(rest, childcommand, ' ');
166                 } while (more);
167                 argv[index] = 0;
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;
174         } else { // parent
175                 return cpid;
176         }
177         return 0;
178 }
179
180
181 // Reuse of instance
182
183 int Systemcalls::startscript(Starttype how, string const & what, 
184                              Callbackfct cback)
185 {
186         start   = how;
187         command = what;
188         cbk     = cback;
189         pid     = static_cast<pid_t>(0); // yet no child
190         retval  = 0;
191         return startscript();
192 }
193
194
195
196 //
197 // Mini-Test-environment for script-classes
198 //
199 #ifdef TEST_MAIN
200 #include <stdio.h>
201
202
203 int SimulateTimer;
204 void back(string cmd, int retval)
205 {
206         printf("Done: %s gave %d\n", cmd.c_str(), retval);
207         SimulateTimer = 0;
208 }
209
210
211 int main(int, char **)
212 {
213         
214         SystemcallsSingletoncontroller::Startcontroller starter; 
215         SystemcallsSingletoncontroller *contr= starter.GetController();
216         
217         Systemcalls one(Systemcalls::System, "ls -ltag", back);
218         Systemcalls two(Systemcalls::Wait, "ls -ltag", back);
219         SimulateTimer = 1;
220         Systemcalls three(Systemcalls::DontWait , "ls -ltag", back);
221         // Simulation of timer
222         while (SimulateTimer)
223                 {
224                         sleep(1);
225                         contr->Timer();
226                 }
227 }
228 #endif