xref: /titanic_50/usr/src/cmd/sysdef/sdevinfo.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1990 - 1997, Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate  */
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate #pragma	ident	"%Z%%M%	%I%	%E% SMI"
27*7c478bd9Sstevel@tonic-gate 
28*7c478bd9Sstevel@tonic-gate /*
29*7c478bd9Sstevel@tonic-gate  * For machines that support the openprom, fetch and print the list
30*7c478bd9Sstevel@tonic-gate  * of devices that the kernel has fetched from the prom or conjured up.
31*7c478bd9Sstevel@tonic-gate  */
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include <stdio.h>
34*7c478bd9Sstevel@tonic-gate #include <string.h>
35*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
36*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
37*7c478bd9Sstevel@tonic-gate #include <stdarg.h>
38*7c478bd9Sstevel@tonic-gate #include <libdevinfo.h>
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate static char *progname = "sysdef";
41*7c478bd9Sstevel@tonic-gate extern int devflag;		/* SunOS4.x devinfo compatible output */
42*7c478bd9Sstevel@tonic-gate extern int drvname_flag;	/* print out the driver name too */
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate static int _error(char *opt_noperror, ...);
45*7c478bd9Sstevel@tonic-gate static int dump_node(di_node_t node, void *arg);
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate void
sysdef_devinfo(void)48*7c478bd9Sstevel@tonic-gate sysdef_devinfo(void)
49*7c478bd9Sstevel@tonic-gate {
50*7c478bd9Sstevel@tonic-gate 	di_node_t root_node;
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate 	/* take a snapshot of kernel devinfo tree */
53*7c478bd9Sstevel@tonic-gate 	if ((root_node = di_init("/", DINFOSUBTREE)) == DI_NODE_NIL) {
54*7c478bd9Sstevel@tonic-gate 		exit(_error("di_init() failed."));
55*7c478bd9Sstevel@tonic-gate 	}
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate 	/*
58*7c478bd9Sstevel@tonic-gate 	 * ...and call di_walk_node to report it out...
59*7c478bd9Sstevel@tonic-gate 	 */
60*7c478bd9Sstevel@tonic-gate 	di_walk_node(root_node, DI_WALK_CLDFIRST, NULL, dump_node);
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate 	di_fini(root_node);
63*7c478bd9Sstevel@tonic-gate }
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate /*
66*7c478bd9Sstevel@tonic-gate  * print out information about this node
67*7c478bd9Sstevel@tonic-gate  */
68*7c478bd9Sstevel@tonic-gate static int
dump_node(di_node_t node,void * arg)69*7c478bd9Sstevel@tonic-gate dump_node(di_node_t node, void *arg)
70*7c478bd9Sstevel@tonic-gate {
71*7c478bd9Sstevel@tonic-gate 	int i;
72*7c478bd9Sstevel@tonic-gate 	char *driver_name;
73*7c478bd9Sstevel@tonic-gate 	di_node_t tmp;
74*7c478bd9Sstevel@tonic-gate 	int indent_level = 0;
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate 	/* find indent level */
77*7c478bd9Sstevel@tonic-gate 	tmp = node;
78*7c478bd9Sstevel@tonic-gate 	while ((tmp = di_parent_node(tmp)) != DI_NODE_NIL)
79*7c478bd9Sstevel@tonic-gate 		indent_level++;
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate 	/* we would start at 0, except that we skip the root node */
82*7c478bd9Sstevel@tonic-gate 	if (!devflag)
83*7c478bd9Sstevel@tonic-gate 		indent_level--;
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < indent_level; i++)
86*7c478bd9Sstevel@tonic-gate 		(void) putchar('\t');
87*7c478bd9Sstevel@tonic-gate 
88*7c478bd9Sstevel@tonic-gate 	if (indent_level >= 0) {
89*7c478bd9Sstevel@tonic-gate 		if (devflag) {
90*7c478bd9Sstevel@tonic-gate 			/*
91*7c478bd9Sstevel@tonic-gate 			 * 4.x devinfo(8) compatible..
92*7c478bd9Sstevel@tonic-gate 			 */
93*7c478bd9Sstevel@tonic-gate 			(void) printf("Node '%s', unit #%d",
94*7c478bd9Sstevel@tonic-gate 				di_node_name(node),
95*7c478bd9Sstevel@tonic-gate 				di_instance(node));
96*7c478bd9Sstevel@tonic-gate 			if (drvname_flag) {
97*7c478bd9Sstevel@tonic-gate 				if (driver_name = di_driver_name(node)) {
98*7c478bd9Sstevel@tonic-gate 					(void) printf(" (driver name: %s)",
99*7c478bd9Sstevel@tonic-gate 					    driver_name);
100*7c478bd9Sstevel@tonic-gate 				}
101*7c478bd9Sstevel@tonic-gate 			} else if (di_state(node) & DI_DRIVER_DETACHED) {
102*7c478bd9Sstevel@tonic-gate 				(void) printf(" (no driver)");
103*7c478bd9Sstevel@tonic-gate 			}
104*7c478bd9Sstevel@tonic-gate 		} else {
105*7c478bd9Sstevel@tonic-gate 			/*
106*7c478bd9Sstevel@tonic-gate 			 * prtconf(1M) compatible..
107*7c478bd9Sstevel@tonic-gate 			 */
108*7c478bd9Sstevel@tonic-gate 			(void) printf("%s", di_node_name(node));
109*7c478bd9Sstevel@tonic-gate 			if (di_instance(node) >= 0)
110*7c478bd9Sstevel@tonic-gate 				(void) printf(", instance #%d",
111*7c478bd9Sstevel@tonic-gate 				    di_instance(node));
112*7c478bd9Sstevel@tonic-gate 			if (drvname_flag) {
113*7c478bd9Sstevel@tonic-gate 				if (driver_name = di_driver_name(node)) {
114*7c478bd9Sstevel@tonic-gate 					(void) printf(" (driver name: %s)",
115*7c478bd9Sstevel@tonic-gate 					    driver_name);
116*7c478bd9Sstevel@tonic-gate 				}
117*7c478bd9Sstevel@tonic-gate 			} else if (di_state(node) & DI_DRIVER_DETACHED) {
118*7c478bd9Sstevel@tonic-gate 				(void) printf(" (driver not attached)");
119*7c478bd9Sstevel@tonic-gate 			}
120*7c478bd9Sstevel@tonic-gate 		}
121*7c478bd9Sstevel@tonic-gate 		(void) printf("\n");
122*7c478bd9Sstevel@tonic-gate 	}
123*7c478bd9Sstevel@tonic-gate 	return (DI_WALK_CONTINUE);
124*7c478bd9Sstevel@tonic-gate }
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate /*
127*7c478bd9Sstevel@tonic-gate  * utility routines
128*7c478bd9Sstevel@tonic-gate  */
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate /* _error([no_perror, ] fmt [, arg ...]) */
131*7c478bd9Sstevel@tonic-gate static int
_error(char * opt_noperror,...)132*7c478bd9Sstevel@tonic-gate _error(char *opt_noperror, ...)
133*7c478bd9Sstevel@tonic-gate {
134*7c478bd9Sstevel@tonic-gate 	int saved_errno;
135*7c478bd9Sstevel@tonic-gate 	va_list ap;
136*7c478bd9Sstevel@tonic-gate 	int no_perror = 0;
137*7c478bd9Sstevel@tonic-gate 	char *fmt;
138*7c478bd9Sstevel@tonic-gate 	extern int errno, _doprnt();
139*7c478bd9Sstevel@tonic-gate 
140*7c478bd9Sstevel@tonic-gate 	saved_errno = errno;
141*7c478bd9Sstevel@tonic-gate 
142*7c478bd9Sstevel@tonic-gate 	if (progname)
143*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: ", progname);
144*7c478bd9Sstevel@tonic-gate 
145*7c478bd9Sstevel@tonic-gate 	va_start(ap, opt_noperror);
146*7c478bd9Sstevel@tonic-gate 	if (opt_noperror == NULL) {
147*7c478bd9Sstevel@tonic-gate 		no_perror = 1;
148*7c478bd9Sstevel@tonic-gate 		fmt = va_arg(ap, char *);
149*7c478bd9Sstevel@tonic-gate 	} else
150*7c478bd9Sstevel@tonic-gate 		fmt = opt_noperror;
151*7c478bd9Sstevel@tonic-gate 	(void) _doprnt(fmt, ap, stderr);
152*7c478bd9Sstevel@tonic-gate 	va_end(ap);
153*7c478bd9Sstevel@tonic-gate 
154*7c478bd9Sstevel@tonic-gate 	if (no_perror)
155*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "\n");
156*7c478bd9Sstevel@tonic-gate 	else {
157*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, ": ");
158*7c478bd9Sstevel@tonic-gate 		errno = saved_errno;
159*7c478bd9Sstevel@tonic-gate 		perror("");
160*7c478bd9Sstevel@tonic-gate 	}
161*7c478bd9Sstevel@tonic-gate 
162*7c478bd9Sstevel@tonic-gate 	return (1);
163*7c478bd9Sstevel@tonic-gate }
164