Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 18 Dec 2002 19:27:24 +0100
From:      Eirik Nygaard <eirikn@bluezone.no>
To:        current@FreeBSD.ORG
Subject:   Re: swapoff code comitted.
Message-ID:  <20021218182724.GB853@eirikn.net>
In-Reply-To: <200212152247.gBFMlp4d098705@apollo.backplane.com>
References:  <200212151946.gBFJktmo090730@apollo.backplane.com> <20021215223540.GA601@unixpages.org> <200212152247.gBFMlp4d098705@apollo.backplane.com>

next in thread | previous in thread | raw e-mail | index | archive | help

--V88s5gaDVPzZ0KCq
Content-Type: multipart/mixed; boundary="98e8jtXdkpgskNou"
Content-Disposition: inline


--98e8jtXdkpgskNou
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Sun, Dec 15, 2002 at 02:47:51PM -0800, Matthew Dillon wrote:
> :
> :How about renaming swapon(8) into swapctl(8) after this function
> enhancemen=3D
> :t?=20
> :This name reflects it's purpose much better and would be consistent
> with the=20
> :other BSDs.
> :
> :- Christian
>
>     I am not volunteering to do this, at least not right now.  I
>     have too
>     big a stack of things that still need to be committed, but if
>     someone
>     else would like to tackle this I think it would be a nice little
>     project
>     for a developer with some free time to waste and I would be
>     happy to
>     review and test the work.
>

I have made a small patch, added l, s and h switches to show
information about the swap devices. And the U switch to swapctl only
to remove all activated swap devices.
If anything else is needed let me know and I will add it.

--=20

Eirik Nygaard <eirikn@bluezone.no>
PGP Key: 83C55EDE


--98e8jtXdkpgskNou
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="swapctl.diff"
Content-Transfer-Encoding: quoted-printable

Index: sbin/swapon/Makefile
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /home/ncvs/src/sbin/swapon/Makefile,v
retrieving revision 1.7
diff -u -r1.7 Makefile
--- sbin/swapon/Makefile	15 Dec 2002 19:17:56 -0000	1.7
+++ sbin/swapon/Makefile	17 Dec 2002 17:00:47 -0000
@@ -3,7 +3,9 @@
=20
 PROG=3D	swapon
 MAN=3D	swapon.8
-LINKS=3D	${BINDIR}/swapon ${BINDIR}/swapoff
+LINKS=3D	${BINDIR}/swapoff ${BINDIR}/swapon
+LINKS+=3D	${BINDIR}/swapctl ${BINDIR}/swapon
 MLINKS=3D	swapon.8 swapoff.8
+LDADD=3D   -lc -lkvm
=20
 .include <bsd.prog.mk>
Index: sbin/swapon/swapon.c
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /home/ncvs/src/sbin/swapon/swapon.c,v
retrieving revision 1.13
diff -u -r1.13 swapon.c
--- sbin/swapon/swapon.c	15 Dec 2002 19:17:56 -0000	1.13
+++ sbin/swapon/swapon.c	17 Dec 2002 17:00:47 -0000
@@ -52,10 +52,17 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <kvm.h>
+#include <fcntl.h>
=20
-static void usage(const char *);
+#define MAXSWAP 100
+
+static void usage(void);
 static int is_swapoff(const char *);
+static int is_swapon(const char *);
+static int is_swapctl(const char *);
 int	swap_on_off(char *name, int ignoreebusy, int do_swapoff);
+void	swaplist(int, int, int);
=20
 int
 main(int argc, char **argv)
@@ -63,41 +70,68 @@
 	struct fstab *fsp;
 	int stat;
 	int ch, doall;
-	int do_swapoff;
-	char *pname =3D argv[0];
-
-	do_swapoff =3D is_swapoff(pname);
-
+	int do_swapoff, do_swapon;
+	int sflag =3D 0, lflag =3D 0, hflag =3D 0;
+=09
+	do_swapoff =3D is_swapoff(getprogname());
+	do_swapon  =3D is_swapon(getprogname());
+=09
 	doall =3D 0;
