1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22 /*
23 * Copyright (c) 1998 by Sun Microsystems, Inc.
24 * All rights reserved.
25 */
26
27 #include <sys/param.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31
32 #include "utils.h"
33 #include "swap.h"
34
35 swaptbl_t *
swap_list(void)36 swap_list(void)
37 {
38 swaptbl_t *swt;
39
40 int n, i;
41 char *p;
42
43 if ((n = swapctl(SC_GETNSWP, NULL)) == -1) {
44 warn(gettext("failed to get swap table size"));
45 return (NULL);
46 }
47
48 swt = malloc(sizeof (int) + n * sizeof (swapent_t) + n * MAXPATHLEN);
49
50 if (swt == NULL) {
51 warn(gettext("failed to allocate swap table"));
52 return (NULL);
53 }
54
55 swt->swt_n = n;
56 p = (char *)swt + (sizeof (int) + n * sizeof (swapent_t));
57
58 for (i = 0; i < n; i++) {
59 swt->swt_ent[i].ste_path = p;
60 p += MAXPATHLEN;
61 }
62
63 if ((n = swapctl(SC_LIST, swt)) == -1) {
64 warn(gettext("failed to get swap table"));
65 free(swt);
66 return (NULL);
67 }
68
69 swt->swt_n = n; /* Number of entries filled in */
70 n = 0; /* Number of valid entries */
71
72 /*
73 * Shrink the array of swapent_t structures by stripping out
74 * all those which are ST_INDEL or ST_DOINGDEL.
75 */
76 for (i = 0; i < swt->swt_n; i++) {
77 if (!(swt->swt_ent[i].ste_flags & (ST_INDEL | ST_DOINGDEL))) {
78 /*
79 * If i is ahead of the valid count (n), copy the
80 * ith entry back to the nth entry so valid entries
81 * fill the initial part of swt_ent[].
82 */
83 if (i > n) {
84 (void) memcpy(&swt->swt_ent[n],
85 &swt->swt_ent[i], sizeof (swapent_t));
86 }
87
88 /*
89 * If the pathname isn't absolute, assume it begins
90 * with /dev as swap(8) does.
91 */
92 if (swt->swt_ent[n].ste_path[0] != '/') {
93 char buf[MAXPATHLEN];
94
95 (void) snprintf(buf, sizeof (buf), "/dev/%s",
96 swt->swt_ent[n].ste_path);
97 (void) strcpy(swt->swt_ent[n].ste_path, buf);
98 }
99
100 n++;
101 }
102 }
103
104 swt->swt_n = n; /* Update swt_n with number of valid entries */
105 return (swt);
106 }
107