properly handle return codes
authorJohn Crispin <blogic@openwrt.org>
Sat, 28 Mar 2015 14:41:58 +0000 (15:41 +0100)
committerJohn Crispin <blogic@openwrt.org>
Sat, 28 Mar 2015 17:35:21 +0000 (18:35 +0100)
Signed-off-by: John Crispin <blogic@openwrt.org>
initd/early.c
initd/mkdev.c
initd/preinit.c
inittab.c
jail/jail.c
plug/hotplug.c
service/instance.c
state.c
trace/trace.c
upgraded/upgraded.c

index 5ee49ee..593449b 100644 (file)
@@ -77,7 +77,8 @@ early_mounts(void)
        mkdir("/tmp/run", 0777);
        mkdir("/tmp/lock", 0777);
        mkdir("/tmp/state", 0777);
        mkdir("/tmp/run", 0777);
        mkdir("/tmp/lock", 0777);
        mkdir("/tmp/state", 0777);
-       symlink("/tmp", "/var");
+       if (symlink("/tmp", "/var"))
+               ERROR("failed to symlink /tmp -> /var\n");
 }
 
 static void
 }
 
 static void
index 5ac6e95..e6d3d0c 100644 (file)
@@ -121,7 +121,5 @@ int mkdev(const char *name, int _mode)
        n_patterns = 1;
        find_devs(true);
        find_devs(false);
        n_patterns = 1;
        find_devs(true);
        find_devs(false);
-       chdir("/");
-
-       return 0;
+       return chdir("/");
 }
 }
index fb94527..f38d8ef 100644 (file)
@@ -38,7 +38,8 @@ check_dbglvl(void)
 
        if (!fp)
                return;
 
        if (!fp)
                return;
-       fscanf(fp, "%d", &lvl);
+       if (fscanf(fp, "%d", &lvl) == EOF)
+               ERROR("failed to read debug level\n");
        fclose(fp);
        unlink("/tmp/debug_level");
 
        fclose(fp);
        unlink("/tmp/debug_level");
 
index 623103d..eb402f8 100644 (file)
--- a/inittab.c
+++ b/inittab.c
@@ -70,9 +70,11 @@ static int dev_open(const char *dev)
        int fd = -1;
 
        if (dev) {
        int fd = -1;
 
        if (dev) {
-               chdir("/dev");
-               fd = open( dev, O_RDWR);
-               chdir("/");
+               if (chdir("/dev"))
+                       ERROR("failed to change dir to /dev\n");
+               fd = open(dev, O_RDWR);
+               if (chdir("/"))
+                       ERROR("failed to change dir to /\n");
        }
 
        return fd;
        }
 
        return fd;
@@ -83,9 +85,8 @@ static int dev_exist(const char *dev)
        int res;
 
        res = dev_open(dev);
        int res;
 
        res = dev_open(dev);
-       if (res != -1) {
+       if (res != -1)
                close(res);
                close(res);
-       }
 
        return (res != -1);
 }
 
        return (res != -1);
 }
index 3b5587a..a6de133 100644 (file)
@@ -313,12 +313,16 @@ static int spawn_child(void *arg)
                        sysfs = 1;
                        break;
                case 'n':
                        sysfs = 1;
                        break;
                case 'n':
-                       sethostname(optarg, strlen(optarg));
+                       if (sethostname(optarg, strlen(optarg)))
+                               ERROR("failed to sethostname: %s\n", strerror(errno));
                        break;
                }
        }
 
                        break;
                }
        }
 
-       asprintf(&mpoint, "%s/old", path);
+       if (asprintf(&mpoint, "%s/old", path) < 0) {
+               ERROR("failed to alloc pivot path: %s\n", strerror(errno));
+               return -1;
+       }
        mkdir_p(mpoint, 0755);
        if (pivot_root(path, mpoint) == -1) {
                ERROR("pivot_root failed:%s\n", strerror(errno));
        mkdir_p(mpoint, 0755);
        if (pivot_root(path, mpoint) == -1) {
                ERROR("pivot_root failed:%s\n", strerror(errno));
@@ -370,13 +374,17 @@ static void spawn_namespace(const char *path, int argc, char **argv)
        char *dir = get_current_dir_name();
 
        uloop_init();
        char *dir = get_current_dir_name();
 
        uloop_init();
-       chdir(path);
+       if (chdir(path)) {
+               ERROR("failed to chdir() into the jail\n");
+               return;
+       }
        namespace_process.pid = clone(spawn_child,
                        child_stack + STACK_SIZE,
                        CLONE_NEWUTS | CLONE_NEWPID | CLONE_NEWNS | SIGCHLD, argv);
 
        if (namespace_process.pid != -1) {
        namespace_process.pid = clone(spawn_child,
                        child_stack + STACK_SIZE,
                        CLONE_NEWUTS | CLONE_NEWPID | CLONE_NEWNS | SIGCHLD, argv);
 
        if (namespace_process.pid != -1) {
-               chdir(dir);
+               if (chdir(dir))
+                       ERROR("failed to chdir() out of the jail\n");
                free(dir);
                uloop_process_add(&namespace_process);
                uloop_run();
                free(dir);
                uloop_process_add(&namespace_process);
                uloop_run();
index 6df7971..1a98e8b 100644 (file)
@@ -198,7 +198,10 @@ send_to_kernel:
                ERROR("Failed to open %s\n", loadpath);
                exit(-1);
        }
                ERROR("Failed to open %s\n", loadpath);
                exit(-1);
        }
-       write(load, "1", 1);
+       if (write(load, "1", 1) == -1) {
+               ERROR("Failed to write to %s\n", loadpath);
+               exit(-1);
+       }
        close(load);
 
        snprintf(syspath, sizeof(syspath), "/sys/%s/data", dev);
        close(load);
 
        snprintf(syspath, sizeof(syspath), "/sys/%s/data", dev);
@@ -214,7 +217,10 @@ send_to_kernel:
                if (len <= 0)
                        break;
 
                if (len <= 0)
                        break;
 
-               write(fw, buf, len);
+               if (write(fw, buf, len) == -1) {
+                       ERROR("failed to write firmware file %s/%s to %s\n", dir, file, dev);
+                       break;
+               }
        }
 
        if (src >= 0)
        }
 
        if (src >= 0)
