yes! ich habs kaputt gemacht! (doesn't compile anymore, doesn't work anymore,
[enigma2.git] / lib / base / console.cpp
1 /*
2  * console.cpp
3  *
4  * Copyright (C) 2002 Felix Domke <tmbinc@tuxbox.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * $Id: console.cpp,v 1.1 2003-10-17 15:35:47 tmbinc Exp $
21  */
22
23 #include <lib/base/console.h>
24
25 #include <lib/base/estring.h>
26 #include <sys/vfs.h> // for statfs
27 #include <unistd.h>
28 #include <signal.h>
29 #include <errno.h>
30
31 int bidirpipe(int pfd[], char *cmd , char *argv[])
32 {
33         int pfdin[2];  /* from child to parent */
34         int pfdout[2]; /* from parent to child */
35         int pfderr[2]; /* stderr from child to parent */
36         int pid;       /* child's pid */
37
38         if ( pipe(pfdin) == -1 || pipe(pfdout) == -1 || pipe(pfderr) == -1)
39                 return(-1);
40
41         if ( ( pid = fork() ) == -1 )
42                 return(-1);
43         else if (pid == 0) /* child process */
44         {
45                 if ( close(0) == -1 || close(1) == -1 || close(2) == -1 )
46                         _exit(0);
47
48                 if (dup(pfdout[0]) != 0 || dup(pfdin[1]) != 1 || dup(pfderr[1]) != 2 )
49                         _exit(0);
50
51                 if (close(pfdout[0]) == -1 || close(pfdout[1]) == -1 ||
52                                 close(pfdin[0]) == -1 || close(pfdin[1]) == -1 ||
53                                 close(pfderr[0]) == -1 || close(pfderr[1]) == -1 )
54                         _exit(0);
55
56                 execv(cmd,argv);
57                 _exit(0);
58         }
59         if (close(pfdout[0]) == -1 || close(pfdin[1]) == -1 || close(pfderr[1]) == -1)
60                         return(-1);
61
62         pfd[0] = pfdin[0];
63         pfd[1] = pfdout[1];
64         pfd[2] = pfderr[0];
65
66         return(pid);
67 }
68
69 eConsoleAppContainer::eConsoleAppContainer( const eString &cmd )
70 :pid(-1), killstate(0), outbuf(0)
71 {
72 //      eDebug("cmd = %s", cmd.c_str() );
73         memset(fd, 0, sizeof(fd) );
74         int cnt=2; // path to app + terminated 0
75         eString str(cmd?cmd:"");
76
77         while( str.length() && str[0] == ' ' )  // kill spaces at beginning
78                 str = str.mid(1);
79
80         while( str.length() && str[str.length()-1] == ' ' )  // kill spaces at the end
81                 str = str.left( str.length() - 1 );
82
83         if (!str.length())
84                 return;
85
86         unsigned int idx=0;
87         eString path = str.left( (idx = str.find(' ')) != eString::npos ? idx : str.length() );
88 //      eDebug("path = %s", path.c_str() );
89
90         eString cmds = str.mid( path.length()+1 );
91 //      eDebug("cmds = %s", cmds.c_str() );
92
93         idx = 0;
94         while ( (idx = cmds.find(' ',idx) ) != eString::npos )  // count args
95         {
96                 cnt++;
97                 idx++;
98         }
99
100 //      eDebug("idx = %d, %d counted spaces", idx, cnt-2);
101
102         if ( cmds.length() )
103         {
104                 cnt++;
105 //              eDebug("increase cnt");
106         }
107
108 //      eDebug("%d args", cnt-2);
109         char **argv = new char*[cnt];  // min two args... path and terminating 0
110         argv[0] = new char[ path.length() ];
111         strcpy( argv[0], path.c_str() );
112         argv[cnt-1] = 0;               // set terminating null
113
114         if ( cnt > 2 )  // more then default args?
115         {
116                 cnt=1;  // do not overwrite path in argv[0]
117
118                 while ( (idx = cmds.find(' ')) != eString::npos )  // parse all args..
119                 {
120                         argv[cnt] = new char[ idx ];
121 //                      eDebug("idx=%d, arg = %s", idx, cmds.left(idx).c_str() );
122                         strcpy( argv[cnt++], cmds.left( idx ).c_str() );
123                         cmds = cmds.mid(idx+1);
124 //                      eDebug("str = %s", cmds.c_str() );
125                 }
126                 // store the last arg
127                 argv[cnt] = new char[ cmds.length() ];
128                 strcpy( argv[cnt], cmds.c_str() );
129         }
130
131   // get one read ,one write and the err pipe to the prog..
132   
133         if ( (pid = bidirpipe(fd, argv[0], argv)) == -1 )
134         {
135                 while ( cnt-- > 0 )
136                         delete [] argv[cnt];
137                 delete [] argv;
138                 return;
139         }
140
141         while ( cnt-- > 0 )  // release heap memory
142                 delete [] argv[cnt];
143         delete [] argv;
144
145         eDebug("pipe in = %d, out = %d, err = %d", fd[0], fd[1], fd[2]);
146
147         in = new eSocketNotifier(eApp, fd[0], 19 );  // 19 = POLLIN, POLLPRI, POLLHUP
148         out = new eSocketNotifier(eApp, fd[1], eSocketNotifier::Write);  // POLLOUT
149         err = new eSocketNotifier(eApp, fd[2], 19 );  // 19 = POLLIN, POLLPRI, POLLHUP
150         CONNECT(in->activated, eConsoleAppContainer::readyRead);
151         CONNECT(out->activated, eConsoleAppContainer::readyWrite);
152         CONNECT(err->activated, eConsoleAppContainer::readyErrRead);
153         signal(SIGCHLD, SIG_IGN);   // no zombie when child killed
154 }
155
156 eConsoleAppContainer::~eConsoleAppContainer()
157 {
158         if ( running() )
159         {
160                 killstate=-1;
161                 kill();
162         }
163         if ( outbuf )
164                 delete [] outbuf;
165 }
166
167 void eConsoleAppContainer::kill()
168 {
169         killstate=-1;
170         system( eString().sprintf("kill %d", pid).c_str() );
171         eDebug("user kill console App");
172 }
173
174 void eConsoleAppContainer::closePipes()
175 {
176         in->stop();
177         out->stop();
178         err->stop();
179         ::close(fd[0]);
180         fd[0]=0;
181         ::close(fd[1]);
182         fd[1]=0;
183         ::close(fd[2]);
184         fd[2]=0;
185         eDebug("pipes closed");
186 }
187
188 void eConsoleAppContainer::readyRead(int what)
189 {
190         if (what & POLLPRI|POLLIN)
191         {
192                 eDebug("what = %d");
193                 char buf[2048];
194                 int readed = read(fd[0], buf, 2048);
195                 eDebug("%d bytes read", readed);
196                 if ( readed != -1 && readed )
197                         /*emit*/ dataAvail( eString( buf ) );
198                 else if (readed == -1)
199                         eDebug("readerror %d", errno);
200         }
201         if (what & eSocketNotifier::Hungup)
202         {
203                 eDebug("child has terminated");
204                 closePipes();
205                 /*emit*/ appClosed(killstate);
206         }
207 }
208
209 void eConsoleAppContainer::readyErrRead(int what)
210 {
211         if (what & POLLPRI|POLLIN)
212         {
213                 eDebug("what = %d");
214                 char buf[2048];
215                 int readed = read(fd[2], buf, 2048);
216                 eDebug("%d bytes read", readed);
217                 if ( readed != -1 && readed )
218                         /*emit*/ dataAvail( eString( buf ) );
219                 else if (readed == -1)
220                         eDebug("readerror %d", errno);
221         }
222 }
223
224 void eConsoleAppContainer::write( const eString & str )
225 {
226         outbuf = new char[ str.length()];
227         strcpy( outbuf, str.c_str() );
228 }
229
230 void eConsoleAppContainer::readyWrite(int what)
231 {
232         if (what == 4 && outbuf)
233         {
234                 if ( ::write( fd[1], outbuf, strlen(outbuf) ) != (int) strlen(outbuf) )
235                 {
236                         /* emit */ dataSent(-1);
237                         eDebug("writeError");
238                 }
239                 else
240                 {
241                         /* emit */ dataSent(0);
242                         eDebug("write ok");
243                 }
244
245                 delete outbuf;
246                 outbuf=0;
247         }
248 }