servicemp3.cpp: more simple/flexible streaming detection
[enigma2.git] / lib / base / console.cpp
index 2609582f3b152c344f90265a67552ebc137165bb..00187683258be0e8348e1d7ad9e07ff074504915 100644 (file)
@@ -1,34 +1,15 @@
-/*
- * console.cpp
- *
- * Copyright (C) 2002 Felix Domke <tmbinc@tuxbox.org>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- *
- * $Id: console.cpp,v 1.1 2003-10-17 15:35:47 tmbinc Exp $
- */
-
 #include <lib/base/console.h>
-
-#include <lib/base/estring.h>
+#include <lib/base/eerror.h>
 #include <sys/vfs.h> // for statfs
 #include <unistd.h>
 #include <signal.h>
 #include <errno.h>
+#include <poll.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <fcntl.h>
 
-int bidirpipe(int pfd[], char *cmd , char *argv[])
+int bidirpipe(int pfd[], const char *cmd , const char * const argv[], const char *cwd )
 {
        int pfdin[2];  /* from child to parent */
        int pfdout[2]; /* from parent to child */
@@ -38,10 +19,11 @@ int bidirpipe(int pfd[], char *cmd , char *argv[])
        if ( pipe(pfdin) == -1 || pipe(pfdout) == -1 || pipe(pfderr) == -1)
                return(-1);
 
-       if ( ( pid = fork() ) == -1 )
+       if ( ( pid = vfork() ) == -1 )
                return(-1);
        else if (pid == 0) /* child process */
        {
+               setsid();
                if ( close(0) == -1 || close(1) == -1 || close(2) == -1 )
                        _exit(0);
 
@@ -53,7 +35,14 @@ int bidirpipe(int pfd[], char *cmd , char *argv[])
                                close(pfderr[0]) == -1 || close(pfderr[1]) == -1 )
                        _exit(0);
 
-               execv(cmd,argv);
+               for (unsigned int i=3; i < 90; ++i )
+                       close(i);
+
+               if (cwd)
+                       chdir(cwd);
+
+               execvp(cmd, (char * const *)argv); 
+                               /* the vfork will actually suspend the parent thread until execvp is called. thus it's ok to use the shared arg/cmdline pointers here. */
                _exit(0);
        }
        if (close(pfdout[0]) == -1 || close(pfdin[1]) == -1 || close(pfderr[1]) == -1)
@@ -66,183 +55,672 @@ int bidirpipe(int pfd[], char *cmd , char *argv[])
        return(pid);
 }
 
-eConsoleAppContainer::eConsoleAppContainer( const eString &cmd )
-:pid(-1), killstate(0), outbuf(0)
+DEFINE_REF(eConsoleAppContainer);
+
+eConsoleAppContainer::eConsoleAppContainer()
+:pid(-1), killstate(0)
 {
-//     eDebug("cmd = %s", cmd.c_str() );
-       memset(fd, 0, sizeof(fd) );
-       int cnt=2; // path to app + terminated 0
-       eString str(cmd?cmd:"");
+       for (int i=0; i < 3; ++i)
+       {
+               fd[i]=-1;
+               filefd[i]=-1;
+       }
+}
 
