1 /* dlutils.c - handy functions and definitions for dl_*.xs files
3 * Currently this file is simply #included into dl_*.xs/.c files.
4 * It should really be split into a dlutils.h and dlutils.c
7 * 29th Feburary 2000 - Alan Burlison: Added functionality to close dlopen'd
8 * files when the interpreter exits
12 # define XS_VERSION "0"
14 #define MY_CXT_KEY "DynaLoader::_guts" XS_VERSION
17 char * x_dl_last_error; /* pointer to allocated memory for
19 int x_dl_nonlazy; /* flag for immediate rather than lazy
20 linking (spots unresolved symbol) */
21 #ifdef DL_LOADONCEONLY
22 HV * x_dl_loaded_files; /* only needed on a few systems */
25 my_cxtx_t x_dl_cxtx; /* extra platform-specific data */
28 int x_dl_debug; /* value copied from $DynaLoader::dl_debug */
34 #define dl_last_error (MY_CXT.x_dl_last_error)
35 #define dl_nonlazy (MY_CXT.x_dl_nonlazy)
36 #ifdef DL_LOADONCEONLY
37 #define dl_loaded_files (MY_CXT.x_dl_loaded_files)
40 #define dl_cxtx (MY_CXT.x_dl_cxtx)
43 #define dl_debug (MY_CXT.x_dl_debug)
47 #define DLDEBUG(level,code) \
50 if (dl_debug>=level) { code; } \
53 #define DLDEBUG(level,code) NOOP
56 #ifdef DL_UNLOAD_ALL_AT_EXIT
57 /* Close all dlopen'd files */
59 dl_unload_all_files(pTHX_ void *unused)
65 if ((sub = get_cv("DynaLoader::dl_unload_file", FALSE)) != NULL) {
66 dl_librefs = get_av("DynaLoader::dl_librefs", FALSE);
67 while ((dl_libref = av_pop(dl_librefs)) != &PL_sv_undef) {
72 XPUSHs(sv_2mortal(dl_libref));
74 call_sv((SV*)sub, G_DISCARD | G_NODEBUG);
83 dl_generic_private_init(pTHX) /* called by dl_*.xs dl_private_init() */
85 char *perl_dl_nonlazy;
90 #ifdef DL_LOADONCEONLY
91 dl_loaded_files = Nullhv;
95 SV *sv = get_sv("DynaLoader::dl_debug", 0);
96 dl_debug = sv ? SvIV(sv) : 0;
99 if ( (perl_dl_nonlazy = getenv("PERL_DL_NONLAZY")) != NULL )
100 dl_nonlazy = atoi(perl_dl_nonlazy);
102 DLDEBUG(1,PerlIO_printf(Perl_debug_log, "DynaLoader bind mode is 'non-lazy'\n"));
103 #ifdef DL_LOADONCEONLY
104 if (!dl_loaded_files)
105 dl_loaded_files = newHV(); /* provide cache for dl_*.xs if needed */
107 #ifdef DL_UNLOAD_ALL_AT_EXIT
108 call_atexit(&dl_unload_all_files, (void*)0);
113 /* SaveError() takes printf style args and saves the result in dl_last_error */
115 SaveError(pTHX_ char* pat, ...)
123 /* This code is based on croak/warn, see mess() in util.c */
126 msv = vmess(pat, &args);
129 message = SvPV(msv,len);
130 len++; /* include terminating null char */
132 /* Allocate some memory for the error message */
134 dl_last_error = (char*)saferealloc(dl_last_error, len);
136 dl_last_error = (char*)safemalloc(len);
138 /* Copy message into dl_last_error (including terminating null char) */
139 strncpy(dl_last_error, message, len) ;
140 DLDEBUG(2,PerlIO_printf(Perl_debug_log, "DynaLoader: stored error msg '%s'\n",dl_last_error));