Browse Source

properly handle return codes

Signed-off-by: John Crispin <blogic@openwrt.org>
John Crispin 9 years ago
parent
commit
a5fa5b6144
6 changed files with 32 additions and 20 deletions
  1. 15 13
      block.c
  2. 2 1
      libblkid-tiny/libblkid-tiny.c
  3. 1 2
      libblkid-tiny/mkdev.c
  4. 2 1
      libfstools/extroot.c
  5. 4 1
      libfstools/overlay.c
  6. 8 2
      libfstools/snapshot.c

+ 15 - 13
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;

+ 2 - 1
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))

+ 1 - 2
libblkid-tiny/mkdev.c

@@ -88,7 +88,6 @@ int mkblkdev(void)
 
 	mode = 0600;
 	find_devs(true);
-	chdir("/");
 
-	return 0;
+	return chdir("/");
 }

+ 2 - 1
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();

+ 4 - 1
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));

+ 8 - 2
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;