swapon.c revision 114589
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 * 3. All advertising materials mentioning features or use of this software 141558Srgrimes * must display the following acknowledgement: 151558Srgrimes * This product includes software developed by the University of 161558Srgrimes * California, Berkeley and its contributors. 171558Srgrimes * 4. Neither the name of the University nor the names of its contributors 181558Srgrimes * may be used to endorse or promote products derived from this software 191558Srgrimes * without specific prior written permission. 201558Srgrimes * 211558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 221558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 231558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 241558Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 251558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 261558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 271558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 281558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 291558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 301558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311558Srgrimes * SUCH DAMAGE. 321558Srgrimes */ 331558Srgrimes 34114589Sobrien#if 0 351558Srgrimes#ifndef lint 3638039Scharnierstatic const char copyright[] = 371558Srgrimes"@(#) Copyright (c) 1980, 1993\n\ 381558Srgrimes The Regents of the University of California. All rights reserved.\n"; 391558Srgrimes#endif /* not lint */ 401558Srgrimes 411558Srgrimes#ifndef lint 421558Srgrimesstatic char sccsid[] = "@(#)swapon.c 8.1 (Berkeley) 6/5/93"; 43114589Sobrien#endif /* not lint */ 4438039Scharnier#endif 45114589Sobrien#include <sys/cdefs.h> 46114589Sobrien__FBSDID("$FreeBSD: head/sbin/swapon/swapon.c 114589 2003-05-03 18:41:59Z obrien $"); 471558Srgrimes 48108375Sdillon#include <sys/stat.h> 49108375Sdillon#include <sys/param.h> 50108375Sdillon#include <sys/user.h> 51108375Sdillon#include <sys/sysctl.h> 52108375Sdillon 5338039Scharnier#include <err.h> 5438039Scharnier#include <errno.h> 551558Srgrimes#include <fstab.h> 561558Srgrimes#include <stdio.h> 5778732Sdd#include <stdlib.h> 5838039Scharnier#include <string.h> 596661Sphk#include <unistd.h> 60108375Sdillon#include <fcntl.h> 611558Srgrimes 62108375Sdillonstatic void usage(void); 63108375Sdillonstatic int swap_on_off(char *name, int ignoreebusy); 64108375Sdillonstatic void swaplist(int, int, int); 656661Sphk 66108375Sdillonenum { SWAPON, SWAPOFF, SWAPCTL } orig_prog, which_prog = SWAPCTL; 67108375Sdillon 686661Sphkint 696661Sphkmain(int argc, char **argv) 701558Srgrimes{ 7192806Sobrien struct fstab *fsp; 72108375Sdillon char *ptr; 7392806Sobrien int stat; 741558Srgrimes int ch, doall; 75108375Sdillon int sflag = 0, lflag = 0, hflag = 0; 761558Srgrimes 77108375Sdillon if ((ptr = strrchr(argv[0], '/')) == NULL) 78108375Sdillon ptr = argv[0]; 79108375Sdillon if (strstr(ptr, "swapon")) 80108375Sdillon which_prog = SWAPON; 81108375Sdillon else if (strstr(ptr, "swapoff")) 82108375Sdillon which_prog = SWAPOFF; 83108375Sdillon orig_prog = which_prog; 84108375Sdillon 851558Srgrimes doall = 0; 86108375Sdillon while ((ch = getopt(argc, argv, "AadlhksU")) != -1) { 87108375Sdillon switch(ch) { 88108375Sdillon case 'A': 89108375Sdillon if (which_prog == SWAPCTL) { 90108375Sdillon doall = 1; 91108375Sdillon which_prog = SWAPON; 92108375Sdillon } else { 93108375Sdillon usage(); 94108375Sdillon } 95108375Sdillon break; 961558Srgrimes case 'a': 97108375Sdillon if (which_prog == SWAPON || which_prog == SWAPOFF) 98108375Sdillon doall = 1; 99108375Sdillon else 100108375Sdillon which_prog = SWAPON; 1011558Srgrimes break; 102108375Sdillon case 'd': 103108375Sdillon if (which_prog == SWAPCTL) 104108375Sdillon which_prog = SWAPOFF; 105108375Sdillon else 106108375Sdillon usage(); 107108375Sdillon break; 108108375Sdillon case 's': 109108375Sdillon sflag = 1; 110108375Sdillon break; 111108375Sdillon case 'l': 112108375Sdillon lflag = 1; 113108375Sdillon break; 114108375Sdillon case 'h': 115108375Sdillon hflag = 'M'; 116108375Sdillon break; 117108375Sdillon case 'k': 118108375Sdillon hflag = 'K'; 119108375Sdillon break; 120108375Sdillon case 'U': 121108375Sdillon if (which_prog == SWAPCTL) { 122108375Sdillon doall = 1; 123108375Sdillon which_prog = SWAPOFF; 124108375Sdillon } else { 125108375Sdillon usage(); 126108375Sdillon } 127108375Sdillon break; 1281558Srgrimes case '?': 1291558Srgrimes default: 130108375Sdillon usage(); 1311558Srgrimes } 132108375Sdillon } 1331558Srgrimes argv += optind; 1341558Srgrimes 1351558Srgrimes stat = 0; 136108375Sdillon if (which_prog == SWAPON || which_prog == SWAPOFF) { 137108375Sdillon if (doall) { 138108375Sdillon while ((fsp = getfsent()) != NULL) { 139108375Sdillon if (strcmp(fsp->fs_type, FSTAB_SW)) 140108375Sdillon continue; 141108375Sdillon if (strstr(fsp->fs_mntops, "noauto")) 142108375Sdillon continue; 143111424Sdas if (swap_on_off(fsp->fs_spec, 1)) { 144108375Sdillon stat = 1; 145108375Sdillon } else { 146108375Sdillon printf("%s: %sing %s as swap device\n", 147108375Sdillon getprogname(), which_prog == SWAPOFF ? "remov" : "add", 148108375Sdillon fsp->fs_spec); 149108375Sdillon } 150108375Sdillon } 151108375Sdillon } 152108375Sdillon else if (!*argv) 153108375Sdillon usage(); 154108375Sdillon for (; *argv; ++argv) { 155108375Sdillon if (swap_on_off(*argv, 0)) { 1561558Srgrimes stat = 1; 157108375Sdillon } else if (orig_prog == SWAPCTL) { 158107913Sdillon printf("%s: %sing %s as swap device\n", 159108375Sdillon getprogname(), which_prog == SWAPOFF ? "remov" : "add", 160108375Sdillon *argv); 161108375Sdillon } 1621558Srgrimes } 163108375Sdillon } else { 164108375Sdillon if (lflag || sflag) 165108375Sdillon swaplist(lflag, sflag, hflag); 166108375Sdillon else 167108375Sdillon usage(); 168108375Sdillon } 1691558Srgrimes exit(stat); 1701558Srgrimes} 1711558Srgrimes 172108375Sdillonstatic int 173111424Sdasswap_on_off(char *name, int doingall) 1741558Srgrimes{ 175108375Sdillon if ((which_prog == SWAPOFF ? swapoff(name) : swapon(name)) == -1) { 1761558Srgrimes switch (errno) { 1771558Srgrimes case EBUSY: 178111424Sdas if (!doingall) 17926740Scharnier warnx("%s: device already in use", name); 1801558Srgrimes break; 181111424Sdas case EINVAL: 182111424Sdas if (which_prog == SWAPON) 183111424Sdas warnx("%s: NSWAPDEV limit reached", name); 184111424Sdas else if (!doingall) 185111424Sdas warn("%s", name); 186111424Sdas break; 1871558Srgrimes default: 18826740Scharnier warn("%s", name); 1891558Srgrimes break; 1901558Srgrimes } 1911558Srgrimes return(1); 1921558Srgrimes } 1931558Srgrimes return(0); 1941558Srgrimes} 1951558Srgrimes 19626740Scharnierstatic void 197108375Sdillonusage(void) 1981558Srgrimes{ 199108375Sdillon fprintf(stderr, "usage: %s ", getprogname()); 200108375Sdillon switch(orig_prog) { 201108375Sdillon case SWAPOFF: 202108375Sdillon fprintf(stderr, "[-a] [special_file ...]\n"); 203108375Sdillon break; 204108375Sdillon case SWAPON: 205108375Sdillon fprintf(stderr, "[-a] [special_file ...]\n"); 206108375Sdillon break; 207108375Sdillon case SWAPCTL: 208108375Sdillon fprintf(stderr, "[-lshAU] [-a/-d special_file ...]\n"); 209108375Sdillon break; 210108375Sdillon } 2111558Srgrimes exit(1); 2121558Srgrimes} 213107913Sdillon 214108375Sdillonstatic void 215108375Sdillonswaplist(int lflag, int sflag, int hflag) 216107913Sdillon{ 217108375Sdillon size_t mibsize, size; 218108375Sdillon struct xswdev xsw; 219108425Smike int hlen, mib[16], n, pagesize; 220108375Sdillon long blocksize; 221108375Sdillon long long total = 0; 222108375Sdillon long long used = 0; 223108375Sdillon long long tmp_total; 224108375Sdillon long long tmp_used; 225108375Sdillon 226108375Sdillon pagesize = getpagesize(); 227108375Sdillon switch(hflag) { 228108375Sdillon case 'K': 229108375Sdillon blocksize = 1024; 230108375Sdillon hlen = 10; 231108375Sdillon break; 232108375Sdillon case 'M': 233108375Sdillon blocksize = 1024 * 1024; 234108375Sdillon hlen = 10; 235108375Sdillon break; 236108375Sdillon default: 237108459Smike getbsize(&hlen, &blocksize); 238108375Sdillon break; 239108375Sdillon } 240108375Sdillon 241108375Sdillon mibsize = sizeof mib / sizeof mib[0]; 242108375Sdillon if (sysctlnametomib("vm.swap_info", mib, &mibsize) == -1) 243108375Sdillon err(1, "sysctlnametomib()"); 244108375Sdillon 245108375Sdillon if (lflag) { 246108375Sdillon char buf[32]; 247108375Sdillon snprintf(buf, sizeof(buf), "%ld-blocks", blocksize); 248108375Sdillon printf("%-13s %*s %*s\n", 249108375Sdillon "Device:", 250108375Sdillon hlen, buf, 251108375Sdillon hlen, "Used:"); 252108375Sdillon } 253108375Sdillon 254108375Sdillon for (n = 0; ; ++n) { 255108375Sdillon mib[mibsize] = n; 256108375Sdillon size = sizeof xsw; 257108375Sdillon if (sysctl(mib, mibsize + 1, &xsw, &size, NULL, NULL) == -1) 258108375Sdillon break; 259108375Sdillon if (xsw.xsw_version != XSWDEV_VERSION) 260108375Sdillon errx(1, "xswdev version mismatch"); 261108375Sdillon 262108375Sdillon tmp_total = (long long)xsw.xsw_nblks * pagesize / blocksize; 263108375Sdillon tmp_used = (long long)xsw.xsw_used * pagesize / blocksize; 264108375Sdillon total += tmp_total; 265108375Sdillon used += tmp_used; 266108375Sdillon if (lflag) { 267108375Sdillon printf("/dev/%-8s %*lld %*lld\n", 268108375Sdillon devname(xsw.xsw_dev, S_IFCHR), 269108375Sdillon hlen, tmp_total, 270108375Sdillon hlen, tmp_used); 271108375Sdillon } 272108375Sdillon } 273108375Sdillon if (errno != ENOENT) 274108375Sdillon err(1, "sysctl()"); 275108375Sdillon 276108375Sdillon if (sflag) { 277108375Sdillon printf("Total: %*lld %*lld\n", 278108375Sdillon hlen, total, 279108375Sdillon hlen, used); 280108375Sdillon } 281108375Sdillon} 282107913Sdillon 283