blob: 49e51f40c895860632fed5ecc86b061d4fcbd69d [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
Egor Starkov7ce2e972015-09-25 18:00:27 +030028#include <stdlib.h>
29#include <systemd/sd-daemon.h>
Friedrich, Eugen (ADITG/SW1)265aeb32016-04-05 20:43:29 +000030#include <sys/socket.h>
Egor Starkov7ce2e972015-09-25 18:00:27 +030031#include <wayland-server.h>
Bryce Harrington25a2bdd2016-08-03 17:40:52 -070032
Egor Starkov7ce2e972015-09-25 18:00:27 +030033#include "shared/helpers.h"
Bryce Harrington25a2bdd2016-08-03 17:40:52 -070034#include "shared/string-helpers.h"
Egor Starkov7ce2e972015-09-25 18:00:27 +030035#include "shared/zalloc.h"
36#include "compositor.h"
37
38struct systemd_notifier {
39 int watchdog_time;
40 struct wl_event_source *watchdog_source;
41 struct wl_listener compositor_destroy_listener;
42};
43
44static int
Friedrich, Eugen (ADITG/SW1)265aeb32016-04-05 20:43:29 +000045add_systemd_sockets(struct weston_compositor *compositor)
46{
47 int fd;
48 int cnt_systemd_sockets;
49 int current_fd = 0;
50
51 cnt_systemd_sockets = sd_listen_fds(1);
52
53 if (cnt_systemd_sockets < 0) {
54 weston_log("sd_listen_fds failed with: %d\n",
55 cnt_systemd_sockets);
56 return -1;
57 }
58
59 /* socket-based activation not used, return silently */
60 if (cnt_systemd_sockets == 0)
61 return 0;
62
63 while (current_fd < cnt_systemd_sockets) {
64 fd = SD_LISTEN_FDS_START + current_fd;
65
66 if (sd_is_socket(fd, AF_UNIX, SOCK_STREAM,1) <= 0) {
67 weston_log("invalid socket provided from systemd\n");
68 return -1;
69 }
70
71 if (wl_display_add_socket_fd(compositor->wl_display, fd)) {
72 weston_log("wl_display_add_socket_fd failed"
73 "for systemd provided socket\n");
74 return -1;
75 }
76 current_fd++;
77 }
78
79 weston_log("info: add %d socket(s) provided by systemd\n",
80 current_fd);
81
82 return current_fd;
83}
84
85static int
Egor Starkov7ce2e972015-09-25 18:00:27 +030086watchdog_handler(void *data)
87{
88 struct systemd_notifier *notifier = data;
89
90 wl_event_source_timer_update(notifier->watchdog_source,
91 notifier->watchdog_time);
92
93 sd_notify(0, "WATCHDOG=1");
94
95 return 1;
96}
97
98static void
99weston_compositor_destroy_listener(struct wl_listener *listener, void *data)
100{
101 struct systemd_notifier *notifier;
102
103 sd_notify(0, "STOPPING=1");
104
105 notifier = container_of(listener,
106 struct systemd_notifier,
107 compositor_destroy_listener);
108
109 if (notifier->watchdog_source)
110 wl_event_source_remove(notifier->watchdog_source);
111
112 wl_list_remove(&notifier->compositor_destroy_listener.link);
113 free(notifier);
114}
115
116WL_EXPORT int
117module_init(struct weston_compositor *compositor,
118 int *argc, char *argv[])
119{
Egor Starkov7ce2e972015-09-25 18:00:27 +0300120 char *watchdog_time_env;
121 struct wl_event_loop *loop;
122 long watchdog_time_conv;
123 struct systemd_notifier *notifier;
124
125 notifier = zalloc(sizeof *notifier);
126 if (notifier == NULL)
127 return -1;
128
129 notifier->compositor_destroy_listener.notify =
130 weston_compositor_destroy_listener;
131 wl_signal_add(&compositor->destroy_signal,
132 &notifier->compositor_destroy_listener);
133
Friedrich, Eugen (ADITG/SW1)265aeb32016-04-05 20:43:29 +0000134 if (add_systemd_sockets(compositor) < 0)
135 return -1;
136
Egor Starkov7ce2e972015-09-25 18:00:27 +0300137 sd_notify(0, "READY=1");
138
139 /* 'WATCHDOG_USEC' is environment variable that is set
140 * by systemd to transfer 'WatchdogSec' watchdog timeout
141 * setting from service file.*/
142 watchdog_time_env = getenv("WATCHDOG_USEC");
Egor Starkov7ce2e972015-09-25 18:00:27 +0300143 if (!watchdog_time_env)
144 return 0;
145
Bryce Harrington25a2bdd2016-08-03 17:40:52 -0700146 if (!safe_strtoint(watchdog_time_env, &watchdog_time_conv))
Egor Starkov7ce2e972015-09-25 18:00:27 +0300147 return 0;
148
149 /* Convert 'WATCHDOG_USEC' to milliseconds and notify
150 * systemd every half of that time.*/
151 watchdog_time_conv /= 1000 * 2;
152 if (watchdog_time_conv <= 0)
153 return 0;
154
155 notifier->watchdog_time = watchdog_time_conv;
156
157 loop = wl_display_get_event_loop(compositor->wl_display);
158 notifier->watchdog_source =
159 wl_event_loop_add_timer(loop, watchdog_handler, notifier);
160 wl_event_source_timer_update(notifier->watchdog_source,
161 notifier->watchdog_time);
162
163 return 0;
164}
165