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 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#) Copyright (c) 1980, 1993 The Regents of the University of California. All rights reserved.
34 * @(#)swapon.c 8.1 (Berkeley) 6/5/93
35 * $FreeBSD: src/sbin/swapon/swapon.c,v 1.8.2.2 2001/07/30 10:30:11 dd Exp $
38 #include <sys/param.h>
40 #include <sys/sysctl.h>
41 #include <vm/vm_param.h>
53 static void usage(void);
54 static int swap_on_off(char *name, int doingall);
55 static void swaplist(int lflag, int sflag, int hflag);
57 enum { SWAPON, SWAPOFF, SWAPCTL } orig_prog, which_prog = SWAPCTL;
60 main(int argc, char **argv)
66 int doall, sflag, lflag, hflag, qflag;
68 if ((ptr = strrchr(argv[0], '/')) == NULL)
70 if (strstr(ptr, "swapon"))
72 else if (strstr(ptr, "swapoff"))
74 orig_prog = which_prog;
76 sflag = lflag = hflag = qflag = doall = 0;
77 while ((ch = getopt(argc, argv, "AadghklmqsU")) != -1) {
80 if (which_prog == SWAPCTL) {
88 if (which_prog == SWAPON || which_prog == SWAPOFF)
94 if (which_prog == SWAPCTL)
115 if (which_prog == SWAPON || which_prog == SWAPOFF)
122 if (which_prog == SWAPCTL) {
124 which_prog = SWAPOFF;
137 if (which_prog == SWAPON || which_prog == SWAPOFF) {
139 while ((fsp = getfsent()) != NULL) {
140 if (strcmp(fsp->fs_type, FSTAB_SW))
142 if (strstr(fsp->fs_mntops, "noauto"))
144 if (swap_on_off(fsp->fs_spec, 1)) {
148 printf("%s: %sing %s as swap device\n",
150 which_prog == SWAPOFF ? "remov" : "add",
155 } else if (*argv == NULL) {
158 for (; *argv; ++argv) {
159 if (swap_on_off(getdevpath(*argv, 0), 0)) {
161 } else if (orig_prog == SWAPCTL) {
162 printf("%s: %sing %s as swap device\n",
164 which_prog == SWAPOFF ? "remov" : "add",
170 swaplist(lflag, sflag, hflag);
178 swap_on_off(char *name, int doingall)
180 if ((which_prog == SWAPOFF ? swapoff(name) : swapon(name)) == -1) {
184 warnx("%s: device already in use", name);
187 if (which_prog == SWAPON)
188 warnx("%s: NSWAPDEV limit reached", name);
204 fprintf(stderr, "usage: %s ", getprogname());
208 fprintf(stderr, "-aq | file ...\n");
211 fprintf(stderr, "[-AghklmsU] [-a file ... | -d file ...]\n");
218 sizetobuf(char *buf, size_t bufsize, int hflag, long long val, int hlen,
224 humanize_number(tmp, 5, (int64_t)val, "", HN_AUTOSCALE,
225 HN_B | HN_NOSPACE | HN_DECIMAL);
226 snprintf(buf, bufsize, "%*s", hlen, tmp);
228 snprintf(buf, bufsize, "%*lld", hlen, val / blocksize);
233 swaplist(int lflag, int sflag, int hflag)
235 size_t ksize, bytes = 0;
241 long long total, used, tmp_total, tmp_used;
244 pagesize = getpagesize();
247 blocksize = 1024 * 1024 * 1024;
248 strlcpy(buf, "1GB-blocks", sizeof(buf));
253 strlcpy(buf, "Bytes", sizeof(buf));
258 strlcpy(buf, "1kB-blocks", sizeof(buf));
262 blocksize = 1024 * 1024;
263 strlcpy(buf, "1MB-blocks", sizeof(buf));
267 getbsize(&hlen, &blocksize);
268 snprintf(buf, sizeof(buf), "%ld-blocks", blocksize);
272 if (sysctlbyname("vm.swap_info_array", NULL, &bytes, NULL, 0) < 0)
273 err(1, "sysctlbyname()");
275 err(1, "sysctlbyname()");
277 xswbuf = malloc(bytes);
278 if (sysctlbyname("vm.swap_info_array", xswbuf, &bytes, NULL, 0) < 0) {
280 err(1, "sysctlbyname()");
284 err(1, "sysctlbyname()");
288 * Calculate size of xsw entry returned by kernel (it can be larger
289 * than the one we have if there is a version mismatch).
291 ksize = ((struct xswdev *)xswbuf)->xsw_size;
292 n = (int)(bytes / ksize);
295 printf("%-13s %*s %*s\n",
301 total = used = tmp_total = tmp_used = 0;
302 for (i = 0; i < n; ++i) {
303 xsw = (void *)((char *)xswbuf + i * ksize);
305 if (xsw->xsw_nblks == 0)
308 tmp_total = (long long)xsw->xsw_nblks * pagesize;
309 tmp_used = (long long)xsw->xsw_used * pagesize;
313 sizetobuf(buf, sizeof(buf), hflag, tmp_total, hlen,
315 if (xsw->xsw_dev == NODEV) {
316 printf("%-13s %s ", "[NFS swap]", buf);
318 printf("/dev/%-8s %s ",
319 devname(xsw->xsw_dev, S_IFCHR), buf);
322 sizetobuf(buf, sizeof(buf), hflag, tmp_used, hlen,
329 sizetobuf(buf, sizeof(buf), hflag, total, hlen, blocksize);
330 printf("Total: %s ", buf);
331 sizetobuf(buf, sizeof(buf), hflag, used, hlen, blocksize);