--- /dev/null
+/*
+ * faultd
+ *
+ * Copyright © 2017 Samsung Electronics
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <stdio.h>
+
+#include "action.h"
+#include "action_executor.h"
+#include "decision_made_event.h"
+#include "log.h"
+#include "systemd_dbus.h"
+
+#define REBOOT_PARAM_PATH "/run/systemd/reboot-param"
+#define REBOOT_PARAM "recovery\n"
+
+static int reboot_system_to_recovery(struct faultd_action *action)
+{
+ struct faultd_event *ev = nqueue_pop(&action->execution_queue,
+ struct faultd_event, nq_node);
+ mode_t prev_umask;
+ FILE *rp_file;
+ size_t n_wrte;
+ int ret;
+
+ prev_umask = umask(0022);
+
+ rp_file = fopen(REBOOT_PARAM_PATH, "w");
+ if (!rp_file) {
+ log_error("Unable to open param file");
+ goto reboot;
+ }
+
+ n_wrte = fwrite(REBOOT_PARAM, sizeof(REBOOT_PARAM), 1, rp_file);
+ fclose(rp_file);
+ umask(prev_umask);
+
+ if (n_wrte < 1)
+ log_error("Unable to set reboot param");
+
+reboot:
+ /*
+ * We reboot the system without using logind because this action may be
+ * executed when for example dbus daemon is dead
+ */
+ ret = faultd_dbus_call_systemd_simple(SYSTEMD_OBJ,
+ SYSTEMD_MANAGER_INTERFACE,
+ "StartUnit",
+ "ss",
+ "reboot.target",
+ "replace-irreversibly");
+ if (ret < 0)
+ log_error_errno(ret, "Failed to reboot the system to the recovery");
+
+ faultd_event_unref(ev);
+ return 0;
+}
+
+static struct faultd_action system_reboot_to_recovery_action = {
+ .action_id = FAULTD_ACTION_RECOVERY_REBOOT_ID,
+ .impl_name = FAULTD_DEFAULT_ACTION_IMPL,
+ .execute = reboot_system_to_recovery,
+ .node = LIST_HEAD_INIT(system_reboot_to_recovery_action.node),
+};
+
+FAULTD_ACTION_REGISTER_SIMPLE(system_reboot_to_recovery_action);