xref: /titanic_51/usr/src/test/zfs-tests/cmd/devname2devid/devname2devid.c (revision d583b39bfb4e2571d3e41097c5c357ffe353ad45)
1*d583b39bSJohn Wren Kennedy /*
2*d583b39bSJohn Wren Kennedy  * CDDL HEADER START
3*d583b39bSJohn Wren Kennedy  *
4*d583b39bSJohn Wren Kennedy  * The contents of this file are subject to the terms of the
5*d583b39bSJohn Wren Kennedy  * Common Development and Distribution License (the "License").
6*d583b39bSJohn Wren Kennedy  * You may not use this file except in compliance with the License.
7*d583b39bSJohn Wren Kennedy  *
8*d583b39bSJohn Wren Kennedy  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*d583b39bSJohn Wren Kennedy  * or http://www.opensolaris.org/os/licensing.
10*d583b39bSJohn Wren Kennedy  * See the License for the specific language governing permissions
11*d583b39bSJohn Wren Kennedy  * and limitations under the License.
12*d583b39bSJohn Wren Kennedy  *
13*d583b39bSJohn Wren Kennedy  * When distributing Covered Code, include this CDDL HEADER in each
14*d583b39bSJohn Wren Kennedy  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*d583b39bSJohn Wren Kennedy  * If applicable, add the following below this CDDL HEADER, with the
16*d583b39bSJohn Wren Kennedy  * fields enclosed by brackets "[]" replaced with your own identifying
17*d583b39bSJohn Wren Kennedy  * information: Portions Copyright [yyyy] [name of copyright owner]
18*d583b39bSJohn Wren Kennedy  *
19*d583b39bSJohn Wren Kennedy  * CDDL HEADER END
20*d583b39bSJohn Wren Kennedy  */
21*d583b39bSJohn Wren Kennedy 
22*d583b39bSJohn Wren Kennedy /*
23*d583b39bSJohn Wren Kennedy  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24*d583b39bSJohn Wren Kennedy  * Use is subject to license terms.
25*d583b39bSJohn Wren Kennedy  */
26*d583b39bSJohn Wren Kennedy 
27*d583b39bSJohn Wren Kennedy #include <sys/types.h>
28*d583b39bSJohn Wren Kennedy #include <sys/stat.h>
29*d583b39bSJohn Wren Kennedy #include <devid.h>
30*d583b39bSJohn Wren Kennedy #include <errno.h>
31*d583b39bSJohn Wren Kennedy #include <stdio.h>
32*d583b39bSJohn Wren Kennedy #include <stdlib.h>
33*d583b39bSJohn Wren Kennedy #include <fcntl.h>
34*d583b39bSJohn Wren Kennedy 
35*d583b39bSJohn Wren Kennedy /*
36*d583b39bSJohn Wren Kennedy  * Usage: devname2devid <devicepath>
37*d583b39bSJohn Wren Kennedy  *
38*d583b39bSJohn Wren Kennedy  * Examples:
39*d583b39bSJohn Wren Kennedy  *	# ./devname2devid /dev/dsk/c1t4d0s0
40*d583b39bSJohn Wren Kennedy  *	devid id1,sd@SSEAGATE_ST318404LSUN18G_3BT2G0Z300002146G4CR/a
41*d583b39bSJohn Wren Kennedy  *	# ./devname2devid /dev/dsk/c1t4d0
42*d583b39bSJohn Wren Kennedy  *	devid id1,sd@SSEAGATE_ST318404LSUN18G_3BT2G0Z300002146G4CR/wd
43*d583b39bSJohn Wren Kennedy  *	# ./devname2devid /dev/dsk/c1t4d0s1
44*d583b39bSJohn Wren Kennedy  *	devid id1,sd@SSEAGATE_ST318404LSUN18G_3BT2G0Z300002146G4CR/b
45*d583b39bSJohn Wren Kennedy  *	#
46*d583b39bSJohn Wren Kennedy  *
47*d583b39bSJohn Wren Kennedy  * This program accepts a disk or disk slice path and prints a
48*d583b39bSJohn Wren Kennedy  * device id.
49*d583b39bSJohn Wren Kennedy  *
50*d583b39bSJohn Wren Kennedy  * Exit values:
51*d583b39bSJohn Wren Kennedy  *	0 - means success
52*d583b39bSJohn Wren Kennedy  *	1 - means failure
53*d583b39bSJohn Wren Kennedy  *
54*d583b39bSJohn Wren Kennedy  */
55*d583b39bSJohn Wren Kennedy int
56*d583b39bSJohn Wren Kennedy main(int argc, char *argv[])
57*d583b39bSJohn Wren Kennedy {
58*d583b39bSJohn Wren Kennedy 	int		fd;
59*d583b39bSJohn Wren Kennedy 	ddi_devid_t	devid;
60*d583b39bSJohn Wren Kennedy 	char		*minor_name, *devidstr, *device;
61*d583b39bSJohn Wren Kennedy #ifdef DEBUG
62*d583b39bSJohn Wren Kennedy 	devid_nmlist_t  *list = NULL;
63*d583b39bSJohn Wren Kennedy 	char		*search_path;
64*d583b39bSJohn Wren Kennedy 	int		i;
65*d583b39bSJohn Wren Kennedy #endif
66*d583b39bSJohn Wren Kennedy 
67*d583b39bSJohn Wren Kennedy 	if (argc == 1) {
68*d583b39bSJohn Wren Kennedy 		(void) printf("%s <devicepath> [search path]\n",
69*d583b39bSJohn Wren Kennedy 		    argv[0]);
70*d583b39bSJohn Wren Kennedy 		exit(1);
71*d583b39bSJohn Wren Kennedy 	}
72*d583b39bSJohn Wren Kennedy 	device = argv[1];
73*d583b39bSJohn Wren Kennedy 
74*d583b39bSJohn Wren Kennedy 	if ((fd = open(device, O_RDONLY|O_NDELAY)) < 0) {
75*d583b39bSJohn Wren Kennedy 		perror(device);
76*d583b39bSJohn Wren Kennedy 		exit(1);
77*d583b39bSJohn Wren Kennedy 	}
78*d583b39bSJohn Wren Kennedy 	if (devid_get(fd, &devid) != 0) {
79*d583b39bSJohn Wren Kennedy 		perror("devid_get");
80*d583b39bSJohn Wren Kennedy 		exit(1);
81*d583b39bSJohn Wren Kennedy 	}
82*d583b39bSJohn Wren Kennedy 	if (devid_get_minor_name(fd, &minor_name) != 0) {
83*d583b39bSJohn Wren Kennedy 		perror("devid_get_minor_name");
84*d583b39bSJohn Wren Kennedy 		exit(1);
85*d583b39bSJohn Wren Kennedy 	}
86*d583b39bSJohn Wren Kennedy 	if ((devidstr = devid_str_encode(devid, minor_name)) == 0) {
87*d583b39bSJohn Wren Kennedy 		perror("devid_str_encode");
88*d583b39bSJohn Wren Kennedy 		exit(1);
89*d583b39bSJohn Wren Kennedy 	}
90*d583b39bSJohn Wren Kennedy 
91*d583b39bSJohn Wren Kennedy 	(void) printf("devid %s\n", devidstr);
92*d583b39bSJohn Wren Kennedy 
93*d583b39bSJohn Wren Kennedy 	devid_str_free(devidstr);
94*d583b39bSJohn Wren Kennedy 
95*d583b39bSJohn Wren Kennedy #ifdef DEBUG
96*d583b39bSJohn Wren Kennedy 	if (argc == 3) {
97*d583b39bSJohn Wren Kennedy 		search_path = argv[2];
98*d583b39bSJohn Wren Kennedy 	} else {
99*d583b39bSJohn Wren Kennedy 		search_path = "/dev/rdsk";
100*d583b39bSJohn Wren Kennedy 	}
101*d583b39bSJohn Wren Kennedy 
102*d583b39bSJohn Wren Kennedy 	if (devid_deviceid_to_nmlist(search_path, devid, DEVID_MINOR_NAME_ALL,
103*d583b39bSJohn Wren Kennedy 	    &list)) {
104*d583b39bSJohn Wren Kennedy 		perror("devid_deviceid_to_nmlist");
105*d583b39bSJohn Wren Kennedy 		exit(1);
106*d583b39bSJohn Wren Kennedy 	}
107*d583b39bSJohn Wren Kennedy 
108*d583b39bSJohn Wren Kennedy 	/* loop through list and process device names and numbers */
109*d583b39bSJohn Wren Kennedy 	for (i = 0; list[i].devname != NULL; i++) {
110*d583b39bSJohn Wren Kennedy 		(void) printf("devname: %s %p\n", list[i].devname, list[i].dev);
111*d583b39bSJohn Wren Kennedy 	}
112*d583b39bSJohn Wren Kennedy 	devid_free_nmlist(list);
113*d583b39bSJohn Wren Kennedy 
114*d583b39bSJohn Wren Kennedy #endif /* DEBUG */
115*d583b39bSJohn Wren Kennedy 
116*d583b39bSJohn Wren Kennedy 	devid_str_free(minor_name);
117*d583b39bSJohn Wren Kennedy 	devid_free(devid);
118*d583b39bSJohn Wren Kennedy 
119*d583b39bSJohn Wren Kennedy 	return (0);
120*d583b39bSJohn Wren Kennedy }
121