1 /*  plugin.c -- low-level path parsing and plugin functions.
2 
3     Copyright (C) 2015-2016, 2020 Genome Research Ltd.
4 
5     Author: John Marshall <jm18@sanger.ac.uk>
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13 
14 The above copyright notice and this permission notice shall be included in
15 all copies or substantial portions of the Software.
16 
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 DEALINGS IN THE SOFTWARE.  */
24 
25 #include <config.h>
26 
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <errno.h>
31 
32 #include <dirent.h>
33 #include <dlfcn.h>
34 
35 #include "hts_internal.h"
36 #include "htslib/kstring.h"
37 
38 #ifndef PLUGINPATH
39 #define PLUGINPATH ""
40 #endif
41 
open_nextdir(struct hts_path_itr * itr)42 static DIR *open_nextdir(struct hts_path_itr *itr)
43 {
44     DIR *dir;
45 
46     while (1) {
47         const char *colon = strchr(itr->pathdir, HTS_PATH_SEPARATOR_CHAR);
48         if (colon == NULL) return NULL;
49 
50         itr->entry.l = 0;
51         kputsn(itr->pathdir, colon - itr->pathdir, &itr->entry);
52         itr->pathdir = &colon[1];
53         if (itr->entry.l == 0) continue;
54 
55         dir = opendir(itr->entry.s);
56         if (dir) break;
57 
58         if (hts_verbose >= 4)
59             fprintf(stderr,
60                     "[W::hts_path_itr] can't scan directory \"%s\": %s\n",
61                     itr->entry.s, strerror(errno));
62     }
63 
64     if (itr->entry.s[itr->entry.l-1] != '/') kputc('/', &itr->entry);
65     itr->entry_dir_l = itr->entry.l;
66     return dir;
67 }
68 
hts_path_itr_setup(struct hts_path_itr * itr,const char * path,const char * builtin_path,const char * prefix,size_t prefix_len,const char * suffix,size_t suffix_len)69 void hts_path_itr_setup(struct hts_path_itr *itr, const char *path,
70         const char *builtin_path, const char *prefix, size_t prefix_len,
71         const char *suffix, size_t suffix_len)
72 {
73     itr->prefix = prefix;
74     itr->prefix_len = prefix_len;
75 
76     if (suffix) itr->suffix = suffix, itr->suffix_len = suffix_len;
77     else itr->suffix = PLUGIN_EXT, itr->suffix_len = strlen(PLUGIN_EXT);
78 
79     itr->path.l = itr->path.m = 0; itr->path.s = NULL;
80     itr->entry.l = itr->entry.m = 0; itr->entry.s = NULL;
81 
82     if (! builtin_path) builtin_path = PLUGINPATH;
83     if (! path) {
84         path = getenv("HTS_PATH");
85         if (! path) path = "";
86     }
87 
88     while (1) {
89         size_t len = strcspn(path, HTS_PATH_SEPARATOR_STR);
90         if (len == 0) kputs(builtin_path, &itr->path);
91         else kputsn(path, len, &itr->path);
92         kputc(HTS_PATH_SEPARATOR_CHAR, &itr->path);
93 
94         path += len;
95         if (*path == HTS_PATH_SEPARATOR_CHAR) path++;
96         else break;
97     }
98 
99     // Note that ':' now terminates entries rather than separates them
100     itr->pathdir = itr->path.s;
101     itr->dirv = open_nextdir(itr);
102 }
103 
hts_path_itr_next(struct hts_path_itr * itr)104 const char *hts_path_itr_next(struct hts_path_itr *itr)
105 {
106     while (itr->dirv) {
107         struct dirent *e;
108         while ((e = readdir((DIR *) itr->dirv)) != NULL) {
109             size_t d_name_len = strlen(e->d_name);
110             if (strncmp(e->d_name, itr->prefix, itr->prefix_len) == 0 &&
111                 d_name_len >= itr->suffix_len &&
112                 strncmp(e->d_name + d_name_len - itr->suffix_len, itr->suffix,
113                         itr->suffix_len) == 0) {
114                 itr->entry.l = itr->entry_dir_l;
115                 kputs(e->d_name, &itr->entry);
116                 return itr->entry.s;
117             }
118         }
119 
120         closedir((DIR *) itr->dirv);
121         itr->dirv = open_nextdir(itr);
122     }
123 
124     itr->pathdir = NULL;
125     free(itr->path.s); itr->path.s = NULL;
126     free(itr->entry.s); itr->entry.s = NULL;
127     return NULL;
128 }
129 
130 
131 #ifndef RTLD_NOLOAD
132 #define RTLD_NOLOAD 0
133 #endif
134 
load_plugin(void ** pluginp,const char * filename,const char * symbol)135 plugin_void_func *load_plugin(void **pluginp, const char *filename, const char *symbol)
136 {
137     void *lib = dlopen(filename, RTLD_NOW | RTLD_LOCAL);
138     if (lib == NULL) goto error;
139 
140     plugin_void_func *sym;
141     *(void **) &sym = dlsym(lib, symbol);
142     if (sym == NULL) {
143         // Reopen the plugin with RTLD_GLOBAL and check for uniquified symbol
144         void *libg = dlopen(filename, RTLD_NOLOAD | RTLD_NOW | RTLD_GLOBAL);
145         if (libg == NULL) goto error;
146         dlclose(lib);
147         lib = libg;
148 
149         kstring_t symbolg = { 0, 0, NULL };
150         kputs(symbol, &symbolg);
151         kputc('_', &symbolg);
152         const char *slash = strrchr(filename, '/');
153         const char *basename = slash? slash+1 : filename;
154         kputsn(basename, strcspn(basename, ".-+"), &symbolg);
155 
156         *(void **) &sym = dlsym(lib, symbolg.s);
157         free(symbolg.s);
158         if (sym == NULL) goto error;
159     }
160 
161     *pluginp = lib;
162     return sym;
163 
164 error:
165     if (hts_verbose >= 4)
166         fprintf(stderr, "[W::%s] can't load plugin \"%s\": %s\n",
167                 __func__, filename, dlerror());
168     if (lib) dlclose(lib);
169     return NULL;
170 }
171 
plugin_sym(void * plugin,const char * name,const char ** errmsg)172 void *plugin_sym(void *plugin, const char *name, const char **errmsg)
173 {
174     void *sym = dlsym(plugin, name);
175     if (sym == NULL) *errmsg = dlerror();
176     return sym;
177 }
178 
plugin_func(void * plugin,const char * name,const char ** errmsg)179 plugin_void_func *plugin_func(void *plugin, const char *name, const char **errmsg)
180 {
181     plugin_void_func *sym;
182     *(void **) &sym = plugin_sym(plugin, name, errmsg);
183     return sym;
184 }
185 
close_plugin(void * plugin)186 void close_plugin(void *plugin)
187 {
188     if (dlclose(plugin) != 0) {
189         if (hts_verbose >= 4)
190             fprintf(stderr, "[W::%s] dlclose() failed: %s\n",
191                     __func__, dlerror());
192     }
193 }
194