1 /*
2  * virmodule.c: APIs for dlopen'ing extension modules
3  *
4  * Copyright (C) 2012-2018 Red Hat, Inc.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library;  If not, see
18  * <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #include <config.h>
23 
24 #include "internal.h"
25 #include "virmodule.h"
26 #include "virerror.h"
27 #include "virfile.h"
28 #include "virlog.h"
29 #include "virutil.h"
30 
31 #define VIR_FROM_THIS VIR_FROM_NONE
32 
33 VIR_LOG_INIT("util.module");
34 
35 #ifdef WITH_DLFCN_H
36 # include <dlfcn.h>
37 
38 static void *
virModuleLoadFile(const char * file)39 virModuleLoadFile(const char *file)
40 {
41     void *handle = NULL;
42     int flags = RTLD_NOW | RTLD_GLOBAL;
43 
44 # ifdef RTLD_NODELETE
45     flags |= RTLD_NODELETE;
46 # endif
47 
48     VIR_DEBUG("Load module file '%s'", file);
49 
50     virUpdateSelfLastChanged(file);
51 
52     if (!(handle = dlopen(file, flags))) {
53         virReportError(VIR_ERR_INTERNAL_ERROR,
54                        _("Failed to load module '%s': %s"), file, dlerror());
55         return NULL;
56     }
57 
58     return handle;
59 }
60 
61 
62 static void *
virModuleLoadFunc(void * handle,const char * file,const char * funcname)63 virModuleLoadFunc(void *handle,
64                   const char *file,
65                   const char *funcname)
66 {
67     void *regsym;
68 
69     VIR_DEBUG("Lookup function '%s'", funcname);
70 
71     if (!(regsym = dlsym(handle, funcname))) {
72         virReportError(VIR_ERR_INTERNAL_ERROR,
73                        _("Failed to find symbol '%s' in module '%s': %s"),
74                        funcname, file, dlerror());
75         return NULL;
76     }
77 
78     return regsym;
79 }
80 
81 
82 /**
83  * virModuleLoad:
84  * @path: filename of module to load
85  * @regfunc: name of the function that registers the module
86  * @required: true if module must exist on disk, false to silently skip
87  *
88  * Loads a loadable module named @path and calls the
89  * registration function @regfunc. The module will never
90  * be unloaded because unloading is not safe in a multi-threaded
91  * application.
92  *
93  * The module is automatically looked up in the appropriate place (git or
94  * installed directory).
95  *
96  * Returns 0 on success, 1 if the module was not found and -1 on any error.
97  */
98 int
virModuleLoad(const char * path,const char * regfunc,bool required)99 virModuleLoad(const char *path,
100               const char *regfunc,
101               bool required)
102 {
103     void *rethandle = NULL;
104     int (*regsym)(void);
105     int ret = -1;
106 
107     if (!virFileExists(path)) {
108         if (required) {
109             virReportSystemError(errno,
110                                  _("Failed to find module '%s'"), path);
111             return -1;
112         } else {
113             VIR_INFO("Module '%s' does not exist", path);
114             return 1;
115         }
116     }
117 
118     if (!(rethandle = virModuleLoadFile(path)))
119         goto cleanup;
120 
121     if (!(regsym = virModuleLoadFunc(rethandle, path, regfunc)))
122         goto cleanup;
123 
124     if ((*regsym)() < 0) {
125         /* regsym() should report an error itself, but lets
126          * just make sure */
127         if (virGetLastErrorCode() == VIR_ERR_OK) {
128             virReportError(VIR_ERR_INTERNAL_ERROR,
129                            _("Failed to execute symbol '%s' in module '%s'"),
130                            regfunc, path);
131         }
132         goto cleanup;
133     }
134 
135     rethandle = NULL;
136     ret = 0;
137 
138  cleanup:
139     if (rethandle)
140         dlclose(rethandle);
141     return ret;
142 }
143 
144 #else /* ! WITH_DLFCN_H */
145 int
virModuleLoad(const char * path,const char * regfunc G_GNUC_UNUSED,bool required)146 virModuleLoad(const char *path,
147               const char *regfunc G_GNUC_UNUSED,
148               bool required)
149 {
150     VIR_DEBUG("dlopen not available on this platform");
151     if (required) {
152         virReportSystemError(ENOSYS,
153                              _("Failed to find module '%s'"), path);
154         return -1;
155     } else {
156         /* Since we have no dlopen(), but definition we have no
157          * loadable modules on disk, so we can reasonably
158          * return '1' instead of an error.
159          */
160         return 1;
161     }
162 }
163 #endif /* ! WITH_DLFCN_H */
164