From b182590a555afe093d2d6e5cdf3e1e2410d1a0f5 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jos=C3=A9=20Bollo?= Date: Wed, 21 Feb 2018 11:38:59 +0100 Subject: [PATCH] afb-systemd: Isolate systemd main entries MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit This change prepares the possibilty to remove the dependency to systemd/libsystemd. Change-Id: Ife4181d6c7195230a11a32e527b8973af6a096e1 Signed-off-by: José Bollo --- src/CMakeLists.txt | 1 + src/afb-api-dbus.c | 4 ++-- src/afb-api-ws.c | 4 ++-- src/afb-common.c | 44 ++---------------------------------- src/afb-common.h | 7 ------ src/afb-export.c | 19 ++++++++-------- src/afb-hsrv.c | 4 ++-- src/afb-stub-ws.c | 6 ++--- src/afb-systemd.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/afb-systemd.h | 27 ++++++++++++++++++++++ src/afb-ws-json1.c | 4 ++-- src/afs-supervisor.c | 4 ++-- 12 files changed, 117 insertions(+), 71 deletions(-) create mode 100644 src/afb-systemd.c create mode 100644 src/afb-systemd.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 89a181f5..436eaf02 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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 diff --git a/src/afb-api-dbus.c b/src/afb-api-dbus.c index ad640fb2..eeadb9cf 100644 --- a/src/afb-api-dbus.c +++ b/src/afb-api-dbus.c @@ -29,7 +29,7 @@ #include -#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; diff --git a/src/afb-api-ws.c b/src/afb-api-ws.c index 0e4d1d05..26d01932 100644 --- a/src/afb-api-ws.c +++ b/src/afb-api-ws.c @@ -33,7 +33,7 @@ #include #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) diff --git a/src/afb-common.c b/src/afb-common.c index c6c876b4..88638d43 100644 --- a/src/afb-common.c +++ b/src/afb-common.c @@ -17,57 +17,17 @@ #define _GNU_SOURCE +#include +#include #include #include -#include -#include -#include -#include -#include #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; diff --git a/src/afb-common.h b/src/afb-common.h index 1361d5f5..c5ceb2a0 100644 --- a/src/afb-common.h +++ b/src/afb-common.h @@ -17,13 +17,6 @@ #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(); diff --git a/src/afb-export.c b/src/afb-export.c index 840bd919..b9321833 100644 --- a/src/afb-export.c +++ b/src/afb-export.c @@ -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, diff --git a/src/afb-hsrv.c b/src/afb-hsrv.c index d06d8fc2..1db59cd3 100644 --- a/src/afb-hsrv.c +++ b/src/afb-hsrv.c @@ -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; diff --git a/src/afb-stub-ws.c b/src/afb-stub-ws.c index 036b4943..0049c50c 100644 --- a/src/afb-stub-ws.c +++ b/src/afb-stub-ws.c @@ -36,7 +36,7 @@ #include -#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 index 00000000..4faa9fc7 --- /dev/null +++ b/src/afb-systemd.c @@ -0,0 +1,64 @@ +/* + * Copyright (C) 2015, 2016, 2017 "IoT.bzh" + * Author José Bollo + * + * 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 +#include + +#include +#include + +#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 index 00000000..10f0fbac --- /dev/null +++ b/src/afb-systemd.h @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2015, 2016, 2017 "IoT.bzh" + * Author José Bollo + * + * 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(); + + diff --git a/src/afb-ws-json1.c b/src/afb-ws-json1.c index 6215a684..0fa13dfd 100644 --- a/src/afb-ws-json1.c +++ b/src/afb-ws-json1.c @@ -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; diff --git a/src/afs-supervisor.c b/src/afs-supervisor.c index 35a3f3e1..be0cdbf5 100644 --- a/src/afs-supervisor.c +++ b/src/afs-supervisor.c @@ -36,7 +36,7 @@ #include #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) { -- 2.16.6