From a5fa5b6144253726dcd7e769197eed787a723f5f Mon Sep 17 00:00:00 2001 From: John Crispin Date: Sat, 28 Mar 2015 18:32:22 +0100 Subject: [PATCH] properly handle return codes Signed-off-by: John Crispin --- block.c | 28 +++++++++++++++------------- libblkid-tiny/libblkid-tiny.c | 3 ++- libblkid-tiny/mkdev.c | 3 +-- libfstools/extroot.c | 3 ++- libfstools/overlay.c | 5 ++++- libfstools/snapshot.c | 10 ++++++++-- 6 files changed, 32 insertions(+), 20 deletions(-) diff --git a/block.c b/block.c index 4c479c0..a3db87b 100644 --- a/block.c +++ b/block.c @@ -1020,10 +1020,12 @@ static int check_extroot(char *path) return -1; } - fgets(uuid, sizeof(uuid), fp); + if (!fgets(uuid, sizeof(uuid), fp)) + ULOG_ERR("extroot: failed to read UUID from %s: %d (%s)\n", + tag, errno, strerror(errno)); fclose(fp); - if (!strcasecmp(uuid, pr->uuid)) + if (*uuid || !strcasecmp(uuid, pr->uuid)) return 0; ULOG_ERR("extroot: UUID mismatch (root: %s, %s: %s)\n", @@ -1360,18 +1362,18 @@ static int main_swapoff(int argc, char **argv) ULOG_ERR("failed to open /proc/swaps\n"); return -1; } - fgets(line, sizeof(line), fp); - while (fgets(line, sizeof(line), fp)) { - char *end = strchr(line, ' '); - int err; + if (fgets(line, sizeof(line), fp)) + while (fgets(line, sizeof(line), fp)) { + char *end = strchr(line, ' '); + int err; - if (!end) - continue; - *end = '\0'; - err = swapoff(line); - if (err) - ULOG_ERR("failed to swapoff %s (%d)\n", line, err); - } + if (!end) + continue; + *end = '\0'; + err = swapoff(line); + if (err) + ULOG_ERR("failed to swapoff %s (%d)\n", line, err); + } fclose(fp); } else { struct stat s; diff --git a/libblkid-tiny/libblkid-tiny.c b/libblkid-tiny/libblkid-tiny.c index 671c6c8..f66f3a1 100644 --- a/libblkid-tiny/libblkid-tiny.c +++ b/libblkid-tiny/libblkid-tiny.c @@ -197,7 +197,8 @@ int probe_block(char *block, struct blkid_struct_probe *pr) char magic[32] = { 0 }; lseek(pr->fd, off, SEEK_SET); - read(pr->fd, magic, mag->len); + if (read(pr->fd, magic, mag->len) < 0) + return -1; DEBUG("magic: %s %s %d\n", mag->magic, magic, mag->len); if (!memcmp(mag->magic, magic, mag->len)) diff --git a/libblkid-tiny/mkdev.c b/libblkid-tiny/mkdev.c index 06c0358..f1d504f 100644 --- a/libblkid-tiny/mkdev.c +++ b/libblkid-tiny/mkdev.c @@ -88,7 +88,6 @@ int mkblkdev(void) mode = 0600; find_devs(true); - chdir("/"); - return 0; + return chdir("/"); } diff --git a/libfstools/extroot.c b/libfstools/extroot.c index e548bb1..418df94 100644 --- a/libfstools/extroot.c +++ b/libfstools/extroot.c @@ -64,7 +64,8 @@ int mount_extroot(void) setenv("LD_LIBRARY_PATH", ldlib_path, 1); snprintf(kmod_loader, sizeof(kmod_loader), "/sbin/kmodloader %s/etc/modules-boot.d/", dirname(ldlib_path)); - system(kmod_loader); + if (system(kmod_loader)) + ULOG_ERR("failed to launch kmodloader from internal overlay\n"); } pid = fork(); diff --git a/libfstools/overlay.c b/libfstools/overlay.c index 3269a52..0677c34 100644 --- a/libfstools/overlay.c +++ b/libfstools/overlay.c @@ -143,7 +143,10 @@ switch2jffs(struct volume *v) return -1; } - system("cp -a /tmp/root/* /rom/overlay"); /**/ + if (system("cp -a /tmp/root/* /rom/overlay")) { + ULOG_ERR("failed - cp -a /tmp/root/* /rom/overlay: %s\n", strerror(errno)); + return -1; + } if (pivot("/rom", "/mnt")) { ULOG_ERR("failed - pivot /rom /mnt: %s\n", strerror(errno)); diff --git a/libfstools/snapshot.c b/libfstools/snapshot.c index 1ec7b87..936f2af 100644 --- a/libfstools/snapshot.c +++ b/libfstools/snapshot.c @@ -329,13 +329,19 @@ mount_snapshot(struct volume *v) snapshot_sync(v); setenv("SNAPSHOT", "magic", 1); _ramoverlay("/rom", "/overlay"); - system("/sbin/snapshot unpack"); + if (system("/sbin/snapshot unpack") == -1) { + perror("system"); + return -1; + } foreachdir("/overlay/", handle_whiteout); mkdir("/volatile", 0700); _ramoverlay("/rom", "/volatile"); mount_move("/rom/volatile", "/volatile", ""); mount_move("/rom/rom", "/rom", ""); - system("/sbin/snapshot config_unpack"); + if (system("/sbin/snapshot config_unpack")) { + perror("system"); + return -1; + } foreachdir("/volatile/", handle_whiteout); unsetenv("SNAPSHOT"); return -1; -- 2.30.2