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