proto-shell: reset l3 device if it was set before
[project/netifd.git] / ubus.c
diff --git a/ubus.c b/ubus.c
index 8c237c3376f4643a698c6747689ad852b30de093..9b0f5158e89b273255936d1e4b78144b025a64a2 100644 (file)
--- a/ubus.c
+++ b/ubus.c
@@ -1,4 +1,7 @@
+#define _GNU_SOURCE
+
 #include <string.h>
+#include <stdio.h>
 
 #include "netifd.h"
 #include "interface.h"
@@ -8,6 +11,7 @@
 
 static struct ubus_context *ctx = NULL;
 static struct blob_buf b;
+static struct netifd_fd ubus_fd;
 
 /* global object */
 
@@ -102,6 +106,8 @@ netifd_ubus_init(const char *path)
        DPRINTF("connected as %08x\n", ctx->local_id);
        uloop_init();
        ubus_add_uloop(ctx);
+       ubus_fd.fd = ctx->sock.fd;
+       netifd_fd_add(&ubus_fd);
 
        ret = ubus_add_object(ctx, &main_object);
        if (ret)
@@ -193,24 +199,11 @@ netifd_handle_status(struct ubus_context *ctx, struct ubus_object *obj,
        if (iface->state == IFS_UP) {
                time_t cur = system_get_rtime();
                blobmsg_add_u32(&b, "uptime", cur - iface->start_time);
+               blobmsg_add_string(&b, "l3_device", iface->l3_dev->dev->ifname);
        }
 
-       if (iface->main_dev.dev) {
-               struct device *dev = iface->main_dev.dev;
-               const char *field;
-               void *devinfo;
-
-               /* use a different field for virtual devices */
-               if (dev->avl.key)
-                       field = "device";
-               else
-                       field = "link";
-
-               devinfo = blobmsg_open_table(&b, field);
-               blobmsg_add_string(&b, "name", dev->ifname);
-
-               blobmsg_close_table(&b, devinfo);
-       }
+       if (!(iface->proto_handler->flags & PROTO_FLAG_NODEV))
+               blobmsg_add_string(&b, "device", iface->main_dev.dev->ifname);
 
        if (!list_is_empty(&iface->errors))
                netifd_add_interface_errors(&b, iface);
@@ -336,13 +329,12 @@ void
 netifd_ubus_add_interface(struct interface *iface)
 {
        struct ubus_object *obj = &iface->ubus;
-       char *name;
+       char *name = NULL;
 
-       name = malloc(strlen(main_object.name) + strlen(iface->name) + 2);
+       asprintf(&name, "%s.interface.%s", main_object.name, iface->name);
        if (!name)
                return;
 
-       sprintf(name, "%s.%s", main_object.name, iface->name);
        obj->name = name;
        obj->type = &iface_object_type;
        obj->methods = iface_object_methods;