darope@0: // vi: set ts=4 sw=4 : darope@0: // vim: set tw=75 : darope@0: darope@0: // mplugin.cpp - functions for individual plugin (class MPlugin) darope@0: darope@0: /* darope@0: * Copyright (c) 2001-2003 Will Day darope@0: * darope@0: * This file is part of Metamod. darope@0: * darope@0: * Metamod is free software; you can redistribute it and/or modify it darope@0: * under the terms of the GNU General Public License as published by the darope@0: * Free Software Foundation; either version 2 of the License, or (at darope@0: * your option) any later version. darope@0: * darope@0: * Metamod is distributed in the hope that it will be useful, but darope@0: * WITHOUT ANY WARRANTY; without even the implied warranty of darope@0: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU darope@0: * General Public License for more details. darope@0: * darope@0: * You should have received a copy of the GNU General Public License darope@0: * along with Metamod; if not, write to the Free Software Foundation, darope@0: * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA darope@0: * darope@0: * In addition, as a special exception, the author gives permission to darope@0: * link the code of this program with the Half-Life Game Engine ("HL darope@0: * Engine") and Modified Game Libraries ("MODs") developed by Valve, darope@0: * L.L.C ("Valve"). You must obey the GNU General Public License in all darope@0: * respects for all of the code used other than the HL Engine and MODs darope@0: * from Valve. If you modify this file, you may extend this exception darope@0: * to your version of the file, but you are not obligated to do so. If darope@0: * you do not wish to do so, delete this exception statement from your darope@0: * version. darope@0: * darope@0: */ darope@0: darope@0: #include // errno, etc darope@0: #include // malloc, etc darope@0: #include // stat darope@0: #include // stat darope@0: darope@0: #include // always darope@0: darope@0: #include "mplugin.h" // me darope@0: #include "metamod.h" // GameDLL, etc darope@0: #include "mreg.h" // MRegCmdList::show(int), etc darope@0: #include "mhook.h" // class MHook darope@0: #include "h_export.h" // GIVE_ENGINE_FUNCTIONS_FN, etc darope@0: #include "dllapi.h" // FN_GAMEINIT, etc darope@0: #include "support_meta.h" // full_gamedir_path, darope@0: #include "types_meta.h" // mBOOL darope@0: #include "log_meta.h" // logging functions, etc darope@0: #include "osdep.h" // win32 snprintf, is_absolute_path, darope@0: darope@0: darope@0: // Parse a line from plugins.ini into a plugin. darope@0: // meta_errno values: darope@0: // - ME_COMMENT ignored commented line darope@0: // - ME_FORMAT invalid line format darope@0: // - ME_OSNOTSUP plugin is not for this OS darope@0: mBOOL MPlugin::ini_parseline(char *line) { darope@0: char *token; darope@0: char *ptr_token; darope@0: char *cp; darope@0: darope@244: // skip whitespace at start of line darope@244: while(*line==' ' || *line=='\t' ) line++; darope@244: darope@245: // remove whitespace at end of line darope@245: cp = line + strlen(line) -1; darope@245: while(*cp==' ' || *cp=='\t') *cp--='\0'; darope@245: darope@244: // skip empty lines darope@244: if(line[0]=='\0') { darope@244: META_DEBUG(7, ("ini: Ignoring empty line: %s", line)); darope@244: RETURN_ERRNO(mFALSE, ME_BLANK); darope@244: } darope@244: darope@0: if(line[0]=='#' || line[0]==';' || strstr(line, "//")==line) { darope@0: META_DEBUG(7, ("ini: Ignoring commented line: %s", line)); darope@0: RETURN_ERRNO(mFALSE, ME_COMMENT); darope@0: } darope@0: darope@0: // grab platform ("win32" or "linux") darope@0: token=strtok_r(line, " \t", &ptr_token); darope@0: if(!token) darope@0: RETURN_ERRNO(mFALSE, ME_FORMAT); darope@244: if(strcasecmp(token, PLATFORM) == 0) { darope@244: pfspecific=0; darope@244: } else if(strcasecmp(token, PLATFORM_SPC) == 0) { darope@244: pfspecific=1; darope@244: } else { darope@0: // plugin is not for this OS darope@0: META_DEBUG(7, ("ini: Ignoring entry for %s", token)); darope@0: RETURN_ERRNO(mFALSE, ME_OSNOTSUP); darope@0: } darope@0: darope@0: // grab filename darope@0: token=strtok_r(NULL, " \t\r\n", &ptr_token); darope@0: if(!token) darope@0: RETURN_ERRNO(mFALSE, ME_FORMAT); darope@0: STRNCPY(filename, token, sizeof(filename)); darope@0: normalize_pathname(filename); darope@0: // Store name of just the actual _file_, without dir components. darope@0: cp=strrchr(filename, '/'); darope@0: if(cp) darope@0: file=cp+1; darope@0: else darope@0: file=filename; darope@0: darope@0: // Grab description. darope@0: // Just get the the rest of the line, minus line-termination. darope@0: token=strtok_r(NULL, "\n\r", &ptr_token); darope@0: if(token) { darope@0: token=token+strspn(token, " \t"); // skip whitespace darope@0: STRNCPY(desc, token, sizeof(desc)); darope@0: } darope@0: else { darope@0: // If no description is specified, temporarily use plugin file, darope@0: // until plugin can be queried, and desc replaced with info->name. darope@0: snprintf(desc, sizeof(desc), "<%s>", file); darope@0: } darope@0: darope@0: // Make full pathname (from gamedir if relative, remove "..", darope@0: // backslashes, etc). darope@0: full_gamedir_path(filename, pathname); darope@0: darope@0: source=PS_INI; darope@0: status=PL_VALID; darope@0: return(mTRUE); darope@0: } darope@0: darope@0: // Parse a line from console "load" command into a plugin. darope@0: // meta_errno values: darope@0: // - ME_FORMAT invalid line format darope@0: mBOOL MPlugin::cmd_parseline(const char *line) { darope@0: char buf[NAME_MAX + PATH_MAX + MAX_DESC_LEN]; darope@0: char *token; darope@0: char *ptr_token; darope@0: char *cp; darope@0: darope@0: STRNCPY(buf, line, sizeof(buf)); darope@0: darope@0: // remove "load" darope@0: token=strtok_r(buf, " \t", &ptr_token); darope@0: if(!token) darope@0: RETURN_ERRNO(mFALSE, ME_FORMAT); darope@0: darope@0: // grab filename darope@0: token=strtok_r(NULL, " \t", &ptr_token); darope@0: if(!token) darope@0: RETURN_ERRNO(mFALSE, ME_FORMAT); darope@0: STRNCPY(filename, token, sizeof(filename)); darope@0: normalize_pathname(filename); darope@0: // store name of just the actual _file_, without dir components darope@0: cp=strrchr(filename, '/'); darope@0: if(cp) darope@0: file=cp+1; darope@0: else darope@0: file=filename; darope@0: darope@0: // Grab description. darope@0: // Specify no delimiter chars, as we just want the rest of the line. darope@0: token=strtok_r(NULL, "", &ptr_token); darope@0: if(token) { darope@0: token=token+strspn(token, " \t"); // skip whitespace darope@0: STRNCPY(desc, token, sizeof(desc)); darope@0: } darope@0: else { darope@0: // if no description is specified, temporarily use plugin file, darope@0: // until plugin can be queried, and desc replaced with info->name. darope@0: snprintf(desc, sizeof(desc), "<%s>", file); darope@0: } darope@0: darope@0: // Make full pathname (from gamedir if relative, remove "..", darope@0: // backslashes, etc). darope@0: full_gamedir_path(filename, pathname); darope@0: darope@0: source=PS_CMD; darope@0: status=PL_VALID; darope@0: return(mTRUE); darope@0: } darope@0: darope@0: // Make sure this plugin has the necessary minimal information. darope@0: // meta_errno values: darope@0: // - ME_ARGUMENT missing necessary fields in plugin darope@0: mBOOL MPlugin::check_input(void) { darope@0: // doublecheck our input/state darope@0: if(status < PL_VALID) { darope@0: META_ERROR("dll: Tried to operate on plugin[%d] with a non-valid status (%d)", index, str_status()); darope@0: RETURN_ERRNO(mFALSE, ME_ARGUMENT); darope@0: } darope@0: if(!file || !file[0]) { darope@0: META_ERROR("dll: Tried to operate on plugin[%d] with an empty file", darope@0: index); darope@0: RETURN_ERRNO(mFALSE, ME_ARGUMENT); darope@0: } darope@0: if(!filename[0]) { darope@0: META_ERROR("dll: Tried to operate on plugin[%d] with an empty filename", darope@0: index); darope@0: RETURN_ERRNO(mFALSE, ME_ARGUMENT); darope@0: } darope@0: if(!pathname[0]) { darope@0: META_ERROR("dll: Tried to operate on plugin[%d] with an empty pathname", darope@0: index); darope@0: RETURN_ERRNO(mFALSE, ME_ARGUMENT); darope@0: } darope@0: if(!desc[0]) { darope@0: // if no description is specified, temporarily use plugin file, darope@0: // until plugin can be queried, and desc replaced with info->name. darope@0: snprintf(desc, sizeof(desc), "<%s>", file); darope@0: } darope@0: return(mTRUE); darope@0: } darope@0: darope@0: // Try to resolve a plugin's filename as a (possibly partial) path to an darope@0: // actual filename on disk, to facilitate easier console load-command darope@0: // arguments. Uses resolve_dirs, resolve_prefix, and resolve_suffix below. darope@0: // Example paths that it tries: darope@0: // filename darope@0: // Gamedir/filename.dll, Gamedir/filename.so darope@0: // Gamedir/filename_i386.so darope@0: // Gamedir/dlls/mm_filename_i386.so darope@0: // Gamedir/dlls/filename_mm_i386.so darope@0: // Gamedir/dlls/filename_MM_i386.so darope@0: // meta_errno values: darope@0: // - ME_NOTFOUND couldn't find a matching file for the partial name darope@0: // - errno's from check_input() darope@0: mBOOL MPlugin::resolve(void) { darope@0: char *found; darope@0: char *cp; darope@0: int len; darope@0: if(!check_input()) { darope@0: // details logged, meta_errno set in check_input() darope@0: return(mFALSE); darope@0: } darope@0: if(is_absolute_path(filename)) darope@0: found=resolve_prefix(filename); darope@0: else darope@0: found=resolve_dirs(filename); darope@0: darope@0: if(!found) { darope@0: META_DEBUG(2, ("Couldn't resolve '%s' to file", filename)); darope@0: RETURN_ERRNO(mFALSE, ME_NOTFOUND); darope@0: } darope@0: META_DEBUG(2, ("Resolved '%s' to file '%s'", filename, found)); darope@0: // store pathname: the resolved path (should be absolute) darope@0: STRNCPY(pathname, found, sizeof(pathname)); darope@0: // store file: the name of the file (without dir) darope@0: cp=strrchr(pathname, '/'); darope@0: if(cp) darope@0: file=cp+1; darope@0: else darope@0: file=pathname; darope@0: // store pathname: the gamedir relative path, or an absolute path darope@0: len=strlen(GameDLL.gamedir); darope@0: if(strncasecmp(pathname, GameDLL.gamedir, len) == 0) darope@0: STRNCPY(filename, pathname+len+1, sizeof(filename)); darope@0: else darope@0: STRNCPY(filename, pathname, sizeof(filename)); darope@0: darope@0: return(mTRUE); darope@0: } darope@0: darope@0: // For the given path, tries to find file in several possible darope@0: // directories. darope@0: // Try: darope@0: // GAMEDIR/filename darope@0: // GAMEDIR/dlls/filename darope@0: // meta_errno values: darope@0: // - none darope@0: char *MPlugin::resolve_dirs(char *path) { darope@0: struct stat st; darope@0: static char buf[PATH_MAX]; darope@0: char *found; darope@0: darope@0: snprintf(buf, sizeof(buf), "%s/%s", GameDLL.gamedir, path); darope@0: // try this path darope@0: if(stat(buf, &st) == 0 && S_ISREG(st.st_mode)) darope@0: return(buf); darope@0: // try other file prefixes in this path darope@0: if((found=resolve_prefix(buf))) darope@0: return(found); darope@0: darope@0: snprintf(buf, sizeof(buf), "%s/dlls/%s", GameDLL.gamedir, path); darope@0: // try this path darope@0: if(stat(buf, &st) == 0 && S_ISREG(st.st_mode)) darope@0: return(buf); darope@0: // try other file prefixes for this path darope@0: if((found=resolve_prefix(buf))) darope@0: return(found); darope@0: darope@0: return(NULL); darope@0: } darope@0: darope@0: // For the given path, tries several possible filename prefixes. darope@0: // Try: darope@0: // dir/mm_file darope@0: // dir/file darope@0: // meta_errno values: darope@0: // - none darope@0: char *MPlugin::resolve_prefix(char *path) { darope@0: struct stat st; darope@0: char *cp, *fname; darope@0: char dname[PATH_MAX]; darope@0: static char buf[PATH_MAX]; darope@0: char *found; darope@0: darope@0: // try "mm_" prefix FIRST. darope@0: // split into dirname and filename darope@0: STRNCPY(dname, path, sizeof(dname)); darope@0: cp=strrchr(dname, '/'); darope@0: if(cp) { darope@0: *cp='\0'; darope@0: fname=cp+1; darope@0: snprintf(buf, sizeof(buf), "%s/mm_%s", dname, fname); darope@0: } darope@0: else { darope@0: // no directory in given path darope@0: snprintf(buf, sizeof(buf), "mm_%s", path); darope@0: } darope@0: // try this path darope@0: if(stat(buf, &st) == 0 && S_ISREG(st.st_mode)) darope@0: return(buf); darope@0: // try other suffixes for this path darope@0: if((found=resolve_suffix(buf))) darope@0: return(found); darope@0: darope@0: // try other suffixes for the original path darope@0: if((found=resolve_suffix(path))) darope@0: return(found); darope@0: darope@0: return(NULL); darope@0: } darope@0: darope@0: // For the given path, tries several different filename suffixes. darope@0: // Try: darope@0: // path darope@0: // path_mm darope@0: // path_MM darope@0: // path.so (linux), path.dll (win32) darope@0: // path_i386.so, path_i486.so, etc (if linux) darope@0: // meta_errno values: darope@0: // - none darope@0: char *MPlugin::resolve_suffix(char *path) { darope@0: struct stat st; darope@0: static char buf[PATH_MAX]; darope@0: char *found; darope@0: darope@0: // Hmm, recursion. darope@0: if(!strstr(path, "_mm")) { darope@0: char *tmpbuf; darope@0: snprintf(buf, sizeof(buf), "%s_mm", path); darope@0: tmpbuf=strdup(buf); darope@0: found=resolve_suffix(tmpbuf); darope@0: free(tmpbuf); darope@0: if(found) return(found); darope@0: } darope@0: if(!strstr(path, "_MM")) { darope@0: char *tmpbuf; darope@0: snprintf(buf, sizeof(buf), "%s_MM", path); darope@0: tmpbuf=strdup(buf); darope@0: found=resolve_suffix(tmpbuf); darope@0: free(tmpbuf); darope@0: if(found) return(found); darope@0: } darope@0: darope@0: #ifdef _WIN32 darope@0: snprintf(buf, sizeof(buf), "%s.dll", path); darope@0: #elif defined(linux) darope@0: snprintf(buf, sizeof(buf), "%s.so", path); darope@0: #else darope@0: #error "OS unrecognized" darope@0: #endif /* _WIN32 */ darope@0: if(stat(buf, &st) == 0 && S_ISREG(st.st_mode)) darope@0: return(buf); darope@0: darope@0: #ifdef linux darope@0: snprintf(buf, sizeof(buf), "%s_i386.so", path); darope@0: if(stat(buf, &st) == 0 && S_ISREG(st.st_mode)) darope@0: return(buf); darope@0: snprintf(buf, sizeof(buf), "%s_i486.so", path); darope@0: if(stat(buf, &st) == 0 && S_ISREG(st.st_mode)) darope@0: return(buf); darope@0: snprintf(buf, sizeof(buf), "%s_i586.so", path); darope@0: if(stat(buf, &st) == 0 && S_ISREG(st.st_mode)) darope@0: return(buf); darope@0: snprintf(buf, sizeof(buf), "%s_i686.so", path); darope@0: if(stat(buf, &st) == 0 && S_ISREG(st.st_mode)) darope@0: return(buf); darope@244: snprintf(buf, sizeof(buf), "%s_amd64.so", path); darope@244: if(stat(buf, &st) == 0 && S_ISREG(st.st_mode)) darope@244: return(buf); darope@0: #endif /* linux */ darope@0: darope@0: return(NULL); darope@0: } darope@0: darope@246: darope@246: // Check if a passed string starts with a known platform postfix. darope@246: // It does not check beyond the period in order to work for both darope@246: // Linux and Win32. darope@246: static mBOOL is_platform_postfix(char* pf) darope@246: { darope@246: if ( NULL == pf ) return mFALSE; darope@246: darope@246: if ( 0 == strncmp(pf, "_i386.", 6) ) return mTRUE; darope@246: if ( 0 == strncmp(pf, "_i486.", 6) ) return mTRUE; darope@246: if ( 0 == strncmp(pf, "_i586.", 6) ) return mTRUE; darope@246: if ( 0 == strncmp(pf, "_i686.", 6) ) return mTRUE; darope@246: if ( 0 == strncmp(pf, "_amd64.", 7) ) return mTRUE; darope@246: darope@246: return mFALSE; darope@246: } darope@246: darope@246: darope@244: // Check if a given plugin is the same but possibly for a darope@244: // different platform. A match is considered to be found if darope@244: // 1. the filename without the path is the same, or darope@244: // 2a. for an attached plugin the logtag is the same, or darope@244: // 2b. the description is the same, or darope@244: // 3. a significant part of the filename is the same. darope@246: // A significant part of a plugin name is currently defined to: darope@246: // the part up to a known platform postfix as determined by darope@246: // the is_platform_postfix() function (see above), or darope@246: // the part up to the last dot, if one exists. darope@244: // meta_errno values: darope@244: // - none darope@244: mBOOL MPlugin::platform_match(MPlugin* other) { darope@244: char *end, *other_end; darope@244: int prefixlen; darope@244: darope@244: if(status == PL_EMPTY || other->status == PL_EMPTY) return mFALSE; darope@244: darope@244: if(strcmp(file, other->file) == 0) return mTRUE; darope@244: darope@244: if(status >= PL_OPENED && other->status >= PL_OPENED && darope@244: strcmp(info->logtag, other->info->logtag) == 0 ) return mTRUE; darope@244: if(*desc != '\0' && strcasecmp(desc,other->desc) == 0) return mTRUE; darope@244: darope@244: end=strrchr(file, '_'); darope@246: if(end == NULL || !is_platform_postfix(end)) end=strrchr(file, '.'); darope@244: other_end=strrchr(other->file, '_'); darope@246: if(other_end == NULL || !is_platform_postfix(other_end)) other_end=strrchr(other->file, '.'); darope@244: darope@244: if(end == NULL || other_end == NULL) return mFALSE; darope@244: darope@244: prefixlen=end-file; darope@244: if((other_end-other->file) != prefixlen) return mFALSE; darope@244: darope@244: if(strncmp(file,other->file, prefixlen) == 0) return mTRUE; darope@244: darope@244: return mFALSE; darope@244: } darope@244: darope@244: darope@0: // Load a plugin; query, check allowed time, attach. darope@0: // meta_errno values: darope@0: // - ME_ARGUMENT missing necessary fields in plugin darope@0: // - ME_ALREADY this plugin already loaded darope@0: // - ME_BADREQ plugin not marked for load darope@0: // - ME_DELAYED load request is delayed (till changelevel?) darope@0: // - ME_NOTALLOWED plugin not loadable after startup darope@0: // - errno's from query() darope@0: // - errno's from attach() darope@0: // - errno's from check_input() darope@0: mBOOL MPlugin::load(PLUG_LOADTIME now) { darope@0: if(!check_input()) { darope@0: // details logged, meta_errno set in check_input() darope@0: RETURN_ERRNO(mFALSE, ME_ARGUMENT); darope@0: } darope@0: if(status >= PL_RUNNING) { darope@0: META_ERROR("dll: Not loading plugin '%s'; already loaded (status=%s)", darope@0: desc, str_status()); darope@0: RETURN_ERRNO(mFALSE, ME_ALREADY); darope@0: } darope@0: if(action != PA_LOAD && action != PA_ATTACH) { darope@0: META_ERROR("dll: Not loading plugin '%s'; not marked for load (action=%s)", desc, str_action()); darope@0: RETURN_ERRNO(mFALSE, ME_BADREQ); darope@0: } darope@0: darope@0: if(status != PL_OPENED) { darope@0: // query plugin; open file and get info about it darope@0: if(!query()) { darope@0: META_ERROR("dll: Skipping plugin '%s'; couldn't query", desc); darope@0: if(meta_errno != ME_DLOPEN) { darope@0: if(DLCLOSE(handle) != 0) { darope@0: META_ERROR("dll: Couldn't close plugin file '%s': %s", darope@0: file, DLERROR()); darope@0: } darope@0: else darope@0: handle=NULL; darope@0: } darope@0: status=PL_BADFILE; darope@0: // meta_errno should be already set in query() darope@0: return(mFALSE); darope@0: } darope@0: status=PL_OPENED; darope@0: } darope@0: darope@0: // are we allowed to attach this plugin at this time? darope@0: if(info->loadable < now) { darope@0: if(info->loadable > PT_STARTUP) { darope@0: // will try to attach again at next opportunity darope@0: META_DEBUG(2, ("dll: Delaying load plugin '%s'; can't attach now: allowed=%s; now=%s", darope@0: desc, str_loadable(), str_loadtime(now, SL_SIMPLE))); darope@0: RETURN_ERRNO(mFALSE, ME_DELAYED); darope@0: } darope@0: else { darope@0: META_DEBUG(2, ("dll: Failed load plugin '%s'; can't attach now: allowed=%s; now=%s", darope@0: desc, str_loadable(), str_loadtime(now, SL_SIMPLE))); darope@0: // don't try to attach again later darope@0: action=PA_NONE; darope@0: RETURN_ERRNO(mFALSE, ME_NOTALLOWED); darope@0: } darope@0: } darope@0: darope@0: // attach plugin; get function tables darope@0: if(attach(now) != mTRUE) { darope@0: META_ERROR("dll: Failed to attach plugin '%s'", desc); darope@0: // Note we don't dlclose() here, since we're returning PL_FAILED, darope@0: // which implies that it's been dlopened and queried successfully. darope@0: // Doing so causes crashes, because things like "meta list" try to darope@0: // look at *info, which is in the DLL memory space and unaccessible darope@0: // (segfault) after dlclosed. darope@0: status=PL_FAILED; darope@0: // meta_errno should be already set in attach() darope@0: return(mFALSE); darope@0: } darope@0: status=PL_RUNNING; darope@0: action=PA_NONE; darope@0: darope@0: // If not loading at server startup, then need to call plugin's darope@0: // GameInit, since we've passed that. darope@0: if(now != PT_STARTUP) { darope@0: FN_GAMEINIT pfn_gameinit=NULL; darope@0: if(dllapi_table && (pfn_gameinit=dllapi_table->pfnGameInit)) darope@0: pfn_gameinit(); darope@0: } darope@0: // If loading during map, then I'd like to call plugin's darope@0: // ServerActivate, since we've passed that. However, I'm not sure what darope@0: // arguments to give it... So, we'll just have to say for the darope@0: // moment that plugins that are loadable during a map can't need to darope@0: // hook ServerActivate. darope@0: darope@0: META_LOG("dll: Loaded plugin '%s': %s v%s %s, %s", desc, info->name, darope@0: info->version, info->date, info->author); darope@0: return(mTRUE); darope@0: } darope@0: darope@0: // Query a plugin: darope@0: // - dlopen() the file, store the handle darope@0: // - dlsym() and call: darope@0: // Meta_Init (if present) - tell dll it'll be used as a metamod plugin darope@0: // GiveFnptrsToDll - give engine function ptrs darope@0: // Meta_Query - say "hi" and get info about plugin darope@0: // meta_errno values: darope@0: // - ME_DLOPEN dlopen/loadlibrary failed; see dlerror() for details darope@0: // - ME_DLMISSING couldn't find a query() or giveFuncs() in plugin darope@0: // - ME_DLERROR plugin query() returned error darope@0: // - ME_NULLDATA info struct from query() was null darope@0: mBOOL MPlugin::query(void) { darope@0: META_INIT_FN pfn_init; darope@0: GIVE_ENGINE_FUNCTIONS_FN pfn_give_engfuncs; darope@0: META_QUERY_FN pfn_query; darope@0: darope@0: // open the plugin DLL darope@0: if(!(handle=DLOPEN(pathname))) { darope@0: META_ERROR("dll: Failed query plugin '%s'; Couldn't open file '%s': %s", darope@0: desc, pathname, DLERROR()); darope@0: RETURN_ERRNO(mFALSE, ME_DLOPEN); darope@0: } darope@0: darope@0: // First, we check to see if they have a Meta_Query. We would normally darope@0: // dlsym this just prior to calling it, after having called darope@0: // GiveFnptrsToDll, but things like standalone-DLL bots do all their darope@0: // startup in GiveFnptrsToDll, and trying to load them as metamod darope@0: // plugins creates all sorts of crashes. So, we do a trivial check darope@0: // first to see if the DLL looks like a metamod plugin before darope@0: // proceeding with the normal order. Note that we still have to call darope@0: // GiveFnptrsToDll before Meta_Query, because the latter typically uses darope@0: // engine functions like AlertMessage, which have to be passed along via darope@0: // GiveFnptrsToDll. darope@0: pfn_query = (META_QUERY_FN) DLSYM(handle, "Meta_Query"); darope@0: if(!pfn_query) { darope@0: META_ERROR("dll: Failed query plugin '%s'; Couldn't find Meta_Query(): %s", desc, DLERROR()); darope@0: // caller will dlclose() darope@0: RETURN_ERRNO(mFALSE, ME_DLMISSING); darope@0: } darope@0: darope@0: // Call Meta_Init, if present. This is an optional plugin routine to darope@0: // allow plugin to do any special initializing or other processing darope@0: // prior to the standard query/attach procedure. darope@0: // darope@0: // In particular, this should allow for DLL's that can operate as both darope@0: // a standalone dll AND a metamod plugin. This routine has to be darope@0: // called _before_ GiveFnptrsToDll, since the dll will usually do all darope@0: // its startup processing in GiveFn, and has to know at that point darope@0: // whether it needs to do just mm plugin startup, or all the extra darope@0: // startup needed for a standalone DLL. darope@0: // darope@0: // This passes nothing and returns nothing, and the routine in the darope@0: // plugin can NOT use any Engine functions, as they haven't been darope@0: // provided yet (done next, in GiveFnptrsToDll). darope@0: pfn_init = (META_INIT_FN) DLSYM(handle, "Meta_Init"); darope@0: if(pfn_init) { darope@0: pfn_init(); darope@0: META_DEBUG(6, ("dll: Plugin '%s': Called Meta_Init()", desc)); darope@0: } darope@0: else { darope@0: META_DEBUG(5, ("dll: no Meta_Init present in plugin '%s'", desc)); darope@0: // don't return; not an error darope@0: } darope@0: darope@0: // pass on engine function table and globals to plugin darope@0: if(!(pfn_give_engfuncs = (GIVE_ENGINE_FUNCTIONS_FN) DLSYM(handle, "GiveFnptrsToDll"))) { darope@0: // META_ERROR("dll: Couldn't find GiveFnptrsToDll() in plugin '%s': %s", desc, DLERROR()); darope@0: META_ERROR("dll: Failed query plugin '%s'; Couldn't find GiveFnptrsToDll(): %s", desc, DLERROR()); darope@0: // caller will dlclose() darope@0: RETURN_ERRNO(mFALSE, ME_DLMISSING); darope@0: } darope@0: pfn_give_engfuncs(Engine.pl_funcs, Engine.globals); darope@0: META_DEBUG(6, ("dll: Plugin '%s': Called GiveFnptrsToDll()", desc)); darope@0: darope@0: // Call plugin's Meta_Query(), to pass our meta interface version, and get darope@0: // plugin's info structure. darope@0: meta_errno=ME_NOERROR; darope@0: info=NULL; darope@0: // Make a copy of the meta_util function table for each plugin, for the darope@0: // same reason. darope@0: memcpy(&mutil_funcs, &MetaUtilFunctions, sizeof(mutil_funcs)); darope@0: darope@0: if(pfn_query(META_INTERFACE_VERSION, &info, &mutil_funcs) != TRUE) { darope@0: META_ERROR("dll: Failed query plugin '%s'; Meta_Query returned error", darope@0: desc); darope@0: meta_errno=ME_DLERROR; darope@0: } darope@0: else { darope@0: META_DEBUG(6, ("dll: Plugin '%s': Called Meta_Query() successfully", darope@0: desc)); darope@0: } darope@0: darope@0: // Check for interface differences... Generally, a difference in major darope@0: // version will be incompatible, and a plugin that expects a later darope@0: // minor version will be incompatible (it's expecting things that this darope@0: // Metamod won't be supplying). Plugins that use an older minor darope@0: // version will still work, as backward-compability within major darope@0: // version is expected (forward-compatibility is not). darope@0: // darope@0: // Note, this check is done regardless of whether meta_query returns an darope@0: // error. darope@0: if(info && !FStrEq(info->ifvers, META_INTERFACE_VERSION)) { darope@0: int mmajor=0, mminor=0, pmajor=0, pminor=0; darope@0: META_DEBUG(3, ("dll: Note: Plugin '%s' interface version didn't match; expected %s, found %s", desc, META_INTERFACE_VERSION, info->ifvers)); darope@0: sscanf(META_INTERFACE_VERSION, "%d:%d", &mmajor, &mminor); darope@0: sscanf(info->ifvers, "%d:%d", &pmajor, &pminor); darope@0: // If plugin has later interface version, it's incompatible darope@0: // (update metamod). darope@0: if(pmajor > mmajor || (pmajor==mmajor && pminor > mminor)) { darope@0: META_ERROR("dll: Plugin '%s' requires a newer version of Metamod (Metamod needs at least interface %s not the current %s)", desc, info->ifvers, META_INTERFACE_VERSION); darope@0: meta_errno=ME_IFVERSION; darope@0: } darope@0: // If plugin has older major interface version, it's incompatible darope@0: // (update plugin). darope@0: else if(pmajor < mmajor) { darope@0: META_ERROR("dll: Plugin '%s' is out of date and incompatible with this version of Metamod; please find a newer version of the plugin (plugin needs at least interface %s not the current %s)", desc, META_INTERFACE_VERSION, info->ifvers); darope@0: meta_errno=ME_IFVERSION; darope@0: } darope@0: // Plugin has identical major, with older minor. This is supposed to be darope@0: // backwards compatible, so we warn, but we still accept it. darope@0: else if(pmajor==mmajor && pminor < mminor) darope@0: META_LOG("dll: Note: plugin '%s' is using an older interface version (%s), not the latest interface version (%s); there might be an updated version of the plugin", desc, info->ifvers, META_INTERFACE_VERSION); darope@0: else darope@0: META_LOG("dll: Plugin '%s': unexpected version comparision; metavers=%s, mmajor=%d, mminor=%d; plugvers=%s, pmajor=%d, pminor=%d", darope@0: desc, META_INTERFACE_VERSION, mmajor, mminor, info->ifvers, pmajor, pminor); darope@0: } darope@0: darope@0: if(meta_errno == ME_IFVERSION) { darope@0: META_ERROR("dll: Rejected plugin '%s' due to interface version incompatibility (mm=%s, pl=%s)", desc, META_INTERFACE_VERSION, info->ifvers); darope@0: // meta_errno is set already above darope@0: // caller will dlclose() darope@0: return(mFALSE); darope@0: } darope@0: else if(meta_errno != ME_NOERROR) darope@0: // some other error, already logged darope@0: return(mFALSE); darope@0: darope@0: if(!info) { darope@0: META_ERROR("dll: Failed query plugin '%s'; Empty info structure", desc); darope@0: // caller will dlclose() darope@0: RETURN_ERRNO(mFALSE, ME_NULLRESULT); darope@0: } darope@0: // Replace temporary desc with plugin's internal name. darope@0: if(desc[0] == '<') darope@0: STRNCPY(desc, info->name, sizeof(desc)); darope@0: META_DEBUG(6, ("dll: Plugin '%s': Query successful", desc)); darope@0: return(mTRUE); darope@0: } darope@0: darope@0: // Attach a plugin: darope@0: // - dlsym() and call: darope@0: // Meta_Attach - get table of api tables, give meta_globals darope@0: // - if provided by plugin, call various "Get" function pointers, darope@0: // and store resulting function tables: darope@0: // GetEntityAPI (std) darope@0: // GetEntityAPI2 (std sdk2) darope@0: // GetNewDLLFunctions (std sdk2) darope@0: // darope@0: // GetEntityAPI_Post (meta) darope@0: // GetEntityAPI2_Post (meta) darope@0: // GetNewDLLFunctions_Post (meta) darope@0: // darope@0: // GetEngineFunctions (meta) darope@0: // GetEngineFunctions_Post (meta) darope@0: // meta_errno values: darope@0: // - ME_DLMISSING couldn't find meta_attach() in plugin darope@0: // - ME_DLERROR plugin attach() returned error darope@0: // - ME_NOMEM failed malloc darope@0: mBOOL MPlugin::attach(PLUG_LOADTIME now) { darope@0: int ret; darope@0: int iface_vers; darope@0: darope@0: META_ATTACH_FN pfn_attach; darope@0: META_FUNCTIONS meta_table; darope@0: darope@0: // Make copy of gameDLL's function tables for each plugin, so we don't darope@0: // risk the plugins screwing with the tables everyone uses. darope@0: if(GameDLL.funcs.dllapi_table && !gamedll_funcs.dllapi_table) { darope@247: gamedll_funcs.dllapi_table = (DLL_FUNCTIONS *) calloc(1, sizeof(DLL_FUNCTIONS)); darope@0: if(!gamedll_funcs.dllapi_table) { darope@0: META_ERROR("dll: Failed attach plugin '%s': Failed malloc() for dllapi_table"); darope@0: RETURN_ERRNO(mFALSE, ME_NOMEM); darope@0: } darope@0: memcpy(gamedll_funcs.dllapi_table, GameDLL.funcs.dllapi_table, darope@0: sizeof(DLL_FUNCTIONS)); darope@0: } darope@0: if(GameDLL.funcs.newapi_table && !gamedll_funcs.newapi_table) { darope@0: gamedll_funcs.newapi_table = (NEW_DLL_FUNCTIONS *) malloc(sizeof(NEW_DLL_FUNCTIONS)); darope@0: if(!gamedll_funcs.newapi_table) { darope@0: META_ERROR("dll: Failed attach plugin '%s': Failed malloc() for newapi_table"); darope@0: RETURN_ERRNO(mFALSE, ME_NOMEM); darope@0: } darope@0: memcpy(gamedll_funcs.newapi_table, GameDLL.funcs.newapi_table, darope@0: sizeof(NEW_DLL_FUNCTIONS)); darope@0: } darope@0: if(!(pfn_attach = (META_ATTACH_FN) DLSYM(handle, "Meta_Attach"))) { darope@0: META_ERROR("dll: Failed attach plugin '%s': Couldn't find Meta_Attach(): %s", desc, DLERROR()); darope@0: // caller will dlclose() darope@0: RETURN_ERRNO(mFALSE, ME_DLMISSING); darope@0: } darope@0: darope@0: memset(&meta_table, 0, sizeof(meta_table)); darope@0: // get table of function tables, darope@0: // give public meta globals darope@0: ret=pfn_attach(now, &meta_table, &PublicMetaGlobals, &gamedll_funcs); darope@0: if(ret != TRUE) { darope@0: META_ERROR("dll: Failed attach plugin '%s': Error from Meta_Attach(): %d", desc, ret); darope@0: // caller will dlclose() darope@0: RETURN_ERRNO(mFALSE, ME_DLERROR); darope@0: } darope@0: META_DEBUG(6, ("dll: Plugin '%s': Called Meta_Attach() successfully", desc)); darope@0: darope@0: // Rather than duplicate code, we use another ugly macro. Again, darope@0: // a function isn't an option since we have varying types. darope@0: #define GET_FUNC_TABLE_FROM_PLUGIN(pfnGetFuncs, STR_GetFuncs, struct_field, API_TYPE, TABLE_TYPE, vers_pass, vers_int, vers_want) \ darope@0: if(meta_table.pfnGetFuncs) { \ darope@247: if (!struct_field) \ darope@247: struct_field = (TABLE_TYPE*) calloc(1, sizeof(TABLE_TYPE)); \ darope@0: if(meta_table.pfnGetFuncs(struct_field, vers_pass)) { \ darope@0: META_DEBUG(3, ("dll: Plugin '%s': Found %s", desc, STR_GetFuncs)); \ darope@0: } \ darope@0: else { \ darope@0: META_ERROR("dll: Failure calling %s in plugin '%s'", STR_GetFuncs, desc); \ darope@0: if(vers_int != vers_want) \ darope@0: META_ERROR("dll: Interface version didn't match; expected %d, found %d", vers_want, vers_int); \ darope@0: } \ darope@0: } \ darope@0: else { \ darope@0: META_DEBUG(5, ("dll: Plugin '%s': No %s", desc, STR_GetFuncs)); \ darope@247: if (struct_field) \ darope@247: free(struct_field); \ darope@0: struct_field=NULL; \ darope@0: } darope@0: darope@0: // Look for API-NEW interface in plugin. We do this before API2/API, because darope@0: // that's what the engine appears to do.. darope@0: // But we only do this if the gamedll provides these, so that we don't darope@0: // give a plugin the idea that we'll call them, when in fact we won't darope@0: // (since we don't export them to the engine when the gamedll doesn't darope@0: // provide them). darope@0: if(GameDLL.funcs.newapi_table) { darope@0: iface_vers=NEW_DLL_FUNCTIONS_VERSION; darope@0: GET_FUNC_TABLE_FROM_PLUGIN(pfnGetNewDLLFunctions, darope@0: "GetNewDLLFunctions", newapi_table, darope@0: NEW_DLL_FUNCTIONS_FN, NEW_DLL_FUNCTIONS, darope@0: &iface_vers, iface_vers, NEW_DLL_FUNCTIONS_VERSION); darope@0: iface_vers=NEW_DLL_FUNCTIONS_VERSION; darope@0: GET_FUNC_TABLE_FROM_PLUGIN(pfnGetNewDLLFunctions_Post, darope@0: "GetNewDLLFunctions_Post", newapi_post_table, darope@0: NEW_DLL_FUNCTIONS_FN, NEW_DLL_FUNCTIONS, darope@0: &iface_vers, iface_vers, NEW_DLL_FUNCTIONS_VERSION); darope@0: } darope@0: darope@0: // Look for API2 interface in plugin; preferred over API-1. darope@0: iface_vers=INTERFACE_VERSION; darope@0: GET_FUNC_TABLE_FROM_PLUGIN(pfnGetEntityAPI2, darope@0: "GetEntityAPI2", dllapi_table, darope@0: APIFUNCTION2, DLL_FUNCTIONS, darope@0: &iface_vers, iface_vers, INTERFACE_VERSION); darope@0: iface_vers=INTERFACE_VERSION; darope@0: GET_FUNC_TABLE_FROM_PLUGIN(pfnGetEntityAPI2_Post, darope@0: "GetEntityAPI2_Post", dllapi_post_table, darope@0: APIFUNCTION2, DLL_FUNCTIONS, darope@0: &iface_vers, iface_vers, INTERFACE_VERSION); darope@0: darope@0: // Look for old-style API in plugin, if API2 interface wasn't found. darope@0: if(!dllapi_table && !dllapi_post_table) { darope@0: GET_FUNC_TABLE_FROM_PLUGIN(pfnGetEntityAPI, darope@0: "GetEntityAPI", dllapi_table, darope@0: APIFUNCTION, DLL_FUNCTIONS, darope@0: INTERFACE_VERSION, INTERFACE_VERSION, INTERFACE_VERSION); darope@0: GET_FUNC_TABLE_FROM_PLUGIN(pfnGetEntityAPI_Post, darope@0: "GetEntityAPI_Post", dllapi_post_table, darope@0: APIFUNCTION, DLL_FUNCTIONS, darope@0: INTERFACE_VERSION, INTERFACE_VERSION, INTERFACE_VERSION); darope@0: } darope@0: darope@0: // Look for Engine interface. darope@0: iface_vers=ENGINE_INTERFACE_VERSION; darope@0: GET_FUNC_TABLE_FROM_PLUGIN(pfnGetEngineFunctions, darope@0: "GetEngineFunctions", engine_table, darope@0: GET_ENGINE_FUNCTIONS_FN, enginefuncs_t, darope@0: &iface_vers, iface_vers, ENGINE_INTERFACE_VERSION); darope@0: iface_vers=ENGINE_INTERFACE_VERSION; darope@0: GET_FUNC_TABLE_FROM_PLUGIN(pfnGetEngineFunctions_Post, darope@0: "GetEngineFunctions_Post", engine_post_table, darope@0: GET_ENGINE_FUNCTIONS_FN, enginefuncs_t, darope@0: &iface_vers, iface_vers, ENGINE_INTERFACE_VERSION); darope@0: darope@0: if(!dllapi_table && !dllapi_post_table darope@0: && !newapi_table && !newapi_post_table darope@0: && !engine_table && !engine_post_table) darope@0: { darope@0: META_LOG("dll: Plugin '%s' isn't catching _any_ functions ??", desc); darope@0: } darope@0: darope@0: time_loaded=time(NULL); darope@0: return(mTRUE); darope@0: } darope@0: darope@0: // Unload a plugin. Check time, detach. darope@0: // meta_errno values: darope@0: // - ME_ARGUMENT missing necessary fields in plugin darope@0: // - ME_ALREADY this plugin already unloaded darope@0: // - ME_BADREQ plugin not marked for unload darope@0: // - ME_DELAYED unload request is delayed (till changelevel?) darope@0: // - ME_NOTALLOWED plugin not unloadable after startup darope@0: // - errno's from check_input() darope@0: mBOOL MPlugin::unload(PLUG_LOADTIME now, PL_UNLOAD_REASON reason) { darope@0: if(!check_input()) { darope@0: // details logged, meta_errno set in check_input() darope@0: RETURN_ERRNO(mFALSE, ME_ARGUMENT); darope@0: } darope@0: if(status < PL_RUNNING) { darope@0: META_ERROR("dll: Not unloading plugin '%s'; already unloaded (status=%s)", desc, str_status()); darope@0: RETURN_ERRNO(mFALSE, ME_ALREADY); darope@0: } darope@0: if(action != PA_UNLOAD && action != PA_RELOAD) { darope@0: META_ERROR("dll: Not unloading plugin '%s'; not marked for unload (action=%s)", desc, str_action()); darope@0: RETURN_ERRNO(mFALSE, ME_BADREQ); darope@0: } darope@0: darope@0: // Are we allowed to detach this plugin at this time? darope@0: // If forcing unload, we disregard when plugin wants to be unloaded. darope@0: if(info->unloadable < now) { darope@0: if(reason == PNL_CMD_FORCED) { darope@0: META_DEBUG(2, ("dll: Forced unload plugin '%s' overriding allowed times: allowed=%s; now=%s", desc, str_unloadable(), str_loadtime(now, SL_SIMPLE))); darope@0: } darope@0: else { darope@0: if(info->unloadable > PT_STARTUP) { darope@0: META_DEBUG(2, ("dll: Delaying unload plugin '%s'; can't detach now: allowed=%s; now=%s", desc, str_unloadable(), str_loadtime(now, SL_SIMPLE))); darope@0: // caller should give message to user darope@0: // try to unload again at next opportunity darope@0: RETURN_ERRNO(mFALSE, ME_DELAYED); darope@0: } darope@0: else { darope@0: META_DEBUG(2, ("dll: Failed unload plugin '%s'; can't detach now: allowed=%s; now=%s", desc, str_unloadable(), str_loadtime(now, SL_SIMPLE))); darope@0: // don't try to unload again later darope@0: action=PA_NONE; darope@0: RETURN_ERRNO(mFALSE, ME_NOTALLOWED); darope@0: } darope@0: } darope@0: } darope@0: darope@0: // If unloading during map, then I'd like to call plugin's darope@0: // ServerDeactivate. However, I don't want to do this until I start darope@0: // calling ServerActivate when loading during map, since the SDK darope@0: // indicates these two routines should match call for call. darope@0: darope@0: // detach plugin darope@0: if(!detach(now, reason)) { darope@0: if(reason == PNL_CMD_FORCED) { darope@0: META_DEBUG(2, ("dll: Forced unload plugin '%s' overriding failed detach")); darope@0: } darope@0: else { darope@0: META_ERROR("dll: Failed to detach plugin '%s'; ", desc); darope@0: // meta_errno should be already set in detach() darope@0: return(mFALSE); darope@0: } darope@0: } darope@0: darope@0: // successful detach, or forced unload darope@0: darope@0: // Unmark registered commands for this plugin (by index number). darope@0: RegCmds->disable(index); darope@0: // Unmark registered cvars for this plugin (by index number). darope@0: RegCvars->disable(index); darope@0: darope@0: #ifdef UNFINISHED darope@0: // Remove all requested hooks from this plugin (by index number). darope@0: Hooks->remove_all(info); darope@0: #endif /* UNFINISHED */ darope@0: darope@0: // Close the file. Note: after this, attempts to reference any memory darope@0: // locations in the file will produce a segfault. darope@0: if(DLCLOSE(handle) != 0) { darope@0: META_ERROR("dll: Couldn't close plugin file '%s': %s", file, DLERROR()); darope@0: status=PL_FAILED; darope@0: RETURN_ERRNO(mFALSE, ME_DLERROR); darope@0: } darope@0: handle=NULL; darope@0: darope@0: if(action==PA_UNLOAD) { darope@0: status=PL_EMPTY; darope@0: clear(); darope@0: } darope@0: else if(action==PA_RELOAD) { darope@0: status=PL_VALID; darope@0: action=PA_LOAD; darope@247: clear(); darope@0: } darope@0: META_LOG("dll: Unloaded plugin '%s' for reason '%s'", desc, str_reason(reason)); darope@0: return(mTRUE); darope@0: } darope@0: darope@0: // Inform plugin we're going to unload it. darope@0: // meta_errno values: darope@0: // - ME_DLMISSING couldn't find meta_detach() in plugin darope@0: // - ME_DLERROR plugin detach() returned error darope@0: mBOOL MPlugin::detach(PLUG_LOADTIME now, PL_UNLOAD_REASON reason) { darope@0: int ret; darope@0: META_DETACH_FN pfn_detach; darope@0: darope@0: if(!(pfn_detach = (META_DETACH_FN) DLSYM(handle, "Meta_Detach"))) { darope@0: META_ERROR("dll: Error detach plugin '%s': Couldn't find Meta_detach(): %s", desc, DLERROR()); darope@0: // caller will dlclose() darope@0: RETURN_ERRNO(mFALSE, ME_DLMISSING); darope@0: } darope@0: darope@0: ret=pfn_detach(now, reason); darope@0: if(ret != TRUE) { darope@0: META_ERROR("dll: Failed detach plugin '%s': Error from Meta_Detach(): %d", desc, ret); darope@0: RETURN_ERRNO(mFALSE, ME_DLERROR); darope@0: } darope@0: META_DEBUG(6, ("dll: Plugin '%s': Called Meta_Detach() successfully", desc)); darope@0: return(mTRUE); darope@0: } darope@0: darope@0: // Reload a plugin; unload and load again. darope@0: // meta_errno values: darope@246: // - ME_NOTALLOWED plugin not loadable after startup darope@246: // - errno's from check_input() darope@0: // - errno's from unload() darope@0: // - errno's from load() darope@0: mBOOL MPlugin::reload(PLUG_LOADTIME now, PL_UNLOAD_REASON reason) { darope@246: if(!check_input()) { darope@246: // details logged, meta_errno set in check_input() darope@246: RETURN_ERRNO(mFALSE, ME_ARGUMENT); darope@246: } darope@246: // Are we allowed to load this plugin at this time? darope@246: // If we cannot load the plugin after unloading it, we keep it. darope@246: if(info->loadable < now) { darope@246: if(info->loadable > PT_STARTUP) { darope@246: META_DEBUG(2, ("dll: Delaying reload plugin '%s'; would not be able to reattach now: allowed=%s; now=%s", desc, str_loadable(), str_loadtime(now, SL_SIMPLE))); darope@246: // caller should give message to user darope@246: // try to reload again at next opportunity darope@246: RETURN_ERRNO(mFALSE, ME_DELAYED); darope@246: } darope@246: else { darope@246: META_DEBUG(2, ("dll: Failed reload plugin '%s'; would not be able to reattach now: allowed=%s; now=%s", desc, str_loadable(), str_loadtime(now, SL_SIMPLE))); darope@246: // don't try to reload again later darope@246: action=PA_NONE; darope@246: RETURN_ERRNO(mFALSE, ME_NOTALLOWED); darope@246: } darope@246: } darope@246: darope@0: if(!unload(now, reason)) { darope@0: META_ERROR("dll: Failed to unload plugin '%s' for reloading", desc); darope@0: // meta_errno should be set already in unload() darope@0: return(mFALSE); darope@0: } darope@0: if(!load(now)) { darope@0: META_ERROR("dll: Failed to reload plugin '%s' after unloading", desc); darope@0: // meta_errno should be set already in load() darope@0: return(mFALSE); darope@0: } darope@0: return(mTRUE); darope@0: } darope@0: darope@0: // Pause a plugin; temporarily disabled for API routines. darope@0: // meta_errno values: darope@0: // - ME_ALREADY this plugin already paused darope@0: // - ME_BADREQ can't pause; not running darope@0: // - ME_NOTALLOWED plugin doesn't want to be paused darope@0: mBOOL MPlugin::pause(void) { darope@0: if(status == PL_PAUSED) { darope@0: META_ERROR("Not pausing plugin '%s'; already paused", desc); darope@0: RETURN_ERRNO(mFALSE, ME_ALREADY); darope@0: } darope@0: if(status != PL_RUNNING) { darope@0: META_ERROR("Cannot pause plugin '%s'; not currently running (status=%s)", desc, str_status()); darope@0: RETURN_ERRNO(mFALSE, ME_BADREQ); darope@0: } darope@0: darope@0: // are we allowed to pause this plugin? darope@0: if(info->unloadable < PT_ANYPAUSE) { darope@0: META_ERROR("Cannot pause plugin '%s'; not allowed by plugin (allowed=%s)", desc, str_unloadable()); darope@0: action=PA_NONE; darope@0: RETURN_ERRNO(mFALSE, ME_NOTALLOWED); darope@0: } darope@0: darope@0: status=PL_PAUSED; darope@0: META_LOG("Paused plugin '%s'", desc); darope@0: return(mTRUE); darope@0: } darope@0: darope@0: // Unpause a plugin. darope@0: // meta_errno values: darope@0: // - ME_BADREQ can't unpause; not paused darope@0: mBOOL MPlugin::unpause(void) { darope@0: if(status != PL_PAUSED) { darope@0: META_ERROR("Cannot unpause plugin '%s'; not currently paused (status=%s)", desc, str_status()); darope@0: RETURN_ERRNO(mFALSE, ME_BADREQ); darope@0: } darope@0: status=PL_RUNNING; darope@0: META_LOG("Unpaused plugin '%s'", desc); darope@0: return(mTRUE); darope@0: } darope@0: darope@0: // Retry pending action, presumably from a previous failure. darope@0: // meta_errno values: darope@0: // - ME_BADREQ no pending action darope@0: // - errno's from load() darope@0: // - errno's from unload() darope@0: // - errno's from reload() darope@0: mBOOL MPlugin::retry(PLUG_LOADTIME now, PL_UNLOAD_REASON reason) { darope@0: if(action==PA_LOAD) darope@0: return(load(now)); darope@0: else if(action==PA_ATTACH) darope@0: return(load(now)); darope@0: else if(action==PA_UNLOAD) darope@0: return(unload(now, reason)); darope@0: else if(action==PA_RELOAD) darope@0: return(reload(now, reason)); darope@0: else { darope@0: META_ERROR("No pending action to retry for plugin '%s'; (status=%s, action=%s)", desc, str_status(), str_action()); darope@0: RETURN_ERRNO(mFALSE, ME_BADREQ); darope@0: } darope@0: } darope@0: darope@0: // Clear a plugin (it failed a previous action and should be darope@0: // removed from the list, or it's being unloaded). darope@0: // meta_errno values: darope@0: // - ME_BADREQ not marked for clearing darope@0: // - ME_DLERROR failed to dlclose darope@0: mBOOL MPlugin::clear(void) { darope@0: if(status != PL_FAILED && status != PL_BADFILE darope@0: && status != PL_EMPTY && status != PL_OPENED) { darope@0: META_ERROR("Cannot clear plugin '%s'; not marked as failed, empty, or open (status=%s)", desc, str_status()); darope@0: RETURN_ERRNO(mFALSE, ME_BADREQ); darope@0: } darope@0: // If file is open, close the file. Note: after this, attempts to darope@0: // reference any memory locations in the file will produce a segfault. darope@0: if(handle && DLCLOSE(handle) != 0) { darope@0: META_ERROR("dll: Couldn't close plugin file '%s': %s", file, DLERROR()); darope@0: status=PL_FAILED; darope@0: RETURN_ERRNO(mFALSE, ME_DLERROR); darope@0: } darope@0: handle=NULL; darope@0: darope@0: if(gamedll_funcs.dllapi_table) free(gamedll_funcs.dllapi_table); darope@0: if(gamedll_funcs.newapi_table) free(gamedll_funcs.newapi_table); darope@246: if(dllapi_table) free(dllapi_table); darope@246: if(dllapi_post_table) free(dllapi_post_table); darope@246: if(newapi_table) free(newapi_table); darope@246: if(newapi_post_table) free(newapi_post_table); darope@246: if(engine_table) free(engine_table); darope@246: if(engine_post_table) free(engine_post_table); darope@0: darope@0: status=PL_EMPTY; darope@0: action=PA_NULL; darope@0: handle=NULL; darope@0: info=NULL; darope@0: time_loaded=0; darope@0: dllapi_table=NULL; darope@0: dllapi_post_table=NULL; darope@0: newapi_table=NULL; darope@0: newapi_post_table=NULL; darope@0: engine_table=NULL; darope@0: engine_post_table=NULL; darope@246: gamedll_funcs.dllapi_table = NULL; darope@246: gamedll_funcs.newapi_table = NULL; darope@0: return(mTRUE); darope@0: } darope@0: darope@0: // List information about plugin to console. darope@0: void MPlugin::show(void) { darope@0: char *cp, *tstr; darope@0: int n, width; darope@0: width=13; darope@0: META_CONS("%*s: %s", width, "name", info ? info->name : "(nil)"); darope@0: META_CONS("%*s: %s", width, "desc", desc); darope@0: META_CONS("%*s: %s", width, "status", str_status()); darope@0: META_CONS("%*s: %s", width, "action", str_action()); darope@0: META_CONS("%*s: %s", width, "filename", filename); darope@0: META_CONS("%*s: %s", width, "file", file); darope@0: META_CONS("%*s: %s", width, "pathname", pathname); darope@0: META_CONS("%*s: %d", width, "index", index); darope@0: META_CONS("%*s: %s", width, "source", str_source()); darope@0: META_CONS("%*s: %s", width, "loadable", str_loadable(SL_ALLOWED)); darope@0: META_CONS("%*s: %s", width, "unloadable", str_unloadable(SL_ALLOWED)); darope@0: META_CONS("%*s: %s", width, "version", info ? info->version : "(nil)"); darope@0: META_CONS("%*s: %s", width, "date", info ? info->date : "(nil)"); darope@0: META_CONS("%*s: %s", width, "author", info ? info->author : "(nil)"); darope@0: META_CONS("%*s: %s", width, "url", info ? info->url : "(nil)"); darope@0: META_CONS("%*s: %s", width, "logtag", info ? info->logtag : "(nil)"); darope@0: // ctime() includes newline at EOL darope@0: tstr=ctime(&time_loaded); darope@0: if((cp=strchr(tstr, '\n'))) darope@0: *cp='\0'; darope@0: META_CONS("%*s: %s", width, "last loaded", tstr); darope@0: // XXX show file time ? darope@0: darope@0: if(dllapi_table) { darope@0: META_CONS("DLLAPI functions:"); darope@0: SHOW_DEF_DLLAPI(dllapi_table, " ", ""); darope@0: META_CONS("%d functions (dllapi)", n); darope@0: } darope@0: else darope@0: META_CONS("No DLLAPI functions."); darope@0: if(dllapi_post_table) { darope@0: META_CONS("DLLAPI-Post functions:"); darope@0: SHOW_DEF_DLLAPI(dllapi_post_table, " ", "_Post"); darope@0: META_CONS("%d functions (dllapi post)", n); darope@0: } darope@0: else darope@0: META_CONS("No DLLAPI-Post functions."); darope@0: darope@0: if(newapi_table) { darope@0: META_CONS("NEWAPI functions:"); darope@0: SHOW_DEF_NEWAPI(newapi_table, " ", ""); darope@0: META_CONS("%d functions (newapi)", n); darope@0: } darope@0: else darope@0: META_CONS("No NEWAPI functions."); darope@0: if(newapi_post_table) { darope@0: META_CONS("NEWAPI-Post functions:"); darope@0: SHOW_DEF_NEWAPI(newapi_post_table, " ", "_Post"); darope@0: META_CONS("%d functions (newapi post)", n); darope@0: } darope@0: else darope@0: META_CONS("No NEWAPI-Post functions."); darope@0: darope@0: if(engine_table) { darope@0: META_CONS("Engine functions:"); darope@0: SHOW_DEF_ENGINE(engine_table, " ", ""); darope@0: META_CONS("%d functions (engine)", n); darope@0: } darope@0: else darope@0: META_CONS("No Engine functions."); darope@0: if(engine_post_table) { darope@0: META_CONS("Engine-Post functions:"); darope@0: SHOW_DEF_ENGINE(engine_post_table, " ", "_Post"); darope@0: META_CONS("%d functions (engine post)", n); darope@0: } darope@0: else darope@0: META_CONS("No Engine-Post functions."); darope@0: RegCmds->show(index); darope@0: RegCvars->show(index); darope@0: } darope@0: darope@0: // Check whether the file on disk looks like it's been updated since we darope@0: // last loaded the plugin. darope@0: // meta_errno values: darope@0: // - ME_NOFILE couldn't find file darope@0: // - ME_NOERROR no error; false indicates file not newer darope@0: mBOOL MPlugin::newer_file(void) { darope@0: struct stat st; darope@0: time_t file_time; darope@0: darope@0: if(stat(pathname, &st) != 0) darope@0: RETURN_ERRNO(mFALSE, ME_NOFILE); darope@0: file_time=max(st.st_ctime, st.st_mtime); darope@0: META_DEBUG(5, ("newer_file? file=%s; load=%d, file=%d; ctime=%d, mtime=%d", darope@0: file, time_loaded, file_time, st.st_ctime, st.st_mtime)); darope@0: if(file_time > time_loaded) darope@0: return(mTRUE); darope@0: else darope@0: RETURN_ERRNO(mFALSE, ME_NOERROR); darope@0: } darope@0: darope@0: // Return a string describing status of plugin. darope@0: // SIMPLE is the default. darope@0: // SHOW is max 4 chars, for "show" output. darope@0: // meta_errno values: darope@0: // - none darope@0: char *MPlugin::str_status(STR_STATUS fmt) { darope@0: switch(status) { darope@0: case PL_EMPTY: darope@0: if(fmt==ST_SHOW) return("empt"); darope@0: else return("empty"); darope@0: case PL_VALID: darope@0: if(fmt==ST_SHOW) return("info"); darope@0: else return("valid"); darope@0: case PL_BADFILE: darope@0: if(fmt==ST_SHOW) return("badf"); darope@0: else return("badfile"); darope@0: case PL_OPENED: darope@0: if(fmt==ST_SHOW) return("open"); darope@0: else return("opened"); darope@0: case PL_FAILED: darope@0: if(fmt==ST_SHOW) return("fail"); darope@0: else return("failed"); darope@0: case PL_RUNNING: darope@0: if(fmt==ST_SHOW) return("RUN"); darope@0: else return("running"); darope@0: case PL_PAUSED: darope@0: if(fmt==ST_SHOW) return("PAUS"); darope@0: else return("paused"); darope@0: default: darope@0: if(fmt==ST_SHOW) return(UTIL_VarArgs("UNK%d", status)); darope@0: return(UTIL_VarArgs("unknown (%d)", status)); darope@0: } darope@0: } darope@0: darope@0: // Return a string (one word) describing requested action for plugin. darope@0: // SIMPLE is the default. darope@0: // SHOW is max 4 chars, for "show" output. darope@0: // meta_errno values: darope@0: // - none darope@0: char *MPlugin::str_action(STR_ACTION fmt) { darope@0: switch(action) { darope@0: case PA_NULL: darope@0: if(fmt==SA_SHOW) return("NULL"); darope@0: else return("null"); darope@0: case PA_NONE: darope@0: if(fmt==SA_SHOW) return(" - "); darope@0: else return("none"); darope@0: case PA_KEEP: darope@0: if(fmt==SA_SHOW) return("keep"); darope@0: else return("keep"); darope@0: case PA_LOAD: darope@0: if(fmt==SA_SHOW) return("load"); darope@0: else return("load"); darope@0: case PA_ATTACH: darope@0: if(fmt==SA_SHOW) return("atch"); darope@0: else return("attach"); darope@0: case PA_UNLOAD: darope@0: if(fmt==SA_SHOW) return("unld"); darope@0: else return("unload"); darope@0: case PA_RELOAD: darope@0: if(fmt==SA_SHOW) return("relo"); darope@0: else return("reload"); darope@0: default: darope@0: if(fmt==SA_SHOW) return(UTIL_VarArgs("UNK%d", action)); darope@0: else return(UTIL_VarArgs("unknown (%d)", action)); darope@0: } darope@0: } darope@0: darope@0: // Return a string describing given plugin loadtime. darope@0: // SIMPLE is the default. darope@0: // SHOW is max 3 chars, for "show" output. darope@0: // ALLOWED is in terms of when it's allowed to load/unload. darope@0: // NOW is to describe current situation of load/unload attempt. darope@0: // meta_errno values: darope@0: // - none darope@0: char *MPlugin::str_loadtime(PLUG_LOADTIME ptime, STR_LOADTIME fmt) { darope@0: switch(ptime) { darope@0: case PT_NEVER: darope@0: if(fmt==SL_SHOW) return("Never"); darope@0: else return("never"); darope@0: case PT_STARTUP: darope@0: if(fmt==SL_SHOW) return("Start"); darope@0: else if(fmt==SL_ALLOWED) return("at server startup"); darope@0: else if(fmt==SL_NOW) return("during server startup"); darope@0: else return("startup"); darope@0: case PT_CHANGELEVEL: darope@0: if(fmt==SL_SHOW) return("Chlvl"); darope@0: else if(fmt==SL_ALLOWED) return("at changelevel"); darope@0: else if(fmt==SL_NOW) return("during changelevel"); darope@0: else return("changelevel"); darope@0: case PT_ANYTIME: darope@0: if(fmt==SL_SHOW) return("ANY"); darope@0: else if(fmt==SL_ALLOWED) return("at any time"); darope@0: else if(fmt==SL_NOW) return("during map"); darope@0: else return("anytime"); darope@0: case PT_ANYPAUSE: darope@0: if(fmt==SL_SHOW) return("Pause"); darope@0: else if(fmt==SL_ALLOWED) return("at any time, and pausable"); darope@0: else if(fmt==SL_NOW) return("for requested pause"); darope@0: else return("pausable"); darope@0: default: darope@0: if(fmt==SL_SHOW) return(UTIL_VarArgs("UNK-%d", ptime)); darope@0: else return(UTIL_VarArgs("unknown (%d)", ptime)); darope@0: } darope@0: } darope@0: darope@0: // Return a string describing why a plugin is to be unloaded. darope@0: // meta_errno values: darope@0: // - none darope@0: char *MPlugin::str_reason(PL_UNLOAD_REASON preason) { darope@0: switch(preason) { darope@0: case PNL_NULL: darope@0: return("null"); darope@0: case PNL_INI_DELETED: darope@0: return("deleted from ini file"); darope@0: case PNL_FILE_NEWER: darope@0: return("file on disk is newer"); darope@0: case PNL_COMMAND: darope@0: return("server command"); darope@0: case PNL_CMD_FORCED: darope@0: return("forced by server command"); darope@0: default: darope@0: return(UTIL_VarArgs("unknown (%d)", preason)); darope@0: } darope@0: } darope@0: darope@0: // Return a string describing how the plugin was loaded. darope@0: // meta_errno values: darope@0: // - none darope@0: char *MPlugin::str_source(STR_SOURCE fmt) { darope@0: switch(source) { darope@0: case PS_INI: darope@0: if(fmt==SO_SHOW) return("ini"); darope@0: else return("ini file"); darope@0: case PS_CMD: darope@0: if(fmt==SO_SHOW) return("cmd"); darope@0: else return("console command"); darope@0: default: darope@0: if(fmt==SO_SHOW) return(UTIL_VarArgs("UNK%d", source)); darope@0: else return(UTIL_VarArgs("unknown (%d)", source)); darope@0: } darope@0: }