use a reference instead of a local copy
[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 eConsoleAppContainer::eConsoleAppContainer()
59 :pid(-1), killstate(0), in(0), out(0), err(0)
60 {
61         for (int i=0; i < 3; ++i)
62                 fd[i]=-1;
63 }
64
65 static char brakets[][2] = {
66         { '\'','\'' },
67         {'"','"'},
68         {'`','`'},
69         {'(',')'},
70         {'{','}'},
71         {'[',']'},
72         {'<','>'}
73 };
74
75 static char *find_bracket(char ch)
76 {
77         size_t idx=0;
78         while (idx < sizeof(brakets)/2) {
79                 if (brakets[idx][0] == ch)
80                         return &brakets[idx][0];
81                 ++idx;
82         }
83         return NULL;
84 }
85
86 int eConsoleAppContainer::setCWD( const char *path )
87 {
88         struct stat dir_stat;
89
90         if (stat(path, &dir_stat) == -1)
91                 return -1;
92
93         if (!S_ISDIR(dir_stat.st_mode))
94                 return -2;
95
96         m_cwd = path;
97         return 0;
98 }
99
100 int eConsoleAppContainer::execute( const char *cmd )
101 {
102         int cnt=0, slen=strlen(cmd);
103         char buf[slen+1];
104         char *tmp=0, *argv[64], *path=buf, *cmds = buf;
105         memcpy(buf, cmd, slen+1);
106
107 //      printf("cmd = %s, len %d\n", cmd, slen);
108
109         // kill spaces at beginning
110         while(path[0] == ' ') {
111                 ++path;
112                 ++cmds;
113                 --slen;
114         }
115
116         // kill spaces at the end
117         while(slen && path[slen-1] == ' ') {
118                 path[slen-1] = 0;
119                 --slen;
120         }
121
122         if (!slen)
123                 return -2;
124
125         tmp = strchr(path, ' ');
126         if (tmp) {
127                 *tmp = 0;
128                 cmds = tmp+1;
129                 while(*cmds && *cmds == ' ')
130                         ++cmds;
131         }
132         else
133                 cmds = path+slen;
134
135         memset(argv, 0, sizeof(argv));
136         argv[cnt++] = path;
137
138         if (*cmds) {
139                 char *argb=NULL, *it=NULL;
140                 while ( (tmp = strchr(cmds, ' ')) ) {
141                         if (!it && *cmds && (it = find_bracket(*cmds)) )
142                                 *cmds = 'X'; // replace open braket...
143                         if (!argb) // not arg begin
144                                 argb = cmds;
145                         if (it && *(tmp-1) == it[1]) {
146                                 *argb = it[0]; // set old char for open braket
147                                 it = 0;
148                         }
149                         if (!it) { // end of arg
150                                 *tmp = 0;
151                                 argv[cnt++] = argb;
152                                 argb=0; // reset arg begin
153                         }
154                         cmds = tmp+1;
155                         while (*cmds && *cmds == ' ')
156                                 ++cmds;
157                 }
158                 argv[cnt++] = argb ? argb : cmds;
159                 if (it)
160                     *argv[cnt-1] = it[0]; // set old char for open braket
161         }
162
163 //      int tmp=0;
164 //      while(argv[tmp])
165 //              eDebug("%d is %s", tmp, argv[tmp++]);
166         return execute(argv[0], argv);
167 }
168
169 int eConsoleAppContainer::execute(const char *cmdline, const char * const argv[])
170 {
171         if (running())
172                 return -1;
173
174         pid=-1;
175         killstate=0;
176
177         // get one read ,one write and the err pipe to the prog..
178         pid = bidirpipe(fd, cmdline, argv, m_cwd.length() ? m_cwd.c_str() : 0);
179
180         if ( pid == -1 )
181                 return -3;
182
183 //      eDebug("pipe in = %d, out = %d, err = %d", fd[0], fd[1], fd[2]);
184
185         ::fcntl(fd[1], F_SETFL, O_NONBLOCK);
186         ::fcntl(fd[2], F_SETFL, O_NONBLOCK);
187         in = new eSocketNotifier(eApp, fd[0], eSocketNotifier::Read|eSocketNotifier::Priority|eSocketNotifier::Hungup );
188         out = new eSocketNotifier(eApp, fd[1], eSocketNotifier::Write, false);  
189         err = new eSocketNotifier(eApp, fd[2], eSocketNotifier::Read|eSocketNotifier::Priority );
190         CONNECT(in->activated, eConsoleAppContainer::readyRead);
191         CONNECT(out->activated, eConsoleAppContainer::readyWrite);
192         CONNECT(err->activated, eConsoleAppContainer::readyErrRead);
193
194         return 0;
195 }
196
197 int eConsoleAppContainer::execute( PyObject *cmdline, PyObject *args )
198 {
199         if (!PyString_Check(cmdline))
200                 return -1;
201         if (!PyList_Check(args))
202                 return -1;
203         const char *argv[PyList_Size(args) + 1];
204         int i;
205         for (i = 0; i < PyList_Size(args); ++i)
206         {
207                 PyObject *arg = PyList_GetItem(args, i); /* borrowed ref */
208                 if (!arg)
209                         return -1;
210                 if (!PyString_Check(arg))
211                         return -1;
212                 argv[i] = PyString_AsString(arg); /* borrowed pointer */
213         }
214         argv[i] = 0;
215
216         return execute(PyString_AsString(cmdline), argv); /* borrowed pointer */
217 }
218
219 eConsoleAppContainer::~eConsoleAppContainer()
220 {
221         kill();
222 }
223
224 void eConsoleAppContainer::kill()
225 {
226         if ( killstate != -1 && pid != -1 )
227         {
228                 eDebug("user kill(SIGKILL) console App");
229                 killstate=-1;
230                 /*
231                  * Use a negative pid value, to signal the whole process group
232                  * ('pid' might not even be running anymore at this point)
233                  */
234                 ::kill(-pid, SIGKILL);
235                 closePipes();
236         }
237         while( outbuf.size() ) // cleanup out buffer
238         {
239                 queue_data d = outbuf.front();
240                 outbuf.pop();
241                 delete [] d.data;
242         }
243         delete in;
244         delete out;
245         delete err;
246         in=out=err=0;
247 }
248
249 void eConsoleAppContainer::sendCtrlC()
250 {
251         if ( killstate != -1 && pid != -1 )
252         {
253                 eDebug("user send SIGINT(Ctrl-C) to console App");
254                 /*
255                  * Use a negative pid value, to signal the whole process group
256                  * ('pid' might not even be running anymore at this point)
257                  */
258                 ::kill(-pid, SIGINT);
259         }
260 }
261
262 void eConsoleAppContainer::closePipes()
263 {
264         if (in)
265                 in->stop();
266         if (out)
267                 out->stop();
268         if (err)
269                 err->stop();
270         if (fd[0] != -1)
271         {
272                 ::close(fd[0]);
273                 fd[0]=-1;
274         }
275         if (fd[1] != -1)
276         {
277                 ::close(fd[1]);
278                 fd[1]=-1;
279         }
280         if (fd[2] != -1)
281         {
282                 ::close(fd[2]);
283                 fd[2]=-1;
284         }
285         eDebug("pipes closed");
286         while( outbuf.size() ) // cleanup out buffer
287         {
288                 queue_data d = outbuf.front();
289                 outbuf.pop();
290                 delete [] d.data;
291         }
292         pid = -1;
293 }
294
295 void eConsoleAppContainer::readyRead(int what)
296 {
297         bool hungup = what & eSocketNotifier::Hungup;
298         if (what & (eSocketNotifier::Priority|eSocketNotifier::Read))
299         {
300 //              eDebug("what = %d");
301                 char buf[2049];
302                 int rd;
303                 while((rd = read(fd[0], buf, 2048)) > 0)
304                 {
305                         buf[rd]=0;
306                         /*emit*/ dataAvail(buf);
307                         if (!hungup)
308                                 break;
309                 }
310         }
311         readyErrRead(eSocketNotifier::Priority|eSocketNotifier::Read); /* be sure to flush all data which might be already written */
312         if (hungup)
313         {
314                 eDebug("child has terminated");
315                 closePipes();
316                 int childstatus;
317                 int retval = killstate;
318                 /*
319                  * We have to call 'wait' on the child process, in order to avoid zombies.
320                  * Also, this gives us the chance to provide better exit status info to appClosed.
321                  */
322                 if (::waitpid(pid, &childstatus, 0) > 0)
323                 {
324                         if (WIFEXITED(childstatus))
325                         {
326                                 retval = WEXITSTATUS(childstatus);
327                         }
328                 }
329                 /*emit*/ appClosed(retval);
330         }
331 }
332
333 void eConsoleAppContainer::readyErrRead(int what)
334 {
335         if (what & (eSocketNotifier::Priority|eSocketNotifier::Read))
336         {
337 //              eDebug("what = %d");
338                 char buf[2049];
339                 int rd;
340                 while((rd = read(fd[2], buf, 2048)) > 0)
341                 {
342 /*                      for ( int i = 0; i < rd; i++ )
343                                 eDebug("%d = %c (%02x)", i, buf[i], buf[i] );*/
344                         buf[rd]=0;
345                         /*emit*/ dataAvail(buf);
346                 }
347         }
348 }
349
350 void eConsoleAppContainer::write( const char *data, int len )
351 {
352         char *tmp = new char[len];
353         memcpy(tmp, data, len);
354         outbuf.push(queue_data(tmp,len));
355         if (out)
356                 out->start();
357 }
358
359 void eConsoleAppContainer::write( PyObject *data )
360 {
361         char *buffer;
362         int length;
363         if (PyString_AsStringAndSize(data, &buffer, &length))
364                 return;
365         if (buffer && length)
366                 write(buffer, length);
367 }
368
369 void eConsoleAppContainer::readyWrite(int what)
370 {
371         if (what&eSocketNotifier::Write && outbuf.size() )
372         {
373                 queue_data &d = outbuf.front();
374                 int wr = ::write( fd[1], d.data+d.dataSent, d.len-d.dataSent );
375                 if (wr < 0)
376                         eDebug("eConsoleAppContainer write failed (%m)");
377                 else
378                         d.dataSent += wr;
379                 if (d.dataSent == d.len)
380                 {
381                         outbuf.pop();
382                         delete [] d.data;
383                         /* emit */ dataSent(0);
384                 }
385         }
386         if ( !outbuf.size() )
387                 out->stop();
388 }