Fedora 30 packaging fix issu
[src/app-framework-binder.git] / src / afb-systemd.c
index 4faa9fc..5ba695b 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2015, 2016, 2017 "IoT.bzh"
+ * Copyright (C) 2015-2018 "IoT.bzh"
  * Author José Bollo <jose.bollo@iot.bzh>
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -22,6 +22,7 @@
 
 #include <systemd/sd-event.h>
 #include <systemd/sd-bus.h>
+#include <systemd/sd-daemon.h>
 
 #include "afb-systemd.h"
 #include "jobs.h"
@@ -62,3 +63,41 @@ struct sd_bus *afb_systemd_get_system_bus()
        return sdbusopen((void*)&result, (void*)sd_bus_open_system);
 }
 
+static char **fds_names()
+{
+       static char *null;
+       static char **names;
+
+       int rc;
+
+       if (!names) {
+               rc = sd_listen_fds_with_names(1, &names);
+               if (rc <= 0) {
+                       errno = -rc;
+                       names = &null;
+               }
+       }
+       return names;
+}
+
+int afb_systemd_fds_init()
+{
+       errno = 0;
+       fds_names();
+       return -!!errno;
+}
+
+int afb_systemd_fds_for(const char *name)
+{
+       int idx;
+       char **names;
+
+       names = fds_names();
+       for (idx = 0 ; names[idx] != NULL ; idx++)
+               if (!strcmp(name, names[idx]))
+                       return idx + SD_LISTEN_FDS_START;
+
+       errno = ENOENT;
+       return -1;
+}
+