swapon.c revision 253834
1/*
2 * Copyright (c) 1980, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
16 *
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
27 * SUCH DAMAGE.
28 */
29
30#if 0
31#ifndef lint
32static const char copyright[] =
33"@(#) Copyright (c) 1980, 1993\n\
34	The Regents of the University of California.  All rights reserved.\n";
35#endif /* not lint */
36
37#ifndef lint
38static char sccsid[] = "@(#)swapon.c	8.1 (Berkeley) 6/5/93";
39#endif /* not lint */
40#endif
41#include <sys/cdefs.h>
42__FBSDID("$FreeBSD: head/sbin/swapon/swapon.c 253834 2013-07-31 07:09:35Z delphij $");
43
44#include <sys/param.h>
45#include <sys/types.h>
46#include <sys/mdioctl.h>
47#include <sys/stat.h>
48#include <sys/sysctl.h>
49#include <sys/wait.h>
50#include <vm/vm_param.h>
51
52#include <err.h>
53#include <errno.h>
54#include <fcntl.h>
55#include <fnmatch.h>
56#include <fstab.h>
57#include <libgen.h>
58#include <libutil.h>
59#include <limits.h>
60#include <paths.h>
61#include <stdarg.h>
62#include <stdio.h>
63#include <stdlib.h>
64#include <string.h>
65#include <unistd.h>
66
67static void usage(void);
68static const char *swap_on_off(const char *, int, char *);
69static const char *swap_on_off_gbde(const char *, int);
70static const char *swap_on_off_geli(const char *, char *, int);
71static const char *swap_on_off_md(const char *, char *, int);
72static const char *swap_on_off_sfile(const char *, int);
73static void swaplist(int, int, int);
74static int run_cmd(int *, const char *, ...) __printflike(2, 3);
75
76static enum { SWAPON, SWAPOFF, SWAPCTL } orig_prog, which_prog = SWAPCTL;
77
78static int qflag;
79
80int
81main(int argc, char **argv)
82{
83	struct fstab *fsp;
84	const char *swfile;
85	char *ptr;
86	int ret;
87	int ch, doall;
88	int sflag = 0, lflag = 0, late = 0, hflag = 0;
89	const char *etc_fstab;
90
91	if ((ptr = strrchr(argv[0], '/')) == NULL)
92		ptr = argv[0];
93	if (strstr(ptr, "swapon"))
94		which_prog = SWAPON;
95	else if (strstr(ptr, "swapoff"))
96		which_prog = SWAPOFF;
97	orig_prog = which_prog;
98
99	doall = 0;
100	etc_fstab = NULL;
101	while ((ch = getopt(argc, argv, "AadghklLmqsUF:")) != -1) {
102		switch(ch) {
103		case 'A':
104			if (which_prog == SWAPCTL) {
105				doall = 1;
106				which_prog = SWAPON;
107			} else {
108				usage();
109			}
110			break;
111		case 'a':
112			if (which_prog == SWAPON || which_prog == SWAPOFF)
113				doall = 1;
114			else
115				which_prog = SWAPON;
116			break;
117		case 'd':
118			if (which_prog == SWAPCTL)
119				which_prog = SWAPOFF;
120			else
121				usage();
122			break;
123		case 'g':
124			hflag = 'G';
125			break;
126		case 'h':
127			hflag = 'H';
128			break;
129		case 'k':
130			hflag = 'K';
131			break;
132		case 'l':
133			lflag = 1;
134			break;
135		case 'L':
136			late = 1;
137			break;
138		case 'm':
139			hflag = 'M';
140			break;
141		case 'q':
142			if (which_prog == SWAPON || which_prog == SWAPOFF)
143				qflag = 1;
144			break;
145		case 's':
146			sflag = 1;
147			break;
148		case 'U':
149			if (which_prog == SWAPCTL) {
150				doall = 1;
151				which_prog = SWAPOFF;
152			} else {
153				usage();
154			}
155			break;
156		case 'F':
157			etc_fstab = optarg;
158			break;
159		case '?':
160		default:
161			usage();
162		}
163	}
164	argv += optind;
165
166	ret = 0;
167	swfile = NULL;
168	if (etc_fstab != NULL)
169		setfstab(etc_fstab);
170	if (which_prog == SWAPON || which_prog == SWAPOFF) {
171		if (doall) {
172			while ((fsp = getfsent()) != NULL) {
173				if (strcmp(fsp->fs_type, FSTAB_SW))
174					continue;
175				if (strstr(fsp->fs_mntops, "noauto"))
176					continue;
177				if (which_prog != SWAPOFF &&
178				    strstr(fsp->fs_mntops, "late") &&
179				    !late)
180					continue;
181				swfile = swap_on_off(fsp->fs_spec, 1,
182				    fsp->fs_mntops);
183				if (swfile == NULL) {
184					ret = 1;
185					continue;
186				}
187				if (!qflag) {
188					printf("%s: %sing %s as swap device\n",
189					    getprogname(),
190					    (which_prog == SWAPOFF) ?
191					    "remov" : "add", swfile);
192				}
193			}
194		}
195		else if (!*argv)
196			usage();
197		for (; *argv; ++argv) {
198			swfile = swap_on_off(*argv, 0, NULL);
199			if (swfile == NULL) {
200				ret = 1;
201				continue;
202			}
203			if (orig_prog == SWAPCTL) {
204				printf("%s: %sing %s as swap device\n",
205				    getprogname(),
206				    (which_prog == SWAPOFF) ? "remov" : "add",
207				    swfile);
208			}
209		}
210	} else {
211		if (lflag || sflag)
212			swaplist(lflag, sflag, hflag);
213		else
214			usage();
215	}
216	exit(ret);
217}
218
219static const char *
220swap_on_off(const char *name, int doingall, char *mntops)
221{
222	char base[PATH_MAX];
223
224	/* Swap on vnode-backed md(4) device. */
225	if (mntops != NULL &&
226	    (fnmatch(_PATH_DEV MD_NAME "[0-9]*", name, 0) == 0 ||
227	     fnmatch(MD_NAME "[0-9]*", name, 0) == 0 ||
228	     strncmp(_PATH_DEV MD_NAME, name,
229		sizeof(_PATH_DEV) + sizeof(MD_NAME)) == 0 ||
230	     strncmp(MD_NAME, name, sizeof(MD_NAME)) == 0))
231		return (swap_on_off_md(name, mntops, doingall));
232
233	basename_r(name, base);
234
235	/* Swap on encrypted device by GEOM_BDE. */
236	if (fnmatch("*.bde", base, 0) == 0)
237		return (swap_on_off_gbde(name, doingall));
238
239	/* Swap on encrypted device by GEOM_ELI. */
240	if (fnmatch("*.eli", base, 0) == 0)
241		return (swap_on_off_geli(name, mntops, doingall));
242
243	/* Swap on special file. */
244	return (swap_on_off_sfile(name, doingall));
245}
246
247/* Strip off .bde or .eli suffix from swap device name */
248static char *
249swap_basename(const char *name)
250{
251	char *dname, *p;
252
253	dname = strdup(name);
254	p = strrchr(dname, '.');
255	/* assert(p != NULL); */
256	*p = '\0';
257
258	return (dname);
259}
260
261static const char *
262swap_on_off_gbde(const char *name, int doingall)
263{
264	const char *ret;
265	char pass[64 * 2 + 1], bpass[64];
266	char *dname;
267	int i, error;
268
269	dname = swap_basename(name);
270	if (dname == NULL)
271		return (NULL);
272
273	if (which_prog == SWAPON) {
274		arc4random_buf(bpass, sizeof(bpass));
275		for (i = 0; i < (int)sizeof(bpass); i++)
276			sprintf(&pass[2 * i], "%02x", bpass[i]);
277		pass[sizeof(pass) - 1] = '\0';
278
279		error = run_cmd(NULL, "%s init %s -P %s", _PATH_GBDE,
280		    dname, pass);
281		if (error) {
282			/* bde device found.  Ignore it. */
283			free(dname);
284			if (!qflag)
285				warnx("%s: Device already in use", name);
286			return (NULL);
287		}
288		error = run_cmd(NULL, "%s attach %s -p %s", _PATH_GBDE,
289		    dname, pass);
290		free(dname);
291		if (error) {
292			warnx("gbde (attach) error: %s", name);
293			return (NULL);
294		}
295	}
296
297	ret = swap_on_off_sfile(name, doingall);
298
299	if (which_prog == SWAPOFF) {
300		error = run_cmd(NULL, "%s detach %s", _PATH_GBDE, dname);
301		free(dname);
302		if (error) {
303			/* bde device not found.  Ignore it. */
304			if (!qflag)
305				warnx("%s: Device not found", name);
306			return (NULL);
307		}
308	}
309
310	return (ret);
311}
312
313/* Build geli(8) arguments from mntops */
314static char *
315swap_on_geli_args(const char *mntops)
316{
317	const char *aalgo, *ealgo, *keylen_str, *sectorsize_str;
318	const char *aflag, *eflag, *lflag, *sflag;
319	char *p;
320	char *args;
321	char *token, *string, *ops;
322	int argsize, pagesize;
323	size_t pagesize_len;
324	u_long ul;
325
326	/* Use built-in defaults for geli(8) */
327	aalgo = ealgo = keylen_str = "";
328	aflag = eflag = lflag = "";
329
330	/* We will always specify sectorsize */
331	sflag = " -s ";
332	sectorsize_str = NULL;
333
334	if (mntops != NULL) {
335		string = ops = strdup(mntops);
336
337		while ((token = strsep(&string, ",")) != NULL) {
338			if ((p = strstr(token, "aalgo=")) == token) {
339				aalgo = p + sizeof("aalgo=") - 1;
340				aflag = " -a ";
341			} else if ((p = strstr(token, "ealgo=")) == token) {
342				ealgo = p + sizeof("ealgo=") - 1;
343				eflag = " -e ";
344			} else if ((p = strstr(token, "keylen=")) == token) {
345				keylen_str = p + sizeof("keylen=") - 1;
346				errno = 0;
347				ul = strtoul(keylen_str, &p, 10);
348				if (errno == 0) {
349					if (*p != '\0' || ul > INT_MAX)
350						errno = EINVAL;
351				}
352				if (errno) {
353					warn("Invalid keylen: %s", keylen_str);
354					free(ops);
355					return (NULL);
356				}
357				lflag = " -l ";
358			} else if ((p = strstr(token, "sectorsize=")) == token) {
359				sectorsize_str = p + sizeof("sectorsize=") - 1;
360				errno = 0;
361				ul = strtoul(sectorsize_str, &p, 10);
362				if (errno == 0) {
363					if (*p != '\0' || ul > INT_MAX)
364						errno = EINVAL;
365				}
366				if (errno) {
367					warn("Invalid sectorsize: %s", sectorsize_str);
368					free(ops);
369					return (NULL);
370				}
371			} else if (strcmp(token, "sw") != 0) {
372				warnx("Invalid option: %s", token);
373				free(ops);
374				return (NULL);
375			}
376		}
377	} else
378		ops = NULL;
379
380	/*
381	 * If we do not have a sector size at this point, fill in
382	 * pagesize as sector size.
383	 */
384	if (sectorsize_str == NULL) {
385		/* Use pagesize as default sectorsize */
386		pagesize = getpagesize();
387		pagesize_len = snprintf(NULL, 0, "%d", pagesize) + 1;
388		p = alloca(pagesize_len);
389		snprintf(p, pagesize_len, "%d", pagesize);
390		sectorsize_str = p;
391	}
392
393	argsize = asprintf(&args, "%s%s%s%s%s%s%s%s -d",
394	    aflag, aalgo, eflag, ealgo, lflag, keylen_str,
395	    sflag, sectorsize_str);
396
397	free(ops);
398	return (args);
399}
400
401static const char *
402swap_on_off_geli(const char *name, char *mntops, int doingall)
403{
404	char *dname;
405	char *args;
406	struct stat sb;
407	int error;
408
409	error = stat(name, &sb);
410
411	if (which_prog == SWAPON) do {
412		/* Skip if the .eli device already exists */
413		if (error == 0)
414			break;
415
416		args = swap_on_geli_args(mntops);
417		if (args == NULL)
418			return (NULL);
419
420		dname = swap_basename(name);
421		if (dname == NULL) {
422			free(args);
423			return (NULL);
424		}
425
426		error = run_cmd(NULL, "%s onetime%s %s", _PATH_GELI, args,
427		    dname);
428
429		free(dname);
430		free(args);
431
432		if (error) {
433			/* error occured during creation */
434			if (!qflag)
435				warnx("%s: Invalid parameters", name);
436			return (NULL);
437		}
438	} while (0);
439
440	return (swap_on_off_sfile(name, doingall));
441}
442
443static const char *
444swap_on_off_md(const char *name, char *mntops, int doingall)
445{
446	FILE *sfd;
447	int fd, mdunit, error;
448	const char *ret;
449	static char mdpath[PATH_MAX], linebuf[PATH_MAX];
450	char *p, *vnodefile;
451	size_t linelen;
452	u_long ul;
453
454	fd = -1;
455	sfd = NULL;
456	if (strlen(name) == (sizeof(MD_NAME) - 1))
457		mdunit = -1;
458	else {
459		errno = 0;
460		ul = strtoul(name + 2, &p, 10);
461		if (errno == 0) {
462			if (*p != '\0' || ul > INT_MAX)
463				errno = EINVAL;
464		}
465		if (errno) {
466			warn("Bad device unit: %s", name);
467			return (NULL);
468		}
469		mdunit = (int)ul;
470	}
471
472	vnodefile = NULL;
473	if ((p = strstr(mntops, "file=")) != NULL) {
474		vnodefile = strdup(p + sizeof("file=") - 1);
475		p = strchr(vnodefile, ',');
476		if (p != NULL)
477			*p = '\0';
478	}
479	if (vnodefile == NULL) {
480		warnx("file option not found for %s", name);
481		return (NULL);
482	}
483
484	if (which_prog == SWAPON) {
485		if (mdunit == -1) {
486			error = run_cmd(&fd, "%s -l -n -f %s",
487			    _PATH_MDCONFIG, vnodefile);
488			if (error == 0) {
489				/* md device found.  Ignore it. */
490				close(fd);
491				if (!qflag)
492					warnx("%s: Device already in use",
493					    vnodefile);
494				free(vnodefile);
495				return (NULL);
496			}
497			error = run_cmd(&fd, "%s -a -t vnode -n -f %s",
498			    _PATH_MDCONFIG, vnodefile);
499			if (error) {
500				warnx("mdconfig (attach) error: file=%s",
501				    vnodefile);
502				free(vnodefile);
503				return (NULL);
504			}
505			sfd = fdopen(fd, "r");
506			if (sfd == NULL) {
507				warn("mdconfig (attach) fdopen error");
508				ret = NULL;
509				goto err;
510			}
511			p = fgetln(sfd, &linelen);
512			if (p == NULL &&
513			    (linelen < 2 || linelen > sizeof(linebuf))) {
514				warn("mdconfig (attach) unexpected output");
515				ret = NULL;
516				goto err;
517			}
518			strncpy(linebuf, p, linelen);
519			linebuf[linelen - 1] = '\0';
520			errno = 0;
521			ul = strtoul(linebuf, &p, 10);
522			if (errno == 0) {
523				if (*p != '\0' || ul > INT_MAX)
524					errno = EINVAL;
525			}
526			if (errno) {
527				warn("mdconfig (attach) unexpected output: %s",
528				    linebuf);
529				ret = NULL;
530				goto err;
531			}
532			mdunit = (int)ul;
533		} else {
534			error = run_cmd(&fd, "%s -l -n -f %s -u %d",
535			    _PATH_MDCONFIG, vnodefile, mdunit);
536			if (error == 0) {
537				/* md device found.  Ignore it. */
538				close(fd);
539				if (!qflag)
540					warnx("md%d on %s: Device already "
541					    "in use", mdunit, vnodefile);
542				free(vnodefile);
543				return (NULL);
544			}
545			error = run_cmd(NULL, "%s -a -t vnode -u %d -f %s",
546			    _PATH_MDCONFIG, mdunit, vnodefile);
547			if (error) {
548				warnx("mdconfig (attach) error: "
549				    "md%d on file=%s", mdunit, vnodefile);
550				free(vnodefile);
551				return (NULL);
552			}
553		}
554	} else /* SWAPOFF */ {
555		if (mdunit == -1) {
556			error = run_cmd(&fd, "%s -l -n -f %s",
557			    _PATH_MDCONFIG, vnodefile);
558			if (error) {
559				/* md device not found.  Ignore it. */
560				close(fd);
561				if (!qflag)
562					warnx("md on %s: Device not found",
563					    vnodefile);
564				free(vnodefile);
565				return (NULL);
566			}
567			sfd = fdopen(fd, "r");
568			if (sfd == NULL) {
569				warn("mdconfig (list) fdopen error");
570				ret = NULL;
571				goto err;
572			}
573			p = fgetln(sfd, &linelen);
574			if (p == NULL &&
575			    (linelen < 2 || linelen > sizeof(linebuf) - 1)) {
576				warn("mdconfig (list) unexpected output");
577				ret = NULL;
578				goto err;
579			}
580			strncpy(linebuf, p, linelen);
581			linebuf[linelen - 1] = '\0';
582			p = strchr(linebuf, ' ');
583			if (p != NULL)
584				*p = '\0';
585			errno = 0;
586			ul = strtoul(linebuf, &p, 10);
587			if (errno == 0) {
588				if (*p != '\0' || ul > INT_MAX)
589					errno = EINVAL;
590			}
591			if (errno) {
592				warn("mdconfig (list) unexpected output: %s",
593				    linebuf);
594				ret = NULL;
595				goto err;
596			}
597			mdunit = (int)ul;
598		} else {
599			error = run_cmd(&fd, "%s -l -n -f %s -u %d",
600			    _PATH_MDCONFIG, vnodefile, mdunit);
601			if (error) {
602				/* md device not found.  Ignore it. */
603				close(fd);
604				if (!qflag)
605					warnx("md%d on %s: Device not found",
606					    mdunit, vnodefile);
607				free(vnodefile);
608				return (NULL);
609			}
610		}
611	}
612	snprintf(mdpath, sizeof(mdpath), "%s%s%d", _PATH_DEV,
613	    MD_NAME, mdunit);
614	mdpath[sizeof(mdpath) - 1] = '\0';
615	ret = swap_on_off_sfile(mdpath, doingall);
616
617	if (which_prog == SWAPOFF) {
618		if (ret != NULL) {
619			error = run_cmd(NULL, "%s -d -u %d",
620			    _PATH_MDCONFIG, mdunit);
621			if (error)
622				warn("mdconfig (detach) detach failed: %s%s%d",
623				    _PATH_DEV, MD_NAME, mdunit);
624		}
625	}
626err:
627	if (sfd != NULL)
628		fclose(sfd);
629	if (fd != -1)
630		close(fd);
631	free(vnodefile);
632	return (ret);
633}
634
635static int
636run_cmd(int *ofd, const char *cmdline, ...)
637{
638	va_list ap;
639	char **argv, **argvp, *cmd, *p;
640	int argc, pid, status, rv;
641	int pfd[2], nfd, dup2dn;
642
643	va_start(ap, cmdline);
644	rv = vasprintf(&cmd, cmdline, ap);
645	if (rv == -1) {
646		warn("%s", __func__);
647		return (rv);
648	}
649	va_end(ap);
650
651	for (argc = 1, p = cmd; (p = strchr(p, ' ')) != NULL; p++)
652		argc++;
653	argv = (char **)malloc(sizeof(*argv) * (argc + 1));
654	for (p = cmd, argvp = argv; (*argvp = strsep(&p, " ")) != NULL;)
655		if (**argvp != '\0' && (++argvp > &argv[argc])) {
656			*argvp = NULL;
657			break;
658		}
659	/* The argv array ends up NULL-terminated here. */
660#if 0
661	{
662		int i;
663
664		fprintf(stderr, "DEBUG: running:");
665		/* Should be equivalent to 'cmd' (before strsep, of course). */
666		for (i = 0; argv[i] != NULL; i++)
667			fprintf(stderr, " %s", argv[i]);
668		fprintf(stderr, "\n");
669	}
670#endif
671	dup2dn = 1;
672	if (ofd != NULL) {
673		if (pipe(&pfd[0]) == -1) {
674			warn("%s: pipe", __func__);
675			return (-1);
676		}
677		*ofd = pfd[0];
678		dup2dn = 0;
679	}
680	pid = fork();
681	switch (pid) {
682	case 0:
683		/* Child process. */
684		if (ofd != NULL)
685			if (dup2(pfd[1], STDOUT_FILENO) < 0)
686				err(1, "dup2 in %s", __func__);
687		nfd = open(_PATH_DEVNULL, O_RDWR);
688		if (nfd == -1)
689			err(1, "%s: open %s", __func__, _PATH_DEVNULL);
690		if (dup2(nfd, STDIN_FILENO) < 0)
691			err(1, "%s: dup2", __func__);
692		if (dup2dn && dup2(nfd, STDOUT_FILENO) < 0)
693			err(1, "%s: dup2", __func__);
694		if (dup2(nfd, STDERR_FILENO) < 0)
695			err(1, "%s: dup2", __func__);
696		execv(argv[0], argv);
697		warn("exec: %s", argv[0]);
698		_exit(-1);
699	case -1:
700		err(1, "%s: fork", __func__);
701	}
702	free(cmd);
703	free(argv);
704	while (waitpid(pid, &status, 0) != pid)
705		;
706	return (WEXITSTATUS(status));
707}
708
709static const char *
710swap_on_off_sfile(const char *name, int doingall)
711{
712	int error;
713
714	if (which_prog == SWAPON)
715		error = swapon(name);
716	else /* SWAPOFF */
717		error = swapoff(name);
718
719	if (error == -1) {
720		switch (errno) {
721		case EBUSY:
722			if (!doingall)
723				warnx("%s: Device already in use", name);
724			break;
725		case EINVAL:
726			if (which_prog == SWAPON)
727				warnx("%s: NSWAPDEV limit reached", name);
728			else if (!doingall)
729				warn("%s", name);
730			break;
731		default:
732			warn("%s", name);
733			break;
734		}
735		return (NULL);
736	}
737	return (name);
738}
739
740static void
741usage(void)
742{
743	fprintf(stderr, "usage: %s ", getprogname());
744	switch(orig_prog) {
745	case SWAPON:
746	case SWAPOFF:
747	    fprintf(stderr, "[-F fstab] -aLq | file ...\n");
748	    break;
749	case SWAPCTL:
750	    fprintf(stderr, "[-AghklmsU] [-a file ... | -d file ...]\n");
751	    break;
752	}
753	exit(1);
754}
755
756static void
757sizetobuf(char *buf, size_t bufsize, int hflag, long long val, int hlen,
758    long blocksize)
759{
760
761	if (hflag == 'H') {
762		char tmp[16];
763
764		humanize_number(tmp, 5, (int64_t)val, "", HN_AUTOSCALE,
765		    HN_B | HN_NOSPACE | HN_DECIMAL);
766		snprintf(buf, bufsize, "%*s", hlen, tmp);
767	} else {
768		snprintf(buf, bufsize, "%*lld", hlen, val / blocksize);
769	}
770}
771
772static void
773swaplist(int lflag, int sflag, int hflag)
774{
775	size_t mibsize, size;
776	struct xswdev xsw;
777	int hlen, mib[16], n, pagesize;
778	long blocksize;
779	long long total = 0;
780	long long used = 0;
781	long long tmp_total;
782	long long tmp_used;
783	char buf[32];
784
785	pagesize = getpagesize();
786	switch(hflag) {
787	case 'G':
788	    blocksize = 1024 * 1024 * 1024;
789	    strlcpy(buf, "1GB-blocks", sizeof(buf));
790	    hlen = 10;
791	    break;
792	case 'H':
793	    blocksize = -1;
794	    strlcpy(buf, "Bytes", sizeof(buf));
795	    hlen = 10;
796	    break;
797	case 'K':
798	    blocksize = 1024;
799	    strlcpy(buf, "1kB-blocks", sizeof(buf));
800	    hlen = 10;
801	    break;
802	case 'M':
803	    blocksize = 1024 * 1024;
804	    strlcpy(buf, "1MB-blocks", sizeof(buf));
805	    hlen = 10;
806	    break;
807	default:
808	    getbsize(&hlen, &blocksize);
809	    snprintf(buf, sizeof(buf), "%ld-blocks", blocksize);
810	    break;
811	}
812
813	mibsize = sizeof mib / sizeof mib[0];
814	if (sysctlnametomib("vm.swap_info", mib, &mibsize) == -1)
815		err(1, "sysctlnametomib()");
816
817	if (lflag) {
818		printf("%-13s %*s %*s\n",
819		    "Device:",
820		    hlen, buf,
821		    hlen, "Used:");
822	}
823
824	for (n = 0; ; ++n) {
825		mib[mibsize] = n;
826		size = sizeof xsw;
827		if (sysctl(mib, mibsize + 1, &xsw, &size, NULL, 0) == -1)
828			break;
829		if (xsw.xsw_version != XSWDEV_VERSION)
830			errx(1, "xswdev version mismatch");
831
832		tmp_total = (long long)xsw.xsw_nblks * pagesize;
833		tmp_used  = (long long)xsw.xsw_used * pagesize;
834		total += tmp_total;
835		used  += tmp_used;
836		if (lflag) {
837			sizetobuf(buf, sizeof(buf), hflag, tmp_total, hlen,
838			    blocksize);
839			printf("/dev/%-8s %s ", devname(xsw.xsw_dev, S_IFCHR),
840			    buf);
841			sizetobuf(buf, sizeof(buf), hflag, tmp_used, hlen,
842			    blocksize);
843			printf("%s\n", buf);
844		}
845	}
846	if (errno != ENOENT)
847		err(1, "sysctl()");
848
849	if (sflag) {
850		sizetobuf(buf, sizeof(buf), hflag, total, hlen, blocksize);
851		printf("Total:        %s ", buf);
852		sizetobuf(buf, sizeof(buf), hflag, used, hlen, blocksize);
853		printf("%s\n", buf);
854	}
855}
856
857