Annotation of parser3/src/lib/ltdl/lt_dlloader.c, revision 1.2
1.1 moko 1: /* lt_dlloader.c -- dynamic library loader interface
2:
1.2 ! moko 3: Copyright (C) 2004, 2007-2008, 2011-2019, 2021-2024 Free Software
! 4: Foundation, Inc.
1.1 moko 5: Written by Gary V. Vaughan, 2004
6:
7: NOTE: The canonical source of this file is maintained with the
8: GNU Libtool package. Report bugs to bug-libtool@gnu.org.
9:
10: GNU Libltdl is free software; you can redistribute it and/or
11: modify it under the terms of the GNU Lesser General Public
12: License as published by the Free Software Foundation; either
13: version 2 of the License, or (at your option) any later version.
14:
15: As a special exception to the GNU Lesser General Public License,
16: if you distribute this file as part of a program or library that
17: is built using GNU Libtool, you may include this file under the
18: same distribution terms that you use for the rest of that program.
19:
20: GNU Libltdl is distributed in the hope that it will be useful,
21: but WITHOUT ANY WARRANTY; without even the implied warranty of
22: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23: GNU Lesser General Public License for more details.
24:
25: You should have received a copy of the GNU Lesser General Public
1.2 ! moko 26: License along with GNU Libltdl. If not, see <https://www.gnu.org/licenses/>.
1.1 moko 27: */
28:
29: #include "lt__private.h"
30: #include "lt_dlloader.h"
31:
32: #define RETURN_SUCCESS 0
33: #define RETURN_FAILURE 1
34:
35: static void * loader_callback (SList *item, void *userdata);
36:
37: /* A list of all the dlloaders we know about, each stored as a boxed
38: SList item: */
39: static SList *loaders = 0;
40:
41:
42: /* Return NULL, unless the loader in this ITEM has a matching name,
43: in which case we return the matching item so that its address is
44: passed back out (for possible freeing) by slist_remove. */
45: static void *
46: loader_callback (SList *item, void *userdata)
47: {
48: const lt_dlvtable *vtable = (const lt_dlvtable *) item->userdata;
49: const char * name = (const char *) userdata;
50:
51: assert (vtable);
52:
1.2 ! moko 53: return STREQ (vtable->name, name) ? (void *) item : NULL;
1.1 moko 54: }
55:
56:
57: /* Hook VTABLE into our global LOADERS list according to its own
58: PRIORITY field value. */
59: int
60: lt_dlloader_add (const lt_dlvtable *vtable)
61: {
62: SList *item;
63:
64: if ((vtable == 0) /* diagnose invalid vtable fields */
65: || (vtable->module_open == 0)
66: || (vtable->module_close == 0)
67: || (vtable->find_sym == 0)
68: || ((vtable->priority != LT_DLLOADER_PREPEND) &&
69: (vtable->priority != LT_DLLOADER_APPEND)))
70: {
71: LT__SETERROR (INVALID_LOADER);
72: return RETURN_FAILURE;
73: }
74:
75: item = slist_box (vtable);
76: if (!item)
77: {
78: (*lt__alloc_die) ();
79:
80: /* Let the caller know something went wrong if lt__alloc_die
81: doesn't abort. */
82: return RETURN_FAILURE;
83: }
84:
85: if (vtable->priority == LT_DLLOADER_PREPEND)
86: {
87: loaders = slist_cons (item, loaders);
88: }
89: else
90: {
91: assert (vtable->priority == LT_DLLOADER_APPEND);
92: loaders = slist_concat (loaders, item);
93: }
94:
95: return RETURN_SUCCESS;
96: }
97:
98: #ifdef LT_DEBUG_LOADERS
99: static void *
100: loader_dump_callback (SList *item, void *userdata)
101: {
102: const lt_dlvtable *vtable = (const lt_dlvtable *) item->userdata;
103: fprintf (stderr, ", %s", (vtable && vtable->name) ? vtable->name : "(null)");
104: return 0;
105: }
106:
107: void
108: lt_dlloader_dump (void)
109: {
110: fprintf (stderr, "loaders: ");
111: if (!loaders)
112: {
113: fprintf (stderr, "(empty)");
114: }
115: else
116: {
117: const lt_dlvtable *head = (const lt_dlvtable *) loaders->userdata;
118: fprintf (stderr, "%s", (head && head->name) ? head->name : "(null)");
119: if (slist_tail (loaders))
120: slist_foreach (slist_tail (loaders), loader_dump_callback, NULL);
121: }
122: fprintf (stderr, "\n");
123: }
124: #endif
125:
126: /* An iterator for the global loader list: if LOADER is NULL, then
127: return the first element, otherwise the following element. */
128: lt_dlloader
129: lt_dlloader_next (lt_dlloader loader)
130: {
131: SList *item = (SList *) loader;
132: return (lt_dlloader) (item ? item->next : loaders);
133: }
134:
135:
136: /* Non-destructive unboxing of a loader. */
137: const lt_dlvtable *
138: lt_dlloader_get (lt_dlloader loader)
139: {
140: return (const lt_dlvtable *) (loader ? ((SList *) loader)->userdata : NULL);
141: }
142:
143:
144: /* Return the contents of the first item in the global loader list
145: with a matching NAME after removing it from that list. If there
146: was no match, return NULL; if there is an error, return NULL and
147: set an error for lt_dlerror; do not set an error if only resident
148: modules need this loader; in either case, the loader list is not
149: changed if NULL is returned. */
150: lt_dlvtable *
151: lt_dlloader_remove (const char *name)
152: {
153: const lt_dlvtable * vtable = lt_dlloader_find (name);
154: static const char id_string[] = "lt_dlloader_remove";
155: lt_dlinterface_id iface;
156: lt_dlhandle handle = 0;
157: int in_use = 0;
158: int in_use_by_resident = 0;
159:
160: if (!vtable)
161: {
162: LT__SETERROR (INVALID_LOADER);
163: return 0;
164: }
165:
1.2 ! moko 166: /* Fail if there are any open modules that use this loader. */
1.1 moko 167: iface = lt_dlinterface_register (id_string, NULL);
1.2 ! moko 168: if (!iface)
! 169: /* No memory, error is already set. */
! 170: return 0;
! 171:
1.1 moko 172: while ((handle = lt_dlhandle_iterate (iface, handle)))
173: {
174: lt_dlhandle cur = handle;
175: if (cur->vtable == vtable)
176: {
177: in_use = 1;
178: if (lt_dlisresident (handle))
179: in_use_by_resident = 1;
180: }
181: }
182: lt_dlinterface_free (iface);
183: if (in_use)
184: {
185: if (!in_use_by_resident)
186: LT__SETERROR (REMOVE_LOADER);
187: return 0;
188: }
189:
190: /* Call the loader finalisation function. */
191: if (vtable && vtable->dlloader_exit)
192: {
193: if ((*vtable->dlloader_exit) (vtable->dlloader_data) != 0)
194: {
195: /* If there is an exit function, and it returns non-zero
196: then it must set an error, and we will not remove it
197: from the list. */
198: return 0;
199: }
200: }
201:
202: /* If we got this far, remove the loader from our global list. */
203: return (lt_dlvtable *)
204: slist_unbox ((SList *) slist_remove (&loaders, loader_callback, (void *) name));
205: }
206:
207:
208: const lt_dlvtable *
209: lt_dlloader_find (const char *name)
210: {
211: return lt_dlloader_get (slist_find (loaders, loader_callback, (void *) name));
212: }
E-mail: