action: Add unit_start action
authorPaweł Szewczyk <p.szewczyk@samsung.com>
Tue, 29 Jan 2019 12:44:30 +0000 (13:44 +0100)
committerPaweł Szewczyk <p.szewczyk@samsung.com>
Tue, 29 Jan 2019 13:10:03 +0000 (14:10 +0100)
Change-Id: I86ef58114a55e876ebce14ec2c9e8f44ae428677
Signed-off-by: Paweł Szewczyk <p.szewczyk@samsung.com>
Makefile.am
packaging/faultd.spec
src/action/unit_start.c [new file with mode: 0644]
src/core/action.c
src/core/action.h

index 3530c111bf4ea47e824de28f0a92690ffc38fa44..95a249ef407f1526294dec5f5a42e05cd2818dd7 100644 (file)
@@ -130,7 +130,8 @@ EXTRA_faultd_SOURCES = \
     src/action/service_restart.c \
     src/action/system_reboot.c \
     src/action/system_reboot_to_recovery.c \
-    src/action/service_recover.c
+    src/action/service_recover.c \
+    src/action/unit_start.c
 
 faultd_CFLAGS = $(AM_CFLAGS)
 faultd_LDFLAGS = -ldl
@@ -147,7 +148,8 @@ modules_LTLIBRARIES = audit_listener.la \
                  service_restart_action.la \
                  system_reboot_action.la \
                  system_reboot_to_recovery_action.la \
-                 service_recover_action.la
+                 service_recover_action.la \
+                 unit_start_action.la
 
 audit_listener_la_SOURCES = src/listeners/audit.c
 audit_listener_la_LIBADD = $(AUDIT_LIBS)
@@ -167,6 +169,7 @@ service_restart_action_la_SOURCES = src/action/service_restart.c
 system_reboot_action_la_SOURCES = src/action/system_reboot.c
 system_reboot_to_recovery_action_la_SOURCES = src/action/system_reboot_to_recovery.c
 service_recover_action_la_SOURCES = src/action/service_recover.c
+unit_start_action_la_SOURCES = src/action/unit_start.c
 
 if USE_EJDB
 EXTRA_faultd_SOURCES += src/database/ejdb.c
index 60de1149788a3b0e09b5594ed047f07f5cfbce88..d1d78cdcfbfff02b4461600bb9f45e995a6c7af2 100644 (file)
@@ -85,7 +85,8 @@ for mod in audit_listener \
        standard_fault_eh \
        standard_reboot_eh \
        startup_listener \
-       system_reboot_to_recovery_action
+       system_reboot_to_recovery_action \
+       unit_start_action
 do
        ln -s %{moduledir}/${mod}.so %{buildroot}/%{moduleconfdir}/${mod}.so;
        ln -s ../available-modules/${mod}.so %{buildroot}/%{enabled_moduledir}/${mod}.so;
diff --git a/src/action/unit_start.c b/src/action/unit_start.c
new file mode 100644 (file)
index 0000000..128e8f3
--- /dev/null
@@ -0,0 +1,69 @@
+/*
+ * This file is part of faultd.
+ *
+ * Copyright © 2019 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 "action.h"
+#include "action_executor.h"
+#include "decision_made_event.h"
+#include "log.h"
+#include "systemd_dbus.h"
+#include "common.h"
+
+static int start_unit(struct faultd_action *action,
+               struct action_executed_event *exe_info)
+{
+       struct faultd_event *ev = pop_faultd_event(&action->execution_queue);
+       struct decision_made_event *dm_ev = to_decision_made_event(ev);
+       char *unit_path = NULL;
+       int ret;
+
+       /*
+        * We are passing an event to exe_info, so there is no need to unref it
+        * in this function
+        */
+       exe_info->reason = ev;
+
+       ret = faultd_object_get_string(dm_ev->action_data,
+                       FAULTD_AD_UNIT_NAME, &unit_path);
+       if (!unit_path) {
+               faultd_object_append_string(exe_info->action_log, "error",
+                               "Unit path not specified");
+               exe_info->result = -EINVAL;
+               return 0;
+       }
+
+       ret = faultd_dbus_call_systemd_simple(unit_path,
+                       SYSTEMD_UNIT_INTERFACE,
+                       "Start", NULL);
+       if (ret < 0)
+               faultd_object_append_string(exe_info->action_log, "error",
+                               "Failed to call systemd");
+       else
+               log_kmsg("Starting unit: %s", unit_path);
+
+       exe_info->result = ret;
+       return 0;
+}
+
+static struct faultd_action unit_start_action = {
+       .action_id = FAULTD_ACTION_UNIT_START_ID,
+       .impl_name = FAULTD_DEFAULT_ACTION_IMPL,
+       .execute = start_unit,
+       .node = LIST_HEAD_INIT(unit_start_action.node),
+};
+
+FAULTD_ACTION_REGISTER_SIMPLE(unit_start_action);
index 517d3d64082a6cadd38f1bf3f86c81f2ed7cd53e..92c85e707c43a09960d8f9c78108b53dc2edae9f 100644 (file)
@@ -58,3 +58,17 @@ int faultd_fill_for_srv_recover(struct faultd_object *obj, char *service_path, c
 
        return 0;
 }
+
+int faultd_fill_for_unit_start(struct faultd_object *obj, char *unit_path)
+{
+       int ret;
+
+       ret = faultd_object_append_string(obj, FAULTD_AD_UNIT_NAME, unit_path);
+       if (ret < 0) {
+               log_error("Unable to append %s:%s",
+                                 FAULTD_AD_UNIT_NAME, unit_path);
+               return -ENOMEM;
+       }
+
+       return 0;
+}
index 13785fcfb29b929a1c3d0dad22be923894e16ad9..d13fcb6514e4de56e18b13a3e5efa9172abdfc7a 100644 (file)
 #define FAULTD_ACTION_SERVICE_RECOVER_ID "org.tizen.faultd.action.SERVICE_RECOVER"
 #define FAULTD_ACTION_REBOOT_ID "org.tizen.faultd.action.REBOOT"
 #define FAULTD_ACTION_RECOVERY_REBOOT_ID "org.tizen.faultd.action.RECOVERY_REBOOT"
+#define FAULTD_ACTION_UNIT_START_ID "org.tizen.faultd.action.UNIT_START"
 
 #define FAULTD_DEFAULT_ACTION_IMPL "default"
 
 #define FAULTD_AD_SERVICE_NAME "ServiceName"
 #define FAULTD_AD_CLEANUP_UNIT "RecoveryUnit"
+#define FAULTD_AD_UNIT_NAME "UnitName"
 
 int faultd_fill_for_srv_restart(struct faultd_object *obj, char *service_path);
 
 int faultd_fill_for_srv_recover(struct faultd_object *obj, char *service_path, char *recovery);
 
+int faultd_fill_for_unit_start(struct faultd_object *obj, char *unit_path);
+
 #define faultd_fill_for_reboot(o)                                      \
        faultd_object_fill_empty(o)