1 #include <lib/base/console.h>
2 #include <lib/base/eerror.h>
3 #include <sys/vfs.h> // for statfs
11 int bidirpipe(int pfd[], const char *cmd , const char * const 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 )
40 execvp(cmd, (char * const *)argv);
41 /* the vfork will actually suspend the parent thread until execvp is called. thus it's ok to use the shared arg/cmdline pointers here. */
44 if (close(pfdout[0]) == -1 || close(pfdin[1]) == -1 || close(pfderr[1]) == -1)
54 eConsoleAppContainer::eConsoleAppContainer()
55 :pid(-1), killstate(0), in(0), out(0), err(0)
57 for (int i=0; i < 3; ++i)
61 static char brakets[][2] = {
71 static char *find_bracket(char ch)
74 while (idx < sizeof(brakets)/2) {
75 if (brakets[idx][0] == ch)
76 return &brakets[idx][0];
82 int eConsoleAppContainer::execute( const char *cmd )
84 int cnt=0, slen=strlen(cmd);
86 char *tmp=0, *argv[64], *path=buf, *cmds = buf;
87 memcpy(buf, cmd, slen+1);
89 // printf("cmd = %s, len %d\n", cmd, slen);
91 // kill spaces at beginning
92 while(path[0] == ' ') {
98 // kill spaces at the end
99 while(slen && path[slen-1] == ' ') {
107 tmp = strchr(path, ' ');
111 while(*cmds && *cmds == ' ')
117 memset(argv, 0, sizeof(argv));
121 char *argb=NULL, *it=NULL;
122 while ( (tmp = strchr(cmds, ' ')) ) {
123 if (!it && *cmds && (it = find_bracket(*cmds)) )
124 *cmds = 'X'; // replace open braket...
125 if (!argb) // not arg begin
127 if (it && *(tmp-1) == it[1]) {
128 *argb = it[0]; // set old char for open braket
131 if (!it) { // end of arg
134 argb=0; // reset arg begin
137 while (*cmds && *cmds == ' ')
140 argv[cnt++] = argb ? argb : cmds;
142 *argv[cnt-1] = it[0]; // set old char for open braket
147 // eDebug("%d is %s", tmp, argv[tmp++]);
148 return execute(argv[0], argv);
151 int eConsoleAppContainer::execute(const char *cmdline, const char * const argv[])
159 // get one read ,one write and the err pipe to the prog..
160 pid = bidirpipe(fd, cmdline, argv);
165 // eDebug("pipe in = %d, out = %d, err = %d", fd[0], fd[1], fd[2]);
167 in = new eSocketNotifier(eApp, fd[0], eSocketNotifier::Read|eSocketNotifier::Priority|eSocketNotifier::Hungup );
168 out = new eSocketNotifier(eApp, fd[1], eSocketNotifier::Write, false);
169 err = new eSocketNotifier(eApp, fd[2], eSocketNotifier::Read|eSocketNotifier::Priority );
170 CONNECT(in->activated, eConsoleAppContainer::readyRead);
171 CONNECT(out->activated, eConsoleAppContainer::readyWrite);
172 CONNECT(err->activated, eConsoleAppContainer::readyErrRead);
177 int eConsoleAppContainer::execute( PyObject *cmdline, PyObject *args )
179 if (!PyString_Check(cmdline))
181 if (!PyList_Check(args))
183 const char *argv[PyList_Size(args) + 1];
185 for (i = 0; i < PyList_Size(args); ++i)
187 PyObject *arg = PyList_GetItem(args, i); /* borrowed ref */
190 if (!PyString_Check(arg))
192 argv[i] = PyString_AsString(arg); /* borrowed pointer */
196 return execute(PyString_AsString(cmdline), argv); /* borrowed pointer */
199 eConsoleAppContainer::~eConsoleAppContainer()
204 void eConsoleAppContainer::kill()
206 if ( killstate != -1 && pid != -1 )
208 eDebug("user kill(SIGKILL) console App");
211 * Use a negative pid value, to signal the whole process group
212 * ('pid' might not even be running anymore at this point)
214 ::kill(-pid, SIGKILL);
217 while( outbuf.size() ) // cleanup out buffer
219 queue_data d = outbuf.front();
229 void eConsoleAppContainer::sendCtrlC()
231 if ( killstate != -1 && pid != -1 )
233 eDebug("user send SIGINT(Ctrl-C) to console App");
235 * Use a negative pid value, to signal the whole process group
236 * ('pid' might not even be running anymore at this point)
238 ::kill(-pid, SIGINT);
242 void eConsoleAppContainer::closePipes()
265 eDebug("pipes closed");
266 while( outbuf.size() ) // cleanup out buffer
268 queue_data d = outbuf.front();
275 void eConsoleAppContainer::readyRead(int what)
277 bool hungup = what & eSocketNotifier::Hungup;
278 if (what & (eSocketNotifier::Priority|eSocketNotifier::Read))
280 // eDebug("what = %d");
283 while((rd = read(fd[0], buf, 2047)) > 0)
285 /* for ( int i = 0; i < rd; i++ )
286 eDebug("%d = %c (%02x)", i, buf[i], buf[i] );*/
288 /*emit*/ dataAvail(buf);
295 eDebug("child has terminated");
298 int retval = killstate;
300 * We have to call 'wait' on the child process, in order to avoid zombies.
301 * Also, this gives us the chance to provide better exit status info to appClosed.
303 if (::waitpid(pid, &childstatus, 0) > 0)
305 if (WIFEXITED(childstatus))
307 retval = WEXITSTATUS(childstatus);
310 /*emit*/ appClosed(retval);
314 void eConsoleAppContainer::readyErrRead(int what)
316 if (what & (eSocketNotifier::Priority|eSocketNotifier::Read))
318 // eDebug("what = %d");
321 while((rd = read(fd[2], buf, 2047)) > 0)
323 /* for ( int i = 0; i < rd; i++ )
324 eDebug("%d = %c (%02x)", i, buf[i], buf[i] );*/
326 /*emit*/ dataAvail(buf);
331 void eConsoleAppContainer::write( const char *data, int len )
333 char *tmp = new char[len];
334 memcpy(tmp, data, len);
335 outbuf.push(queue_data(tmp,len));
340 void eConsoleAppContainer::write( PyObject *data )
344 if (PyString_AsStringAndSize(data, &buffer, &length))
346 if (buffer && length)
347 write(buffer, length);
350 void eConsoleAppContainer::readyWrite(int what)
352 if (what&eSocketNotifier::Write && outbuf.size() )
354 queue_data d = outbuf.front();
356 if ( ::write( fd[1], d.data, d.len ) != d.len )
358 /* emit */ dataSent(-1);
359 // eDebug("writeError");
363 /* emit */ dataSent(0);
364 // eDebug("write ok");
368 if ( !outbuf.size() )