xref: /titanic_41/usr/src/cmd/availdevs/availdevs.c (revision 70025d765b044c6d8594bb965a2247a61e991a99)
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 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include "availdevs.h"
30 #include <libzfs_jni_diskmgt.h>
31 #include <libxml/parser.h>
32 
33 /*
34  * Function prototypes
35  */
36 
37 static void handle_error(const char *, va_list);
38 static int add_disk_to_xml(dmgt_disk_t *, void *);
39 static xmlDocPtr create_doc();
40 int main();
41 
42 /*
43  * Static functions
44  */
45 
46 static void
47 handle_error(const char *fmt, va_list ap)
48 {
49 	(void) vfprintf(stderr, fmt, ap);
50 	(void) fprintf(stderr, "\n");
51 }
52 
53 static int
54 add_disk_to_xml(dmgt_disk_t *dp, void *data)
55 {
56 	int i, n;
57 	char tmp[64];
58 	xmlNodePtr available = *((xmlNodePtr *)data);
59 
60 	xmlNodePtr disk = xmlNewChild(
61 	    available, NULL, (xmlChar *)ELEMENT_DISK, NULL);
62 	xmlSetProp(disk,
63 	    (xmlChar *)ATTR_DISK_NAME, (xmlChar *)dp->name);
64 	n = snprintf(tmp, sizeof (tmp) - 1, "%llu", dp->size);
65 	tmp[n] = '\0';
66 	xmlSetProp(disk, (xmlChar *)ATTR_DISK_SIZE, (xmlChar *)tmp);
67 
68 	if (dp->aliases != NULL) {
69 		for (i = 0; dp->aliases[i] != NULL; i++) {
70 			xmlNodePtr alias = xmlNewChild(
71 			    disk, NULL, (xmlChar *)ELEMENT_ALIAS, NULL);
72 			xmlSetProp(alias,
73 			    (xmlChar *)ATTR_ALIAS_NAME,
74 			    (xmlChar *)dp->aliases[i]);
75 		}
76 	}
77 
78 	if (dp->slices != NULL) {
79 		for (i = 0; dp->slices[i] != NULL; i++) {
80 			dmgt_slice_t *sp = dp->slices[i];
81 			xmlNodePtr slice = xmlNewChild(
82 			    disk, NULL, (xmlChar *)ELEMENT_SLICE, NULL);
83 			xmlSetProp(slice,
84 			    (xmlChar *)ATTR_SLICE_NAME, (xmlChar *)sp->name);
85 
86 			n = snprintf(tmp, sizeof (tmp) - 1, "%llu", sp->size);
87 			tmp[n] = '\0';
88 			xmlSetProp(slice, (xmlChar *)ATTR_SLICE_SIZE,
89 			    (xmlChar *)tmp);
90 
91 			n = snprintf(tmp, sizeof (tmp) - 1, "%llu", sp->start);
92 			tmp[n] = '\0';
93 			xmlSetProp(slice, (xmlChar *)ATTR_SLICE_START,
94 			    (xmlChar *)tmp);
95 
96 			if (sp->used_name != NULL) {
97 				xmlSetProp(slice,
98 				    (xmlChar *)ATTR_SLICE_USED_NAME,
99 				    (xmlChar *)sp->used_name);
100 			}
101 
102 			if (sp->used_by != NULL) {
103 				xmlSetProp(slice, (xmlChar *)ATTR_SLICE_USED_BY,
104 				    (xmlChar *)sp->used_by);
105 			}
106 		}
107 	}
108 
109 	return (0);
110 }
111 
112 static xmlDocPtr
113 create_doc(void)
114 {
115 	/* Create the XML document */
116 	xmlDocPtr doc = xmlNewDoc((xmlChar *)"1.0");
117 
118 	/* Create the root node */
119 	xmlNodePtr root = xmlNewDocNode(
120 	    doc, NULL, (xmlChar *)ELEMENT_ROOT, NULL);
121 	xmlAddChild((xmlNodePtr) doc, (xmlNodePtr)root);
122 
123 	/* Create the available node */
124 	xmlNewChild(root, NULL, (xmlChar *)ELEMENT_AVAILABLE, NULL);
125 
126 	return (doc);
127 }
128 
129 /*
130  * Main entry to availdisks.
131  *
132  * @return      0 on successful exit, non-zero otherwise
133  */
134 int
135 main(void)
136 {
137 	int error;
138 	xmlDocPtr doc;
139 	xmlNodePtr root;
140 	xmlNodePtr available;
141 
142 	/* diskmgt.o error handler */
143 	dmgt_set_error_handler(handle_error);
144 
145 	doc = create_doc();
146 	root = xmlDocGetRootElement(doc);
147 	available = xmlGetLastChild(root);
148 
149 	error = dmgt_avail_disk_iter(add_disk_to_xml, &available);
150 	if (!error) {
151 		/* Print out XML */
152 		xmlDocFormatDump(stdout, doc, 1);
153 	}
154 
155 	xmlFreeDoc(doc);
156 
157 	return (error != 0);
158 }
159