[Devel] dlopen(... RTLD_LOCAL vs. RTLD_GLOBAL

Daniel-Constantin Mierla daniel at voice-system.ro
Tue Nov 28 16:39:03 CET 2006


Hello,

looking in the dlopen man, I see that at least one flag should be present:

<snip>
       One of the following two values must be included in flag:

       RTLD_LAZY
              Perform lazy binding.  Only resolve symbols as the code 
that references them is executed.  If the symbol is never referenced, 
then it is never resolved.  (Lazy binding
              is only performed for function references; references to 
variables are always immediately bound when the library is loaded.)

       RTLD_NOW
              If this value is specified, or the environment variable 
LD_BIND_NOW is set to a non-empty string, all undefined symbols in the 
library  are  resolved  before  dlopen()
              returns. If this cannot be done, an error is returned.
</snip>

They have the value 1 and 2, so 0 is not valid, as I could get. I would 
prefer to use it for default flags (I will do it as define to be easy to 
change in the future if needed).

I will apply the patch very soon, but I will introduce the flags in 
module's exported structure, to be bundled with the other fields, 
otherwise it may get lost in variable/function declarations and hard to 
spot.

Cheers,
Daniel


On 11/28/06 11:31, Bastian Friedrich wrote:
> Hi,
>
> Am Dienstag, 28. November 2006 10:09 schrieb Daniel-Constantin Mierla:
>   
>> to optimize a bit, I would keep the default flags in a variable and I
>> will check the flags forced by a module with those ones, not to
>> close/open again with same flags.
>>     
>
> ACK. See attached patch.
>
>   
>> Also, maybe a value (like 0) should be 
>> used to signal that the module wants to be loaded with the default flags
>> and not double-check again.
>>     
>
> I used "if (modflags >= 0)" (i.e.: -1 is "default flags"); 0 could potentially 
> be a valid flag.
>
> Thx a lot,
>    Bastian
>
>   
> ------------------------------------------------------------------------
>
> Index: sr_module.h
> ===================================================================
> RCS file: /cvsroot/openser/sip-server/sr_module.h,v
> retrieving revision 1.8
> diff -u -r1.8 sr_module.h
> --- sr_module.h	23 Nov 2006 21:57:14 -0000	1.8
> +++ sr_module.h	28 Nov 2006 09:28:28 -0000
> @@ -159,5 +159,10 @@
>   *  - returns >0 if ok, 0 to drop message
>   */
>  
> +struct mod_info_ {
> +	int dlopenflags;
> +};
> +
> +typedef struct mod_info_ mod_info_t;
>  
>  #endif
> Index: sr_module.c
> ===================================================================
> RCS file: /cvsroot/openser/sip-server/sr_module.c,v
> retrieving revision 1.7
> diff -u -r1.7 sr_module.c
> --- sr_module.c	23 Nov 2006 21:57:13 -0000	1.7
> +++ sr_module.c	28 Nov 2006 09:28:28 -0000
> @@ -199,17 +199,40 @@
>  	char* error;
>  	struct module_exports* exp;
>  	struct sr_module* t;
> +	mod_info_t *info;
> +	int defaultflags;
> +	int modflags;
>  	
>  #ifndef RTLD_NOW
>  /* for openbsd */
>  #define RTLD_NOW DL_LAZY
>  #endif
> -	handle=dlopen(path, RTLD_NOW); /* resolve all symbols now */
> +	defaultflags = RTLD_NOW;
> +	handle=dlopen(path, defaultflags); /* resolve all symbols now */
>  	if (handle==0){
>  		LOG(L_ERR, "ERROR: load_module: could not open module <%s>: %s\n",
>  					path, dlerror() );
>  		goto error;
>  	}
> +
> +	dlerror();
> +	if ((info = dlsym(handle, "mod_info"))) {	/* mod_info found */
> +		if (!dlerror()) {			/* ... and no error occured */
> +			modflags = info->dlopenflags;
> +			if ((modflags != defaultflags) && (modflags >= 0)) {
> +				dlclose(handle);
> +				DBG("DEBUG:load_module:Reloading module %s with flags %d\n",
> +					 path, modflags);
> +				handle = dlopen(path, modflags);
> +				if (handle==0){
> +					LOG(L_ERR, "ERROR: load_module: could not open module <%s>: %s\n",
> +								path, dlerror() );
> +					goto error;
> +				}
> +			}
> +
> +		}
> +	}
>  	
>  	for(t=modules;t; t=t->next){
>  		if (t->handle==handle){
>   



More information about the Devel mailing list