blob: e61db0fbb7894e790cf8d22c6dd7a01e0948b615 [file] [log] [blame]
Egor Starkov7ce2e972015-09-25 18:00:27 +03001/*
2 * Copyright (c) 2015 General Electric Company. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial
14 * portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 */
25
26#include "config.h"
27
28#include <errno.h>
29#include <stdlib.h>
30#include <systemd/sd-daemon.h>
31#include <wayland-server.h>
32#include "shared/helpers.h"
33#include "shared/zalloc.h"
34#include "compositor.h"
35
36struct systemd_notifier {
37 int watchdog_time;
38 struct wl_event_source *watchdog_source;
39 struct wl_listener compositor_destroy_listener;
40};
41
42static int
43watchdog_handler(void *data)
44{
45 struct systemd_notifier *notifier = data;
46
47 wl_event_source_timer_update(notifier->watchdog_source,
48 notifier->watchdog_time);
49
50 sd_notify(0, "WATCHDOG=1");
51
52 return 1;
53}
54
55static void
56weston_compositor_destroy_listener(struct wl_listener *listener, void *data)
57{
58 struct systemd_notifier *notifier;
59
60 sd_notify(0, "STOPPING=1");
61
62 notifier = container_of(listener,
63 struct systemd_notifier,
64 compositor_destroy_listener);
65
66 if (notifier->watchdog_source)
67 wl_event_source_remove(notifier->watchdog_source);
68
69 wl_list_remove(&notifier->compositor_destroy_listener.link);
70 free(notifier);
71}
72
73WL_EXPORT int
74module_init(struct weston_compositor *compositor,
75 int *argc, char *argv[])
76{
77 char *tail;
78 char *watchdog_time_env;
79 struct wl_event_loop *loop;
80 long watchdog_time_conv;
81 struct systemd_notifier *notifier;
82
83 notifier = zalloc(sizeof *notifier);
84 if (notifier == NULL)
85 return -1;
86
87 notifier->compositor_destroy_listener.notify =
88 weston_compositor_destroy_listener;
89 wl_signal_add(&compositor->destroy_signal,
90 &notifier->compositor_destroy_listener);
91
92 sd_notify(0, "READY=1");
93
94 /* 'WATCHDOG_USEC' is environment variable that is set
95 * by systemd to transfer 'WatchdogSec' watchdog timeout
96 * setting from service file.*/
97 watchdog_time_env = getenv("WATCHDOG_USEC");
98
99 if (!watchdog_time_env)
100 return 0;
101
102 errno = 0;
103 watchdog_time_conv = strtol(watchdog_time_env, &tail, 0);
104 if ((errno != 0) || (*tail != '\0'))
105 return 0;
106
107 /* Convert 'WATCHDOG_USEC' to milliseconds and notify
108 * systemd every half of that time.*/
109 watchdog_time_conv /= 1000 * 2;
110 if (watchdog_time_conv <= 0)
111 return 0;
112
113 notifier->watchdog_time = watchdog_time_conv;
114
115 loop = wl_display_get_event_loop(compositor->wl_display);
116 notifier->watchdog_source =
117 wl_event_loop_add_timer(loop, watchdog_handler, notifier);
118 wl_event_source_timer_update(notifier->watchdog_source,
119 notifier->watchdog_time);
120
121 return 0;
122}
123