afb-systemd: Isolate systemd main entries
authorJosé Bollo <jose.bollo@iot.bzh>
Wed, 21 Feb 2018 10:38:59 +0000 (11:38 +0100)
committerJosé Bollo <jose.bollo@iot.bzh>
Wed, 21 Feb 2018 10:38:59 +0000 (11:38 +0100)
This change prepares the possibilty to remove the dependency
to systemd/libsystemd.

Change-Id: Ife4181d6c7195230a11a32e527b8973af6a096e1
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
12 files changed:
src/CMakeLists.txt
src/afb-api-dbus.c
src/afb-api-ws.c
src/afb-common.c
src/afb-common.h
src/afb-export.c
src/afb-hsrv.c
src/afb-stub-ws.c
src/afb-systemd.c [new file with mode: 0644]
src/afb-systemd.h [new file with mode: 0644]
src/afb-ws-json1.c
src/afs-supervisor.c

index 89a181f..436eaf0 100644 (file)
@@ -53,6 +53,7 @@ ADD_LIBRARY(afb-lib STATIC
        afb-session.c
        afb-stub-ws.c
        afb-supervision.c
+       afb-systemd.c
        afb-trace.c
        afb-websock.c
        afb-ws-client.c
index ad640fb..eeadb9c 100644 (file)
@@ -29,7 +29,7 @@
 
 #include <afb/afb-event.h>
 
-#include "afb-common.h"
+#include "afb-systemd.h"
 
 #include "afb-session.h"
 #include "afb-msg-json.h"
@@ -128,7 +128,7 @@ static struct api_dbus *make_api_dbus_3(int system, const char *path, size_t pat
        }
 
        /* choose the bus */
-       sdbus = (system ? afb_common_get_system_bus : afb_common_get_user_bus)();
+       sdbus = (system ? afb_systemd_get_system_bus : afb_systemd_get_user_bus)();
        if (sdbus == NULL)
                goto error2;
 
index 0e4d1d0..26d0193 100644 (file)
@@ -33,7 +33,7 @@
 #include <systemd/sd-event.h>
 #include "afb-api.h"
 #include "afb-apiset.h"
-#include "afb-common.h"
+#include "afb-systemd.h"
 #include "afb-stub-ws.h"
 #include "verbose.h"
 #include "sd-fds.h"
@@ -310,7 +310,7 @@ static int api_ws_server_connect(struct api_ws *apiws)
                ERROR("can't create socket %s", apiws->path);
        else {
                /* listen for service */
-               rc = sd_event_add_io(afb_common_get_event_loop(),
+               rc = sd_event_add_io(afb_systemd_get_event_loop(),
                                &apiws->listensrc, apiws->fd, EPOLLIN,
                                api_ws_server_listen_callback, apiws);
                if (rc >= 0)
index c6c876b..88638d4 100644 (file)
 
 #define _GNU_SOURCE
 
+#include <unistd.h>
+#include <fcntl.h>
 #include <sys/types.h>
 #include <sys/stat.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include <errno.h>
-#include <systemd/sd-event.h>
-#include <systemd/sd-bus.h>
 
 #include "afb-common.h"
 #include "locale-root.h"
-#include "jobs.h"
 
 static const char *default_locale = NULL;
 static struct locale_root *rootdir = NULL;
 
-static struct sd_bus *sdbusopen(struct sd_bus **p, int (*f)(struct sd_bus **))
-{
-       if (*p == NULL) {
-               int rc = f(p);
-               if (rc < 0) {
-                       errno = -rc;
-                       *p = NULL;
-               } else {
-                       rc = sd_bus_attach_event(*p, afb_common_get_event_loop(), 0);
-                       if (rc < 0) {
-                               sd_bus_unref(*p);
-                               errno = -rc;
-                               *p = NULL;
-                       }
-               }
-       }
-       return *p;
-}
-
-struct sd_event *afb_common_get_event_loop()
-{
-       return jobs_get_sd_event();
-}
-
-struct sd_bus *afb_common_get_user_bus()
-{
-       static struct sd_bus *result = NULL;
-       return sdbusopen((void*)&result, (void*)sd_bus_open_user);
-}
-
-struct sd_bus *afb_common_get_system_bus()
-{
-       static struct sd_bus *result = NULL;
-       return sdbusopen((void*)&result, (void*)sd_bus_open_system);
-}
-
 void afb_common_default_locale_set(const char *locale)
 {
        default_locale = locale;
index 1361d5f..c5ceb2a 100644 (file)
 
 #pragma once
 
-struct sd_event;
-struct sd_bus;
-
-extern struct sd_event *afb_common_get_event_loop();
-extern struct sd_bus *afb_common_get_user_bus();
-extern struct sd_bus *afb_common_get_system_bus();
-
 extern void afb_common_default_locale_set(const char *locale);
 extern const char *afb_common_default_locale_get();
 
index 840bd91..b932183 100644 (file)
@@ -30,6 +30,7 @@
 #include "afb-apiset.h"
 #include "afb-api-dyn.h"
 #include "afb-common.h"
+#include "afb-systemd.h"
 #include "afb-cred.h"
 #include "afb-evt.h"
 #include "afb-export.h"
@@ -300,21 +301,21 @@ static int hooked_event_broadcast_cb(void *closure, const char *name, struct jso
 static struct sd_event *hooked_get_event_loop(void *closure)
 {
        struct afb_export *export = closure;
-       struct sd_event *r = afb_common_get_event_loop();
+       struct sd_event *r = afb_systemd_get_event_loop();
        return afb_hook_ditf_get_event_loop(export, r);
 }
 
 static struct sd_bus *hooked_get_user_bus(void *closure)
 {
        struct afb_export *export = closure;
-       struct sd_bus *r = afb_common_get_user_bus();
+       struct sd_bus *r = afb_systemd_get_user_bus();
        return afb_hook_ditf_get_user_bus(export, r);
 }
 
 static struct sd_bus *hooked_get_system_bus(void *closure)
 {
        struct afb_export *export = closure;
-       struct sd_bus *r = afb_common_get_system_bus();
+       struct sd_bus *r = afb_systemd_get_system_bus();
        return afb_hook_ditf_get_system_bus(export, r);
 }
 
@@ -382,9 +383,9 @@ static const struct afb_daemon_itf daemon_itf = {
        .vverbose_v2 = vverbose_cb,
        .event_make = event_make_cb,
        .event_broadcast = event_broadcast_cb,
-       .get_event_loop = afb_common_get_event_loop,
-       .get_user_bus = afb_common_get_user_bus,
-       .get_system_bus = afb_common_get_system_bus,
+       .get_event_loop = afb_systemd_get_event_loop,
+       .get_user_bus = afb_systemd_get_user_bus,
+       .get_system_bus = afb_systemd_get_system_bus,
        .rootdir_get_fd = afb_common_rootdir_get_fd,
        .rootdir_open_locale = rootdir_open_locale_cb,
        .queue_job = queue_job_cb,
@@ -940,9 +941,9 @@ static const struct afb_dynapi_itf dynapi_itf = {
 
        .vverbose = (void*)vverbose_cb,
 
-       .get_event_loop = afb_common_get_event_loop,
-       .get_user_bus = afb_common_get_user_bus,
-       .get_system_bus = afb_common_get_system_bus,
+       .get_event_loop = afb_systemd_get_event_loop,
+       .get_user_bus = afb_systemd_get_user_bus,
+       .get_system_bus = afb_systemd_get_system_bus,
        .rootdir_get_fd = afb_common_rootdir_get_fd,
        .rootdir_open_locale = rootdir_open_locale_cb,
        .queue_job = queue_job_cb,
index d06d8fc..1db59cd 100644 (file)
@@ -38,7 +38,7 @@
 #include "verbose.h"
 #include "locale-root.h"
 
-#include "afb-common.h"
+#include "afb-systemd.h"
 #include "jobs.h"
 
 #define JSON_CONTENT  "application/json"
@@ -420,7 +420,7 @@ int afb_hsrv_start(struct afb_hsrv *hsrv, uint16_t port, unsigned int connection
                return 0;
        }
 
-       rc = sd_event_add_io(afb_common_get_event_loop(), &evsrc, info->listen_fd, EPOLLIN, io_event_callback, hsrv);
+       rc = sd_event_add_io(afb_systemd_get_event_loop(), &evsrc, info->listen_fd, EPOLLIN, io_event_callback, hsrv);
        if (rc < 0) {
                MHD_stop_daemon(httpd);
                errno = -rc;
index 036b494..0049c50 100644 (file)
@@ -36,7 +36,7 @@
 
 #include <afb/afb-event.h>
 
-#include "afb-common.h"
+#include "afb-systemd.h"
 
 #include "afb-session.h"
 #include "afb-cred.h"
@@ -670,9 +670,9 @@ static struct afb_stub_ws *afb_stub_ws_create(int fd, const char *apiname, struc
                errno = ENOMEM;
        else {
                if (client)
-                       stubws->proto = afb_proto_ws_create_client(afb_common_get_event_loop(), fd, &client_itf, stubws);
+                       stubws->proto = afb_proto_ws_create_client(afb_systemd_get_event_loop(), fd, &client_itf, stubws);
                else
-                       stubws->proto = afb_proto_ws_create_server(afb_common_get_event_loop(), fd, &server_itf, stubws);
+                       stubws->proto = afb_proto_ws_create_server(afb_systemd_get_event_loop(), fd, &server_itf, stubws);
                if (stubws->proto != NULL) {
                        strcpy(stubws->apiname, apiname);
                        stubws->apiset = afb_apiset_addref(apiset);
diff --git a/src/afb-systemd.c b/src/afb-systemd.c
new file mode 100644 (file)
index 0000000..4faa9fc
--- /dev/null
@@ -0,0 +1,64 @@
+/*
+ * Copyright (C) 2015, 2016, 2017 "IoT.bzh"
+ * Author José Bollo <jose.bollo@iot.bzh>
+ *
+ * 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.
+ */
+
+#define _GNU_SOURCE
+
+#include <unistd.h>
+#include <errno.h>
+
+#include <systemd/sd-event.h>
+#include <systemd/sd-bus.h>
+
+#include "afb-systemd.h"
+#include "jobs.h"
+
+static struct sd_bus *sdbusopen(struct sd_bus **p, int (*f)(struct sd_bus **))
+{
+       if (*p == NULL) {
+               int rc = f(p);
+               if (rc < 0) {
+                       errno = -rc;
+                       *p = NULL;
+               } else {
+                       rc = sd_bus_attach_event(*p, afb_systemd_get_event_loop(), 0);
+                       if (rc < 0) {
+                               sd_bus_unref(*p);
+                               errno = -rc;
+                               *p = NULL;
+                       }
+               }
+       }
+       return *p;
+}
+
+struct sd_event *afb_systemd_get_event_loop()
+{
+       return jobs_get_sd_event();
+}
+
+struct sd_bus *afb_systemd_get_user_bus()
+{
+       static struct sd_bus *result = NULL;
+       return sdbusopen((void*)&result, (void*)sd_bus_open_user);
+}
+
+struct sd_bus *afb_systemd_get_system_bus()
+{
+       static struct sd_bus *result = NULL;
+       return sdbusopen((void*)&result, (void*)sd_bus_open_system);
+}
+
diff --git a/src/afb-systemd.h b/src/afb-systemd.h
new file mode 100644 (file)
index 0000000..10f0fba
--- /dev/null
@@ -0,0 +1,27 @@
+/*
+ * Copyright (C) 2015, 2016, 2017 "IoT.bzh"
+ * Author José Bollo <jose.bollo@iot.bzh>
+ *
+ * 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.
+ */
+
+#pragma once
+
+struct sd_event;
+struct sd_bus;
+
+extern struct sd_event *afb_systemd_get_event_loop();
+extern struct sd_bus *afb_systemd_get_user_bus();
+extern struct sd_bus *afb_systemd_get_system_bus();
+
+
index 6215a68..0fa13df 100644 (file)
@@ -27,7 +27,7 @@
 
 #include "afb-wsj1.h"
 #include "afb-ws-json1.h"
-#include "afb-common.h"
+#include "afb-systemd.h"
 #include "afb-msg-json.h"
 #include "afb-session.h"
 #include "afb-cred.h"
@@ -118,7 +118,7 @@ struct afb_ws_json1 *afb_ws_json1_create(int fd, struct afb_apiset *apiset, stru
        if (result->session == NULL)
                goto error2;
 
-       result->wsj1 = afb_wsj1_create(afb_common_get_event_loop(), fd, &wsj1_itf, result);
+       result->wsj1 = afb_wsj1_create(afb_systemd_get_event_loop(), fd, &wsj1_itf, result);
        if (result->wsj1 == NULL)
                goto error3;
 
index 35a3f3e..be0cdbf 100644 (file)
@@ -36,7 +36,7 @@
 #include <afb/afb-binding-v2.h>
 
 #include "afs-supervision.h"
-#include "afb-common.h"
+#include "afb-systemd.h"
 #include "afb-session.h"
 #include "afb-cred.h"
 #include "afb-stub-ws.h"
@@ -339,7 +339,7 @@ static int init(const char *spec)
        }
 
        /* integrate the socket to the loop */
-       rc = sd_event_add_io(afb_common_get_event_loop(),
+       rc = sd_event_add_io(afb_systemd_get_event_loop(),
                                NULL, fd, EPOLLIN,
                                listening, NULL);
        if (rc < 0) {