cmake: Find libubox/blobmsg_json.h
[project/rpcd.git] / uci.c
diff --git a/uci.c b/uci.c
index ef255a8e3a03e880f1afdf1a9cd4c7732aada93e..72ae5a67f99ae8f00784be8e2aa94eff5d3116e4 100644 (file)
--- a/uci.c
+++ b/uci.c
@@ -23,6 +23,7 @@
 #include <libubox/blobmsg_json.h>
 
 #include <rpcd/uci.h>
+#include <rpcd/exec.h>
 #include <rpcd/session.h>
 
 static struct blob_buf buf;
@@ -271,8 +272,7 @@ rpc_uci_format_blob(struct blob_attr *v, const char **p)
        switch (blobmsg_type(v))
        {
        case BLOBMSG_TYPE_STRING:
-               if (blobmsg_data_len(v) > 1)
-                       *p = blobmsg_data(v);
+               *p = blobmsg_data(v);
                break;
 
        case BLOBMSG_TYPE_INT64:
@@ -1150,7 +1150,10 @@ rpc_uci_revert_commit(struct ubus_context *ctx, struct blob_attr *msg, bool comm
        else
        {
                if (!uci_lookup_ptr(cursor, &ptr, NULL, true) && ptr.p)
+               {
                        uci_revert(cursor, &ptr);
+                       uci_unload(cursor, ptr.p);
+               }
        }
 
        return rpc_uci_status();
@@ -1236,11 +1239,8 @@ static int
 rpc_uci_apply_config(struct ubus_context *ctx, char *config)
 {
        struct uci_package *p = NULL;
-       struct uci_ptr ptr = { 0 };
-
-       ptr.package = config;
 
-       if (!uci_load(cursor, ptr.package, &p)) {
+       if (!uci_load(cursor, config, &p)) {
                uci_commit(cursor, &p, false);
                uci_unload(cursor, p);
        }
@@ -1490,6 +1490,21 @@ rpc_uci_rollback(struct ubus_context *ctx, struct ubus_object *obj,
        return 0;
 }
 
+static int
+rpc_uci_reload(struct ubus_context *ctx, struct ubus_object *obj,
+                 struct ubus_request_data *req, const char *method,
+                 struct blob_attr *msg)
+{
+       char * const cmd[2] = { "/sbin/reload_config", NULL };
+
+       if (!fork()) {
+               /* wait for the RPC call to complete */
+               sleep(2);
+               return execv(cmd[0], cmd);
+       }
+
+       return 0;
+}
 
 /*
  * Session destroy callback to purge associated delta directory.
@@ -1538,6 +1553,7 @@ int rpc_uci_api_init(struct ubus_context *ctx)
                UBUS_METHOD("apply",    rpc_uci_apply,    rpc_uci_apply_policy),
                UBUS_METHOD("confirm",  rpc_uci_confirm,  rpc_uci_rollback_policy),
                UBUS_METHOD("rollback", rpc_uci_rollback, rpc_uci_rollback_policy),
+               UBUS_METHOD_NOARG("reload_config", rpc_uci_reload),
        };
 
        static struct ubus_object_type uci_type =