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;
73 const char *etc_fstab;
75 if ((ptr = strrchr(argv[0], '/')) == NULL)
77 if (strstr(ptr, "swapon"))
79 else if (strstr(ptr, "swapoff"))
81 orig_prog = which_prog;
85 while ((ch = getopt(argc, argv, "AadghklmqsUF:")) != -1) {
88 if (which_prog == SWAPCTL) {
96 if (which_prog == SWAPON || which_prog == SWAPOFF)
102 if (which_prog == SWAPCTL)
103 which_prog = SWAPOFF;
123 if (which_prog == SWAPON || which_prog == SWAPOFF)
130 if (which_prog == SWAPCTL) {
132 which_prog = SWAPOFF;
148 if (etc_fstab != NULL)
150 if (which_prog == SWAPON || which_prog == SWAPOFF) {
152 while ((fsp = getfsent()) != NULL) {
153 if (strcmp(fsp->fs_type, FSTAB_SW))
155 if (strstr(fsp->fs_mntops, "noauto"))
157 if (swap_on_off(fsp->fs_spec, 1)) {
161 printf("%s: %sing %s as swap device\n",
163 which_prog == SWAPOFF ? "remov" : "add",
171 for (; *argv; ++argv) {
172 if (swap_on_off(*argv, 0)) {
174 } else if (orig_prog == SWAPCTL) {
175 printf("%s: %sing %s as swap device\n",
176 getprogname(), which_prog == SWAPOFF ? "remov" : "add",
182 swaplist(lflag, sflag, hflag);
190 swap_on_off(char *name, int doingall)
192 if ((which_prog == SWAPOFF ? swapoff(name) : swapon(name)) == -1) {
196 warnx("%s: device already in use", name);
199 if (which_prog == SWAPON)
200 warnx("%s: NSWAPDEV limit reached", name);
216 fprintf(stderr, "usage: %s ", getprogname());
220 fprintf(stderr, "[-F fstab] -aq | file ...\n");
223 fprintf(stderr, "[-AghklmsU] [-a file ... | -d file ...]\n");
230 sizetobuf(char *buf, size_t bufsize, int hflag, long long val, int hlen,
237 humanize_number(tmp, 5, (int64_t)val, "", HN_AUTOSCALE,
238 HN_B | HN_NOSPACE | HN_DECIMAL);
239 snprintf(buf, bufsize, "%*s", hlen, tmp);
241 snprintf(buf, bufsize, "%*lld", hlen, val / blocksize);
246 swaplist(int lflag, int sflag, int hflag)
248 size_t mibsize, size;
250 int hlen, mib[16], n, pagesize;
258 pagesize = getpagesize();
261 blocksize = 1024 * 1024 * 1024;
262 strlcpy(buf, "1GB-blocks", sizeof(buf));
267 strlcpy(buf, "Bytes", sizeof(buf));
272 strlcpy(buf, "1kB-blocks", sizeof(buf));
276 blocksize = 1024 * 1024;
277 strlcpy(buf, "1MB-blocks", sizeof(buf));
281 getbsize(&hlen, &blocksize);
282 snprintf(buf, sizeof(buf), "%ld-blocks", blocksize);
286 mibsize = sizeof mib / sizeof mib[0];
287 if (sysctlnametomib("vm.swap_info", mib, &mibsize) == -1)
288 err(1, "sysctlnametomib()");
291 printf("%-13s %*s %*s\n",
300 if (sysctl(mib, mibsize + 1, &xsw, &size, NULL, 0) == -1)
302 if (xsw.xsw_version != XSWDEV_VERSION)
303 errx(1, "xswdev version mismatch");
305 tmp_total = (long long)xsw.xsw_nblks * pagesize;
306 tmp_used = (long long)xsw.xsw_used * pagesize;
310 sizetobuf(buf, sizeof(buf), hflag, tmp_total, hlen,
312 printf("/dev/%-8s %s ", devname(xsw.xsw_dev, S_IFCHR),
314 sizetobuf(buf, sizeof(buf), hflag, tmp_used, hlen,
323 sizetobuf(buf, sizeof(buf), hflag, total, hlen, blocksize);
324 printf("Total: %s ", buf);
325 sizetobuf(buf, sizeof(buf), hflag, used, hlen, blocksize);