-       while( str.length() && str[0] == ' ' )  // kill spaces at beginning
-               str = str.mid(1);
+int eConsoleAppContainer::setCWD( const char *path )
+{
+       struct stat dir_stat;
 
-       while( str.length() && str[str.length()-1] == ' ' )  // kill spaces at the end
-               str = str.left( str.length() - 1 );
+       if (stat(path, &dir_stat) == -1)
+               return -1;
 
-       if (!str.length())
-               return;
+       if (!S_ISDIR(dir_stat.st_mode))
+               return -2;
+
+       m_cwd = path;
+       return 0;
+}
+
+int eConsoleAppContainer::execute( const char *cmd )
+{
+       int argc = 3;
+       const char *argv[argc + 1];
+       argv[0] = "/bin/sh";
+       argv[1] = "-c";
+       argv[2] = cmd;
+       argv[argc] = NULL;
+
+       return execute(argv[0], argv);
+}
+
+int eConsoleAppContainer::execute(const char *cmdline, const char * const argv[])
+{
+       if (running())
+               return -1;
 
-       unsigned int idx=0;
-       eString path = str.left( (idx = str.find(' ')) != eString::npos ? idx : str.length() );
-//     eDebug("path = %s", path.c_str() );
+       pid=-1;
+       killstate=0;
 
-       eString cmds = str.mid( path.length()+1 );
-//     eDebug("cmds = %s", cmds.c_str() );
+       // get one read ,one write and the err pipe to the prog..
+       pid = bidirpipe(fd, cmdline, argv, m_cwd.length() ? m_cwd.c_str() : 0);
 
-       idx = 0;
-       while ( (idx = cmds.find(' ',idx) ) != eString::npos )  // count args
+       if ( pid == -1 )
+               return -3;
+
+//     eDebug("pipe in = %d, out = %d, err = %d", fd[0], fd[1], fd[2]);
+
+       ::fcntl(fd[0], F_SETFL, O_NONBLOCK);
+       ::fcntl(fd[1], F_SETFL, O_NONBLOCK);
+       ::fcntl(fd[2], F_SETFL, O_NONBLOCK);
+       in = eSocketNotifier::create(eApp, fd[0], eSocketNotifier::Read|eSocketNotifier::Priority|eSocketNotifier::Hungup );
+       out = eSocketNotifier::create(eApp, fd[1], eSocketNotifier::Write, false);  
+       err = eSocketNotifier::create(eApp, fd[2], eSocketNotifier::Read|eSocketNotifier::Priority );
+       CONNECT(in->activated, eConsoleAppContainer::readyRead);
+       CONNECT(out->activated, eConsoleAppContainer::readyWrite);
+       CONNECT(err->activated, eConsoleAppContainer::readyErrRead);
+       in->m_clients.push_back(this);
+       out->m_clients.push_back(this);
+       err->m_clients.push_back(this);
+
+       return 0;
+}
+
+eConsoleAppContainer::~eConsoleAppContainer()
+{
+       kill();
+}
+
+void eConsoleAppContainer::kill()
+{
+       if ( killstate != -1 && pid != -1 )
        {
-               cnt++;
-               idx++;
+               eDebug("user kill(SIGKILL) console App");
+               killstate=-1;
+               /*
+                * Use a negative pid value, to signal the whole process group
+                * ('pid' might not even be running anymore at this point)
+                */
+               ::kill(-pid, SIGKILL);
+               closePipes();
        }
+       while( outbuf.size() ) // cleanup out buffer
+       {
+               queue_data d = outbuf.front();
+               outbuf.pop();
+               delete [] d.data;
+       }
+       in = 0;
+       out = 0;
+       err = 0;
 
-//     eDebug("idx = %d, %d counted spaces", idx, cnt-2);
+       for (int i=0; i < 3; ++i)
+       {
+               if ( filefd[i] > 0 )
+                       close(filefd[i]);
+       }
+}
 
-       if ( cmds.length() )
+void eConsoleAppContainer::sendCtrlC()
+{
+       if ( killstate != -1 && pid != -1 )
        {
-               cnt++;
-//             eDebug("increase cnt");
+               eDebug("user send SIGINT(Ctrl-C) to console App");
+               /*
+                * Use a negative pid value, to signal the whole process group
+                * ('pid' might not even be running anymore at this point)
+                */
+               ::kill(-pid, SIGINT);
        }
+}
 
-//     eDebug("%d args", cnt-2);
-       char **argv = new char*[cnt];  // min two args... path and terminating 0
-       argv[0] = new char[ path.length() ];
-       strcpy( argv[0], path.c_str() );
-       argv[cnt-1] = 0;               // set terminating null
+void eConsoleAppContainer::sendEOF()
+{
+       if (out)
+               out->stop();
+       if (fd[1] != -1)
+       {
+               ::close(fd[1]);
+               fd[1]=-1;
+       }
+}
 
