#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 bool apply_running;
+
+char apply_sid[RPC_SID_LEN + 1];
enum {
RPC_G_CONFIG,
}
}
+/*
+ * Clear all save directories from the uci cursor and append the given path
+ * as new save directory.
+ */
+static void
+rpc_uci_replace_savedir(const char *path)
+{
+ struct uci_element *e, *tmp;
+
+ 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 (path)
+ uci_set_savedir(cursor, path);
+}
+
/*
* Setup per-session delta save directory. If the passed "sid" blob attribute
* pointer is NULL then the precedure was not invoked through the ubus-rpc so
if (!sid)
{
- uci_set_savedir(cursor, "/tmp/.uci");
+ rpc_uci_replace_savedir("/tmp/.uci");
return;
}
snprintf(path, sizeof(path) - 1,
RPC_UCI_SAVEDIR_PREFIX "%s", blobmsg_get_string(sid));
- uci_set_savedir(cursor, path);
+ rpc_uci_replace_savedir(path);
}
/*
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:
struct uci_package *p = NULL;
struct uci_ptr ptr = { 0 };
- if (apply_running)
+ if (apply_sid[0])
return UBUS_STATUS_PERMISSION_DENIED;
blobmsg_parse(rpc_uci_config_policy, __RPC_C_MAX, tb,
else
{
if (!uci_lookup_ptr(cursor, &ptr, NULL, true) && ptr.p)
+ {
uci_revert(cursor, &ptr);
+ uci_unload(cursor, ptr.p);
+ }
}
return rpc_uci_status();
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);
}
rpc_uci_purge_dir(RPC_SNAPSHOT_DELTA);
uloop_timeout_cancel(&apply_timer);
- apply_running = false;
+ memset(apply_sid, 0, sizeof(apply_sid));
apply_ctx = NULL;
}
if (tb[RPC_T_ROLLBACK])
rollback = blobmsg_get_bool(tb[RPC_T_ROLLBACK]);
- if (apply_running && rollback)
+ if (apply_sid[0] && rollback)
return UBUS_STATUS_PERMISSION_DENIED;
if (!tb[RPC_T_SESSION])
rpc_uci_purge_dir(RPC_SNAPSHOT_FILES);
rpc_uci_purge_dir(RPC_SNAPSHOT_DELTA);
- if (!apply_running) {
+ if (!apply_sid[0]) {
mkdir(RPC_SNAPSHOT_FILES, 0700);
mkdir(RPC_SNAPSHOT_DELTA, 0700);
snprintf(tmp, sizeof(tmp), RPC_UCI_SAVEDIR_PREFIX "%s/*", sid);
if (glob(tmp, GLOB_PERIOD, NULL, &gl) < 0)
- return -1;
+ return UBUS_STATUS_NOT_FOUND;
snprintf(tmp, sizeof(tmp), RPC_UCI_SAVEDIR_PREFIX "%s/", sid);
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;
globfree(&gl);
- apply_running = true;
- apply_timer.cb = rpc_uci_apply_timeout;
- uloop_timeout_set(&apply_timer, timeout * 1000);
- apply_ctx = ctx;
+ if (rollback) {
+ apply_timer.cb = rpc_uci_apply_timeout;
+ uloop_timeout_set(&apply_timer, timeout * 1000);
+ apply_ctx = ctx;
+ }
}
- if (apply_running && !rollback) {
- rpc_uci_purge_dir(RPC_SNAPSHOT_FILES);
- rpc_uci_purge_dir(RPC_SNAPSHOT_DELTA);
+ return 0;
+}
- uloop_timeout_cancel(&apply_timer);
- apply_running = false;
- apply_ctx = NULL;
- }
+static int
+rpc_uci_confirm(struct ubus_context *ctx, struct ubus_object *obj,
+ struct ubus_request_data *req, const char *method,
+ struct blob_attr *msg)
+{
+ struct blob_attr *tb[__RPC_B_MAX];
+ char *sid;
+
+ blobmsg_parse(rpc_uci_rollback_policy, __RPC_B_MAX, tb,
+ blob_data(msg), blob_len(msg));
+
+ if (!tb[RPC_B_SESSION])
+ return UBUS_STATUS_INVALID_ARGUMENT;
+
+ sid = blobmsg_data(tb[RPC_B_SESSION]);
+
+ if (!apply_sid[0])
+ return UBUS_STATUS_NO_DATA;
+
+ if (strcmp(apply_sid, sid))
+ return UBUS_STATUS_PERMISSION_DENIED;
+
+ rpc_uci_purge_dir(RPC_SNAPSHOT_FILES);
+ rpc_uci_purge_dir(RPC_SNAPSHOT_DELTA);
+
+ uloop_timeout_cancel(&apply_timer);
+ memset(apply_sid, 0, sizeof(apply_sid));
+ apply_ctx = NULL;
return 0;
}
char tmp[PATH_MAX];
glob_t gl;
char *sid;
- int ret;
blobmsg_parse(rpc_uci_rollback_policy, __RPC_B_MAX, tb,
blob_data(msg), blob_len(msg));
- if (!apply_running)
- return UBUS_STATUS_PERMISSION_DENIED;
+ if (!apply_sid[0])
+ return UBUS_STATUS_NO_DATA;
if (!tb[RPC_B_SESSION])
return UBUS_STATUS_INVALID_ARGUMENT;
sid = blobmsg_data(tb[RPC_B_SESSION]);
+ if (strcmp(apply_sid, sid))
+ return UBUS_STATUS_PERMISSION_DENIED;
+
snprintf(tmp, sizeof(tmp), "%s/*", RPC_SNAPSHOT_FILES);
if (glob(tmp, GLOB_PERIOD, NULL, &gl) < 0)
- return -1;
-
- ret = rpc_uci_apply_access(sid, &gl);
- if (ret) {
- globfree(&gl);
- return ret;
- }
+ return UBUS_STATUS_NOT_FOUND;
rpc_uci_do_rollback(ctx, sid, &gl);
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.
UBUS_METHOD("revert", rpc_uci_revert, rpc_uci_config_policy),
UBUS_METHOD("commit", rpc_uci_commit, rpc_uci_config_policy),
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 =