Windows: AFSTearDownExtents may experience active extents
[openafs.git] / src / WINNT / afsapplib / al_dynlink.cpp
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  *
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <winsock2.h>
11 #include <ws2tcpip.h>
12
13 extern "C" {
14 #include <afs/afsconfig.h>
15 #include <afs/param.h>
16 #include <afs/stds.h>
17 #include <roken.h>
18 }
19
20 #include <WINNT/afsapplib.h>
21 #include "al_dynlink.h"
22
23
24 /*
25  * LIBRARIES __________________________________________________________________
26  *
27  */
28
29 #define cszLIBRARY_UTIL        TEXT("AfsAdminUtil.dll")
30 #define cszLIBRARY_KAS         TEXT("AfsKasAdmin.dll")
31 #define cszLIBRARY_CLIENT      TEXT("AfsClientAdmin.dll")
32
33
34 /*
35  * ADMIN ______________________________________________________________________
36  *
37  */
38
39 static size_t g_cReqUtilLibrary = 0;
40 static HINSTANCE g_hiUtilLibrary = NULL;
41
42 util_AdminErrorCodeTranslate_t util_AdminErrorCodeTranslateP = NULL;
43
44
45 BOOL OpenUtilLibrary (ULONG *pStatus)
46 {
47    BOOL rc = FALSE;
48    ULONG status = ERROR_DLL_NOT_FOUND;
49
50    if ((++g_cReqUtilLibrary) == 1)
51       {
52       if ( ((g_hiUtilLibrary = LoadLibrary (cszLIBRARY_UTIL)) == NULL) ||
53            ((util_AdminErrorCodeTranslateP = (util_AdminErrorCodeTranslate_t)GetProcAddress (g_hiUtilLibrary, "util_AdminErrorCodeTranslate")) == NULL) )
54          {
55          status = GetLastError();
56          CloseUtilLibrary();
57          }
58       }
59    if (g_hiUtilLibrary)
60       {
61       rc = TRUE;
62       }
63
64    if (pStatus)
65       *pStatus = status;
66    return rc;
67 }
68
69
70 void CloseUtilLibrary (void)
71 {
72    if ((--g_cReqUtilLibrary) == 0)
73       {
74       if (g_hiUtilLibrary)
75          FreeLibrary (g_hiUtilLibrary);
76       g_hiUtilLibrary = NULL;
77       }
78 }
79
80
81 /*
82  * KAS ________________________________________________________________________
83  *
84  */
85
86 static size_t g_cReqKasLibrary = 0;
87 static HINSTANCE g_hiKasLibrary = NULL;
88
89 kas_PrincipalGetBegin_t kas_PrincipalGetBeginP = NULL;
90 kas_PrincipalGetNext_t kas_PrincipalGetNextP = NULL;
91 kas_PrincipalGetDone_t kas_PrincipalGetDoneP = NULL;
92 kas_PrincipalGet_t kas_PrincipalGetP = NULL;
93
94
95 BOOL OpenKasLibrary (ULONG *pStatus)
96 {
97    BOOL rc = FALSE;
98    ULONG status = ERROR_DLL_NOT_FOUND;
99
100    if ((++g_cReqKasLibrary) == 1)
101       {
102       if ( ((g_hiKasLibrary = LoadLibrary (cszLIBRARY_KAS)) == NULL) ||
103            ((kas_PrincipalGetBeginP = (kas_PrincipalGetBegin_t)GetProcAddress (g_hiKasLibrary, "kas_PrincipalGetBegin")) == NULL) ||
104            ((kas_PrincipalGetNextP = (kas_PrincipalGetNext_t)GetProcAddress (g_hiKasLibrary, "kas_PrincipalGetNext")) == NULL) ||
105            ((kas_PrincipalGetDoneP = (kas_PrincipalGetDone_t)GetProcAddress (g_hiKasLibrary, "kas_PrincipalGetDone")) == NULL) ||
106            ((kas_PrincipalGetP = (kas_PrincipalGet_t)GetProcAddress (g_hiKasLibrary, "kas_PrincipalGet")) == NULL) )
107          {
108          status = GetLastError();
109          CloseKasLibrary();
110          }
111       }
112    if (g_hiKasLibrary)
113       {
114       rc = TRUE;
115       }
116
117    if (pStatus)
118       *pStatus = status;
119    return rc;
120 }
121
122
123 void CloseKasLibrary (void)
124 {
125    if ((--g_cReqKasLibrary) == 0)
126       {
127       if (g_hiKasLibrary)
128          FreeLibrary (g_hiKasLibrary);
129       g_hiKasLibrary = NULL;
130       }
131 }
132
133
134 /*
135  * CLIENT _____________________________________________________________________
136  *
137  */
138
139 static size_t g_cReqClientLibrary = 0;
140 static HINSTANCE g_hiClientLibrary = NULL;
141
142 typedef int (ADMINAPI *afsclient_Init_t)(afs_status_p st);
143
144 afsclient_TokenGetExisting_t afsclient_TokenGetExistingP = NULL;
145 afsclient_TokenGetNew_t afsclient_TokenGetNewP = NULL;
146 afsclient_TokenClose_t afsclient_TokenCloseP = NULL;
147 afsclient_TokenQuery_t afsclient_TokenQueryP = NULL;
148 afsclient_CellOpen_t afsclient_CellOpenP = NULL;
149 afsclient_CellClose_t afsclient_CellCloseP = NULL;
150 afsclient_LocalCellGet_t afsclient_LocalCellGetP = NULL;
151 afsclient_Init_t afsclient_InitP = NULL;
152
153
154 BOOL OpenClientLibrary (ULONG *pStatus)
155 {
156    BOOL rc = FALSE;
157    ULONG status = ERROR_DLL_NOT_FOUND;
158
159    if ((++g_cReqClientLibrary) == 1)
160       {
161       if ( ((g_hiClientLibrary = LoadLibrary (cszLIBRARY_CLIENT)) == NULL) ||
162            ((afsclient_TokenGetExistingP = (afsclient_TokenGetExisting_t)GetProcAddress (g_hiClientLibrary, "afsclient_TokenGetExisting")) == NULL) ||
163            ((afsclient_TokenGetNewP = (afsclient_TokenGetNew_t)GetProcAddress (g_hiClientLibrary, "afsclient_TokenGetNew")) == NULL) ||
164            ((afsclient_TokenCloseP = (afsclient_TokenClose_t)GetProcAddress (g_hiClientLibrary, "afsclient_TokenClose")) == NULL) ||
165            ((afsclient_TokenQueryP = (afsclient_TokenQuery_t)GetProcAddress (g_hiClientLibrary, "afsclient_TokenQuery")) == NULL) ||
166            ((afsclient_CellOpenP = (afsclient_CellOpen_t)GetProcAddress (g_hiClientLibrary, "afsclient_CellOpen")) == NULL) ||
167            ((afsclient_CellCloseP = (afsclient_CellClose_t)GetProcAddress (g_hiClientLibrary, "afsclient_CellClose")) == NULL) ||
168            ((afsclient_LocalCellGetP = (afsclient_LocalCellGet_t)GetProcAddress (g_hiClientLibrary, "afsclient_LocalCellGet")) == NULL) ||
169            ((afsclient_InitP = (afsclient_Init_t)GetProcAddress (g_hiClientLibrary, "afsclient_Init")) == NULL) )
170          {
171          status = GetLastError();
172          CloseClientLibrary();
173          }
174       else if (!(*afsclient_InitP)((afs_status_p)&status))
175          {
176          CloseClientLibrary();
177          }
178       }
179    if (g_hiClientLibrary)
180       {
181       rc = TRUE;
182       }
183
184    if (pStatus)
185       *pStatus = status;
186    return rc;
187 }
188
189
190 void CloseClientLibrary (void)
191 {
192    if ((--g_cReqClientLibrary) == 0)
193       {
194       if (g_hiClientLibrary)
195          FreeLibrary (g_hiClientLibrary);
196       g_hiClientLibrary = NULL;
197       }
198 }
199