-       if ( cnt > 2 )  // more then default args?
+void eConsoleAppContainer::closePipes()
+{
+       if (in)
+               in->stop();
+       if (out)
+               out->stop();
+       if (err)
+               err->stop();
+       if (fd[0] != -1)
        {
-               cnt=1;  // do not overwrite path in argv[0]
+               ::close(fd[0]);
+               fd[0]=-1;
+       }
+       if (fd[1] != -1)
+       {
+               ::close(fd[1]);
+               fd[1]=-1;
+       }
+       if (fd[2] != -1)
+       {
+               ::close(fd[2]);
+               fd[2]=-1;
+       }
+       eDebug("pipes closed");
+       while( outbuf.size() ) // cleanup out buffer
+       {
+               queue_data d = outbuf.front();
+               outbuf.pop();
+               delete [] d.data;
+       }
+       in = 0; out = 0; err = 0;
+       pid = -1;
+}
 
-               while ( (idx = cmds.find(' ')) != eString::npos )  // parse all args..
+void eConsoleAppContainer::readyRead(int what)
+{
+       bool hungup = what & eSocketNotifier::Hungup;
+       if (what & (eSocketNotifier::Priority|eSocketNotifier::Read))
+       {
+//             eDebug("what = %d");
+               char buf[2049];
+               int rd;
+               while((rd = read(fd[0], buf, 2048)) > 0)
+               {
+                       buf[rd]=0;
+                       /*emit*/ dataAvail(buf);
+                       stdoutAvail(buf);
+                       if ( filefd[1] > 0 )
+                               ::write(filefd[1], buf, rd);
+                       if (!hungup)
+                               break;
+               }
+       }
+       readyErrRead(eSocketNotifier::Priority|eSocketNotifier::Read); /* be sure to flush all data which might be already written */
+       if (hungup)
+       {
+               eDebug("child has terminated");
+               closePipes();
+               int childstatus;
+               int retval = killstate;
+               /*
+                * We have to call 'wait' on the child process, in order to avoid zombies.
+                * Also, this gives us the chance to provide better exit status info to appClosed.
+                */
+               if (::waitpid(pid, &childstatus, 0) > 0)
                {
-                       argv[cnt] = new char[ idx ];
-//                     eDebug("idx=%d, arg = %s", idx, cmds.left(idx).c_str() );
-                       strcpy( argv[cnt++], cmds.left( idx ).c_str() );
-                       cmds = cmds.mid(idx+1);
-//                     eDebug("str = %s", cmds.c_str() );
+                       if (WIFEXITED(childstatus))
+                       {
+                               retval = WEXITSTATUS(childstatus);
+                       }
                }
-               // store the last arg
-               argv[cnt] = new char[ cmds.length() ];
-               strcpy( argv[cnt], cmds.c_str() );
+               /*emit*/ appClosed(retval);
        }
+}
 
-  // get one read ,one write and the err pipe to the prog..
-  
-       if ( (pid = bidirpipe(fd, argv[0], argv)) == -1 )
+void eConsoleAppContainer::readyErrRead(int what)
+{
+       if (what & (eSocketNotifier::Priority|eSocketNotifier::Read))
        {
-               while ( cnt-- > 0 )
-                       delete [] argv[cnt];
-               delete [] argv;
-               return;
+//             eDebug("what = %d");
+               char buf[2049];
+               int rd;
+               while((rd = read(fd[2], buf, 2048)) > 0)
+               {
+/*                     for ( int i = 0; i < rd; i++ )
+                               eDebug("%d = %c (%02x)", i, buf[i], buf[i] );*/
+                       buf[rd]=0;
+                       /*emit*/ dataAvail(buf);
+                       stderrAvail(buf);
+               }
        }
+}
 
-       while ( cnt-- > 0 )  // release heap memory
-               delete [] argv[cnt];
-       delete [] argv;
+void eConsoleAppContainer::write( const char *data, int len )
+{
+       char *tmp = new char[len];
+       memcpy(tmp, data, len);
+       outbuf.push(queue_data(tmp,len));
+       if (out)
+               out->start();
+}
 
