paths: substitute literal with macro
[project/rpcd.git] / uci.c
diff --git a/uci.c b/uci.c
index 3bbb9c8878e74ac1bb86bcb1e2251d34f2c0f76a..421a4c726310e8c616d7233d35426b28cda6fb7b 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;
@@ -1150,7 +1151,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 +1240,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);
        }
@@ -1392,6 +1393,10 @@ rpc_uci_apply(struct ubus_context *ctx, struct ubus_object *obj,
                        return ret;
                }
 
+               /* copy SID early because rpc_uci_apply_config() will clobber buf */
+               if (rollback)
+                       strncpy(apply_sid, sid, RPC_SID_LEN);
+
                for (i = 0; i < gl.gl_pathc; i++) {
                        char *config = basename(gl.gl_pathv[i]);
                        struct stat s;
@@ -1410,7 +1415,6 @@ rpc_uci_apply(struct ubus_context *ctx, struct ubus_object *obj,
                globfree(&gl);
 
                if (rollback) {
-                       strncpy(apply_sid, sid, RPC_SID_LEN);
                        apply_timer.cb = rpc_uci_apply_timeout;
                        uloop_timeout_set(&apply_timer, timeout * 1000);
                        apply_ctx = ctx;
@@ -1487,6 +1491,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.
@@ -1535,6 +1554,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 =