xref: /titanic_51/usr/src/cmd/scsi/sestopo/common/sestopo.c (revision 0219346b07c8d846112a335f1543309c21e3d8da)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 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 <libnvpair.h>
30 #include <stdio.h>
31 #include <unistd.h>
32 #include <scsi/libses.h>
33 
34 static void fatal(int, const char *, ...) __NORETURN;
35 
36 static void
37 fatal(int err, const char *fmt, ...)
38 {
39 	va_list ap;
40 
41 	va_start(ap, fmt);
42 	(void) vfprintf(stderr, fmt, ap);
43 	va_end(ap);
44 
45 	(void) fprintf(stderr, "\n");
46 	(void) fflush(stderr);
47 
48 	_exit(err);
49 }
50 
51 /*ARGSUSED*/
52 static ses_walk_action_t
53 node(ses_node_t *np, void *arg)
54 {
55 	ses_node_type_t type;
56 	uint64_t val;
57 	nvlist_t *props;
58 	char *t;
59 
60 	type = ses_node_type(np);
61 	(void) printf("Node Type: %d\n", type);
62 	if ((props = ses_node_props(np)) == NULL) {
63 		(void) printf("No properties\n");
64 		return (SES_WALK_ACTION_CONTINUE);
65 	}
66 	if (type == SES_NODE_ELEMENT || type == SES_NODE_AGGREGATE) {
67 		(void) nvlist_lookup_uint64(props, SES_PROP_ELEMENT_TYPE, &val);
68 		if (nvlist_lookup_string(props, LIBSES_PROP_ELEMENT_TYPE_NAME,
69 		    &t) != 0)
70 			t = NULL;
71 		(void) printf("Element Type: %s\n", t ? t : "<unknown>");
72 	}
73 	nvlist_print(stdout, props);
74 
75 	return (SES_WALK_ACTION_CONTINUE);
76 }
77 
78 int
79 main(int argc, char *argv[])
80 {
81 	ses_target_t *tp;
82 	ses_snap_t *sp;
83 
84 	if (argc != 2)
85 		fatal(1, "Usage: %s <device>", argv[0]);
86 
87 	if ((tp = ses_open(LIBSES_VERSION, argv[1])) == NULL)
88 		fatal(-1, "failed to open %s: %s", argv[1], ses_errmsg());
89 
90 	sp = ses_snap_hold(tp);
91 
92 	(void) ses_walk(sp, node, NULL);
93 
94 	ses_snap_rele(sp);
95 	ses_close(tp);
96 
97 	return (0);
98 }
99