/***********************************************************************1* *2* This software is part of the ast package *3* Copyright (c) 1997-2012 AT&T Intellectual Property *4* and is licensed under the *5* Eclipse Public License, Version 1.0 *6* by AT&T Intellectual Property *7* *8* A copy of the License is available at *9* http://www.eclipse.org/org/documents/epl-v10.html *10* (with md5 checksum b35adb5213ca9657e911e9befb180842) *11* *12* Information and Software Systems Research *13* AT&T Research *14* Florham Park NJ *15* *16* Glenn Fowler <[email protected]> *17* *18***********************************************************************/19#pragma prototyped20/*21* Glenn Fowler22* AT&T Research23*/2425#include "dlllib.h"2627/*28* find and load lib plugin/module library name with optional version ver and dlopen() flags29* at least one dlopen() is called to initialize dlerror()30* if path!=0 then library path up to size chars copied to path with trailing 031* if name contains a directory prefix then library search is limited to the dir and siblings32*/3334extern void*35dllplugin(const char* lib, const char* name, const char* ver, unsigned long rel, unsigned long* cur, int flags, char* path, size_t size)36{37void* dll;38int err;39int hit;40Dllscan_t* dls;41Dllent_t* dle;4243err = hit = 0;44for (;;)45{46if (dls = dllsopen(lib, name, ver))47{48while (dle = dllsread(dls))49{50hit = 1;51#if 052again:53#endif54if (dll = dllopen(dle->path, flags|RTLD_GLOBAL|RTLD_PARENT))55{56if (!dllcheck(dll, dle->path, rel, cur))57{58err = state.error;59dlclose(dll);60dll = 0;61continue;62}63if (path && size)64strlcpy(path, dle->path, size);65break;66}67else68{69#if 070/*71* dlopen() should load implicit libraries72* this code does that73* but it doesn't help on galadriel74*/7576char* s;77char* e;7879if ((s = dllerror(1)) && (e = strchr(s, ':')))80{81*e = 0;82error(1, "AHA %s implicit", s);83dll = dllplugin(lib, s, 0, 0, 0, flags, path, size);84*e = ':';85if (dll)86{87error(1, "AHA implicit %s => %s", s, path);88goto again;89}90}91#endif92errorf("dll", NiL, 1, "dllplugin: %s dlopen failed: %s", dle->path, dllerror(1));93err = state.error;94}95}96dllsclose(dls);97}98if (hit)99{100if (!dll)101state.error = err;102return dll;103}104if (!lib)105break;106lib = 0;107}108if (dll = dllopen(name, flags))109{110if (!dllcheck(dll, name, rel, cur))111{112dlclose(dll);113dll = 0;114}115else if (path && size)116strlcpy(path, name, size);117}118return dll;119}120121extern void*122dllplug(const char* lib, const char* name, const char* ver, int flags, char* path, size_t size)123{124return dllplugin(lib, name, ver, 0, NiL, flags, path, size);125}126127128