summaryrefslogtreecommitdiff
path: root/usr/src/lib/libdevinfo/devinfo_dim.c
blob: 141f5a95140e5ef7d59e5683bd250330a80b0172 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include "libdevinfo.h"
#include <strings.h>
#include <sys/modctl.h>

/*
 *
 * This file contains interfaces to translate <driver><instance><minorname>
 * information into /devices and /dev paths.  It does this using interfaces to
 * the kernel instance tree so that it can provide translations for devices
 * which are no longer present. An example consumer of these interfaces is
 * iostat(1M) - which shows, in its first iteration, activity since reboot.
 * With persistant kstats, a device which was busy a long time ago can still
 * have a decaying presence in iostat output, and that output, when '-n' is
 * used, should show the public name.
 */

typedef	struct {
	di_devlink_handle_t	i_devlink_hdl;
} *idim_t;

di_dim_t
di_dim_init()
{
	idim_t		idim;

	if ((idim = (idim_t)malloc(sizeof (*idim))) == NULL)
		return (NULL);
	idim->i_devlink_hdl = di_devlink_init(NULL, 0);
	if (idim->i_devlink_hdl == NULL) {
		free(idim);
		return (NULL);
	}
	return ((di_dim_t)idim);
}

void
di_dim_fini(di_dim_t dim)
{
	idim_t	idim = (idim_t)dim;

	if (idim->i_devlink_hdl) {
		(void) di_devlink_fini(&idim->i_devlink_hdl);
	}
	free(idim);
}

/*ARGSUSED*/
char *
di_dim_path_devices(di_dim_t dim, char *drv_name, int instance,
    char *minor_name)
{
	major_t	major;
	int	len;
	int	mlen;
	char	*devices;

	/* convert drv_name to major_t */
	if (modctl(MODGETMAJBIND, drv_name, strlen(drv_name) + 1, &major) < 0)
		return (NULL);

	/* find the length of the devices path given major,instance */
	if (modctl(MODGETDEVFSPATH_MI_LEN, major, instance, &len) != 0)
		return (NULL);

	/*
	 * MODGETDEVFSPATH_MI_LEN result includes '\0' termination, but we
	 * may need to add space for ":<minor_name>"
	 */
	if (minor_name)
		mlen = len + 1 + strlen(minor_name);
	else
		mlen = len;
	if ((devices = (char *)malloc(mlen)) == NULL)
		return (NULL);

	if (modctl(MODGETDEVFSPATH_MI, major, instance, len, devices) != 0) {
		free(devices);
		return (NULL);
	}

	if (minor_name) {
		/* add ':<minot_name>' to the end of /devices path */
		(void) strcat(devices, ":");
		(void) strcat(devices, minor_name);
	}
	return (devices);
}

/* di_dim_path_dev di_devlink callback */
static int
di_dim_path_dev_callback(di_devlink_t dl, void *arg)
{
	char		**devp = (char **)arg;
	char		*devpath = (char *)di_devlink_path(dl);

	if (devpath)
		*devp = strdup(devpath);
	return (DI_WALK_TERMINATE);
}

char *
di_dim_path_dev(di_dim_t dim, char *drv_name, int instance, char *minor_name)
{
	idim_t	idim = (idim_t)dim;
	char	*devices;
	char	*dev = NULL;

	/* we must have a minor_name to resolve to a public name */
	if (minor_name == NULL)
		return (NULL);

	/* convert <driver><instance><minor_name> to /devices path */
	devices = di_dim_path_devices(dim, drv_name, instance, minor_name);
	if (devices == NULL)
		return (NULL);

	/* convert /devices path to /dev path */
	(void) di_devlink_walk(idim->i_devlink_hdl, NULL,
	    devices, DI_PRIMARY_LINK, (void *)&dev, di_dim_path_dev_callback);
	free(devices);
	return (dev);
}