-
Notifications
You must be signed in to change notification settings - Fork 30
/
dataservice.c
393 lines (359 loc) · 15.8 KB
/
dataservice.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*
This program and the accompanying materials are
made available under the terms of the Eclipse Public License v2.0 which accompanies
this distribution, and is available at https://www.eclipse.org/legal/epl-v20.html
SPDX-License-Identifier: EPL-2.0
Copyright Contributors to the Zowe Project.
*/
#ifdef METTLE
#include <metal/metal.h>
#include <metal/stddef.h>
#include <metal/stdio.h>
#include <metal/stdlib.h>
#include <metal/string.h>
#include <metal/stdarg.h>
#include "metalio.h"
#include "qsam.h"
#else
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <stdarg.h>
#include <pthread.h>
#include <inttypes.h>
#define __SUSV3 1 /* This heinous hack is needed because dlfcn */
#include <dlfcn.h>
#undef __SUSV3
#endif
#include "logging.h"
#include "zowetypes.h"
#include "alloc.h"
#include "utils.h"
#include "collections.h"
#include "bpxnet.h"
#include "socketmgmt.h"
#include "le.h"
#include "json.h"
#include "httpserver.h"
#include "storage.h"
#include "dataservice.h"
#define SERVICE_TYPE_SERVICE 1
#define SERVICE_TYPE_GROUP 2
#define SERVICE_TYPE_IMPORT 3
typedef struct {
const char* pluginString;
int pluginType;
} PlugInMapEntry;
static PlugInMapEntry plugInMap[] = {
{"library", WEB_PLUGIN_TYPE_LIBRARY},
{"application", WEB_PLUGIN_TYPE_APPLICATION},
{"library", WEB_PLUGIN_TYPE_LIBRARY},
{"windowManager", WEB_PLUGIN_TYPE_WINDOW_MANAGER},
{"unknown", WEB_PLUGIN_TYPE_UNKNOWN},
{0, 0}
};
int pluginTypeFromString(const char*pluginType)
{
PlugInMapEntry *e = plugInMap;
while (e->pluginString != 0) {
if (0 == strcasecmp(pluginType, e->pluginString)) {
return e->pluginType;
}
++e;
}
return WEB_PLUGIN_TYPE_UNKNOWN;
}
const char* pluginTypeString(int pluginType)
{
PlugInMapEntry *e = plugInMap;
while (e->pluginString != 0) {
// this seems like an error, but it appears vestigial
if (pluginType = e->pluginType) {
return e->pluginString;
}
++e;
}
return pluginTypeString(WEB_PLUGIN_TYPE_UNKNOWN);
}
static ExternalAPI *lookupAPI(InternalAPIMap *namedAPIMap, char *entryPointName){
int i;
zowelog(NULL, LOG_COMP_DATASERVICE, ZOWE_LOG_DEBUG, "lookup count=%d name=%s\n", namedAPIMap->length, entryPointName);
for (i=0; i<namedAPIMap->length; i++){
if (!strcmp(namedAPIMap->names[i],entryPointName)){
return namedAPIMap->entryPoints[i];
}
}
return NULL;
}
static void *lookupDLLEntryPoint(char *libraryName, char *functionName){
void* ep = NULL;
#ifndef METTLE
int flags = RTLD_GLOBAL
#ifdef __ZOWE_OS_LINUX
| RTLD_NOW
#endif
;
FileInfo info;
int status = 0;
int returnCode = 0;
int reasonCode = 0;
status = fileInfo(libraryName, &info, &returnCode, &reasonCode);
if (status == 0) {
if (!(info.attributeFlags & BPXYSTAT_ATTR_PROGCTL)) {
zowelog(NULL, LOG_COMP_DATASERVICE, ZOWE_LOG_DEBUG,
"FAILURE: Dataservice: %s does not have the Program Control attribute this may cause unexpected errors therefore will not be loaded\n",
libraryName);
} else {
void *library = dlopen(libraryName,flags);
if (library){
zowelog(NULL, LOG_COMP_DATASERVICE, ZOWE_LOG_DEBUG, "dll handle for %s = 0x%" PRIxPTR "\n", libraryName, library);
ep = dlsym(library,functionName);
/* dlclose(library); - this will really close the DLL, like not be able to call */
if (ep == NULL){
zowelog(NULL, LOG_COMP_DATASERVICE, ZOWE_LOG_DEBUG, "%s.%s could not be found - dlsym error %s\n", libraryName, functionName, dlerror());
} else {
zowelog(NULL, LOG_COMP_DATASERVICE, ZOWE_LOG_DEBUG, "%s.%s is at 0x%" PRIxPTR "\n", libraryName, functionName, ep);
}
} else {
zowelog(NULL, LOG_COMP_DATASERVICE, ZOWE_LOG_DEBUG, "dlopen error for %s - %s\n",libraryName, dlerror());
}
}
} else {
zowelog(NULL, LOG_COMP_DATASERVICE, ZOWE_LOG_DEBUG,
"FAILURE: Dataservice: %s status not available therefore will not be loaded, please check the provided file name: (return = 0x%x, reason = 0x%x)\n",
libraryName, returnCode, reasonCode);
}
#endif /* not METTLE */
return ep;
}
static DataService *makeDataService(WebPlugin *plugin, JsonObject *serviceJsonObject, char *subURI, InternalAPIMap *namedAPIMap,
unsigned int *idMultiplier, int pluginLogLevel, Storage *storage) {
zowelog(NULL, LOG_COMP_DATASERVICE, ZOWE_LOG_DEBUG, "%s begin data service:\n", __FUNCTION__);
DataService *service = (DataService*) safeMalloc(sizeof (DataService), "DataService");
memset(service, 0, sizeof (DataService));
service->plugin = plugin;
service->serviceDefinition = serviceJsonObject;
service->pattern = jsonObjectGetString(serviceJsonObject, "pattern");
service->subURI = subURI;
service->storage = storage;
if (subURI) { /* group member */
service->name = jsonObjectGetString(serviceJsonObject, "subserviceName");
} else {
service->name = jsonObjectGetString(serviceJsonObject, "name");
}
service->identifier = safeMalloc(strlen(plugin->identifier) + 1
+ (service->subURI ? strlen(service->subURI) + 1 : 0)
+ (service->name ? strlen(service->name) : 0) + 1, "service identifier name");
if (service->subURI && service->name) {
sprintf(service->identifier, "%s/%s/%s", plugin->identifier, service->subURI, service->name);
} else if (service->subURI) {
sprintf(service->identifier, "%s/%s", plugin->identifier, service->subURI);
} else if (service->name) {
sprintf(service->identifier, "%s/%s", plugin->identifier, service->name);
} else {
sprintf(service->identifier, "%s", plugin->identifier);
}
service->loggingIdentifier = LOG_PROD_PLUGINS + (0x10000 * (*idMultiplier));
logConfigureComponent(NULL, service->loggingIdentifier, service->identifier,
LOG_DEST_PRINTF_STDOUT, pluginLogLevel);
zowelog(NULL, service->loggingIdentifier, ZOWE_LOG_DEBUG, "added identifier for %s\n", service->identifier);
char *initializerLookupMethod = jsonObjectGetString(serviceJsonObject, "initializerLookupMethod");
char *initializerName = jsonObjectGetString(serviceJsonObject, "initializerName");
if (!strcmp(initializerLookupMethod, "internal")) {
service->initializer = (InitializerAPI*) lookupAPI(namedAPIMap, initializerName);
zowelog(NULL, LOG_COMP_DATASERVICE, ZOWE_LOG_DEBUG, "Service=%s initializer set internal method=0x%p\n",
service->identifier, service->name, service->initializer);
} else if (!strcmp(initializerLookupMethod,"external")){
char *libraryName = jsonObjectGetString(serviceJsonObject, "libraryName");
/* "external will load a DLL in LE on the path, and a load module in the STEPLIB in METAL */
#ifdef METTLE
/* do a load of 8 char name. Pray that the STEPLIB yields something good. */
service->initializer = NULL;
zowelog(NULL, LOG_COMP_DATASERVICE, ZOWE_LOG_DEBUG, "*** PANIC *** service loading in METTLE not implemented\n");
#else
char dllLocation[1023] = {0}; /* USS max path length */
int pluginLocationLen = strlen(plugin->pluginLocation);
if (plugin->pluginLocation[pluginLocationLen - 1] == '/') {
snprintf(dllLocation, sizeof(dllLocation), "%.*s/lib/%s", strlen(plugin->pluginLocation) - 1, plugin->pluginLocation, libraryName);
}
else {
snprintf(dllLocation, sizeof(dllLocation), "%s/lib/%s", plugin->pluginLocation, libraryName);
}
zowelog(NULL, LOG_COMP_DATASERVICE, ZOWE_LOG_DEBUG, "going for DLL EP lib=%s epName=%s\n", dllLocation, initializerName);
service->initializer = (InitializerAPI*) lookupDLLEntryPoint(dllLocation,initializerName);
if (service->initializer == NULL) {
zowelog(NULL, LOG_COMP_DATASERVICE, ZOWE_LOG_DEBUG, "going for DLL EP lib=%s epName=%s\n", libraryName, initializerName);
service->initializer = (InitializerAPI*) lookupDLLEntryPoint(libraryName,initializerName);
}
zowelog(NULL, LOG_COMP_DATASERVICE, ZOWE_LOG_DEBUG, "DLL EP = 0x%x\n", service->initializer);
fflush(stdout);
fflush(stderr);
#endif
} else {
zowelog(NULL, LOG_COMP_DATASERVICE, ZOWE_LOG_DEBUG, "unknown plugin initialize lookup method %s\n", initializerLookupMethod);
}
zowelog(NULL, LOG_COMP_DATASERVICE, ZOWE_LOG_DEBUG, "%s end\n", __FUNCTION__);
return service;
}
void initalizeWebPlugin(WebPlugin *plugin, HttpServer *server) {
int i = 0;
int count = plugin->dataServiceCount;
zowelog(NULL, LOG_COMP_DATASERVICE, ZOWE_LOG_DEBUG2, "%s begin, count=%d\n", __FUNCTION__,count);
for (i = 0; i < count; i++) {
DataService *service = plugin->dataServices[i];
if (service->initializer){
service->initializer(service, server);
} else{
zowelog(NULL, LOG_COMP_DATASERVICE, ZOWE_LOG_DEBUG, "*** PANIC *** service %s has no installer\n", service->identifier);
}
}
zowelog(NULL, LOG_COMP_DATASERVICE, ZOWE_LOG_DEBUG2, "%s end\n", __FUNCTION__);
}
static void freeWebPlugin(WebPlugin *plugin) {
safeFree((char*)plugin, sizeof(WebPlugin));
}
static bool isValidServiceDef(JsonObject *serviceDef) {
char *type = jsonObjectGetString(serviceDef, "type");
char *serviceName = jsonObjectGetString(serviceDef, "name");
char *sourceName = jsonObjectGetString(serviceDef, "sourceName");
bool isImport = false;
if (type) {
isImport = strcmp(type, "import") ? false : true;
if (!isImport && !serviceName) {
// Return a null plugin when 'name' is not set for dataservices of types: router or service or modeService or external.
zowelog(NULL, LOG_COMP_DATASERVICE, ZOWE_LOG_DEBUG, "** PANIC: Missing 'name' fields for dataservices. **\n");
return false;
} else if (isImport && !sourceName) {
// Return a null plugin when 'sourceName' is not set for dataservices of type: import.
zowelog(NULL, LOG_COMP_DATASERVICE, ZOWE_LOG_DEBUG, "** PANIC: Missing 'sourceName' fields for dataservices of type 'import'. **\n");
return false;
}else{
// Add more validations if any.
}
} else {
zowelog(NULL, LOG_COMP_DATASERVICE, ZOWE_LOG_DEBUG, "** PANIC: Check pluginDefinition for correct 'type' fields on dataservices.** \n");
return false;
}
return true;
}
WebPlugin *makeWebPlugin2(char *pluginLocation, JsonObject *pluginDefintion, InternalAPIMap *internalAPIMap,
unsigned int *idMultiplier, int pluginLogLevel, Storage *storage) {
zowelog(NULL, LOG_COMP_DATASERVICE, ZOWE_LOG_DEBUG2, "%s begin\n", __FUNCTION__);
WebPlugin *plugin = (WebPlugin*)safeMalloc(sizeof(WebPlugin),"WebPlugin");
memset(plugin, 0, sizeof (WebPlugin));
plugin->pluginLocation = pluginLocation;
plugin->identifier = jsonObjectGetString(pluginDefintion, "identifier");
plugin->baseURI = jsonObjectGetString(pluginDefintion, "baseURI");
char *pluginType = jsonObjectGetString(pluginDefintion, "pluginType");
if (pluginType) {
plugin->pluginType = pluginTypeFromString(pluginType);
} else {
plugin->pluginType = WEB_PLUGIN_TYPE_APPLICATION;
}
plugin->pluginDefinition = pluginDefintion;
JsonArray *dataServices = jsonObjectGetArray(pluginDefintion, "dataServices");
if (dataServices && jsonArrayGetCount(dataServices) > 0) {
/* count data services */
plugin->dataServiceCount = 0;
for (int i = 0; i < jsonArrayGetCount(dataServices); i ++) {
JsonObject *serviceDef = jsonArrayGetObject(dataServices, i);
char *type = jsonObjectGetString(serviceDef, "type");
if (!isValidServiceDef(serviceDef)){
freeWebPlugin(plugin);
plugin = NULL;
return NULL;
}
if (!strcmp(type, "service")){
plugin->dataServiceCount ++;
} else if (!strcmp(type, "group")) {
JsonArray* group = jsonObjectGetArray(serviceDef, "subservices");
if (group) {
plugin->dataServiceCount += jsonArrayGetCount(group);
}
}
}
zowelog(NULL, LOG_COMP_DATASERVICE, ZOWE_LOG_INFO, "Plugin=%s, found %d data service(s)\n", plugin->identifier, plugin->dataServiceCount);
if (plugin->dataServiceCount > 0) {
plugin->dataServices = (DataService**)safeMalloc(sizeof(DataService*) * plugin->dataServiceCount,"DataServices");
} else {
plugin->dataServices = NULL;
}
int k = 0;
for (int i = 0; i < jsonArrayGetCount(dataServices); i++) {
JsonObject *serviceDef = jsonArrayGetObject(dataServices, i);
char *type = jsonObjectGetString(serviceDef, "type");
if (!type || !strcmp(type, "service")) {
plugin->dataServices[k++] = makeDataService(plugin, jsonArrayGetObject(dataServices, i), NULL, internalAPIMap,
idMultiplier, pluginLogLevel, storage);
(*idMultiplier++);
} else if (!strcmp(type, "group")) {
char *subURI = jsonObjectGetString(serviceDef, "name");
JsonArray* group = jsonObjectGetArray(serviceDef, "subservices");
if (group) {
for (int j = 0; j < jsonArrayGetCount(group); j++) {
plugin->dataServices[k++] = makeDataService(plugin, jsonArrayGetObject(group, j), subURI, internalAPIMap,
idMultiplier, pluginLogLevel, storage);
(*idMultiplier++);
}
}
} else {
/* import, ignore */
}
}
}
zowelog(NULL, LOG_COMP_DATASERVICE, ZOWE_LOG_DEBUG2, "%s end\n", __FUNCTION__);
return plugin;
}
WebPlugin *makeWebPlugin(char *pluginLocation, JsonObject *pluginDefintion, InternalAPIMap *internalAPIMap,
unsigned int *idMultiplier, int pluginLogLevel) {
return makeWebPlugin2(pluginLocation, pluginDefintion, internalAPIMap, idMultiplier, pluginLogLevel, NULL);
}
HttpService *makeHttpDataService(DataService *dataService, HttpServer *server) {
char urlMask[512] = {0};
int result;
HttpService *httpService = NULL;
result = makeHttpDataServiceUrlMask(dataService, urlMask, sizeof(urlMask), server->defaultProductURLPrefix);
if (result != -1) {
zowelog(NULL, LOG_COMP_DATASERVICE, ZOWE_LOG_INFO, "Installing dataservice %s to URI %s\n", dataService->identifier, urlMask);
httpService = makeGeneratedService(dataService->identifier, urlMask);
httpService->authType = SERVICE_AUTH_NATIVE_WITH_SESSION_TOKEN; /* The default */
registerHttpService(server, httpService);
httpService->userPointer = dataService;
}
return httpService;
}
int makeHttpDataServiceUrlMask(DataService *dataService, char *urlMaskBuffer, int urlMaskBufferSize, char *productPrefix) {
WebPlugin *plugin = dataService->plugin;
if (productPrefix) {
if (dataService->subURI) {
snprintf(urlMaskBuffer, urlMaskBufferSize, "/%s/plugins/%s/services/%s/%s", productPrefix, plugin->identifier,
dataService->subURI, dataService->pattern);
} else if (dataService->name) {
snprintf(urlMaskBuffer, urlMaskBufferSize, "/%s/plugins/%s/services/%s/**", productPrefix, plugin->identifier,
dataService->name);
} else {
zowelog(NULL, LOG_COMP_DATASERVICE, ZOWE_LOG_DEBUG, "** PANIC: Data service has no name, group name, or pattern **\n");
return -1;
}
} else {
if (dataService->subURI) {
snprintf(urlMaskBuffer, urlMaskBufferSize, "/plugins/%s/services/%s/%s", plugin->identifier,
dataService->subURI, dataService->pattern);
} else if (dataService->name) {
snprintf(urlMaskBuffer, urlMaskBufferSize, "/plugins/%s/services/%s/**", plugin->identifier,
dataService->name);
} else {
zowelog(NULL, LOG_COMP_DATASERVICE, ZOWE_LOG_DEBUG, "** PANIC: Data service has no name, group name, or pattern **\n");
return -1;
}
}
return 0;
}
/*
This program and the accompanying materials are
made available under the terms of the Eclipse Public License v2.0 which accompanies
this distribution, and is available at https://www.eclipse.org/legal/epl-v20.html
SPDX-License-Identifier: EPL-2.0
Copyright Contributors to the Zowe Project.
*/