-
Notifications
You must be signed in to change notification settings - Fork 24
/
module.c
451 lines (404 loc) · 10.5 KB
/
module.c
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/* PANDAseq -- Assemble paired FASTQ Illumina reads and strip the region between amplification primers.
Copyright (C) 2011-2012 Andre Masella
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 3 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, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <libgen.h>
#include <ltdl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_PTHREAD
# include <pthread.h>
#endif
#include "pandaseq.h"
#include "assembler.h"
#include "buffer.h"
#define STR0(x) #x
#define STR(x) STR0(x)
#define LOGV(code, fmt, ...) snprintf(static_buffer(), BUFFER_SIZE, fmt, __VA_ARGS__); panda_log_proxy_write(assembler->logger, (code), assembler, NULL, static_buffer());
static const char path_sep_string[] = { LT_PATHSEP_CHAR, '\0' };
static volatile int ltdl_count = 0;
#ifdef HAVE_PTHREAD
/* All modules share a single mutex to control reference counts */
static pthread_mutex_t ref_lock = PTHREAD_MUTEX_INITIALIZER;
#endif
static bool ref_ltdl(
void) {
#ifdef HAVE_PTHREAD
pthread_mutex_lock(&ref_lock);
#endif
if (ltdl_count == 0) {
const char *path;
if (lt_dlinit() != 0) {
#ifdef HAVE_PTHREAD
pthread_mutex_unlock(&ref_lock);
#endif
return false;
}
path = lt_dlgetsearchpath();
if (path == NULL || strstr(path, STR(PKGLIBDIR)) == NULL) {
if (lt_dladdsearchdir(STR(PKGLIBDIR)) != 0) {
(void) lt_dlexit();
return false;
}
}
}
ltdl_count++;
#ifdef HAVE_PTHREAD
pthread_mutex_unlock(&ref_lock);
#endif
return true;
}
static void unref_ltdl(
void) {
#ifdef HAVE_PTHREAD
pthread_mutex_lock(&ref_lock);
#endif
if (--ltdl_count == 0) {
lt_dlexit();
}
#ifdef HAVE_PTHREAD
pthread_mutex_unlock(&ref_lock);
#endif
}
struct panda_module {
volatile size_t refcnt;
PandaCheck check;
PandaPreCheck precheck;
PandaDestroy destroy;
void *user_data;
lt_dlhandle handle;
char *name;
int api;
char **version;
};
void module_destroy(
PandaAssembler assembler) {
size_t it;
if (assembler->rejected)
free(assembler->rejected);
for (it = 0; it < assembler->modules_length; it++) {
panda_module_unref(assembler->modules[it]);
}
assembler->modules_length = 0;
free(assembler->modules);
}
void module_init(
PandaAssembler assembler) {
size_t it;
for (it = 0; it < assembler->modules_length; it++) {
PandaModule module = assembler->modules[it];
if (module->handle != NULL) {
const lt_dlinfo *info = lt_dlgetinfo(module->handle);
LOGV(PANDA_CODE_MOD_INFO, "%s(%s:%d)", info == NULL ? "unknown" : info->name, module->version == NULL ? "?" : *(module->version), module->api);
assembler->rejected[it] = 0;
}
}
}
bool module_checkseq(
PandaAssembler assembler,
panda_result_seq *sequence) {
size_t it;
for (it = 0; it < assembler->modules_length; it++) {
PandaModule module = assembler->modules[it];
if (module->check != NULL && !module->check(assembler->logger, sequence, module->user_data)) {
assembler->rejected[it]++;
return false;
}
}
return true;
}
bool module_precheckseq(
PandaAssembler assembler,
panda_seq_identifier *id,
const panda_qual *forward,
size_t forward_length,
const panda_qual *reverse,
size_t reverse_length) {
size_t it;
for (it = 0; it < assembler->modules_length; it++) {
PandaModule module = assembler->modules[it];
if (module->precheck != NULL && !module->precheck(assembler->logger, id, forward, forward_length, reverse, reverse_length, module->user_data)) {
assembler->rejected[it]++;
return false;
}
}
return true;
}
bool panda_assembler_add_module(
PandaAssembler assembler,
PandaModule module) {
if (module == NULL) {
return false;
}
#ifdef HAVE_PTHREAD
pthread_mutex_lock(&assembler->mutex);
#endif
if (assembler->modules_length == assembler->modules_size) {
if (assembler->modules_size == 0) {
assembler->modules_size = 8;
} else {
assembler->modules_size *= 2;
}
assembler->modules = realloc(assembler->modules, assembler->modules_size * sizeof(PandaModule));
assembler->rejected = realloc(assembler->rejected, assembler->modules_size * sizeof(size_t));
}
assembler->rejected[assembler->modules_length] = 0;
assembler->modules[assembler->modules_length++] = panda_module_ref(module);
#ifdef HAVE_PTHREAD
pthread_mutex_unlock(&assembler->mutex);
#endif
return true;
}
size_t panda_assembler_add_modules(
PandaAssembler assembler,
PandaModule *modules,
size_t modules_length) {
size_t it;
for (it = 0; it < modules_length; it++) {
if (!panda_assembler_add_module(assembler, modules[it])) {
return it;
}
}
return it;
}
bool panda_assembler_foreach_module(
PandaAssembler assembler,
PandaModuleCallback callback,
void *data) {
size_t it;
for (it = 0; it < assembler->modules_length; it++) {
if (!callback(assembler, assembler->modules[it], assembler->rejected[it], data)) {
return false;
}
}
return true;
}
void panda_assembler_module_stats(
PandaAssembler assembler) {
size_t it;
for (it = 0; it < assembler->modules_length; it++) {
if (assembler->rejected[it] > 0) {
LOGV(PANDA_CODE_REJECT_STAT, "%s\t%zu", assembler->modules[it]->name, assembler->rejected[it]);
}
}
}
typedef bool (
*PandaPluginOpen) (
PandaLogProxy logger,
const char *args,
PandaPreCheck *precheck,
PandaCheck *check,
void **user_data,
PandaDestroy *destroy);
PandaModule panda_module_load(
PandaLogProxy logger,
const char *path) {
PandaModule m;
lt_dlhandle handle;
union {
PandaPluginOpen func;
void *ptr;
} opener;
PandaCheck check = NULL;
PandaPreCheck precheck = NULL;
void *user_data;
PandaDestroy destroy;
size_t name_length;
int *api;
char *name;
name_length = strcspn(path, path_sep_string);
name = malloc(name_length + 1);
memcpy(name, path, name_length);
name[name_length] = '\0';
if (!ref_ltdl()) {
free(name);
return NULL;
}
handle = lt_dlopenext(name);
if (handle == NULL) {
fprintf(stderr, "Could not open module %s: %s\n", name, lt_dlerror());
free(name);
unref_ltdl();
return NULL;
}
api = lt_dlsym(handle, "api");
if (api == NULL || *api != PANDA_API) {
fprintf(stderr, "Invalid API in %s (%d != %d). Are you sure this module was compiled for this version of PANDAseq?\n", name, api == NULL ? -1 : *api, PANDA_API);
lt_dlclose(handle);
free(name);
unref_ltdl();
return NULL;
}
opener.ptr = lt_dlsym(handle, "opener");
if (opener.ptr == NULL) {
lt_dlclose(handle);
fprintf(stderr, "Could not find opener function in %s\n", name);
free(name);
unref_ltdl();
return NULL;
}
if ((*opener.func) (logger, (path[name_length] == LT_PATHSEP_CHAR) ? (path + name_length + 1) : NULL, &precheck, &check, &user_data, &destroy) && (precheck != NULL || check != NULL)) {
m = malloc(sizeof(struct panda_module));
m->api = *api;
m->check = check;
m->precheck = precheck;
m->handle = handle;
m->name = name;
m->refcnt = 1;
m->user_data = user_data;
m->destroy = destroy;
m->version = lt_dlsym(handle, "version");
return m;
} else {
free(name);
unref_ltdl();
return NULL;
}
}
PandaModule panda_module_new(
const char *name,
PandaCheck check,
PandaPreCheck precheck,
void *user_data,
PandaDestroy cleanup) {
PandaModule m;
if (check == NULL && precheck == NULL)
return NULL;
m = malloc(sizeof(struct panda_module));
m->api = PANDA_API;
m->check = check;
m->destroy = cleanup;
m->handle = NULL;
m->name = malloc(strlen(name) + 1);
memcpy(m->name, name, strlen(name) + 1);
m->precheck = precheck;
m->refcnt = 1;
m->user_data = user_data;
m->version = NULL;
return m;
}
PandaModule panda_module_ref(
PandaModule module) {
#ifdef HAVE_PTHREAD
pthread_mutex_lock(&ref_lock);
#endif
module->refcnt++;
#ifdef HAVE_PTHREAD
pthread_mutex_unlock(&ref_lock);
#endif
return module;
}
void panda_module_unref(
PandaModule module) {
size_t count;
if (module == NULL)
return;
#ifdef HAVE_PTHREAD
pthread_mutex_lock(&ref_lock);
#endif
count = --(module->refcnt);
#ifdef HAVE_PTHREAD
pthread_mutex_unlock(&ref_lock);
#endif
if (count == 0) {
if (module->destroy != NULL)
module->destroy(module->user_data);
if (module->name != NULL)
free(module->name);
if (module->handle != NULL) {
lt_dlclose(module->handle);
unref_ltdl();
}
free(module);
}
}
int panda_module_get_api(
PandaModule module) {
return module->api;
}
const char *panda_module_get_description(
PandaModule module) {
char **val;
if (module->handle == NULL)
return NULL;
val = lt_dlsym(module->handle, "desc");
return val == NULL ? NULL : *val;
}
const char *panda_module_get_name(
PandaModule module) {
return module->name;
}
const char *panda_module_get_version(
PandaModule module) {
return module->version == NULL ? NULL : *module->version;
}
const char *panda_module_get_usage(
PandaModule module) {
char **val;
if (module->handle == NULL)
return NULL;
val = lt_dlsym(module->handle, "usage");
return val == NULL ? NULL : *val;
}
static int show_module(
const char *filename,
void *data) {
int *api;
char buffer[2048];
char *base_filename;
const char **description;
lt_dlhandle handle;
const char **usage;
const char **version;
(void) data;
handle = lt_dlopenext(filename);
if (handle == NULL) {
return 0;
}
strncpy(buffer, filename, sizeof(buffer));
buffer[sizeof(buffer) - 1] = '\0';
base_filename = basename(buffer);
api = lt_dlsym(handle, "api");
if (api == NULL || *api != PANDA_API) {
fprintf(stderr, "Invalid API in %s (%d != %d).\n", base_filename, api == NULL ? -1 : *api, PANDA_API);
lt_dlclose(handle);
return 0;
}
fprintf(stderr, "%s", base_filename);
version = lt_dlsym(handle, "version");
if (version != NULL && *version != NULL) {
fprintf(stderr, "(%s)", *version);
}
description = lt_dlsym(handle, "description");
if (description != NULL && *description != NULL) {
fprintf(stderr, ": %s\n", *description);
} else {
fprintf(stderr, "\n");
}
usage = lt_dlsym(handle, "usage");
if (usage != NULL && *usage != NULL) {
fprintf(stderr, "\tUsage: %s\n", *usage);
}
lt_dlclose(handle);
return 0;
}
void module_show_all(
) {
if (!ref_ltdl()) {
return;
}
fprintf(stderr, "\nKnown modules:\n");
lt_dlforeachfile(STR(PKGLIBDIR), show_module, NULL);
unref_ltdl();
}