2 * Copyright (c) 2010 Todd C. Miller <Todd.Miller@courtesan.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
29 #endif /* STDC_HEADERS */
32 #endif /* HAVE_STRING_H */
35 #endif /* HAVE_STRINGS_H */
38 #include "compat/dlfcn.h"
43 * Emulate dlopen() using shl_load().
48 # define DYNAMIC_PATH 0
52 sudo_dlopen(const char *path, int mode)
54 int flags = DYNAMIC_PATH;
57 mode = RTLD_LAZY; /* default behavior */
59 /* We don't support RTLD_GLOBAL or RTLD_LOCAL yet. */
60 if (ISSET(mode, RTLD_LAZY))
61 flags |= BIND_DEFERRED;
62 if (ISSET(mode, RTLD_NOW))
63 flags |= BIND_IMMEDIATE;
65 return (void *)shl_load(path, flags, 0L);
69 sudo_dlclose(void *handle)
71 return shl_unload((shl_t)handle);
75 sudo_dlsym(void *vhandle, const char *symbol)
77 shl_t handle = vhandle;
80 (void)shl_findsym(&handle, symbol, TYPE_UNDEFINED, &value);
88 return strerror(errno);
91 #else /* !HAVE_SHL_LOAD */
94 * Emulate dlopen() using a static list of symbols compiled into sudo.
97 struct sudo_preload_table {
101 extern struct sudo_preload_table sudo_preload_table[];
104 sudo_dlopen(const char *path, int mode)
110 sudo_dlclose(void *handle)
116 sudo_dlsym(void *handle, const char *symbol)
118 struct sudo_preload_table *sym;
120 for (sym = sudo_preload_table; sym->name != NULL; sym++) {
121 if (strcmp(symbol, sym->name) == 0)
130 return strerror(errno);
133 #endif /* HAVE_SHL_LOAD */