From: John Crispin Date: Sat, 28 Mar 2015 14:41:58 +0000 (+0100) Subject: properly handle return codes X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=91da63d3d3fd680c805dd1a1b78df5b8731a8173;p=oweals%2Fprocd.git properly handle return codes Signed-off-by: John Crispin --- diff --git a/initd/early.c b/initd/early.c index 5ee49ee..593449b 100644 --- a/initd/early.c +++ b/initd/early.c @@ -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 diff --git a/initd/mkdev.c b/initd/mkdev.c index 5ac6e95..e6d3d0c 100644 --- a/initd/mkdev.c +++ b/initd/mkdev.c @@ -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("/"); } diff --git a/initd/preinit.c b/initd/preinit.c index fb94527..f38d8ef 100644 --- a/initd/preinit.c +++ b/initd/preinit.c @@ -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"); diff --git a/inittab.c b/inittab.c index 623103d..eb402f8 100644 --- 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); } diff --git a/jail/jail.c b/jail/jail.c index 3b5587a..a6de133 100644 --- a/jail/jail.c +++ b/jail/jail.c @@ -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(); diff --git a/plug/hotplug.c b/plug/hotplug.c index 6df7971..1a98e8b 100644 --- a/plug/hotplug.c +++ b/plug/hotplug.c @@ -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); diff --git a/service/instance.c b/service/instance.c index 8d2001a..f5b61fa 100644 --- a/service/instance.c +++ b/service/instance.c @@ -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 22a06a1..1ed70f5 100644 --- 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); diff --git a/trace/trace.c b/trace/trace.c index c6f32d7..12f0ee6 100644 --- a/trace/trace.c +++ b/trace/trace.c @@ -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); diff --git a/upgraded/upgraded.c b/upgraded/upgraded.c index 1e4057a..d7433e7 100644 --- a/upgraded/upgraded.c +++ b/upgraded/upgraded.c @@ -18,6 +18,7 @@ #include #include #include +#include #include @@ -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;