1 /*  plugin.c -- low-level path parsing and plugin functions.
2 
3     Copyright (C) 2015 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, ':');
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, ":");
90         if (len == 0) kputs(builtin_path, &itr->path);
91         else kputsn(path, len, &itr->path);
92         kputc(':', &itr->path);
93 
94         path += len;
95         if (*path == ':') 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 void *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     void *sym = dlsym(lib, symbol);
141     if (sym == NULL) {
142         // Reopen the plugin with RTLD_GLOBAL and check for uniquified symbol
143         void *libg = dlopen(filename, RTLD_NOLOAD | RTLD_NOW | RTLD_GLOBAL);
144         if (libg == NULL) goto error;
145         dlclose(lib);
146         lib = libg;
147 
148         kstring_t symbolg = { 0, 0, NULL };
149         kputs(symbol, &symbolg);
150         kputc('_', &symbolg);
151         const char *slash = strrchr(filename, '/');
152         const char *basename = slash? slash+1 : filename;
153         kputsn(basename, strcspn(basename, ".-+"), &symbolg);
154 
155         sym = dlsym(lib, symbolg.s);
156         free(symbolg.s);
157         if (sym == NULL) goto error;
158     }
159 
160     *pluginp = lib;
161     return sym;
162 
163 error:
164     if (hts_verbose >= 4)
165         fprintf(stderr, "[W::%s] can't load plugin \"%s\": %s\n",
166                 __func__, filename, dlerror());
167     if (lib) dlclose(lib);
168     return NULL;
169 }
170 
plugin_sym(void * plugin,const char * name,const char ** errmsg)171 void *plugin_sym(void *plugin, const char *name, const char **errmsg)
172 {
173     void *sym = dlsym(plugin, name);
174     if (sym == NULL) *errmsg = dlerror();
175     return sym;
176 }
177 
close_plugin(void * plugin)178 void close_plugin(void *plugin)
179 {
180     if (dlclose(plugin) != 0) {
181         if (hts_verbose >= 4)
182             fprintf(stderr, "[W::%s] dlclose() failed: %s\n",
183                     __func__, dlerror());
184     }
185 }
186