make uci commit calls trigger config.change events
[project/rpcd.git] / uci.c
diff --git a/uci.c b/uci.c
index 34c6830..f5bb076 100644 (file)
--- a/uci.c
+++ b/uci.c
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
-#include "uci.h"
-#include "session.h"
+#include <libubox/blobmsg.h>
+#include <libubox/blobmsg_json.h>
+
+#include <rpcd/uci.h>
+#include <rpcd/session.h>
 
 static struct blob_buf buf;
 static struct uci_context *cursor;
@@ -185,7 +188,7 @@ rpc_uci_set_savedir(struct blob_attr *sid)
        }
 
        snprintf(path, sizeof(path) - 1,
-                "/tmp/.uci-rpc-%s", (char *)blobmsg_data(sid));
+                RPC_UCI_SAVEDIR_PREFIX "%s", blobmsg_get_string(sid));
 
        uci_set_savedir(cursor, path);
 }
@@ -1030,8 +1033,28 @@ out:
        return rpc_uci_status();
 }
 
+static void
+rpc_uci_trigger_event(struct ubus_context *ctx, const char *config)
+{
+       char *pkg = strdup(config);
+       static struct blob_buf b;
+       uint32_t id;
+
+       if (!ubus_lookup_id(ctx, "service", &id)) {
+               void *c;
+
+               blob_buf_init(&b, 0);
+               blobmsg_add_string(&b, "type", "config.change");
+               c = blobmsg_open_table(&b, "data");
+               blobmsg_add_string(&b, "package", pkg);
+               blobmsg_close_table(&b, c);
+               ubus_invoke(ctx, id, "event", b.head, NULL, 0, 1000);
+       }
+       free(pkg);
+}
+
 static int
-rpc_uci_revert_commit(struct blob_attr *msg, bool commit)
+rpc_uci_revert_commit(struct ubus_context *ctx, struct blob_attr *msg, bool commit)
 {
        struct blob_attr *tb[__RPC_C_MAX];
        struct uci_package *p = NULL;
@@ -1057,6 +1080,7 @@ rpc_uci_revert_commit(struct blob_attr *msg, bool commit)
                        uci_commit(cursor, &p, false);
                        uci_unload(cursor, p);
                }
+               rpc_uci_trigger_event(ctx, blobmsg_get_string(tb[RPC_C_CONFIG]));
        }
        else
        {
@@ -1072,7 +1096,7 @@ rpc_uci_revert(struct ubus_context *ctx, struct ubus_object *obj,
                struct ubus_request_data *req, const char *method,
                struct blob_attr *msg)
 {
-       return rpc_uci_revert_commit(msg, false);
+       return rpc_uci_revert_commit(ctx, msg, false);
 }
 
 static int
@@ -1080,7 +1104,7 @@ rpc_uci_commit(struct ubus_context *ctx, struct ubus_object *obj,
                struct ubus_request_data *req, const char *method,
                struct blob_attr *msg)
 {
-       return rpc_uci_revert_commit(msg, true);
+       return rpc_uci_revert_commit(ctx, msg, true);
 }
 
 static int
@@ -1115,7 +1139,7 @@ out:
  * Remove given delta save directory (if any).
  */
 static void
-rpc_uci_purge_savedir(const char *path)
+rpc_uci_purge_dir(const char *path)
 {
        DIR *d;
        struct stat s;
@@ -1151,24 +1175,23 @@ rpc_uci_purge_savedir_cb(struct rpc_session *ses, void *priv)
 {
        char path[PATH_MAX];
 
-       snprintf(path, sizeof(path) - 1, "/tmp/.uci-rpc-%s", ses->id);
-       rpc_uci_purge_savedir(path);
+       snprintf(path, sizeof(path) - 1, RPC_UCI_SAVEDIR_PREFIX "%s", ses->id);
+       rpc_uci_purge_dir(path);
 }
 
 /*
- * Removes all delta directories which match the /tmp/.uci-rpc-* pattern.
+ * Removes all delta directories which match the RPC_UCI_SAVEDIR_PREFIX.
  * This is used to clean up garbage when starting rpcd.
  */
-static void
-rpc_uci_purge_savedirs(void)
+void rpc_uci_purge_savedirs(void)
 {
        int i;
        glob_t gl;
 
-       if (!glob("/tmp/.uci-rpc-*", 0, NULL, &gl))
+       if (!glob(RPC_UCI_SAVEDIR_PREFIX "*", 0, NULL, &gl))
        {
                for (i = 0; i < gl.gl_pathc; i++)
-                       rpc_uci_purge_savedir(gl.gl_pathv[i]);
+                       rpc_uci_purge_dir(gl.gl_pathv[i]);
 
                globfree(&gl);
        }
@@ -1208,7 +1231,6 @@ int rpc_uci_api_init(struct ubus_context *ctx)
        if (!cursor)
                return UBUS_STATUS_UNKNOWN_ERROR;
 
-       rpc_uci_purge_savedirs();
        rpc_session_destroy_cb(&cb);
 
        return ubus_add_object(ctx, &obj);