[PATCH] fstools: block: remove the swapon/swapoff applets

Rui Salvaterra rsalvaterra at gmail.com
Wed Jul 15 09:59:19 EDT 2020


The swapon/swapoff applets enabled by default in BusyBox have more features than
the ones from block-mount's block app, which makes them redundant.

This patch removes only those applets, while both keeping all the internal
functionality required to handle hotplug/fstab swap and also shaving off a few
bytes from the block executable:

Before:	92724 bytes
After:	89532 bytes

Signed-off-by: Rui Salvaterra <rsalvaterra at gmail.com>
---
 block.c | 127 --------------------------------------------------------
 1 file changed, 127 deletions(-)

diff --git a/block.c b/block.c
index 569bf56..ffb92d9 100644
--- a/block.c
+++ b/block.c
@@ -1691,127 +1691,6 @@ static int main_info(int argc, char **argv)
 	return 0;
 }
 
-static int swapon_usage(void)
-{
-	fprintf(stderr, "Usage: swapon [-s] [-a] [[-p pri] DEVICE]\n\n"
-		"\tStart swapping on [DEVICE]\n"
-		" -a\tStart swapping on all swap devices\n"
-		" -p pri\tSet priority of swap device\n"
-		" -s\tShow summary\n");
-	return -1;
-}
-
-static int main_swapon(int argc, char **argv)
-{
-	int ch;
-	FILE *fp;
-	char *lineptr;
-	size_t s;
-	struct probe_info *pr;
-	int flags = 0;
-	int pri;
-	struct stat st;
-	int err;
-
-	while ((ch = getopt(argc, argv, "ap:s")) != -1) {
-		switch(ch) {
-		case 's':
-			fp = fopen("/proc/swaps", "r");
-			lineptr = NULL;
-
-			if (!fp) {
-				ULOG_ERR("failed to open /proc/swaps\n");
-				return -1;
-			}
-			while (getline(&lineptr, &s, fp) > 0)
-				printf("%s", lineptr);
-			if (lineptr)
-				free(lineptr);
-			fclose(fp);
-			return 0;
-		case 'a':
-			cache_load(0);
-			list_for_each_entry(pr, &devices, list) {
-				if (strcmp(pr->type, "swap"))
-					continue;
-				if (swapon(pr->dev, 0))
-					ULOG_ERR("failed to swapon %s\n", pr->dev);
-			}
-			return 0;
-		case 'p':
-			pri = atoi(optarg);
-			if (pri >= 0)
-				flags = ((pri << SWAP_FLAG_PRIO_SHIFT) & SWAP_FLAG_PRIO_MASK) | SWAP_FLAG_PREFER;
-			break;
-		default:
-			return swapon_usage();
-		}
-	}
-
-	if (optind != (argc - 1))
-		return swapon_usage();
-
-	if (stat(argv[optind], &st) || (!S_ISBLK(st.st_mode) && !S_ISREG(st.st_mode))) {
-		ULOG_ERR("%s is not a block device or file\n", argv[optind]);
-		return -1;
-	}
-	err = swapon(argv[optind], flags);
-	if (err) {
-		ULOG_ERR("failed to swapon %s (%d)\n", argv[optind], err);
-		return err;
-	}
-
-	return 0;
-}
-
-static int main_swapoff(int argc, char **argv)
-{
-	if (argc != 2) {
-		ULOG_ERR("Usage: swapoff [-a] [DEVICE]\n\n"
-			"\tStop swapping on DEVICE\n"
-			" -a\tStop swapping on all swap devices\n");
-		return -1;
-	}
-
-	if (!strcmp(argv[1], "-a")) {
-		FILE *fp = fopen("/proc/swaps", "r");
-		char line[256];
-
-		if (!fp) {
-			ULOG_ERR("failed to open /proc/swaps\n");
-			return -1;
-		}
-		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);
-			}
-		fclose(fp);
-	} else {
-		struct stat s;
-		int err;
-
-		if (stat(argv[1], &s) || (!S_ISBLK(s.st_mode) && !S_ISREG(s.st_mode))) {
-			ULOG_ERR("%s is not a block device or file\n", argv[1]);
-			return -1;
-		}
-		err = swapoff(argv[1]);
-		if (err) {
-			ULOG_ERR("failed to swapoff %s (%d)\n", argv[1], err);
-			return err;
-		}
-	}
-
-	return 0;
-}
-
 int main(int argc, char **argv)
 {
 	char *base = basename(*argv);
@@ -1821,12 +1700,6 @@ int main(int argc, char **argv)
 	ulog_open(-1, -1, "block");
 	ulog_threshold(LOG_NOTICE);
 
-	if (!strcmp(base, "swapon"))
-		return main_swapon(argc, argv);
-
-	if (!strcmp(base, "swapoff"))
-		return main_swapoff(argc, argv);
-
 	if ((argc > 1) && !strcmp(base, "block")) {
 		if (!strcmp(argv[1], "info"))
 			return main_info(argc, argv);
-- 
2.27.0




More information about the openwrt-devel mailing list