free(m);
}
-static void
+static bool
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));
+ if (!m)
+ return false;
+
m->cl = cl;
- list_add(&m->list, &monitors);
+ list_add_tail(&m->list, &monitors);
+
+ return true;
}
-void
-ubusd_monitor_disconnect(struct ubus_client *cl)
+static struct ubus_monitor*
+ubusd_monitor_find(struct ubus_client *cl)
{
- struct ubus_monitor *m;
+ struct ubus_monitor *m, *tmp;
- list_for_each_entry(m, &monitors, list) {
+ list_for_each_entry_safe(m, tmp, &monitors, list) {
if (m->cl != cl)
continue;
- ubusd_monitor_free(m);
- return;
+ return m;
}
+
+ return NULL;
+}
+
+void
+ubusd_monitor_disconnect(struct ubus_client *cl)
+{
+ struct ubus_monitor *m;
+
+ m = ubusd_monitor_find(cl);
+ if (!m)
+ return;
+
+ ubusd_monitor_free(m);
}
void
blob_put_int8(&mb, UBUS_MONITOR_SEND, send);
blob_put(&mb, UBUS_MONITOR_DATA, blob_data(ub->data), blob_len(ub->data));
+ ub = ubus_msg_new(mb.head, blob_raw_len(mb.head), true);
+ ub->hdr.type = UBUS_MSG_MONITOR;
+
list_for_each_entry(m, &monitors, list) {
- ub = ubus_msg_new(mb.head, blob_raw_len(mb.head), true);
- ub->hdr.type = UBUS_MSG_MONITOR;
ub->hdr.seq = ++m->seq;
- ubus_msg_send(m->cl, ub, true);
+ ubus_msg_send(m->cl, ub);
}
+
+ ubus_msg_free(ub);
}
static int
return UBUS_STATUS_PERMISSION_DENIED;
if (!strcmp(method, "add")) {
- ubusd_monitor_connect(cl, ub);
- return 0;
+ if (!ubusd_monitor_connect(cl, ub))
+ return UBUS_STATUS_UNKNOWN_ERROR;
+
+ return UBUS_STATUS_OK;
}
if (!strcmp(method, "remove")) {
ubusd_monitor_disconnect(cl);
- return 0;
+ return UBUS_STATUS_OK;
}
return UBUS_STATUS_METHOD_NOT_FOUND;