@@ -222,7 +228,8 @@ send_to_kernel:
        close(fw);
 
        load = open(loadpath, O_WRONLY);
        close(fw);
 
        load = open(loadpath, O_WRONLY);
-       write(load, "0", 1);
+       if (write(load, "0", 1) == -1)
+               ERROR("failed to write to %s\n", loadpath);
        close(load);
 
        DEBUG(2, "Done loading %s\n", path);
        close(load);
 
        DEBUG(2, "Done loading %s\n", path);
index 8d2001a..f5b61fa 100644 (file)
@@ -283,8 +283,10 @@ instance_run(struct service_instance *in, int _stdout, int _stderr)
        }
 
        if (in->uid || in->gid) {
        }
 
        if (in->uid || in->gid) {
-               setuid(in->uid);
-               setgid(in->gid);
+               if (setuid(in->uid) || setgid(in->gid)) {
+                       ERROR("failed to set uid:%d, gid:%d\n", in->uid, in->gid);
+                       exit(127);
+               }
        }
        execvp(argv[0], argv);
        exit(127);
        }
        execvp(argv[0], argv);
        exit(127);
diff --git a/state.c b/state.c
index 22a06a1..1ed70f5 100644 (file)
--- a/state.c
+++ b/state.c
@@ -43,12 +43,14 @@ static int reboot_event;
 
 static void set_stdio(const char* tty)
 {
 
 static void set_stdio(const char* tty)
 {
-       chdir("/dev");
-       freopen(tty, "r", stdin);
-       freopen(tty, "w", stdout);
-       freopen(tty, "w", stderr);
-       chdir("/");
-       fcntl(STDERR_FILENO, F_SETFL, fcntl(STDERR_FILENO, F_GETFL) | O_NONBLOCK);
+       if (chdir("/dev") ||
+           !freopen(tty, "r", stdin) ||
+           !freopen(tty, "w", stdout) ||
+           !freopen(tty, "w", stderr) ||
+           chdir("/"))
+               ERROR("failed to set stdio\n");
+       else
+               fcntl(STDERR_FILENO, F_SETFL, fcntl(STDERR_FILENO, F_GETFL) | O_NONBLOCK);
 }
 
 static void set_console(void)
 }
 
 static void set_console(void)
@@ -70,7 +72,10 @@ static void set_console(void)
                i++;
        }
 
                i++;
        }
 
-       chdir("/dev");
+       if (chdir("/dev")) {
+               ERROR("failed to change dir to /dev\n");
+               return;
+       }
        while (tty!=NULL) {
                f = open(tty, O_RDONLY);
                if (f >= 0) {
        while (tty!=NULL) {
                f = open(tty, O_RDONLY);
                if (f >= 0) {
@@ -81,7 +86,8 @@ static void set_console(void)
                tty=try[i];
                i++;
        }
                tty=try[i];
                i++;
        }
-       chdir("/");
+       if (chdir("/"))
+               ERROR("failed to change dir to /\n");
 
        if (tty != NULL)
                set_stdio(tty);
 
        if (tty != NULL)
                set_stdio(tty);
index c6f32d7..12f0ee6 100644 (file)
@@ -214,7 +214,8 @@ int main(int argc, char **argv, char **envp)
        uloop_done();
 
        if (!json)
        uloop_done();
 
        if (!json)
-               asprintf(&json, "/tmp/%s.%u.json", basename(*argv), child);
+               if (asprintf(&json, "/tmp/%s.%u.json", basename(*argv), child) < 0)
+                       ERROR("failed to allocate output path: %s\n", strerror(errno));
 
        print_syscalls(policy, json);
 
 
        print_syscalls(policy, json);
 
index 1e4057a..d7433e7 100644 (file)
@@ -18,6 +18,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <errno.h>
 
 #include <libubox/uloop.h>
 
 
 #include <libubox/uloop.h>
 
@@ -55,12 +56,14 @@ int main(int argc, char **argv)
 {
        pid_t p = getpid();
 
 {
        pid_t p = getpid();
 
-       chdir("/tmp");
-
        if (p != 1) {
                fprintf(stderr, "this tool needs to run as pid 1\n");
                return -1;
        }
        if (p != 1) {
                fprintf(stderr, "this tool needs to run as pid 1\n");
                return -1;
        }
+       if (chdir("/tmp") == -1) {
+               fprintf(stderr, "failed to chdir to /tmp: %s\n", strerror(errno));
+               return -1;
+       }
        if (argc != 2) {
                fprintf(stderr, "sysupgrade stage 2 failed, no folder specified\n");
                return -1;
        if (argc != 2) {
                fprintf(stderr, "sysupgrade stage 2 failed, no folder specified\n");
                return -1;