2 * Copyright (c) 2015 General Electric Company. All rights reserved.
3 * Copyright © 2020 Collabora, Ltd.
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 #include <systemd/sd-daemon.h>
32 #include <sys/socket.h>
33 #include <wayland-server.h>
35 #include "ivi-compositor.h"
36 #include "shared/helpers.h"
38 struct systemd_notifier {
40 struct wl_event_source *watchdog_source;
41 struct wl_listener compositor_destroy_listener;
45 safe_strtoint(const char *str, int32_t *value)
53 ret = strtol(str, &end, 10);
56 } else if (end == str || *end != '\0') {
61 if ((long)((int32_t)ret) != ret) {
66 *value = (int32_t)ret;
72 add_systemd_sockets(struct weston_compositor *compositor)
75 int cnt_systemd_sockets;
78 cnt_systemd_sockets = sd_listen_fds(1);
80 if (cnt_systemd_sockets < 0) {
81 weston_log("sd_listen_fds failed with: %d\n",
86 /* socket-based activation not used, return silently */
87 if (cnt_systemd_sockets == 0)
90 while (current_fd < cnt_systemd_sockets) {
91 fd = SD_LISTEN_FDS_START + current_fd;
93 if (sd_is_socket(fd, AF_UNIX, SOCK_STREAM, 1) <= 0) {
94 weston_log("invalid socket provided from systemd\n");
98 if (wl_display_add_socket_fd(compositor->wl_display, fd)) {
99 weston_log("wl_display_add_socket_fd failed"
100 "for systemd provided socket\n");
106 weston_log("info: add %d socket(s) provided by systemd\n",
113 watchdog_handler(void *data)
115 struct systemd_notifier *notifier = data;
117 wl_event_source_timer_update(notifier->watchdog_source,
118 notifier->watchdog_time);
120 sd_notify(0, "WATCHDOG=1");
126 weston_compositor_destroy_listener(struct wl_listener *listener, void *data)
128 struct systemd_notifier *notifier;
130 sd_notify(0, "STOPPING=1");
132 notifier = container_of(listener, struct systemd_notifier,
133 compositor_destroy_listener);
135 if (notifier->watchdog_source)
136 wl_event_source_remove(notifier->watchdog_source);
138 wl_list_remove(¬ifier->compositor_destroy_listener.link);
143 ivi_agl_systemd_notify(struct ivi_compositor *ivi)
145 struct weston_compositor *compositor = ivi->compositor;
146 char *watchdog_time_env;
147 struct wl_event_loop *loop;
148 int32_t watchdog_time_conv;
150 struct systemd_notifier *notifier;
152 notifier = zalloc(sizeof *notifier);
153 if (notifier == NULL)
156 notifier->compositor_destroy_listener.notify =
157 weston_compositor_destroy_listener;
158 wl_signal_add(&compositor->destroy_signal,
159 ¬ifier->compositor_destroy_listener);
161 if (add_systemd_sockets(compositor) < 0)
164 weston_log("Sending ready to systemd\n");
166 sd_notify(0, "READY=1");
168 /* 'WATCHDOG_USEC' is environment variable that is set
169 * by systemd to transfer 'WatchdogSec' watchdog timeout
170 * setting from service file.*/
171 watchdog_time_env = getenv("WATCHDOG_USEC");
172 if (!watchdog_time_env)
175 if (!safe_strtoint(watchdog_time_env, &watchdog_time_conv))
178 /* Convert 'WATCHDOG_USEC' to milliseconds and notify
179 * systemd every half of that time.*/
180 watchdog_time_conv /= 1000 * 2;
181 if (watchdog_time_conv <= 0)
184 notifier->watchdog_time = watchdog_time_conv;
186 loop = wl_display_get_event_loop(compositor->wl_display);
187 notifier->watchdog_source =
188 wl_event_loop_add_timer(loop, watchdog_handler, notifier);
189 wl_event_source_timer_update(notifier->watchdog_source,
190 notifier->watchdog_time);