lift single lib statement restriction
[m6w6/ext-psi] / src / types / decl_file.c
1 /*******************************************************************************
2 Copyright (c) 2016, 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 #include "php_psi_stdinc.h"
27 #include "data.h"
28
29 #include <dlfcn.h>
30
31 void psi_decl_file_dtor(struct psi_decl_file *file)
32 {
33 if (file->libnames) {
34 psi_plist_free(file->libnames);
35 }
36 if (file->dlopened) {
37 psi_plist_free(file->dlopened);
38 }
39 if (file->filename) {
40 free(file->filename);
41 }
42 memset(file, 0, sizeof(*file));
43 }
44
45 static inline bool validate_lib(struct psi_data *dst, const char *libname, void **dlopened)
46 {
47 char lib[MAXPATHLEN];
48 size_t len;
49
50 if (!libname) {
51 /* FIXME: assume stdlib */
52 return true;
53 } else if (!strchr(libname, '/')) {
54 len = snprintf(lib, MAXPATHLEN, "lib%s.%s", libname, PHP_PSI_SHLIB_SUFFIX);
55 if (MAXPATHLEN == len) {
56 dst->error(dst, NULL, PSI_WARNING, "Library name too long: '%s'",
57 libname);
58 }
59 lib[len] = 0;
60 libname = lib;
61 }
62 if (!(*dlopened = dlopen(libname, RTLD_LAZY | RTLD_LOCAL))) {
63 dst->error(dst, NULL, PSI_WARNING, "Could not open library '%s': %s.",
64 libname, dlerror());
65 return false;
66 }
67
68 return true;
69 }
70
71 bool psi_decl_file_validate(struct psi_data *dst, struct psi_data *src)
72 {
73 size_t i = 0;
74 char *libname;
75 void *dlopened;
76
77 while (psi_plist_get(src->file.libnames, i++, &libname)) {
78 if (!validate_lib(dst, libname, &dlopened)) {
79 return false;
80 }
81
82 libname = strdup(libname);
83 dst->file.libnames = psi_plist_add(dst->file.libnames, &libname);
84 dst->file.dlopened = psi_plist_add(dst->file.dlopened, &dlopened);
85 }
86
87 if (src->file.filename) {
88 dst->file.filename = strdup(src->file.filename);
89 }
90 return true;
91 }
92
93 void psi_libs_free(void **dlopened) {
94 if (*dlopened) {
95 dlclose(*dlopened);
96 }
97 }
98
99 void psi_names_free(char **name) {
100 if (*name) {
101 free(*name);
102 }
103 }