summaryrefslogtreecommitdiff
path: root/lib/rpmplugins.c
blob: f06fd7895cfef3687d62add6bccc913815e91d09 (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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422

#include "system.h"

#include <rpm/rpmmacro.h>
#include <rpm/rpmtypes.h>
#include <rpm/rpmlog.h>
#include <rpm/rpmstring.h>
#include <rpm/rpmts.h>

#include "lib/rpmplugins.h"
#include <dlfcn.h>


#define STR1(x) #x
#define STR(x) STR1(x)

static rpmRC rpmpluginsCallInit(rpmPlugin plugin, rpmts ts);

struct rpmPlugin_s {
    char *name;
    char *opts;
    void *handle;
    void *priv;
    rpmPluginHooks hooks;
};

struct rpmPlugins_s {
    rpmPlugin *plugins;
    int count;
    rpmts ts;
};

static rpmPlugin rpmpluginsGetPlugin(rpmPlugins plugins, const char *name)
{
    int i;
    for (i = 0; i < plugins->count; i++) {
	rpmPlugin plugin = plugins->plugins[i];
	if (rstreq(plugin->name, name)) {
	    return plugin;
	}
    }
    return NULL;
}

int rpmpluginsPluginAdded(rpmPlugins plugins, const char *name)
{
    return (rpmpluginsGetPlugin(plugins, name) != NULL);
}

rpmPlugins rpmpluginsNew(rpmts ts)
{
    rpmPlugins plugins = xcalloc(1, sizeof(*plugins));
    plugins->ts = ts;
    return plugins;
}

static rpmPlugin rpmPluginNew(const char *name, const char *path,
			      const char *opts)
{
    rpmPlugin plugin = NULL;
    rpmPluginHooks hooks = NULL;
    char *error;
    char *hooks_name = NULL;

    void *handle = dlopen(path, RTLD_LAZY);
    if (!handle) {
	rpmlog(RPMLOG_ERR, _("Failed to dlopen %s %s\n"), path, dlerror());
	return NULL;
    }

    /* make sure the plugin has the supported hooks flag */
    hooks_name = rstrscat(NULL, name, "_hooks", NULL);
    /* clear out any old errors that weren't fetched */
    dlerror();
    hooks = dlsym(handle, hooks_name);
    if ((error = dlerror()) != NULL) {
	rpmlog(RPMLOG_ERR, _("Failed to resolve symbol %s: %s\n"),
	       hooks_name, error);
	dlclose(handle);
    } else {
	plugin = xcalloc(1, sizeof(*plugin));
	plugin->name = xstrdup(name);
	plugin->handle = handle;
	plugin->hooks = hooks;
	if (opts)
	    plugin->opts = xstrdup(opts);
    }
    free(hooks_name);

    return plugin;
}

static rpmPlugin rpmPluginFree(rpmPlugin plugin)
{
    if (plugin) {
	rpmPluginHooks hooks = plugin->hooks;
	if (hooks->cleanup)
	    hooks->cleanup(plugin);
	dlclose(plugin->handle);
	free(plugin->name);
	free(plugin->opts);
	free(plugin);
    }
    return NULL;
}

const char *rpmPluginName(rpmPlugin plugin)
{
    return (plugin != NULL) ? plugin->name : NULL;
}

const char *rpmPluginOpts(rpmPlugin plugin)
{
    return (plugin != NULL) ? plugin->opts : NULL;
}

void * rpmPluginGetData(rpmPlugin plugin)
{
    return (plugin != NULL) ? plugin->priv : NULL;
}

void rpmPluginSetData(rpmPlugin plugin, void *data)
{
    if (plugin)
	plugin->priv = data;
}

rpmRC rpmpluginsAdd(rpmPlugins plugins, const char *name, const char *path,
		    const char *opts)
{
    rpmRC rc;
    rpmPlugin plugin = rpmPluginNew(name, path, opts);

    if (plugin == NULL)
	return RPMRC_FAIL;
    
    rc = rpmpluginsCallInit(plugin, plugins->ts);

    if (rc == RPMRC_OK) {
	plugins->plugins = xrealloc(plugins->plugins,
			    (plugins->count + 1) * sizeof(*plugins->plugins));
	plugins->plugins[plugins->count] = plugin;
	plugins->count++;
    } else {
	rpmPluginFree(plugin);
    }

    return rc;
}

rpmRC rpmpluginsAddPlugin(rpmPlugins plugins, const char *type, const char *name)
{
    char *path;
    char *options;
    rpmRC rc = RPMRC_FAIL;

    path = rpmExpand("%{?__", type, "_", name, "}", NULL);
    if (!path || rstreq(path, "")) {
	rpmlog(RPMLOG_DEBUG, _("Plugin %%__%s_%s not configured\n"),
	       type, name);
	rc = RPMRC_NOTFOUND;
	goto exit;
    }

    /* split the options from the path */
#define SKIPSPACE(s)    { while (*(s) &&  risspace(*(s))) (s)++; }
#define SKIPNONSPACE(s) { while (*(s) && !risspace(*(s))) (s)++; }
    options = path;
    SKIPNONSPACE(options);
    if (risspace(*options)) {
	*options = '\0';
	options++;
	SKIPSPACE(options);
    }
    if (*options == '\0') {
	options = NULL;
    }

    rc = rpmpluginsAdd(plugins, name, path, options);

  exit:
    _free(path);
    return rc;
}

rpmPlugins rpmpluginsFree(rpmPlugins plugins)
{
    if (plugins) {
	for (int i = 0; i < plugins->count; i++) {
	    rpmPlugin plugin = plugins->plugins[i];
	    rpmPluginFree(plugin);
	}
	plugins->plugins = _free(plugins->plugins);
	plugins->ts = NULL;
	_free(plugins);
    }

    return NULL;
}

#define RPMPLUGINS_GET_PLUGIN(name) \
	plugin = rpmpluginsGetPlugin(plugins, name); \
	if (plugin == NULL || plugin->handle == NULL) { \
		rpmlog(RPMLOG_ERR, _("Plugin %s not loaded\n"), name); \
		return RPMRC_FAIL; \
	}

/* Common define for all rpmpluginsCall* hook functions */
#define RPMPLUGINS_SET_HOOK_FUNC(hook) \
	rpmPluginHooks hooks = (plugin != NULL) ? plugin->hooks : NULL; \
	hookFunc = (hooks != NULL) ? hooks->hook : NULL; \
	if (hookFunc) { \
	    rpmlog(RPMLOG_DEBUG, "Plugin: calling hook %s in %s plugin\n", \
		   STR(hook), plugin->name); \
	}

static rpmRC rpmpluginsCallInit(rpmPlugin plugin, rpmts ts)
{
    rpmRC rc = RPMRC_OK;
    plugin_init_func hookFunc;
    RPMPLUGINS_SET_HOOK_FUNC(init);
    if (hookFunc) {
        rc = hookFunc(plugin, ts);
        if (rc != RPMRC_OK && rc != RPMRC_NOTFOUND)
            rpmlog(RPMLOG_ERR, "Plugin %s: hook init failed\n", plugin->name);
    }
    return rc;
}

rpmRC rpmpluginsCallTsmPre(rpmPlugins plugins, rpmts ts)
{
    plugin_tsm_pre_func hookFunc;
    int i;
    rpmRC rc = RPMRC_OK;

    for (i = 0; i < plugins->count; i++) {
	rpmPlugin plugin = plugins->plugins[i];
	RPMPLUGINS_SET_HOOK_FUNC(tsm_pre);
	if (hookFunc && hookFunc(plugin, ts) == RPMRC_FAIL) {
	    rpmlog(RPMLOG_ERR, "Plugin %s: hook tsm_pre failed\n", plugin->name);
	    rc = RPMRC_FAIL;
	}
    }

    return rc;
}

rpmRC rpmpluginsCallTsmPost(rpmPlugins plugins, rpmts ts, int res)
{
    plugin_tsm_post_func hookFunc;
    int i;
    rpmRC rc = RPMRC_OK;

    for (i = 0; i < plugins->count; i++) {
	rpmPlugin plugin = plugins->plugins[i];
	RPMPLUGINS_SET_HOOK_FUNC(tsm_post);
	if (hookFunc && hookFunc(plugin, ts, res) == RPMRC_FAIL) {
	    rpmlog(RPMLOG_WARNING, "Plugin %s: hook tsm_post failed\n", plugin->name);
	}
    }

    return rc;
}

rpmRC rpmpluginsCallPsmPre(rpmPlugins plugins, rpmte te)
{
    plugin_psm_pre_func hookFunc;
    int i;
    rpmRC rc = RPMRC_OK;

    for (i = 0; i < plugins->count; i++) {
	rpmPlugin plugin = plugins->plugins[i];
	RPMPLUGINS_SET_HOOK_FUNC(psm_pre);
	if (hookFunc && hookFunc(plugin, te) == RPMRC_FAIL) {
	    rpmlog(RPMLOG_ERR, "Plugin %s: hook psm_pre failed\n", plugin->name);
	    rc = RPMRC_FAIL;
	}
    }

    return rc;
}

rpmRC rpmpluginsCallPsmPost(rpmPlugins plugins, rpmte te, int res)
{
    plugin_psm_post_func hookFunc;
    int i;
    rpmRC rc = RPMRC_OK;

    for (i = 0; i < plugins->count; i++) {
	rpmPlugin plugin = plugins->plugins[i];
	RPMPLUGINS_SET_HOOK_FUNC(psm_post);
	if (hookFunc && hookFunc(plugin, te, res) == RPMRC_FAIL) {
	    rpmlog(RPMLOG_WARNING, "Plugin %s: hook psm_post failed\n", plugin->name);
	}
    }

    return rc;
}

rpmRC rpmpluginsCallScriptletPre(rpmPlugins plugins, const char *s_name, int type)
{
    plugin_scriptlet_pre_func hookFunc;
    int i;
    rpmRC rc = RPMRC_OK;

    for (i = 0; i < plugins->count; i++) {
	rpmPlugin plugin = plugins->plugins[i];
	RPMPLUGINS_SET_HOOK_FUNC(scriptlet_pre);
	if (hookFunc && hookFunc(plugin, s_name, type) == RPMRC_FAIL) {
	    rpmlog(RPMLOG_ERR, "Plugin %s: hook scriplet_pre failed\n", plugin->name);
	    rc = RPMRC_FAIL;
	}
    }

    return rc;
}

rpmRC rpmpluginsCallScriptletForkPost(rpmPlugins plugins, const char *path, int type)
{
    plugin_scriptlet_fork_post_func hookFunc;
    int i;
    rpmRC rc = RPMRC_OK;

    for (i = 0; i < plugins->count; i++) {
	rpmPlugin plugin = plugins->plugins[i];
	RPMPLUGINS_SET_HOOK_FUNC(scriptlet_fork_post);
	if (hookFunc && hookFunc(plugin, path, type) == RPMRC_FAIL) {
	    rpmlog(RPMLOG_ERR, "Plugin %s: hook scriplet_fork_post failed\n", plugin->name);
	    rc = RPMRC_FAIL;
	}
    }

    return rc;
}

rpmRC rpmpluginsCallScriptletPost(rpmPlugins plugins, const char *s_name, int type, int res)
{
    plugin_scriptlet_post_func hookFunc;
    int i;
    rpmRC rc = RPMRC_OK;

    for (i = 0; i < plugins->count; i++) {
	rpmPlugin plugin = plugins->plugins[i];
	RPMPLUGINS_SET_HOOK_FUNC(scriptlet_post);
	if (hookFunc && hookFunc(plugin, s_name, type, res) == RPMRC_FAIL) {
	    rpmlog(RPMLOG_WARNING, "Plugin %s: hook scriplet_post failed\n", plugin->name);
	}
    }

    return rc;
}

static char *abspath(rpmfi fi, const char *path)
{
    if (*path == '/')
	return xstrdup(path);
    else
	return rstrscat(NULL, rpmfiDN(fi), path, NULL);
}

rpmRC rpmpluginsCallFsmFilePre(rpmPlugins plugins, rpmfi fi, const char *path,
			       mode_t file_mode, rpmFsmOp op)
{
    plugin_fsm_file_pre_func hookFunc;
    int i;
    rpmRC rc = RPMRC_OK;
    char *apath = abspath(fi, path);

    for (i = 0; i < plugins->count; i++) {
	rpmPlugin plugin = plugins->plugins[i];
	RPMPLUGINS_SET_HOOK_FUNC(fsm_file_pre);
	if (hookFunc && hookFunc(plugin, fi, apath, file_mode, op) == RPMRC_FAIL) {
	    rpmlog(RPMLOG_ERR, "Plugin %s: hook fsm_file_pre failed\n", plugin->name);
	    rc = RPMRC_FAIL;
	}
    }
    free(apath);

    return rc;
}

rpmRC rpmpluginsCallFsmFilePost(rpmPlugins plugins, rpmfi fi, const char *path,
                                mode_t file_mode, rpmFsmOp op, int res)
{
    plugin_fsm_file_post_func hookFunc;
    int i;
    rpmRC rc = RPMRC_OK;
    char *apath = abspath(fi, path);

    for (i = 0; i < plugins->count; i++) {
	rpmPlugin plugin = plugins->plugins[i];
	RPMPLUGINS_SET_HOOK_FUNC(fsm_file_post);
	if (hookFunc && hookFunc(plugin, fi, apath, file_mode, op, res) == RPMRC_FAIL) {
	    rpmlog(RPMLOG_WARNING, "Plugin %s: hook fsm_file_post failed\n", plugin->name);
	}
    }
    free(apath);

    return rc;
}

rpmRC rpmpluginsCallFsmFilePrepare(rpmPlugins plugins, rpmfi fi,
				   int fd, const char *path, const char *dest,
				   mode_t file_mode, rpmFsmOp op)
{
    plugin_fsm_file_prepare_func hookFunc;
    int i;
    rpmRC rc = RPMRC_OK;
    char *apath = abspath(fi, path);

    for (i = 0; i < plugins->count; i++) {
	rpmPlugin plugin = plugins->plugins[i];
	RPMPLUGINS_SET_HOOK_FUNC(fsm_file_prepare);
	if (hookFunc && hookFunc(plugin, fi, fd, apath, dest, file_mode, op) == RPMRC_FAIL) {
	    rpmlog(RPMLOG_ERR, "Plugin %s: hook fsm_file_prepare failed\n", plugin->name);
	    rc = RPMRC_FAIL;
	}
    }
    free(apath);

    return rc;
}