luci2: add rclocal_get and rclocal_set calls
authorJo-Philipp Wich <jow@openwrt.org>
Thu, 11 Jul 2013 10:56:24 +0000 (12:56 +0200)
committerJo-Philipp Wich <jow@openwrt.org>
Mon, 15 Jul 2013 15:04:29 +0000 (17:04 +0200)
luci2.c

diff --git a/luci2.c b/luci2.c
index d6f2e5f1cd9f6b9f613effeae5fc009b99da5fc0..a96bdd64886f6a8d3bbae3c6891d9048fbc5823e 100644 (file)
--- a/luci2.c
+++ b/luci2.c
@@ -58,6 +58,15 @@ static const struct blobmsg_policy rpc_init_policy[__RPC_I_MAX] = {
        [RPC_I_ACTION] = { .name = "action", .type = BLOBMSG_TYPE_STRING },
 };
 
+enum {
+       RPC_RC_DATA,
+       __RPC_RC_MAX
+};
+
+static const struct blobmsg_policy rpc_rclocal_policy[__RPC_RC_MAX] = {
+       [RPC_RC_DATA]   = { .name = "data",  .type = BLOBMSG_TYPE_STRING },
+};
+
 enum {
        RPC_K_KEYS,
        __RPC_K_MAX
@@ -528,6 +537,51 @@ rpc_luci2_init_action(struct ubus_context *ctx, struct ubus_object *obj,
        }
 }
 
+static int
+rpc_luci2_rclocal_get(struct ubus_context *ctx, struct ubus_object *obj,
+                      struct ubus_request_data *req, const char *method,
+                      struct blob_attr *msg)
+{
+       FILE *f;
+       char data[4096] = { 0 };
+
+       if (!(f = fopen("/etc/rc.local", "r")))
+               return rpc_errno_status();
+
+       fread(data, sizeof(data) - 1, 1, f);
+       fclose(f);
+
+       blob_buf_init(&buf, 0);
+       blobmsg_add_string(&buf, "data", data);
+
+       ubus_send_reply(ctx, req, buf.head);
+       return 0;
+}
+
+static int
+rpc_luci2_rclocal_set(struct ubus_context *ctx, struct ubus_object *obj,
+                      struct ubus_request_data *req, const char *method,
+                      struct blob_attr *msg)
+{
+       FILE *f;
+       struct blob_attr *tb[__RPC_RC_MAX];
+
+       blobmsg_parse(rpc_rclocal_policy, __RPC_RC_MAX, tb,
+                     blob_data(msg), blob_len(msg));
+
+       if (!tb[RPC_RC_DATA] || blobmsg_data_len(tb[RPC_RC_DATA]) >= 4096)
+               return UBUS_STATUS_INVALID_ARGUMENT;
+
+       if (!(f = fopen("/etc/rc.local", "w")))
+               return rpc_errno_status();
+
+       fwrite(blobmsg_data(tb[RPC_RC_DATA]),
+              blobmsg_data_len(tb[RPC_RC_DATA]) - 1, 1, f);
+
+       fclose(f);
+       return 0;
+}
+
 static int
 rpc_luci2_sshkeys_get(struct ubus_context *ctx, struct ubus_object *obj,
                       struct ubus_request_data *req, const char *method,
@@ -1449,6 +1503,9 @@ int rpc_luci2_api_init(struct ubus_context *ctx)
                UBUS_METHOD_NOARG("init_list",    rpc_luci2_init_list),
                UBUS_METHOD("init_action",        rpc_luci2_init_action,
                                                  rpc_init_policy),
+               UBUS_METHOD_NOARG("rclocal_get",  rpc_luci2_rclocal_get),
+               UBUS_METHOD("rclocal_set",        rpc_luci2_rclocal_set,
+                                                 rpc_rclocal_policy),
                UBUS_METHOD_NOARG("sshkeys_get",  rpc_luci2_sshkeys_get),
                UBUS_METHOD("sshkeys_set",        rpc_luci2_sshkeys_set,
                                                  rpc_sshkey_policy),