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[], const char *cwd )
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 execvp(cmd, (char * const *)argv);
44 /* the vfork will actually suspend the parent thread until execvp is called. thus it's ok to use the shared arg/cmdline pointers here. */
47 if (close(pfdout[0]) == -1 || close(pfdin[1]) == -1 || close(pfderr[1]) == -1)
57 eConsoleAppContainer::eConsoleAppContainer()
58 :pid(-1), killstate(0), in(0), out(0), err(0)
60 for (int i=0; i < 3; ++i)
64 static char brakets[][2] = {
74 static char *find_bracket(char ch)
77 while (idx < sizeof(brakets)/2) {
78 if (brakets[idx][0] == ch)
79 return &brakets[idx][0];
85 int eConsoleAppContainer::setCWD( const char *path )
89 if (stat(path, &dir_stat) == -1)
92 if (!S_ISDIR(dir_stat.st_mode))
99 int eConsoleAppContainer::execute( const char *cmd )
101 int cnt=0, slen=strlen(cmd);
103 char *tmp=0, *argv[64], *path=buf, *cmds = buf;
104 memcpy(buf, cmd, slen+1);
106 // printf("cmd = %s, len %d\n", cmd, slen);
108 // kill spaces at beginning
109 while(path[0] == ' ') {
115 // kill spaces at the end
116 while(slen && path[slen-1] == ' ') {
124 tmp = strchr(path, ' ');
128 while(*cmds && *cmds == ' ')
134 memset(argv, 0, sizeof(argv));
138 char *argb=NULL, *it=NULL;
139 while ( (tmp = strchr(cmds, ' ')) ) {
140 if (!it && *cmds && (it = find_bracket(*cmds)) )
141 *cmds = 'X'; // replace open braket...
142 if (!argb) // not arg begin
144 if (it && *(tmp-1) == it[1]) {
145 *argb = it[0]; // set old char for open braket
148 if (!it) { // end of arg
151 argb=0; // reset arg begin
154 while (*cmds && *cmds == ' ')
157 argv[cnt++] = argb ? argb : cmds;
159 *argv[cnt-1] = it[0]; // set old char for open braket
164 // eDebug("%d is %s", tmp, argv[tmp++]);
165 return execute(argv[0], argv);
168 int eConsoleAppContainer::execute(const char *cmdline, const char * const argv[])
176 // get one read ,one write and the err pipe to the prog..
177 pid = bidirpipe(fd, cmdline, argv, m_cwd.length() ? m_cwd.c_str() : 0);
183 // eDebug("pipe in = %d, out = %d, err = %d", fd[0], fd[1], fd[2]);
185 in = new eSocketNotifier(eApp, fd[0], eSocketNotifier::Read|eSocketNotifier::Priority|eSocketNotifier::Hungup );
186 out = new eSocketNotifier(eApp, fd[1], eSocketNotifier::Write, false);
187 err = new eSocketNotifier(eApp, fd[2], eSocketNotifier::Read|eSocketNotifier::Priority );
188 CONNECT(in->activated, eConsoleAppContainer::readyRead);
189 CONNECT(out->activated, eConsoleAppContainer::readyWrite);
190 CONNECT(err->activated, eConsoleAppContainer::readyErrRead);
195 int eConsoleAppContainer::execute( PyObject *cmdline, PyObject *args )
197 if (!PyString_Check(cmdline))
199 if (!PyList_Check(args))
201 const char *argv[PyList_Size(args) + 1];
203 for (i = 0; i < PyList_Size(args); ++i)
205 PyObject *arg = PyList_GetItem(args, i); /* borrowed ref */
208 if (!PyString_Check(arg))
210 argv[i] = PyString_AsString(arg); /* borrowed pointer */
214 return execute(PyString_AsString(cmdline), argv); /* borrowed pointer */
217 eConsoleAppContainer::~eConsoleAppContainer()
222 void eConsoleAppContainer::kill()
224 if ( killstate != -1 && pid != -1 )
226 eDebug("user kill(SIGKILL) console App");
229 * Use a negative pid value, to signal the whole process group
230 * ('pid' might not even be running anymore at this point)
232 ::kill(-pid, SIGKILL);
235 while( outbuf.size() ) // cleanup out buffer
237 queue_data d = outbuf.front();
247 void eConsoleAppContainer::sendCtrlC()
249 if ( killstate != -1 && pid != -1 )
251 eDebug("user send SIGINT(Ctrl-C) to console App");
253 * Use a negative pid value, to signal the whole process group
254 * ('pid' might not even be running anymore at this point)
256 ::kill(-pid, SIGINT);
260 void eConsoleAppContainer::closePipes()
283 eDebug("pipes closed");
284 while( outbuf.size() ) // cleanup out buffer
286 queue_data d = outbuf.front();
293 void eConsoleAppContainer::readyRead(int what)
295 bool hungup = what & eSocketNotifier::Hungup;
296 if (what & (eSocketNotifier::Priority|eSocketNotifier::Read))
298 // eDebug("what = %d");
301 while((rd = read(fd[0], buf, 2047)) > 0)
303 /* for ( int i = 0; i < rd; i++ )
304 eDebug("%d = %c (%02x)", i, buf[i], buf[i] );*/
306 /*emit*/ dataAvail(buf);
313 eDebug("child has terminated");
316 int retval = killstate;
318 * We have to call 'wait' on the child process, in order to avoid zombies.
319 * Also, this gives us the chance to provide better exit status info to appClosed.
321 if (::waitpid(pid, &childstatus, 0) > 0)
323 if (WIFEXITED(childstatus))
325 retval = WEXITSTATUS(childstatus);
328 /*emit*/ appClosed(retval);
332 void eConsoleAppContainer::readyErrRead(int what)
334 if (what & (eSocketNotifier::Priority|eSocketNotifier::Read))
336 // eDebug("what = %d");
339 while((rd = read(fd[2], buf, 2047)) > 0)
341 /* for ( int i = 0; i < rd; i++ )
342 eDebug("%d = %c (%02x)", i, buf[i], buf[i] );*/
344 /*emit*/ dataAvail(buf);
349 void eConsoleAppContainer::write( const char *data, int len )
351 char *tmp = new char[len];
352 memcpy(tmp, data, len);
353 outbuf.push(queue_data(tmp,len));
358 void eConsoleAppContainer::write( PyObject *data )
362 if (PyString_AsStringAndSize(data, &buffer, &length))
364 if (buffer && length)
365 write(buffer, length);
368 void eConsoleAppContainer::readyWrite(int what)
370 if (what&eSocketNotifier::Write && outbuf.size() )
372 queue_data d = outbuf.front();
374 if ( ::write( fd[1], d.data, d.len ) != d.len )
376 /* emit */ dataSent(-1);
377 // eDebug("writeError");
381 /* emit */ dataSent(0);
382 // eDebug("write ok");
386 if ( !outbuf.size() )