1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
|
/* sqUnixExternalPrims.c -- Unix named primitives and loadable modules
*
* Copyright (C) 1996-2009 by Ian Piumarta
* All rights reserved.
*
* This file is part of Unix Squeak.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/* Last edited: 2010-04-09 00:37:36 by piumarta on ubuntu
*/
#define DEBUG 0
#include "sq.h" /* sqUnixConfig.h */
#if (DEBUG)
# define fdebugf(ARGS) fprintf ARGS
#else
# define fdebugf(ARGS)
#endif
#if !defined(HAVE_DLOPEN) && defined(HAVE_DYLD)
# include "dlfcn-dyld.c"
#endif
#if defined(HAVE_DLOPEN) /* non-starter without this! */
#ifdef HAVE_DLFCN_H
# include <dlfcn.h>
#else
extern void *dlopen (const char *filename, int flag);
extern const char *dlerror(void);
extern void *dlsym(void *handle, const char *symbol);
extern int dlclose (void *handle);
#endif
#include <limits.h>
#include <sys/stat.h>
/* get a value for RTLD_NOW, with increasing levels of desperation... */
#if !defined(RTLD_NOW)
# if defined(DL_NOW)
# define RTLD_NOW DL_NOW
# elif defined(RTLD_LAZY)
# define RTLD_NOW RTLD_LAZY
# elif defined(DL_LAZY)
# define RTLD_NOW DL_LAZY
# else
# warning: defining RTLD_NOW as 1
# define RTLD_NOW 1
# endif
#endif
#if !defined(RTLD_GLOBAL)
# define RTLD_GLOBAL 0
#endif
#ifndef NAME_MAX
# ifdef MAXPATHLEN
# define NAME_MAX MAXPATHLEN
# else
# ifdef FILENAME_MAX
# define NAME_MAX FILENAME_MAX
# else
# define NAME_MAX 256 /* nobody has fewer than this (since the PDP-8 ;) */
# endif
# endif
#endif
#if !defined(HAVE_SNPRINTF)
# if defined(HAVE___SNPRINTF) /* Solaris 2.5 */
extern int __snprintf(char *buf, size_t limit, const char *fmt, ...);
# define snprintf __snprintf
# define HAVE_SNPRINTF
# endif
#endif
#if (DEBUG)
# define sqIgnorePluginErrors 0
#else
extern int sqIgnorePluginErrors;
#endif
/*** options ***/
extern char *squeakPlugins;
/*** configured variables ***/
extern char vmLibDir[];
extern char vmPath[];
/*** local functions ***/
#if 1 /* simplified plugin logic */
static void *tryLoadModule(char *in, char *name)
{
char path[PATH_MAX], *out= path;
void *handle= 0;
int c;
while ((c= *in++) && ':' != c) { /* copy next plugin path to path[] */
switch (c) {
case '%':
if ('n' == *in || 'N' == *in) { /* replace %n with name of plugin */
++in;
strcpy(out, name);
out += strlen(name);
continue;
}
if ('%' == *in) {
++in;
*out++= '%';
continue;
}
/* fall through... */
default:
*out++= c;
continue;
}
}
sprintf(out, "/" MODULE_PREFIX "%s" MODULE_SUFFIX, name);
handle= dlopen(path, RTLD_NOW | RTLD_GLOBAL);
fdebugf((stderr, "tryLoading(%s) = %p\n", path, handle));
if (!handle) {
struct stat buf;
if ((0 == stat(path, &buf)) && ! S_ISDIR(buf.st_mode))
fprintf(stderr, "%s\n", dlerror());
}
return handle;
}
void *ioLoadModule(char *pluginName)
{
char path[PATH_MAX];
char *dir= squeakPlugins;
void *handle= 0;
if ((0 == pluginName) || ('\0' == pluginName[0])) { /* find module in main program */
handle= dlopen(0, RTLD_NOW | RTLD_GLOBAL);
if (handle == 0) {
fprintf(stderr, "ioLoadModule(<intrinsic>): %s\n", dlerror());
}
else {
fdebugf((stderr, "loaded: <intrinsic>\n"));
}
return handle;
}
/* try loading the name unmodified */
if ((handle= dlopen(pluginName, RTLD_NOW | RTLD_GLOBAL)))
return handle;
/* try loading {pluginPaths}/MODULE_PREFIX<name>MODULE_SUFFIX */
while (*dir) {
if ((handle= tryLoadModule(dir, pluginName)))
return handle;
while (*dir && ':' != *dir++)
;
}
/* try dlopen()ing LIBRARY_PREFIX<name>LIBRARY_SUFFIX searching only the default locations modulo LD_LIBRARY_PATH et al */
# if defined(HAVE_SNPRINTF)
snprintf(path, sizeof(path), "%s%s%s", LIBRARY_PREFIX, pluginName, LIBRARY_SUFFIX);
# else
sprintf(path, "%s%s%s", LIBRARY_PREFIX, pluginName, LIBRARY_SUFFIX);
# endif
if ((handle= dlopen(path, RTLD_NOW | RTLD_GLOBAL)))
return handle;
fdebugf((stderr, "ioLoadModule(%s) = %p\n", path, handle));
return handle;
}
#else /* obsolete plugin logic */
/* Attempt to load the shared library named by the concatenation of prefix,
* moduleName and suffix. Answer the new module entry, or 0 if the shared
* library could not be loaded.
*/
static void *tryLoading(char *dirName, char *moduleName)
{
static char *prefixes[]= { "", "lib", 0 };
static char *suffixes[]= { "", ".so", ".dylib", 0 };
void *handle= 0;
char **prefix= 0, **suffix= 0;
for (prefix= prefixes; *prefix; ++prefix)
for (suffix= suffixes; *suffix; ++suffix)
{
char libName[NAME_MAX + 32]; /* headroom for prefix/suffix */
struct stat buf;
int err;
sprintf(libName, "%s%s%s%s", dirName, *prefix, moduleName, *suffix);
if ((err= stat(libName, &buf)))
fdebugf((stderr, "cannot read: %s\n", libName));
else
{
if (S_ISDIR(buf.st_mode))
fdebugf((stderr, "ignoring directory: %s\n", libName));
else
{
fdebugf((stderr, "tryLoading %s\n", libName));
handle= dlopen(libName, RTLD_NOW | RTLD_GLOBAL);
if (handle == 0)
{
/*if ((!err) && !(sqIgnorePluginErrors))*/
fprintf(stderr, "ioLoadModule(%s):\n %s\n", libName, dlerror());
}
else
{
# if DEBUG
printf("squeak: loaded plugin `%s'\n", libName);
# endif
return handle;
}
}
}
}
return 0;
}
static void *tryLoadingPath(char *varName, char *pluginName)
{
char *path= getenv(varName);
void *handle= 0;
if (path)
{
char pbuf[MAXPATHLEN];
fdebugf((stderr, "try %s=%s\n", varName, path));
strncpy(pbuf, path, sizeof(pbuf));
pbuf[sizeof(pbuf) - 1]= '\0';
for (path= strtok(pbuf, ":");
path != 0;
path= strtok(0, ":"))
{
char buf[MAXPATHLEN];
sprintf(buf, "%s/", path);
fdebugf((stderr, " path dir = %s\n", buf));
if ((handle= tryLoading(buf, pluginName)) != 0)
break;
}
}
return handle;
}
/* Find and load the named module. Answer 0 if not found (do NOT fail
* the primitive!).
*/
void *ioLoadModule(char *pluginName)
{
void *handle= 0;
if ((pluginName == 0) || (pluginName[0] == '\0'))
{
handle= dlopen(0, RTLD_NOW | RTLD_GLOBAL);
if (handle == 0)
fprintf(stderr, "ioLoadModule(<intrinsic>): %s\n", dlerror());
else
{
fdebugf((stderr, "loaded: <intrinsic>\n"));
return handle;
}
}
if (squeakPlugins)
{
char path[NAME_MAX];
char c, *in= squeakPlugins, *out= path;
while ((c= *in++))
{
if (c == '%' && ((*in == 'n') || (*in == 'N')))
{
++in;
strcpy(out, pluginName);
out+= strlen(pluginName);
}
else
*out++= c;
}
*out= '\0';
fdebugf((stderr, "ioLoadModule plugins = %s\n path = %s\n",
squeakPlugins, path));
if ((handle= tryLoading("", path)))
return handle;
*out++= '/';
*out= '\0';
if ((handle= tryLoading(path, pluginName)))
return handle;
}
if (( handle= tryLoading( "./", pluginName))
|| (handle= tryLoadingPath("SQUEAK_PLUGIN_PATH", pluginName))
|| (handle= tryLoading( VM_LIBDIR"/", pluginName))
|| (handle= tryLoadingPath("LD_LIBRARY_PATH", pluginName))
|| (handle= tryLoading( "", pluginName))
# if defined(VM_X11DIR)
|| (handle= tryLoading(VM_X11DIR"/", pluginName))
# endif
)
return handle;
#if defined(DARWIN)
// look in the bundle contents dir
{
static char *contents= 0;
if (!contents)
{
char *delim;
contents= strdup(vmPath);
if ((delim= strrchr(contents, '/')))
delim[1]= '\0';
}
if ((handle= tryLoading(contents, pluginName)))
return handle;
}
// the following is needed so that, for example, the FFI can pick up
// things like <cdecl: 'xyz' module: 'CoreServices'>
{
static char *frameworks[]=
{
"/System/Library/Frameworks",
"/System/Library/Frameworks/CoreServices.framework/Frameworks",
"/System/Library/Frameworks/ApplicationServices.framework/Frameworks",
"/System/Library/Frameworks/Carbon.framework/Frameworks",
0
};
char **framework= 0;
for (framework= frameworks; *framework; ++framework)
{
char path[NAME_MAX];
sprintf(path, "%s/%s.framework/", *framework, pluginName);
if ((handle= tryLoading(path, pluginName)))
return handle;
}
}
#endif /* DARWIN */
/* finally (for VM hackers) try the pre-install build location */
{
char pluginDir[MAXPATHLEN];
# ifdef HAVE_SNPRINTF
snprintf(pluginDir, sizeof(pluginDir), "%s%s/", vmPath, pluginName);
# else
sprintf(pluginDir, "%s%s/", vmPath, pluginName);
# endif
if ((handle= tryLoading(pluginDir, pluginName)))
return handle;
# ifdef HAVE_SNPRINTF
snprintf(pluginDir, sizeof(pluginDir), "%s%s/.libs/", vmPath, pluginName);
# else
sprintf(pluginDir, "%s%s/.libs/", vmPath, pluginName);
# endif
if ((handle= tryLoading(pluginDir, pluginName)))
return handle;
}
#if DEBUG
fprintf(stderr, "squeak: could not load plugin `%s'\n", pluginName);
#endif
return 0;
}
#endif /* obsolete plugin logic */
/* Find a function in a loaded module. Answer 0 if not found (do NOT
* fail the primitive!).
*/
void *ioFindExternalFunctionIn(char *lookupName, void *moduleHandle)
{
void *fn= dlsym(moduleHandle, lookupName);
fdebugf((stderr, "ioFindExternalFunctionIn(%s, %p) = %p\n", lookupName, moduleHandle, fn));
if ((fn == 0) && (!sqIgnorePluginErrors)
&& strcmp(lookupName, "initialiseModule")
&& strcmp(lookupName, "shutdownModule")
&& strcmp(lookupName, "setInterpreter")
&& strcmp(lookupName, "getModuleName"))
fprintf(stderr, "ioFindExternalFunctionIn(%s, %p):\n %s\n", lookupName, moduleHandle, dlerror());
return fn;
}
/* Free the module with the associated handle. Answer 0 on error (do
* NOT fail the primitive!).
*/
sqInt ioFreeModule(void *moduleHandle)
{
if (dlclose(moduleHandle))
{
fdebugf((stderr, "ioFreeModule(%d): %s\n", moduleHandle, dlerror()));
return 0;
}
return 1;
}
#else /* !HAVE_DLOPEN */
void *ioLoadModule(char *pluginName)
{
return 0;
}
void *ioFindExternalFunctionIn(char *lookupName, void *moduleHandle)
{
return 0;
}
sqInt ioFreeModule(void *moduleHandle)
{
return 0;
}
#endif /* !HAVE_DLOPEN */
|