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=
> :t? 
> :This name reflects it's purpose much better and would be consistent
> with the 
> :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.

-- 

Eirik Nygaard <[EMAIL PROTECTED]>
PGP Key: 83C55EDE

Index: sbin/swapon/Makefile
===================================================================
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 @@
 
 PROG=  swapon
 MAN=   swapon.8
-LINKS= ${BINDIR}/swapon ${BINDIR}/swapoff
+LINKS= ${BINDIR}/swapoff ${BINDIR}/swapon
+LINKS+=        ${BINDIR}/swapctl ${BINDIR}/swapon
 MLINKS=        swapon.8 swapoff.8
+LDADD=   -lc -lkvm
 
 .include <bsd.prog.mk>
Index: sbin/swapon/swapon.c
===================================================================
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>
 
-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);
 
 int
 main(int argc, char **argv)
@@ -63,41 +70,68 @@
        struct fstab *fsp;
        int stat;
        int ch, doall;
-       int do_swapoff;
-       char *pname = argv[0];
-
-       do_swapoff = is_swapoff(pname);
-
+       int do_swapoff, do_swapon;
+       int sflag = 0, lflag = 0, hflag = 0;
+       
+       do_swapoff = is_swapoff(getprogname());
+       do_swapon  = is_swapon(getprogname());
+       
        doall = 0;
-       while ((ch = getopt(argc, argv, "a")) != -1)
-               switch((char)ch) {
+       while ((ch = getopt(argc, argv, "alhsU")) != -1)
+               switch(ch) {
                case 'a':
                        doall = 1;
                        break;
+               case 's':
+                       sflag = 1;
+                       break;
+               case 'l':
+                       lflag = 1;
+                       break;
+               case 'h':
+                       hflag = 1;
+                       break;
+               case 'U':
+                       if (!do_swapon) {
+                               doall = 1;
+                               do_swapoff = 1;
+                               break;
+                       } /* Remove the if if you want the U switch to work with 
+swapon also, don't know if that is wanted */
                case '?':
                default:
-                       usage(pname);
+                       usage();
                }
        argv += optind;
-
+       
        stat = 0;
-       if (doall)
-               while ((fsp = getfsent()) != 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 = 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 = getfsent()) != 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 = 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 |= swap_on_off(*argv, 0, do_swapoff);
+               else if (!*argv)
+                       usage();
+               for (; *argv; ++argv)
+                       stat |= swap_on_off(*argv, 0, do_swapoff);
+       }
+       else {
+               if (lflag == 1 || sflag == 1)
+                       swaplist(lflag, sflag, hflag);
+
+               else 
+                       usage();
+       }
+       
        exit(stat);
 }
 
@@ -120,23 +154,95 @@
 }
 
 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(), 
+               is_swapctl ? "lsU" : "");
        exit(1);
 }
 
 static int
 is_swapoff(const char *s)
 {
-       const char *u;
+       if (strcmp(s, "swapoff") == 0)
+               return 1;
+       else
+               return 0;
+}
 
-       if ((u = strrchr(s, '/')) != NULL)
-               ++u;
+static int
+is_swapon(const char *s)
+{
+       if (strcmp(s, "swapon") == 0)
+               return 1;
        else
-               u = s;
-       if (strcmp(u, "swapoff") == 0)
+               return 0;
+}
+
+static int
+is_swapctl(const char *s)
+{
+       if (strcmp(s, "swapctl") == 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 = 0;
+       int used, total;
+       
+       if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null", O_RDONLY, 
+"kvm_open")) < 0) {
+               perror("kvm_open");
+               exit(1);
+       }
+       if (kvm_getswapinfo(kd, swapinfo, MAXSWAP, 0) < -1) {
+               fprintf(stderr, "Error getting swap information.");
+               exit(2);
+       }
+       
+       if (lflag)
+               printf("%-*s %s%10s %s%10s\n",
+                       13, "Device:", 
+                                 hflag ? " " : "", "Total:",
+                                 hflag ? " " : "", "Used:");
+       
+       pagesize = getpagesize();
+       used = total = 0;
+       while (swapinfo[i + 1].ksw_total != 0) {
+               if (lflag) {
+                       total = swapinfo[i].ksw_total * pagesize / 1024;
+                       used  = swapinfo[i].ksw_used * pagesize / 1024;
+                       if (hflag) {
+                               total = total / 1000;
+                               used  = used / 1000;
+                       }
+               
+                       printf("%-*s %10d%s %10d%s\n", 
+                                         13, swapinfo[i].ksw_devname, 
+                                         total, hflag ? "M" : "",
+                                         used, hflag ? "M" : "");
+               }
+               if (sflag) {
+                       total += swapinfo[i].ksw_total * pagesize / 1024;
+                       used  += swapinfo[i].ksw_used * pagesize / 1024;
+               }
+               i++;
+       }
+       if (hflag) {
+               total = total / 1000;
+               used  = used / 1000;
+       }
+       if (sflag)
+               printf("Swap: Total: %8d%s  Used: %8d%s\n",
+                      total, hflag ? "M" : "",
+                      used, hflag ? "M" : "");
+       
+       kvm_close(kd);
+       
+}
+

Attachment: msg49004/pgp00000.pgp
Description: PGP signature

Reply via email to