]> git.proxmox.com Git - mirror_frr.git/blame - lib/module.c
ospf: Fix segfault if compiled with DEBUG
[mirror_frr.git] / lib / module.c
CommitLineData
30771d65
DL
1/*
2 * Copyright (c) 2015-16 David Lamparter, for NetDEF, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 */
22
23#include "config.h"
24
25#include <stdlib.h>
26#include <stdio.h>
27#include <string.h>
28#include <unistd.h>
29#include <limits.h>
30#include <dlfcn.h>
31
32#include "module.h"
33#include "memory.h"
34#include "version.h"
35
36DEFINE_MTYPE_STATIC(LIB, MODULE_LOADNAME, "Module loading name")
37DEFINE_MTYPE_STATIC(LIB, MODULE_LOADARGS, "Module loading arguments")
38
39static struct frrmod_info frrmod_default_info = {
40 .name = "libfrr",
41 .version = FRR_VERSION,
42 .description = "libfrr core module",
43};
44union _frrmod_runtime_u frrmod_default = {
45 .r.info = &frrmod_default_info,
46 .r.finished_loading = 1,
47};
48
49// if defined(HAVE_SYS_WEAK_ALIAS_ATTRIBUTE)
50// union _frrmod_runtime_u _frrmod_this_module
51// __attribute__((weak, alias("frrmod_default")));
52// elif defined(HAVE_SYS_WEAK_ALIAS_PRAGMA)
53#pragma weak _frrmod_this_module = frrmod_default
54// else
55// error need weak symbol support
56// endif
57
58struct frrmod_runtime *frrmod_list = &frrmod_default.r;
59static struct frrmod_runtime **frrmod_last = &frrmod_default.r.next;
60static const char *execname = NULL;
61
62void frrmod_init(struct frrmod_runtime *modinfo)
63{
64 modinfo->finished_loading = 1;
65 *frrmod_last = modinfo;
66 frrmod_last = &modinfo->next;
67
68 execname = modinfo->info->name;
69}
70
d62a17ae 71struct frrmod_runtime *frrmod_load(const char *spec, const char *dir, char *err,
72 size_t err_len)
30771d65
DL
73{
74 void *handle = NULL;
75 char name[PATH_MAX], fullpath[PATH_MAX], *args;
76 struct frrmod_runtime *rtinfo, **rtinfop;
77 const struct frrmod_info *info;
78
79 snprintf(name, sizeof(name), "%s", spec);
80 args = strchr(name, ':');
81 if (args)
82 *args++ = '\0';
83
84 if (!strchr(name, '/')) {
85 if (!handle && execname) {
d62a17ae 86 snprintf(fullpath, sizeof(fullpath), "%s/%s_%s.so", dir,
87 execname, name);
30771d65
DL
88 handle = dlopen(fullpath, RTLD_NOW | RTLD_GLOBAL);
89 }
90 if (!handle) {
d62a17ae 91 snprintf(fullpath, sizeof(fullpath), "%s/%s.so", dir,
92 name);
30771d65
DL
93 handle = dlopen(fullpath, RTLD_NOW | RTLD_GLOBAL);
94 }
95 }
96 if (!handle) {
97 snprintf(fullpath, sizeof(fullpath), "%s", name);
98 handle = dlopen(fullpath, RTLD_NOW | RTLD_GLOBAL);
99 }
100 if (!handle) {
101 if (err)
102 snprintf(err, err_len,
d62a17ae 103 "loading module \"%s\" failed: %s", name,
104 dlerror());
30771d65
DL
105 return NULL;
106 }
107
108 rtinfop = dlsym(handle, "frr_module");
109 if (!rtinfop) {
110 dlclose(handle);
111 if (err)
112 snprintf(err, err_len,
d62a17ae 113 "\"%s\" is not an FRR module: %s", name,
114 dlerror());
30771d65
DL
115 return NULL;
116 }
117 rtinfo = *rtinfop;
118 rtinfo->load_name = XSTRDUP(MTYPE_MODULE_LOADNAME, name);
119 rtinfo->dl_handle = handle;
120 if (args)
121 rtinfo->load_args = XSTRDUP(MTYPE_MODULE_LOADARGS, args);
122 info = rtinfo->info;
123
124 if (rtinfo->finished_loading) {
125 dlclose(handle);
126 if (err)
d62a17ae 127 snprintf(err, err_len, "module \"%s\" already loaded",
128 name);
30771d65
DL
129 goto out_fail;
130 }
131
132 if (info->init && info->init()) {
133 dlclose(handle);
134 if (err)
135 snprintf(err, err_len,
d62a17ae 136 "module \"%s\" initialisation failed", name);
30771d65
DL
137 goto out_fail;
138 }
139
140 rtinfo->finished_loading = 1;
141
142 *frrmod_last = rtinfo;
143 frrmod_last = &rtinfo->next;
144 return rtinfo;
145
146out_fail:
147 if (rtinfo->load_args)
148 XFREE(MTYPE_MODULE_LOADARGS, rtinfo->load_args);
149 XFREE(MTYPE_MODULE_LOADNAME, rtinfo->load_name);
150 return NULL;
151}
152
153#if 0
154void frrmod_unload(struct frrmod_runtime *module)
155{
156}
157#endif