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