xref: /illumos-gate/usr/src/cmd/dumpadm/swap.c (revision fe4627ef755b7c263f91a0e6f07cdca5d7083501)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/param.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #include <stdio.h>
33 
34 #include "utils.h"
35 #include "swap.h"
36 
37 swaptbl_t *
38 swap_list(void)
39 {
40 	swaptbl_t *swt;
41 
42 	int n, i;
43 	char *p;
44 
45 	if ((n = swapctl(SC_GETNSWP, NULL)) == -1) {
46 		warn(gettext("failed to get swap table size"));
47 		return (NULL);
48 	}
49 
50 	swt = malloc(sizeof (int) + n * sizeof (swapent_t) + n * MAXPATHLEN);
51 
52 	if (swt == NULL) {
53 		warn(gettext("failed to allocate swap table"));
54 		return (NULL);
55 	}
56 
57 	swt->swt_n = n;
58 	p = (char *)swt + (sizeof (int) + n * sizeof (swapent_t));
59 
60 	for (i = 0; i < n; i++) {
61 		swt->swt_ent[i].ste_path = p;
62 		p += MAXPATHLEN;
63 	}
64 
65 	if ((n = swapctl(SC_LIST, swt)) == -1) {
66 		warn(gettext("failed to get swap table"));
67 		free(swt);
68 		return (NULL);
69 	}
70 
71 	swt->swt_n = n;	/* Number of entries filled in */
72 	n = 0;		/* Number of valid entries */
73 
74 	/*
75 	 * Shrink the array of swapent_t structures by stripping out
76 	 * all those which are ST_INDEL or ST_DOINGDEL.
77 	 */
78 	for (i = 0; i < swt->swt_n; i++) {
79 		if (!(swt->swt_ent[i].ste_flags & (ST_INDEL | ST_DOINGDEL))) {
80 			/*
81 			 * If i is ahead of the valid count (n), copy the
82 			 * ith entry back to the nth entry so valid entries
83 			 * fill the initial part of swt_ent[].
84 			 */
85 			if (i > n) {
86 				(void) memcpy(&swt->swt_ent[n],
87 				    &swt->swt_ent[i], sizeof (swapent_t));
88 			}
89 
90 			/*
91 			 * If the pathname isn't absolute, assume it begins
92 			 * with /dev as swap(1m) does.
93 			 */
94 			if (swt->swt_ent[n].ste_path[0] != '/') {
95 				char buf[MAXPATHLEN];
96 
97 				(void) snprintf(buf, sizeof (buf), "/dev/%s",
98 				    swt->swt_ent[n].ste_path);
99 				(void) strcpy(swt->swt_ent[n].ste_path, buf);
100 			}
101 
102 			n++;
103 		}
104 	}
105 
106 	swt->swt_n = n;	/* Update swt_n with number of valid entries */
107 	return (swt);
108 }
109