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