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 $
36 * $DragonFly: src/sbin/swapon/swapon.c,v 1.5 2005/11/06 12:50:21 swildner Exp $
39 #include <sys/param.h>
41 #include <sys/sysctl.h>
42 #include <vm/vm_param.h>
54 static void usage(void);
55 static int swap_on_off(char *name, int doingall);
56 static void swaplist(int lflag, int sflag, int hflag);
58 enum { SWAPON, SWAPOFF, SWAPCTL } orig_prog, which_prog = SWAPCTL;
61 main(int argc, char **argv)
67 int doall, sflag, lflag, hflag, qflag;
69 if ((ptr = strrchr(argv[0], '/')) == NULL)
71 if (strstr(ptr, "swapon"))
73 else if (strstr(ptr, "swapoff"))
75 orig_prog = which_prog;
77 sflag = lflag = hflag = qflag = doall = 0;
78 while ((ch = getopt(argc, argv, "AadghklmqsU")) != -1) {
81 if (which_prog == SWAPCTL) {
89 if (which_prog == SWAPON || which_prog == SWAPOFF)
95 if (which_prog == SWAPCTL)
116 if (which_prog == SWAPON || which_prog == SWAPOFF)
123 if (which_prog == SWAPCTL) {
125 which_prog = SWAPOFF;
138 if (which_prog == SWAPON || which_prog == SWAPOFF) {
140 while ((fsp = getfsent()) != NULL) {
141 if (strcmp(fsp->fs_type, FSTAB_SW))
143 if (strstr(fsp->fs_mntops, "noauto"))
145 if (swap_on_off(fsp->fs_spec, 1)) {
149 printf("%s: %sing %s as swap device\n",
151 which_prog == SWAPOFF ? "remov" : "add",
156 } else if (*argv == NULL) {
159 for (; *argv; ++argv) {
160 if (swap_on_off(getdevpath(*argv, 0), 0)) {
162 } else if (orig_prog == SWAPCTL) {
163 printf("%s: %sing %s as swap device\n",
165 which_prog == SWAPOFF ? "remov" : "add",
171 swaplist(lflag, sflag, hflag);
179 swap_on_off(char *name, int doingall)
181 if ((which_prog == SWAPOFF ? swapoff(name) : swapon(name)) == -1) {
185 warnx("%s: device already in use", name);
188 if (which_prog == SWAPON)
189 warnx("%s: NSWAPDEV limit reached", name);
205 fprintf(stderr, "usage: %s ", getprogname());
209 fprintf(stderr, "-aq | file ...\n");
212 fprintf(stderr, "[-AghklmsU] [-a file ... | -d file ...]\n");
219 sizetobuf(char *buf, size_t bufsize, int hflag, long long val, int hlen,
225 humanize_number(tmp, 5, (int64_t)val, "", HN_AUTOSCALE,
226 HN_B | HN_NOSPACE | HN_DECIMAL);
227 snprintf(buf, bufsize, "%*s", hlen, tmp);
229 snprintf(buf, bufsize, "%*lld", hlen, val / blocksize);
234 swaplist(int lflag, int sflag, int hflag)
236 size_t ksize, bytes = 0;
242 long long total, used, tmp_total, tmp_used;
245 pagesize = getpagesize();
248 blocksize = 1024 * 1024 * 1024;
249 strlcpy(buf, "1GB-blocks", sizeof(buf));
254 strlcpy(buf, "Bytes", sizeof(buf));
259 strlcpy(buf, "1kB-blocks", sizeof(buf));
263 blocksize = 1024 * 1024;
264 strlcpy(buf, "1MB-blocks", sizeof(buf));
268 getbsize(&hlen, &blocksize);
269 snprintf(buf, sizeof(buf), "%ld-blocks", blocksize);
273 if (sysctlbyname("vm.swap_info_array", NULL, &bytes, NULL, 0) < 0)
274 err(1, "sysctlbyname()");
276 err(1, "sysctlbyname()");
278 xswbuf = malloc(bytes);
279 if (sysctlbyname("vm.swap_info_array", xswbuf, &bytes, NULL, 0) < 0) {
281 err(1, "sysctlbyname()");
285 err(1, "sysctlbyname()");
289 * Calculate size of xsw entry returned by kernel (it can be larger
290 * than the one we have if there is a version mismatch).
292 ksize = ((struct xswdev *)xswbuf)->xsw_size;
293 n = (int)(bytes / ksize);
296 printf("%-13s %*s %*s\n",
302 total = used = tmp_total = tmp_used = 0;
303 for (i = 0; i < n; ++i) {
304 xsw = (void *)((char *)xswbuf + i * ksize);
306 if (xsw->xsw_nblks == 0)
310 tmp_total = (long long)xsw->xsw_nblks * pagesize;
311 tmp_used = (long long)xsw->xsw_used * pagesize;
317 sizetobuf(buf, sizeof(buf), hflag, tmp_total, hlen,
319 if (xsw->xsw_dev == NODEV) {
320 printf("%-13s %s ", "[NFS swap]", buf);
322 printf("/dev/%-8s %s ",
323 devname(xsw->xsw_dev, S_IFCHR), buf);
326 sizetobuf(buf, sizeof(buf), hflag, tmp_used, hlen,
333 sizetobuf(buf, sizeof(buf), hflag, total, hlen, blocksize);
334 printf("Total: %s ", buf);
335 sizetobuf(buf, sizeof(buf), hflag, used, hlen, blocksize);