metamod/mplugin.cpp
changeset 248 f72386298cba
parent 247 2b1bf332bf41
child 250 1f5fb7d1d9b3
     1.1 --- a/metamod/mplugin.cpp	Fri Feb 18 20:52:34 2005 +0000
     1.2 +++ b/metamod/mplugin.cpp	Tue Aug 23 20:31:28 2005 +0000
     1.3 @@ -130,6 +130,85 @@
     1.4  	return(mTRUE);
     1.5  }
     1.6  
     1.7 +// Unload a plugin from plugin request
     1.8 +// meta_errno values:
     1.9 +//  - errno's from unload()
    1.10 +mBOOL MPlugin::plugin_unload(plid_t plid, PLUG_LOADTIME now, PL_UNLOAD_REASON reason)
    1.11 +{
    1.12 +	PLUG_ACTION old_action;
    1.13 +	MPlugin *pl_unloader;
    1.14 +
    1.15 +	if ( !(pl_unloader=Plugins->find(plid)) )
    1.16 +	{
    1.17 +		META_WARNING("dll: Not unloading plugin '%s'; plugin that requested unload is not found.", desc);
    1.18 +		RETURN_ERRNO(mFALSE, ME_BADREQ);
    1.19 +	} else if (pl_unloader->index == index) {
    1.20 +		META_WARNING("dll: Not unloading plugin '%s'; Plugin tried to unload itself.", desc);
    1.21 +		RETURN_ERRNO(mFALSE, ME_UNLOAD_SELF);
    1.22 +	} else if (is_unloader) {
    1.23 +		META_WARNING("dll: Not unloading plugin '%s'; Plugin is unloading plugin that tried to unload it.", desc);
    1.24 +		RETURN_ERRNO(mFALSE, ME_UNLOAD_UNLOADER);
    1.25 +	} else {
    1.26 +		unloader_index = pl_unloader->index;
    1.27 +	}
    1.28 +
    1.29 +	//block unloader from being unloaded by other plugin
    1.30 +	pl_unloader->is_unloader = mTRUE;
    1.31 +
    1.32 +	//try unload
    1.33 +	old_action = action;
    1.34 +	action = PA_UNLOAD;
    1.35 +	if (unload(now, reason, PNL_PLG_FORCED))
    1.36 +	{
    1.37 +		META_DEBUG(1,("Unloaded plugin '%s'", desc));
    1.38 +		pl_unloader->is_unloader = mFALSE;
    1.39 +		return mTRUE;
    1.40 +	}
    1.41 +
    1.42 +	pl_unloader->is_unloader = mFALSE;
    1.43 +
    1.44 +	//can't unload plugin now, set delayed
    1.45 +	if (meta_errno==ME_DELAYED)
    1.46 +	{
    1.47 +		action = old_action;
    1.48 +		meta_errno = ME_NOTALLOWED;
    1.49 +		META_DEBUG(2, ("dll: Failed unload plugin '%s'; can't detach now: allowed=%s; now=%s", desc, str_unloadable(), str_loadtime(PT_ANYTIME, SL_SIMPLE)));
    1.50 +	}
    1.51 +
    1.52 +	return mFALSE;
    1.53 +}
    1.54 +
    1.55 +// Parse a filename string from PEXT_LOAD_PLUGIN_BY_* function into a plugin.
    1.56 +// meta_errno values:
    1.57 +mBOOL MPlugin::plugin_parseline(const char *fname, int loader_index)
    1.58 +{
    1.59 +	char *cp;
    1.60 +
    1.61 +	source_plugin_index = loader_index;
    1.62 +
    1.63 +	STRNCPY(filename, fname, sizeof(filename));
    1.64 +	normalize_pathname(filename);
    1.65 +
    1.66 +	//store just name of the actual _file, without path
    1.67 +	cp = strrchr(filename, '/');
    1.68 +	if (cp)
    1.69 +		file = cp+1;
    1.70 +	else
    1.71 +		file = filename;
    1.72 +
    1.73 +	//grab description
    1.74 +	//temporarily use plugin file until plugin can be queried
    1.75 +	snprintf(desc, sizeof(desc), "<%s>", file);
    1.76 +
    1.77 +	//make full pathname
    1.78 +	full_gamedir_path(filename, pathname);
    1.79 +
    1.80 +	source = PS_PLUGIN;
    1.81 +	status = PL_VALID;
    1.82 +
    1.83 +	return mTRUE;
    1.84 +}
    1.85 +
    1.86  // Parse a line from console "load" command into a plugin.
    1.87  // meta_errno values:
    1.88  //  - ME_FORMAT		invalid line format
    1.89 @@ -479,7 +558,7 @@
    1.90  		RETURN_ERRNO(mFALSE, ME_BADREQ);
    1.91  	}
    1.92  
    1.93 -	if(status != PL_OPENED) {
    1.94 +	if(status < PL_OPENED) {
    1.95  		// query plugin; open file and get info about it
    1.96  		if(!query()) {
    1.97  			META_ERROR("dll: Skipping plugin '%s'; couldn't query", desc);
    1.98 @@ -856,23 +935,25 @@
    1.99  //  - ME_DELAYED	unload request is delayed (till changelevel?)
   1.100  //  - ME_NOTALLOWED	plugin not unloadable after startup
   1.101  //  - errno's from check_input()
   1.102 -mBOOL MPlugin::unload(PLUG_LOADTIME now, PL_UNLOAD_REASON reason) {
   1.103 +mBOOL MPlugin::unload(PLUG_LOADTIME now, PL_UNLOAD_REASON reason, PL_UNLOAD_REASON real_reason) {
   1.104  	if(!check_input()) {
   1.105  		// details logged, meta_errno set in check_input()
   1.106  		RETURN_ERRNO(mFALSE, ME_ARGUMENT);
   1.107  	}
   1.108  	if(status < PL_RUNNING) {
   1.109 -		META_ERROR("dll: Not unloading plugin '%s'; already unloaded (status=%s)", desc, str_status());
   1.110 -		RETURN_ERRNO(mFALSE, ME_ALREADY);
   1.111 +		if (reason != PNL_CMD_FORCED && reason != PNL_RELOAD) {
   1.112 +			META_ERROR("dll: Not unloading plugin '%s'; already unloaded (status=%s)", desc, str_status());
   1.113 +			RETURN_ERRNO(mFALSE, ME_ALREADY);
   1.114 +		}
   1.115  	}
   1.116  	if(action != PA_UNLOAD && action != PA_RELOAD) {
   1.117 -		META_ERROR("dll: Not unloading plugin '%s'; not marked for unload (action=%s)", desc, str_action());
   1.118 +		META_WARNING("dll: Not unloading plugin '%s'; not marked for unload (action=%s)", desc, str_action());
   1.119  		RETURN_ERRNO(mFALSE, ME_BADREQ);
   1.120  	}
   1.121  
   1.122  	// Are we allowed to detach this plugin at this time?
   1.123  	// If forcing unload, we disregard when plugin wants to be unloaded.
   1.124 -	if(info->unloadable < now) {
   1.125 +	if(info && info->unloadable < now) {
   1.126  		if(reason == PNL_CMD_FORCED) {
   1.127  			META_DEBUG(2, ("dll: Forced unload plugin '%s' overriding allowed times: allowed=%s; now=%s", desc, str_unloadable(), str_loadtime(now, SL_SIMPLE)));
   1.128  		}
   1.129 @@ -899,16 +980,20 @@
   1.130  
   1.131  	// detach plugin
   1.132  	if(!detach(now, reason)) {
   1.133 -		if(reason == PNL_CMD_FORCED) {
   1.134 +		if (reason == PNL_RELOAD){
   1.135 +			META_DEBUG(2, ("dll: Reload plugin '%s' overriding failed detach", desc));
   1.136 +		} else if(reason == PNL_CMD_FORCED) {
   1.137  			META_DEBUG(2, ("dll: Forced unload plugin '%s' overriding failed detach"));
   1.138  		}
   1.139  		else {
   1.140 -			META_ERROR("dll: Failed to detach plugin '%s'; ", desc);
   1.141 +			META_WARNING("dll: Failed to detach plugin '%s'; ", desc);
   1.142  			// meta_errno should be already set in detach()
   1.143  			return(mFALSE);
   1.144  		}
   1.145  	}
   1.146  
   1.147 +	Plugins->clear_source_plugin_index(index);
   1.148 +
   1.149  	// successful detach, or forced unload
   1.150  
   1.151  	// Unmark registered commands for this plugin (by index number).
   1.152 @@ -924,9 +1009,9 @@
   1.153  	// Close the file.  Note: after this, attempts to reference any memory
   1.154  	// locations in the file will produce a segfault.
   1.155  	if(DLCLOSE(handle) != 0) {
   1.156 -		META_ERROR("dll: Couldn't close plugin file '%s': %s", file, DLERROR());
   1.157 -		status=PL_FAILED;
   1.158 -		RETURN_ERRNO(mFALSE, ME_DLERROR);
   1.159 +		// If DLL cannot be closed, OS is badly broken or we are giving invalid handle.
   1.160 +		// So we don't return here but instead remove plugin from our listings.
   1.161 +		META_WARNING("dll: Couldn't close plugin file '%s': %s", file, DLERROR());
   1.162  	}
   1.163  	handle=NULL;
   1.164  
   1.165 @@ -939,7 +1024,7 @@
   1.166  		action=PA_LOAD;
   1.167  		clear();
   1.168  	}
   1.169 -	META_LOG("dll: Unloaded plugin '%s' for reason '%s'", desc, str_reason(reason));
   1.170 +	META_LOG("dll: Unloaded plugin '%s' for reason '%s'", desc, str_reason(reason, real_reason));
   1.171  	return(mTRUE);
   1.172  }
   1.173  
   1.174 @@ -951,6 +1036,12 @@
   1.175  	int ret;
   1.176  	META_DETACH_FN pfn_detach;
   1.177  
   1.178 +	// If we have no handle, i.e. no dll loaded, we return true because the
   1.179 +	// dll is obviously detached. We shouldn't call DLSYM() with a NULL
   1.180 +	// handle since this will DLSYM() ourself.
   1.181 +	if(!handle)
   1.182 +		return mTRUE;
   1.183 +
   1.184  	if(!(pfn_detach = (META_DETACH_FN) DLSYM(handle, "Meta_Detach"))) {
   1.185  		META_ERROR("dll: Error detach plugin '%s': Couldn't find Meta_detach(): %s", desc, DLERROR());
   1.186  		// caller will dlclose()
   1.187 @@ -979,7 +1070,8 @@
   1.188  	}
   1.189  	// Are we allowed to load this plugin at this time?
   1.190  	// If we cannot load the plugin after unloading it, we keep it.
   1.191 -	if(info->loadable < now) {
   1.192 +	if (info && info->loadable < now)
   1.193 +	{
   1.194  		if(info->loadable > PT_STARTUP) {
   1.195  			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)));
   1.196  			// caller should give message to user
   1.197 @@ -994,13 +1086,18 @@
   1.198  		}
   1.199  	}
   1.200  
   1.201 -	if(!unload(now, reason)) {
   1.202 -		META_ERROR("dll: Failed to unload plugin '%s' for reloading", desc);
   1.203 +	if (status < PL_RUNNING)
   1.204 +	{
   1.205 +		META_WARNING("dll: Plugin '%s' isn't running; Forcing unload plugin for reloading", desc);
   1.206 +		reason = PNL_RELOAD;
   1.207 +	}
   1.208 +	if (!unload(now, reason, reason)) {
   1.209 +		META_WARNING("dll: Failed to unload plugin '%s' for reloading", desc);
   1.210  		// meta_errno should be set already in unload()
   1.211  		return(mFALSE);
   1.212  	}
   1.213  	if(!load(now)) {
   1.214 -		META_ERROR("dll: Failed to reload plugin '%s' after unloading", desc);
   1.215 +		META_WARNING("dll: Failed to reload plugin '%s' after unloading", desc);
   1.216  		// meta_errno should be set already in load()
   1.217  		return(mFALSE);
   1.218  	}
   1.219 @@ -1059,7 +1156,7 @@
   1.220  	else if(action==PA_ATTACH)
   1.221  		return(load(now));
   1.222  	else if(action==PA_UNLOAD)
   1.223 -		return(unload(now, reason));
   1.224 +		return(unload(now, reason, reason));
   1.225  	else if(action==PA_RELOAD)
   1.226  		return(reload(now, reason));
   1.227  	else {
   1.228 @@ -1110,6 +1207,9 @@
   1.229  	engine_post_table=NULL;
   1.230  	gamedll_funcs.dllapi_table = NULL;
   1.231  	gamedll_funcs.newapi_table = NULL;
   1.232 +	source_plugin_index = 0;
   1.233 +	unloader_index = 0;
   1.234 +	is_unloader = mFALSE;
   1.235  	return(mTRUE);
   1.236  }
   1.237  
   1.238 @@ -1134,6 +1234,7 @@
   1.239  	META_CONS("%*s: %s", width, "author", info ? info->author : "(nil)");
   1.240  	META_CONS("%*s: %s", width, "url", info ? info->url : "(nil)");
   1.241  	META_CONS("%*s: %s", width, "logtag", info ? info->logtag : "(nil)");
   1.242 +	META_CONS("%*s: %s", width, "ifvers", info ? info->ifvers : "(nil)");
   1.243  	// ctime() includes newline at EOL
   1.244  	tstr=ctime(&time_loaded);
   1.245  	if((cp=strchr(tstr, '\n')))
   1.246 @@ -1187,6 +1288,10 @@
   1.247  		META_CONS("No Engine-Post functions.");
   1.248  	RegCmds->show(index);
   1.249  	RegCvars->show(index);
   1.250 +	if (Plugins->found_child_plugins(index))
   1.251 +		Plugins->show(index);
   1.252 +	else
   1.253 +		META_CONS("No child plugins.");
   1.254  }
   1.255  
   1.256  // Check whether the file on disk looks like it's been updated since we
   1.257 @@ -1318,8 +1423,15 @@
   1.258  // Return a string describing why a plugin is to be unloaded.
   1.259  // meta_errno values:
   1.260  //  - none
   1.261 -char *MPlugin::str_reason(PL_UNLOAD_REASON preason) {
   1.262 -	switch(preason) {
   1.263 +char *MPlugin::str_reason(PL_UNLOAD_REASON preason, PL_UNLOAD_REASON preal_reason) {
   1.264 +	char buf[128];
   1.265 +	
   1.266 +	if(preason == PNL_PLUGIN)
   1.267 +		preason = PNL_NULL;
   1.268 +	if(preason == PNL_PLG_FORCED)
   1.269 +		preason = PNL_NULL;
   1.270 +
   1.271 +	switch(preal_reason) {
   1.272  		case PNL_NULL:
   1.273  			return("null");
   1.274  		case PNL_INI_DELETED:
   1.275 @@ -1330,8 +1442,16 @@
   1.276  			return("server command");
   1.277  		case PNL_CMD_FORCED:
   1.278  			return("forced by server command");
   1.279 +		case PNL_PLUGIN:
   1.280 +			STRNCPY(buf, str_reason(PNL_NULL, preason), sizeof(buf));
   1.281 +			return(UTIL_VarArgs("%s (request from plugin[%d])", buf, unloader_index));
   1.282 +		case PNL_PLG_FORCED:
   1.283 +			STRNCPY(buf, str_reason(PNL_NULL, preason), sizeof(buf));
   1.284 +			return(UTIL_VarArgs("%s (forced request from plugin[%d])", buf, unloader_index));
   1.285 +		case PNL_RELOAD:
   1.286 +			return("reloading");
   1.287  		default:
   1.288 -			return(UTIL_VarArgs("unknown (%d)", preason));
   1.289 +			return(UTIL_VarArgs("unknown (%d)", preal_reason));
   1.290  	}
   1.291  }
   1.292  
   1.293 @@ -1346,6 +1466,16 @@
   1.294  		case PS_CMD:
   1.295  			if(fmt==SO_SHOW) return("cmd");
   1.296  			else return("console command");
   1.297 +		case PS_PLUGIN:
   1.298 +			if (source_plugin_index <= 0) {
   1.299 +				if (fmt==SO_SHOW) 
   1.300 +					return("plUN");
   1.301 +				else 
   1.302 +					return("unloaded plugin");
   1.303 +			} else {
   1.304 +				if(fmt==SO_SHOW) return(UTIL_VarArgs("pl%d", source_plugin_index));
   1.305 +				else return(UTIL_VarArgs("plugin [%d]", source_plugin_index));
   1.306 +			}
   1.307  		default:
   1.308  			if(fmt==SO_SHOW) return(UTIL_VarArgs("UNK%d", source));
   1.309  			else return(UTIL_VarArgs("unknown (%d)", source));