From: Marius Vlad Date: Mon, 6 Jan 2020 16:22:38 +0000 (+0200) Subject: ivi-compositor: Add systemd notify message X-Git-Tag: 9.0.0~10 X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=commitdiff_plain;h=ffd00ab3a05351bac2124e5eba8a66f20a3b62da;p=src%2Fagl-compositor.git ivi-compositor: Add systemd notify message Notifies systemd that agl-compositor has been started. Re-uses most of the weston bits. Bug-AGL: SPEC-3119 Change-Id: I1a0b41093b2ce68dd4a64475af18d27338712c3d Signed-off-by: Marius Vlad --- diff --git a/meson.build b/meson.build index 08548f8..c338ee6 100644 --- a/meson.build +++ b/meson.build @@ -9,6 +9,8 @@ project('agl-compositor', license: 'MIT/Expat', ) +config_h = configuration_data() + cc = meson.get_compiler('c') add_project_arguments( cc.get_supported_arguments([ @@ -33,6 +35,7 @@ foreach func: optional_libc_funcs endif endforeach +dep_libsystemd = dependency('libsystemd', required: false) dep_scanner = dependency('wayland-scanner', native: true) prog_scanner = find_program(dep_scanner.get_pkgconfig_variable('wayland_scanner')) dep_wp = dependency('wayland-protocols', version: '>= 1.12') @@ -101,6 +104,17 @@ srcs_agl_compositor = [ xdg_shell_protocol_c, ] +if dep_libsystemd.found() + config_h.set('HAVE_SYSTEMD', 1) + + srcs_agl_compositor += 'src/systemd-notify.c' + deps_libweston += dep_libsystemd + + message('Found systemd, enabling notify support') +endif + +configure_file(output: 'config.h', configuration: config_h) + exe_agl_compositor = executable( 'agl-compositor', srcs_agl_compositor, diff --git a/src/ivi-compositor.h b/src/ivi-compositor.h index 3765f65..68cbbdf 100644 --- a/src/ivi-compositor.h +++ b/src/ivi-compositor.h @@ -27,6 +27,7 @@ #define IVI_COMPOSITOR_H #include +#include "config.h" #include #include @@ -174,6 +175,16 @@ struct ivi_shell_client { struct ivi_compositor * to_ivi_compositor(struct weston_compositor *ec); +#ifdef HAVE_SYSTEMD +int +ivi_agl_systemd_notify(struct ivi_compositor *ivi); +#else +static int +ivi_agl_systemd_notify(struct ivi_compositor *ivi) +{ +} +#endif + int ivi_shell_init(struct ivi_compositor *ivi); diff --git a/src/main.c b/src/main.c index fd0006f..eefdc19 100644 --- a/src/main.c +++ b/src/main.c @@ -1202,6 +1202,7 @@ int main(int argc, char *argv[]) ivi_shell_create_global(&ivi); ivi_launch_shell_client(&ivi); + ivi_agl_systemd_notify(&ivi); wl_display_run(display); diff --git a/src/systemd-notify.c b/src/systemd-notify.c new file mode 100644 index 0000000..1d77936 --- /dev/null +++ b/src/systemd-notify.c @@ -0,0 +1,193 @@ +/* + * Copyright (c) 2015 General Electric Company. All rights reserved. + * Copyright © 2020 Collabora, Ltd. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice (including the + * next paragraph) shall be included in all copies or substantial + * portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include +#include +#include + +#include +#include +#include + +#include "ivi-compositor.h" +#include "shared/helpers.h" + +struct systemd_notifier { + int watchdog_time; + struct wl_event_source *watchdog_source; + struct wl_listener compositor_destroy_listener; +}; + +static inline bool +safe_strtoint(const char *str, int32_t *value) +{ + long ret; + char *end; + + assert(str != NULL); + + errno = 0; + ret = strtol(str, &end, 10); + if (errno != 0) { + return false; + } else if (end == str || *end != '\0') { + errno = EINVAL; + return false; + } + + if ((long)((int32_t)ret) != ret) { + errno = ERANGE; + return false; + } + + *value = (int32_t)ret; + + return true; +} + +static int +add_systemd_sockets(struct weston_compositor *compositor) +{ + int fd; + int cnt_systemd_sockets; + int current_fd = 0; + + cnt_systemd_sockets = sd_listen_fds(1); + + if (cnt_systemd_sockets < 0) { + weston_log("sd_listen_fds failed with: %d\n", + cnt_systemd_sockets); + return -1; + } + + /* socket-based activation not used, return silently */ + if (cnt_systemd_sockets == 0) + return 0; + + while (current_fd < cnt_systemd_sockets) { + fd = SD_LISTEN_FDS_START + current_fd; + + if (sd_is_socket(fd, AF_UNIX, SOCK_STREAM, 1) <= 0) { + weston_log("invalid socket provided from systemd\n"); + return -1; + } + + if (wl_display_add_socket_fd(compositor->wl_display, fd)) { + weston_log("wl_display_add_socket_fd failed" + "for systemd provided socket\n"); + return -1; + } + current_fd++; + } + + weston_log("info: add %d socket(s) provided by systemd\n", + current_fd); + + return current_fd; +} + +static int +watchdog_handler(void *data) +{ + struct systemd_notifier *notifier = data; + + wl_event_source_timer_update(notifier->watchdog_source, + notifier->watchdog_time); + + sd_notify(0, "WATCHDOG=1"); + + return 1; +} + +static void +weston_compositor_destroy_listener(struct wl_listener *listener, void *data) +{ + struct systemd_notifier *notifier; + + sd_notify(0, "STOPPING=1"); + + notifier = container_of(listener, struct systemd_notifier, + compositor_destroy_listener); + + if (notifier->watchdog_source) + wl_event_source_remove(notifier->watchdog_source); + + wl_list_remove(¬ifier->compositor_destroy_listener.link); + free(notifier); +} + +int +ivi_agl_systemd_notify(struct ivi_compositor *ivi) +{ + struct weston_compositor *compositor = ivi->compositor; + char *watchdog_time_env; + struct wl_event_loop *loop; + int32_t watchdog_time_conv; + + struct systemd_notifier *notifier; + + notifier = zalloc(sizeof *notifier); + if (notifier == NULL) + return -1; + + notifier->compositor_destroy_listener.notify = + weston_compositor_destroy_listener; + wl_signal_add(&compositor->destroy_signal, + ¬ifier->compositor_destroy_listener); + + if (add_systemd_sockets(compositor) < 0) + return -1; + + weston_log("Sending ready to systemd\n"); + + sd_notify(0, "READY=1"); + + /* 'WATCHDOG_USEC' is environment variable that is set + * by systemd to transfer 'WatchdogSec' watchdog timeout + * setting from service file.*/ + watchdog_time_env = getenv("WATCHDOG_USEC"); + if (!watchdog_time_env) + return 0; + + if (!safe_strtoint(watchdog_time_env, &watchdog_time_conv)) + return 0; + + /* Convert 'WATCHDOG_USEC' to milliseconds and notify + * systemd every half of that time.*/ + watchdog_time_conv /= 1000 * 2; + if (watchdog_time_conv <= 0) + return 0; + + notifier->watchdog_time = watchdog_time_conv; + + loop = wl_display_get_event_loop(compositor->wl_display); + notifier->watchdog_source = + wl_event_loop_add_timer(loop, watchdog_handler, notifier); + wl_event_source_timer_update(notifier->watchdog_source, + notifier->watchdog_time); + + return 0; +}