1di_init(3DEVINFO) Device Information Library Functions di_init(3DEVINFO)
2
3
4
6 di_init, di_fini - create and destroy a snapshot of kernel device tree
7
9 cc [ flag... ] file... -ldevinfo [ library... ]
10 #include <libdevinfo.h>
11
12 di_node_t di_init(const char *phys_path, uint_t flags);
13
14
15 void di_fini(di_node_t root);
16
17
19 flags Snapshot content specification. The possible values can be
20 a bitwise OR of at least one of the following:
21
22 DINFOSUBTREE Include subtree.
23
24
25 DINFOPROP Include properties.
26
27
28 DINFOMINOR Include minor node data.
29
30
31 DINFOCPYALL Include all of the above.
32
33
34 DINFOPATH Include multipath path node data.
35
36
37 DINFOLYR Include device layering data.
38
39
40 DINFOCPYONE Include only a single node without proper‐
41 ties, minor nodes, or path nodes.
42
43
44
45 phys_path Physical path of the root device node of the snapshot. See
46 di_devfs_path(3DEVINFO).
47
48
49 root Handle obtained by calling di_init().
50
51
53 The di_init() function creates a snapshot of the kernel device tree and
54 returns a handle of the root device node. The caller specifies the con‐
55 tents of the snapshot by providing flag and phys_path.
56
57
58 The di_fini() function destroys the snapshot of the kernel device tree
59 and frees the associated memory. All handles associated with this
60 snapshot become invalid after the call to di_fini().
61
63 Upon success, di_init() returns a handle. Otherwise, DI_NODE_NIL is
64 returned and errno is set to indicate the error.
65
67 The di_init() function can set errno to any error code that can also be
68 set by open(2), ioctl(2) or mmap(2). The most common error codes
69 include:
70
71 EACCES Insufficient privilege for accessing device configuration
72 data.
73
74
75 ENXIO Either the device named by phys_path is not present in the
76 system, or the devinfo(7D) driver is not installed properly.
77
78
79 EINVAL Either phys_path is incorrectly formed or the flags argument
80 is invalid.
81
82
84 Example 1 Using the libdevinfo Interfaces To Print All Device Tree Node
85 Names
86
87
88 The following is an example using the libdevinfo interfaces to print
89 all device tree device node names:
90
91
92 /*
93 * Code to print all device tree device node names
94 */
95
96 #include <stdio.h>
97 #include <libdevinfo.h>
98
99 int
100 prt_nodename(di_node_t node, void *arg)
101 {
102 printf("%s\n", di_node_name(node));
103 return (DI_WALK_CONTINUE);
104 }
105
106 main()
107 {
108 di_node_t root_node;
109 if((root_node = di_init("/", DINFOSUBTREE)) == DI_NODE_NIL) {
110 fprintf(stderr, "di_init() failed\n");
111 exit(1);
112 }
113 di_walk_node(root_node, DI_WALK_CLDFIRST, NULL, prt_nodename);
114 di_fini(root_node);
115 }
116
117
118 Example 2 Using the libdevinfo Interfaces To Print The Physical Path Of
119 SCSI Disks
120
121
122 The following example uses the libdevinfo interfaces to print the phys‐
123 ical path of SCSI disks:
124
125
126 /*
127 * Code to print physical path of scsi disks
128 */
129
130 #include <stdio.h>
131 #include <libdevinfo.h>
132 #define DISK_DRIVER "sd" /* driver name */
133
134 void
135 prt_diskinfo(di_node_t node)
136 {
137 int instance;
138 char *phys_path;
139
140 /*
141 * If the device node exports no minor nodes,
142 * there is no physical disk.
143 */
144 if (di_minor_next(node, DI_MINOR_NIL) == DI_MINOR_NIL) {
145 return;
146 }
147
148 instance = di_instance(node);
149 phys_path = di_devfs_path(node);
150 printf("%s%d: %s\n", DISK_DRIVER, instance, phys_path);
151 di_devfs_path_free(phys_path);
152 }
153
154 void
155 walk_disknodes(di_node_t node)
156 {
157 node = di_drv_first_node(DISK_DRIVER, node);
158 while (node != DI_NODE_NIL) {
159 prt_diskinfo(node);
160 node = di_drv_next_node(node);
161 }
162 }
163
164 main()
165 {
166 di_node_t root_node;
167 if ((root_node = di_init("/", DINFOCPYALL)) == DI_NODE_NIL) {
168 fprintf(stderr, "di_init() failed\n");
169 exit(1);
170 }
171 walk_disknodes(root_node);
172 di_fini(root_node);
173 }
174
175
177 See attributes(5) for descriptions of the following attributes:
178
179
180
181
182 ┌─────────────────────────────┬─────────────────────────────┐
183 │ ATTRIBUTE TYPE │ ATTRIBUTE VALUE │
184 ├─────────────────────────────┼─────────────────────────────┤
185 │Interface Stability │Committed │
186 ├─────────────────────────────┼─────────────────────────────┤
187 │MT-Level │Safe │
188 └─────────────────────────────┴─────────────────────────────┘
189
191 open(2), ioctl(2), mmap(2), libdevinfo(3LIB), attributes(5)
192
193
194 Writing Device Drivers
195
196
197
198SunOS 5.11 15 May 2008 di_init(3DEVINFO)