diff options
author | Laurent Bercot <ska-skaware@skarnet.org> | 2014-09-19 15:01:58 +0000 |
---|---|---|
committer | Laurent Bercot <ska-skaware@skarnet.org> | 2014-09-19 15:01:58 +0000 |
commit | 54b6467013bfbdb3ee606961c02fbff1271ca582 (patch) | |
tree | e3969743e61542979f12fca01017e67c4480eca8 /src/minutils/s6-swapoff.c | |
download | s6-linux-utils-54b6467013bfbdb3ee606961c02fbff1271ca582.tar.xz |
initial commit
Diffstat (limited to 'src/minutils/s6-swapoff.c')
-rw-r--r-- | src/minutils/s6-swapoff.c | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/src/minutils/s6-swapoff.c b/src/minutils/s6-swapoff.c new file mode 100644 index 0000000..1067040 --- /dev/null +++ b/src/minutils/s6-swapoff.c @@ -0,0 +1,53 @@ +/* ISC license. */ + +#include <errno.h> +#include <skalibs/bytestr.h> +#include <skalibs/buffer.h> +#include <skalibs/strerr2.h> +#include <skalibs/stralloc.h> +#include <skalibs/djbunix.h> +#include <skalibs/skamisc.h> + +extern int swapoff (char const *) ; + +#define USAGE "s6-swapoff device <or> s6-swapoff -a" + +#define BUFSIZE 4096 + +static int swapoffall ( ) +{ + char buf[BUFSIZE+1] ; + buffer b ; + stralloc sa = STRALLOC_ZERO ; + int e = 0 ; + int r ; + int fd = open_readb("/proc/swaps") ; + if (fd < 0) strerr_diefu1sys(111, "open_readb /proc/swaps") ; + buffer_init(&b, &buffer_read, fd, buf, BUFSIZE+1) ; + if (skagetln(&b, &sa, '\n') < 0) strerr_diefu1sys(111, "skagetln") ; + for (;;) + { + unsigned int n ; + sa.len = 0 ; + r = skagetln(&b, &sa, '\n') ; + if (r < 0) strerr_diefu1sys(111, "skagetln") ; + if (!r) break ; + n = byte_chr(sa.s, sa.len, ' ') ; + if (n >= sa.len) strerr_dief1x(111, "invalid line in /proc/swaps") ; + sa.s[n] = 0 ; + if (swapoff(sa.s) < 0) { e++ ; strerr_warnwu2sys("swapoff ", sa.s) ; } + } + fd_close(fd) ; + stralloc_free(&sa) ; + return e ; +} + +int main (int argc, char const *const *argv) +{ + PROG = "s6-swapoff" ; + if (argc < 2) strerr_dieusage(100, USAGE) ; + if ((argv[1][0] == '-') && (argv[1][1] == 'a') && !argv[1][2]) + return swapoffall() ; + if (swapoff(argv[1]) == -1) strerr_diefu2sys(111, "swapoff ", argv[1]) ; + return 0 ; +} |