2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
32 static const char copyright[] =
33 "@(#) Copyright (c) 1980, 1993\n\
34 The Regents of the University of California. All rights reserved.\n";
38 static char sccsid[] = "@(#)swapon.c 8.1 (Berkeley) 6/5/93";
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
44 #include <sys/param.h>
45 #include <sys/types.h>
46 #include <sys/mdioctl.h>
48 #include <sys/sysctl.h>
50 #include <vm/vm_param.h>
67 static void usage(void);
68 static const char *swap_on_off(char *, int, char *);
69 static const char *swap_on_off_gbde(char *, int);
70 static const char *swap_on_off_geli(char *, char *, int);
71 static const char *swap_on_off_md(char *, char *, int);
72 static const char *swap_on_off_sfile(char *, int);
73 static void swaplist(int, int, int);
74 static int run_cmd(int *, const char *, ...) __printflike(2, 3);
76 static enum { SWAPON, SWAPOFF, SWAPCTL } orig_prog, which_prog = SWAPCTL;
81 main(int argc, char **argv)
88 int sflag = 0, lflag = 0, late = 0, hflag = 0;
89 const char *etc_fstab;
91 if ((ptr = strrchr(argv[0], '/')) == NULL)
93 if (strstr(ptr, "swapon"))
95 else if (strstr(ptr, "swapoff"))
97 orig_prog = which_prog;
101 while ((ch = getopt(argc, argv, "AadghklLmqsUF:")) != -1) {
104 if (which_prog == SWAPCTL) {
112 if (which_prog == SWAPON || which_prog == SWAPOFF)
118 if (which_prog == SWAPCTL)
119 which_prog = SWAPOFF;
142 if (which_prog == SWAPON || which_prog == SWAPOFF)
149 if (which_prog == SWAPCTL) {
151 which_prog = SWAPOFF;
168 if (etc_fstab != NULL)
170 if (which_prog == SWAPON || which_prog == SWAPOFF) {
172 while ((fsp = getfsent()) != NULL) {
173 if (strcmp(fsp->fs_type, FSTAB_SW))
175 if (strstr(fsp->fs_mntops, "noauto"))
177 if (which_prog != SWAPOFF &&
178 strstr(fsp->fs_mntops, "late") &&
181 swfile = swap_on_off(fsp->fs_spec, 1,
183 if (swfile == NULL) {
188 printf("%s: %sing %s as swap device\n",
190 (which_prog == SWAPOFF) ?
191 "remov" : "add", swfile);
197 for (; *argv; ++argv) {
198 swfile = swap_on_off(*argv, 0, NULL);
199 if (swfile == NULL) {
203 if (orig_prog == SWAPCTL) {
204 printf("%s: %sing %s as swap device\n",
206 (which_prog == SWAPOFF) ? "remov" : "add",
212 swaplist(lflag, sflag, hflag);
220 swap_on_off(char *name, int doingall, char *mntops)
224 /* Swap on vnode-backed md(4) device. */
225 if (mntops != NULL &&
226 (fnmatch(_PATH_DEV MD_NAME "[0-9]*", name, 0) != FNM_NOMATCH ||
227 fnmatch(MD_NAME "[0-9]*", name, 0) != FNM_NOMATCH ||
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));
233 /* Swap on encrypted device by GEOM_BDE. */
234 basename_r(name, base);
235 if (fnmatch("*.bde", base, 0) != FNM_NOMATCH)
236 return (swap_on_off_gbde(name, doingall));
238 /* Swap on encrypted device by GEOM_ELI. */
239 if (fnmatch("*.eli", base, 0) != FNM_NOMATCH)
240 return (swap_on_off_geli(name, mntops, doingall));
242 /* Swap on special file. */
243 return (swap_on_off_sfile(name, doingall));
247 swap_on_off_gbde(char *name, int doingall)
250 char pass[64 * 2 + 1], bpass[64];
254 dname = strdup(name);
255 p = strrchr(dname, '.');
257 warnx("%s: Malformed device name", name);
263 switch (which_prog) {
265 arc4random_buf(bpass, sizeof(bpass));
266 for (i = 0; i < (int)sizeof(bpass); i++)
267 sprintf(&pass[2 * i], "%02x", bpass[i]);
268 pass[sizeof(pass) - 1] = '\0';
270 error = run_cmd(&fd, "%s init %s -P %s", _PATH_GBDE,
273 /* bde device found. Ignore it. */
276 warnx("%s: Device already in use", name);
280 error = run_cmd(&fd, "%s attach %s -p %s", _PATH_GBDE,
284 warnx("gbde (attach) error: %s", name);
296 ret = swap_on_off_sfile(name, doingall);
299 switch (which_prog) {
301 error = run_cmd(&fd, "%s detach %s", _PATH_GBDE, dname);
303 /* bde device not found. Ignore it. */
305 warnx("%s: Device not found", dname);
320 swap_on_off_geli(char *name, char *mntops, int doingall)
322 const char *ops, *aalgo, *ealgo, *keylen_str, *sectorsize_str;
326 int fd, error, keylen, sectorsize;
329 dname = strdup(name);
330 p = strrchr(dname, '.');
332 warnx("%s: Malformed device name", name);
337 ops = strdup(mntops);
339 /* Default parameters for geli(8). */
340 aalgo = "hmac/sha256";
345 if ((p = strstr(ops, "aalgo=")) != NULL) {
346 aalgo = p + sizeof("aalgo=") - 1;
347 p = strchr(aalgo, ',');
351 if ((p = strstr(ops, "ealgo=")) != NULL) {
352 ealgo = p + sizeof("ealgo=") - 1;
353 p = strchr(ealgo, ',');
357 if ((p = strstr(ops, "keylen=")) != NULL) {
358 keylen_str = p + sizeof("keylen=") - 1;
359 p = strchr(keylen_str, ',');
363 ul = strtoul(keylen_str, &p, 10);
365 if (*p != '\0' || ul > INT_MAX)
369 warn("Invalid keylen: %s", keylen_str);
374 if ((p = strstr(ops, "sectorsize=")) != NULL) {
375 sectorsize_str = p + sizeof("sectorsize=") - 1;
376 p = strchr(sectorsize_str, ',');
380 ul = strtoul(sectorsize_str, &p, 10);
382 if (*p != '\0' || ul > INT_MAX)
386 warn("Invalid sectorsize: %s", sectorsize_str);
389 sectorsize = (int)ul;
391 snprintf(args, sizeof(args), "-a %s -e %s -l %d -s %d -d",
392 aalgo, ealgo, keylen, sectorsize);
393 args[sizeof(args) - 1] = '\0';
397 switch (which_prog) {
399 error = run_cmd(&fd, "%s onetime %s %s", _PATH_GELI, args,
402 /* eli device found. Ignore it. */
405 warnx("%s: Device already in use "
406 "or invalid parameters", name);
411 if (stat(name, &sb) == -1 && errno == ENOENT) {
413 warnx("%s: Device not found", name);
424 return (swap_on_off_sfile(name, doingall));
428 swap_on_off_md(char *name, char *mntops, int doingall)
431 int fd, mdunit, error;
433 char mdpath[PATH_MAX], linebuf[PATH_MAX];
440 if (strlen(name) == (sizeof(MD_NAME) - 1))
444 ul = strtoul(name + 2, &p, 10);
446 if (*p != '\0' || ul > INT_MAX)
450 warn("Bad device unit: %s", name);
457 if ((p = strstr(mntops, "file=")) != NULL) {
458 vnodefile = strdup(p + sizeof("file=") - 1);
459 p = strchr(vnodefile, ',');
463 if (vnodefile == NULL) {
464 warnx("file option not found for %s", name);
468 switch (which_prog) {
471 error = run_cmd(&fd, "%s -l -n -f %s",
472 _PATH_MDCONFIG, vnodefile);
474 /* md device found. Ignore it. */
477 warnx("%s: Device already in use",
481 error = run_cmd(&fd, "%s -a -t vnode -n -f %s",
482 _PATH_MDCONFIG, vnodefile);
484 warnx("mdconfig (attach) error: file=%s",
488 sfd = fdopen(fd, "r");
490 warn("mdconfig (attach) fdopen error");
494 p = fgetln(sfd, &linelen);
496 (linelen < 2 || linelen > sizeof(linebuf))) {
497 warn("mdconfig (attach) unexpected output");
501 strncpy(linebuf, p, linelen);
502 linebuf[linelen - 1] = '\0';
504 ul = strtoul(linebuf, &p, 10);
506 if (*p != '\0' || ul > INT_MAX)
510 warn("mdconfig (attach) unexpected output: %s",
517 error = run_cmd(&fd, "%s -l -n -f %s -u %d",
518 _PATH_MDCONFIG, vnodefile, mdunit);
520 /* md device found. Ignore it. */
523 warnx("md%d on %s: Device already "
524 "in use", mdunit, vnodefile);
527 error = run_cmd(NULL, "%s -a -t vnode -u %d -f %s",
528 _PATH_MDCONFIG, mdunit, vnodefile);
530 warnx("mdconfig (attach) error: "
531 "md%d on file=%s", mdunit, vnodefile);
538 error = run_cmd(&fd, "%s -l -n -f %s",
539 _PATH_MDCONFIG, vnodefile);
541 /* md device not found. Ignore it. */
544 warnx("md on %s: Device not found",
548 sfd = fdopen(fd, "r");
550 warn("mdconfig (list) fdopen error");
554 p = fgetln(sfd, &linelen);
556 (linelen < 2 || linelen > sizeof(linebuf) - 1)) {
557 warn("mdconfig (list) unexpected output");
561 strncpy(linebuf, p, linelen);
562 linebuf[linelen - 1] = '\0';
563 p = strchr(linebuf, ' ');
567 ul = strtoul(linebuf, &p, 10);
569 if (*p != '\0' || ul > INT_MAX)
573 warn("mdconfig (list) unexpected output: %s",
580 error = run_cmd(&fd, "%s -l -n -f %s -u %d",
581 _PATH_MDCONFIG, vnodefile, mdunit);
583 /* md device not found. Ignore it. */
586 warnx("md%d on %s: Device not found",
595 snprintf(mdpath, sizeof(mdpath), "%s%s%d", _PATH_DEV,
597 mdpath[sizeof(mdpath) - 1] = '\0';
598 ret = swap_on_off_sfile(mdpath, doingall);
600 switch (which_prog) {
603 error = run_cmd(NULL, "%s -d -u %d",
604 _PATH_MDCONFIG, mdunit);
606 warn("mdconfig (detach) detach failed: %s%s%d",
607 _PATH_DEV, MD_NAME, mdunit);
622 run_cmd(int *ofd, const char *cmdline, ...)
625 char **argv, **argvp, *cmd, *p;
626 int argc, pid, status, rv;
627 int pfd[2], nfd, dup2dn;
629 va_start(ap, cmdline);
630 rv = vasprintf(&cmd, cmdline, ap);
632 warn("%s", __func__);
637 for (argc = 1, p = cmd; (p = strchr(p, ' ')) != NULL; p++)
639 argv = (char **)malloc(sizeof(*argv) * (argc + 1));
640 for (p = cmd, argvp = argv; (*argvp = strsep(&p, " ")) != NULL;)
641 if (**argvp != '\0' && (++argvp > &argv[argc])) {
645 /* The argv array ends up NULL-terminated here. */
650 fprintf(stderr, "DEBUG: running:");
651 /* Should be equivalent to 'cmd' (before strsep, of course). */
652 for (i = 0; argv[i] != NULL; i++)
653 fprintf(stderr, " %s", argv[i]);
654 fprintf(stderr, "\n");
659 if (pipe(&pfd[0]) == -1) {
660 warn("%s: pipe", __func__);
671 if (dup2(pfd[1], STDOUT_FILENO) < 0)
672 err(1, "dup2 in %s", __func__);
673 nfd = open(_PATH_DEVNULL, O_RDWR);
675 err(1, "%s: open %s", __func__, _PATH_DEVNULL);
676 if (dup2(nfd, STDIN_FILENO) < 0)
677 err(1, "%s: dup2", __func__);
678 if (dup2dn && dup2(nfd, STDOUT_FILENO) < 0)
679 err(1, "%s: dup2", __func__);
680 if (dup2(nfd, STDERR_FILENO) < 0)
681 err(1, "%s: dup2", __func__);
682 execv(argv[0], argv);
683 warn("exec: %s", argv[0]);
686 err(1, "%s: fork", __func__);
690 while (waitpid(pid, &status, 0) != pid)
692 return (WEXITSTATUS(status));
696 swap_on_off_sfile(char *name, int doingall)
700 switch (which_prog) {
702 error = swapon(name);
705 error = swapoff(name);
715 warnx("%s: Device already in use", name);
718 if (which_prog == SWAPON)
719 warnx("%s: NSWAPDEV limit reached", name);
735 fprintf(stderr, "usage: %s ", getprogname());
739 fprintf(stderr, "[-F fstab] -aLq | file ...\n");
742 fprintf(stderr, "[-AghklmsU] [-a file ... | -d file ...]\n");
749 sizetobuf(char *buf, size_t bufsize, int hflag, long long val, int hlen,
756 humanize_number(tmp, 5, (int64_t)val, "", HN_AUTOSCALE,
757 HN_B | HN_NOSPACE | HN_DECIMAL);
758 snprintf(buf, bufsize, "%*s", hlen, tmp);
760 snprintf(buf, bufsize, "%*lld", hlen, val / blocksize);
765 swaplist(int lflag, int sflag, int hflag)
767 size_t mibsize, size;
769 int hlen, mib[16], n, pagesize;
777 pagesize = getpagesize();
780 blocksize = 1024 * 1024 * 1024;
781 strlcpy(buf, "1GB-blocks", sizeof(buf));
786 strlcpy(buf, "Bytes", sizeof(buf));
791 strlcpy(buf, "1kB-blocks", sizeof(buf));
795 blocksize = 1024 * 1024;
796 strlcpy(buf, "1MB-blocks", sizeof(buf));
800 getbsize(&hlen, &blocksize);
801 snprintf(buf, sizeof(buf), "%ld-blocks", blocksize);
805 mibsize = sizeof mib / sizeof mib[0];
806 if (sysctlnametomib("vm.swap_info", mib, &mibsize) == -1)
807 err(1, "sysctlnametomib()");
810 printf("%-13s %*s %*s\n",
819 if (sysctl(mib, mibsize + 1, &xsw, &size, NULL, 0) == -1)
821 if (xsw.xsw_version != XSWDEV_VERSION)
822 errx(1, "xswdev version mismatch");
824 tmp_total = (long long)xsw.xsw_nblks * pagesize;
825 tmp_used = (long long)xsw.xsw_used * pagesize;
829 sizetobuf(buf, sizeof(buf), hflag, tmp_total, hlen,
831 printf("/dev/%-8s %s ", devname(xsw.xsw_dev, S_IFCHR),
833 sizetobuf(buf, sizeof(buf), hflag, tmp_used, hlen,
842 sizetobuf(buf, sizeof(buf), hflag, total, hlen, blocksize);
843 printf("Total: %s ", buf);
844 sizetobuf(buf, sizeof(buf), hflag, used, hlen, blocksize);