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