1 #include <lib/base/console.h>
2 #include <lib/base/eerror.h>
3 #include <sys/vfs.h> // for statfs
11 int bidirpipe(int pfd[], char *cmd , char *argv[])
13 int pfdin[2]; /* from child to parent */
14 int pfdout[2]; /* from parent to child */
15 int pfderr[2]; /* stderr from child to parent */
16 int pid; /* child's pid */
18 if ( pipe(pfdin) == -1 || pipe(pfdout) == -1 || pipe(pfderr) == -1)
21 if ( ( pid = vfork() ) == -1 )
23 else if (pid == 0) /* child process */
26 if ( close(0) == -1 || close(1) == -1 || close(2) == -1 )
29 if (dup(pfdout[0]) != 0 || dup(pfdin[1]) != 1 || dup(pfderr[1]) != 2 )
32 if (close(pfdout[0]) == -1 || close(pfdout[1]) == -1 ||
33 close(pfdin[0]) == -1 || close(pfdin[1]) == -1 ||
34 close(pfderr[0]) == -1 || close(pfderr[1]) == -1 )
37 for (unsigned int i=3; i < 90; ++i )
43 if (close(pfdout[0]) == -1 || close(pfdin[1]) == -1 || close(pfderr[1]) == -1)
53 eConsoleAppContainer::eConsoleAppContainer()
54 :pid(-1), killstate(0), in(0), out(0), err(0)
56 for (int i=0; i < 3; ++i)
60 static char brakets[][2] = {
70 static char *find_bracket(char ch)
73 while (idx < sizeof(brakets)/2) {
74 if (brakets[idx][0] == ch)
75 return &brakets[idx][0];
81 int eConsoleAppContainer::execute( const char *cmd )
88 int cnt=0, slen=strlen(cmd);
90 char *tmp=0, *argv[64], *path=buf, *cmds = buf;
91 memcpy(buf, cmd, slen+1);
93 // printf("cmd = %s, len %d\n", cmd, slen);
95 // kill spaces at beginning
96 while(path[0] == ' ') {
102 // kill spaces at the end
103 while(slen && path[slen-1] == ' ') {
111 tmp = strchr(path, ' ');
118 memset(argv, 0, sizeof(argv));
123 char *it = find_bracket(*cmds);
124 while ( (tmp = strchr(cmds, ' ')) ) {
125 if (!argb) // not arg begin
127 if (it && *(tmp-1) == it[1]) {
130 if (!it) { // end of arg
132 argb=0; // reset arg begin
133 it = find_bracket(*(tmp+1));
138 argv[cnt++] = argb ? argb : cmds;
141 // get one read ,one write and the err pipe to the prog..
145 // eDebug("%d is %s", tmp, argv[tmp++]);
147 pid = bidirpipe(fd, argv[0], argv);
152 // eDebug("pipe in = %d, out = %d, err = %d", fd[0], fd[1], fd[2]);
154 in = new eSocketNotifier(eApp, fd[0], eSocketNotifier::Read|eSocketNotifier::Priority|eSocketNotifier::Hungup );
155 out = new eSocketNotifier(eApp, fd[1], eSocketNotifier::Write, false);
156 err = new eSocketNotifier(eApp, fd[2], eSocketNotifier::Read|eSocketNotifier::Priority );
157 CONNECT(in->activated, eConsoleAppContainer::readyRead);
158 CONNECT(out->activated, eConsoleAppContainer::readyWrite);
159 CONNECT(err->activated, eConsoleAppContainer::readyErrRead);
164 eConsoleAppContainer::~eConsoleAppContainer()
169 void eConsoleAppContainer::kill()
171 if ( killstate != -1 && pid != -1 )
173 eDebug("user kill(SIGKILL) console App");
176 * Use a negative pid value, to signal the whole process group
177 * ('pid' might not even be running anymore at this point)
179 ::kill(-pid, SIGKILL);
182 while( outbuf.size() ) // cleanup out buffer
184 queue_data d = outbuf.front();
194 void eConsoleAppContainer::sendCtrlC()
196 if ( killstate != -1 && pid != -1 )
198 eDebug("user send SIGINT(Ctrl-C) to console App");
200 * Use a negative pid value, to signal the whole process group
201 * ('pid' might not even be running anymore at this point)
203 ::kill(-pid, SIGINT);
207 void eConsoleAppContainer::closePipes()
230 eDebug("pipes closed");
231 while( outbuf.size() ) // cleanup out buffer
233 queue_data d = outbuf.front();
240 void eConsoleAppContainer::readyRead(int what)
242 bool hungup = what & eSocketNotifier::Hungup;
243 if (what & (eSocketNotifier::Priority|eSocketNotifier::Read))
245 // eDebug("what = %d");
248 while((rd = read(fd[0], buf, 2047)) > 0)
250 /* for ( int i = 0; i < rd; i++ )
251 eDebug("%d = %c (%02x)", i, buf[i], buf[i] );*/
253 /*emit*/ dataAvail(buf);
260 eDebug("child has terminated");
263 int retval = killstate;
265 * We have to call 'wait' on the child process, in order to avoid zombies.
266 * Also, this gives us the chance to provide better exit status info to appClosed.
268 if (::waitpid(pid, &childstatus, 0) > 0)
270 if (WIFEXITED(childstatus))
272 retval = WEXITSTATUS(childstatus);
275 /*emit*/ appClosed(retval);
279 void eConsoleAppContainer::readyErrRead(int what)
281 if (what & (eSocketNotifier::Priority|eSocketNotifier::Read))
283 // eDebug("what = %d");
286 while((rd = read(fd[2], buf, 2047)) > 0)
288 /* for ( int i = 0; i < rd; i++ )
289 eDebug("%d = %c (%02x)", i, buf[i], buf[i] );*/
291 /*emit*/ dataAvail(buf);
296 void eConsoleAppContainer::write( const char *data, int len )
298 char *tmp = new char[len];
299 memcpy(tmp, data, len);
300 outbuf.push(queue_data(tmp,len));
304 void eConsoleAppContainer::readyWrite(int what)
306 if (what&eSocketNotifier::Write && outbuf.size() )
308 queue_data d = outbuf.front();
310 if ( ::write( fd[1], d.data, d.len ) != d.len )
312 /* emit */ dataSent(-1);
313 // eDebug("writeError");
317 /* emit */ dataSent(0);
318 // eDebug("write ok");
322 if ( !outbuf.size() )