aboutsummaryrefslogtreecommitdiff
path: root/harness/src/plugin.c
blob: 3c18d03c6b57a13b34bf70b707aee194837e1bbc (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
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
#include "plugin.h"
#include "foreign_intf.h"

#include <sys/stat.h>
#include <unistd.h>
#include <ctype.h>
#include <dlfcn.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.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_dl_(dlhandle_t dl, plugin_t *plug);

static void lock(plugin_t *plugin) { pthread_mutex_lock(&plugin->lock); };

static void unlock(plugin_t *plugin) { pthread_mutex_unlock(&plugin->lock); };

static int plugin_hot_reload_same_state_action_(plugin_t *plugin, void *ignore)
{
  return plugin_hot_reload_same_state(plugin);
}

void do_request_hot_reload(void *plugv)
{
  plugin_t *plugin = plugv;

  size_t n = plugin->n_requested_actions++;
  if (n < 8) {
    plugin->requested_actions[n].action = plugin_hot_reload_same_state_action_;
    plugin->requested_actions[n].arg_dtor = NULL;
  }
}

static int plugin_do_log(plugin_t *plugin, void *chrs)
{
  char *str = chrs;
  puts(str);
  return 0;
}

void do_request_log(void *plugv, const char *str)
{
  plugin_t *plugin = plugv;

  size_t n = plugin->n_requested_actions++;
  if (n < 8) {
    plugin->requested_actions[n].action = plugin_do_log;
    plugin->requested_actions[n].str_arg = strdup(str);
    plugin->requested_actions[n].arg_dtor = free;
  }
}

static int plugin_do_exit(void *plugv, int ec)
{
  exit(ec);
  return 0;
}

void do_request_exit(void *plugv, int ec)
{
  plugin_t *plugin = plugv;

  size_t n = plugin->n_requested_actions++;
  if (n < 8) {
    plugin->requested_actions[n].action =
        (int (*)(plugin_t *, void *))plugin_do_exit;
    plugin->requested_actions[n].int_arg = ec;
    plugin->requested_actions[n].arg_dtor = NULL;
  }
}

static int load_plugin_from_file_(int argc, char **argv, const char *filename,
                                  plugin_t *plugin)
{
  dlhandle_t lib = dlopen(filename, RTLD_LAZY);
  int ec = 0;

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

  printf("Loading file.\n");
  ec = load_plugin_from_dl_(lib, plugin);

  if (ec) {
    goto end;
  }

  strncpy(plugin->filename, filename, sizeof(plugin->filename));
  plugin->argc = argc;
  plugin->argv = argv;

  plugin->foreign_intf.ctx = plugin;
  plugin->foreign_intf.request_hot_reload = do_request_hot_reload;
  plugin->foreign_intf.do_log = do_request_log;
  plugin->foreign_intf.request_exit = do_request_exit;

  plugin->plugin_load(plugin->argc, plugin->argv, &plugin->foreign_intf);
end:
  return ec;
}

static void maybe_run_metaload(int argc, char **argv, plugin_t *plugin)
{
  static char *loaded_plugins[12];
  int i;
  for (i = 0; i < 12 && loaded_plugins[i]; ++i) {
    if (strcmp(loaded_plugins[i], plugin->plugin_name) == 0) {
      return; // Plugin is already loaded
    }
  }
  loaded_plugins[i] = strdup(plugin->plugin_name);

  printf("First time loading %s, running metaload.\n", plugin->plugin_name);
  if (plugin->plugin_metaload) {
    plugin->plugin_metaload(argc, argv);
  }
}

int load_plugin_from_file(int argc, char **argv, const char *filename,
                          plugin_t *plugin)
{
  memset(plugin, 0, sizeof(*plugin));

  pthread_mutexattr_t attr;
  if (pthread_mutexattr_init(&attr)) {
    perror("pthread_mutexattr_init");
    return 1;
  }

  if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE)) {
    perror("pthread_mutexattr_settype");
    return 1;
  }

  if (pthread_mutex_init(&plugin->lock, &attr)) {
    pthread_mutexattr_destroy(&attr);
    perror("pthread_mutexattr_init");
    return 1;
  }
  pthread_mutexattr_destroy(&attr);
  int rc = load_plugin_from_file_(argc, argv, filename, plugin);

  if (rc == 0) {
    maybe_run_metaload(argc, argv, plugin);
  }

  return rc;
}

int plugin_hot_reload_same_state(plugin_t *plugin)
{
  char filename_cpy[PATH_MAX];
  strncpy(filename_cpy, plugin->filename, sizeof(filename_cpy));
  return plugin_hot_reload(plugin->argc, plugin->argv, filename_cpy, 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);
  lock(plugin);

  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");
  if (dlclose(plugin->library_handle)) {
    printf("Could not close library handle: %s\n", dlerror());
  }

  if ((ec = load_plugin_from_file_(argc, argv, filepath, plugin))) {
    goto fail;
  }

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

fail:
  free(marshalled_state);
  unlock(plugin);
  return ec;
}

void plugin_run_requested_actions(plugin_t *plugin)
{
  lock(plugin);
  requested_action_t requested_actions[MAX_QUEUED_ACTIONS];
  size_t n_requested_actions = plugin->n_requested_actions;
  memcpy(&requested_actions, plugin->requested_actions,
         sizeof(requested_actions));
  plugin->n_requested_actions = 0;
  unlock(plugin);

  size_t i;
  for (i = 0; i < n_requested_actions; ++i) {
    requested_actions[i].action(plugin, requested_actions[i].str_arg);
    if (requested_actions[i].arg_dtor) {
      requested_actions[i].arg_dtor(requested_actions[i].ptr_arg);
    }
  }
}

void plugin_cold_start(plugin_t *plugin)
{
  lock(plugin);
  plugin->state = plugin->plugin_cold_start();
  unlock(plugin);
}