[OpenWrt-Devel,packages] rpcd-mod-lxc: add "info" ubus method (similar to the lxc-info)

Message ID 20171107110253.31718-1-zajec5@gmail.com
State New
Headers show
Series
  • [OpenWrt-Devel,packages] rpcd-mod-lxc: add "info" ubus method (similar to the lxc-info)
Related show

Commit Message

Rafał Miłecki Nov. 7, 2017, 11:02 a.m.
From: Rafał Miłecki <rafal@milecki.pl>

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>
---
 utils/rpcd-mod-lxc/files/lxc.c | 43 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 43 insertions(+)

Comments

Luka Perkov Nov. 7, 2017, 3:51 p.m. | #1
> On Nov 7, 2017, at 12:02 PM, Rafał Miłecki <zajec5@gmail.com> wrote:
> 
> From: Rafał Miłecki <rafal@milecki.pl>
> 
> 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 | 43 ++++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 43 insertions(+)
> 
> diff --git a/utils/rpcd-mod-lxc/files/lxc.c b/utils/rpcd-mod-lxc/files/lxc.c
> index 0d52f167..6db1b311 100644
> --- a/utils/rpcd-mod-lxc/files/lxc.c
> +++ b/utils/rpcd-mod-lxc/files/lxc.c
> @@ -317,6 +317,48 @@ out:
> }
> 
> 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,
>        struct blob_attr *msg)
> @@ -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),
> -- 
> 2.11.0

Patch

diff --git a/utils/rpcd-mod-lxc/files/lxc.c b/utils/rpcd-mod-lxc/files/lxc.c
index 0d52f167..6db1b311 100644
--- a/utils/rpcd-mod-lxc/files/lxc.c
+++ b/utils/rpcd-mod-lxc/files/lxc.c
@@ -317,6 +317,48 @@  out:
 }
 
 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,
 		struct blob_attr *msg)
@@ -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),