xref: /titanic_52/usr/src/test/zfs-tests/cmd/devname2devid/devname2devid.c (revision 2bc987325e3ded1865bff043128661815c4690b9)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <devid.h>
30 #include <errno.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <fcntl.h>
34 
35 /*
36  * Usage: devname2devid <devicepath>
37  *
38  * Examples:
39  *	# ./devname2devid /dev/dsk/c1t4d0s0
40  *	devid id1,sd@SSEAGATE_ST318404LSUN18G_3BT2G0Z300002146G4CR/a
41  *	# ./devname2devid /dev/dsk/c1t4d0
42  *	devid id1,sd@SSEAGATE_ST318404LSUN18G_3BT2G0Z300002146G4CR/wd
43  *	# ./devname2devid /dev/dsk/c1t4d0s1
44  *	devid id1,sd@SSEAGATE_ST318404LSUN18G_3BT2G0Z300002146G4CR/b
45  *	#
46  *
47  * This program accepts a disk or disk slice path and prints a
48  * device id.
49  *
50  * Exit values:
51  *	0 - means success
52  *	1 - means failure
53  *
54  */
55 int
56 main(int argc, char *argv[])
57 {
58 	int		fd;
59 	ddi_devid_t	devid;
60 	char		*minor_name, *devidstr, *device;
61 #ifdef DEBUG
62 	devid_nmlist_t  *list = NULL;
63 	char		*search_path;
64 	int		i;
65 #endif
66 
67 	if (argc == 1) {
68 		(void) printf("%s <devicepath> [search path]\n",
69 		    argv[0]);
70 		exit(1);
71 	}
72 	device = argv[1];
73 
74 	if ((fd = open(device, O_RDONLY|O_NDELAY)) < 0) {
75 		perror(device);
76 		exit(1);
77 	}
78 	if (devid_get(fd, &devid) != 0) {
79 		perror("devid_get");
80 		exit(1);
81 	}
82 	if (devid_get_minor_name(fd, &minor_name) != 0) {
83 		perror("devid_get_minor_name");
84 		exit(1);
85 	}
86 	if ((devidstr = devid_str_encode(devid, minor_name)) == 0) {
87 		perror("devid_str_encode");
88 		exit(1);
89 	}
90 
91 	(void) printf("devid %s\n", devidstr);
92 
93 	devid_str_free(devidstr);
94 
95 #ifdef DEBUG
96 	if (argc == 3) {
97 		search_path = argv[2];
98 	} else {
99 		search_path = "/dev/rdsk";
100 	}
101 
102 	if (devid_deviceid_to_nmlist(search_path, devid, DEVID_MINOR_NAME_ALL,
103 	    &list)) {
104 		perror("devid_deviceid_to_nmlist");
105 		exit(1);
106 	}
107 
108 	/* loop through list and process device names and numbers */
109 	for (i = 0; list[i].devname != NULL; i++) {
110 		(void) printf("devname: %s %p\n", list[i].devname, list[i].dev);
111 	}
112 	devid_free_nmlist(list);
113 
114 #endif /* DEBUG */
115 
116 	devid_str_free(minor_name);
117 	devid_free(devid);
118 
119 	return (0);
120 }
121