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>
59 static void usage(void);
60 static int swap_on_off(char *name, int ignoreebusy);
61 static void swaplist(int, int, int);
63 enum { SWAPON, SWAPOFF, SWAPCTL } orig_prog, which_prog = SWAPCTL;
66 main(int argc, char **argv)
72 int sflag = 0, lflag = 0, hflag = 0, qflag = 0;
74 if ((ptr = strrchr(argv[0], '/')) == NULL)
76 if (strstr(ptr, "swapon"))
78 else if (strstr(ptr, "swapoff"))
80 orig_prog = which_prog;
83 while ((ch = getopt(argc, argv, "AadghklmqsU")) != -1) {
86 if (which_prog == SWAPCTL) {
94 if (which_prog == SWAPON || which_prog == SWAPOFF)
100 if (which_prog == SWAPCTL)
101 which_prog = SWAPOFF;
121 if (which_prog == SWAPON || which_prog == SWAPOFF)
128 if (which_prog == SWAPCTL) {
130 which_prog = SWAPOFF;
143 if (which_prog == SWAPON || which_prog == SWAPOFF) {
145 while ((fsp = getfsent()) != NULL) {
146 if (strcmp(fsp->fs_type, FSTAB_SW))
148 if (strstr(fsp->fs_mntops, "noauto"))
150 if (swap_on_off(fsp->fs_spec, 1)) {
154 printf("%s: %sing %s as swap device\n",
156 which_prog == SWAPOFF ? "remov" : "add",
164 for (; *argv; ++argv) {
165 if (swap_on_off(*argv, 0)) {
167 } else if (orig_prog == SWAPCTL) {
168 printf("%s: %sing %s as swap device\n",
169 getprogname(), which_prog == SWAPOFF ? "remov" : "add",
175 swaplist(lflag, sflag, hflag);
183 swap_on_off(char *name, int doingall)
185 if ((which_prog == SWAPOFF ? swapoff(name) : swapon(name)) == -1) {
189 warnx("%s: device already in use", name);
192 if (which_prog == SWAPON)
193 warnx("%s: NSWAPDEV limit reached", name);
209 fprintf(stderr, "usage: %s ", getprogname());
213 fprintf(stderr, "-aq | file ...\n");
216 fprintf(stderr, "[-AghklmsU] [-a file ... | -d file ...]\n");
223 sizetobuf(char *buf, size_t bufsize, int hflag, long long val, int hlen,
230 humanize_number(tmp, 5, (int64_t)val, "", HN_AUTOSCALE,
231 HN_B | HN_NOSPACE | HN_DECIMAL);
232 snprintf(buf, bufsize, "%*s", hlen, tmp);
234 snprintf(buf, bufsize, "%*lld", hlen, val / blocksize);
239 swaplist(int lflag, int sflag, int hflag)
241 size_t mibsize, size;
243 int hlen, mib[16], n, pagesize;
251 pagesize = getpagesize();
254 blocksize = 1024 * 1024 * 1024;
255 strlcpy(buf, "1GB-blocks", sizeof(buf));
260 strlcpy(buf, "Bytes", sizeof(buf));
265 strlcpy(buf, "1kB-blocks", sizeof(buf));
269 blocksize = 1024 * 1024;
270 strlcpy(buf, "1MB-blocks", sizeof(buf));
274 getbsize(&hlen, &blocksize);
275 snprintf(buf, sizeof(buf), "%ld-blocks", blocksize);
279 mibsize = sizeof mib / sizeof mib[0];
280 if (sysctlnametomib("vm.swap_info", mib, &mibsize) == -1)
281 err(1, "sysctlnametomib()");
284 printf("%-13s %*s %*s\n",
293 if (sysctl(mib, mibsize + 1, &xsw, &size, NULL, 0) == -1)
295 if (xsw.xsw_version != XSWDEV_VERSION)
296 errx(1, "xswdev version mismatch");
298 tmp_total = (long long)xsw.xsw_nblks * pagesize;
299 tmp_used = (long long)xsw.xsw_used * pagesize;
303 sizetobuf(buf, sizeof(buf), hflag, tmp_total, hlen,
305 printf("/dev/%-8s %s ", devname(xsw.xsw_dev, S_IFCHR),
307 sizetobuf(buf, sizeof(buf), hflag, tmp_used, hlen,
316 sizetobuf(buf, sizeof(buf), hflag, total, hlen, blocksize);
317 printf("Total: %s ", buf);
318 sizetobuf(buf, sizeof(buf), hflag, used, hlen, blocksize);