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