-       eDebug("pipe in = %d, out = %d, err = %d", fd[0], fd[1], fd[2]);
+void eConsoleAppContainer::readyWrite(int what)
+{
+       if (what&eSocketNotifier::Write && outbuf.size() )
+       {
+               queue_data &d = outbuf.front();
+               int wr = ::write( fd[1], d.data+d.dataSent, d.len-d.dataSent );
+               if (wr < 0)
+                       eDebug("eConsoleAppContainer write failed (%m)");
+               else
+                       d.dataSent += wr;
+               if (d.dataSent == d.len)
+               {
+                       outbuf.pop();
+                       delete [] d.data;
+                       if ( filefd[0] == -1 )
+                       /* emit */ dataSent(0);
+               }
+       }
+       if ( !outbuf.size() )
+       {
+               if ( filefd[0] > 0 )
+               {
+                       char readbuf[32*1024];
+                       int rsize = read(filefd[0], readbuf, 32*1024);
+                       if ( rsize > 0 )
+                               write(readbuf, rsize);
+                       else
+                       {
+                               close(filefd[0]);
+                               filefd[0] = -1;
+                               ::close(fd[1]);
+                               eDebug("readFromFile done - closing eConsoleAppContainer stdin pipe");
+                               fd[1]=-1;
+                               dataSent(0);
+                               out->stop();
+                       }
+               }
+               else
+                       out->stop();
+       }
+}
 
