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  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 
27 #include "HBANPIVPort.h"
28 #include "Exceptions.h"
29 #include "Trace.h"
30 #include <iostream>
31 #include <iomanip>
32 #include <cerrno>
33 #include <cstring>
34 #include <sys/types.h>
35 #include <sys/mkdev.h>
36 #include <sys/stat.h>
37 #include <fcntl.h>
38 #include <unistd.h>
39 #include <stropts.h>
40 #include <dirent.h>
41 #include <libdevinfo.h>
42 
43 using namespace std;
44 
45 
46 /**
47  * @memo            Construct a new default HBA Port
48  * @version         1.7
49  */
HBANPIVPort()50 HBANPIVPort::HBANPIVPort() {
51 }
52 
53 /**
54  * @memo            Compare two HBA ports for equality
55  * @return          TRUE if both ports are the same
56  * @return          FALSE if the ports are different
57  * @version         1.7
58  *
59  * @doc             Comparison is based on Node WWN, Port WWN and path
60  */
operator ==(HBANPIVPort & comp)61 bool HBANPIVPort::operator==(HBANPIVPort &comp) {
62 	return (this->getPortWWN() == comp.getPortWWN() &&
63 	    this->getNodeWWN() == comp.getNodeWWN());
64 }
65 
66 /*
67  * Finds controller path for a give device path.
68  *
69  * Return vale: controller path.
70  */
lookupControllerPath(string path)71 string HBANPIVPort::lookupControllerPath(string path) {
72 	Trace log("lookupControllerPath");
73 	DIR	*dp;
74 	char	buf[MAXPATHLEN];
75 	char	node[MAXPATHLEN];
76 	struct dirent	**dirpp, *dirp;
77 	const char	dir[] = "/dev/cfg";
78 	ssize_t	count;
79 	uchar_t	*dir_buf = new uchar_t[sizeof (struct dirent) + MAXPATHLEN];
80 
81 	if ((dp = opendir(dir)) == NULL) {
82 		string tmp = "Unable to open ";
83 		tmp += dir;
84 		tmp += "to find controller number.";
85 		delete[] (dir_buf);
86 		throw IOError(tmp);
87 	}
88 
89 	dirp = (struct dirent *) dir_buf;
90 	dirpp = &dirp;
91 	while ((readdir_r(dp, dirp, dirpp)) == 0  && dirp != NULL) {
92 		if (strcmp(dirp->d_name, ".") == 0 ||
93 		    strcmp(dirp->d_name, "..") == 0) {
94 			continue;
95 		}
96 		sprintf(node, "%s/%s", dir, dirp->d_name);
97 		if ((count = readlink(node,buf,sizeof(buf)))) {
98 			buf[count] = '\0';
99 			if (strstr(buf, path.c_str())) {
100 				string cfg_path = dir;
101 				cfg_path += "/";
102 				cfg_path += dirp->d_name;
103 				closedir(dp);
104 				delete[] (dir_buf);
105 				return (cfg_path);
106 			}
107 		}
108 	}
109 
110 	closedir(dp);
111 	delete[] (dir_buf);
112 	throw InternalError("Unable to find controller path");
113 }
114 
115