1 #include <lib/base/console.h>
2 #include <lib/base/eerror.h>
3 #include <sys/vfs.h> // for statfs
12 int bidirpipe(int pfd[], const char *cmd , const char * const argv[], const char *cwd )
14 int pfdin[2]; /* from child to parent */
15 int pfdout[2]; /* from parent to child */
16 int pfderr[2]; /* stderr from child to parent */
17 int pid; /* child's pid */
19 if ( pipe(pfdin) == -1 || pipe(pfdout) == -1 || pipe(pfderr) == -1)
22 if ( ( pid = vfork() ) == -1 )
24 else if (pid == 0) /* child process */
27 if ( close(0) == -1 || close(1) == -1 || close(2) == -1 )
30 if (dup(pfdout[0]) != 0 || dup(pfdin[1]) != 1 || dup(pfderr[1]) != 2 )
33 if (close(pfdout[0]) == -1 || close(pfdout[1]) == -1 ||
34 close(pfdin[0]) == -1 || close(pfdin[1]) == -1 ||
35 close(pfderr[0]) == -1 || close(pfderr[1]) == -1 )
38 for (unsigned int i=3; i < 90; ++i )
44 execvp(cmd, (char * const *)argv);
45 /* the vfork will actually suspend the parent thread until execvp is called. thus it's ok to use the shared arg/cmdline pointers here. */
48 if (close(pfdout[0]) == -1 || close(pfdin[1]) == -1 || close(pfderr[1]) == -1)
58 eConsoleAppContainer::eConsoleAppContainer()
59 :pid(-1), killstate(0), in(0), out(0), err(0)
61 for (int i=0; i < 3; ++i)
68 static char brakets[][2] = {
78 static char *find_bracket(char ch)
81 while (idx < sizeof(brakets)/2) {
82 if (brakets[idx][0] == ch)
83 return &brakets[idx][0];
89 int eConsoleAppContainer::setCWD( const char *path )
93 if (stat(path, &dir_stat) == -1)
96 if (!S_ISDIR(dir_stat.st_mode))
103 int eConsoleAppContainer::execute( const char *cmd )
105 int cnt=0, slen=strlen(cmd);
107 char *tmp=0, *argv[64], *path=buf, *cmds = buf;
108 memcpy(buf, cmd, slen+1);
110 // printf("cmd = %s, len %d\n", cmd, slen);
112 // kill spaces at beginning
113 while(path[0] == ' ') {
119 // kill spaces at the end
120 while(slen && path[slen-1] == ' ') {
128 tmp = strchr(path, ' ');
132 while(*cmds && *cmds == ' ')
138 memset(argv, 0, sizeof(argv));
142 char *argb=NULL, *it=NULL;
143 while ( (tmp = strchr(cmds, ' ')) ) {
144 if (!it && *cmds && (it = find_bracket(*cmds)) )
145 *cmds = 'X'; // replace open braket...
146 if (!argb) // not arg begin
148 if (it && *(tmp-1) == it[1]) {
149 *argb = it[0]; // set old char for open braket
152 if (!it) { // end of arg
155 argb=0; // reset arg begin
158 while (*cmds && *cmds == ' ')
161 argv[cnt++] = argb ? argb : cmds;
163 *argv[cnt-1] = it[0]; // set old char for open braket
168 // eDebug("%d is %s", tmp, argv[tmp++]);
169 return execute(argv[0], argv);
172 int eConsoleAppContainer::execute(const char *cmdline, const char * const argv[])
180 // get one read ,one write and the err pipe to the prog..
181 pid = bidirpipe(fd, cmdline, argv, m_cwd.length() ? m_cwd.c_str() : 0);
186 // eDebug("pipe in = %d, out = %d, err = %d", fd[0], fd[1], fd[2]);
188 ::fcntl(fd[1], F_SETFL, O_NONBLOCK);
189 ::fcntl(fd[2], F_SETFL, O_NONBLOCK);
190 in = new eSocketNotifier(eApp, fd[0], eSocketNotifier::Read|eSocketNotifier::Priority|eSocketNotifier::Hungup );
191 out = new eSocketNotifier(eApp, fd[1], eSocketNotifier::Write, false);
192 err = new eSocketNotifier(eApp, fd[2], eSocketNotifier::Read|eSocketNotifier::Priority );
193 CONNECT(in->activated, eConsoleAppContainer::readyRead);
194 CONNECT(out->activated, eConsoleAppContainer::readyWrite);
195 CONNECT(err->activated, eConsoleAppContainer::readyErrRead);
200 int eConsoleAppContainer::execute( PyObject *cmdline, PyObject *args )
202 if (!PyString_Check(cmdline))
204 if (!PyList_Check(args))
206 const char *argv[PyList_Size(args) + 1];
208 for (i = 0; i < PyList_Size(args); ++i)
210 PyObject *arg = PyList_GetItem(args, i); /* borrowed ref */
213 if (!PyString_Check(arg))
215 argv[i] = PyString_AsString(arg); /* borrowed pointer */
219 return execute(PyString_AsString(cmdline), argv); /* borrowed pointer */
222 eConsoleAppContainer::~eConsoleAppContainer()
227 void eConsoleAppContainer::kill()
229 if ( killstate != -1 && pid != -1 )
231 eDebug("user kill(SIGKILL) console App");
234 * Use a negative pid value, to signal the whole process group
235 * ('pid' might not even be running anymore at this point)
237 ::kill(-pid, SIGKILL);
240 while( outbuf.size() ) // cleanup out buffer
242 queue_data d = outbuf.front();
251 for (int i=0; i < 3; ++i)
258 void eConsoleAppContainer::sendCtrlC()
260 if ( killstate != -1 && pid != -1 )
262 eDebug("user send SIGINT(Ctrl-C) to console App");
264 * Use a negative pid value, to signal the whole process group
265 * ('pid' might not even be running anymore at this point)
267 ::kill(-pid, SIGINT);
271 void eConsoleAppContainer::sendEOF()
282 void eConsoleAppContainer::closePipes()
305 eDebug("pipes closed");
306 while( outbuf.size() ) // cleanup out buffer
308 queue_data d = outbuf.front();
315 void eConsoleAppContainer::readyRead(int what)
317 bool hungup = what & eSocketNotifier::Hungup;
318 if (what & (eSocketNotifier::Priority|eSocketNotifier::Read))
320 // eDebug("what = %d");
323 while((rd = read(fd[0], buf, 2048)) > 0)
326 /*emit*/ dataAvail(buf);
329 ::write(filefd[1], buf, rd);
334 readyErrRead(eSocketNotifier::Priority|eSocketNotifier::Read); /* be sure to flush all data which might be already written */
337 eDebug("child has terminated");
340 int retval = killstate;
342 * We have to call 'wait' on the child process, in order to avoid zombies.
343 * Also, this gives us the chance to provide better exit status info to appClosed.
345 if (::waitpid(pid, &childstatus, 0) > 0)
347 if (WIFEXITED(childstatus))
349 retval = WEXITSTATUS(childstatus);
352 /*emit*/ appClosed(retval);
356 void eConsoleAppContainer::readyErrRead(int what)
358 if (what & (eSocketNotifier::Priority|eSocketNotifier::Read))
360 // eDebug("what = %d");
363 while((rd = read(fd[2], buf, 2048)) > 0)
365 /* for ( int i = 0; i < rd; i++ )
366 eDebug("%d = %c (%02x)", i, buf[i], buf[i] );*/
368 /*emit*/ dataAvail(buf);
374 void eConsoleAppContainer::dumpToFile( PyObject *py_filename )
376 char *filename = PyString_AsString(py_filename);
377 filefd[1] = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0644);
378 eDebug("eConsoleAppContainer::dumpToFile open(%s, O_WRONLY|O_CREAT|O_TRUNC, 0644)=%i", filename, filefd[1]);
381 void eConsoleAppContainer::readFromFile( PyObject *py_filename )
383 char *filename = PyString_AsString(py_filename);
384 char readbuf[32*1024];
385 filefd[0] = open(filename, O_RDONLY);
386 int rsize = read(filefd[0], readbuf, 32*1024);
387 eDebug("eConsoleAppContainer::readFromFile open(%s, O_RDONLY)=%i, read: %i", filename, filefd[0], rsize);
388 if ( filefd[0] > 0 && rsize > 0 )
389 write(readbuf, rsize);
392 void eConsoleAppContainer::write( const char *data, int len )
394 char *tmp = new char[len];
395 memcpy(tmp, data, len);
396 outbuf.push(queue_data(tmp,len));
401 void eConsoleAppContainer::write( PyObject *data )
405 if (PyString_AsStringAndSize(data, &buffer, &length))
407 if (buffer && length)
408 write(buffer, length);
411 void eConsoleAppContainer::readyWrite(int what)
413 if (what&eSocketNotifier::Write && outbuf.size() )
415 queue_data &d = outbuf.front();
416 int wr = ::write( fd[1], d.data+d.dataSent, d.len-d.dataSent );
418 eDebug("eConsoleAppContainer write failed (%m)");
421 if (d.dataSent == d.len)
425 if ( filefd[0] == -1 )
426 /* emit */ dataSent(0);
429 if ( !outbuf.size() )
433 char readbuf[32*1024];
434 int rsize = read(filefd[0], readbuf, 32*1024);
436 write(readbuf, rsize);
442 eDebug("readFromFile done - closing eConsoleAppContainer stdin pipe");