]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/swapon/swapon.c
Fix build.
[FreeBSD/FreeBSD.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(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);
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;
87         int ch, doall;
88         int sflag = 0, lflag = 0, late = 0, hflag = 0;
89         const char *etc_fstab;
90
91         if ((ptr = strrchr(argv[0], '/')) == NULL)
92                 ptr = argv[0];
93         if (strstr(ptr, "swapon"))
94                 which_prog = SWAPON;
95         else if (strstr(ptr, "swapoff"))
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                         }
110                         break;
111                 case 'a':
112                         if (which_prog == SWAPON || which_prog == SWAPOFF)
113                                 doall = 1;
114                         else
115                                 which_prog = SWAPON;
116                         break;
117                 case 'd':
118                         if (which_prog == SWAPCTL)
119                                 which_prog = SWAPOFF;
120                         else
121                                 usage();
122                         break;
123                 case 'g':
124                         hflag = 'G';
125                         break;
126                 case 'h':
127                         hflag = 'H';
128                         break;
129                 case 'k':
130                         hflag = 'K';
131                         break;
132                 case 'l':
133                         lflag = 1;
134                         break;
135                 case 'L':
136                         late = 1;
137                         break;
138                 case 'm':
139                         hflag = 'M';
140                         break;
141                 case 'q':
142                         if (which_prog == SWAPON || which_prog == SWAPOFF)
143                                 qflag = 1;
144                         break;
145                 case 's':
146                         sflag = 1;
147                         break;
148                 case 'U':
149                         if (which_prog == SWAPCTL) {
150                                 doall = 1;
151                                 which_prog = SWAPOFF;
152                         } else {
153                                 usage();
154                         }
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))
174                                         continue;
175                                 if (strstr(fsp->fs_mntops, "noauto"))
176                                         continue;
177                                 if (which_prog != SWAPOFF &&
178                                     strstr(fsp->fs_mntops, "late") &&
179                                     !late)
180                                         continue;
181                                 swfile = swap_on_off(fsp->fs_spec, 1,
182                                     fsp->fs_mntops);
183                                 if (swfile == NULL) {
184                                         ret = 1;
185                                         continue;
186                                 }
187                                 if (!qflag) {
188                                         printf("%s: %sing %s as swap device\n",
189                                             getprogname(),
190                                             (which_prog == SWAPOFF) ?
191                                             "remov" : "add", swfile);
192                                 }
193                         }
194                 }
195                 else if (!*argv)
196                         usage();
197                 for (; *argv; ++argv) {
198                         swfile = swap_on_off(*argv, 0, NULL);
199                         if (swfile == NULL) {
200                                 ret = 1;
201                                 continue;
202                         }
203                         if (orig_prog == SWAPCTL) {
204                                 printf("%s: %sing %s as swap device\n",
205                                     getprogname(),
206                                     (which_prog == SWAPOFF) ? "remov" : "add",
207                                     swfile);
208                         }
209                 }
210         } else {
211                 if (lflag || sflag)
212                         swaplist(lflag, sflag, hflag);
213                 else 
214                         usage();
215         }
216         exit(ret);
217 }
218
219 static const char *
220 swap_on_off(char *name, int doingall, char *mntops)
221 {
222         char base[PATH_MAX];
223
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));
232
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));
237
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));
241
242         /* Swap on special file. */
243         return (swap_on_off_sfile(name, doingall));
244 }
245
246 static const char *
247 swap_on_off_gbde(char *name, int doingall)
248 {
249         const char *ret;
250         char pass[64 * 2 + 1], bpass[64];
251         char *dname, *p;
252         int i, fd, error;
253
254         dname = strdup(name);
255         p = strrchr(dname, '.');
256         if (p == NULL) {
257                 warnx("%s: Malformed device name", name);
258                 return (NULL);
259         }
260         *p = '\0';
261
262         fd = -1;
263         switch (which_prog) {
264         case SWAPON:
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';
269
270                 error = run_cmd(&fd, "%s init %s -P %s", _PATH_GBDE,
271                     dname, pass);
272                 if (error) {
273                         /* bde device found.  Ignore it. */
274                         close(fd);
275                         if (!qflag)
276                                 warnx("%s: Device already in use", name);
277                         return (NULL);
278                 }
279                 close(fd);
280                 error = run_cmd(&fd, "%s attach %s -p %s", _PATH_GBDE,
281                     dname, pass);
282                 if (error) {
283                         close(fd);
284                         warnx("gbde (attach) error: %s", name);
285                         return (NULL);
286                 }
287                 break;
288         case SWAPOFF:
289                 break;
290         default:
291                 return (NULL);
292                 break;
293         }
294         if (fd != -1)
295                 close(fd);
296         ret = swap_on_off_sfile(name, doingall);
297
298         fd = -1;
299         switch (which_prog) {
300         case SWAPOFF:
301                 error = run_cmd(&fd, "%s detach %s", _PATH_GBDE, dname);
302                 if (error) {
303                         /* bde device not found.  Ignore it. */
304                         if (!qflag)
305                                 warnx("%s: Device not found", dname);
306                         return (NULL);
307                 }
308                 break;
309         default:
310                 return (NULL);
311                 break;
312         }
313
314         if (fd != -1)
315                 close(fd);
316         return (ret);
317 }
318
319 static const char *
320 swap_on_off_geli(char *name, char *mntops, int doingall)
321 {
322         const char *ops, *aalgo, *ealgo, *keylen_str, *sectorsize_str;
323         char *dname, *p;
324         char args[4096];
325         struct stat sb;
326         int fd, error, keylen, sectorsize;
327         u_long ul;
328
329         dname = strdup(name);
330         p = strrchr(dname, '.');
331         if (p == NULL) {
332                 warnx("%s: Malformed device name", name);
333                 return (NULL);
334         }
335         *p = '\0';
336
337         ops = strdup(mntops);
338
339         /* Default parameters for geli(8). */
340         aalgo = "hmac/sha256";
341         ealgo = "aes";
342         keylen = 256;
343         sectorsize = 4096;
344
345         if ((p = strstr(ops, "aalgo=")) != NULL) {
346                 aalgo = p + sizeof("aalgo=") - 1;
347                 p = strchr(aalgo, ',');
348                 if (p != NULL)
349                         *p = '\0';
350         }
351         if ((p = strstr(ops, "ealgo=")) != NULL) {
352                 ealgo = p + sizeof("ealgo=") - 1;
353                 p = strchr(ealgo, ',');
354                 if (p != NULL)
355                         *p = '\0';
356         }
357         if ((p = strstr(ops, "keylen=")) != NULL) {
358                 keylen_str = p + sizeof("keylen=") - 1;
359                 p = strchr(keylen_str, ',');
360                 if (p != NULL)
361                         *p = '\0';
362                 errno = 0;
363                 ul = strtoul(keylen_str, &p, 10);
364                 if (errno == 0) {
365                         if (*p != '\0' || ul > INT_MAX)
366                                 errno = EINVAL;
367                 }
368                 if (errno) {
369                         warn("Invalid keylen: %s", keylen_str);
370                         return (NULL);
371                 }
372                 keylen = (int)ul;
373         }
374         if ((p = strstr(ops, "sectorsize=")) != NULL) {
375                 sectorsize_str = p + sizeof("sectorsize=") - 1;
376                 p = strchr(sectorsize_str, ',');
377                 if (p != NULL)
378                         *p = '\0';
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", sectorsize_str);
387                         return (NULL);
388                 }
389                 sectorsize = (int)ul;
390         }
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';
394         free((void *)ops);
395
396         fd = -1;
397         switch (which_prog) {
398         case SWAPON:
399                 error = run_cmd(&fd, "%s onetime %s %s", _PATH_GELI, args,
400                     dname);
401                 if (error) {
402                         /* eli device found.  Ignore it. */
403                         close(fd);
404                         if (!qflag)
405                                 warnx("%s: Device already in use "
406                                     "or invalid parameters", name);
407                         return (NULL);
408                 }
409                 break;
410         case SWAPOFF:
411                 if (stat(name, &sb) == -1 && errno == ENOENT) {
412                         if (!qflag)
413                                 warnx("%s: Device not found", name);
414                         return (NULL);
415                 }
416                 break;
417         default:
418                 return (NULL);
419                 break;
420         }
421         if (fd != -1)
422                 close(fd);
423
424         return (swap_on_off_sfile(name, doingall));
425 }
426
427 static const char *
428 swap_on_off_md(char *name, char *mntops, int doingall)
429 {
430         FILE *sfd;
431         int fd, mdunit, error;
432         const char *ret;
433         char mdpath[PATH_MAX], linebuf[PATH_MAX];
434         char *p, *vnodefile;
435         size_t linelen;
436         u_long ul;
437
438         fd = -1;
439         sfd = NULL;
440         if (strlen(name) == (sizeof(MD_NAME) - 1))
441                 mdunit = -1;
442         else {
443                 errno = 0;
444                 ul = strtoul(name + 2, &p, 10);
445                 if (errno == 0) {
446                         if (*p != '\0' || ul > INT_MAX)
447                                 errno = EINVAL;
448                 }
449                 if (errno) {
450                         warn("Bad device unit: %s", name);
451                         return (NULL);
452                 }
453                 mdunit = (int)ul;
454         }
455
456         vnodefile = NULL;
457         if ((p = strstr(mntops, "file=")) != NULL) {
458                 vnodefile = strdup(p + sizeof("file=") - 1);
459                 p = strchr(vnodefile, ',');
460                 if (p != NULL)
461                         *p = '\0';
462         }
463         if (vnodefile == NULL) {
464                 warnx("file option not found for %s", name);
465                 return (NULL);
466         }
467
468         switch (which_prog) {
469         case SWAPON:
470                 if (mdunit == -1) {
471                         error = run_cmd(&fd, "%s -l -n -f %s",
472                             _PATH_MDCONFIG, vnodefile);
473                         if (error == 0) {
474                                 /* md device found.  Ignore it. */
475                                 close(fd);
476                                 if (!qflag)
477                                         warnx("%s: Device already in use",
478                                             vnodefile);
479                                 return (NULL);
480                         }
481                         error = run_cmd(&fd, "%s -a -t vnode -n -f %s",
482                             _PATH_MDCONFIG, vnodefile);
483                         if (error) {
484                                 warnx("mdconfig (attach) error: file=%s",
485                                     vnodefile);
486                                 return (NULL);
487                         }
488                         sfd = fdopen(fd, "r");
489                         if (sfd == NULL) {
490                                 warn("mdconfig (attach) fdopen error");
491                                 ret = NULL;
492                                 goto err;
493                         }
494                         p = fgetln(sfd, &linelen);
495                         if (p == NULL &&
496                             (linelen < 2 || linelen > sizeof(linebuf))) {
497                                 warn("mdconfig (attach) unexpected output");
498                                 ret = NULL;
499                                 goto err;
500                         }
501                         strncpy(linebuf, p, linelen);
502                         linebuf[linelen - 1] = '\0';
503                         errno = 0;
504                         ul = strtoul(linebuf, &p, 10);
505                         if (errno == 0) {
506                                 if (*p != '\0' || ul > INT_MAX)
507                                         errno = EINVAL;
508                         }
509                         if (errno) {
510                                 warn("mdconfig (attach) unexpected output: %s",
511                                     linebuf);
512                                 ret = NULL;
513                                 goto err;
514                         }
515                         mdunit = (int)ul;
516                 } else {
517                         error = run_cmd(&fd, "%s -l -n -f %s -u %d",
518                             _PATH_MDCONFIG, vnodefile, mdunit);
519                         if (error == 0) {
520                                 /* md device found.  Ignore it. */
521                                 close(fd);
522                                 if (!qflag)
523                                         warnx("md%d on %s: Device already "
524                                             "in use", mdunit, vnodefile);
525                                 return (NULL);
526                         }
527                         error = run_cmd(NULL, "%s -a -t vnode -u %d -f %s",
528                             _PATH_MDCONFIG, mdunit, vnodefile);
529                         if (error) {
530                                 warnx("mdconfig (attach) error: "
531                                     "md%d on file=%s", mdunit, vnodefile);
532                                 return (NULL);
533                         }
534                 }
535                 break;
536         case SWAPOFF:
537                 if (mdunit == -1) {
538                         error = run_cmd(&fd, "%s -l -n -f %s",
539                             _PATH_MDCONFIG, vnodefile);
540                         if (error) {
541                                 /* md device not found.  Ignore it. */
542                                 close(fd);
543                                 if (!qflag)
544                                         warnx("md on %s: Device not found",
545                                             vnodefile);
546                                 return (NULL);
547                         }
548                         sfd = fdopen(fd, "r");
549                         if (sfd == NULL) {
550                                 warn("mdconfig (list) fdopen error");
551                                 ret = NULL;
552                                 goto err;
553                         }
554                         p = fgetln(sfd, &linelen);
555                         if (p == NULL &&
556                             (linelen < 2 || linelen > sizeof(linebuf) - 1)) {
557                                 warn("mdconfig (list) unexpected output");
558                                 ret = NULL;
559                                 goto err;
560                         }
561                         strncpy(linebuf, p, linelen);
562                         linebuf[linelen - 1] = '\0';
563                         p = strchr(linebuf, ' ');
564                         if (p != NULL)
565                                 *p = '\0';
566                         errno = 0;
567                         ul = strtoul(linebuf, &p, 10);
568                         if (errno == 0) {
569                                 if (*p != '\0' || ul > INT_MAX)
570                                         errno = EINVAL;
571                         }
572                         if (errno) {
573                                 warn("mdconfig (list) unexpected output: %s",
574                                     linebuf);
575                                 ret = NULL;
576                                 goto err;
577                         }
578                         mdunit = (int)ul;
579                 } else {
580                         error = run_cmd(&fd, "%s -l -n -f %s -u %d",
581                             _PATH_MDCONFIG, vnodefile, mdunit);
582                         if (error) {
583                                 /* md device not found.  Ignore it. */
584                                 close(fd);
585                                 if (!qflag)
586                                         warnx("md%d on %s: Device not found",
587                                             mdunit, vnodefile);
588                                 return (NULL);
589                         }
590                 }
591                 break;
592         default:
593                 return (NULL);
594         }
595         snprintf(mdpath, sizeof(mdpath), "%s%s%d", _PATH_DEV,
596             MD_NAME, mdunit);
597         mdpath[sizeof(mdpath) - 1] = '\0';
598         ret = swap_on_off_sfile(mdpath, doingall);
599
600         switch (which_prog) {
601         case SWAPOFF:
602                 if (ret != NULL) {
603                         error = run_cmd(NULL, "%s -d -u %d",
604                             _PATH_MDCONFIG, mdunit);
605                         if (error)
606                                 warn("mdconfig (detach) detach failed: %s%s%d",
607                                     _PATH_DEV, MD_NAME, mdunit);
608                 }
609                 break;
610         default:
611                 break;
612         }
613 err:
614         if (sfd != NULL)
615                 fclose(sfd);
616         if (fd != -1)
617                 close(fd);
618         return (ret);
619 }
620
621 static int
622 run_cmd(int *ofd, const char *cmdline, ...)
623 {
624         va_list ap;
625         char **argv, **argvp, *cmd, *p;
626         int argc, pid, status, rv;
627         int pfd[2], nfd, dup2dn;
628
629         va_start(ap, cmdline);
630         rv = vasprintf(&cmd, cmdline, ap);
631         if (rv == -1) {
632                 warn("%s", __func__);
633                 return (rv);
634         }
635         va_end(ap);
636
637         for (argc = 1, p = cmd; (p = strchr(p, ' ')) != NULL; p++)
638                 argc++;
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])) {
642                         *argvp = NULL;
643                         break;
644                 }
645         /* The argv array ends up NULL-terminated here. */
646 #if 0
647         {
648                 int i;
649
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");
655         }
656 #endif
657         dup2dn = 1;
658         if (ofd != NULL) {
659                 if (pipe(&pfd[0]) == -1) {
660                         warn("%s: pipe", __func__);
661                         return (-1);
662                 }
663                 *ofd = pfd[0];
664                 dup2dn = 0;
665         }
666         pid = fork();
667         switch (pid) {
668         case 0:
669                 /* Child process. */
670                 if (ofd != NULL)
671                         if (dup2(pfd[1], STDOUT_FILENO) < 0)
672                                 err(1, "dup2 in %s", __func__);
673                 nfd = open(_PATH_DEVNULL, O_RDWR);
674                 if (nfd == -1)
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]);
684                 _exit(-1);
685         case -1:
686                 err(1, "%s: fork", __func__);
687         }
688         free(cmd);
689         free(argv);
690         while (waitpid(pid, &status, 0) != pid)
691                 ;
692         return (WEXITSTATUS(status));
693 }
694
695 static const char *
696 swap_on_off_sfile(char *name, int doingall)
697 {
698         int error;
699
700         switch (which_prog) {
701         case SWAPON:
702                 error = swapon(name);
703                 break;
704         case SWAPOFF:
705                 error = swapoff(name);
706                 break;
707         default:
708                 error = 0;
709                 break;
710         }
711         if (error == -1) {
712                 switch (errno) {
713                 case EBUSY:
714                         if (!doingall)
715                                 warnx("%s: Device already in use", name);
716                         break;
717                 case EINVAL:
718                         if (which_prog == SWAPON)
719                                 warnx("%s: NSWAPDEV limit reached", name);
720                         else if (!doingall)
721                                 warn("%s", name);
722                         break;
723                 default:
724                         warn("%s", name);
725                         break;
726                 }
727                 return (NULL);
728         }
729         return (name);
730 }
731
732 static void
733 usage(void)
734 {
735         fprintf(stderr, "usage: %s ", getprogname());
736         switch(orig_prog) {
737         case SWAPON:
738         case SWAPOFF:
739             fprintf(stderr, "[-F fstab] -aLq | file ...\n");
740             break;
741         case SWAPCTL:
742             fprintf(stderr, "[-AghklmsU] [-a file ... | -d file ...]\n");
743             break;
744         }
745         exit(1);
746 }
747
748 static void
749 sizetobuf(char *buf, size_t bufsize, int hflag, long long val, int hlen,
750     long blocksize)
751 {
752
753         if (hflag == 'H') {
754                 char tmp[16];
755
756                 humanize_number(tmp, 5, (int64_t)val, "", HN_AUTOSCALE,
757                     HN_B | HN_NOSPACE | HN_DECIMAL);
758                 snprintf(buf, bufsize, "%*s", hlen, tmp);
759         } else {
760                 snprintf(buf, bufsize, "%*lld", hlen, val / blocksize);
761         }
762 }
763
764 static void
765 swaplist(int lflag, int sflag, int hflag)
766 {
767         size_t mibsize, size;
768         struct xswdev xsw;
769         int hlen, mib[16], n, pagesize;
770         long blocksize;
771         long long total = 0;
772         long long used = 0;
773         long long tmp_total;
774         long long tmp_used;
775         char buf[32];
776         
777         pagesize = getpagesize();
778         switch(hflag) {
779         case 'G':
780             blocksize = 1024 * 1024 * 1024;
781             strlcpy(buf, "1GB-blocks", sizeof(buf));
782             hlen = 10;
783             break;
784         case 'H':
785             blocksize = -1;
786             strlcpy(buf, "Bytes", sizeof(buf));
787             hlen = 10;
788             break;
789         case 'K':
790             blocksize = 1024;
791             strlcpy(buf, "1kB-blocks", sizeof(buf));
792             hlen = 10;
793             break;
794         case 'M':
795             blocksize = 1024 * 1024;
796             strlcpy(buf, "1MB-blocks", sizeof(buf));
797             hlen = 10;
798             break;
799         default:
800             getbsize(&hlen, &blocksize);
801             snprintf(buf, sizeof(buf), "%ld-blocks", blocksize);
802             break;
803         }
804         
805         mibsize = sizeof mib / sizeof mib[0];
806         if (sysctlnametomib("vm.swap_info", mib, &mibsize) == -1)
807                 err(1, "sysctlnametomib()");
808         
809         if (lflag) {
810                 printf("%-13s %*s %*s\n",
811                     "Device:", 
812                     hlen, buf,
813                     hlen, "Used:");
814         }
815         
816         for (n = 0; ; ++n) {
817                 mib[mibsize] = n;
818                 size = sizeof xsw;
819                 if (sysctl(mib, mibsize + 1, &xsw, &size, NULL, 0) == -1)
820                         break;
821                 if (xsw.xsw_version != XSWDEV_VERSION)
822                         errx(1, "xswdev version mismatch");
823                 
824                 tmp_total = (long long)xsw.xsw_nblks * pagesize;
825                 tmp_used  = (long long)xsw.xsw_used * pagesize;
826                 total += tmp_total;
827                 used  += tmp_used;
828                 if (lflag) {
829                         sizetobuf(buf, sizeof(buf), hflag, tmp_total, hlen,
830                             blocksize);
831                         printf("/dev/%-8s %s ", devname(xsw.xsw_dev, S_IFCHR),
832                             buf);
833                         sizetobuf(buf, sizeof(buf), hflag, tmp_used, hlen,
834                             blocksize);
835                         printf("%s\n", buf);
836                 }
837         }
838         if (errno != ENOENT)
839                 err(1, "sysctl()");
840         
841         if (sflag) {
842                 sizetobuf(buf, sizeof(buf), hflag, total, hlen, blocksize);
843                 printf("Total:        %s ", buf);
844                 sizetobuf(buf, sizeof(buf), hflag, used, hlen, blocksize);
845                 printf("%s\n", buf);
846         }
847 }
848