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 5ee49ee3f5e7e6591e6a01a0f52e582fe9aa0a9e..593449b619a669bf5c09add4be70b9b1bf1c064d 100644 (file)
@@ -77,7 +77,8 @@ early_mounts(void)
        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
index 5ac6e957c5bddcd35da0e93a3449f715e21d05ec..e6d3d0c2920414847617b38ed92a18a0214b1d51 100644 (file)
@@ -121,7 +121,5 @@ int mkdev(const char *name, int _mode)
        n_patterns = 1;
        find_devs(true);
        find_devs(false);
-       chdir("/");
-
-       return 0;
+       return chdir("/");
 }
index fb94527d50c002043d8500d17ebae61886e412c7..f38d8ef2a014acac7311a06a16c7a2401907debb 100644 (file)
@@ -38,7 +38,8 @@ check_dbglvl(void)
 
        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");
 
index 623103de640b1e7609933a175fce3dfed716e715..eb402f8587e04522fa91f579a062168deda69159 100644 (file)
--- a/inittab.c
+++ b/inittab.c
@@ -70,9 +70,11 @@ static int dev_open(const char *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;
@@ -83,9 +85,8 @@ static int dev_exist(const char *dev)
        int res;
 
        res = dev_open(dev);
-       if (res != -1) {
+       if (res != -1)
                close(res);
-       }
 
        return (res != -1);
 }
index 3b5587a9ddf77f88f5683a68e1f6196b9eeb2977..a6de1338a1e4e09fe7765b9e8db33fe9847163d5 100644 (file)
@@ -313,12 +313,16 @@ static int spawn_child(void *arg)
                        sysfs = 1;
                        break;
                case 'n':
-                       sethostname(optarg, strlen(optarg));
+                       if (sethostname(optarg, strlen(optarg)))
+                               ERROR("failed to sethostname: %s\n", strerror(errno));
                        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));
@@ -370,13 +374,17 @@ static void spawn_namespace(const char *path, int argc, char **argv)
        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) {
-               chdir(dir);
+               if (chdir(dir))
+                       ERROR("failed to chdir() out of the jail\n");
                free(dir);
                uloop_process_add(&namespace_process);
                uloop_run();
index 6df79717bd36cfffcb8dff90899869dd3db30eb5..1a98e8bb6f4be7245adc08c3f0ff78f437400414 100644 (file)
@@ -198,7 +198,10 @@ send_to_kernel:
                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);
@@ -214,7 +217,10 @@ send_to_kernel:
                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)
@@ -222,7 +228,8 @@ send_to_kernel:
        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);
index 8d2001aa044cb076c2c1ef2110bcd65aa742aeba..f5b61fa42c626bde52c11d80455235b9d47a44b1 100644 (file)
@@ -283,8 +283,10 @@ instance_run(struct service_instance *in, int _stdout, int _stderr)
        }
 
        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);
diff --git a/state.c b/state.c
index 22a06a14f9be2c61714e89494cd82bde97845281..1ed70f542f13d3ec7b8a3d3cf2fcf8e31cf42cd8 100644 (file)
--- a/state.c
+++ b/state.c
@@ -43,12 +43,14 @@ static int reboot_event;
 
 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)
@@ -70,7 +72,10 @@ static void set_console(void)
                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) {
@@ -81,7 +86,8 @@ static void set_console(void)
                tty=try[i];
                i++;
        }
-       chdir("/");
+       if (chdir("/"))
+               ERROR("failed to change dir to /\n");
 
        if (tty != NULL)
                set_stdio(tty);
index c6f32d780fb4a7025fb7bbb26e11421c50e3e4d9..12f0ee6a73ca1503b2cbef15e0f4ae7f2c13e496 100644 (file)
@@ -214,7 +214,8 @@ int main(int argc, char **argv, char **envp)
        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);
 
index 1e4057a782ddf412aa2f7361aa9cadb293f0d77c..d7433e76071524a5e69f0e153730b2801e5406b8 100644 (file)
@@ -18,6 +18,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <errno.h>
 
 #include <libubox/uloop.h>
 
@@ -55,12 +56,14 @@ int main(int argc, char **argv)
 {
        pid_t p = getpid();
 
-       chdir("/tmp");
-
        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;