Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unix: Refactored sys_dylib into a Library class
This will handle dynamic unloading of conflicting plugins. A Windows implementation for Library can be done later.
- Loading branch information
Showing
8 changed files
with
323 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/**\file library.h | ||
*\section License | ||
* License: GPL | ||
* Online License Link: http://www.gnu.org/licenses/gpl.html | ||
* | ||
*\author Copyright © 2006-2011 Jaakko Keränen <jaakko.keranen@iki.fi> | ||
*\author Copyright © 2009-2011 Daniel Swanson <danij@dengine.net> | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin St, Fifth Floor, | ||
* Boston, MA 02110-1301 USA | ||
*/ | ||
|
||
/** | ||
* Dynamic Libraries. | ||
* | ||
* These functions provide roughly the same functionality as the ltdl | ||
* library. Since the ltdl library appears to be broken on Mac OS X, | ||
* these will be used instead when loading plugin libraries. | ||
*/ | ||
|
||
#ifndef LIBDENG_SYSTEM_UTILS_DYNAMIC_LIBRARY_H | ||
#define LIBDENG_SYSTEM_UTILS_DYNAMIC_LIBRARY_H | ||
|
||
struct library_s; // The library instance (opaque). | ||
typedef struct library_s Library; | ||
|
||
/** | ||
* Initializes the library loader. | ||
*/ | ||
void Library_Init(void); | ||
|
||
/** | ||
* Release all resources associated with dynamic libraries. Must be called | ||
* when shutting down the engine. | ||
*/ | ||
void Library_Shutdown(void); | ||
|
||
/** | ||
* Defines an additional library @a dir where to look for dynamic libraries. | ||
*/ | ||
void Library_AddSearchDir(const char* dir); | ||
|
||
/** | ||
* Looks for dynamic libraries and calls @a func for each one. | ||
*/ | ||
int Library_IterateAvailableLibraries(int (*func)(const char* fileName, void* data), void* data); | ||
|
||
/** | ||
* Loads a dynamic library. | ||
* | ||
* @param fileName Name of the library to open. | ||
*/ | ||
Library* Library_New(const char* fileName); | ||
|
||
void Library_Delete(Library* lib); | ||
|
||
/** | ||
* Looks up a symbol from the library. | ||
* | ||
* @param symbolName Name of the symbol. | ||
* | ||
* @return @c NULL if the symbol is not defined. Otherwise the address of | ||
* the symbol. | ||
*/ | ||
void* Library_Symbol(Library* lib, const char* symbolName); | ||
|
||
/** | ||
* Returns the latest error message. | ||
*/ | ||
const char* Library_LastError(void); | ||
|
||
#endif /* LIBDENG_SYSTEM_UTILS_DYNAMIC_LIBRARY_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,199 @@ | ||
/**\file library.c | ||
*\section License | ||
* License: GPL | ||
* Online License Link: http://www.gnu.org/licenses/gpl.html | ||
* | ||
*\author Copyright © 2006-2011 Jaakko Keränen <jaakko.keranen@iki.fi> | ||
*\author Copyright © 2009-2011 Daniel Swanson <danij@dengine.net> | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin St, Fifth Floor, | ||
* Boston, MA 02110-1301 USA | ||
*/ | ||
|
||
#include "de_base.h" | ||
#include "de_filesys.h" | ||
#include "m_misc.h" | ||
#include "m_args.h" | ||
|
||
#include <sys/types.h> | ||
#include <unistd.h> | ||
#include <dirent.h> | ||
#include <dlfcn.h> | ||
#include <string.h> | ||
|
||
typedef void* handle_t; | ||
|
||
static filename_t appDir; /// @todo Use ddstring_t | ||
static ddstring_t* lastError; | ||
|
||
struct library_s { | ||
ddstring_t* path; | ||
handle_t handle; | ||
}; | ||
|
||
static void getBundlePath(char* path, size_t len) | ||
{ | ||
if(ArgCheckWith("-libdir", 1)) | ||
{ | ||
strncpy(path, ArgNext(), len); | ||
return; | ||
} | ||
|
||
if(ArgCheckWith("-appdir", 1)) | ||
{ | ||
dd_snprintf(path, len, "%s/%s", appDir, ArgNext()); | ||
return; | ||
} | ||
|
||
#ifdef MACOSX | ||
// This is the default location where bundles are. | ||
dd_snprintf(path, len, "%s/Bundles", appDir); | ||
#endif | ||
#ifdef UNIX | ||
#ifdef DENG_LIBRARY_DIR | ||
strncpy(path, DENG_LIBRARY_DIR, len); | ||
#else | ||
// Assume they are in the cwd. | ||
strncpy(path, appDir, len); | ||
#endif | ||
#endif | ||
} | ||
|
||
void Library_Init(void) | ||
{ | ||
lastError = Str_NewStd(); | ||
getcwd(appDir, sizeof(appDir)); | ||
} | ||
|
||
void Library_Shutdown(void) | ||
{ | ||
Str_Delete(lastError); lastError = 0; | ||
|
||
/// @todo Unload all remaining libraries. | ||
} | ||
|
||
Library* Library_New(const char *fileName) | ||
{ | ||
Library* lib = 0; | ||
handle_t handle; | ||
filename_t bundlePath; /// @todo Use ddstring_t | ||
#ifdef MACOSX | ||
char* ptr; | ||
#endif | ||
|
||
getBundlePath(bundlePath, FILENAME_T_MAXLEN); | ||
if(bundlePath[strlen(bundlePath) - 1] != '/') | ||
strncat(bundlePath, "/", FILENAME_T_MAXLEN); | ||
|
||
#ifdef MACOSX | ||
strncat(bundlePath, fileName, FILENAME_T_MAXLEN); | ||
strncat(bundlePath, "/", FILENAME_T_MAXLEN); | ||
#endif | ||
|
||
strncat(bundlePath, fileName, FILENAME_T_MAXLEN); | ||
|
||
#ifdef MACOSX | ||
{ const char* ext = F_FindFileExtension(bundlePath); | ||
if(ext && stricmp(ext, "dylib") && stricmp(ext, "bundle")) { | ||
// Not a dynamic library... We already know this will fail. | ||
Str_Set(lastError, "not a dynamic library"); | ||
return NULL; | ||
}} | ||
// Get rid of the ".bundle" in the end. | ||
if(NULL != (ptr = strrchr(bundlePath, '.'))) | ||
*ptr = '\0'; | ||
#endif | ||
Str_Clear(lastError); | ||
|
||
handle = dlopen(bundlePath, RTLD_NOW); | ||
if(!handle) | ||
{ | ||
Str_Set(lastError, dlerror()); | ||
printf("Library_New: Error opening \"%s\" (%s).\n", bundlePath, Library_LastError()); | ||
return 0; | ||
} | ||
|
||
// Create the Library instance. | ||
lib = calloc(1, sizeof(*lib)); | ||
lib->handle = handle; | ||
lib->path = Str_NewStd(); | ||
Str_Set(lib->path, bundlePath); | ||
return lib; | ||
} | ||
|
||
void Library_Delete(Library *lib) | ||
{ | ||
assert(lib); | ||
if(lib->handle) | ||
{ | ||
dlclose(lib->handle); | ||
} | ||
Str_Delete(lib->path); | ||
free(lib); | ||
} | ||
|
||
void* Library_Symbol(Library* lib, const char* symbolName) | ||
{ | ||
assert(lib); | ||
void* ptr = dlsym(lib->handle, symbolName); | ||
if(!ptr) | ||
{ | ||
Str_Set(lastError, dlerror()); | ||
} | ||
return ptr; | ||
} | ||
|
||
const char* Library_LastError(void) | ||
{ | ||
return Str_Text(lastError); | ||
} | ||
|
||
void Library_AddSearchDir(const char *dir) | ||
{ | ||
/// @todo Implement this (and use it in the lookup) | ||
} | ||
|
||
int Library_IterateAvailableLibraries(int (*func)(const char *, void *), void *data) | ||
{ | ||
struct dirent* entry = NULL; | ||
filename_t bundlePath; | ||
DIR* dir = NULL; | ||
|
||
// This is the default location where bundles are. | ||
getBundlePath(bundlePath, FILENAME_T_MAXLEN); | ||
|
||
dir = opendir(bundlePath); | ||
if(!dir) | ||
{ | ||
Str_Set(lastError, strerror(errno)); | ||
printf("Library_IterateAvailableLibraries: Error opening \"%s\" (%s).\n", | ||
bundlePath, Library_LastError()); | ||
return 0; | ||
} | ||
|
||
while((entry = readdir(dir))) | ||
{ | ||
#ifdef MACOSX | ||
// Mac plugins are bundled in a subdir. | ||
if(entry->d_type != DT_REG && entry->d_type != DT_DIR) continue; | ||
if(!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) continue; | ||
#else | ||
if(entry->d_type != DT_REG) continue; | ||
#endif | ||
if(func(entry->d_name, data)) break; | ||
} | ||
closedir(dir); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.