xref: /aosp_15_r20/external/toybox/toys/other/swapoff.c (revision cf5a6c84e2b8763fc1a7db14496fd4742913b199)
1 /* swapoff.c - Disable region for swapping
2  *
3  * Copyright 2012 Elie De Brauwer <[email protected]>
4 
5 USE_SWAPOFF(NEWTOY(swapoff, "<1>1av", TOYFLAG_SBIN|TOYFLAG_NEEDROOT))
6 
7 config SWAPOFF
8   bool "swapoff"
9   default y
10   help
11     usage: swapoff FILE
12 
13     Disable swapping on a device or file.
14 */
15 
16 #define FOR_swapoff
17 #include "toys.h"
18 
xswapoff(char * str)19 static void xswapoff(char *str)
20 {
21   if (FLAG(v)) printf("swapoff %s", str);
22   if (swapoff(str)) perror_msg("failed to remove swaparea");
23 }
24 
swapoff_main(void)25 void swapoff_main(void)
26 {
27   char *ss, *line, **args;
28   FILE *fp;
29 
30   if (FLAG(a) && (fp = fopen("/proc/swaps", "r"))) {
31     while ((line = xgetline(fp))) {
32       if (*line != '/' || !(ss = strchr(line, ' '))) continue;
33       *ss = 0;
34       octal_deslash(line);
35       xswapoff(line);
36       free(line);
37     }
38     fclose(fp);
39   }
40   for (args = toys.optargs; *args; args++) xswapoff(*args);
41 }
42