1 /* $OpenBSD: swaplist.c,v 1.14 2019/12/05 12:46:54 mpi Exp $ */
2 /* $NetBSD: swaplist.c,v 1.8 1998/10/08 10:00:31 mrg Exp $ */
3
4 /*
5 * Copyright (c) 1997 Matthew R. Green
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 * 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 #include <sys/param.h> /* dbtob */
31 #include <sys/types.h>
32 #include <sys/swap.h>
33
34 #include <err.h>
35 #include <stdint.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40
41 #include "swapctl.h"
42
43 #define dbtoqb(b) dbtob((int64_t)(b))
44
45 void
list_swap(int pri,int kflag,int pflag,int dolong)46 list_swap(int pri, int kflag, int pflag, int dolong)
47 {
48 struct swapent *sep, *fsep;
49 long blocksize;
50 char *header;
51 size_t l;
52 int hlen, totalsize, size, totalinuse, inuse, ncounted, pathmax;
53 int rnswap, nswap, i;
54
55 nswap = swapctl(SWAP_NSWAP, 0, 0);
56 if (nswap < 1)
57 errx(1, "no swap devices configured");
58
59 fsep = sep = calloc(nswap, sizeof(*sep));
60 if (sep == NULL)
61 err(1, "calloc");
62 rnswap = swapctl(SWAP_STATS, (void *)sep, nswap);
63 if (rnswap == -1)
64 err(1, "SWAP_STATS");
65 if (nswap != rnswap)
66 warnx("SWAP_STATS different to SWAP_NSWAP (%d != %d)",
67 rnswap, nswap);
68
69 pathmax = 11;
70 if (kflag) {
71 header = "1K-blocks";
72 blocksize = 1024;
73 hlen = strlen(header);
74 } else
75 header = getbsize(&hlen, &blocksize);
76
77 if (dolong) {
78 for (i = rnswap; i-- > 0; sep++)
79 if (pathmax < (l = strlen(sep->se_path)))
80 pathmax = l;
81 sep = fsep;
82 (void)printf("%-*s %*s %8s %8s %8s %s\n",
83 pathmax, "Device", hlen, header,
84 "Used", "Avail", "Capacity", "Priority");
85 }
86 totalsize = totalinuse = ncounted = 0;
87 for (; rnswap-- > 0; sep++) {
88 if (pflag && sep->se_priority != pri)
89 continue;
90 ncounted++;
91 size = sep->se_nblks;
92 inuse = sep->se_inuse;
93 totalsize += size;
94 totalinuse += inuse;
95
96 if (dolong) {
97 (void)printf("%-*s %*ld ", pathmax, sep->se_path, hlen,
98 (long)(dbtoqb(size) / blocksize));
99
100 (void)printf("%8ld %8ld %5.0f%% %d\n",
101 (long)(dbtoqb(inuse) / blocksize),
102 (long)(dbtoqb(size - inuse) / blocksize),
103 (double)inuse / (double)size * 100.0,
104 sep->se_priority);
105 }
106 }
107 if (dolong == 0)
108 printf("total: %ld %*s allocated, %ld used, "
109 "%ld available\n",
110 (long)(dbtoqb(totalsize) / blocksize),
111 hlen, header,
112 (long)(dbtoqb(totalinuse) / blocksize),
113 (long)(dbtoqb(totalsize - totalinuse) / blocksize));
114 else if (ncounted > 1)
115 (void)printf("%-*s %*ld %8ld %8ld %5.0f%%\n", pathmax, "Total",
116 hlen,
117 (long)(dbtoqb(totalsize) / blocksize),
118 (long)(dbtoqb(totalinuse) / blocksize),
119 (long)(dbtoqb(totalsize - totalinuse) / blocksize),
120 (double)(totalinuse) / (double)totalsize * 100.0);
121 free(fsep);
122 }
123