-	while ((ch =3D getopt(argc, argv, "a")) !=3D -1)
-		switch((char)ch) {
+	while ((ch =3D getopt(argc, argv, "alhsU")) !=3D -1)
+		switch(ch) {
 		case 'a':
 			doall =3D 1;
 			break;
+		case 's':
+			sflag =3D 1;
+			break;
+		case 'l':
+			lflag =3D 1;
+			break;
+		case 'h':
+			hflag =3D 1;
+			break;
+		case 'U':
+			if (!do_swapon) {
+				doall =3D 1;
+				do_swapoff =3D 1;
+				break;
+			} /* Remove the if if you want the U switch to work with swapon also, d=
on't know if that is wanted */
 		case '?':
 		default:
-			usage(pname);
+			usage();
 		}
 	argv +=3D optind;
-
+=09
 	stat =3D 0;
-	if (doall)
-		while ((fsp =3D getfsent()) !=3D NULL) {
-			if (strcmp(fsp->fs_type, FSTAB_SW))
-				continue;
-			if (strstr(fsp->fs_mntops, "noauto"))
-				continue;
-			if (swap_on_off(fsp->fs_spec, 1, do_swapoff))
-				stat =3D 1;
-			else
-				printf("%s: %sing %s as swap device\n",
-				    pname, do_swapoff ? "remov" : "add",
-				    fsp->fs_spec);
+	if (do_swapoff || do_swapon) {
+		if (doall) {
+			while ((fsp =3D getfsent()) !=3D NULL) {
+				if (strcmp(fsp->fs_type, FSTAB_SW))
+					continue;
+				if (strstr(fsp->fs_mntops, "noauto"))
+					continue;
+				if (swap_on_off(fsp->fs_spec, 1, do_swapoff))
+					stat =3D 1;
+				else
+					printf("%s: %sing %s as swap device\n",
+					    getprogname(), do_swapoff ? "remov" : "add",
+					    fsp->fs_spec);
+			}
 		}
-	else if (!*argv)
-		usage(pname);
-	for (; *argv; ++argv)
-		stat |=3D swap_on_off(*argv, 0, do_swapoff);
+		else if (!*argv)
+			usage();
+		for (; *argv; ++argv)
+			stat |=3D swap_on_off(*argv, 0, do_swapoff);
+	}
+	else {
+		if (lflag =3D=3D 1 || sflag =3D=3D 1)
+			swaplist(lflag, sflag, hflag);
+
+		else=20
+			usage();
+	}
+=09
 	exit(stat);
 }
=20
@@ -120,23 +154,95 @@
 }
=20
 static void
-usage(const char *pname)
+usage(void)
 {
-	fprintf(stderr, "usage: %s [-a] [special_file ...]\n", pname);
+	fprintf(stderr, "usage: %s [-a%s] [special_file ...]\n", getprogname(),=
=20
+	        is_swapctl ? "lsU" : "");
 	exit(1);
 }
=20
 static int
 is_swapoff(const char *s)
 {
-	const char *u;
+	if (strcmp(s, "swapoff") =3D=3D 0)
+		return 1;
+	else
+		return 0;
+}
=20
-	if ((u =3D strrchr(s, '/')) !=3D NULL)
-		++u;
+static int
+is_swapon(const char *s)
+{
+	if (strcmp(s, "swapon") =3D=3D 0)
+		return 1;
 	else
-		u =3D s;
-	if (strcmp(u, "swapoff") =3D=3D 0)
+		return 0;
+}
+
+static int
+is_swapctl(const char *s)
+{
+	if (strcmp(s, "swapctl") =3D=3D 0)
 		return 1;
 	else
 		return 0;
 }
+
+void
+swaplist(int lflag, int sflag, int hflag)
+{
+	struct kvm_swap swapinfo[MAXSWAP];
+	kvm_t *kd;
+	int pagesize, i =3D 0;
+	int used, total;
+=09
+	if ((kd =3D kvm_open("/dev/null", "/dev/null", "/dev/null", O_RDONLY, "kv=
m_open")) < 0) {
+		perror("kvm_open");
+		exit(1);
+	}
+	if (kvm_getswapinfo(kd, swapinfo, MAXSWAP, 0) < -1) {
+		fprintf(stderr, "Error getting swap information.");
+		exit(2);
+	}
+=09
+	if (lflag)
+		printf("%-*s %s%10s %s%10s\n",
+		        13, "Device:",=20
+				  hflag ? " " : "", "Total:",
+				  hflag ? " " : "", "Used:");
+=09
+	pagesize =3D getpagesize();
+	used =3D total =3D 0;
+	while (swapinfo[i + 1].ksw_total !=3D 0) {
+		if (lflag) {
+			total =3D swapinfo[i].ksw_total * pagesize / 1024;
+			used  =3D swapinfo[i].ksw_used * pagesize / 1024;
+			if (hflag) {
+				total =3D total / 1000;
+				used  =3D used / 1000;
+			}
+	=09
+			printf("%-*s %10d%s %10d%s\n",=20
+					  13, swapinfo[i].ksw_devname,=20
+					  total, hflag ? "M" : "",
+					  used, hflag ? "M" : "");
+		}
+		if (sflag) {
+			total +=3D swapinfo[i].ksw_total * pagesize / 1024;
+			used  +=3D swapinfo[i].ksw_used * pagesize / 1024;
+		}
+		i++;
+	}
+	if (hflag) {
+		total =3D total / 1000;
+		used  =3D used / 1000;
+	}
+	if (sflag)
+		printf("Swap: Total: %8d%s  Used: %8d%s\n",
+		       total, hflag ? "M" : "",
+		       used, hflag ? "M" : "");
+=09
+	kvm_close(kd);
+=09
+}
+

--98e8jtXdkpgskNou--

--V88s5gaDVPzZ0KCq
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (FreeBSD)

iD8DBQE+AL4M1JB0Z4PFXt4RAhpsAKCPIGca66OoPd67XQoUPPiBn3+pugCdFF0W
3ms6ed+c7PrAPGJqMVaG4KM=
=KhPi
-----END PGP SIGNATURE-----

--V88s5gaDVPzZ0KCq--

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-current" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20021218182724.GB853>