File: plugin-manager.c

package info (click to toggle)
fuse-overlayfs 1.14-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 632 kB
  • sloc: ansic: 7,231; sh: 211; python: 38; makefile: 34
file content (127 lines) | stat: -rw-r--r-- 3,348 bytes parent folder | download
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
/* fuse-overlayfs: Overlay Filesystem in Userspace

   Copyright (C) 2019 Red Hat Inc.

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#define _GNU_SOURCE
#define _FILE_OFFSET_BITS 64

#include <config.h>
#include <plugin.h>
#include <stdlib.h>
#include <fuse_overlayfs_error.h>
#include <errno.h>
#include <string.h>

struct ovl_plugin_context *
load_plugins (const char *plugins)
{
  char *saveptr = NULL, *it;
  cleanup_free char *buf = NULL;
  struct ovl_plugin_context *ctx;

  ctx = calloc (1, sizeof (*ctx));
  if (ctx == NULL)
    error (EXIT_FAILURE, errno, "cannot allocate context");

  buf = strdup (plugins);
  if (buf == NULL)
    error (EXIT_FAILURE, errno, "cannot allocate memory");

  for (it = strtok_r (buf, ":", &saveptr); it; it = strtok_r (NULL, ":", &saveptr))
    plugin_load_one (ctx, it);

  return ctx;
}

void
plugin_load_one (struct ovl_plugin_context *context, const char *path)
{
  plugin_name name;
  struct ovl_plugin *p;
  plugin_version version;
  void *handle = dlopen (path, RTLD_NOW | RTLD_LOCAL);
  if (! handle)
    error (EXIT_FAILURE, 0, "cannot load plugin %s: %s", path, dlerror ());

  p = calloc (1, sizeof (*p));
  if (p == NULL)
    error (EXIT_FAILURE, errno, "cannot load plugin %s", path);
  p->next = context->plugins;

  version = dlsym (handle, "plugin_version");
  if (version == NULL)
    error (EXIT_FAILURE, 0, "cannot find symbol `plugin_version` in plugin %s", path);

  if (version () != 1)
    error (EXIT_FAILURE, 0, "invalid plugin version for %s", path);

  p->handle = handle;
  name = dlsym (handle, "plugin_name");
  if (name == NULL)
    error (EXIT_FAILURE, 0, "cannot find symbol `plugin_name` in plugin %s", path);

  p->name = name ();

  if (plugin_find (context, p->name))
    error (EXIT_FAILURE, 0, "plugin %s added twice", p->name);

  p->load = dlsym (handle, "plugin_load");
  if (p->load == NULL)
    error (EXIT_FAILURE, 0, "cannot find symbol `plugin_load` in plugin %s", path);

  p->release = dlsym (handle, "plugin_release");
  if (p->release == NULL)
    error (EXIT_FAILURE, 0, "cannot find symbol `plugin_release` in plugin %s", path);

  context->plugins = p;
}

struct ovl_plugin *
plugin_find (struct ovl_plugin_context *context, const char *name)
{
  struct ovl_plugin *it;

  for (it = context->plugins; it; it = it->next)
    {
      if (strcmp (name, it->name) == 0)
        return it;
    }
  return NULL;
}

int
plugin_free_all (struct ovl_plugin_context *context)
{
  struct ovl_plugin *it, *next;

  it = context->plugins;
  while (it)
    {
      next = it->next;

      it->release ();

      /* Skip dlclose (it->handle) as it causes plugins written in Go to crash.  */

      free (it);

      it = next;
    }

  free (context);

  return 0;
}