1 #if !defined(__FreeBSD__) && !defined(_GNU_SOURCE)
12 #include <sys/types.h>
21 #define VLOCK_MODULE_DIR PREFIX "/lib/vlock/modules"
23 typedef bool (*vlock_hook_fn
)(void **);
24 typedef vlock_hook_fn (*hook_dlsym_t
)(void *, const char *);
29 vlock_hook_fn hooks
[nr_hooks
];
32 static void close_module(struct plugin
*m
);
34 struct plugin
*open_module(const char *name
, char **error
)
37 struct plugin
*m
= __allocate_plugin(name
);
38 struct module_context
*context
= ensure_malloc(sizeof (struct module_context
));
40 if (asprintf(&path
, "%s/%s.so", VLOCK_MODULE_DIR
, name
) < 0) {
41 *error
= strdup("filename too long");
45 if (access(path
, R_OK
) < 0) {
46 (void) asprintf(error
, "%s: %s", path
, strerror(errno
));
50 context
->dl_handle
= dlopen(path
, RTLD_NOW
| RTLD_LOCAL
);
52 if (context
->dl_handle
== NULL
) {
53 *error
= strdup(dlerror());
59 for (size_t i
= 0; i
< nr_hooks
; i
++)
60 *(void **) (&context
->hooks
[i
]) = dlsym(context
->dl_handle
, hooks
[i
].name
);
63 for (size_t i
= 0; i
< nr_dependencies
; i
++) {
64 const char *(*dependency
)[] = dlsym(context
->dl_handle
, dependency_names
[i
]);
66 for (size_t j
= 0; dependency
!= NULL
&& (*dependency
)[j
] != NULL
; j
++)
67 list_append(m
->dependencies
[i
], strdup((*dependency
)[j
]));
71 m
->close
= close_module
;
84 static void close_module(struct plugin
*m
)
86 struct module_context
*context
= m
->context
;
87 dlclose(context
->dl_handle
);