session: support reclaiming pending apply session
[project/rpcd.git] / uci.c
diff --git a/uci.c b/uci.c
index acbbfd808c6139f5e8b7a8768c53d6476dd8da27..91d4ba23b78f1dc38343683975dd4db1164648cf 100644 (file)
--- a/uci.c
+++ b/uci.c
 #include <libubox/blobmsg_json.h>
 
 #include <rpcd/uci.h>
+#include <rpcd/exec.h>
 #include <rpcd/session.h>
 
 static struct blob_buf buf;
 static struct uci_context *cursor;
 static struct uloop_timeout apply_timer;
 static struct ubus_context *apply_ctx;
-static char apply_sid[RPC_SID_LEN + 1];
+
+char apply_sid[RPC_SID_LEN + 1];
 
 enum {
        RPC_G_CONFIG,
@@ -209,8 +211,15 @@ rpc_uci_status(void)
 static void
 rpc_uci_set_savedir(struct blob_attr *sid)
 {
+       struct uci_element *e, *tmp;
        char path[PATH_MAX];
 
+       uci_foreach_element_safe(&cursor->delta_path, tmp, e)
+               free(e);
+
+       cursor->delta_path.prev = &cursor->delta_path;
+       cursor->delta_path.next = &cursor->delta_path;
+
        if (!sid)
        {
                uci_set_savedir(cursor, "/tmp/.uci");
@@ -271,8 +280,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 +1158,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();
@@ -1487,6 +1498,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 +1561,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 =