123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504 |
- /*
- * rpcd-lxc-plugin
- *
- * Copyright (C) 2014 Cisco Systems, Inc.
- * Author: Luka Perkov <luka@openwrt.org>
- *
- * Permission to use, copy, modify, and/or distribute this software for any
- * purpose with or without fee is hereby granted, provided that the above
- * copyright notice and this permission notice appear in all copies.
- *
- * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
- * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
- * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
- * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
- * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
- * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
- * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
- */
- #include <libubus.h>
- #include <lxc/lxccontainer.h>
- #include <rpcd/plugin.h>
- static struct blob_buf buf;
- struct rpc_lxc {
- /* ubus options */
- char *name;
- char *config;
- /* lxc container */
- struct lxc_container *container;
- };
- enum {
- RPC_LXC_NAME,
- RPC_LXC_CONFIG,
- __RPC_LXC_MAX,
- };
- enum {
- RPC_LXC_SHUTDOWN_NAME,
- RPC_LXC_SHUTDOWN_CONFIG,
- RPC_LXC_SHUTDOWN_TIMEOUT,
- __RPC_LXC_SHUTDOWN_MAX,
- };
- enum {
- RPC_LXC_RENAME_NAME,
- RPC_LXC_RENAME_CONFIG,
- RPC_LXC_RENAME_NEWNAME,
- __RPC_LXC_RENAME_MAX,
- };
- enum {
- RPC_LXC_CREATE_NAME,
- RPC_LXC_CREATE_CONFIG,
- RPC_LXC_CREATE_TEMPLATE,
- RPC_LXC_CREATE_FLAGS,
- RPC_LXC_CREATE_ARGS,
- __RPC_LXC_CREATE_MAX,
- };
- static const struct blobmsg_policy rpc_lxc_min_policy[__RPC_LXC_MAX] = {
- [RPC_LXC_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
- [RPC_LXC_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
- };
- static const struct blobmsg_policy rpc_lxc_shutdown_policy[__RPC_LXC_SHUTDOWN_MAX] = {
- [RPC_LXC_SHUTDOWN_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
- [RPC_LXC_SHUTDOWN_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
- [RPC_LXC_SHUTDOWN_TIMEOUT] = { .name = "timeout", .type = BLOBMSG_TYPE_INT32 },
- };
- static const struct blobmsg_policy rpc_lxc_rename_policy[__RPC_LXC_RENAME_MAX] = {
- [RPC_LXC_RENAME_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
- [RPC_LXC_RENAME_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
- [RPC_LXC_RENAME_NEWNAME] = { .name = "newname", .type = BLOBMSG_TYPE_STRING },
- };
- static const struct blobmsg_policy rpc_lxc_create_policy[__RPC_LXC_CREATE_MAX] = {
- [RPC_LXC_CREATE_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
- [RPC_LXC_CREATE_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
- [RPC_LXC_CREATE_TEMPLATE] = { .name = "template", .type = BLOBMSG_TYPE_STRING },
- [RPC_LXC_CREATE_FLAGS] = { .name = "flags", .type = BLOBMSG_TYPE_INT32 },
- [RPC_LXC_CREATE_ARGS] = { .name = "args", .type = BLOBMSG_TYPE_ARRAY },
- };
- static struct rpc_lxc *
- rpc_lxc_init(struct blob_attr *tb[__RPC_LXC_MAX])
- {
- struct rpc_lxc *l = NULL;
- l = calloc(1, sizeof(struct rpc_lxc));
- if (!l) return NULL;
- if (tb[RPC_LXC_NAME]) {
- l->name = blobmsg_data(tb[RPC_LXC_NAME]);
- } else {
- goto error;
- }
- if (tb[RPC_LXC_CONFIG]) {
- l->config = blobmsg_data(tb[RPC_LXC_CONFIG]);
- } else {
- l->config = NULL;
- }
- l->container = lxc_container_new(l->name, l->config);
- if (!l->container) {
- goto error;
- }
- return l;
- error:
- free(l);
- return NULL;
- }
- static void
- rpc_lxc_done(struct rpc_lxc *l)
- {
- if (l) {
- lxc_container_put(l->container);
- free(l);
- }
- return;
- }
- static int
- rpc_lxc_start(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_MAX];
- struct rpc_lxc *l = NULL;
- int rc;
- 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;
- if (l->container->is_running(l->container)) {
- rc = UBUS_STATUS_UNKNOWN_ERROR;
- goto out;
- }
- if (!l->container->start(l->container, 0, NULL)) {
- rc = UBUS_STATUS_INVALID_ARGUMENT;
- goto out;
- }
- rc = UBUS_STATUS_OK;
- out:
- rpc_lxc_done(l);
- return rc;
- }
- static int
- rpc_lxc_reboot(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_MAX];
- struct rpc_lxc *l = NULL;
- int rc;
- 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;
- if (!l->container->is_running(l->container)) {
- rc = UBUS_STATUS_UNKNOWN_ERROR;
- goto out;
- }
- if (!l->container->reboot(l->container)) {
- rc = UBUS_STATUS_INVALID_ARGUMENT;
- goto out;
- }
- rc = UBUS_STATUS_OK;
- out:
- rpc_lxc_done(l);
- return rc;
- }
- static int
- rpc_lxc_shutdown(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_SHUTDOWN_MAX];
- struct rpc_lxc *l = NULL;
- int rc;
- blobmsg_parse(rpc_lxc_shutdown_policy, __RPC_LXC_SHUTDOWN_MAX, tb, blob_data(msg), blob_len(msg));
- l = rpc_lxc_init(tb);
- if (!l) return UBUS_STATUS_INVALID_ARGUMENT;
- if (!l->container->is_running(l->container)) {
- rc = UBUS_STATUS_UNKNOWN_ERROR;
- goto out;
- }
- /* define default timeout */
- int timeout = 30;
- if (tb[RPC_LXC_SHUTDOWN_TIMEOUT]) {
- timeout = blobmsg_get_u32(tb[RPC_LXC_SHUTDOWN_TIMEOUT]);
- }
- if (!l->container->shutdown(l->container, timeout)) {
- rc = UBUS_STATUS_UNKNOWN_ERROR;
- goto out;
- }
- rc = UBUS_STATUS_OK;
- out:
- rpc_lxc_done(l);
- return rc;
- }
- static int
- rpc_lxc_stop(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_MAX];
- struct rpc_lxc *l = NULL;
- int rc;
- 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;
- if (!l->container->is_running(l->container)) {
- rc = UBUS_STATUS_UNKNOWN_ERROR;
- goto out;
- }
- if (!l->container->stop(l->container)) {
- rc = UBUS_STATUS_INVALID_ARGUMENT;
- goto out;
- }
- rc = UBUS_STATUS_OK;
- out:
- rpc_lxc_done(l);
- return rc;
- }
- static int
- rpc_lxc_freeze(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_MAX];
- struct rpc_lxc *l = NULL;
- int rc;
- 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;
- if (!l->container->is_running(l->container)) {
- rc = UBUS_STATUS_UNKNOWN_ERROR;
- goto out;
- }
- if (!l->container->freeze(l->container)) {
- rc = UBUS_STATUS_INVALID_ARGUMENT;
- goto out;
- }
- rc = UBUS_STATUS_OK;
- out:
- rpc_lxc_done(l);
- return rc;
- }
- static int
- rpc_lxc_unfreeze(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_MAX];
- struct rpc_lxc *l = NULL;
- int rc;
- 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;
- if (!l->container->is_running(l->container)) {
- rc = UBUS_STATUS_UNKNOWN_ERROR;
- goto out;
- }
- if (!l->container->unfreeze(l->container)) {
- rc = UBUS_STATUS_INVALID_ARGUMENT;
- goto out;
- }
- rc = UBUS_STATUS_OK;
- out:
- rpc_lxc_done(l);
- return rc;
- }
- 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)
- {
- struct blob_attr *tb[__RPC_LXC_RENAME_MAX];
- struct rpc_lxc *l = NULL;
- int rc;
- blobmsg_parse(rpc_lxc_rename_policy, __RPC_LXC_RENAME_MAX, tb, blob_data(msg), blob_len(msg));
- l = rpc_lxc_init(tb);
- if (!l) return UBUS_STATUS_INVALID_ARGUMENT;
- if (!tb[RPC_LXC_RENAME_NEWNAME]) {
- rc = UBUS_STATUS_INVALID_ARGUMENT;
- goto out;
- }
- if (l->container->is_running(l->container)) {
- rc = UBUS_STATUS_UNKNOWN_ERROR;
- goto out;
- }
- char *newname = blobmsg_data(tb[RPC_LXC_RENAME_NEWNAME]);
- if (!newname || !l->container->rename(l->container, newname)) {
- rc = UBUS_STATUS_INVALID_ARGUMENT;
- goto out;
- }
- rc = UBUS_STATUS_OK;
- out:
- rpc_lxc_done(l);
- return rc;
- }
- static int
- rpc_lxc_create(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_CREATE_MAX];
- struct rpc_lxc *l = NULL;
- int rc;
- char *template = "none";
- int flags = 0;
- char **args = NULL;
- blobmsg_parse(rpc_lxc_create_policy, __RPC_LXC_CREATE_MAX, tb, blob_data(msg), blob_len(msg));
- l = rpc_lxc_init(tb);
- if (!l) return UBUS_STATUS_INVALID_ARGUMENT;
- if (tb[RPC_LXC_CREATE_TEMPLATE])
- template = blobmsg_data(tb[RPC_LXC_CREATE_TEMPLATE]);
- if (tb[RPC_LXC_CREATE_FLAGS])
- flags = blobmsg_get_u32(tb[RPC_LXC_CREATE_FLAGS]);
- if (tb[RPC_LXC_CREATE_ARGS]) {
- struct blob_attr *cur;
- int num, rem;
- num = blobmsg_check_array(tb[RPC_LXC_CREATE_ARGS], BLOBMSG_TYPE_STRING);
- // trailing NULL is needed
- args = calloc(num + 1, sizeof(char *));
- if (!args) {
- rc = UBUS_STATUS_UNKNOWN_ERROR;
- goto out;
- }
- num = 0;
- blobmsg_for_each_attr(cur, tb[RPC_LXC_CREATE_ARGS], rem)
- {
- if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
- continue;
- args[num++] = (char *) blobmsg_data(cur);
- }
- }
- if (!l->container->create(l->container, template, NULL, NULL, flags, args)) {
- rc = UBUS_STATUS_INVALID_ARGUMENT;
- goto out;
- }
- rc = UBUS_STATUS_OK;
- out:
- rpc_lxc_done(l);
- free(args);
- return rc;
- }
- static int
- rpc_lxc_destroy(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_MAX];
- struct rpc_lxc *l = NULL;
- int rc;
- 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;
- if (l->container->is_running(l->container)) {
- rc = UBUS_STATUS_UNKNOWN_ERROR;
- goto out;
- }
- if (!l->container->destroy(l->container)) {
- rc = UBUS_STATUS_INVALID_ARGUMENT;
- goto out;
- }
- rc = UBUS_STATUS_OK;
- out:
- rpc_lxc_done(l);
- return rc;
- }
- static int
- rpc_lxc_list(struct ubus_context *ctx, struct ubus_object *obj,
- struct ubus_request_data *req, const char *method,
- struct blob_attr *msg)
- {
- blob_buf_init(&buf, 0);
- int rc;
- char **names;
- struct lxc_container **cret;
- rc = list_all_containers(NULL, &names, &cret);
- if (rc == -1)
- return UBUS_STATUS_UNKNOWN_ERROR;
- for (int i = 0; i < rc; i++) {
- struct lxc_container *c = cret[i];
- blobmsg_add_string(&buf, names[i], c->state(c));
- free(names[i]);
- lxc_container_put(c);
- }
- ubus_send_reply(ctx, req, buf.head);
- return UBUS_STATUS_OK;
- }
- static int
- rpc_lxc_api_init(const struct rpc_daemon_ops *o, struct ubus_context *ctx)
- {
- static const struct ubus_method lxc_methods[] = {
- UBUS_METHOD("start", rpc_lxc_start, rpc_lxc_min_policy),
- UBUS_METHOD("reboot", rpc_lxc_reboot, rpc_lxc_min_policy),
- UBUS_METHOD("shutdown", rpc_lxc_shutdown, rpc_lxc_shutdown_policy),
- 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("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),
- UBUS_METHOD_NOARG("list", rpc_lxc_list),
- };
- static struct ubus_object_type lxc_type =
- UBUS_OBJECT_TYPE("luci-rpc-lxc", lxc_methods);
- static struct ubus_object obj = {
- .name = "lxc",
- .type = &lxc_type,
- .methods = lxc_methods,
- .n_methods = ARRAY_SIZE(lxc_methods),
- };
- return ubus_add_object(ctx, &obj);
- }
- struct rpc_plugin rpc_plugin = {
- .init = rpc_lxc_api_init
- };
|