summaryrefslogtreecommitdiff
path: root/accel-pptpd/triton/loader.c
blob: b6c1914ebec6d638a6811e7a5b887cc5ef1807fc (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
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <dlfcn.h>
#include <limits.h>

#include "triton_p.h"

#include "memdebug.h"

int load_modules(const char *name)
{
	struct conf_sect_t *sect;
	struct conf_option_t *opt;
	char *fname;
	char *path = MODULE_PATH;

	sect = conf_get_section(name);
	if (!sect) {
		fprintf(stderr, "loader: section '%s' not found\n", name);
		return -1;
	}

	fname = _malloc(PATH_MAX);

	list_for_each_entry(opt, &sect->items, entry) {
		if (!strcmp(opt->name,"path") && opt->val) {
			path = opt->val;
			continue;
		}

		strcpy(fname, path);
		strcat(fname, "/");
		strcat(fname, opt->name);
		if (access(fname, F_OK)) {
			strcpy(fname, path);
			strcat(fname, "/lib");
			strcat(fname, opt->name);
			strcat(fname, ".so");
			if (access(fname, F_OK)) {
				strcpy(fname, opt->name);
				if (access(opt->name, F_OK)) {
					triton_log_error("loader: '%s' not found\n", opt->name);
					continue;
				}
			}
		}

		if (!dlopen(fname, RTLD_NOW | RTLD_GLOBAL)) {
			triton_log_error("loader: failed to load '%s': %s\n", opt->name, dlerror());
			_free(fname);
			return -1;
		}
	}

	_free(fname);

	return 0;
}