swapon.c revision 179966
11558Srgrimes/*
21558Srgrimes * Copyright (c) 1980, 1993
31558Srgrimes *	The Regents of the University of California.  All rights reserved.
41558Srgrimes *
51558Srgrimes * Redistribution and use in source and binary forms, with or without
61558Srgrimes * modification, are permitted provided that the following conditions
71558Srgrimes * are met:
81558Srgrimes * 1. Redistributions of source code must retain the above copyright
91558Srgrimes *    notice, this list of conditions and the following disclaimer.
101558Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111558Srgrimes *    notice, this list of conditions and the following disclaimer in the
121558Srgrimes *    documentation and/or other materials provided with the distribution.
131558Srgrimes * 4. Neither the name of the University nor the names of its contributors
141558Srgrimes *    may be used to endorse or promote products derived from this software
151558Srgrimes *    without specific prior written permission.
161558Srgrimes *
171558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201558Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271558Srgrimes * SUCH DAMAGE.
281558Srgrimes */
291558Srgrimes
30114589Sobrien#if 0
311558Srgrimes#ifndef lint
3238039Scharnierstatic const char copyright[] =
331558Srgrimes"@(#) Copyright (c) 1980, 1993\n\
341558Srgrimes	The Regents of the University of California.  All rights reserved.\n";
351558Srgrimes#endif /* not lint */
361558Srgrimes
371558Srgrimes#ifndef lint
381558Srgrimesstatic char sccsid[] = "@(#)swapon.c	8.1 (Berkeley) 6/5/93";
39114589Sobrien#endif /* not lint */
4038039Scharnier#endif
41114589Sobrien#include <sys/cdefs.h>
42114589Sobrien__FBSDID("$FreeBSD: head/sbin/swapon/swapon.c 179966 2008-06-23 22:17:08Z mtm $");
431558Srgrimes
44108375Sdillon#include <sys/stat.h>
45108375Sdillon#include <sys/param.h>
46108375Sdillon#include <sys/sysctl.h>
47138129Sdas#include <vm/vm_param.h>
48108375Sdillon
4938039Scharnier#include <err.h>
5038039Scharnier#include <errno.h>
511558Srgrimes#include <fstab.h>
521558Srgrimes#include <stdio.h>
5378732Sdd#include <stdlib.h>
5438039Scharnier#include <string.h>
556661Sphk#include <unistd.h>
56108375Sdillon#include <fcntl.h>
57179154Spjd#include <libutil.h>
581558Srgrimes
59108375Sdillonstatic void usage(void);
60108375Sdillonstatic int swap_on_off(char *name, int ignoreebusy);
61108375Sdillonstatic void swaplist(int, int, int);
626661Sphk
63108375Sdillonenum { SWAPON, SWAPOFF, SWAPCTL } orig_prog, which_prog = SWAPCTL;
64108375Sdillon
656661Sphkint
666661Sphkmain(int argc, char **argv)
671558Srgrimes{
6892806Sobrien	struct fstab *fsp;
69108375Sdillon	char *ptr;
7092806Sobrien	int stat;
711558Srgrimes	int ch, doall;
72179966Smtm	int sflag = 0, lflag = 0, hflag = 0, qflag = 0;
731558Srgrimes
74108375Sdillon	if ((ptr = strrchr(argv[0], '/')) == NULL)
75108375Sdillon		ptr = argv[0];
76108375Sdillon	if (strstr(ptr, "swapon"))
77108375Sdillon		which_prog = SWAPON;
78108375Sdillon	else if (strstr(ptr, "swapoff"))
79108375Sdillon		which_prog = SWAPOFF;
80108375Sdillon	orig_prog = which_prog;
81108375Sdillon
821558Srgrimes	doall = 0;
83179966Smtm	while ((ch = getopt(argc, argv, "AadghklmqsU")) != -1) {
84108375Sdillon		switch(ch) {
85108375Sdillon		case 'A':
86108375Sdillon			if (which_prog == SWAPCTL) {
87108375Sdillon				doall = 1;
88108375Sdillon				which_prog = SWAPON;
89108375Sdillon			} else {
90108375Sdillon				usage();
91108375Sdillon			}
92108375Sdillon			break;
931558Srgrimes		case 'a':
94108375Sdillon			if (which_prog == SWAPON || which_prog == SWAPOFF)
95108375Sdillon				doall = 1;
96108375Sdillon			else
97108375Sdillon				which_prog = SWAPON;
981558Srgrimes			break;
99108375Sdillon		case 'd':
100108375Sdillon			if (which_prog == SWAPCTL)
101108375Sdillon				which_prog = SWAPOFF;
102108375Sdillon			else
103108375Sdillon				usage();
104108375Sdillon			break;
105179154Spjd		case 'g':
106179154Spjd			hflag = 'G';
107108375Sdillon			break;
108179154Spjd		case 'h':
109179154Spjd			hflag = 'H';
110179154Spjd			break;
111179154Spjd		case 'k':
112179154Spjd			hflag = 'K';
113179154Spjd			break;
114108375Sdillon		case 'l':
115108375Sdillon			lflag = 1;
116108375Sdillon			break;
117179154Spjd		case 'm':
118108375Sdillon			hflag = 'M';
119108375Sdillon			break;
120179966Smtm		case 'q':
121179966Smtm			if (which_prog == SWAPON || which_prog == SWAPOFF)
122179966Smtm				qflag = 1;
123179966Smtm			break;
124179154Spjd		case 's':
125179154Spjd			sflag = 1;
126108375Sdillon			break;
127108375Sdillon		case 'U':
128108375Sdillon			if (which_prog == SWAPCTL) {
129108375Sdillon				doall = 1;
130108375Sdillon				which_prog = SWAPOFF;
131108375Sdillon			} else {
132108375Sdillon				usage();
133108375Sdillon			}
134108375Sdillon			break;
1351558Srgrimes		case '?':
1361558Srgrimes		default:
137108375Sdillon			usage();
1381558Srgrimes		}
139108375Sdillon	}
1401558Srgrimes	argv += optind;
1411558Srgrimes
1421558Srgrimes	stat = 0;
143108375Sdillon	if (which_prog == SWAPON || which_prog == SWAPOFF) {
144108375Sdillon		if (doall) {
145108375Sdillon			while ((fsp = getfsent()) != NULL) {
146108375Sdillon				if (strcmp(fsp->fs_type, FSTAB_SW))
147108375Sdillon					continue;
148108375Sdillon				if (strstr(fsp->fs_mntops, "noauto"))
149108375Sdillon					continue;
150111424Sdas				if (swap_on_off(fsp->fs_spec, 1)) {
151108375Sdillon					stat = 1;
152108375Sdillon				} else {
153179966Smtm					if (!qflag) {
154179966Smtm						printf("%s: %sing %s as swap device\n",
155179966Smtm						    getprogname(),
156179966Smtm						    which_prog == SWAPOFF ? "remov" : "add",
157179966Smtm						    fsp->fs_spec);
158179966Smtm					}
159108375Sdillon				}
160108375Sdillon			}
161108375Sdillon		}
162108375Sdillon		else if (!*argv)
163108375Sdillon			usage();
164108375Sdillon		for (; *argv; ++argv) {
165108375Sdillon			if (swap_on_off(*argv, 0)) {
1661558Srgrimes				stat = 1;
167108375Sdillon			} else if (orig_prog == SWAPCTL) {
168107913Sdillon				printf("%s: %sing %s as swap device\n",
169108375Sdillon				    getprogname(), which_prog == SWAPOFF ? "remov" : "add",
170108375Sdillon				    *argv);
171108375Sdillon			}
1721558Srgrimes		}
173108375Sdillon	} else {
174108375Sdillon		if (lflag || sflag)
175108375Sdillon			swaplist(lflag, sflag, hflag);
176108375Sdillon		else
177108375Sdillon			usage();
178108375Sdillon	}
1791558Srgrimes	exit(stat);
1801558Srgrimes}
1811558Srgrimes
182108375Sdillonstatic int
183111424Sdasswap_on_off(char *name, int doingall)
1841558Srgrimes{
185108375Sdillon	if ((which_prog == SWAPOFF ? swapoff(name) : swapon(name)) == -1) {
1861558Srgrimes		switch (errno) {
1871558Srgrimes		case EBUSY:
188111424Sdas			if (!doingall)
18926740Scharnier				warnx("%s: device already in use", name);
1901558Srgrimes			break;
191111424Sdas		case EINVAL:
192111424Sdas			if (which_prog == SWAPON)
193111424Sdas				warnx("%s: NSWAPDEV limit reached", name);
194111424Sdas			else if (!doingall)
195111424Sdas				warn("%s", name);
196111424Sdas			break;
1971558Srgrimes		default:
19826740Scharnier			warn("%s", name);
1991558Srgrimes			break;
2001558Srgrimes		}
2011558Srgrimes		return(1);
2021558Srgrimes	}
2031558Srgrimes	return(0);
2041558Srgrimes}
2051558Srgrimes
20626740Scharnierstatic void
207108375Sdillonusage(void)
2081558Srgrimes{
209108375Sdillon	fprintf(stderr, "usage: %s ", getprogname());
210108375Sdillon	switch(orig_prog) {
211141611Sru	case SWAPON:
212108375Sdillon	case SWAPOFF:
213179966Smtm	    fprintf(stderr, "-aq | file ...\n");
214108375Sdillon	    break;
215108375Sdillon	case SWAPCTL:
216179154Spjd	    fprintf(stderr, "[-AghklmsU] [-a file ... | -d file ...]\n");
217108375Sdillon	    break;
218108375Sdillon	}
2191558Srgrimes	exit(1);
2201558Srgrimes}
221107913Sdillon
222108375Sdillonstatic void
223179154Spjdsizetobuf(char *buf, size_t bufsize, int hflag, long long val, int hlen,
224179154Spjd    long blocksize)
225179154Spjd{
226179154Spjd
227179154Spjd	if (hflag == 'H') {
228179154Spjd		char tmp[16];
229179154Spjd
230179154Spjd		humanize_number(tmp, 5, (int64_t)val, "", HN_AUTOSCALE,
231179154Spjd		    HN_B | HN_NOSPACE | HN_DECIMAL);
232179154Spjd		snprintf(buf, bufsize, "%*s", hlen, tmp);
233179154Spjd	} else {
234179154Spjd		snprintf(buf, bufsize, "%*lld", hlen, val / blocksize);
235179154Spjd	}
236179154Spjd}
237179154Spjd
238179154Spjdstatic void
239108375Sdillonswaplist(int lflag, int sflag, int hflag)
240107913Sdillon{
241108375Sdillon	size_t mibsize, size;
242108375Sdillon	struct xswdev xsw;
243108425Smike	int hlen, mib[16], n, pagesize;
244108375Sdillon	long blocksize;
245108375Sdillon	long long total = 0;
246108375Sdillon	long long used = 0;
247108375Sdillon	long long tmp_total;
248108375Sdillon	long long tmp_used;
249179154Spjd	char buf[32];
250108375Sdillon
251108375Sdillon	pagesize = getpagesize();
252108375Sdillon	switch(hflag) {
253179154Spjd	case 'G':
254179154Spjd	    blocksize = 1024 * 1024 * 1024;
255179154Spjd	    strlcpy(buf, "1GB-blocks", sizeof(buf));
256179154Spjd	    hlen = 10;
257179154Spjd	    break;
258179154Spjd	case 'H':
259179154Spjd	    blocksize = -1;
260179154Spjd	    strlcpy(buf, "Bytes", sizeof(buf));
261179154Spjd	    hlen = 10;
262179154Spjd	    break;
263108375Sdillon	case 'K':
264108375Sdillon	    blocksize = 1024;
265179154Spjd	    strlcpy(buf, "1kB-blocks", sizeof(buf));
266108375Sdillon	    hlen = 10;
267108375Sdillon	    break;
268108375Sdillon	case 'M':
269108375Sdillon	    blocksize = 1024 * 1024;
270179154Spjd	    strlcpy(buf, "1MB-blocks", sizeof(buf));
271108375Sdillon	    hlen = 10;
272108375Sdillon	    break;
273108375Sdillon	default:
274108459Smike	    getbsize(&hlen, &blocksize);
275179154Spjd	    snprintf(buf, sizeof(buf), "%ld-blocks", blocksize);
276108375Sdillon	    break;
277108375Sdillon	}
278108375Sdillon
279108375Sdillon	mibsize = sizeof mib / sizeof mib[0];
280108375Sdillon	if (sysctlnametomib("vm.swap_info", mib, &mibsize) == -1)
281108375Sdillon		err(1, "sysctlnametomib()");
282108375Sdillon
283108375Sdillon	if (lflag) {
284108375Sdillon		printf("%-13s %*s %*s\n",
285108375Sdillon		    "Device:",
286108375Sdillon		    hlen, buf,
287108375Sdillon		    hlen, "Used:");
288108375Sdillon	}
289108375Sdillon
290108375Sdillon	for (n = 0; ; ++n) {
291108375Sdillon		mib[mibsize] = n;
292108375Sdillon		size = sizeof xsw;
293126643Smarkm		if (sysctl(mib, mibsize + 1, &xsw, &size, NULL, 0) == -1)
294108375Sdillon			break;
295108375Sdillon		if (xsw.xsw_version != XSWDEV_VERSION)
296108375Sdillon			errx(1, "xswdev version mismatch");
297108375Sdillon
298179154Spjd		tmp_total = (long long)xsw.xsw_nblks * pagesize;
299179154Spjd		tmp_used  = (long long)xsw.xsw_used * pagesize;
300108375Sdillon		total += tmp_total;
301108375Sdillon		used  += tmp_used;
302108375Sdillon		if (lflag) {
303179154Spjd			sizetobuf(buf, sizeof(buf), hflag, tmp_total, hlen,
304179154Spjd			    blocksize);
305179154Spjd			printf("/dev/%-8s %s ", devname(xsw.xsw_dev, S_IFCHR),
306179154Spjd			    buf);
307179154Spjd			sizetobuf(buf, sizeof(buf), hflag, tmp_used, hlen,
308179154Spjd			    blocksize);
309179154Spjd			printf("%s\n", buf);
310108375Sdillon		}
311108375Sdillon	}
312108375Sdillon	if (errno != ENOENT)
313108375Sdillon		err(1, "sysctl()");
314108375Sdillon
315108375Sdillon	if (sflag) {
316179154Spjd		sizetobuf(buf, sizeof(buf), hflag, total, hlen, blocksize);
317179154Spjd		printf("Total:        %s ", buf);
318179154Spjd		sizetobuf(buf, sizeof(buf), hflag, used, hlen, blocksize);
319179154Spjd		printf("%s\n", buf);
320108375Sdillon	}
321108375Sdillon}
322107913Sdillon
323