1*10156SZhang.Yi@Sun.COM /*
2*10156SZhang.Yi@Sun.COM  * CDDL HEADER START
3*10156SZhang.Yi@Sun.COM  *
4*10156SZhang.Yi@Sun.COM  * The contents of this file are subject to the terms of the
5*10156SZhang.Yi@Sun.COM  * Common Development and Distribution License (the "License").
6*10156SZhang.Yi@Sun.COM  * You may not use this file except in compliance with the License.
7*10156SZhang.Yi@Sun.COM  *
8*10156SZhang.Yi@Sun.COM  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*10156SZhang.Yi@Sun.COM  * or http://www.opensolaris.org/os/licensing.
10*10156SZhang.Yi@Sun.COM  * See the License for the specific language governing permissions
11*10156SZhang.Yi@Sun.COM  * and limitations under the License.
12*10156SZhang.Yi@Sun.COM  *
13*10156SZhang.Yi@Sun.COM  * When distributing Covered Code, include this CDDL HEADER in each
14*10156SZhang.Yi@Sun.COM  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*10156SZhang.Yi@Sun.COM  * If applicable, add the following below this CDDL HEADER, with the
16*10156SZhang.Yi@Sun.COM  * fields enclosed by brackets "[]" replaced with your own identifying
17*10156SZhang.Yi@Sun.COM  * information: Portions Copyright [yyyy] [name of copyright owner]
18*10156SZhang.Yi@Sun.COM  *
19*10156SZhang.Yi@Sun.COM  * CDDL HEADER END
20*10156SZhang.Yi@Sun.COM  */
21*10156SZhang.Yi@Sun.COM /*
22*10156SZhang.Yi@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23*10156SZhang.Yi@Sun.COM  * Use is subject to license terms.
24*10156SZhang.Yi@Sun.COM  */
25*10156SZhang.Yi@Sun.COM 
26*10156SZhang.Yi@Sun.COM #include <dlfcn.h>
27*10156SZhang.Yi@Sun.COM #include <pthread.h>
28*10156SZhang.Yi@Sun.COM #include <string.h>
29*10156SZhang.Yi@Sun.COM #include <sys/sem.h>
30*10156SZhang.Yi@Sun.COM 
31*10156SZhang.Yi@Sun.COM #include "libsun_ima.h"
32*10156SZhang.Yi@Sun.COM #include "ima.h"
33*10156SZhang.Yi@Sun.COM #include "ima-plugin.h"
34*10156SZhang.Yi@Sun.COM 
35*10156SZhang.Yi@Sun.COM extern int number_of_plugins;
36*10156SZhang.Yi@Sun.COM extern int libMutex;
37*10156SZhang.Yi@Sun.COM extern IMA_PLUGIN_INFO	 plugintable[IMA_MAX_NUM_PLUGINS];
38*10156SZhang.Yi@Sun.COM extern void InitLibrary();
39*10156SZhang.Yi@Sun.COM 
40*10156SZhang.Yi@Sun.COM static void os_obtainmutex(int semid);
41*10156SZhang.Yi@Sun.COM static void os_releasemutex(int semid);
42*10156SZhang.Yi@Sun.COM 
43*10156SZhang.Yi@Sun.COM IMA_API IMA_STATUS SUN_IMA_SetTunableProperties(
44*10156SZhang.Yi@Sun.COM 		IMA_OID oid,
45*10156SZhang.Yi@Sun.COM 		ISCSI_TUNABLE_PARAM *param) {
46*10156SZhang.Yi@Sun.COM 	SUN_IMA_SetTunablePropertiesFn PassFunc;
47*10156SZhang.Yi@Sun.COM 	IMA_UINT i;
48*10156SZhang.Yi@Sun.COM 	IMA_STATUS status;
49*10156SZhang.Yi@Sun.COM 
50*10156SZhang.Yi@Sun.COM 	if (number_of_plugins == -1) {
51*10156SZhang.Yi@Sun.COM 		InitLibrary();
52*10156SZhang.Yi@Sun.COM 	}
53*10156SZhang.Yi@Sun.COM 
54*10156SZhang.Yi@Sun.COM 	if (param == NULL) {
55*10156SZhang.Yi@Sun.COM 		return (IMA_ERROR_INVALID_PARAMETER);
56*10156SZhang.Yi@Sun.COM 	}
57*10156SZhang.Yi@Sun.COM 
58*10156SZhang.Yi@Sun.COM 	if ((oid.objectType != IMA_OBJECT_TYPE_LHBA) &&
59*10156SZhang.Yi@Sun.COM 	    (oid.objectType != IMA_OBJECT_TYPE_TARGET)) {
60*10156SZhang.Yi@Sun.COM 		return (IMA_ERROR_INCORRECT_OBJECT_TYPE);
61*10156SZhang.Yi@Sun.COM 	}
62*10156SZhang.Yi@Sun.COM 
63*10156SZhang.Yi@Sun.COM 	os_obtainmutex(libMutex);
64*10156SZhang.Yi@Sun.COM 	status = IMA_ERROR_OBJECT_NOT_FOUND;
65*10156SZhang.Yi@Sun.COM 
66*10156SZhang.Yi@Sun.COM 	for (i = 0; i < number_of_plugins; i++) {
67*10156SZhang.Yi@Sun.COM 		if (plugintable[i].ownerId == oid.ownerId) {
68*10156SZhang.Yi@Sun.COM 			status = IMA_ERROR_UNEXPECTED_OS_ERROR;
69*10156SZhang.Yi@Sun.COM 			os_obtainmutex(plugintable[i].pluginMutex);
70*10156SZhang.Yi@Sun.COM #ifdef SOLARIS
71*10156SZhang.Yi@Sun.COM 			PassFunc = (SUN_IMA_SetTunablePropertiesFn)
72*10156SZhang.Yi@Sun.COM 			    dlsym(plugintable[i].hPlugin,
73*10156SZhang.Yi@Sun.COM 			    "SUN_IMA_SetTunableProperties");
74*10156SZhang.Yi@Sun.COM #endif
75*10156SZhang.Yi@Sun.COM 			if (PassFunc != NULL) {
76*10156SZhang.Yi@Sun.COM 				status = PassFunc(oid, param);
77*10156SZhang.Yi@Sun.COM 			}
78*10156SZhang.Yi@Sun.COM 			os_releasemutex(plugintable[i].pluginMutex);
79*10156SZhang.Yi@Sun.COM 			break;
80*10156SZhang.Yi@Sun.COM 		}
81*10156SZhang.Yi@Sun.COM 	}
82*10156SZhang.Yi@Sun.COM 	os_releasemutex(libMutex);
83*10156SZhang.Yi@Sun.COM 	return (status);
84*10156SZhang.Yi@Sun.COM }
85*10156SZhang.Yi@Sun.COM 
86*10156SZhang.Yi@Sun.COM IMA_API IMA_STATUS SUN_IMA_GetTunableProperties(
87*10156SZhang.Yi@Sun.COM 		IMA_OID oid,
88*10156SZhang.Yi@Sun.COM 		ISCSI_TUNABLE_PARAM *param) {
89*10156SZhang.Yi@Sun.COM 	SUN_IMA_GetTunablePropertiesFn PassFunc = NULL;
90*10156SZhang.Yi@Sun.COM 	int i;
91*10156SZhang.Yi@Sun.COM 	IMA_STATUS status;
92*10156SZhang.Yi@Sun.COM 
93*10156SZhang.Yi@Sun.COM 	if (number_of_plugins == -1) {
94*10156SZhang.Yi@Sun.COM 		InitLibrary();
95*10156SZhang.Yi@Sun.COM 	}
96*10156SZhang.Yi@Sun.COM 
97*10156SZhang.Yi@Sun.COM 	if (param == NULL) {
98*10156SZhang.Yi@Sun.COM 		return (IMA_ERROR_INVALID_PARAMETER);
99*10156SZhang.Yi@Sun.COM 	}
100*10156SZhang.Yi@Sun.COM 
101*10156SZhang.Yi@Sun.COM 	if ((oid.objectType != IMA_OBJECT_TYPE_LHBA) &&
102*10156SZhang.Yi@Sun.COM 	    (oid.objectType != IMA_OBJECT_TYPE_TARGET)) {
103*10156SZhang.Yi@Sun.COM 		return (IMA_ERROR_INCORRECT_OBJECT_TYPE);
104*10156SZhang.Yi@Sun.COM 	}
105*10156SZhang.Yi@Sun.COM 
106*10156SZhang.Yi@Sun.COM 	os_obtainmutex(libMutex);
107*10156SZhang.Yi@Sun.COM 	status = IMA_ERROR_OBJECT_NOT_FOUND;
108*10156SZhang.Yi@Sun.COM 	for (i = 0; i < number_of_plugins; i++) {
109*10156SZhang.Yi@Sun.COM 		status = IMA_ERROR_UNEXPECTED_OS_ERROR;
110*10156SZhang.Yi@Sun.COM 		if (plugintable[i].ownerId == oid.ownerId) {
111*10156SZhang.Yi@Sun.COM 			os_obtainmutex(plugintable[i].pluginMutex);
112*10156SZhang.Yi@Sun.COM #ifdef SOLARIS
113*10156SZhang.Yi@Sun.COM 			PassFunc = (SUN_IMA_GetTunablePropertiesFn)
114*10156SZhang.Yi@Sun.COM 			    dlsym(plugintable[i].hPlugin,
115*10156SZhang.Yi@Sun.COM 			    "SUN_IMA_GetTunableProperties");
116*10156SZhang.Yi@Sun.COM #endif
117*10156SZhang.Yi@Sun.COM 			if (PassFunc != NULL) {
118*10156SZhang.Yi@Sun.COM 				status = PassFunc(oid, param);
119*10156SZhang.Yi@Sun.COM 			}
120*10156SZhang.Yi@Sun.COM 			os_releasemutex(plugintable[i].pluginMutex);
121*10156SZhang.Yi@Sun.COM 			break;
122*10156SZhang.Yi@Sun.COM 		}
123*10156SZhang.Yi@Sun.COM 	}
124*10156SZhang.Yi@Sun.COM 	os_releasemutex(libMutex);
125*10156SZhang.Yi@Sun.COM 	return (status);
126*10156SZhang.Yi@Sun.COM }
127*10156SZhang.Yi@Sun.COM 
128*10156SZhang.Yi@Sun.COM static void os_obtainmutex(int semid) {
129*10156SZhang.Yi@Sun.COM 	int retVal;
130*10156SZhang.Yi@Sun.COM 	struct sembuf sem_b;
131*10156SZhang.Yi@Sun.COM 
132*10156SZhang.Yi@Sun.COM 	sem_b.sem_num = 0;
133*10156SZhang.Yi@Sun.COM 	sem_b.sem_op = -1;
134*10156SZhang.Yi@Sun.COM 	sem_b.sem_flg = SEM_UNDO;
135*10156SZhang.Yi@Sun.COM 	retVal = semop(semid, &sem_b, 1);
136*10156SZhang.Yi@Sun.COM 
137*10156SZhang.Yi@Sun.COM }
138*10156SZhang.Yi@Sun.COM 
139*10156SZhang.Yi@Sun.COM static void os_releasemutex(int semid) {
140*10156SZhang.Yi@Sun.COM 	int retVal;
141*10156SZhang.Yi@Sun.COM 	struct sembuf sem_b;
142*10156SZhang.Yi@Sun.COM 
143*10156SZhang.Yi@Sun.COM 	sem_b.sem_num = 0;
144*10156SZhang.Yi@Sun.COM 	sem_b.sem_op = 1;
145*10156SZhang.Yi@Sun.COM 	sem_b.sem_flg = SEM_UNDO;
146*10156SZhang.Yi@Sun.COM 	retVal = semop(semid, &sem_b, 1);
147*10156SZhang.Yi@Sun.COM 
148*10156SZhang.Yi@Sun.COM }
149