rpcd-mod-lxc: add "info" ubus method (similar to the lxc-info)
authorRafał Miłecki <rafal@milecki.pl>
Tue, 7 Nov 2017 11:02:53 +0000 (12:02 +0100)
committerRafał Miłecki <rafal@milecki.pl>
Tue, 7 Nov 2017 22:34:59 +0000 (23:34 +0100)
This method allows getting basic info about a queried container. It's
based on the lxc-info command-line tool.

Example output:
> ubus call lxc info '{ "name": "foo" }'
{
        "name": "foo",
        "state": "RUNNING",
        "pid": 2946,
        "ips": [
                "192.168.0.124"
        ]
}

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
Approved-by: Luka Perkov <luka@openwrt.org>
utils/rpcd-mod-lxc/files/lxc.c

index 0d52f167749f70588a108cab448a7f9d494a038e..6db1b311752cc39f27da592f4bd21e85078df735 100644 (file)
@@ -316,6 +316,48 @@ out:
        return rc;
 }
 
+static int
+rpc_lxc_info(struct ubus_context *ctx, struct ubus_object *obj,
+            struct ubus_request_data *req, const char *method,
+            struct blob_attr *msg)
+{
+       struct blob_attr *tb[__RPC_LXC_RENAME_MAX];
+       struct rpc_lxc *l = NULL;
+       char **addresses;
+       void *k;
+       pid_t initpid;
+
+       blobmsg_parse(rpc_lxc_min_policy, __RPC_LXC_MAX, tb, blob_data(msg), blob_len(msg));
+
+       l = rpc_lxc_init(tb);
+       if (!l)
+               return UBUS_STATUS_INVALID_ARGUMENT;
+       blob_buf_init(&buf, 0);
+
+       blobmsg_add_string(&buf, "name", l->container->name);
+
+       blobmsg_add_string(&buf, "state", l->container->state(l->container));
+
+       initpid = l->container->init_pid(l->container);
+       if (initpid >= 0)
+               blobmsg_add_u32(&buf, "pid", initpid);
+
+       k = blobmsg_open_array(&buf, "ips");
+       addresses = l->container->get_ips(l->container, NULL, NULL, 0);
+       if (addresses) {
+               int i;
+
+               for (i = 0; addresses[i]; i++)
+                       blobmsg_add_string(&buf, "ip", addresses[i]);
+       }
+       blobmsg_close_array(&buf, k);
+
+       ubus_send_reply(ctx, req, buf.head);
+       rpc_lxc_done(l);
+
+       return UBUS_STATUS_OK;
+}
+
 static int
 rpc_lxc_rename(struct ubus_context *ctx, struct ubus_object *obj,
                struct ubus_request_data *req, const char *method,
@@ -480,6 +522,7 @@ rpc_lxc_api_init(const struct rpc_daemon_ops *o, struct ubus_context *ctx)
                UBUS_METHOD("stop", rpc_lxc_stop, rpc_lxc_min_policy),
                UBUS_METHOD("freeze", rpc_lxc_freeze, rpc_lxc_min_policy),
                UBUS_METHOD("unfreeze", rpc_lxc_unfreeze, rpc_lxc_min_policy),
+               UBUS_METHOD("info", rpc_lxc_info, rpc_lxc_min_policy),
                UBUS_METHOD("rename", rpc_lxc_rename, rpc_lxc_rename_policy),
                UBUS_METHOD("create", rpc_lxc_create, rpc_lxc_create_policy),
                UBUS_METHOD("destroy", rpc_lxc_destroy, rpc_lxc_min_policy),