moberg_driver.c 1.22 KB
Newer Older
1
2
3
4
5
6
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <moberg_driver.h>
#include <dlfcn.h>

7
struct moberg_driver *moberg_driver_open(struct moberg_config_parser_ident id)
8
9
10
11
12
13
14
{
  struct moberg_driver *result = NULL;

  char *driver_name = malloc(sizeof("libmoberg_.so") + id.length + 1);
  if (!driver_name) { goto out; }
  sprintf(driver_name, "libmoberg_%.*s.so", id.length, id.value);
  void *handle = dlopen(driver_name, RTLD_LAZY || RTLD_DEEPBIND);
15
16
17
18
  if (! handle) {
    fprintf(stderr, "Could not find driver %s\n", driver_name);
    goto free_driver_name;
  }
19
20
  struct moberg_driver_module *module =
    (struct moberg_driver_module *) dlsym(handle, "moberg_module");
21
22
23
24
  if (! module) {
    fprintf(stderr, "No moberg_module in driver %s\n", driver_name);
    goto dlclose_driver;
  }
25
  result = malloc(sizeof(*result));
26
27
28
29
  if (! result) {
    fprintf(stderr, "Could not allocate result for %s\n", driver_name);
    goto dlclose_driver;
  }
30
31
32
33
34
35
36
37
38
39
40
41
  result->handle = handle;
  result->module = *module;
  goto free_driver_name;

dlclose_driver:
    dlclose(handle);
free_driver_name:
    free(driver_name);
out:
    return result;
}

42
void moberg_driver_close(struct moberg_driver *driver)
43
44
45
46
47
{
  dlclose(driver->handle);
  free(driver);
}