forked from community-ssu/dsme
-
Notifications
You must be signed in to change notification settings - Fork 1
/
modulebase.c
696 lines (545 loc) · 16.4 KB
/
modulebase.c
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
/**
@file modulebase.c
Implements DSME plugin framework.
<p>
Copyright (C) 2004-2009 Nokia Corporation
@author Ari Saastamoinen
@author Semi Malinen <[email protected]>
This file is part of Dsme.
Dsme is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License
version 2.1 as published by the Free Software Foundation.
Dsme 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with Dsme. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __cplusplus
#define _GNU_SOURCE
#endif
#include "dsme/modulebase.h"
#include "dsme/messages.h"
#include "dsme/protocol.h"
#include "dsme/logging.h"
#include <glib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dlfcn.h>
#include <sys/types.h>
#include <unistd.h>
/**
Loaded module information.
*/
struct module_t {
char* name;
int priority;
void* handle;
};
/**
Registered handler information.
*/
typedef struct {
u_int32_t msg_type;
size_t msg_size;
const module_t* owner;
handler_fn_t* callback;
} msg_handler_info_t;
/**
Sender of a queued message.
*/
struct endpoint_t {
const module_t* module;
dsmesock_connection_t* conn;
struct ucred ucred; // only valid when conn != 0
};
/**
Queued message.
*/
typedef struct {
endpoint_t from;
const module_t* to;
dsmemsg_generic_t* data;
} queued_msg_t;
static int add_msghandlers(module_t* module);
static void remove_msghandlers(module_t* module);
/**
Comparison function; matches messages with handlers by message type.
@param a Handler to be compared
@param b Message type to be compared. Internally cast to integer.
@return negative/zero/positive if a is smaller/equal/larger than b
*/
static gint msg_comparator(gconstpointer a, gconstpointer b);
/**
Comparison function; used to sort message handlers.
Message handlers are sorted primarily by message type in descending order.
Handlers for same message type are sorted by priority in descending order.
@param a New handler to be added to the list of handlers.
@param b Existing handler in list of handlers.
@return negative/zero/positive if a is smaller/equal/larger than b.
*/
static gint sort_comparator(gconstpointer a, gconstpointer b, gpointer unused);
/**
Type agnostic, overflow safe macro for comparing numeric values.
@param v1 1st number
@param v2 2nd number (of the same type as the 1st one)
@return -1,0,+1 depending if v1 is smaller, equal or larger than v2
*/
#define compare(v1,v2) (((v1)>(v2))-((v1)<(v2)))
/**
Passes a message to all matching message handlers.
@param from Sender of the message
@param msg Message to be handled
@return 0
*/
static int handle_message(endpoint_t* from,
const module_t* to,
const dsmemsg_generic_t* msg);
static GSList* modules = 0;
static GSList* callbacks = 0;
static GSList* message_queue = 0;
static const struct ucred bogus_ucred = {
.pid = 0,
.uid = -1,
.gid = -1
};
static int msg_comparator(gconstpointer a, gconstpointer b)
{
const msg_handler_info_t* handler = (msg_handler_info_t*)a;
u_int32_t msg_type = GPOINTER_TO_UINT(b);
return compare(handler->msg_type, msg_type);
}
static gint sort_comparator(gconstpointer a, gconstpointer b, gpointer unused)
{
const msg_handler_info_t* handler = (msg_handler_info_t*)a;
const msg_handler_info_t* existing = (msg_handler_info_t*)b;
return compare(handler->msg_type, existing->msg_type) ?:
compare(handler->owner->priority, existing->owner->priority);
}
#ifdef OBSOLETE
/**
Comparison function; match module by name
@param node Module node to be compared
@param name String to be compared
@return 0 when name equas with node name, <>0 otherwise
*/
static int name_comparator(const module_t* node, const char* name)
{
return !strcmp(node->name, name);
}
#endif
/**
Add single hadler in message handlers list
@param msg_type Type of the message to be registered
@param callback Function to be called when given msg_type is handled
@param owner Pointer to the module module who owns this callback
@return 0 on OK, -1 on error
*/
int add_single_handler(u_int32_t msg_type,
size_t msg_size,
handler_fn_t* callback,
const module_t* owner)
{
msg_handler_info_t* handler = 0;
handler = (msg_handler_info_t*)malloc(sizeof(msg_handler_info_t));
if (!handler) {
return -1;
}
handler->msg_type = msg_type;
handler->msg_size = msg_size;
handler->callback = callback;
handler->owner = owner;
/* Insert into sorted list. */
callbacks = g_slist_insert_sorted_with_data(callbacks,
handler,
sort_comparator,
0);
return 0;
}
/**
Locates message handler table from module and adds all message handlers
to global handler list.
@param module Pointer to the module to be initialised
@return 0 when OK, or -1 on error
*/
static int add_msghandlers(module_t* module)
{
module_fn_info_t* msg_handler_ptr;
if (!module)
return -1;
for (msg_handler_ptr = (module_fn_info_t*)dlsym(module->handle,
"message_handlers");
msg_handler_ptr && msg_handler_ptr->callback;
msg_handler_ptr++)
{
if (add_single_handler(msg_handler_ptr->msg_type,
msg_handler_ptr->msg_size,
msg_handler_ptr->callback,
module))
return -1;
}
return 0;
}
/**
This function is called when some module is unloaded.
This removes all registered message callbacks of gimes module.
@param module Module whose callbacks will be removed
*/
static void remove_msghandlers(module_t* module)
{
GSList* node;
GSList* next;
for (node = callbacks; node != 0; node = next) {
next = g_slist_next(node);
if (node->data &&
((msg_handler_info_t *)(node->data))->owner == module)
{
free(node->data);
node->data = NULL;
callbacks = g_slist_delete_link(callbacks, node);
}
}
}
static const module_t* currently_handling_module = 0;
// TODO: all these returns and mallocs are a mess
static void queue_message(const endpoint_t* from,
const module_t* to,
const void* msg,
size_t extra_size,
const void* extra)
{
queued_msg_t* newmsg;
dsmemsg_generic_t* genmsg = (dsmemsg_generic_t*)msg;
if (!msg) return;
if (genmsg->line_size_ < sizeof(dsmemsg_generic_t)) return;
newmsg = (queued_msg_t*)malloc(sizeof(queued_msg_t));
if (!newmsg) return;
newmsg->data = (dsmemsg_generic_t*)malloc(genmsg->line_size_ + extra_size);
if (newmsg->data) {
memcpy(newmsg->data, genmsg, genmsg->line_size_);
memcpy(((char*)newmsg->data)+genmsg->line_size_, extra, extra_size);
newmsg->data->line_size_ += extra_size;
newmsg->from = *from;
newmsg->to = to;
// TODO: perhaps use GQueue for faster appending?
message_queue = g_slist_append(message_queue, newmsg);
return;
}
free(newmsg);
newmsg = NULL;
}
void broadcast_internally(const void* msg)
{
endpoint_t from = {
.module = currently_handling_module,
.conn = 0,
.ucred = bogus_ucred
};
/* use 0 as recipient for broadcasting */
queue_message(&from, 0, msg, 0, 0);
}
void broadcast_internally_from_socket(const void* msg,
dsmesock_connection_t* conn)
{
endpoint_t from = {
.module = 0,
.conn = conn,
};
const struct ucred* ucred = dsmesock_getucred(conn);
if (ucred) {
from.ucred = *ucred;
} else {
from.ucred = bogus_ucred;
}
/* use 0 as recipient for broadcasting */
queue_message(&from, 0, msg, 0, 0);
}
void broadcast_with_extra(const void* msg, size_t extra_size, const void* extra)
{
endpoint_t from = {
.module = currently_handling_module,
.conn = 0,
.ucred = bogus_ucred
};
queue_message(&from, 0, msg, extra_size, extra);
dsmesock_broadcast_with_extra(msg, extra_size, extra);
}
void broadcast(const void* msg)
{
broadcast_with_extra(msg, 0, 0);
}
static void queue_for_module_with_extra(const module_t* recipient,
const void* msg,
size_t extra_size,
const void* extra)
{
endpoint_t from = {
.module = currently_handling_module,
.conn = 0,
.ucred = bogus_ucred
};
queue_message(&from, recipient, msg, extra_size, extra);
}
void endpoint_send_with_extra(endpoint_t* recipient,
const void* msg,
size_t extra_size,
const void* extra)
{
if (recipient) {
if (recipient->module) {
queue_for_module_with_extra(recipient->module, msg, extra_size, extra);
} else if (recipient->conn) {
dsmesock_send_with_extra(recipient->conn, msg, extra_size, extra);
} else {
dsme_log(LOG_DEBUG, "endpoint_send(): no endpoint");
}
} else {
dsme_log(LOG_DEBUG, "endpoint_send(): null endpoint");
}
}
void endpoint_send(endpoint_t* recipient, const void* msg)
{
endpoint_send_with_extra(recipient, msg, 0, 0);
}
const struct ucred* endpoint_ucred(const endpoint_t* sender)
{
const struct ucred* u = 0;
if (sender) {
if (sender->module) {
static struct ucred module_ucred;
module_ucred.pid = getpid();
module_ucred.uid = getuid();
module_ucred.gid = getgid();
u = &module_ucred;
} else if (sender->conn) {
u = &sender->ucred;
}
}
if (!u) {
u = &bogus_ucred;
}
return u;
}
char* endpoint_name_by_pid(pid_t pid)
{
char* name = 0;
char* filename;
if (asprintf(&filename, "/proc/%u/cmdline", pid) != -1) {
int ret;
FILE* f = fopen(filename, "r");
free(filename);
if (f) {
char* cmdline = 0;
size_t n;
ssize_t l = getline(&cmdline, &n, f);
fclose(f);
if (l > 0) {
cmdline[l - 1] = '\0';
ret = asprintf(&name, "pid %u: %s", pid, cmdline);
} else {
ret = asprintf(&name, "pid %u: (no name)", pid);
}
free(cmdline);
} else {
ret = asprintf(&name, "pid %u: (no such process)", pid);
}
if (ret == -1) {
name = 0;
}
}
return name;
}
char* endpoint_name(const endpoint_t* sender)
{
char* name = 0;
if (!sender) {
name = strdup("(null endpoint)");
} else if (!sender->conn) {
name = strdup("dsme");
} else {
name = endpoint_name_by_pid(sender->conn->ucred.pid);
}
return name;
}
bool endpoint_same(const endpoint_t* a, const endpoint_t* b)
{
bool same = false;
if (a && b) {
if ((a->module && a->module == b->module) ||
(a->conn && a->conn == b->conn))
{
same = true;
}
}
return same;
}
bool endpoint_is_dsme(const endpoint_t* endpoint)
{
return (endpoint && endpoint->conn == 0);
}
endpoint_t* endpoint_copy(const endpoint_t* endpoint)
{
endpoint_t* copy = 0;
if (endpoint) {
copy = malloc(sizeof(endpoint_t));
if (copy) {
copy->module = endpoint->module;
copy->conn = endpoint->conn;
}
}
return copy;
}
void endpoint_free(endpoint_t* endpoint)
{
free(endpoint);
}
void process_message_queue(void)
{
while (message_queue) {
queued_msg_t* front = (queued_msg_t*)message_queue->data;
message_queue = g_slist_delete_link(message_queue,
message_queue);
handle_message(&front->from, front->to, front->data);
free(front->data);
free(front);
}
}
static int handle_message(endpoint_t* from,
const module_t* to,
const dsmemsg_generic_t* msg)
{
GSList* node;
const msg_handler_info_t* handler;
node = callbacks;
while ((node = g_slist_find_custom(node,
GUINT_TO_POINTER(dsmemsg_id(msg)),
msg_comparator)))
{
handler = (msg_handler_info_t*)(node->data);
if (handler && handler->callback) {
if (!to || to == handler->owner) {
if (msg->line_size_ >= handler->msg_size &&
msg->size_ == handler->msg_size)
{
currently_handling_module = handler->owner;
handler->callback(from, msg);
currently_handling_module = 0;
}
}
}
node = g_slist_next(node);
}
return 0;
}
bool unload_module(module_t* module)
{
bool unloaded = false;
GSList* node;
if (module && (node = g_slist_find(modules, module))) {
remove_msghandlers(module);
if (module->handle) {
/* Call module_fini() function if it exists */
module_fini_fn_t* finifunc =
(module_fini_fn_t *)dlsym(module->handle, "module_fini");
if (finifunc) {
currently_handling_module = module;
finifunc();
currently_handling_module = 0;
}
dlclose(module->handle);
}
if (module->name) {
free(module->name);
module->name = NULL;
}
free(module);
module = NULL;
modules = g_slist_delete_link(modules, node);
unloaded = true;
}
return unloaded;
}
module_t* load_module(const char* filename, int priority)
{
void* dlhandle = 0;
module_t* module = 0;
module_init_fn_t* initfunc;
/* Prepend ./ to non-absolute path */
if (*filename != '/') {
int namelen;
char * newname;
namelen = strlen(filename) + 3;
newname = (char*)malloc(namelen);
if (newname) {
snprintf(newname, namelen, "./%s", filename);
dlhandle = dlopen(newname, RTLD_NOW | RTLD_GLOBAL);
free(newname);
}
}
if (!dlhandle) {
dlhandle = dlopen(filename, RTLD_NOW | RTLD_GLOBAL);
}
if (!dlhandle) {
dsme_log(LOG_CRIT, "%s", dlerror());
return 0;
}
/* Now the module should be open */
module = (module_t*)malloc(sizeof(module_t));
if (!module) {
goto error;
}
module->handle = dlhandle;
module->priority = priority;
module->name = strdup(filename);
if (!module->name) {
goto error;
}
/* Call module_init() -function if it exists */
initfunc = (module_init_fn_t *)dlsym(dlhandle, "module_init");
if (initfunc) {
currently_handling_module = module;
initfunc(module);
currently_handling_module = 0;
}
/* Add message handlers for the module */
if(add_msghandlers(module)) {
goto error;
}
/* Insert thee module to the modulelist */
modules = g_slist_append(modules, module); /* Add module to list */
return module;
error:
dsme_log(LOG_CRIT, "%s", dlerror());
if (module) {
remove_msghandlers(module);
free(module);
}
if (dlhandle) dlclose(dlhandle);
return 0;
}
bool modulebase_init(const struct _GSList* module_names)
{
const GSList* modname;
for (modname = module_names; modname; modname = g_slist_next(modname)) {
if (load_module((const char*)modname->data, 0) == NULL) {
dsme_log(LOG_CRIT, "Error loading start-up module: %s", dlerror());
return false;
}
}
return true;
}
const char* module_name(const module_t* module)
{
return module->name;
}
int modulebase_shutdown(void)
{
while (modules) {
process_message_queue();
unload_module((module_t*)modules->data);
}
process_message_queue();
return 0;
}