--- /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 <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;
+}
--- /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.
+ */
+
+#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 */