1 /* 2 * Copyright (c) 1980, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #if 0 31 #ifndef lint 32 static const char copyright[] = 33 "@(#) Copyright (c) 1980, 1993\n\ 34 The Regents of the University of California. All rights reserved.\n"; 35 #endif /* not lint */ 36 37 #ifndef lint 38 static char sccsid[] = "@(#)swapon.c 8.1 (Berkeley) 6/5/93"; 39 #endif /* not lint */ 40 #endif 41 #include <sys/cdefs.h> 42 __FBSDID("$FreeBSD$"); 43 44 #include <sys/stat.h> 45 #include <sys/param.h> 46 #include <sys/user.h> 47 #include <sys/sysctl.h> 48 49 #include <err.h> 50 #include <errno.h> 51 #include <fstab.h> 52 #include <stdio.h> 53 #include <stdlib.h> 54 #include <string.h> 55 #include <unistd.h> 56 #include <fcntl.h> 57 58 static void usage(void); 59 static int swap_on_off(char *name, int ignoreebusy); 60 static void swaplist(int, int, int); 61 62 enum { SWAPON, SWAPOFF, SWAPCTL } orig_prog, which_prog = SWAPCTL; 63 64 int 65 main(int argc, char **argv) 66 { 67 struct fstab *fsp; 68 char *ptr; 69 int stat; 70 int ch, doall; 71 int sflag = 0, lflag = 0, hflag = 0; 72 73 if ((ptr = strrchr(argv[0], '/')) == NULL) 74 ptr = argv[0]; 75 if (strstr(ptr, "swapon")) 76 which_prog = SWAPON; 77 else if (strstr(ptr, "swapoff")) 78 which_prog = SWAPOFF; 79 orig_prog = which_prog; 80 81 doall = 0; 82 while ((ch = getopt(argc, argv, "AadlhksU")) != -1) { 83 switch(ch) { 84 case 'A': 85 if (which_prog == SWAPCTL) { 86 doall = 1; 87 which_prog = SWAPON; 88 } else { 89 usage(); 90 } 91 break; 92 case 'a': 93 if (which_prog == SWAPON || which_prog == SWAPOFF) 94 doall = 1; 95 else 96 which_prog = SWAPON; 97 break; 98 case 'd': 99 if (which_prog == SWAPCTL) 100 which_prog = SWAPOFF; 101 else 102 usage(); 103 break; 104 case 's': 105 sflag = 1; 106 break; 107 case 'l': 108 lflag = 1; 109 break; 110 case 'h': 111 hflag = 'M'; 112 break; 113 case 'k': 114 hflag = 'K'; 115 break; 116 case 'U': 117 if (which_prog == SWAPCTL) { 118 doall = 1; 119 which_prog = SWAPOFF; 120 } else { 121 usage(); 122 } 123 break; 124 case '?': 125 default: 126 usage(); 127 } 128 } 129 argv += optind; 130 131 stat = 0; 132 if (which_prog == SWAPON || which_prog == SWAPOFF) { 133 if (doall) { 134 while ((fsp = getfsent()) != NULL) { 135 if (strcmp(fsp->fs_type, FSTAB_SW)) 136 continue; 137 if (strstr(fsp->fs_mntops, "noauto")) 138 continue; 139 if (swap_on_off(fsp->fs_spec, 1)) { 140 stat = 1; 141 } else { 142 printf("%s: %sing %s as swap device\n", 143 getprogname(), which_prog == SWAPOFF ? "remov" : "add", 144 fsp->fs_spec); 145 } 146 } 147 } 148 else if (!*argv) 149 usage(); 150 for (; *argv; ++argv) { 151 if (swap_on_off(*argv, 0)) { 152 stat = 1; 153 } else if (orig_prog == SWAPCTL) { 154 printf("%s: %sing %s as swap device\n", 155 getprogname(), which_prog == SWAPOFF ? "remov" : "add", 156 *argv); 157 } 158 } 159 } else { 160 if (lflag || sflag) 161 swaplist(lflag, sflag, hflag); 162 else 163 usage(); 164 } 165 exit(stat); 166 } 167 168 static int 169 swap_on_off(char *name, int doingall) 170 { 171 if ((which_prog == SWAPOFF ? swapoff(name) : swapon(name)) == -1) { 172 switch (errno) { 173 case EBUSY: 174 if (!doingall) 175 warnx("%s: device already in use", name); 176 break; 177 case EINVAL: 178 if (which_prog == SWAPON) 179 warnx("%s: NSWAPDEV limit reached", name); 180 else if (!doingall) 181 warn("%s", name); 182 break; 183 default: 184 warn("%s", name); 185 break; 186 } 187 return(1); 188 } 189 return(0); 190 } 191 192 static void 193 usage(void) 194 { 195 fprintf(stderr, "usage: %s ", getprogname()); 196 switch(orig_prog) { 197 case SWAPOFF: 198 fprintf(stderr, "[-a] [special_file ...]\n"); 199 break; 200 case SWAPON: 201 fprintf(stderr, "[-a] [special_file ...]\n"); 202 break; 203 case SWAPCTL: 204 fprintf(stderr, "[-lshAU] [-a/-d special_file ...]\n"); 205 break; 206 } 207 exit(1); 208 } 209 210 static void 211 swaplist(int lflag, int sflag, int hflag) 212 { 213 size_t mibsize, size; 214 struct xswdev xsw; 215 int hlen, mib[16], n, pagesize; 216 long blocksize; 217 long long total = 0; 218 long long used = 0; 219 long long tmp_total; 220 long long tmp_used; 221 222 pagesize = getpagesize(); 223 switch(hflag) { 224 case 'K': 225 blocksize = 1024; 226 hlen = 10; 227 break; 228 case 'M': 229 blocksize = 1024 * 1024; 230 hlen = 10; 231 break; 232 default: 233 getbsize(&hlen, &blocksize); 234 break; 235 } 236 237 mibsize = sizeof mib / sizeof mib[0]; 238 if (sysctlnametomib("vm.swap_info", mib, &mibsize) == -1) 239 err(1, "sysctlnametomib()"); 240 241 if (lflag) { 242 char buf[32]; 243 snprintf(buf, sizeof(buf), "%ld-blocks", blocksize); 244 printf("%-13s %*s %*s\n", 245 "Device:", 246 hlen, buf, 247 hlen, "Used:"); 248 } 249 250 for (n = 0; ; ++n) { 251 mib[mibsize] = n; 252 size = sizeof xsw; 253 if (sysctl(mib, mibsize + 1, &xsw, &size, NULL, 0) == -1) 254 break; 255 if (xsw.xsw_version != XSWDEV_VERSION) 256 errx(1, "xswdev version mismatch"); 257 258 tmp_total = (long long)xsw.xsw_nblks * pagesize / blocksize; 259 tmp_used = (long long)xsw.xsw_used * pagesize / blocksize; 260 total += tmp_total; 261 used += tmp_used; 262 if (lflag) { 263 printf("/dev/%-8s %*lld %*lld\n", 264 devname(xsw.xsw_dev, S_IFCHR), 265 hlen, tmp_total, 266 hlen, tmp_used); 267 } 268 } 269 if (errno != ENOENT) 270 err(1, "sysctl()"); 271 272 if (sflag) { 273 printf("Total: %*lld %*lld\n", 274 hlen, total, 275 hlen, used); 276 } 277 } 278 279