Add setCWD to change task's work directories for eConsoleAppContainers
authorAndreas Frisch <andreas.frisch@multimedia-labs.de>
Mon, 2 Jun 2008 14:21:41 +0000 (14:21 +0000)
committerAndreas Frisch <andreas.frisch@multimedia-labs.de>
Mon, 2 Jun 2008 14:21:41 +0000 (14:21 +0000)
lib/base/console.cpp
lib/base/console.h

index a29772d..2e00804 100644 (file)
@@ -8,7 +8,7 @@
 #include <sys/types.h>
 #include <sys/wait.h>
 
 #include <sys/types.h>
 #include <sys/wait.h>
 
-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 */
 {
        int pfdin[2];  /* from child to parent */
        int pfdout[2]; /* from parent to child */
@@ -37,6 +37,9 @@ int bidirpipe(int pfd[], const char *cmd , const char * const argv[])
                for (unsigned int i=3; i < 90; ++i )
                        close(i);
 
                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);
                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 +82,20 @@ static char *find_bracket(char ch)
        return NULL;
 }
 
        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);
 int eConsoleAppContainer::execute( const char *cmd )
 {
        int cnt=0, slen=strlen(cmd);
@@ -157,7 +174,8 @@ int eConsoleAppContainer::execute(const char *cmdline, const char * const argv[]
        killstate=0;
 
        // get one read ,one write and the err pipe to the prog..
        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;
 
        if ( pid == -1 )
                return -3;
index 07ad6a9..7c80769 100644 (file)
@@ -25,6 +25,7 @@ class eConsoleAppContainer: public Object
        int fd[3];
        int pid;
        int killstate;
        int fd[3];
        int pid;
        int killstate;
+       std::string m_cwd;
        std::queue<struct queue_data> outbuf;
        eSocketNotifier *in, *out, *err;
        void readyRead(int what);
        std::queue<struct queue_data> outbuf;
        eSocketNotifier *in, *out, *err;
        void readyRead(int what);
@@ -34,6 +35,7 @@ class eConsoleAppContainer: public Object
 #endif
 public:
        eConsoleAppContainer();
 #endif
 public:
        eConsoleAppContainer();
+       int setCWD( const char *path );
        int execute( const char *str );
        int execute( const char *cmdline, const char *const argv[] );
        int execute( PyObject *cmdline, PyObject *args );
        int execute( const char *str );
        int execute( const char *cmdline, const char *const argv[] );
        int execute( PyObject *cmdline, PyObject *args );