aboutsummaryrefslogtreecommitdiff
path: root/harness/src/plugin.c
blob: bb32b028340ac67517a97337666e91e9f68e8f45 (plain) (blame)
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
#include "plugin.h"

#include <ctype.h>
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>

static void shx(uint8_t *state, uint32_t sz)
{
  uint32_t i = 0;
  while (i < sz) {
    for (int j = 0; j < 16; ++j) {
      if (i < sz) {
        printf("%02x ", (unsigned int)state[i]);
      }
      else {
        printf("   ");
      }
      ++i;
    }

    i -= 16;

    printf("   ");

    for (int j = 0; j < 16; ++j) {
      if (i < sz) {
        if (isprint(state[i]) && !isspace(state[i])) {
          printf("%c", state[i]);
        }
        else {
          printf(".");
        }
      }
      else {
        printf(" ");
      }
      ++i;
    }
    printf("\n");
  }
}

int load_plugin_from_file(const char *filename, plugin_t *plugin)
{
  dlhandle_t lib = dlopen(filename, RTLD_LAZY);

  if (!lib) {
    fprintf(stderr, "Failed to open library: %s: %s\n", filename, dlerror());
    return 1;
  }

  printf("Loading file.\n");
  return load_plugin_from_dl(lib, plugin);
}

int plugin_hot_reload(int argc, char **argv, const char *filepath,
                      plugin_t *plugin)
{
  int ec = 0;
  uint32_t sz = 0;
  uint8_t *marshalled_state = NULL;

  printf("Hot Reloading %s\n", plugin->plugin_name);
  pthread_mutex_lock(&plugin->lock);

  printf("Marshalling state ...\n");
  marshalled_state = plugin->plugin_marshal_state(plugin->state, &sz);

  printf("Calling teardown ...\n");
  plugin->plugin_teardown(plugin->state);

  printf("State Marshalled:\n");
  shx(marshalled_state, sz);

  printf("Unloading old library handle.\n");
  dlclose(plugin->library_handle);

  if ((ec = load_plugin_from_file(filepath, plugin))) {
    goto fail;
  }

  printf("Loading plugin ...\n");
  plugin->plugin_load(argc, argv);
  printf("Hot starting plugin ...\n");
  plugin->state = plugin->plugin_hot_start(marshalled_state, sz);

fail:
  free(marshalled_state);
  pthread_mutex_unlock(&plugin->lock);
  return ec;
}