Skip to content

Commit f24cbd9

Browse files
committed
[[ SB Inclusions ]] Abstract module loading functions.
The DB drivers can now be passed a DBcallbacks ptr through a exported 'set_callbacksref' function. The callbacks contain module access functions which use the ones provided by the external interface. This allows dbmysql to use the the engine module loader to find revsecurity (which it weakly links to).
1 parent 3978217 commit f24cbd9

8 files changed

Lines changed: 101 additions & 0 deletions

File tree

revdb/src/dbdriver.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,20 @@ inline void DBString::Set(char *p_string, int p_length, Bool p_binary)
288288
}
289289

290290

291+
///////////////////////////////////////////////////////////////////////////////
292+
293+
#define DBcallbacks_version 0
294+
295+
struct DBcallbacks
296+
{
297+
unsigned int version;
298+
299+
// V0 callbacks
300+
void *(*load_module)(const char *module);
301+
void (*unload_module)(void *module);
302+
void *(*resolve_symbol_in_module)(void *module, const char *symbol);
303+
};
304+
291305
///////////////////////////////////////////////////////////////////////////////
292306

293307
// These are the standard exported functions for all db-drivers. We predeclare
@@ -298,6 +312,7 @@ inline void DBString::Set(char *p_string, int p_length, Bool p_binary)
298312
extern "C" DBConnection *newdbconnectionref() __attribute__((visibility("default")));
299313
extern "C" void releasedbconnectionref(DBConnection *dbref) __attribute__((visibility("default")));
300314
extern "C" void setidcounterref(unsigned int *tidcounter) __attribute__((visibility("default")));
315+
extern "C" void setcallbacksref(DBcallbacks *callbacks) __attribute__((visibility("default")));
301316
#endif
302317

303318
///////////////////////////////////////////////////////////////////////////////

revdb/src/dbdrivercommon.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
1616

1717
#include "dbdrivercommon.h"
1818

19+
#if defined(_WINDOWS)
20+
#define LIBRARY_EXPORT __declspec(dllexport)
21+
#elif defined(_MACOSX)
22+
#define LIBRARY_EXPORT
23+
#elif defined(_LINUX)
24+
#define LIBRARY_EXPORT
25+
#elif defined(TARGET_SUBPLATFORM_IPHONE) || defined(TARGET_SUBPLATFORM_ANDROID)
26+
#define LIBRARY_EXPORT
27+
#endif
28+
1929
// Default implementations for DBField
2030
DBField::DBField()
2131
{
@@ -452,3 +462,34 @@ void CDBCursor::FreeFields()
452462
fields = NULL;
453463
}
454464

465+
////////////////////////////////////////////////////////////////////////////////
466+
467+
static DBcallbacks *dbcallbacks = NULL;
468+
469+
extern "C" LIBRARY_EXPORT void setcallbacksref(DBcallbacks *callbacks)
470+
{
471+
dbcallbacks = callbacks;
472+
}
473+
474+
extern "C" void *MCU_loadmodule(const char *p_path)
475+
{
476+
if (dbcallbacks == NULL)
477+
return NULL;
478+
return dbcallbacks -> load_module(p_path);
479+
}
480+
481+
extern "C" void MCU_unloadmodule(void *p_handle)
482+
{
483+
if (dbcallbacks == NULL)
484+
return;
485+
dbcallbacks -> unload_module(p_handle);
486+
}
487+
488+
extern "C" void *MCU_resolvemodulesymbol(void *p_handle, const char *p_symbol)
489+
{
490+
if (dbcallbacks == NULL)
491+
return NULL;
492+
return dbcallbacks -> resolve_symbol_in_module(p_handle, p_symbol);
493+
}
494+
495+
////////////////////////////////////////////////////////////////////////////////

revdb/src/osxsupport.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ DATABASEREC *DoLoadDatabaseDriver(const char *p_path)
137137
t_result -> idcounterptr = (idcounterrefptr)CFBundleGetFunctionPointerForName(t_bundle, CFSTR("setidcounterref"));
138138
t_result -> newconnectionptr = (new_connectionrefptr)CFBundleGetFunctionPointerForName(t_bundle, CFSTR("newdbconnectionref"));
139139
t_result -> releaseconnectionptr = (release_connectionrefptr)CFBundleGetFunctionPointerForName(t_bundle, CFSTR("releasedbconnectionref"));
140+
t_result -> setcallbacksptr = (set_callbacksrefptr)CFBundleGetFunctionPointerForName(t_bundle, CFSTR("setcallbacksref"));
140141

141142
return t_result;
142143
}
@@ -176,6 +177,7 @@ DATABASEREC *DoLoadDatabaseDriver(const char *p_path)
176177
t_result -> idcounterptr = (idcounterrefptr)dlsym(t_driver_handle, "setidcounterref");
177178
t_result -> newconnectionptr = (new_connectionrefptr)dlsym(t_driver_handle, "newdbconnectionref");
178179
t_result -> releaseconnectionptr = (release_connectionrefptr)dlsym(t_driver_handle, "releasedbconnectionref");
180+
t_result -> setcallbacksptr = (set_callbacksrefptr)dlsym(t_driver_handle, "setcallbacksref");
179181
free(t_filename);
180182
return t_result;
181183

