From 2dbf0fe532a66ac5f689901b44ce6e96d342d857 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Thu, 27 Oct 2022 14:01:40 -0500 Subject: [PATCH] Add swapoff -a -v and move octal_deslash() to lib for fstab parsing. --- lib/lib.c | 25 +++++++++++++++++++++++++ lib/lib.h | 1 + lib/portability.c | 24 ------------------------ toys/other/swapoff.c | 28 ++++++++++++++++++++++++---- 4 files changed, 50 insertions(+), 28 deletions(-) diff --git a/lib/lib.c b/lib/lib.c index ffa781cb..800bcc15 100644 --- a/lib/lib.c +++ b/lib/lib.c @@ -1508,3 +1508,28 @@ char *elf_arch_name(int type) sprintf(libbuf, "unknown arch %d", type); return libbuf; } + +// Remove octal escapes from string (common in kernel exports) +void octal_deslash(char *s) +{ + char *o = s; + + while (*s) { + if (*s == '\\') { + int i, oct = 0; + + for (i = 1; i < 4; i++) { + if (!isdigit(s[i])) break; + oct = (oct<<3)+s[i]-'0'; + } + if (i == 4) { + *o++ = oct; + s += i; + continue; + } + } + *o++ = *s++; + } + + *o = 0; +} diff --git a/lib/lib.h b/lib/lib.h index 83234b3e..d74d93a3 100644 --- a/lib/lib.h +++ b/lib/lib.h @@ -271,6 +271,7 @@ void loggit(int priority, char *format, ...); unsigned tar_cksum(void *data); int is_tar_header(void *pkt); char *elf_arch_name(int type); +void octal_deslash(char *s); #define HR_SPACE 1 // Space between number and units #define HR_B 2 // Use "B" for single byte units diff --git a/lib/portability.c b/lib/portability.c index 443d31f9..073a36d8 100644 --- a/lib/portability.c +++ b/lib/portability.c @@ -97,30 +97,6 @@ struct mtab_list *xgetmountlist(char *path) #include -static void octal_deslash(char *s) -{ - char *o = s; - - while (*s) { - if (*s == '\\') { - int i, oct = 0; - - for (i = 1; i < 4; i++) { - if (!isdigit(s[i])) break; - oct = (oct<<3)+s[i]-'0'; - } - if (i == 4) { - *o++ = oct; - s += i; - continue; - } - } - *o++ = *s++; - } - - *o = 0; -} - // Check if this type matches list. // Odd syntax: typelist all yes = if any, typelist all no = if none. diff --git a/toys/other/swapoff.c b/toys/other/swapoff.c index fb171300..bd6a41a9 100644 --- a/toys/other/swapoff.c +++ b/toys/other/swapoff.c @@ -2,20 +2,40 @@ * * Copyright 2012 Elie De Brauwer -USE_SWAPOFF(NEWTOY(swapoff, "<1>1", TOYFLAG_SBIN|TOYFLAG_NEEDROOT)) +USE_SWAPOFF(NEWTOY(swapoff, "<1>1av", TOYFLAG_SBIN|TOYFLAG_NEEDROOT)) config SWAPOFF bool "swapoff" default y help - usage: swapoff swapregion + usage: swapoff FILE - Disable swapping on a given swapregion. + Disable swapping on a device or file. */ +#define FOR_swapoff #include "toys.h" +static void xswapoff(char *str) +{ + if (FLAG(v)) printf("swapoff %s", str); + if (swapoff(str)) perror_msg("failed to remove swaparea"); +} + void swapoff_main(void) { - if (swapoff(toys.optargs[0])) perror_exit("failed to remove swaparea"); + char *ss, *line, **args; + FILE *fp; + + if (FLAG(a) && (fp = fopen("/proc/swaps", "r"))) { + while ((line = xgetline(fp))) { + if (*line != '/' || !(ss = strchr(line, ' '))) continue; + *ss = 0; + octal_deslash(line); + xswapoff(line); + free(line); + } + fclose(fp); + } + for (args = toys.optargs; *args; args++) xswapoff(*args); } -- 2.39.2