fix macro expansion
[m6w6/ext-psi] / src / dl.h
1 /*******************************************************************************
2 Copyright (c) 2018, Michael Wallner <mike@php.net>.
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 * Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
18 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *******************************************************************************/
25
26 #ifndef PSI_DLSYM_H
27 #define PSI_DLSYM_H
28
29 #include <stddef.h>
30 #include <dlfcn.h>
31
32 #ifndef RTLD_NEXT
33 # define RTLD_NEXT ((void *) -1l)
34 #endif
35 #ifndef RTLD_DEFAULT
36 # define RTLD_DEFAULT ((void *) 0)
37 #endif
38
39 #include "plist.h"
40
41 static inline void *psi_dlsym(struct psi_plist *dllist, const char *name, const char *redir)
42 {
43 void *dl, *sym = NULL;
44 const char *test = redir ?: name;
45
46 again:
47 if (dllist) {
48 size_t i = 0;
49
50 while (!sym && psi_plist_get(dllist, i++, &dl)) {
51 sym = dlsym(dl, test);
52 }
53 }
54 if (!sym) {
55 sym = dlsym(RTLD_DEFAULT, test);
56 }
57 if (!sym && test == redir) {
58 test = name;
59 goto again;
60 }
61
62 return sym;
63 }
64
65 static inline size_t psi_dlname(char (*buf)[], size_t *len, const char *libname)
66 {
67 if (strchr(libname, '/')) {
68 return snprintf(*buf, *len, "%s", libname);
69 } else {
70 return snprintf(*buf, *len, "lib%s.%s", libname, PHP_PSI_SHLIB_SUFFIX);
71 }
72 }
73
74 static inline void *psi_dlopen(const char *libname)
75 {
76 return dlopen(libname, RTLD_LAZY | RTLD_LOCAL);
77 }
78
79 static inline void psi_dlclose(void *dl)
80 {
81 dlclose(dl);
82 }
83
84 static inline char *psi_dlerror()
85 {
86 return dlerror();
87 }
88
89 #endif /* PSI_DLSYM_H */