2015-11-19 22:32:11 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ubusd.h"
|
|
|
|
|
|
|
|
static struct ubus_object *monitor_obj;
|
|
|
|
static LIST_HEAD(monitors);
|
|
|
|
|
|
|
|
struct ubus_monitor {
|
|
|
|
struct list_head list;
|
|
|
|
struct ubus_client *cl;
|
|
|
|
uint32_t seq;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
ubusd_monitor_free(struct ubus_monitor *m)
|
|
|
|
{
|
|
|
|
list_del(&m->list);
|
|
|
|
free(m);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ubusd_monitor_connect(struct ubus_client *cl, struct ubus_msg_buf *ub)
|
|
|
|
{
|
|
|
|
struct ubus_monitor *m;
|
|
|
|
|
|
|
|
ubusd_monitor_disconnect(cl);
|
|
|
|
|
|
|
|
m = calloc(1, sizeof(*m));
|
|
|
|
m->cl = cl;
|
|
|
|
list_add(&m->list, &monitors);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ubusd_monitor_disconnect(struct ubus_client *cl)
|
|
|
|
{
|
|
|
|
struct ubus_monitor *m;
|
|
|
|
|
|
|
|
list_for_each_entry(m, &monitors, list) {
|
|
|
|
if (m->cl != cl)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
ubusd_monitor_free(m);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ubusd_monitor_message(struct ubus_client *cl, struct ubus_msg_buf *ub, bool send)
|
|
|
|
{
|
|
|
|
static struct blob_buf mb;
|
|
|
|
struct ubus_monitor *m;
|
|
|
|
|
|
|
|
if (list_empty(&monitors))
|
|
|
|
return;
|
|
|
|
|
|
|
|
blob_buf_init(&mb, 0);
|
|
|
|
blob_put_int32(&mb, UBUS_MONITOR_CLIENT, cl->id.id);
|
|
|
|
blob_put_int32(&mb, UBUS_MONITOR_PEER, ub->hdr.peer);
|
|
|
|
blob_put_int32(&mb, UBUS_MONITOR_SEQ, ub->hdr.seq);
|
|
|
|
blob_put_int32(&mb, UBUS_MONITOR_TYPE, ub->hdr.type);
|
|
|
|
blob_put_int8(&mb, UBUS_MONITOR_SEND, send);
|
|
|
|
blob_put(&mb, UBUS_MONITOR_DATA, blob_data(ub->data), blob_len(ub->data));
|
|
|
|
|
2017-07-05 15:21:47 +02:00
|
|
|
ub = ubus_msg_new(mb.head, blob_raw_len(mb.head), true);
|
|
|
|
ub->hdr.type = UBUS_MSG_MONITOR;
|
|
|
|
|
2015-11-19 22:32:11 +01:00
|
|
|
list_for_each_entry(m, &monitors, list) {
|
|
|
|
ub->hdr.seq = ++m->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(m->cl, ub);
|
2015-11-19 22:32:11 +01:00
|
|
|
}
|
2017-07-05 15:21:47 +02:00
|
|
|
|
|
|
|
ubus_msg_free(ub);
|
2015-11-19 22:32:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ubusd_monitor_recv(struct ubus_client *cl, struct ubus_msg_buf *ub,
|
|
|
|
const char *method, struct blob_attr *msg)
|
|
|
|
{
|
|
|
|
/* Only root is allowed for now */
|
|
|
|
if (cl->uid != 0 || cl->gid != 0)
|
|
|
|
return UBUS_STATUS_PERMISSION_DENIED;
|
|
|
|
|
|
|
|
if (!strcmp(method, "add")) {
|
|
|
|
ubusd_monitor_connect(cl, ub);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmp(method, "remove")) {
|
|
|
|
ubusd_monitor_disconnect(cl);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return UBUS_STATUS_METHOD_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ubusd_monitor_init(void)
|
|
|
|
{
|
|
|
|
monitor_obj = ubusd_create_object_internal(NULL, UBUS_SYSTEM_OBJECT_MONITOR);
|
|
|
|
if (monitor_obj != NULL)
|
|
|
|
monitor_obj->recv_msg = ubusd_monitor_recv;
|
|
|
|
}
|