2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
32 static const char copyright[] =
33 "@(#) Copyright (c) 1980, 1993\n\
34 The Regents of the University of California. All rights reserved.\n";
38 static char sccsid[] = "@(#)swapon.c 8.1 (Berkeley) 6/5/93";
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
45 #include <sys/param.h>
46 #include <sys/sysctl.h>
47 #include <vm/vm_param.h>
58 static void usage(void);
59 static int swap_on_off(char *name, int ignoreebusy);
60 static void swaplist(int, int, int);
62 enum { SWAPON, SWAPOFF, SWAPCTL } orig_prog, which_prog = SWAPCTL;
65 main(int argc, char **argv)
71 int sflag = 0, lflag = 0, hflag = 0;
73 if ((ptr = strrchr(argv[0], '/')) == NULL)
75 if (strstr(ptr, "swapon"))
77 else if (strstr(ptr, "swapoff"))
79 orig_prog = which_prog;
82 while ((ch = getopt(argc, argv, "AadlhksU")) != -1) {
85 if (which_prog == SWAPCTL) {
93 if (which_prog == SWAPON || which_prog == SWAPOFF)
99 if (which_prog == SWAPCTL)
100 which_prog = SWAPOFF;
117 if (which_prog == SWAPCTL) {
119 which_prog = SWAPOFF;
132 if (which_prog == SWAPON || which_prog == SWAPOFF) {
134 while ((fsp = getfsent()) != NULL) {
135 if (strcmp(fsp->fs_type, FSTAB_SW))
137 if (strstr(fsp->fs_mntops, "noauto"))
139 if (swap_on_off(fsp->fs_spec, 1)) {
142 printf("%s: %sing %s as swap device\n",
143 getprogname(), which_prog == SWAPOFF ? "remov" : "add",
150 for (; *argv; ++argv) {
151 if (swap_on_off(*argv, 0)) {
153 } else if (orig_prog == SWAPCTL) {
154 printf("%s: %sing %s as swap device\n",
155 getprogname(), which_prog == SWAPOFF ? "remov" : "add",
161 swaplist(lflag, sflag, hflag);
169 swap_on_off(char *name, int doingall)
171 if ((which_prog == SWAPOFF ? swapoff(name) : swapon(name)) == -1) {
175 warnx("%s: device already in use", name);
178 if (which_prog == SWAPON)
179 warnx("%s: NSWAPDEV limit reached", name);
195 fprintf(stderr, "usage: %s ", getprogname());
199 fprintf(stderr, "-a | file ...\n");
202 fprintf(stderr, "[-AhklsU] [-a file ... | -d file ...]\n");
209 swaplist(int lflag, int sflag, int hflag)
211 size_t mibsize, size;
213 int hlen, mib[16], n, pagesize;
220 pagesize = getpagesize();
227 blocksize = 1024 * 1024;
231 getbsize(&hlen, &blocksize);
235 mibsize = sizeof mib / sizeof mib[0];
236 if (sysctlnametomib("vm.swap_info", mib, &mibsize) == -1)
237 err(1, "sysctlnametomib()");
241 snprintf(buf, sizeof(buf), "%ld-blocks", blocksize);
242 printf("%-13s %*s %*s\n",
251 if (sysctl(mib, mibsize + 1, &xsw, &size, NULL, 0) == -1)
253 if (xsw.xsw_version != XSWDEV_VERSION)
254 errx(1, "xswdev version mismatch");
256 tmp_total = (long long)xsw.xsw_nblks * pagesize / blocksize;
257 tmp_used = (long long)xsw.xsw_used * pagesize / blocksize;
261 printf("/dev/%-8s %*lld %*lld\n",
262 devname(xsw.xsw_dev, S_IFCHR),
271 printf("Total: %*lld %*lld\n",