From fb0f81a1d8b5369842269fa1bdb9ad8d52882491 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jos=C3=A9=20Bollo?= Date: Wed, 15 Mar 2017 11:49:29 +0100 Subject: [PATCH] Emits reload to systemd when needed MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit The current implementation enforce the reload when a 'wants' target is created or deleted. This should work well for system units. However, for system units, this behaviour isn't enought when more that a user is active because only the user that installs the application will be updated. For this reason, a paralelle mechanism has to be defined. Also note that systemd is henceforth required for tools because wgtpkg-installer needs it now. Change-Id: I4fc03a44dbc58c2374ea21dbf6b436f646d04e00 Signed-off-by: José Bollo --- src/CMakeLists.txt | 96 +++++++++++++++++++++++------------------------------ src/utils-systemd.c | 41 +++++++++++++++++++++++ src/utils-systemd.h | 1 + src/wgtpkg-unit.c | 61 ++++++++++++++++++++++++++++------ 4 files changed, 133 insertions(+), 66 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f121a62..41c2b1e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -41,7 +41,7 @@ set(CMAKE_C_FLAGS_CCOV "-g -O2 --coverage") include(FindPkgConfig) -pkg_check_modules(EXTRAS REQUIRED libxml-2.0 openssl xmlsec1 xmlsec1-openssl json-c) +pkg_check_modules(EXTRAS REQUIRED libxml-2.0 openssl xmlsec1 xmlsec1-openssl json-c libsystemd>=222) add_compile_options(${EXTRAS_CFLAGS}) include_directories(${EXTRAS_INCLUDE_DIRS}) link_libraries(${EXTRAS_LIBRARIES}) @@ -100,6 +100,7 @@ add_library(utils STATIC utils-dir.c utils-file.c utils-json.c + utils-jbus.c utils-systemd.c verbose.c ) @@ -116,6 +117,13 @@ add_library(secwrp STATIC secmgr-wrap.c ) +add_library(afm STATIC + afm-db.c + afm-launch.c + afm-launch-mode.c + afm-run.c + ) + ########################################################################### # packaging tools @@ -136,60 +144,38 @@ target_link_libraries(wgtpkg-installer wgtpkg wgt secwrp utils) install(TARGETS wgtpkg-sign wgtpkg-pack wgtpkg-info wgtpkg-installer DESTINATION ${CMAKE_INSTALL_BINDIR}) ########################################################################### -# the targeted - -pkg_check_modules(SYSTEMD libsystemd>=222) -if(SYSTEMD_FOUND) - add_compile_options(${SYSTEMD_CFLAGS}) - include_directories(${SYSTEMD_INCLUDE_DIRS}) - link_libraries(${SYSTEMD_LIBRARIES}) - - add_library(utils2 STATIC - utils-jbus.c - ) - - add_library(afm STATIC - afm-db.c - afm-launch.c - afm-launch-mode.c - afm-run.c - ) - - ########################################################################### - # the daemons - MESSAGE(STATUS "Creating daemons") - - add_executable(afm-user-daemon afm-user-daemon.c) - target_link_libraries(afm-user-daemon afm secwrp wgt utils utils2) - install(TARGETS afm-user-daemon DESTINATION ${CMAKE_INSTALL_BINDIR}) - - add_executable(afm-system-daemon afm-system-daemon.c) - target_link_libraries(afm-system-daemon wgtpkg afm secwrp wgt utils utils2) - install(TARGETS afm-system-daemon DESTINATION ${CMAKE_INSTALL_BINDIR}) - - ########################################################################### - # the binding for afb - - pkg_check_modules(AFB afb-daemon) - if(AFB_FOUND) - message(STATUS "Creation afm-main-binding for AFB-DAEMON") - ############################################################### - pkg_get_variable(afb_binding_install_dir afb-daemon binding_install_dir) - ############################################################### - add_library(afm-main-binding MODULE afm-main-binding.c) - target_compile_options(afm-main-binding PRIVATE ${AFB_CFLAGS}) - target_include_directories(afm-main-binding PRIVATE ${AFB_INCLUDE_DIRS}) - target_link_libraries(afm-main-binding utils utils2 ${AFB_LIBRARIES}) - set_target_properties(afm-main-binding PROPERTIES - PREFIX "" - LINK_FLAGS "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/afm-main-binding.export-map" - ) - install(TARGETS afm-main-binding LIBRARY DESTINATION ${afb_binding_install_dir}) - else() - message(STATUS "Not creating the binding for AFB-DAEMON") - endif() - -endif(SYSTEMD_FOUND) +# the daemons +MESSAGE(STATUS "Creating daemons") + +add_executable(afm-user-daemon afm-user-daemon.c) +target_link_libraries(afm-user-daemon afm secwrp wgt utils) +install(TARGETS afm-user-daemon DESTINATION ${CMAKE_INSTALL_BINDIR}) + +add_executable(afm-system-daemon afm-system-daemon.c) +target_link_libraries(afm-system-daemon wgtpkg afm secwrp wgt utils) +install(TARGETS afm-system-daemon DESTINATION ${CMAKE_INSTALL_BINDIR}) + +########################################################################### +# the binding for afb + +pkg_check_modules(AFB afb-daemon) +if(AFB_FOUND) + message(STATUS "Creation afm-main-binding for AFB-DAEMON") + ############################################################### + pkg_get_variable(afb_binding_install_dir afb-daemon binding_install_dir) + ############################################################### + add_library(afm-main-binding MODULE afm-main-binding.c) + target_compile_options(afm-main-binding PRIVATE ${AFB_CFLAGS}) + target_include_directories(afm-main-binding PRIVATE ${AFB_INCLUDE_DIRS}) + target_link_libraries(afm-main-binding utils ${AFB_LIBRARIES}) + set_target_properties(afm-main-binding PROPERTIES + PREFIX "" + LINK_FLAGS "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/afm-main-binding.export-map" + ) + install(TARGETS afm-main-binding LIBRARY DESTINATION ${afb_binding_install_dir}) +else() + message(STATUS "Not creating the binding for AFB-DAEMON") +endif() ########################################################################### # the tests diff --git a/src/utils-systemd.c b/src/utils-systemd.c index 757a52c..48fb3d8 100644 --- a/src/utils-systemd.c +++ b/src/utils-systemd.c @@ -33,6 +33,35 @@ # define SYSTEMD_UNITS_ROOT "/usr/local/lib/systemd" #endif +static const char sdb_path[] = "/org/freedesktop/systemd1"; +static const char sdb_destination[] = "org.freedesktop.systemd1"; +static const char sdbi_manager[] = "org.freedesktop.systemd1.Manager"; +static const char sdbm_reload[] = "Reload"; + +static struct sd_bus *sysbus; +static struct sd_bus *usrbus; + +static int get_bus(int isuser, struct sd_bus **ret) +{ + int rc; + struct sd_bus *bus; + + bus = isuser ? usrbus : sysbus; + if (bus) { + *ret = bus; + rc = 0; + } else if (isuser) { + rc = sd_bus_open_user(ret); + if (!rc) + usrbus = *ret; + } else { + rc = sd_bus_open_system(ret); + if (!rc) + sysbus = *ret; + } + return rc; +} + int systemd_get_unit_path(char *path, size_t pathlen, int isuser, const char *unit, const char *uext) { int rc = snprintf(path, pathlen, "%s/%s/%s.%s", @@ -75,3 +104,15 @@ int systemd_get_wants_target(char *path, size_t pathlen, const char *unit, const return rc; } +int systemd_daemon_reload(int isuser) +{ + int rc; + struct sd_bus *bus; + + rc = get_bus(isuser, &bus); + if (!rc) { + rc = sd_bus_call_method(bus, sdb_destination, sdb_path, sdbi_manager, sdbm_reload, NULL, NULL, NULL); + } + return rc; +} + diff --git a/src/utils-systemd.h b/src/utils-systemd.h index d149309..30f9522 100644 --- a/src/utils-systemd.h +++ b/src/utils-systemd.h @@ -21,4 +21,5 @@ extern int systemd_get_unit_path(char *path, size_t pathlen, int isuser, const char *unit, const char *uext); extern int systemd_get_wants_path(char *path, size_t pathlen, int isuser, const char *wanter, const char *unit, const char *uext); extern int systemd_get_wants_target(char *path, size_t pathlen, const char *unit, const char *uext); +extern int systemd_daemon_reload(int isuser); diff --git a/src/wgtpkg-unit.c b/src/wgtpkg-unit.c index f141766..48cf3bb 100644 --- a/src/wgtpkg-unit.c +++ b/src/wgtpkg-unit.c @@ -419,28 +419,63 @@ static int get_wants_target(char *path, size_t pathlen, const struct unitdesc *d return rc; } +static int do_send_reload(const struct unitdesc descs[], unsigned count) +{ + unsigned i; + int reloadsys, reloadusr; + + reloadsys = reloadusr = 0; + for (i = 0 ; i < count ; i++) { + if (descs[i].wanted_by != NULL) { + switch (descs[i].scope) { + case unitscope_user: + reloadusr = 1; + break; + case unitscope_system: + reloadsys = 1; + break; + default: + break; + } + } + } + + if (reloadusr) + reloadusr = systemd_daemon_reload(1); + if (reloadsys) + reloadsys = systemd_daemon_reload(0); + return reloadsys ? : reloadusr ? : 0; +} + static int do_uninstall_units(void *closure, const struct unitdesc descs[], unsigned count) { int rc, rc2; unsigned i; char path[PATH_MAX]; + rc = 0; for (i = 0 ; i < count ; i++) { - rc = check_unit_desc(&descs[i], 0); - if (rc == 0) { - rc = get_unit_path(path, sizeof path, &descs[i]); - if (rc >= 0) { - rc = unlink(path); + rc2 = check_unit_desc(&descs[i], 0); + if (rc2 == 0) { + rc2 = get_unit_path(path, sizeof path, &descs[i]); + if (rc2 >= 0) { + rc2 = unlink(path); } + if (rc2 < 0 && rc == 0) + rc = rc2; if (descs[i].wanted_by != NULL) { rc2 = get_wants_path(path, sizeof path, &descs[i]); if (rc2 >= 0) rc2 = unlink(path); - rc = rc < 0 ? rc : rc2; } } + if (rc2 < 0 && rc == 0) + rc = rc2; } - return 0; + rc2 = do_send_reload(descs, count); + if (rc2 < 0 && rc == 0) + rc = rc2; + return rc; } static int do_install_units(void *closure, const struct unitdesc descs[], unsigned count) @@ -469,12 +504,16 @@ static int do_install_units(void *closure, const struct unitdesc descs[], unsign i++; } } - if (rc < 0) { - do_uninstall_units(closure, descs, i); - return rc; - } + if (rc < 0) + goto error; } + rc = do_send_reload(descs, count); + if (rc < 0) + goto error; return 0; +error: + do_uninstall_units(closure, descs, i); + return rc; } static int add_metadata(struct json_object *jdesc, const char *installdir, const char *icondir, int port) -- 2.16.6