X-Git-Url: https://git.cweiske.de/enigma2.git/blobdiff_plain/1c4d458b5df83facce7cf71b39ab247eb1447491..6f50f87ade33d60f05939a38de1ae0f4117b414b:/lib/base/console.cpp diff --git a/lib/base/console.cpp b/lib/base/console.cpp index a29772d8..c923da52 100644 --- a/lib/base/console.cpp +++ b/lib/base/console.cpp @@ -7,8 +7,9 @@ #include #include #include +#include -int bidirpipe(int pfd[], const char *cmd , const char * const 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 */ @@ -37,6 +38,9 @@ int bidirpipe(int pfd[], const char *cmd , const char * const 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); @@ -79,6 +83,20 @@ static char *find_bracket(char ch) return NULL; } +int eConsoleAppContainer::setCWD( const char *path ) +{ + struct stat dir_stat; + + if (stat(path, &dir_stat) == -1) + return -1; + + if (!S_ISDIR(dir_stat.st_mode)) + return -2; + + m_cwd = path; + return 0; +} + int eConsoleAppContainer::execute( const char *cmd ) { int cnt=0, slen=strlen(cmd); @@ -157,13 +175,15 @@ int eConsoleAppContainer::execute(const char *cmdline, const char * const argv[] killstate=0; // get one read ,one write and the err pipe to the prog.. - pid = bidirpipe(fd, cmdline, argv); + pid = bidirpipe(fd, cmdline, argv, m_cwd.length() ? m_cwd.c_str() : 0); if ( pid == -1 ) return -3; // eDebug("pipe in = %d, out = %d, err = %d", fd[0], fd[1], fd[2]); + ::fcntl(fd[1], F_SETFL, O_NONBLOCK); + ::fcntl(fd[2], F_SETFL, O_NONBLOCK); in = new eSocketNotifier(eApp, fd[0], eSocketNotifier::Read|eSocketNotifier::Priority|eSocketNotifier::Hungup ); out = new eSocketNotifier(eApp, fd[1], eSocketNotifier::Write, false); err = new eSocketNotifier(eApp, fd[2], eSocketNotifier::Read|eSocketNotifier::Priority ); @@ -278,18 +298,17 @@ void eConsoleAppContainer::readyRead(int what) if (what & (eSocketNotifier::Priority|eSocketNotifier::Read)) { // eDebug("what = %d"); - char buf[2048]; + char buf[2049]; int rd; - while((rd = read(fd[0], buf, 2047)) > 0) + while((rd = read(fd[0], 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); 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"); @@ -316,9 +335,9 @@ void eConsoleAppContainer::readyErrRead(int what) if (what & (eSocketNotifier::Priority|eSocketNotifier::Read)) { // eDebug("what = %d"); - char buf[2048]; + char buf[2049]; int rd; - while((rd = read(fd[2], buf, 2047)) > 0) + while((rd = read(fd[2], buf, 2048)) > 0) { /* for ( int i = 0; i < rd; i++ ) eDebug("%d = %c (%02x)", i, buf[i], buf[i] );*/