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.
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.
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]
22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
28 #include "libdevinfo.h"
30 #include <sys/modctl.h>
34 * This file contains interfaces to translate <driver><instance><minorname>
35 * information into /devices and /dev paths. It does this using interfaces to
36 * the kernel instance tree so that it can provide translations for devices
37 * which are no longer present. An example consumer of these interfaces is
38 * iostat(1M) - which shows, in its first iteration, activity since reboot.
39 * With persistant kstats, a device which was busy a long time ago can still
40 * have a decaying presence in iostat output, and that output, when '-n' is
41 * used, should show the public name.
45 di_devlink_handle_t i_devlink_hdl
;
53 if ((idim
= (idim_t
)malloc(sizeof (*idim
))) == NULL
)
55 idim
->i_devlink_hdl
= di_devlink_init(NULL
, 0);
56 if (idim
->i_devlink_hdl
== NULL
) {
60 return ((di_dim_t
)idim
);
64 di_dim_fini(di_dim_t dim
)
66 idim_t idim
= (idim_t
)dim
;
68 if (idim
->i_devlink_hdl
) {
69 (void) di_devlink_fini(&idim
->i_devlink_hdl
);
76 di_dim_path_devices(di_dim_t dim
, char *drv_name
, int instance
,
84 /* convert drv_name to major_t */
85 if (modctl(MODGETMAJBIND
, drv_name
, strlen(drv_name
) + 1, &major
) < 0)
88 /* find the length of the devices path given major,instance */
89 if (modctl(MODGETDEVFSPATH_MI_LEN
, major
, instance
, &len
) != 0)
93 * MODGETDEVFSPATH_MI_LEN result includes '\0' termination, but we
94 * may need to add space for ":<minor_name>"
97 mlen
= len
+ 1 + strlen(minor_name
);
100 if ((devices
= (char *)malloc(mlen
)) == NULL
)
103 if (modctl(MODGETDEVFSPATH_MI
, major
, instance
, len
, devices
) != 0) {
109 /* add ':<minot_name>' to the end of /devices path */
110 (void) strcat(devices
, ":");
111 (void) strcat(devices
, minor_name
);
116 /* di_dim_path_dev di_devlink callback */
118 di_dim_path_dev_callback(di_devlink_t dl
, void *arg
)
120 char **devp
= (char **)arg
;
121 char *devpath
= (char *)di_devlink_path(dl
);
124 *devp
= strdup(devpath
);
125 return (DI_WALK_TERMINATE
);
129 di_dim_path_dev(di_dim_t dim
, char *drv_name
, int instance
, char *minor_name
)
131 idim_t idim
= (idim_t
)dim
;
135 /* we must have a minor_name to resolve to a public name */
136 if (minor_name
== NULL
)
139 /* convert <driver><instance><minor_name> to /devices path */
140 devices
= di_dim_path_devices(dim
, drv_name
, instance
, minor_name
);
144 /* convert /devices path to /dev path */
145 (void) di_devlink_walk(idim
->i_devlink_hdl
, NULL
,
146 devices
, DI_PRIMARY_LINK
, (void *)&dev
, di_dim_path_dev_callback
);