OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*******************************************************************************/
-#include "php_psi_stdinc.h"
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#else
+# include "php_config.h"
+#endif
#include "data.h"
-
-#include <dlfcn.h>
+#include "dl.h"
void psi_decl_file_dtor(struct psi_decl_file *file)
{
- if (file->ln) {
- free(file->ln);
+ if (file->libnames) {
+ psi_plist_free(file->libnames);
+ }
+ if (file->dlopened) {
+ psi_plist_free(file->dlopened);
}
- if (file->fn) {
- free(file->fn);
+ if (file->filename) {
+ zend_string_release(file->filename);
}
memset(file, 0, sizeof(*file));
}
-bool psi_decl_file_validate(struct psi_data *dst, struct psi_data *src, void **dlopened)
+static inline bool validate_lib(struct psi_data *dst, const zend_string *libname, void **dlopened)
{
- char lib[MAXPATHLEN];
- const char *ptr = src->file.ln;
- size_t len;
+ char lib[PATH_MAX];
+ size_t len = PATH_MAX;
- if (!ptr) {
+ if (!libname) {
/* FIXME: assume stdlib */
return true;
- } else if (!strchr(ptr, '/')) {
- len = snprintf(lib, MAXPATHLEN, "lib%s.%s", ptr, PHP_PSI_SHLIB_SUFFIX);
- if (MAXPATHLEN == len) {
- dst->error(dst, NULL, PSI_WARNING, "Library name too long: '%s'",
- ptr);
- }
- lib[len] = 0;
- ptr = lib;
}
- if (!(*dlopened = dlopen(ptr, RTLD_LAZY | RTLD_LOCAL))) {
- dst->error(dst, NULL, PSI_WARNING, "Could not open library '%s': %s.",
- src->file.ln, dlerror());
+
+ if (PATH_MAX == psi_dlname(&lib, &len, libname->val)) {
+ dst->error(dst, NULL, PSI_WARNING, "Library name too long: '%s'",
+ libname->val);
return false;
}
- if (!dst->libs) {
- dst->libs = psi_plist_init((psi_plist_dtor) psi_libs_free);
+ if (!(*dlopened = psi_dlopen(lib))) {
+ dst->error(dst, NULL, PSI_WARNING, "Could not open library '%s': %s",
+ libname->val, psi_dlerror());
+ return false;
+ }
+
+ return true;
+}
+
+bool psi_decl_file_validate(struct psi_data *dst, struct psi_data *src)
+{
+ size_t i = 0;
+ zend_string *libname;
+ void *dlopened;
+
+ while (psi_plist_get(src->file.libnames, i++, &libname)) {
+ if (!validate_lib(dst, libname, &dlopened)) {
+ return false;
+ }
+
+ dst->file.libnames = psi_plist_add(dst->file.libnames, &libname);
+ dst->file.dlopened = psi_plist_add(dst->file.dlopened, &dlopened);
}
- dst->libs = psi_plist_add(dst->libs, dlopened);
+ if (src->file.filename) {
+ dst->file.filename = zend_string_copy(src->file.filename);
+ }
return true;
}
void psi_libs_free(void **dlopened) {
if (*dlopened) {
- dlclose(*dlopened);
+ psi_dlclose(*dlopened);
+ *dlopened = NULL;
+ }
+}
+
+void psi_names_free(zend_string **name) {
+ if (*name) {
+ zend_string_release(*name);
+ *name = NULL;
}
}