2011-06-17 16:35:11 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2011 Felix Fietkau <nbd@openwrt.org>
|
|
|
|
*
|
|
|
|
* This program 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
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2011-02-07 16:43:30 +01:00
|
|
|
#include <arpa/inet.h>
|
2011-02-05 01:29:52 +01:00
|
|
|
#include "ubusd.h"
|
|
|
|
|
|
|
|
static struct avl_tree patterns;
|
|
|
|
static struct ubus_object *event_obj;
|
2011-02-07 01:25:28 +01:00
|
|
|
static int event_seq = 0;
|
2011-02-10 01:37:32 +01:00
|
|
|
static int obj_event_seq = 1;
|
2011-02-05 01:29:52 +01:00
|
|
|
|
2011-02-05 19:53:14 +01:00
|
|
|
struct event_source {
|
|
|
|
struct list_head list;
|
2011-02-05 01:29:52 +01:00
|
|
|
struct ubus_object *obj;
|
2011-02-11 01:21:07 +01:00
|
|
|
struct avl_node avl;
|
|
|
|
bool partial;
|
2011-02-05 19:53:14 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static void ubusd_delete_event_source(struct event_source *evs)
|
2011-02-05 01:29:52 +01:00
|
|
|
{
|
2011-02-05 19:53:14 +01:00
|
|
|
list_del(&evs->list);
|
2011-02-11 01:21:07 +01:00
|
|
|
avl_delete(&patterns, &evs->avl);
|
2011-02-05 19:53:14 +01:00
|
|
|
free(evs);
|
2011-02-05 01:29:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ubusd_event_cleanup_object(struct ubus_object *obj)
|
|
|
|
{
|
2019-12-16 23:41:31 +01:00
|
|
|
struct event_source *ev, *tmp;
|
2011-02-05 01:29:52 +01:00
|
|
|
|
2019-12-16 23:41:31 +01:00
|
|
|
list_for_each_entry_safe(ev, tmp, &obj->events, list) {
|
2011-02-05 19:53:14 +01:00
|
|
|
ubusd_delete_event_source(ev);
|
2011-02-05 01:29:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-05 19:53:14 +01:00
|
|
|
enum {
|
2011-02-07 01:25:28 +01:00
|
|
|
EVREG_PATTERN,
|
|
|
|
EVREG_OBJECT,
|
|
|
|
EVREG_LAST,
|
2011-02-05 19:53:14 +01:00
|
|
|
};
|
|
|
|
|
2011-02-07 01:25:28 +01:00
|
|
|
static struct blobmsg_policy evr_policy[] = {
|
|
|
|
[EVREG_PATTERN] = { .name = "pattern", .type = BLOBMSG_TYPE_STRING },
|
|
|
|
[EVREG_OBJECT] = { .name = "object", .type = BLOBMSG_TYPE_INT32 },
|
2011-02-05 19:53:14 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static int ubusd_alloc_event_pattern(struct ubus_client *cl, struct blob_attr *msg)
|
2011-02-05 01:29:52 +01:00
|
|
|
{
|
2011-02-05 19:53:14 +01:00
|
|
|
struct event_source *ev;
|
|
|
|
struct ubus_object *obj;
|
2011-02-07 01:25:28 +01:00
|
|
|
struct blob_attr *attr[EVREG_LAST];
|
2011-04-13 20:13:42 +02:00
|
|
|
char *pattern, *name;
|
2011-02-05 19:53:14 +01:00
|
|
|
uint32_t id;
|
2011-02-07 03:20:05 +01:00
|
|
|
bool partial = false;
|
|
|
|
int len;
|
2011-02-05 19:53:14 +01:00
|
|
|
|
2011-02-07 01:25:28 +01:00
|
|
|
blobmsg_parse(evr_policy, EVREG_LAST, attr, blob_data(msg), blob_len(msg));
|
2011-02-11 01:21:07 +01:00
|
|
|
if (!attr[EVREG_OBJECT] || !attr[EVREG_PATTERN])
|
2011-02-05 19:53:14 +01:00
|
|
|
return UBUS_STATUS_INVALID_ARGUMENT;
|
|
|
|
|
2011-02-07 01:25:28 +01:00
|
|
|
id = blobmsg_get_u32(attr[EVREG_OBJECT]);
|
2011-02-05 19:53:14 +01:00
|
|
|
if (id < UBUS_SYSTEM_OBJECT_MAX)
|
|
|
|
return UBUS_STATUS_PERMISSION_DENIED;
|
|
|
|
|
|
|
|
obj = ubusd_find_object(id);
|
|
|
|
if (!obj)
|
|
|
|
return UBUS_STATUS_NOT_FOUND;
|
|
|
|
|
|
|
|
if (obj->client != cl)
|
|
|
|
return UBUS_STATUS_PERMISSION_DENIED;
|
|
|
|
|
2011-02-07 01:25:28 +01:00
|
|
|
pattern = blobmsg_data(attr[EVREG_PATTERN]);
|
2011-02-07 03:20:05 +01:00
|
|
|
|
|
|
|
len = strlen(pattern);
|
|
|
|
if (pattern[len - 1] == '*') {
|
|
|
|
partial = true;
|
|
|
|
pattern[len - 1] = 0;
|
|
|
|
len--;
|
|
|
|
}
|
|
|
|
|
2018-10-03 15:36:17 +02:00
|
|
|
if (pattern[0] && ubusd_acl_check(cl, pattern, NULL, UBUS_ACL_LISTEN))
|
|
|
|
return UBUS_STATUS_PERMISSION_DENIED;
|
|
|
|
|
2011-02-11 01:21:07 +01:00
|
|
|
ev = calloc(1, sizeof(*ev) + len + 1);
|
|
|
|
if (!ev)
|
|
|
|
return UBUS_STATUS_NO_DATA;
|
|
|
|
|
|
|
|
list_add(&ev->list, &obj->events);
|
|
|
|
ev->obj = obj;
|
|
|
|
ev->partial = partial;
|
2011-04-13 20:13:42 +02:00
|
|
|
name = (char *) (ev + 1);
|
|
|
|
strcpy(name, pattern);
|
|
|
|
ev->avl.key = name;
|
2011-02-11 01:21:07 +01:00
|
|
|
avl_insert(&patterns, &ev->avl);
|
2011-02-05 19:53:14 +01:00
|
|
|
|
2011-02-05 01:29:52 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-02-07 03:51:00 +01:00
|
|
|
static void ubusd_send_event_msg(struct ubus_msg_buf **ub, struct ubus_client *cl,
|
|
|
|
struct ubus_object *obj, const char *id,
|
2011-02-10 01:21:24 +01:00
|
|
|
event_fill_cb fill_cb, void *cb_priv)
|
2011-02-07 01:25:28 +01:00
|
|
|
{
|
|
|
|
uint32_t *objid_ptr;
|
|
|
|
|
2011-02-07 03:51:00 +01:00
|
|
|
/* do not loop back events */
|
|
|
|
if (obj->client == cl)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* do not send duplicate events */
|
|
|
|
if (obj->event_seen == obj_event_seq)
|
|
|
|
return;
|
|
|
|
|
|
|
|
obj->event_seen = obj_event_seq;
|
|
|
|
|
2011-02-10 01:21:24 +01:00
|
|
|
if (!*ub) {
|
|
|
|
*ub = fill_cb(cb_priv, id);
|
2011-02-07 01:25:28 +01:00
|
|
|
(*ub)->hdr.type = UBUS_MSG_INVOKE;
|
|
|
|
(*ub)->hdr.peer = 0;
|
|
|
|
}
|
2011-02-10 01:21:24 +01:00
|
|
|
|
|
|
|
objid_ptr = blob_data(blob_data((*ub)->data));
|
|
|
|
*objid_ptr = htonl(obj->id.id);
|
|
|
|
|
2011-02-07 01:25:28 +01:00
|
|
|
(*ub)->hdr.seq = ++event_seq;
|
ubusd: don't free messages in ubus_send_msg() anymore
This makes it clear that `ubus_msg_send()` is only
about sending and queue-ing messages, and has nothing
to do with free-ing.
It can be a bit misleading/confusing when trying to go
through the code and make assumptions about whether a
buffer is free'd in ubus_send_msg(), or is free'd outside.
In `ubusd_proto_receive_message()` the `ubus_msg_free()`
is now called before the `if (ret == -1)` check.
That way, all callbacks will have their messages free'd,
which is what's desired, but confusing, because:
* ubusd_handle_invoke() called ubus_msg_free() before returning -1
* ubusd_handle_notify() called ubus_msg_free() before returning -1
* ubusd_handle_response() called ubus_msg_send(,,free=true) before returning -1
* ubus_msg_send() would call ubus_msg_send(,,free=false)
* all other callback callers would `ubus_msg_send(,,free=true)`
(free the buffers in ubus_msg_send() )
In all other places, where `ubus_msg_send(,,free=true)`
an explicit `ubus_msg_free()` was added.
Signed-off-by: Alexandru Ardelean <ardeleanalex@gmail.com>
2017-07-05 15:20:51 +02:00
|
|
|
ubus_msg_send(obj->client, *ub);
|
2011-02-07 01:25:28 +01:00
|
|
|
}
|
|
|
|
|
2015-04-21 00:45:52 +02:00
|
|
|
int ubusd_send_event(struct ubus_client *cl, const char *id,
|
|
|
|
event_fill_cb fill_cb, void *cb_priv)
|
2011-02-07 01:25:28 +01:00
|
|
|
{
|
2011-02-10 01:21:24 +01:00
|
|
|
struct ubus_msg_buf *ub = NULL;
|
2011-02-07 01:25:28 +01:00
|
|
|
struct event_source *ev;
|
2011-02-07 03:51:00 +01:00
|
|
|
int match_len = 0;
|
|
|
|
|
2018-10-03 15:36:18 +02:00
|
|
|
if (ubusd_acl_check(cl, id, NULL, UBUS_ACL_SEND))
|
|
|
|
return UBUS_STATUS_PERMISSION_DENIED;
|
|
|
|
|
2011-02-07 03:51:00 +01:00
|
|
|
obj_event_seq++;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Since this tree is sorted alphabetically, we can only expect to find
|
|
|
|
* matching entries as long as the number of matching characters
|
|
|
|
* between the pattern string and our string is monotonically increasing.
|
|
|
|
*/
|
2011-02-11 01:21:07 +01:00
|
|
|
avl_for_each_element(&patterns, ev, avl) {
|
|
|
|
const char *key = ev->avl.key;
|
2011-02-07 03:51:00 +01:00
|
|
|
int cur_match_len;
|
|
|
|
bool full_match;
|
|
|
|
|
2018-10-03 15:36:15 +02:00
|
|
|
full_match = ubus_strmatch_len(id, key, &cur_match_len);
|
2011-02-07 03:51:00 +01:00
|
|
|
if (cur_match_len < match_len)
|
|
|
|
break;
|
|
|
|
|
|
|
|
match_len = cur_match_len;
|
|
|
|
|
|
|
|
if (!full_match) {
|
2011-02-11 01:21:07 +01:00
|
|
|
if (!ev->partial)
|
2011-02-07 03:51:00 +01:00
|
|
|
continue;
|
|
|
|
|
iron out all extra compiler warnings
clang-9 on x86/64 has reported following warnings/errors:
libubus-acl.c:123:2: error: comparison of integers of different signs: 'size_t' (aka 'unsigned long') and 'int' [-Werror,-Wsign-compare]
libubus-io.c:108:18: error: comparison of integers of different signs: 'int' and 'size_t' (aka 'unsigned long') [-Werror,-Wsign-compare]
libubus-io.c:395:56: error: comparison of integers of different signs: 'ssize_t' (aka 'long') and 'size_t' (aka 'unsigned long') [-Werror,-Wsign-compare]
libubus-req.c:441:4: error: comparison of integers of different signs: 'size_t' (aka 'unsigned long') and 'int' [-Werror,-Wsign-compare]
ubusd_acl.c:119:18: error: comparison of integers of different signs: 'int' and 'unsigned long' [-Werror,-Wsign-compare]
ubusd_acl.c:152:5: error: comparison of integers of different signs: 'size_t' (aka 'unsigned long') and 'int' [-Werror,-Wsign-compare]
ubusd_acl.c:348:3: error: comparison of integers of different signs: 'size_t' (aka 'unsigned long') and 'int' [-Werror,-Wsign-compare]
ubusd_acl.c:352:3: error: comparison of integers of different signs: 'size_t' (aka 'unsigned long') and 'int' [-Werror,-Wsign-compare]
ubusd_acl.c:357:3: error: comparison of integers of different signs: 'size_t' (aka 'unsigned long') and 'int' [-Werror,-Wsign-compare]
ubusd_acl.c:362:3: error: comparison of integers of different signs: 'size_t' (aka 'unsigned long') and 'int' [-Werror,-Wsign-compare]
ubusd_acl.c:367:3: error: comparison of integers of different signs: 'size_t' (aka 'unsigned long') and 'int' [-Werror,-Wsign-compare]
ubusd_acl.c:447:16: error: comparison of integers of different signs: 'int' and '__size_t' (aka 'unsigned long') [-Werror,-Wsign-compare]
ubusd_acl.c:502:18: error: comparison of integers of different signs: 'int' and 'unsigned long' [-Werror,-Wsign-compare]
ubusd.c:123:13: error: comparison of integers of different signs: 'int' and 'unsigned long' [-Werror,-Wsign-compare]
ubusd.c:170:15: error: comparison of integers of different signs: 'int' and 'unsigned long' [-Werror,-Wsign-compare]
ubusd.c:262:43: error: comparison of integers of different signs: 'int' and 'unsigned long' [-Werror,-Wsign-compare]
ubusd.c:287:30: error: comparison of integers of different signs: 'int' and 'unsigned long' [-Werror,-Wsign-compare]
ubusd_event.c:170:18: error: comparison of integers of different signs: 'int' and 'unsigned long' [-Werror,-Wsign-compare]
ubusd_obj.c:71:2: error: comparison of integers of different signs: 'size_t' (aka 'unsigned long') and 'int' [-Werror,-Wsign-compare]
Signed-off-by: Petr Štetiar <ynezz@true.cz>
2019-12-11 10:36:36 +01:00
|
|
|
if (match_len != (int) strlen(key))
|
2011-02-07 03:51:00 +01:00
|
|
|
continue;
|
|
|
|
}
|
2011-02-07 01:25:28 +01:00
|
|
|
|
2011-02-10 01:21:24 +01:00
|
|
|
ubusd_send_event_msg(&ub, cl, ev->obj, id, fill_cb, cb_priv);
|
2011-02-07 01:25:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ub)
|
|
|
|
ubus_msg_free(ub);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-02-10 01:05:28 +01:00
|
|
|
enum {
|
|
|
|
EVMSG_ID,
|
|
|
|
EVMSG_DATA,
|
|
|
|
EVMSG_LAST,
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct blobmsg_policy ev_policy[] = {
|
|
|
|
[EVMSG_ID] = { .name = "id", .type = BLOBMSG_TYPE_STRING },
|
|
|
|
[EVMSG_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE },
|
|
|
|
};
|
|
|
|
|
2011-02-10 01:21:24 +01:00
|
|
|
static struct ubus_msg_buf *
|
|
|
|
ubusd_create_event_from_msg(void *priv, const char *id)
|
|
|
|
{
|
|
|
|
struct blob_attr *msg = priv;
|
|
|
|
|
|
|
|
blob_buf_init(&b, 0);
|
|
|
|
blob_put_int32(&b, UBUS_ATTR_OBJID, 0);
|
|
|
|
blob_put_string(&b, UBUS_ATTR_METHOD, id);
|
|
|
|
blob_put(&b, UBUS_ATTR_DATA, blobmsg_data(msg), blobmsg_data_len(msg));
|
|
|
|
|
|
|
|
return ubus_msg_new(b.head, blob_raw_len(b.head), true);
|
|
|
|
}
|
|
|
|
|
2011-02-10 01:05:28 +01:00
|
|
|
static int ubusd_forward_event(struct ubus_client *cl, struct blob_attr *msg)
|
|
|
|
{
|
|
|
|
struct blob_attr *data;
|
|
|
|
struct blob_attr *attr[EVMSG_LAST];
|
|
|
|
const char *id;
|
|
|
|
|
|
|
|
blobmsg_parse(ev_policy, EVMSG_LAST, attr, blob_data(msg), blob_len(msg));
|
|
|
|
if (!attr[EVMSG_ID] || !attr[EVMSG_DATA])
|
|
|
|
return UBUS_STATUS_INVALID_ARGUMENT;
|
|
|
|
|
|
|
|
id = blobmsg_data(attr[EVMSG_ID]);
|
|
|
|
data = attr[EVMSG_DATA];
|
|
|
|
|
|
|
|
if (!strncmp(id, "ubus.", 5))
|
|
|
|
return UBUS_STATUS_PERMISSION_DENIED;
|
|
|
|
|
2011-02-10 01:21:24 +01:00
|
|
|
return ubusd_send_event(cl, id, ubusd_create_event_from_msg, data);
|
2011-02-10 01:05:28 +01:00
|
|
|
}
|
|
|
|
|
2015-04-20 11:08:26 +02:00
|
|
|
static int ubusd_event_recv(struct ubus_client *cl, struct ubus_msg_buf *ub, const char *method, struct blob_attr *msg)
|
2011-02-05 19:53:14 +01:00
|
|
|
{
|
|
|
|
if (!strcmp(method, "register"))
|
|
|
|
return ubusd_alloc_event_pattern(cl, msg);
|
|
|
|
|
2011-02-07 01:25:28 +01:00
|
|
|
if (!strcmp(method, "send"))
|
2011-02-10 01:05:28 +01:00
|
|
|
return ubusd_forward_event(cl, msg);
|
2011-02-07 01:25:28 +01:00
|
|
|
|
2011-02-05 19:53:14 +01:00
|
|
|
return UBUS_STATUS_INVALID_COMMAND;
|
|
|
|
}
|
|
|
|
|
2011-02-10 01:31:52 +01:00
|
|
|
static struct ubus_msg_buf *
|
|
|
|
ubusd_create_object_event_msg(void *priv, const char *id)
|
|
|
|
{
|
|
|
|
struct ubus_object *obj = priv;
|
|
|
|
void *s;
|
|
|
|
|
|
|
|
blob_buf_init(&b, 0);
|
|
|
|
blob_put_int32(&b, UBUS_ATTR_OBJID, 0);
|
|
|
|
blob_put_string(&b, UBUS_ATTR_METHOD, id);
|
|
|
|
s = blob_nest_start(&b, UBUS_ATTR_DATA);
|
|
|
|
blobmsg_add_u32(&b, "id", obj->id.id);
|
|
|
|
blobmsg_add_string(&b, "path", obj->path.key);
|
|
|
|
blob_nest_end(&b, s);
|
|
|
|
|
|
|
|
return ubus_msg_new(b.head, blob_raw_len(b.head), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ubusd_send_obj_event(struct ubus_object *obj, bool add)
|
|
|
|
{
|
|
|
|
const char *id = add ? "ubus.object.add" : "ubus.object.remove";
|
|
|
|
|
|
|
|
ubusd_send_event(NULL, id, ubusd_create_object_event_msg, obj);
|
|
|
|
}
|
|
|
|
|
2011-02-05 01:29:52 +01:00
|
|
|
void ubusd_event_init(void)
|
|
|
|
{
|
|
|
|
ubus_init_string_tree(&patterns, true);
|
|
|
|
event_obj = ubusd_create_object_internal(NULL, UBUS_SYSTEM_OBJECT_EVENT);
|
2015-04-13 18:18:36 +02:00
|
|
|
if (event_obj != NULL)
|
|
|
|
event_obj->recv_msg = ubusd_event_recv;
|
2011-02-05 01:29:52 +01:00
|
|
|
}
|
|
|
|
|