Sfoglia il codice sorgente

Add swapoff -a -v and move octal_deslash() to lib for fstab parsing.

Rob Landley 1 anno fa
parent
commit
2dbf0fe532
4 ha cambiato i file con 50 aggiunte e 28 eliminazioni
  1. 25 0
      lib/lib.c
  2. 1 0
      lib/lib.h
  3. 0 24
      lib/portability.c
  4. 24 4
      toys/other/swapoff.c

+ 25 - 0
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;
+}

+ 1 - 0
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

+ 0 - 24
lib/portability.c

@@ -97,30 +97,6 @@ struct mtab_list *xgetmountlist(char *path)
 
 #include <mntent.h>
 
-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.
 

+ 24 - 4
toys/other/swapoff.c

@@ -2,20 +2,40 @@
  *
  * Copyright 2012 Elie De Brauwer <eliedebrauwer@gmail.com>
 
-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);
 }