[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[PATCH 6/6] Check return value of chdir()



If chdir() does not return 0, display an error of some sort and don't do
what we were about to do.
---
 loader/init.c   |    9 ++++++---
 loader/loader.c |   13 ++++++++-----
 2 files changed, 14 insertions(+), 8 deletions(-)

diff --git a/loader/init.c b/loader/init.c
index 655c545..d30487b 100644
--- a/loader/init.c
+++ b/loader/init.c
@@ -862,9 +862,12 @@ int main(int argc, char **argv) {
             printf("Development mode requested spawning shell...\n");
 
             if ((shellpid = fork()) == 0) {
-                chdir("/root");
-                setenv("HOME", "/root", 1);
-                execl("/sbin/bash", "/sbin/bash", NULL);
+                if (chdir("/root") == 0) {
+                    setenv("HOME", "/root", 1);
+                    execl("/sbin/bash", "/sbin/bash", NULL);
+                } else {
+                    perror("Unable to chdir to /root");
+                }
             }
             else if (shellpid > 0) {
                 waitpid(shellpid, NULL, 0);
diff --git a/loader/loader.c b/loader/loader.c
index a7f4e69..f14713b 100644
--- a/loader/loader.c
+++ b/loader/loader.c
@@ -157,11 +157,14 @@ void doShell(void) {
     child = fork();
 
     if (child == 0) {
-        chdir("/root");
-        setenv("HOME", "/root", 1);
-        if (execl("/sbin/bash", "/sbin/bash", "-i", NULL) == -1) {
-            logMessage(ERROR, "%s (%d): %m", __func__, __LINE__);
-            _exit(1);
+        if (chdir("/root") == 0) {
+            setenv("HOME", "/root", 1);
+            if (execl("/sbin/bash", "/sbin/bash", "-i", NULL) == -1) {
+                logMessage(ERROR, "%s (%d): %m", __func__, __LINE__);
+                _exit(1);
+            }
+        } else {
+            logMessage(ERROR, "missing /root, cannot run shell");
         }
     } else if (child == -1) {
         logMessage(ERROR, "%s (%d): %m", __func__, __LINE__);
-- 
1.7.1.1


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]