123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- #include "lt__private.h"
- #include "lt_dlloader.h"
- #define RETURN_SUCCESS 0
- #define RETURN_FAILURE 1
- static void * loader_callback (SList *item, void *userdata);
- static SList *loaders = 0;
- static void *
- loader_callback (SList *item, void *userdata)
- {
- const lt_dlvtable *vtable = (const lt_dlvtable *) item->userdata;
- const char * name = (const char *) userdata;
- assert (vtable);
- return STREQ (vtable->name, name) ? (void *) item : NULL;
- }
- int
- lt_dlloader_add (const lt_dlvtable *vtable)
- {
- SList *item;
- if ((vtable == 0)
- || (vtable->module_open == 0)
- || (vtable->module_close == 0)
- || (vtable->find_sym == 0)
- || ((vtable->priority != LT_DLLOADER_PREPEND) &&
- (vtable->priority != LT_DLLOADER_APPEND)))
- {
- LT__SETERROR (INVALID_LOADER);
- return RETURN_FAILURE;
- }
- item = slist_box (vtable);
- if (!item)
- {
- (*lt__alloc_die) ();
-
- return RETURN_FAILURE;
- }
- if (vtable->priority == LT_DLLOADER_PREPEND)
- {
- loaders = slist_cons (item, loaders);
- }
- else
- {
- assert (vtable->priority == LT_DLLOADER_APPEND);
- loaders = slist_concat (loaders, item);
- }
- return RETURN_SUCCESS;
- }
- #ifdef LT_DEBUG_LOADERS
- static void *
- loader_dump_callback (SList *item, void *userdata)
- {
- const lt_dlvtable *vtable = (const lt_dlvtable *) item->userdata;
- fprintf (stderr, ", %s", (vtable && vtable->name) ? vtable->name : "(null)");
- return 0;
- }
- void
- lt_dlloader_dump (void)
- {
- fprintf (stderr, "loaders: ");
- if (!loaders)
- {
- fprintf (stderr, "(empty)");
- }
- else
- {
- const lt_dlvtable *head = (const lt_dlvtable *) loaders->userdata;
- fprintf (stderr, "%s", (head && head->name) ? head->name : "(null)");
- if (slist_tail (loaders))
- slist_foreach (slist_tail (loaders), loader_dump_callback, NULL);
- }
- fprintf (stderr, "\n");
- }
- #endif
- lt_dlloader
- lt_dlloader_next (lt_dlloader loader)
- {
- SList *item = (SList *) loader;
- return (lt_dlloader) (item ? item->next : loaders);
- }
- const lt_dlvtable *
- lt_dlloader_get (lt_dlloader loader)
- {
- return (const lt_dlvtable *) (loader ? ((SList *) loader)->userdata : NULL);
- }
- lt_dlvtable *
- lt_dlloader_remove (const char *name)
- {
- const lt_dlvtable * vtable = lt_dlloader_find (name);
- static const char id_string[] = "lt_dlloader_remove";
- lt_dlinterface_id iface;
- lt_dlhandle handle = 0;
- int in_use = 0;
- int in_use_by_resident = 0;
- if (!vtable)
- {
- LT__SETERROR (INVALID_LOADER);
- return 0;
- }
-
- iface = lt_dlinterface_register (id_string, NULL);
- while ((handle = lt_dlhandle_iterate (iface, handle)))
- {
- lt_dlhandle cur = handle;
- if (cur->vtable == vtable)
- {
- in_use = 1;
- if (lt_dlisresident (handle))
- in_use_by_resident = 1;
- }
- }
- lt_dlinterface_free (iface);
- if (in_use)
- {
- if (!in_use_by_resident)
- LT__SETERROR (REMOVE_LOADER);
- return 0;
- }
-
- if (vtable && vtable->dlloader_exit)
- {
- if ((*vtable->dlloader_exit) (vtable->dlloader_data) != 0)
- {
-
- return 0;
- }
- }
-
- return (lt_dlvtable *)
- slist_unbox ((SList *) slist_remove (&loaders, loader_callback, (void *) name));
- }
- const lt_dlvtable *
- lt_dlloader_find (const char *name)
- {
- return lt_dlloader_get (slist_find (loaders, loader_callback, (void *) name));
- }
|