-       in = new eSocketNotifier(eApp, fd[0], 19 );  // 19 = POLLIN, POLLPRI, POLLHUP
-       out = new eSocketNotifier(eApp, fd[1], eSocketNotifier::Write);  // POLLOUT
-       err = new eSocketNotifier(eApp, fd[2], 19 );  // 19 = POLLIN, POLLPRI, POLLHUP
-       CONNECT(in->activated, eConsoleAppContainer::readyRead);
-       CONNECT(out->activated, eConsoleAppContainer::readyWrite);
-       CONNECT(err->activated, eConsoleAppContainer::readyErrRead);
-       signal(SIGCHLD, SIG_IGN);   // no zombie when child killed
+#include "structmember.h"
+
+extern "C" {
+
+struct eConsolePy
+{
+       PyObject_HEAD
+       eConsoleAppContainer *cont;
+       PyObject *in_weakreflist; /* List of weak references */
+};
+
+static PyObject *
+eConsolePy_dataAvail(eConsolePy *self, void *closure)
+{
+       return self->cont->dataAvail.get();
 }
 
-eConsoleAppContainer::~eConsoleAppContainer()
+static PyObject *
+eConsolePy_stdoutAvail(eConsolePy *self, void *closure)
 {
-       if ( running() )
-       {
-               killstate=-1;
-               kill();
+       return self->cont->stdoutAvail.get();
+}
+
+static PyObject *
+eConsolePy_stderrAvail(eConsolePy *self, void *closure)
+{
+       return self->cont->stderrAvail.get();
+}
+
+static PyObject *
+eConsolePy_dataSent(eConsolePy *self, void *closure)
+{
+       return self->cont->dataSent.get();
+}
+
+static PyObject *
+eConsolePy_appClosed(eConsolePy *self, void *closure)
+{
+       return self->cont->appClosed.get();
+}
+
+static PyGetSetDef eConsolePy_getseters[] = {
+       {"dataAvail",
+        (getter)eConsolePy_dataAvail, (setter)0,
+        "dataAvail callback list",
+        NULL},
+       {"stdoutAvail",
+        (getter)eConsolePy_stdoutAvail, (setter)0,
+        "stdoutAvail callback list",
+        NULL},
+       {"stderrAvail",
+        (getter)eConsolePy_stderrAvail, (setter)0,
+        "stderrAvail callback list",
+        NULL},
+       {"dataSent",
+        (getter)eConsolePy_dataSent, (setter)0,
+        "dataSent callback list",
+        NULL},
+       {"appClosed",
+        (getter)eConsolePy_appClosed, (setter)0,
+        "appClosed callback list",
+        NULL},
+       {NULL} /* Sentinel */
+};
+
+static int
+eConsolePy_traverse(eConsolePy *self, visitproc visit, void *arg)
+{
+       PyObject *obj = self->cont->dataAvail.getSteal();
+       if (obj) {
+               Py_VISIT(obj);
+       }
+       obj = self->cont->stdoutAvail.getSteal();
+       if (obj) {
+               Py_VISIT(obj);
+       }
+       obj = self->cont->stderrAvail.getSteal();
+       if (obj) {
+               Py_VISIT(obj);
        }
-       if ( outbuf )
-               delete [] outbuf;
+       obj = self->cont->dataSent.getSteal();
+       if (obj) {
+               Py_VISIT(obj);
+       }
+       obj = self->cont->appClosed.getSteal();
+       if (obj) {
+               Py_VISIT(obj);
+       }
+       return 0;
 }
 
-void eConsoleAppContainer::kill()
+static int
+eConsolePy_clear(eConsolePy *self)
 {
-       killstate=-1;
-       system( eString().sprintf("kill %d", pid).c_str() );
-       eDebug("user kill console App");
+       PyObject *obj = self->cont->dataAvail.getSteal(true);
+       if (obj) {
+               Py_CLEAR(obj);
+       }
+       obj = self->cont->stdoutAvail.getSteal(true);
+       if (obj) {
+               Py_CLEAR(obj);
+       }
+       obj = self->cont->stderrAvail.getSteal(true);
+       if (obj) {
+               Py_CLEAR(obj);
+       }
+       obj = self->cont->dataSent.getSteal(true);
+       if (obj) {
+               Py_CLEAR(obj);
+       }
+       obj = self->cont->appClosed.getSteal(true);
+       if (obj) {
+               Py_CLEAR(obj);
+       }
+       return 0;
 }
 
-void eConsoleAppContainer::closePipes()
+static void
+eConsolePy_dealloc(eConsolePy* self)
 {
-       in->stop();
-       out->stop();
-       err->stop();
-       ::close(fd[0]);
-       fd[0]=0;
-       ::close(fd[1]);
-       fd[1]=0;
-       ::close(fd[2]);
-       fd[2]=0;
-       eDebug("pipes closed");
+       if (self->in_weakreflist != NULL)
+               PyObject_ClearWeakRefs((PyObject *) self);
+       eConsolePy_clear(self);
+       self->cont->Release();
+       self->ob_type->tp_free((PyObject*)self);
 }
 
-void eConsoleAppContainer::readyRead(int what)
+static PyObject *
+eConsolePy_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
+{
+       eConsolePy *self = (eConsolePy *)type->tp_alloc(type, 0);
+       self->cont = new eConsoleAppContainer();
+       self->cont->AddRef();
+       self->in_weakreflist = NULL;
+       return (PyObject *)self;
+}
+
+static PyObject *
+eConsolePy_running(eConsolePy* self)
+{
+       PyObject *ret = NULL;
+       ret = self->cont->running() ? Py_True : Py_False;
+       Org_Py_INCREF(ret);
+       return ret;
+}
+
+static PyObject *
+eConsolePy_execute(eConsolePy* self, PyObject *argt)
 {
-       if (what & POLLPRI|POLLIN)
+       Py_ssize_t argc = PyTuple_Size(argt);
+       if (argc > 1)
        {
-               eDebug("what = %d");
-               char buf[2048];
-               int readed = read(fd[0], buf, 2048);
-               eDebug("%d bytes read", readed);
-               if ( readed != -1 && readed )
-                       /*emit*/ dataAvail( eString( buf ) );
-               else if (readed == -1)
-                       eDebug("readerror %d", errno);
+               const char *argv[argc + 1];
+               int argpos=0;
+               while(argpos < argc)
+               {
+                       PyObject *arg = PyTuple_GET_ITEM(argt, argpos);
+                       if (!PyString_Check(arg))
+                       {
+                               char err[255];
+                               if (argpos)
+                                       snprintf(err, 255, "arg %d is not a string", argpos);
+                               else
+                                       snprintf(err, 255, "cmd is not a string!");
+                               PyErr_SetString(PyExc_TypeError, err);
+                               return NULL;
+                       }
+                       argv[argpos++] = PyString_AsString(arg);
+               }
+               argv[argpos] = 0;
+               return PyInt_FromLong(self->cont->execute(argv[0], argv+1));
        }
-       if (what & eSocketNotifier::Hungup)
+       else
        {
-               eDebug("child has terminated");
-               closePipes();
-               /*emit*/ appClosed(killstate);
+               const char *str;
+               if (PyArg_ParseTuple(argt, "s", &str))
+                       return PyInt_FromLong(self->cont->execute(str));
+               PyErr_SetString(PyExc_TypeError,
+                       "cmd is not a string!");
        }
+       return NULL;
 }
 
-void eConsoleAppContainer::readyErrRead(int what)
+static PyObject *
+eConsolePy_write(eConsolePy* self, PyObject *args)
 {
-       if (what & POLLPRI|POLLIN)
+       int len;
+       char *data;
+       int ret = -1;
+       Py_ssize_t argc = PyTuple_Size(args);
+       if (argc > 1)
+               ret = !PyArg_ParseTuple(args, "si", &data, &len);
+       else if (argc == 1)
+       {
+               PyObject *ob;
+               ret = !PyArg_ParseTuple(args, "O", &ob) || !PyString_Check(ob);
+               if (!ret)
+               {
+                       Py_ssize_t length;
+                       if (!PyString_AsStringAndSize(ob, &data, &length))
+                               len = length;
+                       else
+                               len = 0;
+               }
+       }
+       if (ret)
        {
-               eDebug("what = %d");
-               char buf[2048];
-               int readed = read(fd[2], buf, 2048);
-               eDebug("%d bytes read", readed);
-               if ( readed != -1 && readed )
-                       /*emit*/ dataAvail( eString( buf ) );
-               else if (readed == -1)
-                       eDebug("readerror %d", errno);
+               PyErr_SetString(PyExc_TypeError,
+                       "1st arg must be a string, optionaly 2nd arg can be the string length");
+               return NULL;
        }
+       self->cont->write(data, len);
+       Py_RETURN_NONE;
 }
 
-void eConsoleAppContainer::write( const eString & str )
+static PyObject *
+eConsolePy_getPID(eConsolePy* self)
 {
-       outbuf = new char[ str.length()];
-       strcpy( outbuf, str.c_str() );
+       return PyInt_FromLong(self->cont->getPID());
 }
 
-void eConsoleAppContainer::readyWrite(int what)
+static PyObject *
+eConsolePy_setCWD(eConsolePy* self, PyObject *args)
+{
+       const char *path=0;
+       if (!PyArg_ParseTuple(args, "s", &path))
+               return NULL;
+       self->cont->setCWD(path);
+       Py_RETURN_NONE;
+}
+
+static PyObject *
+eConsolePy_kill(eConsolePy* self)
+{
+       self->cont->kill();
+       Py_RETURN_NONE;
+}
+
+static PyObject *
+eConsolePy_sendCtrlC(eConsolePy* self)
+{
+       self->cont->sendCtrlC();
+       Py_RETURN_NONE;
+}
+
+static PyObject *
+eConsolePy_sendEOF(eConsolePy* self)
 {
-       if (what == 4 && outbuf)
+       self->cont->sendEOF();
+       Py_RETURN_NONE;
+}
+
+static PyObject *
+eConsolePy_dumpToFile(eConsolePy* self, PyObject *args)
+{
+       char *filename;
+       if (!PyArg_ParseTuple(args, "s", &filename))
        {
-               if ( ::write( fd[1], outbuf, strlen(outbuf) ) != (int) strlen(outbuf) )
+               PyErr_SetString(PyExc_TypeError,
+                       "arg must be a string (filename)");
+               return NULL;
+       }
+       else
+       {
+               int fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0644);
+               self->cont->setFileFD(1, fd);
+               eDebug("eConsoleAppContainer::dumpToFile open(%s, O_WRONLY|O_CREAT|O_TRUNC, 0644)=%d", filename, fd);
+       }
+       Py_RETURN_NONE;
+}
+
+static PyObject *
+eConsolePy_readFromFile(eConsolePy* self, PyObject *args)
+{
+       char *filename;
+       if (!PyArg_ParseTuple(args, "s", &filename))
+       {
+               PyErr_SetString(PyExc_TypeError,
+                       "arg must be a string (filename)");
+               return NULL;
+       }
+       else
+       {
+               int fd = open(filename, O_RDONLY);
+               if (fd >= 0)
                {
-                       /* emit */ dataSent(-1);
-                       eDebug("writeError");
+                       char readbuf[32*1024];
+                       int rsize = read(fd, readbuf, 32*1024);
+                       self->cont->setFileFD(0, fd);
+                       eDebug("eConsoleAppContainer::readFromFile open(%s, O_RDONLY)=%d, read: %d", filename, fd, rsize);
+                       self->cont->write(readbuf, rsize);
                }
                else
                {
-                       /* emit */ dataSent(0);
-                       eDebug("write ok");
+                       eDebug("eConsoleAppContainer::readFromFile %s not exist!", filename);
+                       self->cont->setFileFD(0, -1);
                }
+       }
+       Py_RETURN_NONE;
+}
+
+static PyMethodDef eConsolePy_methods[] = {
+       {"setCWD", (PyCFunction)eConsolePy_setCWD, METH_VARARGS,
+        "set working dir"
+       },
+       {"execute", (PyCFunction)eConsolePy_execute, METH_VARARGS,
+        "execute command"
+       },
+       {"dumpToFile", (PyCFunction)eConsolePy_dumpToFile, METH_VARARGS,
+        "set output file"
+       },
+       {"readFromFile", (PyCFunction)eConsolePy_readFromFile, METH_VARARGS,
+        "set input file"
+       },
+       {"getPID", (PyCFunction)eConsolePy_getPID, METH_NOARGS,
+        "execute command"
+       },
+       {"kill", (PyCFunction)eConsolePy_kill, METH_NOARGS,
+        "kill application"
+       },
+       {"sendCtrlC", (PyCFunction)eConsolePy_sendCtrlC, METH_NOARGS,
+        "send Ctrl-C to application"
+       },
+       {"sendEOF", (PyCFunction)eConsolePy_sendEOF, METH_NOARGS,
+        "send EOF to application"
+       },
+       {"write", (PyCFunction)eConsolePy_write, METH_VARARGS,
+        "write data to application"
+       },
+       {"running", (PyCFunction)eConsolePy_running, METH_NOARGS,
+        "returns the running state"
+       },
+       {NULL}  /* Sentinel */
+};
+
+static PyTypeObject eConsolePyType = {
+       PyObject_HEAD_INIT(NULL)
+       0, /*ob_size*/
+       "eConsoleImpl.eConsoleAppContainer", /*tp_name*/
+       sizeof(eConsolePy), /*tp_basicsize*/
+       0, /*tp_itemsize*/
+       (destructor)eConsolePy_dealloc, /*tp_dealloc*/
+       0, /*tp_print*/
+       0, /*tp_getattr*/
+       0, /*tp_setattr*/
+       0, /*tp_compare*/
+       0, /*tp_repr*/
+       0, /*tp_as_number*/
+       0, /*tp_as_sequence*/
+       0, /*tp_as_mapping*/
+       0, /*tp_hash */
+       0, /*tp_call*/
+       0, /*tp_str*/
+       0, /*tp_getattro*/
+       0, /*tp_setattro*/
+       0, /*tp_as_buffer*/
+       Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
+       "eConsoleAppContainer objects", /* tp_doc */
+       (traverseproc)eConsolePy_traverse, /* tp_traverse */
+       (inquiry)eConsolePy_clear, /* tp_clear */
+       0, /* tp_richcompare */
+       offsetof(eConsolePy, in_weakreflist), /* tp_weaklistoffset */
+       0, /* tp_iter */
+       0, /* tp_iternext */
+       eConsolePy_methods, /* tp_methods */
+       0, /* tp_members */
+       eConsolePy_getseters, /* tp_getset */
+       0, /* tp_base */
+       0, /* tp_dict */
+       0, /* tp_descr_get */
+       0, /* tp_descr_set */
+       0, /* tp_dictoffset */
+       0, /* tp_init */
+       0, /* tp_alloc */
+       eConsolePy_new, /* tp_new */
+};
+
+static PyMethodDef module_methods[] = {
+       {NULL}  /* Sentinel */
+};
+
+void eConsoleInit(void)
+{
+       PyObject* m = Py_InitModule3("eConsoleImpl", module_methods,
+               "Module that implements eConsoleAppContainer with working cyclic garbage collection.");
+
+       if (m == NULL)
+               return;
 
-               delete outbuf;
-               outbuf=0;
+       if (!PyType_Ready(&eConsolePyType))
+       {
+               Org_Py_INCREF((PyObject*)&eConsolePyType);
+               PyModule_AddObject(m, "eConsoleAppContainer", (PyObject*)&eConsolePyType);
        }
 }
+}