summaryrefslogtreecommitdiff
path: root/src/module.c
blob: 19819cabba2f860a0e9805ac3ddf38989f35df17 (plain)
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
/*
 * Copyright (C) 1997-2004, Michael Jennings
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies of the Software, its documentation and marketing & publicity
 * materials, and acknowledgment shall be given in the documentation, materials
 * and software packages that this Software was used.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

static const char __attribute__((unused)) cvs_ident[] = "$Id$";

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <libast_internal.h>
#include <dlfcn.h>

/* *INDENT-OFF* */
static SPIF_CONST_TYPE(moduleclass) s_class = {
    {
        SPIF_DECL_CLASSNAME(module),
        (spif_func_t) spif_module_new,
        (spif_func_t) spif_module_init,
        (spif_func_t) spif_module_done,
        (spif_func_t) spif_module_del,
        (spif_func_t) spif_module_show,
        (spif_func_t) spif_module_comp,
        (spif_func_t) spif_module_dup,
        (spif_func_t) spif_module_type
    },
    (spif_func_t) spif_module_call,
    (spif_func_t) spif_module_getsym,
    (spif_func_t) spif_module_load,
    (spif_func_t) spif_module_run,
    (spif_func_t) spif_module_unload,
};
SPIF_TYPE(class) SPIF_CLASS_VAR(module) = SPIF_CAST(class) &s_class;
SPIF_TYPE(moduleclass) SPIF_MODULECLASS_VAR(module) = &s_class;
/* *INDENT-ON* */

static const size_t buff_inc = 4096;

spif_module_t
spif_module_new(void)
{
    spif_module_t self;

    self = SPIF_ALLOC(module);
    if (!spif_module_init(self)) {
        SPIF_DEALLOC(self);
        self = SPIF_NULL_TYPE(module);
    }
    return self;
}

spif_bool_t
spif_module_init(spif_module_t self)
{
    ASSERT_RVAL(!SPIF_MODULE_ISNULL(self), FALSE);
    /* ***NOT NEEDED*** spif_obj_init(SPIF_OBJ(self)); */
    spif_obj_set_class(SPIF_OBJ(self), SPIF_CLASS(SPIF_MODULECLASS_VAR(module)));
    self->name = SPIF_NULL_TYPE(str);
    self->path = SPIF_NULL_TYPE(str);
    self->module_handle = SPIF_NULL_TYPE(ptr);
    self->main_handle = dlopen(NULL, RTLD_LAZY);
    return TRUE;
}

spif_bool_t
spif_module_done(spif_module_t self)
{
    spif_bool_t ret = TRUE;

    ASSERT_RVAL(!SPIF_MODULE_ISNULL(self), FALSE);
    if (self->module_handle) {
        ret = spif_module_unload(self);
        self->module_handle = SPIF_NULL_TYPE(ptr);
    }
    if (!SPIF_STR_ISNULL(self->name)) {
        spif_str_del(self->name);
        self->name = SPIF_NULL_TYPE(str);
    }
    if (!SPIF_STR_ISNULL(self->path)) {
        spif_str_del(self->path);
        self->path = SPIF_NULL_TYPE(str);
    }
    return ret;
}

spif_bool_t
spif_module_del(spif_module_t self)
{
    ASSERT_RVAL(!SPIF_MODULE_ISNULL(self), FALSE);
    spif_module_done(self);
    dlclose(self->main_handle);
    SPIF_DEALLOC(self);
    return TRUE;
}

spif_str_t
spif_module_show(spif_module_t self, spif_charptr_t name, spif_str_t buff, size_t indent)
{
    spif_char_t tmp[4096];

    if (SPIF_MODULE_ISNULL(self)) {
        SPIF_OBJ_SHOW_NULL(module, name, buff, indent, tmp);
        return buff;
    }

    memset(tmp, ' ', indent);
    snprintf(SPIF_CHARPTR_C(tmp) + indent, sizeof(tmp) - indent,
             "(spif_module_t) %s:  %10p { \"",
             name, SPIF_CAST(ptr) self);
    if (SPIF_STR_ISNULL(buff)) {
        buff = spif_str_new_from_ptr(tmp);
    } else {
        spif_str_append_from_ptr(buff, tmp);
    }

    indent += 2;
    if (indent < sizeof(tmp)) {
        memset(tmp, ' ', indent);
    }
    buff = spif_str_show(self->name, SPIF_CHARPTR("name"), buff, indent);
    buff = spif_str_show(self->path, SPIF_CHARPTR("path"), buff, indent);
    snprintf(SPIF_CHARPTR_C(tmp) + indent, sizeof(tmp) - indent, "(spif_ptr_t) module_handle:  0x%p\n", self->module_handle);
    spif_str_append_from_ptr(buff, tmp);

    snprintf(SPIF_CHARPTR_C(tmp) + indent, sizeof(tmp) - indent, "(spif_ptr_t) main_handle:  0x%p\n", self->main_handle);
    spif_str_append_from_ptr(buff, tmp);

    snprintf(SPIF_CHARPTR_C(tmp), sizeof(tmp), "}\n");
    spif_str_append_from_ptr(buff, tmp);
    return buff;
}

spif_cmp_t
spif_module_comp(spif_module_t self, spif_module_t other)
{
    spif_cmp_t ret;

    SPIF_OBJ_COMP_CHECK_NULL(self, other);
    SPIF_OBJ_COMP_CHECK_NULL(self->name, other->name);
    if (!SPIF_CMP_IS_EQUAL(ret = spif_str_comp(self->name, other->name))) {
        return ret;
    }
    SPIF_OBJ_COMP_CHECK_NULL(self->path, other->path);
    if (!SPIF_CMP_IS_EQUAL(ret = spif_str_comp(self->path, other->path))) {
        return ret;
    }
    return SPIF_CMP_FROM_INT((int) self->module_handle - (int) other->module_handle);
}

