Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 1 | /*********************************************************** |
| 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
| 4 | |
| 5 | All Rights Reserved |
| 6 | |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame^] | 7 | Copyright (c) 2000, BeOpen.com. |
| 8 | Copyright (c) 1995-2000, Corporation for National Research Initiatives. |
| 9 | Copyright (c) 1990-1995, Stichting Mathematisch Centrum. |
| 10 | All rights reserved. |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 11 | |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame^] | 12 | See the file "Misc/COPYRIGHT" for information on usage and |
| 13 | redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 14 | |
| 15 | ******************************************************************/ |
| 16 | |
| 17 | /* Support for dynamic loading of extension modules */ |
| 18 | |
| 19 | #include <kernel/image.h> |
| 20 | #include <kernel/OS.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <unistd.h> |
| 23 | |
| 24 | #include "Python.h" |
| 25 | #include "importdl.h" |
| 26 | |
| 27 | const struct filedescr _PyImport_DynLoadFiletab[] = { |
| 28 | {".so", "rb", C_EXTENSION}, |
| 29 | {"module.so", "rb", C_EXTENSION}, |
| 30 | {0, 0} |
| 31 | }; |
| 32 | |
| 33 | #if defined(MAXPATHLEN) && !defined(_SYS_PARAM_H) |
| 34 | #undef MAXPATHLEN |
| 35 | #endif |
| 36 | |
| 37 | #ifdef WITH_THREAD |
| 38 | #include "pythread.h" |
| 39 | static PyThread_type_lock beos_dyn_lock; |
| 40 | #endif |
| 41 | |
| 42 | static PyObject *beos_dyn_images = NULL; |
| 43 | |
| 44 | /* ---------------------------------------------------------------------- |
| 45 | * BeOS dynamic loading support |
| 46 | * |
| 47 | * This uses shared libraries, but BeOS has its own way of doing things |
| 48 | * (much easier than dlfnc.h, from the look of things). We'll use a |
| 49 | * Python Dictionary object to store the images_ids so we can be very |
| 50 | * nice and unload them when we exit. |
| 51 | * |
| 52 | * Note that this is thread-safe. Probably irrelevent, because of losing |
| 53 | * systems... Python probably disables threads while loading modules. |
| 54 | * Note the use of "probably"! Better to be safe than sorry. [chrish] |
| 55 | * |
| 56 | * As of 1.5.1 this should also work properly when you've configured |
| 57 | * Python without thread support; the 1.5 version required it, which wasn't |
| 58 | * very friendly. Note that I haven't tested it without threading... why |
| 59 | * would you want to avoid threads on BeOS? [chrish] |
| 60 | * |
| 61 | * As of 1.5.2, the PyImport_BeImageID() function has been removed; Donn |
| 62 | * tells me it's not necessary anymore because of PyCObject_Import(). |
| 63 | * [chrish] |
| 64 | */ |
| 65 | |
| 66 | /* Whack an item; the item is an image_id in disguise, so we'll call |
| 67 | * unload_add_on() for it. |
| 68 | */ |
| 69 | static void beos_nuke_dyn( PyObject *item ) |
| 70 | { |
| 71 | status_t retval; |
| 72 | |
| 73 | if( item ) { |
| 74 | image_id id = (image_id)PyInt_AsLong( item ); |
| 75 | |
| 76 | retval = unload_add_on( id ); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | /* atexit() handler that'll call unload_add_on() for every item in the |
| 81 | * dictionary. |
| 82 | */ |
| 83 | static void beos_cleanup_dyn( void ) |
| 84 | { |
| 85 | if( beos_dyn_images ) { |
| 86 | int idx; |
| 87 | int list_size; |
| 88 | PyObject *id_list; |
| 89 | |
| 90 | #ifdef WITH_THREAD |
| 91 | PyThread_acquire_lock( beos_dyn_lock, 1 ); |
| 92 | #endif |
| 93 | |
| 94 | id_list = PyDict_Values( beos_dyn_images ); |
| 95 | |
| 96 | list_size = PyList_Size( id_list ); |
| 97 | for( idx = 0; idx < list_size; idx++ ) { |
| 98 | PyObject *the_item; |
| 99 | |
| 100 | the_item = PyList_GetItem( id_list, idx ); |
| 101 | beos_nuke_dyn( the_item ); |
| 102 | } |
| 103 | |
| 104 | PyDict_Clear( beos_dyn_images ); |
| 105 | |
| 106 | #ifdef WITH_THREAD |
| 107 | PyThread_free_lock( beos_dyn_lock ); |
| 108 | #endif |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | /* |
| 113 | * Initialize our dictionary, and the dictionary mutex. |
| 114 | */ |
| 115 | static void beos_init_dyn( void ) |
| 116 | { |
| 117 | /* We're protected from a race condition here by the atomic init_count |
| 118 | * variable. |
| 119 | */ |
| 120 | static int32 init_count = 0; |
| 121 | int32 val; |
| 122 | |
| 123 | val = atomic_add( &init_count, 1 ); |
| 124 | if( beos_dyn_images == NULL && val == 0 ) { |
| 125 | beos_dyn_images = PyDict_New(); |
| 126 | #ifdef WITH_THREAD |
| 127 | beos_dyn_lock = PyThread_allocate_lock(); |
| 128 | #endif |
| 129 | atexit( beos_cleanup_dyn ); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | /* |
| 134 | * Add an image_id to the dictionary; the module name of the loaded image |
| 135 | * is the key. Note that if the key is already in the dict, we unload |
| 136 | * that image; this should allow reload() to work on dynamically loaded |
| 137 | * modules (super-keen!). |
| 138 | */ |
| 139 | static void beos_add_dyn( char *name, image_id id ) |
| 140 | { |
| 141 | int retval; |
| 142 | PyObject *py_id; |
| 143 | |
| 144 | if( beos_dyn_images == NULL ) { |
| 145 | beos_init_dyn(); |
| 146 | } |
| 147 | |
| 148 | #ifdef WITH_THREAD |
| 149 | retval = PyThread_acquire_lock( beos_dyn_lock, 1 ); |
| 150 | #endif |
| 151 | |
| 152 | /* If there's already an object with this key in the dictionary, |
| 153 | * we're doing a reload(), so let's nuke it. |
| 154 | */ |
| 155 | py_id = PyDict_GetItemString( beos_dyn_images, name ); |
| 156 | if( py_id ) { |
| 157 | beos_nuke_dyn( py_id ); |
| 158 | retval = PyDict_DelItemString( beos_dyn_images, name ); |
| 159 | } |
| 160 | |
| 161 | py_id = PyInt_FromLong( (long)id ); |
| 162 | if( py_id ) { |
| 163 | retval = PyDict_SetItemString( beos_dyn_images, name, py_id ); |
| 164 | } |
| 165 | |
| 166 | #ifdef WITH_THREAD |
| 167 | PyThread_release_lock( beos_dyn_lock ); |
| 168 | #endif |
| 169 | } |
| 170 | |
| 171 | |
| 172 | |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 173 | dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname, |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 174 | const char *pathname, FILE *fp) |
| 175 | { |
| 176 | dl_funcptr p; |
| 177 | image_id the_id; |
| 178 | status_t retval; |
| 179 | char fullpath[PATH_MAX]; |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 180 | char funcname[258]; |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 181 | |
| 182 | if( Py_VerboseFlag ) { |
| 183 | printf( "load_add_on( %s )\n", pathname ); |
| 184 | } |
| 185 | |
| 186 | /* Hmm, this old bug appears to have regenerated itself; if the |
| 187 | * path isn't absolute, load_add_on() will fail. Reported to Be |
| 188 | * April 21, 1998. |
| 189 | */ |
| 190 | if( pathname[0] != '/' ) { |
| 191 | (void)getcwd( fullpath, PATH_MAX ); |
| 192 | (void)strncat( fullpath, "/", PATH_MAX ); |
| 193 | (void)strncat( fullpath, pathname, PATH_MAX ); |
| 194 | |
| 195 | if( Py_VerboseFlag ) { |
| 196 | printf( "load_add_on( %s )\n", fullpath ); |
| 197 | } |
| 198 | } else { |
| 199 | (void)strcpy( fullpath, pathname ); |
| 200 | } |
| 201 | |
| 202 | the_id = load_add_on( fullpath ); |
| 203 | if( the_id < B_NO_ERROR ) { |
| 204 | /* It's too bad load_add_on() doesn't set errno or something... |
| 205 | */ |
| 206 | char buff[256]; /* hate hard-coded string sizes... */ |
| 207 | |
| 208 | if( Py_VerboseFlag ) { |
| 209 | printf( "load_add_on( %s ) failed", fullpath ); |
| 210 | } |
| 211 | |
| 212 | switch( the_id ) { |
| 213 | case B_ERROR: |
| 214 | sprintf( buff, "BeOS: Failed to load %.200s", fullpath ); |
| 215 | break; |
| 216 | default: |
| 217 | sprintf( buff, "Unknown error loading %.200s", fullpath ); |
| 218 | break; |
| 219 | } |
| 220 | |
| 221 | PyErr_SetString( PyExc_ImportError, buff ); |
| 222 | return NULL; |
| 223 | } |
| 224 | |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 225 | sprintf(funcname, "init%.200s", shortname); |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 226 | if( Py_VerboseFlag ) { |
| 227 | printf( "get_image_symbol( %s )\n", funcname ); |
| 228 | } |
| 229 | |
| 230 | retval = get_image_symbol( the_id, funcname, B_SYMBOL_TYPE_TEXT, &p ); |
| 231 | if( retval != B_NO_ERROR || p == NULL ) { |
| 232 | /* That's bad, we can't find that symbol in the module... |
| 233 | */ |
| 234 | char buff[256]; /* hate hard-coded string sizes... */ |
| 235 | |
| 236 | if( Py_VerboseFlag ) { |
| 237 | printf( "get_image_symbol( %s ) failed", funcname ); |
| 238 | } |
| 239 | |
| 240 | switch( retval ) { |
| 241 | case B_BAD_IMAGE_ID: |
| 242 | sprintf( buff, "can't load init function for dynamic module: " |
| 243 | "Invalid image ID for %.180s", fullpath ); |
| 244 | break; |
| 245 | case B_BAD_INDEX: |
| 246 | sprintf( buff, "can't load init function for dynamic module: " |
| 247 | "Bad index for %.180s", funcname ); |
| 248 | break; |
| 249 | default: |
| 250 | sprintf( buff, "can't load init function for dynamic module: " |
| 251 | "Unknown error looking up %.180s", funcname ); |
| 252 | break; |
| 253 | } |
| 254 | |
| 255 | retval = unload_add_on( the_id ); |
| 256 | |
| 257 | PyErr_SetString( PyExc_ImportError, buff ); |
| 258 | return NULL; |
| 259 | } |
| 260 | |
| 261 | /* Save the module name and image ID for later so we can clean up |
| 262 | * gracefully. |
| 263 | */ |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 264 | beos_add_dyn( fqname, the_id ); |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 265 | |
| 266 | return p; |
| 267 | } |