revdb/src/osxsupport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ struct DATABASEREC
3232
idcounterrefptr idcounterptr;
3333
new_connectionrefptr newconnectionptr;
3434
release_connectionrefptr releaseconnectionptr;
35+
set_callbacksrefptr setcallbacksptr;
3536
#ifndef _MAC_SERVER
3637
CFBundleRef driverref;
3738
#else

revdb/src/revdb.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,39 @@ const char *dbtypestrings[] = {
135135

136136
};
137137

138+
static void *DBcallback_loadmodule(const char *p_path)
139+
{
140+
int t_success;
141+
void *t_handle;
142+
LoadModule(p_path, &t_handle, &t_success);
143+
if (t_success == EXTERNAL_FAILURE)
144+
return NULL;
145+
return t_handle;
146+
}
147+
148+
static void DBcallback_unloadmodule(void *p_handle)
149+
{
150+
int t_success;
151+
UnloadModule(p_handle, &t_success);
152+
}
153+
154+
static void *DBcallback_resolvesymbol(void *p_handle, const char *p_symbol)
155+
{
156+
int t_success;
157+
void *t_address;
158+
ResolveSymbolInModule(p_handle, p_symbol, &t_address, &t_success);
159+
if (t_success == EXTERNAL_FAILURE)
160+
return NULL;
161+
return t_address;
162+
}
163+
164+
static DBcallbacks dbcallbacks = {
165+
DBcallbacks_version,
166+
DBcallback_loadmodule,
167+
DBcallback_unloadmodule,
168+
DBcallback_resolvesymbol,
169+
};
170+
138171
DATABASERECList databaselist;
139172
DBList connectionlist;
140173

@@ -252,17 +285,21 @@ DATABASEREC *LoadDatabaseDriverFromName(const char *p_type)
252285
#endif
253286

254287
void *id_counterref_ptr, *new_connectionref_ptr, *release_connectionref_ptr;
288+
void *set_callbacksref_ptr;
255289
id_counterref_ptr = NULL;
256290
new_connectionref_ptr = NULL;
257291
release_connectionref_ptr = NULL;
292+
set_callbacksref_ptr = NULL;
258293

259294
ResolveSymbolInModule(t_handle, "setidcounterref", &id_counterref_ptr, &t_retvalue);
260295
ResolveSymbolInModule(t_handle, "newdbconnectionref", &new_connectionref_ptr, &t_retvalue);
261296
ResolveSymbolInModule(t_handle, "releasedbconnectionref", &release_connectionref_ptr, &t_retvalue);
297+
ResolveSymbolInModule(t_handle, "setcallbacksref", &set_callbacksref_ptr, &t_retvalue);
262298

263299
t_result -> idcounterptr = (idcounterrefptr)id_counterref_ptr;
264300
t_result -> newconnectionptr = (new_connectionrefptr)new_connectionref_ptr;
265301
t_result -> releaseconnectionptr = (release_connectionrefptr)release_connectionref_ptr;
302+
t_result -> setcallbacksptr = (set_callbacksrefptr)set_callbacksref_ptr;
266303
return t_result;
267304
}
268305

@@ -368,6 +405,8 @@ DATABASEREC *LoadDatabaseDriver(const char *p_type)
368405
strcpy(t_database_rec -> dbname, p_type);
369406
if (t_database_rec -> idcounterptr)
370407
(*t_database_rec -> idcounterptr)(&idcounter);
408+
if (t_database_rec -> setcallbacksptr)
409+
(*t_database_rec -> setcallbacksptr)(&dbcallbacks);
371410
}
372411

373412
return t_database_rec;

revdb/src/revdb.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
1919
typedef DBConnection *(*new_connectionrefptr) ();
2020
typedef void (*release_connectionrefptr) (DBConnection *dbref);
2121
typedef void (*idcounterrefptr) (unsigned int *tidcounter);
22+
typedef void (*set_callbacksrefptr)(const DBcallbacks *callbacks);
2223

2324
struct DATABASEREC;
2425

revdb/src/unxsupport.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ DATABASEREC *DoLoadDatabaseDriver(const char *p_path)
203203
t_result -> idcounterptr = (idcounterrefptr)dlsym(t_driver_handle, "setidcounterref");
204204
t_result -> newconnectionptr = (new_connectionrefptr)dlsym(t_driver_handle, "newdbconnectionref");
205205
t_result -> releaseconnectionptr = (release_connectionrefptr)dlsym(t_driver_handle, "releasedbconnectionref");
206+
t_result -> setcallbacksptr = (set_callbacksrefptr)dlsym(t_driver_handle, "setcallbacksref");
206207
free(t_filename);
207208
return t_result;
208209

revdb/src/w32support.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ DATABASEREC *DoLoadDatabaseDriver(const char *p_path)
8181
t_result -> idcounterptr = (idcounterrefptr)GetProcAddress(t_module, "setidcounterref");
8282
t_result -> newconnectionptr = (new_connectionrefptr)GetProcAddress(t_module, "newdbconnectionref");
8383
t_result -> releaseconnectionptr = (release_connectionrefptr)GetProcAddress(t_module, "releasedbconnectionref");
84+
t_result -> setcallbacksptr = (set_callbacksrefptr)GetProcAddress(t_module, "setcallbacksref");
8485
return t_result;
8586
}
8687

0 commit comments

Comments
 (0)