1da2e3ebdSchin /***********************************************************************
2da2e3ebdSchin *                                                                      *
3da2e3ebdSchin *               This software is part of the ast package               *
4*b30d1939SAndy Fiddaman *          Copyright (c) 1997-2012 AT&T Intellectual Property          *
5da2e3ebdSchin *                      and is licensed under the                       *
6*b30d1939SAndy Fiddaman *                 Eclipse Public License, Version 1.0                  *
77c2fbfb3SApril Chin *                    by AT&T Intellectual Property                     *
8da2e3ebdSchin *                                                                      *
9da2e3ebdSchin *                A copy of the License is available at                 *
10*b30d1939SAndy Fiddaman *          http://www.eclipse.org/org/documents/epl-v10.html           *
11*b30d1939SAndy Fiddaman *         (with md5 checksum b35adb5213ca9657e911e9befb180842)         *
12da2e3ebdSchin *                                                                      *
13da2e3ebdSchin *              Information and Software Systems Research               *
14da2e3ebdSchin *                            AT&T Research                             *
15da2e3ebdSchin *                           Florham Park NJ                            *
16da2e3ebdSchin *                                                                      *
17da2e3ebdSchin *                 Glenn Fowler <gsf@research.att.com>                  *
18da2e3ebdSchin *                                                                      *
19da2e3ebdSchin ***********************************************************************/
20da2e3ebdSchin #pragma prototyped
21da2e3ebdSchin /*
22da2e3ebdSchin  * Glenn Fowler
23da2e3ebdSchin  * AT&T Research
24da2e3ebdSchin  */
26*b30d1939SAndy Fiddaman #include "dlllib.h"
28da2e3ebdSchin /*
29da2e3ebdSchin  * find and load lib plugin/module library name with optional version ver and dlopen() flags
30da2e3ebdSchin  * at least one dlopen() is called to initialize dlerror()
31da2e3ebdSchin  * if path!=0 then library path up to size chars copied to path with trailing 0
32da2e3ebdSchin  * if name contains a directory prefix then library search is limited to the dir and siblings
33da2e3ebdSchin  */
35da2e3ebdSchin extern void*
dllplugin(const char * lib,const char * name,const char * ver,unsigned long rel,unsigned long * cur,int flags,char * path,size_t size)36*b30d1939SAndy Fiddaman dllplugin(const char* lib, const char* name, const char* ver, unsigned long rel, unsigned long* cur, int flags, char* path, size_t size)
37da2e3ebdSchin {
38da2e3ebdSchin 	void*		dll;
39*b30d1939SAndy Fiddaman 	int		err;
40da2e3ebdSchin 	int		hit;
41da2e3ebdSchin 	Dllscan_t*	dls;
42da2e3ebdSchin 	Dllent_t*	dle;
44*b30d1939SAndy Fiddaman 	err = hit = 0;
45da2e3ebdSchin 	for (;;)
46da2e3ebdSchin 	{
47da2e3ebdSchin 		if (dls = dllsopen(lib, name, ver))
48da2e3ebdSchin 		{
49da2e3ebdSchin 			while (dle = dllsread(dls))
50da2e3ebdSchin 			{
51da2e3ebdSchin 				hit = 1;
52*b30d1939SAndy Fiddaman #if 0
53*b30d1939SAndy Fiddaman 			again:
54*b30d1939SAndy Fiddaman #endif
5534f9b3eeSRoland Mainz 				if (dll = dllopen(dle->path, flags|RTLD_GLOBAL|RTLD_PARENT))
56da2e3ebdSchin 				{
57*b30d1939SAndy Fiddaman 					if (!dllcheck(dll, dle->path, rel, cur))
58*b30d1939SAndy Fiddaman 					{
59*b30d1939SAndy Fiddaman 						err = state.error;
60*b30d1939SAndy Fiddaman 						dlclose(dll);
61*b30d1939SAndy Fiddaman 						dll = 0;
62*b30d1939SAndy Fiddaman 						continue;
63*b30d1939SAndy Fiddaman 					}
64da2e3ebdSchin 					if (path && size)
65*b30d1939SAndy Fiddaman 						strlcpy(path, dle->path, size);
66da2e3ebdSchin 					break;
67da2e3ebdSchin 				}
68da2e3ebdSchin 				else
69*b30d1939SAndy Fiddaman 				{
70*b30d1939SAndy Fiddaman #if 0
71*b30d1939SAndy Fiddaman 					/*
72*b30d1939SAndy Fiddaman 					 * dlopen() should load implicit libraries
73*b30d1939SAndy Fiddaman 					 * this code does that
74*b30d1939SAndy Fiddaman 					 * but it doesn't help on galadriel
75*b30d1939SAndy Fiddaman 					 */
76*b30d1939SAndy Fiddaman 
77*b30d1939SAndy Fiddaman 					char*	s;
78*b30d1939SAndy Fiddaman 					char*	e;
79*b30d1939SAndy Fiddaman 
80*b30d1939SAndy Fiddaman 					if ((s = dllerror(1)) && (e = strchr(s, ':')))
81*b30d1939SAndy Fiddaman 					{
82*b30d1939SAndy Fiddaman 						*e = 0;
83*b30d1939SAndy Fiddaman 						error(1, "AHA %s implicit", s);
84*b30d1939SAndy Fiddaman 						dll = dllplugin(lib, s, 0, 0, 0, flags, path, size);
85*b30d1939SAndy Fiddaman 						*e = ':';
86*b30d1939SAndy Fiddaman 						if (dll)
87*b30d1939SAndy Fiddaman 						{
88*b30d1939SAndy Fiddaman 							error(1, "AHA implicit %s => %s", s, path);
89*b30d1939SAndy Fiddaman 							goto again;
90*b30d1939SAndy Fiddaman 						}
91*b30d1939SAndy Fiddaman 					}
92*b30d1939SAndy Fiddaman #endif
93*b30d1939SAndy Fiddaman 					errorf("dll", NiL, 1, "dllplugin: %s dlopen failed: %s", dle->path, dllerror(1));
94*b30d1939SAndy Fiddaman 					err = state.error;
95*b30d1939SAndy Fiddaman 				}
96da2e3ebdSchin 			}
97da2e3ebdSchin 			dllsclose(dls);
98da2e3ebdSchin 		}
99da2e3ebdSchin 		if (hit)
100*b30d1939SAndy Fiddaman 		{
101*b30d1939SAndy Fiddaman 			if (!dll)
102*b30d1939SAndy Fiddaman 				state.error = err;
103da2e3ebdSchin 			return dll;
104*b30d1939SAndy Fiddaman 		}
105da2e3ebdSchin 		if (!lib)
106da2e3ebdSchin 			break;
107da2e3ebdSchin 		lib = 0;
108da2e3ebdSchin 	}
109*b30d1939SAndy Fiddaman 	if (dll = dllopen(name, flags))
110*b30d1939SAndy Fiddaman 	{
111*b30d1939SAndy Fiddaman 		if (!dllcheck(dll, name, rel, cur))
112*b30d1939SAndy Fiddaman 		{
113*b30d1939SAndy Fiddaman 			dlclose(dll);
114*b30d1939SAndy Fiddaman 			dll = 0;
115*b30d1939SAndy Fiddaman 		}
116*b30d1939SAndy Fiddaman 		else if (path && size)
117*b30d1939SAndy Fiddaman 			strlcpy(path, name, size);
118*b30d1939SAndy Fiddaman 	}
119da2e3ebdSchin 	return dll;
120da2e3ebdSchin }
121*b30d1939SAndy Fiddaman 
122*b30d1939SAndy Fiddaman extern void*
dllplug(const char * lib,const char * name,const char * ver,int flags,char * path,size_t size)123*b30d1939SAndy Fiddaman dllplug(const char* lib, const char* name, const char* ver, int flags, char* path, size_t size)
124*b30d1939SAndy Fiddaman {
125*b30d1939SAndy Fiddaman 	return dllplugin(lib, name, ver, 0, NiL, flags, path, size);
126*b30d1939SAndy Fiddaman }