Add system service abstraction
authorKrzysztof Opasiak <k.opasiak@samsung.com>
Tue, 9 May 2017 11:29:39 +0000 (13:29 +0200)
committerKrzysztof Opasiak <k.opasiak@samsung.com>
Tue, 9 May 2017 11:49:16 +0000 (13:49 +0200)
Internally this entity talks with systemd via DBUS to
translate PID to systemd service name.

Signed-off-by: Krzysztof Opasiak <k.opasiak@samsung.com>
Makefile.am
src/core/service.c [new file with mode: 0644]
src/core/service.h [new file with mode: 0644]

index 34cdaa29b5e8f6411a8486609c65016536a72d49..e7e04c42652c429b01b78f6826a7295000a71599 100644 (file)
@@ -33,6 +33,7 @@ sbin_PROGRAMS = faultd
 faultd_SOURCES = \
     src/core/event.c \
     src/core/event_processor.c \
+    src/core/service.c \
     src/core/module.c \
     src/faultd.c \
     src/util/log.c \
diff --git a/src/core/service.c b/src/core/service.c
new file mode 100644 (file)
index 0000000..3fe8fd2
--- /dev/null
@@ -0,0 +1,99 @@
+/*
+ * 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 <assert.h>
+#include <errno.h>
+#include <malloc.h>
+#include <string.h>
+#include <systemd/sd-bus.h>
+
+#include "log.h"
+#include "service.h"
+#include "common.h"
+
+int systemd_service_init_by_pid(pid_t pid, struct systemd_service *s)
+{
+       _cleanup_free_ const char *n = NULL;
+       int rc;
+
+       assert(s);
+
+       rc = systemd_get_unit_by_pid(pid, &n);
+       if (rc < 0)
+               return rc;
+
+       return systemd_service_init(n, s);
+}
+
+int systemd_service_init(const char *name, struct systemd_service *s)
+{
+       assert(name);
+       assert(s);
+
+       s->name = strdup(name);
+
+       return 0;
+}
+
+void systemd_service_cleanup(struct systemd_service *s)
+{
+       if (s)
+               free(s->name);
+}
+
+int systemd_get_unit_by_pid(pid_t pid, const char **name)
+{
+       _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
+       _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
+       _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL;
+       const char* unit;
+       int rc;
+
+       assert(name);
+
+       rc = sd_bus_default_system(&bus);
+       if (rc < 0) {
+               log_error_errno(rc, "Failed to acquire the defult system bus connection: %m.");
+               return rc;
+       }
+
+       rc = sd_bus_call_method(bus,
+                                                       "org.freedesktop.systemd1",         /* service */
+                                                       "/org/freedesktop/systemd1",        /* object  */
+                                                       "org.freedesktop.systemd1.Manager", /* interface */
+                                                       "GetUnitByPID",                     /* method */
+                                                       &error,                             /* place to store error */
+                                                       &m,                                 /* place to store answer */
+                                                       "u",                                /* input signature */
+                                                       pid);                               /* input argument */
+       if (rc < 0) {
+               log_error("Failed to issue \"GetUnitByPID\" method call: %s.", error.message);
+               return rc;
+       }
+
+       rc = sd_bus_message_read(m, "o", &unit);
+       if (rc < 0) {
+               log_error_errno(rc, "Failed to parse response message: %m.");
+               return rc;
+       }
+
+       *name = strdup(unit);
+
+       return 0;
+}
diff --git a/src/core/service.h b/src/core/service.h
new file mode 100644 (file)
index 0000000..911291e
--- /dev/null
@@ -0,0 +1,34 @@
+/*
+ * 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.
+ */
+
+#ifndef FAULTD_SERVICE_H
+#define FAULTD_SERVICE_H
+
+#include <sys/types.h>
+#include <unistd.h>
+
+struct systemd_service {
+        char *name;
+};
+
+int systemd_service_init_by_pid(pid_t pid, struct systemd_service *s);
+int systemd_service_init(const char *name, struct systemd_service *s);
+void systemd_service_cleanup(struct systemd_service *s);
+int systemd_get_unit_by_pid(pid_t pid, const char **name);
+
+#endif /* FAULTD_SERVICE_H */