1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
|
// loader.h: dynamic backend library loader
// This file is part of scanbuttond.
// Copyleft )c( 2005-2006 by Bernhard Stiftner
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <syslog.h>
#include <limits.h>
#include <dlfcn.h>
#include <errno.h>
#include "scanbuttond/common.h"
#include "scanbuttond/loader.h"
static char* lib_dir = STRINGIFY(LIB_DIR);
int scanbtnd_loader_init(void)
{
/* int error = lt_dlinit();
if (!error) {
error = lt_dlsetsearchpath(module_search_path);
const char *env_path = getenv(MODULE_PATH_ENV);
if (env_path != NULL) {
error = lt_dladdsearchdir(env_path);
}
}
return error;*/
return 0;
}
void scanbtnd_loader_exit(void)
{
/* lt_dlexit();*/
}
backend_t* scanbtnd_load_backend(const char* filename)
{
const char* error;
void* dll_handle;
char* dll_path = (char*)malloc(strlen(lib_dir) + strlen(filename) + 5);
strcpy(dll_path, filename);
strcat(dll_path, ".so");
dll_handle = dlopen(dll_path, RTLD_NOW|RTLD_LOCAL);
if (!dll_handle) {
strcpy(dll_path, lib_dir);
strcat(dll_path, "/");
strcat(dll_path, filename);
strcat(dll_path, ".so");
dll_handle = dlopen(dll_path, RTLD_NOW|RTLD_LOCAL);
if (!dll_handle) {
syslog(LOG_ERR, "loader: failed to load \"%s\". Error message: \"%s\"",
filename, dlerror());
free(dll_path);
return NULL;
}
}
dlerror(); // Clear any existing error
backend_t* backend = (backend_t*)malloc(sizeof(backend_t));
backend->handle = (void*)dll_handle;
backend->scanbtnd_get_backend_name = dlsym(dll_handle, "scanbtnd_get_backend_name");
if ((error = dlerror()) != NULL) {
syslog(LOG_ERR, "loader: dlsym failed! Error message \"%s\"", error);
dlclose(dll_handle);
free(backend);
free(dll_path);
return NULL;
}
backend->scanbtnd_init = dlsym(dll_handle, "scanbtnd_init");
if ((error = dlerror()) != NULL) {
syslog(LOG_ERR, "loader: dlsym failed! Error message \"%s\"", error);
dlclose(dll_handle);
free(backend);
free(dll_path);
return NULL;
}
backend->scanbtnd_rescan = dlsym(dll_handle, "scanbtnd_rescan");
if ((error = dlerror()) != NULL) {
syslog(LOG_ERR, "loader: dlsym failed! Error message \"%s\"", error);
dlclose(dll_handle);
free(backend);
free(dll_path);
return NULL;
}
backend->scanbtnd_get_supported_devices = dlsym(dll_handle, "scanbtnd_get_supported_devices");
if ((error = dlerror()) != NULL) {
syslog(LOG_ERR, "loader: dlsym failed! Error message \"%s\"", error);
dlclose(dll_handle);
free(backend);
free(dll_path);
return NULL;
}
backend->scanbtnd_open = dlsym(dll_handle, "scanbtnd_open");
if ((error = dlerror()) != NULL) {
syslog(LOG_ERR, "loader: dlsym failed! Error message \"%s\"", error);
dlclose(dll_handle);
free(backend);
free(dll_path);
return NULL;
}
backend->scanbtnd_close = dlsym(dll_handle, "scanbtnd_close");
if ((error = dlerror()) != NULL) {
syslog(LOG_ERR, "loader: dlsym failed! Error message \"%s\"", error);
dlclose(dll_handle);
free(backend);
free(dll_path);
return NULL;
}
backend->scanbtnd_get_button = dlsym(dll_handle, "scanbtnd_get_button");
if ((error = dlerror()) != NULL) {
syslog(LOG_ERR, "loader: dlsym failed! Error message \"%s\"", error);
dlclose(dll_handle);
free(backend);
free(dll_path);
return NULL;
}
backend->scanbtnd_get_sane_device_descriptor = dlsym(dll_handle, "scanbtnd_get_sane_device_descriptor");
if ((error = dlerror()) != NULL) {
syslog(LOG_ERR, "loader: dlsym failed! Error message \"%s\"", error);
dlclose(dll_handle);
free(backend);
free(dll_path);
return NULL;
}
backend->scanbtnd_exit = dlsym(dll_handle, "scanbtnd_exit");
if ((error = dlerror()) != NULL) {
syslog(LOG_ERR, "loader: dlsym failed! Error message \"%s\"", error);
dlclose(dll_handle);
free(backend);
free(dll_path);
return NULL;
}
free(dll_path);
return backend;
}
void scanbtnd_unload_backend(backend_t* backend)
{
if (backend->handle != NULL) {
dlclose(backend->handle);
backend->handle = NULL;
free(backend);
}
}
|