[hackers] [ubase] Add swaplabel(8) || Jan Tatje

From: <git_AT_suckless.org>
Date: Sat, 29 Nov 2014 22:23:28 +0100

commit aed9e3f6fa85851a35ad7bfb1b925e4feeb1a240
Author: Jan Tatje <jan_AT_jnt.io>
Date: Sat Nov 29 21:09:39 2014 +0000

    Add swaplabel(8)

diff --git a/LICENSE b/LICENSE
index 29531aa..3a661ad 100644
--- a/LICENSE
+++ b/LICENSE
_AT_@ -9,6 +9,7 @@ MIT/X Consortium License
 © 2014 Hiltjo Posthuma <hiltjo_AT_codemadness.org>
 © 2014 Laslo Hunhold <dev_AT_frign.de>
 © 2014 Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
+© 2014 Jan Tatje <jan_AT_jnt.io>
 
 Permission is hereby granted, free of charge, to any person obtaining a
 copy of this software and associated documentation files (the "Software"),
diff --git a/Makefile b/Makefile
index d267535..150ca13 100644
--- a/Makefile
+++ b/Makefile
_AT_@ -70,6 +70,7 @@ SRC = \
         rmmod.c \
         stat.c \
         su.c \
+ swaplabel.c \
         swapoff.c \
         swapon.c \
         switch_root.c \
_AT_@ -125,6 +126,7 @@ MAN8 = \
         pivot_root.8 \
         readahead.8 \
         rmmod.8 \
+ swaplabel.8 \
         swapoff.8 \
         swapon.8 \
         switch_root.8 \
diff --git a/TODO b/TODO
index 681e77d..c33c2ec 100644
--- a/TODO
+++ b/TODO
_AT_@ -4,7 +4,6 @@ Tools
 vmstat
 top
 Better ps support
-swaplabel
 losetup
 lspci
 mkswap [-L]
diff --git a/swaplabel.8 b/swaplabel.8
new file mode 100644
index 0000000..d442626
--- /dev/null
+++ b/swaplabel.8
_AT_@ -0,0 +1,11 @@
+.TH SWAPLABEL 8 ubase-VERSION
+.SH NAME
+\fBswaplabel\fR - set the label of a swap filesystem
+.SH SYNOPSIS
+\fBswaplabel\fR [\fB-L\fI label\fR] \fIdevice\fR
+.SH DESCRIPTION
+\fBswaplabel\fR is used to change the label of a swap device or file.
+.SH OPTIONS
+.TP
+\fB-L\fR
+Change the label.
diff --git a/swaplabel.c b/swaplabel.c
new file mode 100644
index 0000000..9f89b21
--- /dev/null
+++ b/swaplabel.c
_AT_@ -0,0 +1,80 @@
+/* See LICENSE file for copyright and license details. */
+#include <sys/types.h>
+
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "util.h"
+
+#define SWAP_MAGIC1 "SWAPSPACE2"
+#define SWAP_MAGIC2 "SWAP-SPACE"
+#define SWAP_MAGIC_LENGTH (10)
+#define SWAP_MAGIC_OFFSET (sysconf(_SC_PAGESIZE) - SWAP_MAGIC_LENGTH)
+#define SWAP_LABEL_LENGTH (16)
+#define SWAP_LABEL_OFFSET (1024 + 4 + 4 + 4 + 16)
+
+static void
+usage(void)
+{
+ eprintf("usage: %s [-L label] device\n", argv0);
+}
+
+int
+main(int argc, char *argv[])
+{
+ int setlabel = 0;
+ int fd = -1;
+ char magic[10] = { 0 };
+ char *label = NULL;
+ char *device = NULL;
+ int i;
+
+ ARGBEGIN {
+ case 'L':
+ setlabel = 1;
+ label = EARGF(usage());
+ break;
+ default:
+ usage();
+ } ARGEND;
+
+ if (argc < 1)
+ usage();
+ device = argv[0];
+
+ fd = open(device, O_RDWR);
+ if (fd < 0)
+ eprintf("open %s:", device);
+
+ if (lseek(fd, SWAP_MAGIC_OFFSET, SEEK_SET) != SWAP_MAGIC_OFFSET)
+ eprintf("failed seeking to magic position:");
+ if (read(fd, magic, SWAP_MAGIC_LENGTH) != SWAP_MAGIC_LENGTH)
+ eprintf("reading magic failed:");
+ if (memcmp(magic, SWAP_MAGIC1, 10) && memcmp(magic, SWAP_MAGIC2, 10))
+ eprintf("%s: is not a swap partition\n", device);
+ if (lseek(fd, SWAP_LABEL_OFFSET, SEEK_SET) != SWAP_LABEL_OFFSET)
+ eprintf("failed seeking to label position:");
+
+ if (!setlabel) {
+ label = emalloc(SWAP_LABEL_LENGTH);
+ if (read(fd, label, SWAP_LABEL_LENGTH) != SWAP_LABEL_LENGTH)
+ eprintf("reading label failed:");
+ for (i = 0; i < SWAP_LABEL_LENGTH && label[i] != '\0'; i++)
+ if (i == (SWAP_LABEL_LENGTH - 1) && label[i] != '\0')
+ eprintf("invalid label\n");
+ printf("label: %s\n", label);
+ free(label);
+ } else {
+ if (strlen(label) + 1 > SWAP_LABEL_LENGTH)
+ eprintf("label too long\n");
+ if (write(fd, label, strlen(label) + 1) != (ssize_t)strlen(label) + 1)
+ eprintf("writing label failed:");
+ }
+
+ fsync(fd);
+ close(fd);
+ return 0;
+}
Received on Sat Nov 29 2014 - 22:23:28 CET

This archive was generated by hypermail 2.3.0 : Sat Nov 29 2014 - 22:24:10 CET