11558Srgrimes/* 21558Srgrimes * Copyright (c) 1980, 1993 31558Srgrimes * The Regents of the University of California. All rights reserved. 41558Srgrimes * 51558Srgrimes * Redistribution and use in source and binary forms, with or without 61558Srgrimes * modification, are permitted provided that the following conditions 71558Srgrimes * are met: 81558Srgrimes * 1. Redistributions of source code must retain the above copyright 91558Srgrimes * notice, this list of conditions and the following disclaimer. 101558Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111558Srgrimes * notice, this list of conditions and the following disclaimer in the 121558Srgrimes * documentation and/or other materials provided with the distribution. 131558Srgrimes * 4. Neither the name of the University nor the names of its contributors 141558Srgrimes * may be used to endorse or promote products derived from this software 151558Srgrimes * without specific prior written permission. 161558Srgrimes * 171558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 181558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 191558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 201558Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 211558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 221558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 231558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 241558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 251558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 261558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 271558Srgrimes * SUCH DAMAGE. 281558Srgrimes */ 291558Srgrimes 30114589Sobrien#if 0 311558Srgrimes#ifndef lint 3238039Scharnierstatic const char copyright[] = 331558Srgrimes"@(#) Copyright (c) 1980, 1993\n\ 341558Srgrimes The Regents of the University of California. All rights reserved.\n"; 351558Srgrimes#endif /* not lint */ 361558Srgrimes 371558Srgrimes#ifndef lint 381558Srgrimesstatic char sccsid[] = "@(#)swapon.c 8.1 (Berkeley) 6/5/93"; 39114589Sobrien#endif /* not lint */ 4038039Scharnier#endif 41114589Sobrien#include <sys/cdefs.h> 42114589Sobrien__FBSDID("$FreeBSD$"); 431558Srgrimes 44108375Sdillon#include <sys/stat.h> 45108375Sdillon#include <sys/param.h> 46108375Sdillon#include <sys/sysctl.h> 47138129Sdas#include <vm/vm_param.h> 48108375Sdillon 4938039Scharnier#include <err.h> 5038039Scharnier#include <errno.h> 511558Srgrimes#include <fstab.h> 521558Srgrimes#include <stdio.h> 5378732Sdd#include <stdlib.h> 5438039Scharnier#include <string.h> 556661Sphk#include <unistd.h> 56108375Sdillon#include <fcntl.h> 57179154Spjd#include <libutil.h> 581558Srgrimes 59108375Sdillonstatic void usage(void); 60108375Sdillonstatic int swap_on_off(char *name, int ignoreebusy); 61108375Sdillonstatic void swaplist(int, int, int); 626661Sphk 63108375Sdillonenum { SWAPON, SWAPOFF, SWAPCTL } orig_prog, which_prog = SWAPCTL; 64108375Sdillon 656661Sphkint 666661Sphkmain(int argc, char **argv) 671558Srgrimes{ 6892806Sobrien struct fstab *fsp; 69108375Sdillon char *ptr; 70252871Sdelphij int ret; 711558Srgrimes int ch, doall; 72179966Smtm int sflag = 0, lflag = 0, hflag = 0, qflag = 0; 73252871Sdelphij const char *etc_fstab; 741558Srgrimes 75108375Sdillon if ((ptr = strrchr(argv[0], '/')) == NULL) 76108375Sdillon ptr = argv[0]; 77108375Sdillon if (strstr(ptr, "swapon")) 78108375Sdillon which_prog = SWAPON; 79108375Sdillon else if (strstr(ptr, "swapoff")) 80108375Sdillon which_prog = SWAPOFF; 81108375Sdillon orig_prog = which_prog; 82108375Sdillon 831558Srgrimes doall = 0; 84252871Sdelphij etc_fstab = NULL; 85252871Sdelphij while ((ch = getopt(argc, argv, "AadghklmqsUF:")) != -1) { 86108375Sdillon switch(ch) { 87108375Sdillon case 'A': 88108375Sdillon if (which_prog == SWAPCTL) { 89108375Sdillon doall = 1; 90108375Sdillon which_prog = SWAPON; 91108375Sdillon } else { 92108375Sdillon usage(); 93108375Sdillon } 94108375Sdillon break; 951558Srgrimes case 'a': 96108375Sdillon if (which_prog == SWAPON || which_prog == SWAPOFF) 97108375Sdillon doall = 1; 98108375Sdillon else 99108375Sdillon which_prog = SWAPON; 1001558Srgrimes break; 101108375Sdillon case 'd': 102108375Sdillon if (which_prog == SWAPCTL) 103108375Sdillon which_prog = SWAPOFF; 104108375Sdillon else 105108375Sdillon usage(); 106108375Sdillon break; 107179154Spjd case 'g': 108179154Spjd hflag = 'G'; 109108375Sdillon break; 110179154Spjd case 'h': 111179154Spjd hflag = 'H'; 112179154Spjd break; 113179154Spjd case 'k': 114179154Spjd hflag = 'K'; 115179154Spjd break; 116108375Sdillon case 'l': 117108375Sdillon lflag = 1; 118108375Sdillon break; 119179154Spjd case 'm': 120108375Sdillon hflag = 'M'; 121108375Sdillon break; 122179966Smtm case 'q': 123179966Smtm if (which_prog == SWAPON || which_prog == SWAPOFF) 124179966Smtm qflag = 1; 125179966Smtm break; 126179154Spjd case 's': 127179154Spjd sflag = 1; 128108375Sdillon break; 129108375Sdillon case 'U': 130108375Sdillon if (which_prog == SWAPCTL) { 131108375Sdillon doall = 1; 132108375Sdillon which_prog = SWAPOFF; 133108375Sdillon } else { 134108375Sdillon usage(); 135108375Sdillon } 136108375Sdillon break; 137252871Sdelphij case 'F': 138252871Sdelphij etc_fstab = optarg; 139252871Sdelphij break; 1401558Srgrimes case '?': 1411558Srgrimes default: 142108375Sdillon usage(); 1431558Srgrimes } 144108375Sdillon } 1451558Srgrimes argv += optind; 1461558Srgrimes 147252871Sdelphij ret = 0; 148252871Sdelphij if (etc_fstab != NULL) 149252871Sdelphij setfstab(etc_fstab); 150108375Sdillon if (which_prog == SWAPON || which_prog == SWAPOFF) { 151108375Sdillon if (doall) { 152108375Sdillon while ((fsp = getfsent()) != NULL) { 153108375Sdillon if (strcmp(fsp->fs_type, FSTAB_SW)) 154108375Sdillon continue; 155108375Sdillon if (strstr(fsp->fs_mntops, "noauto")) 156108375Sdillon continue; 157111424Sdas if (swap_on_off(fsp->fs_spec, 1)) { 158252871Sdelphij ret = 1; 159108375Sdillon } else { 160179966Smtm if (!qflag) { 161179966Smtm printf("%s: %sing %s as swap device\n", 162179966Smtm getprogname(), 163179966Smtm which_prog == SWAPOFF ? "remov" : "add", 164179966Smtm fsp->fs_spec); 165179966Smtm } 166108375Sdillon } 167108375Sdillon } 168108375Sdillon } 169108375Sdillon else if (!*argv) 170108375Sdillon usage(); 171108375Sdillon for (; *argv; ++argv) { 172108375Sdillon if (swap_on_off(*argv, 0)) { 173252871Sdelphij ret = 1; 174108375Sdillon } else if (orig_prog == SWAPCTL) { 175107913Sdillon printf("%s: %sing %s as swap device\n", 176108375Sdillon getprogname(), which_prog == SWAPOFF ? "remov" : "add", 177108375Sdillon *argv); 178108375Sdillon } 1791558Srgrimes } 180108375Sdillon } else { 181108375Sdillon if (lflag || sflag) 182108375Sdillon swaplist(lflag, sflag, hflag); 183108375Sdillon else 184108375Sdillon usage(); 185108375Sdillon } 186252871Sdelphij exit(ret); 1871558Srgrimes} 1881558Srgrimes 189108375Sdillonstatic int 190111424Sdasswap_on_off(char *name, int doingall) 1911558Srgrimes{ 192108375Sdillon if ((which_prog == SWAPOFF ? swapoff(name) : swapon(name)) == -1) { 1931558Srgrimes switch (errno) { 1941558Srgrimes case EBUSY: 195111424Sdas if (!doingall) 19626740Scharnier warnx("%s: device already in use", name); 1971558Srgrimes break; 198111424Sdas case EINVAL: 199111424Sdas if (which_prog == SWAPON) 200111424Sdas warnx("%s: NSWAPDEV limit reached", name); 201111424Sdas else if (!doingall) 202111424Sdas warn("%s", name); 203111424Sdas break; 2041558Srgrimes default: 20526740Scharnier warn("%s", name); 2061558Srgrimes break; 2071558Srgrimes } 2081558Srgrimes return(1); 2091558Srgrimes } 2101558Srgrimes return(0); 2111558Srgrimes} 2121558Srgrimes 21326740Scharnierstatic void 214108375Sdillonusage(void) 2151558Srgrimes{ 216108375Sdillon fprintf(stderr, "usage: %s ", getprogname()); 217108375Sdillon switch(orig_prog) { 218141611Sru case SWAPON: 219108375Sdillon case SWAPOFF: 220252871Sdelphij fprintf(stderr, "[-F fstab] -aq | file ...\n"); 221108375Sdillon break; 222108375Sdillon case SWAPCTL: 223179154Spjd fprintf(stderr, "[-AghklmsU] [-a file ... | -d file ...]\n"); 224108375Sdillon break; 225108375Sdillon } 2261558Srgrimes exit(1); 2271558Srgrimes} 228107913Sdillon 229108375Sdillonstatic void 230179154Spjdsizetobuf(char *buf, size_t bufsize, int hflag, long long val, int hlen, 231179154Spjd long blocksize) 232179154Spjd{ 233179154Spjd 234179154Spjd if (hflag == 'H') { 235179154Spjd char tmp[16]; 236179154Spjd 237179154Spjd humanize_number(tmp, 5, (int64_t)val, "", HN_AUTOSCALE, 238179154Spjd HN_B | HN_NOSPACE | HN_DECIMAL); 239179154Spjd snprintf(buf, bufsize, "%*s", hlen, tmp); 240179154Spjd } else { 241179154Spjd snprintf(buf, bufsize, "%*lld", hlen, val / blocksize); 242179154Spjd } 243179154Spjd} 244179154Spjd 245179154Spjdstatic void 246108375Sdillonswaplist(int lflag, int sflag, int hflag) 247107913Sdillon{ 248108375Sdillon size_t mibsize, size; 249108375Sdillon struct xswdev xsw; 250108425Smike int hlen, mib[16], n, pagesize; 251108375Sdillon long blocksize; 252108375Sdillon long long total = 0; 253108375Sdillon long long used = 0; 254108375Sdillon long long tmp_total; 255108375Sdillon long long tmp_used; 256179154Spjd char buf[32]; 257108375Sdillon 258108375Sdillon pagesize = getpagesize(); 259108375Sdillon switch(hflag) { 260179154Spjd case 'G': 261179154Spjd blocksize = 1024 * 1024 * 1024; 262179154Spjd strlcpy(buf, "1GB-blocks", sizeof(buf)); 263179154Spjd hlen = 10; 264179154Spjd break; 265179154Spjd case 'H': 266179154Spjd blocksize = -1; 267179154Spjd strlcpy(buf, "Bytes", sizeof(buf)); 268179154Spjd hlen = 10; 269179154Spjd break; 270108375Sdillon case 'K': 271108375Sdillon blocksize = 1024; 272179154Spjd strlcpy(buf, "1kB-blocks", sizeof(buf)); 273108375Sdillon hlen = 10; 274108375Sdillon break; 275108375Sdillon case 'M': 276108375Sdillon blocksize = 1024 * 1024; 277179154Spjd strlcpy(buf, "1MB-blocks", sizeof(buf)); 278108375Sdillon hlen = 10; 279108375Sdillon break; 280108375Sdillon default: 281108459Smike getbsize(&hlen, &blocksize); 282179154Spjd snprintf(buf, sizeof(buf), "%ld-blocks", blocksize); 283108375Sdillon break; 284108375Sdillon } 285108375Sdillon 286108375Sdillon mibsize = sizeof mib / sizeof mib[0]; 287108375Sdillon if (sysctlnametomib("vm.swap_info", mib, &mibsize) == -1) 288108375Sdillon err(1, "sysctlnametomib()"); 289108375Sdillon 290108375Sdillon if (lflag) { 291108375Sdillon printf("%-13s %*s %*s\n", 292108375Sdillon "Device:", 293108375Sdillon hlen, buf, 294108375Sdillon hlen, "Used:"); 295108375Sdillon } 296108375Sdillon 297108375Sdillon for (n = 0; ; ++n) { 298108375Sdillon mib[mibsize] = n; 299108375Sdillon size = sizeof xsw; 300126643Smarkm if (sysctl(mib, mibsize + 1, &xsw, &size, NULL, 0) == -1) 301108375Sdillon break; 302108375Sdillon if (xsw.xsw_version != XSWDEV_VERSION) 303108375Sdillon errx(1, "xswdev version mismatch"); 304108375Sdillon 305179154Spjd tmp_total = (long long)xsw.xsw_nblks * pagesize; 306179154Spjd tmp_used = (long long)xsw.xsw_used * pagesize; 307108375Sdillon total += tmp_total; 308108375Sdillon used += tmp_used; 309108375Sdillon if (lflag) { 310179154Spjd sizetobuf(buf, sizeof(buf), hflag, tmp_total, hlen, 311179154Spjd blocksize); 312179154Spjd printf("/dev/%-8s %s ", devname(xsw.xsw_dev, S_IFCHR), 313179154Spjd buf); 314179154Spjd sizetobuf(buf, sizeof(buf), hflag, tmp_used, hlen, 315179154Spjd blocksize); 316179154Spjd printf("%s\n", buf); 317108375Sdillon } 318108375Sdillon } 319108375Sdillon if (errno != ENOENT) 320108375Sdillon err(1, "sysctl()"); 321108375Sdillon 322108375Sdillon if (sflag) { 323179154Spjd sizetobuf(buf, sizeof(buf), hflag, total, hlen, blocksize); 324179154Spjd printf("Total: %s ", buf); 325179154Spjd sizetobuf(buf, sizeof(buf), hflag, used, hlen, blocksize); 326179154Spjd printf("%s\n", buf); 327108375Sdillon } 328108375Sdillon} 329107913Sdillon 330