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(const char *, int, char *);
69 static const char *swap_on_off_gbde(const char *, int);
70 static const char *swap_on_off_geli(const char *, char *, int);
71 static const char *swap_on_off_md(const char *, char *, int);
72 static const char *swap_on_off_sfile(const 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)
87 int sflag, lflag, late, hflag;
88 const char *etc_fstab;
90 sflag = lflag = late = hflag = 0;
91 if ((ptr = strrchr(argv[0], '/')) == NULL)
93 if (strstr(ptr, "swapon") != NULL)
95 else if (strstr(ptr, "swapoff") != NULL)
97 orig_prog = which_prog;
101 while ((ch = getopt(argc, argv, "AadghklLmqsUF:")) != -1) {
104 if (which_prog == SWAPCTL) {
111 if (which_prog == SWAPON || which_prog == SWAPOFF)
117 if (which_prog == SWAPCTL)
118 which_prog = SWAPOFF;
141 if (which_prog == SWAPON || which_prog == SWAPOFF)
148 if (which_prog == SWAPCTL) {
150 which_prog = SWAPOFF;
166 if (etc_fstab != NULL)
168 if (which_prog == SWAPON || which_prog == SWAPOFF) {
170 while ((fsp = getfsent()) != NULL) {
171 if (strcmp(fsp->fs_type, FSTAB_SW) != 0)
173 if (strstr(fsp->fs_mntops, "noauto") != NULL)
175 if (which_prog != SWAPOFF &&
176 strstr(fsp->fs_mntops, "late") &&
179 swfile = swap_on_off(fsp->fs_spec, 1,
181 if (swfile == NULL) {
186 printf("%s: %sing %s as swap device\n",
188 (which_prog == SWAPOFF) ?
189 "remov" : "add", swfile);
192 } else if (*argv == NULL)
194 for (; *argv; ++argv) {
195 swfile = swap_on_off(*argv, 0, NULL);
196 if (swfile == NULL) {
200 if (orig_prog == SWAPCTL) {
201 printf("%s: %sing %s as swap device\n",
203 (which_prog == SWAPOFF) ? "remov" : "add",
209 swaplist(lflag, sflag, hflag);
217 swap_on_off(const char *name, int doingall, char *mntops)
221 /* Swap on vnode-backed md(4) device. */
222 if (mntops != NULL &&
223 (fnmatch(_PATH_DEV MD_NAME "[0-9]*", name, 0) == 0 ||
224 fnmatch(MD_NAME "[0-9]*", name, 0) == 0 ||
225 strncmp(_PATH_DEV MD_NAME, name,
226 sizeof(_PATH_DEV) + sizeof(MD_NAME)) == 0 ||
227 strncmp(MD_NAME, name, sizeof(MD_NAME)) == 0))
228 return (swap_on_off_md(name, mntops, doingall));
230 basename_r(name, base);
232 /* Swap on encrypted device by GEOM_BDE. */
233 if (fnmatch("*.bde", base, 0) == 0)
234 return (swap_on_off_gbde(name, doingall));
236 /* Swap on encrypted device by GEOM_ELI. */
237 if (fnmatch("*.eli", base, 0) == 0)
238 return (swap_on_off_geli(name, mntops, doingall));
240 /* Swap on special file. */
241 return (swap_on_off_sfile(name, doingall));
244 /* Strip off .bde or .eli suffix from swap device name */
246 swap_basename(const char *name)
250 dname = strdup(name);
251 p = strrchr(dname, '.');
252 /* assert(p != NULL); */
259 swap_on_off_gbde(const char *name, int doingall)
262 char pass[64 * 2 + 1];
263 unsigned char bpass[64];
267 dname = swap_basename(name);
271 if (which_prog == SWAPON) {
272 arc4random_buf(bpass, sizeof(bpass));
273 for (i = 0; i < (int)sizeof(bpass); i++)
274 sprintf(&pass[2 * i], "%02x", bpass[i]);
275 pass[sizeof(pass) - 1] = '\0';
277 error = run_cmd(NULL, "%s init %s -P %s", _PATH_GBDE,
280 /* bde device found. Ignore it. */
283 warnx("%s: Device already in use", name);
286 error = run_cmd(NULL, "%s attach %s -p %s", _PATH_GBDE,
290 warnx("gbde (attach) error: %s", name);
295 ret = swap_on_off_sfile(name, doingall);
297 if (which_prog == SWAPOFF) {
298 error = run_cmd(NULL, "%s detach %s", _PATH_GBDE, dname);
301 /* bde device not found. Ignore it. */
303 warnx("%s: Device not found", name);
311 /* Build geli(8) arguments from mntops */
313 swap_on_geli_args(const char *mntops)
315 const char *aalgo, *ealgo, *keylen_str, *sectorsize_str;
316 const char *aflag, *eflag, *lflag, *sflag;
317 char *p, *args, *token, *string, *ops;
318 int argsize, pagesize;
322 /* Use built-in defaults for geli(8). */
323 aalgo = ealgo = keylen_str = "";
324 aflag = eflag = lflag = "";
326 /* We will always specify sectorsize. */
328 sectorsize_str = NULL;
330 if (mntops != NULL) {
331 string = ops = strdup(mntops);
333 while ((token = strsep(&string, ",")) != NULL) {
334 if ((p = strstr(token, "aalgo=")) == token) {
335 aalgo = p + sizeof("aalgo=") - 1;
337 } else if ((p = strstr(token, "ealgo=")) == token) {
338 ealgo = p + sizeof("ealgo=") - 1;
340 } else if ((p = strstr(token, "keylen=")) == token) {
341 keylen_str = p + sizeof("keylen=") - 1;
343 ul = strtoul(keylen_str, &p, 10);
345 if (*p != '\0' || ul > INT_MAX)
349 warn("Invalid keylen: %s", keylen_str);
354 } else if ((p = strstr(token, "sectorsize=")) == token) {
355 sectorsize_str = p + sizeof("sectorsize=") - 1;
357 ul = strtoul(sectorsize_str, &p, 10);
359 if (*p != '\0' || ul > INT_MAX)
363 warn("Invalid sectorsize: %s",
368 } else if (strcmp(token, "sw") != 0) {
369 warnx("Invalid option: %s", token);
378 * If we do not have a sector size at this point, fill in
379 * pagesize as sector size.
381 if (sectorsize_str == NULL) {
382 /* Use pagesize as default sectorsize. */
383 pagesize = getpagesize();
384 pagesize_len = snprintf(NULL, 0, "%d", pagesize) + 1;
385 p = alloca(pagesize_len);
386 snprintf(p, pagesize_len, "%d", pagesize);
390 argsize = asprintf(&args, "%s%s%s%s%s%s%s%s -d",
391 aflag, aalgo, eflag, ealgo, lflag, keylen_str,
392 sflag, sectorsize_str);
399 swap_on_off_geli(const char *name, char *mntops, int doingall)
405 error = stat(name, &sb);
407 if (which_prog == SWAPON) do {
408 /* Skip if the .eli device already exists. */
412 args = swap_on_geli_args(mntops);
416 dname = swap_basename(name);
422 error = run_cmd(NULL, "%s onetime%s %s", _PATH_GELI, args,
429 /* error occured during creation. */
431 warnx("%s: Invalid parameters", name);
436 return (swap_on_off_sfile(name, doingall));
440 swap_on_off_md(const char *name, char *mntops, int doingall)
443 int fd, mdunit, error;
445 static char mdpath[PATH_MAX], linebuf[PATH_MAX];
452 if (strlen(name) == (sizeof(MD_NAME) - 1))
456 ul = strtoul(name + 2, &p, 10);
458 if (*p != '\0' || ul > INT_MAX)
462 warn("Bad device unit: %s", name);
469 if ((p = strstr(mntops, "file=")) != NULL) {
470 vnodefile = strdup(p + sizeof("file=") - 1);
471 p = strchr(vnodefile, ',');
475 if (vnodefile == NULL) {
476 warnx("file option not found for %s", name);
480 if (which_prog == SWAPON) {
482 error = run_cmd(&fd, "%s -l -n -f %s",
483 _PATH_MDCONFIG, vnodefile);
485 /* md device found. Ignore it. */
488 warnx("%s: Device already in use",
493 error = run_cmd(&fd, "%s -a -t vnode -n -f %s",
494 _PATH_MDCONFIG, vnodefile);
496 warnx("mdconfig (attach) error: file=%s",
501 sfd = fdopen(fd, "r");
503 warn("mdconfig (attach) fdopen error");
507 p = fgetln(sfd, &linelen);
509 (linelen < 2 || linelen > sizeof(linebuf))) {
510 warn("mdconfig (attach) unexpected output");
514 strncpy(linebuf, p, linelen);
515 linebuf[linelen - 1] = '\0';
517 ul = strtoul(linebuf, &p, 10);
519 if (*p != '\0' || ul > INT_MAX)
523 warn("mdconfig (attach) unexpected output: %s",
530 error = run_cmd(&fd, "%s -l -n -f %s -u %d",
531 _PATH_MDCONFIG, vnodefile, mdunit);
533 /* md device found. Ignore it. */
536 warnx("md%d on %s: Device already "
537 "in use", mdunit, vnodefile);
541 error = run_cmd(NULL, "%s -a -t vnode -u %d -f %s",
542 _PATH_MDCONFIG, mdunit, vnodefile);
544 warnx("mdconfig (attach) error: "
545 "md%d on file=%s", mdunit, vnodefile);
550 } else /* SWAPOFF */ {
552 error = run_cmd(&fd, "%s -l -n -f %s",
553 _PATH_MDCONFIG, vnodefile);
555 /* md device not found. Ignore it. */
558 warnx("md on %s: Device not found",
563 sfd = fdopen(fd, "r");
565 warn("mdconfig (list) fdopen error");
569 p = fgetln(sfd, &linelen);
571 (linelen < 2 || linelen > sizeof(linebuf) - 1)) {
572 warn("mdconfig (list) unexpected output");
576 strncpy(linebuf, p, linelen);
577 linebuf[linelen - 1] = '\0';
578 p = strchr(linebuf, ' ');
582 ul = strtoul(linebuf, &p, 10);
584 if (*p != '\0' || ul > INT_MAX)
588 warn("mdconfig (list) unexpected output: %s",
595 error = run_cmd(&fd, "%s -l -n -f %s -u %d",
596 _PATH_MDCONFIG, vnodefile, mdunit);
598 /* md device not found. Ignore it. */
601 warnx("md%d on %s: Device not found",
608 snprintf(mdpath, sizeof(mdpath), "%s%s%d", _PATH_DEV,
610 mdpath[sizeof(mdpath) - 1] = '\0';
611 ret = swap_on_off_sfile(mdpath, doingall);
613 if (which_prog == SWAPOFF) {
615 error = run_cmd(NULL, "%s -d -u %d",
616 _PATH_MDCONFIG, mdunit);
618 warn("mdconfig (detach) detach failed: %s%s%d",
619 _PATH_DEV, MD_NAME, mdunit);
632 run_cmd(int *ofd, const char *cmdline, ...)
635 char **argv, **argvp, *cmd, *p;
636 int argc, pid, status, rv;
637 int pfd[2], nfd, dup2dn;
639 va_start(ap, cmdline);
640 rv = vasprintf(&cmd, cmdline, ap);
642 warn("%s", __func__);
647 for (argc = 1, p = cmd; (p = strchr(p, ' ')) != NULL; p++)
649 argv = (char **)malloc(sizeof(*argv) * (argc + 1));
650 for (p = cmd, argvp = argv; (*argvp = strsep(&p, " ")) != NULL;)
651 if (**argvp != '\0' && (++argvp > &argv[argc])) {
655 /* The argv array ends up NULL-terminated here. */
660 fprintf(stderr, "DEBUG: running:");
661 /* Should be equivalent to 'cmd' (before strsep, of course). */
662 for (i = 0; argv[i] != NULL; i++)
663 fprintf(stderr, " %s", argv[i]);
664 fprintf(stderr, "\n");
669 if (pipe(&pfd[0]) == -1) {
670 warn("%s: pipe", __func__);
681 if (dup2(pfd[1], STDOUT_FILENO) < 0)
682 err(1, "dup2 in %s", __func__);
683 nfd = open(_PATH_DEVNULL, O_RDWR);
685 err(1, "%s: open %s", __func__, _PATH_DEVNULL);
686 if (dup2(nfd, STDIN_FILENO) < 0)
687 err(1, "%s: dup2", __func__);
688 if (dup2dn && dup2(nfd, STDOUT_FILENO) < 0)
689 err(1, "%s: dup2", __func__);
690 if (dup2(nfd, STDERR_FILENO) < 0)
691 err(1, "%s: dup2", __func__);
692 execv(argv[0], argv);
693 warn("exec: %s", argv[0]);
696 err(1, "%s: fork", __func__);
700 while (waitpid(pid, &status, 0) != pid)
702 return (WEXITSTATUS(status));
706 swap_on_off_sfile(const char *name, int doingall)
710 if (which_prog == SWAPON)
711 error = swapon(name);
713 error = swapoff(name);
719 warnx("%s: Device already in use", name);
722 if (which_prog == SWAPON)
723 warnx("%s: NSWAPDEV limit reached", name);
724 else if (doingall == 0)
740 fprintf(stderr, "usage: %s ", getprogname());
744 fprintf(stderr, "[-F fstab] -aLq | file ...\n");
747 fprintf(stderr, "[-AghklmsU] [-a file ... | -d file ...]\n");
754 sizetobuf(char *buf, size_t bufsize, int hflag, long long val, int hlen,
760 humanize_number(tmp, 5, (int64_t)val, "", HN_AUTOSCALE,
761 HN_B | HN_NOSPACE | HN_DECIMAL);
762 snprintf(buf, bufsize, "%*s", hlen, tmp);
764 snprintf(buf, bufsize, "%*lld", hlen, val / blocksize);
768 swaplist(int lflag, int sflag, int hflag)
770 size_t mibsize, size;
772 int hlen, mib[16], n, pagesize;
780 pagesize = getpagesize();
783 blocksize = 1024 * 1024 * 1024;
784 strlcpy(buf, "1GB-blocks", sizeof(buf));
789 strlcpy(buf, "Bytes", sizeof(buf));
794 strlcpy(buf, "1kB-blocks", sizeof(buf));
798 blocksize = 1024 * 1024;
799 strlcpy(buf, "1MB-blocks", sizeof(buf));
803 getbsize(&hlen, &blocksize);
804 snprintf(buf, sizeof(buf), "%ld-blocks", blocksize);
808 mibsize = nitems(mib);
809 if (sysctlnametomib("vm.swap_info", mib, &mibsize) == -1)
810 err(1, "sysctlnametomib()");
813 printf("%-13s %*s %*s\n",
822 if (sysctl(mib, mibsize + 1, &xsw, &size, NULL, 0) == -1)
824 if (xsw.xsw_version != XSWDEV_VERSION)
825 errx(1, "xswdev version mismatch");
827 tmp_total = (long long)xsw.xsw_nblks * pagesize;
828 tmp_used = (long long)xsw.xsw_used * pagesize;
832 sizetobuf(buf, sizeof(buf), hflag, tmp_total, hlen,
834 printf("/dev/%-8s %s ", devname(xsw.xsw_dev, S_IFCHR),
836 sizetobuf(buf, sizeof(buf), hflag, tmp_used, hlen,
845 sizetobuf(buf, sizeof(buf), hflag, total, hlen, blocksize);
846 printf("Total: %s ", buf);
847 sizetobuf(buf, sizeof(buf), hflag, used, hlen, blocksize);