Merge branch 'bug_411_timeshift_disable_without_live' into experimental
[enigma2.git] / lib / base / console.cpp
1 #include <lib/base/console.h>
2 #include <lib/base/eerror.h>
3 #include <sys/vfs.h> // for statfs
4 #include <unistd.h>
5 #include <signal.h>
6 #include <errno.h>
7 #include <poll.h>
8 #include <sys/types.h>
9 #include <sys/wait.h>
10 #include <fcntl.h>
11
12 int bidirpipe(int pfd[], const char *cmd , const char * const argv[], const char *cwd )
13 {
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 */
18
19         if ( pipe(pfdin) == -1 || pipe(pfdout) == -1 || pipe(pfderr) == -1)
20                 return(-1);
21
22         if ( ( pid = vfork() ) == -1 )
23                 return(-1);
24         else if (pid == 0) /* child process */
25         {
26                 setsid();
27                 if ( close(0) == -1 || close(1) == -1 || close(2) == -1 )
28                         _exit(0);
29
30                 if (dup(pfdout[0]) != 0 || dup(pfdin[1]) != 1 || dup(pfderr[1]) != 2 )
31                         _exit(0);
32
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 )
36                         _exit(0);
37
38                 for (unsigned int i=3; i < 90; ++i )
39                         close(i);
40
41                 if (cwd)
42                         chdir(cwd);
43
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. */
46                 _exit(0);
47         }
48         if (close(pfdout[0]) == -1 || close(pfdin[1]) == -1 || close(pfderr[1]) == -1)
49                         return(-1);
50
51         pfd[0] = pfdin[0];
52         pfd[1] = pfdout[1];
53         pfd[2] = pfderr[0];
54
55         return(pid);
56 }
57
58 DEFINE_REF(eConsoleAppContainer);
59
60 eConsoleAppContainer::eConsoleAppContainer()
61 :pid(-1), killstate(0)
62 {
63         for (int i=0; i < 3; ++i)
64         {
65                 fd[i]=-1;
66                 filefd[i]=-1;
67         }
68 }
69
70 int eConsoleAppContainer::setCWD( const char *path )
71 {
72         struct stat dir_stat;
73
74         if (stat(path, &dir_stat) == -1)
75                 return -1;
76
77         if (!S_ISDIR(dir_stat.st_mode))
78                 return -2;
79
80         m_cwd = path;
81         return 0;
82 }
83
84 int eConsoleAppContainer::execute( const char *cmd )
85 {
86         int argc = 3;
87         const char *argv[argc + 1];
88         argv[0] = "/bin/sh";
89         argv[1] = "-c";
90         argv[2] = cmd;
91         argv[argc] = NULL;
92
93         return execute(argv[0], argv);
94 }
95
96 int eConsoleAppContainer::execute(const char *cmdline, const char * const argv[])
97 {
98         if (running())
99                 return -1;
100
101         pid=-1;
102         killstate=0;
103
104         // get one read ,one write and the err pipe to the prog..
105         pid = bidirpipe(fd, cmdline, argv, m_cwd.length() ? m_cwd.c_str() : 0);
106
107         if ( pid == -1 )
108                 return -3;
109
110 //      eDebug("pipe in = %d, out = %d, err = %d", fd[0], fd[1], fd[2]);
111
112         ::fcntl(fd[0], F_SETFL, O_NONBLOCK);
113         ::fcntl(fd[1], F_SETFL, O_NONBLOCK);
114         ::fcntl(fd[2], F_SETFL, O_NONBLOCK);
115         in = eSocketNotifier::create(eApp, fd[0], eSocketNotifier::Read|eSocketNotifier::Priority|eSocketNotifier::Hungup );
116         out = eSocketNotifier::create(eApp, fd[1], eSocketNotifier::Write, false);  
117         err = eSocketNotifier::create(eApp, fd[2], eSocketNotifier::Read|eSocketNotifier::Priority );
118         CONNECT(in->activated, eConsoleAppContainer::readyRead);
119         CONNECT(out->activated, eConsoleAppContainer::readyWrite);
120         CONNECT(err->activated, eConsoleAppContainer::readyErrRead);
121         in->m_clients.push_back(this);
122         out->m_clients.push_back(this);
123         err->m_clients.push_back(this);
124
125         return 0;
126 }
127
128 eConsoleAppContainer::~eConsoleAppContainer()
129 {
130         kill();
131 }
132
133 void eConsoleAppContainer::kill()
134 {
135         if ( killstate != -1 && pid != -1 )
136         {
137                 eDebug("user kill(SIGKILL) console App");
138                 killstate=-1;
139                 /*
140                  * Use a negative pid value, to signal the whole process group
141                  * ('pid' might not even be running anymore at this point)
142                  */
143                 ::kill(-pid, SIGKILL);
144                 closePipes();
145         }
146         while( outbuf.size() ) // cleanup out buffer
147         {
148                 queue_data d = outbuf.front();
149                 outbuf.pop();
150                 delete [] d.data;
151         }
152         in = 0;
153         out = 0;
154         err = 0;
155
156         for (int i=0; i < 3; ++i)
157         {
158                 if ( filefd[i] > 0 )
159                         close(filefd[i]);
160         }
161 }
162
163 void eConsoleAppContainer::sendCtrlC()
164 {
165         if ( killstate != -1 && pid != -1 )
166         {
167                 eDebug("user send SIGINT(Ctrl-C) to console App");
168                 /*
169                  * Use a negative pid value, to signal the whole process group
170                  * ('pid' might not even be running anymore at this point)
171                  */
172                 ::kill(-pid, SIGINT);
173         }
174 }
175
176 void eConsoleAppContainer::sendEOF()
177 {
178         if (out)
179                 out->stop();
180         if (fd[1] != -1)
181         {
182                 ::close(fd[1]);
183                 fd[1]=-1;
184         }
185 }
186
187 void eConsoleAppContainer::closePipes()
188 {
189         if (in)
190                 in->stop();
191         if (out)
192                 out->stop();
193         if (err)
194                 err->stop();
195         if (fd[0] != -1)
196         {
197                 ::close(fd[0]);
198                 fd[0]=-1;
199         }
200         if (fd[1] != -1)
201         {
202                 ::close(fd[1]);
203                 fd[1]=-1;
204         }
205         if (fd[2] != -1)
206         {
207                 ::close(fd[2]);
208                 fd[2]=-1;
209         }
210         eDebug("pipes closed");
211         while( outbuf.size() ) // cleanup out buffer
212         {
213                 queue_data d = outbuf.front();
214                 outbuf.pop();
215                 delete [] d.data;
216         }
217         in = 0; out = 0; err = 0;
218         pid = -1;
219 }
220
221 void eConsoleAppContainer::readyRead(int what)
222 {
223         bool hungup = what & eSocketNotifier::Hungup;
224         if (what & (eSocketNotifier::Priority|eSocketNotifier::Read))
225         {
226 //              eDebug("what = %d");
227                 char buf[2049];
228                 int rd;
229                 while((rd = read(fd[0], buf, 2048)) > 0)
230                 {
231                         buf[rd]=0;
232                         /*emit*/ dataAvail(buf);
233                         stdoutAvail(buf);
234                         if ( filefd[1] > 0 )
235                                 ::write(filefd[1], buf, rd);
236                         if (!hungup)
237                                 break;
238                 }
239         }
240         readyErrRead(eSocketNotifier::Priority|eSocketNotifier::Read); /* be sure to flush all data which might be already written */
241         if (hungup)
242         {
243                 eDebug("child has terminated");
244                 closePipes();
245                 int childstatus;
246                 int retval = killstate;
247                 /*
248                  * We have to call 'wait' on the child process, in order to avoid zombies.
249                  * Also, this gives us the chance to provide better exit status info to appClosed.
250                  */
251                 if (::waitpid(pid, &childstatus, 0) > 0)
252                 {
253                         if (WIFEXITED(childstatus))
254                         {
255                                 retval = WEXITSTATUS(childstatus);
256                         }
257                 }
258                 /*emit*/ appClosed(retval);
259         }
260 }
261
262 void eConsoleAppContainer::readyErrRead(int what)
263 {
264         if (what & (eSocketNotifier::Priority|eSocketNotifier::Read))
265         {
266 //              eDebug("what = %d");
267                 char buf[2049];
268                 int rd;
269                 while((rd = read(fd[2], buf, 2048)) > 0)
270                 {
271 /*                      for ( int i = 0; i < rd; i++ )
272                                 eDebug("%d = %c (%02x)", i, buf[i], buf[i] );*/
273                         buf[rd]=0;
274                         /*emit*/ dataAvail(buf);
275                         stderrAvail(buf);
276                 }
277         }
278 }
279
280 void eConsoleAppContainer::write( const char *data, int len )
281 {
282         char *tmp = new char[len];
283         memcpy(tmp, data, len);
284         outbuf.push(queue_data(tmp,len));
285         if (out)
286                 out->start();
287 }
288
289 void eConsoleAppContainer::readyWrite(int what)
290 {
291         if (what&eSocketNotifier::Write && outbuf.size() )
292         {
293                 queue_data &d = outbuf.front();
294                 int wr = ::write( fd[1], d.data+d.dataSent, d.len-d.dataSent );
295                 if (wr < 0)
296                         eDebug("eConsoleAppContainer write failed (%m)");
297                 else
298                         d.dataSent += wr;
299                 if (d.dataSent == d.len)
300                 {
301                         outbuf.pop();
302                         delete [] d.data;
303                         if ( filefd[0] == -1 )
304                         /* emit */ dataSent(0);
305                 }
306         }
307         if ( !outbuf.size() )
308         {
309                 if ( filefd[0] > 0 )
310                 {
311                         char readbuf[32*1024];
312                         int rsize = read(filefd[0], readbuf, 32*1024);
313                         if ( rsize > 0 )
314                                 write(readbuf, rsize);
315                         else
316                         {
317                                 close(filefd[0]);
318                                 filefd[0] = -1;
319                                 ::close(fd[1]);
320                                 eDebug("readFromFile done - closing eConsoleAppContainer stdin pipe");
321                                 fd[1]=-1;
322                                 dataSent(0);
323                                 out->stop();
324                         }
325                 }
326                 else
327                         out->stop();
328         }
329 }
330
331 #include "structmember.h"
332
333 extern "C" {
334
335 struct eConsolePy
336 {
337         PyObject_HEAD
338         eConsoleAppContainer *cont;
339         PyObject *in_weakreflist; /* List of weak references */
340 };
341
342 static PyObject *
343 eConsolePy_dataAvail(eConsolePy *self, void *closure)
344 {
345         return self->cont->dataAvail.get();
346 }
347
348 static PyObject *
349 eConsolePy_stdoutAvail(eConsolePy *self, void *closure)
350 {
351         return self->cont->stdoutAvail.get();
352 }
353
354 static PyObject *
355 eConsolePy_stderrAvail(eConsolePy *self, void *closure)
356 {
357         return self->cont->stderrAvail.get();
358 }
359
360 static PyObject *
361 eConsolePy_dataSent(eConsolePy *self, void *closure)
362 {
363         return self->cont->dataSent.get();
364 }
365
366 static PyObject *
367 eConsolePy_appClosed(eConsolePy *self, void *closure)
368 {
369         return self->cont->appClosed.get();
370 }
371
372 static PyGetSetDef eConsolePy_getseters[] = {
373         {"dataAvail",
374          (getter)eConsolePy_dataAvail, (setter)0,
375          "dataAvail callback list",
376          NULL},
377         {"stdoutAvail",
378          (getter)eConsolePy_stdoutAvail, (setter)0,
379          "stdoutAvail callback list",
380          NULL},
381         {"stderrAvail",
382          (getter)eConsolePy_stderrAvail, (setter)0,
383          "stderrAvail callback list",
384          NULL},
385         {"dataSent",
386          (getter)eConsolePy_dataSent, (setter)0,
387          "dataSent callback list",
388          NULL},
389         {"appClosed",
390          (getter)eConsolePy_appClosed, (setter)0,
391          "appClosed callback list",
392          NULL},
393         {NULL} /* Sentinel */
394 };
395
396 static int
397 eConsolePy_traverse(eConsolePy *self, visitproc visit, void *arg)
398 {
399         PyObject *obj = self->cont->dataAvail.getSteal();
400         if (obj) {
401                 Py_VISIT(obj);
402         }
403         obj = self->cont->stdoutAvail.getSteal();
404         if (obj) {
405                 Py_VISIT(obj);
406         }
407         obj = self->cont->stderrAvail.getSteal();
408         if (obj) {
409                 Py_VISIT(obj);
410         }
411         obj = self->cont->dataSent.getSteal();
412         if (obj) {
413                 Py_VISIT(obj);
414         }
415         obj = self->cont->appClosed.getSteal();
416         if (obj) {
417                 Py_VISIT(obj);
418         }
419         return 0;
420 }
421
422 static int
423 eConsolePy_clear(eConsolePy *self)
424 {
425         PyObject *obj = self->cont->dataAvail.getSteal(true);
426         if (obj) {
427                 Py_CLEAR(obj);
428         }
429         obj = self->cont->stdoutAvail.getSteal(true);
430         if (obj) {
431                 Py_CLEAR(obj);
432         }
433         obj = self->cont->stderrAvail.getSteal(true);
434         if (obj) {
435                 Py_CLEAR(obj);
436         }
437         obj = self->cont->dataSent.getSteal(true);
438         if (obj) {
439                 Py_CLEAR(obj);
440         }
441         obj = self->cont->appClosed.getSteal(true);
442         if (obj) {
443                 Py_CLEAR(obj);
444         }
445         return 0;
446 }
447
448 static void
449 eConsolePy_dealloc(eConsolePy* self)
450 {
451         if (self->in_weakreflist != NULL)
452                 PyObject_ClearWeakRefs((PyObject *) self);
453         eConsolePy_clear(self);
454         self->cont->Release();
455         self->ob_type->tp_free((PyObject*)self);
456 }
457
458 static PyObject *
459 eConsolePy_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
460 {
461         eConsolePy *self = (eConsolePy *)type->tp_alloc(type, 0);
462         self->cont = new eConsoleAppContainer();
463         self->cont->AddRef();
464         self->in_weakreflist = NULL;
465         return (PyObject *)self;
466 }
467
468 static PyObject *
469 eConsolePy_running(eConsolePy* self)
470 {
471         PyObject *ret = NULL;
472         ret = self->cont->running() ? Py_True : Py_False;
473         Org_Py_INCREF(ret);
474         return ret;
475 }
476
477 static PyObject *
478 eConsolePy_execute(eConsolePy* self, PyObject *argt)
479 {
480         Py_ssize_t argc = PyTuple_Size(argt);
481         if (argc > 1)
482         {
483                 const char *argv[argc + 1];
484                 int argpos=0;
485                 while(argpos < argc)
486                 {
487                         PyObject *arg = PyTuple_GET_ITEM(argt, argpos);
488                         if (!PyString_Check(arg))
489                         {
490                                 char err[255];
491                                 if (argpos)
492                                         snprintf(err, 255, "arg %d is not a string", argpos);
493                                 else
494                                         snprintf(err, 255, "cmd is not a string!");
495                                 PyErr_SetString(PyExc_TypeError, err);
496                                 return NULL;
497                         }
498                         argv[argpos++] = PyString_AsString(arg);
499                 }
500                 argv[argpos] = 0;
501                 return PyInt_FromLong(self->cont->execute(argv[0], argv+1));
502         }
503         else
504         {
505                 const char *str;
506                 if (PyArg_ParseTuple(argt, "s", &str))
507                         return PyInt_FromLong(self->cont->execute(str));
508                 PyErr_SetString(PyExc_TypeError,
509                         "cmd is not a string!");
510         }
511         return NULL;
512 }
513
514 static PyObject *
515 eConsolePy_write(eConsolePy* self, PyObject *args)
516 {
517         int len;
518         char *data;
519         if (PyArg_ParseTuple(args, "si", &data, &len))
520                 ;
521         else
522         {
523                 PyObject *ob;
524                 if (!PyArg_ParseTuple(args, "O", &ob) || !PyString_Check(ob))
525                 {
526                         PyErr_SetString(PyExc_TypeError,
527                                 "1st arg must be a string, optionaly 2nd arg can be the string length");
528                         return NULL;
529                 }
530                 else
531                 {
532                         Py_ssize_t length;
533                         if (!PyString_AsStringAndSize(ob, &data, &length))
534                                 len = length;
535                         else
536                                 len = 0;
537                 }
538         }
539         self->cont->write(data, len);
540         Py_RETURN_NONE;
541 }
542
543 static PyObject *
544 eConsolePy_getPID(eConsolePy* self)
545 {
546         return PyInt_FromLong(self->cont->getPID());
547 }
548
549 static PyObject *
550 eConsolePy_setCWD(eConsolePy* self, PyObject *args)
551 {
552         const char *path=0;
553         if (!PyArg_ParseTuple(args, "s", &path))
554                 return NULL;
555         self->cont->setCWD(path);
556         Py_RETURN_NONE;
557 }
558
559 static PyObject *
560 eConsolePy_kill(eConsolePy* self)
561 {
562         self->cont->kill();
563         Py_RETURN_NONE;
564 }
565
566 static PyObject *
567 eConsolePy_sendCtrlC(eConsolePy* self)
568 {
569         self->cont->sendCtrlC();
570         Py_RETURN_NONE;
571 }
572
573 static PyObject *
574 eConsolePy_sendEOF(eConsolePy* self)
575 {
576         self->cont->sendEOF();
577         Py_RETURN_NONE;
578 }
579
580 static PyObject *
581 eConsolePy_dumpToFile(eConsolePy* self, PyObject *args)
582 {
583         char *filename;
584         if (!PyArg_ParseTuple(args, "s", &filename))
585         {
586                 PyErr_SetString(PyExc_TypeError,
587                         "arg must be a string (filename)");
588                 return NULL;
589         }
590         else
591         {
592                 int fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0644);
593                 self->cont->setFileFD(1, fd);
594                 eDebug("eConsoleAppContainer::dumpToFile open(%s, O_WRONLY|O_CREAT|O_TRUNC, 0644)=%d", filename, fd);
595         }
596         Py_RETURN_NONE;
597 }
598
599 static PyObject *
600 eConsolePy_readFromFile(eConsolePy* self, PyObject *args)
601 {
602         char *filename;
603         if (!PyArg_ParseTuple(args, "s", &filename))
604         {
605                 PyErr_SetString(PyExc_TypeError,
606                         "arg must be a string (filename)");
607                 return NULL;
608         }
609         else
610         {
611                 int fd = open(filename, O_RDONLY);
612                 if (fd >= 0)
613                 {
614                         char readbuf[32*1024];
615                         int rsize = read(fd, readbuf, 32*1024);
616                         self->cont->setFileFD(0, fd);
617                         eDebug("eConsoleAppContainer::readFromFile open(%s, O_RDONLY)=%d, read: %d", filename, fd, rsize);
618                         self->cont->write(readbuf, rsize);
619                 }
620                 else
621                 {
622                         eDebug("eConsoleAppContainer::readFromFile %s not exist!", filename);
623                         self->cont->setFileFD(0, -1);
624                 }
625         }
626         Py_RETURN_NONE;
627 }
628
629 static PyMethodDef eConsolePy_methods[] = {
630         {"setCWD", (PyCFunction)eConsolePy_setCWD, METH_VARARGS,
631          "set working dir"
632         },
633         {"execute", (PyCFunction)eConsolePy_execute, METH_VARARGS,
634          "execute command"
635         },
636         {"dumpToFile", (PyCFunction)eConsolePy_dumpToFile, METH_VARARGS,
637          "set output file"
638         },
639         {"readFromFile", (PyCFunction)eConsolePy_readFromFile, METH_VARARGS,
640          "set input file"
641         },
642         {"getPID", (PyCFunction)eConsolePy_getPID, METH_NOARGS,
643          "execute command"
644         },
645         {"kill", (PyCFunction)eConsolePy_kill, METH_NOARGS,
646          "kill application"
647         },
648         {"sendCtrlC", (PyCFunction)eConsolePy_sendCtrlC, METH_NOARGS,
649          "send Ctrl-C to application"
650         },
651         {"sendEOF", (PyCFunction)eConsolePy_sendEOF, METH_NOARGS,
652          "send EOF to application"
653         },
654         {"write", (PyCFunction)eConsolePy_write, METH_VARARGS,
655          "write data to application"
656         },
657         {"running", (PyCFunction)eConsolePy_running, METH_NOARGS,
658          "returns the running state"
659         },
660         {NULL}  /* Sentinel */
661 };
662
663 static PyTypeObject eConsolePyType = {
664         PyObject_HEAD_INIT(NULL)
665         0, /*ob_size*/
666         "eConsoleImpl.eConsoleAppContainer", /*tp_name*/
667         sizeof(eConsolePy), /*tp_basicsize*/
668         0, /*tp_itemsize*/
669         (destructor)eConsolePy_dealloc, /*tp_dealloc*/
670         0, /*tp_print*/
671         0, /*tp_getattr*/
672         0, /*tp_setattr*/
673         0, /*tp_compare*/
674         0, /*tp_repr*/
675         0, /*tp_as_number*/
676         0, /*tp_as_sequence*/
677         0, /*tp_as_mapping*/
678         0, /*tp_hash */
679         0, /*tp_call*/
680         0, /*tp_str*/
681         0, /*tp_getattro*/
682         0, /*tp_setattro*/
683         0, /*tp_as_buffer*/
684         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
685         "eConsoleAppContainer objects", /* tp_doc */
686         (traverseproc)eConsolePy_traverse, /* tp_traverse */
687         (inquiry)eConsolePy_clear, /* tp_clear */
688         0, /* tp_richcompare */
689         offsetof(eConsolePy, in_weakreflist), /* tp_weaklistoffset */
690         0, /* tp_iter */
691         0, /* tp_iternext */
692         eConsolePy_methods, /* tp_methods */
693         0, /* tp_members */
694         eConsolePy_getseters, /* tp_getset */
695         0, /* tp_base */
696         0, /* tp_dict */
697         0, /* tp_descr_get */
698         0, /* tp_descr_set */
699         0, /* tp_dictoffset */
700         0, /* tp_init */
701         0, /* tp_alloc */
702         eConsolePy_new, /* tp_new */
703 };
704
705 static PyMethodDef module_methods[] = {
706         {NULL}  /* Sentinel */
707 };
708
709 void eConsoleInit(void)
710 {
711         PyObject* m = Py_InitModule3("eConsoleImpl", module_methods,
712                 "Module that implements eConsoleAppContainer with working cyclic garbage collection.");
713
714         if (m == NULL)
715                 return;
716
717         if (!PyType_Ready(&eConsolePyType))
718         {
719                 Org_Py_INCREF((PyObject*)&eConsolePyType);
720                 PyModule_AddObject(m, "eConsoleAppContainer", (PyObject*)&eConsolePyType);
721         }
722 }
723 }