spif_module_t
spif_module_dup(spif_module_t self)
{
    spif_module_t tmp;

    ASSERT_RVAL(!SPIF_MODULE_ISNULL(self), SPIF_NULL_TYPE(module));
    tmp = SPIF_ALLOC(module);
    memcpy(tmp, self, SPIF_SIZEOF_TYPE(module));
    tmp->name = spif_str_dup(self->name);
    tmp->path = spif_str_dup(self->path);
    return tmp;
}

spif_classname_t
spif_module_type(spif_module_t self)
{
    ASSERT_RVAL(!SPIF_MODULE_ISNULL(self), SPIF_CAST(classname) SPIF_NULLSTR_TYPE(classname));
    return SPIF_OBJ_CLASSNAME(self);
}

spif_ptr_t
spif_module_call(spif_module_t self, spif_charptr_t fname, spif_ptr_t data)
{
    spif_func_t fp;
    spif_charptr_t err;

    fp = SPIF_CAST(func) spif_module_getsym(self, fname);
    if (SPIF_PTR_ISNULL(err)) {
        /* No error.  Proceed. */
        return (fp)(data);
    } else {
        libast_print_warning("Unable to call function %s() from module \"%s\" -- %s\n", fname, SPIF_STR_STR(self->name), err);
    }
    return SPIF_NULL_TYPE(ptr);
}

spif_ptr_t
spif_module_getsym(spif_module_t self, spif_charptr_t sym)
{
    spif_ptr_t psym;
    spif_charptr_t err;

    ASSERT_RVAL(!SPIF_MODULE_ISNULL(self), SPIF_NULL_TYPE(ptr));
    ASSERT_RVAL(!SPIF_PTR_ISNULL(sym), SPIF_NULL_TYPE(ptr));
    REQUIRE_RVAL(!SPIF_PTR_ISNULL(self->main_handle), SPIF_NULL_TYPE(ptr));
    dlerror();
    psym = SPIF_CAST(ptr) dlsym(self->module_handle, (const char *) sym);
    err = SPIF_CAST(charptr) dlerror();
    if (SPIF_PTR_ISNULL(err)) {
        /* No error.  Proceed. */
        return psym;
    } else {
        psym = SPIF_CAST(ptr) dlsym(self->main_handle, (const char *) sym);
        err = SPIF_CAST(charptr) dlerror();
        if (SPIF_PTR_ISNULL(err)) {
            /* No error.  Proceed. */
            return psym;
        } else {
#ifdef RTLD_DEFAULT
            psym = SPIF_CAST(ptr) dlsym(RTLD_DEFAULT, (const char *) sym);
            err = SPIF_CAST(charptr) dlerror();
            if (SPIF_PTR_ISNULL(err)) {
                /* No error.  Proceed. */
                return psym;
            } else
#endif
                libast_print_warning("Unable to resolve symbol \"%s\" from module \"%s\" -- %s\n", sym,
                                     SPIF_STR_STR(self->name), err);
        }
    }
    return SPIF_NULL_TYPE(ptr);
}

spif_bool_t
spif_module_load(spif_module_t self)
{
    spif_func_t fp;

    ASSERT_RVAL(!SPIF_MODULE_ISNULL(self), FALSE);
    REQUIRE_RVAL(!SPIF_STR_ISNULL(self->path), FALSE);

    if (SPIF_STR_ISNULL(self->name)) {
        spif_stridx_t idx;

        idx = spif_str_rindex(self->path, '/');
        if (idx == spif_str_get_len(self->path)) {
            self->name = spif_str_dup(self->path);
        } else {
            self->name = spif_str_substr(self->path, idx, 0);
        }
    }

    self->module_handle = dlopen(SPIF_STR_STR(self->path), RTLD_LAZY | RTLD_GLOBAL);
    if (SPIF_PTR_ISNULL(self->module_handle)) {
        libast_print_error("Unable to dlopen() \"%s\" -- %s\n", SPIF_STR_STR(self->path), dlerror());
        return FALSE;
    }
    fp = spif_module_getsym(self, "init");
    if (fp) {
        return (((fp)(self)) ? (TRUE) : (FALSE));
    } else {
        return TRUE;
    }
}

spif_bool_t
spif_module_run(spif_module_t self)
{
    return ((spif_module_call(self, "run", NULL)) ? (TRUE) : (FALSE));
}

spif_bool_t
spif_module_unload(spif_module_t self)
{
    spif_func_t fp;

    ASSERT_RVAL(!SPIF_MODULE_ISNULL(self), FALSE);
    REQUIRE_RVAL(!SPIF_PTR_ISNULL(self->module_handle), FALSE);

    fp = spif_module_getsym(self, "done");
    if (fp) {
        if (!(fp)()) {
            return FALSE;
        }
    }

    dlerror();
    if (dlclose(self->module_handle)) {
        libast_print_warning("Unable to dlclose() \"%s\" -- %s\n", SPIF_STR_STR(self->path), dlerror());
        return FALSE;
    }
    self->module_handle = SPIF_NULL_TYPE(ptr);
    return TRUE;
}

SPIF_DEFINE_PROPERTY_FUNC(module, str, name);
SPIF_DEFINE_PROPERTY_FUNC(module, str, path);
SPIF_DEFINE_PROPERTY_FUNC(module, ptr, module_handle);
SPIF_DEFINE_PROPERTY_FUNC(module, ptr, main_handle);