1 /*
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 <stdlib.h>
29 #include <systemd/sd-daemon.h>
30 #include <sys/socket.h>
31 #include <wayland-server.h>
32
33 #include "shared/helpers.h"
34 #include "shared/string-helpers.h"
35 #include <libweston/zalloc.h>
36 #include <libweston/libweston.h>
37 #include "weston.h"
38
39 struct systemd_notifier {
40 int watchdog_time;
41 struct wl_event_source *watchdog_source;
42 struct wl_listener compositor_destroy_listener;
43 };
44
45 static int
add_systemd_sockets(struct weston_compositor * compositor)46 add_systemd_sockets(struct weston_compositor *compositor)
47 {
48 int fd;
49 int cnt_systemd_sockets;
50 int current_fd = 0;
51
52 cnt_systemd_sockets = sd_listen_fds(1);
53
54 if (cnt_systemd_sockets < 0) {
55 weston_log("sd_listen_fds failed with: %d\n",
56 cnt_systemd_sockets);
57 return -1;
58 }
59
60 /* socket-based activation not used, return silently */
61 if (cnt_systemd_sockets == 0)
62 return 0;
63
64 while (current_fd < cnt_systemd_sockets) {
65 fd = SD_LISTEN_FDS_START + current_fd;
66
67 if (sd_is_socket(fd, AF_UNIX, SOCK_STREAM,1) <= 0) {
68 weston_log("invalid socket provided from systemd\n");
69 return -1;
70 }
71
72 if (wl_display_add_socket_fd(compositor->wl_display, fd)) {
73 weston_log("wl_display_add_socket_fd failed"
74 "for systemd provided socket\n");
75 return -1;
76 }
77 current_fd++;
78 }
79
80 weston_log("info: add %d socket(s) provided by systemd\n",
81 current_fd);
82
83 return current_fd;
84 }
85
86 static int
watchdog_handler(void * data)87 watchdog_handler(void *data)
88 {
89 struct systemd_notifier *notifier = data;
90
91 wl_event_source_timer_update(notifier->watchdog_source,
92 notifier->watchdog_time);
93
94 sd_notify(0, "WATCHDOG=1");
95
96 return 1;
97 }
98
99 static void
weston_compositor_destroy_listener(struct wl_listener * listener,void * data)100 weston_compositor_destroy_listener(struct wl_listener *listener, void *data)
101 {
102 struct systemd_notifier *notifier;
103
104 sd_notify(0, "STOPPING=1");
105
106 notifier = container_of(listener,
107 struct systemd_notifier,
108 compositor_destroy_listener);
109
110 if (notifier->watchdog_source)
111 wl_event_source_remove(notifier->watchdog_source);
112
113 wl_list_remove(¬ifier->compositor_destroy_listener.link);
114 free(notifier);
115 }
116
117 WL_EXPORT int
wet_module_init(struct weston_compositor * compositor,int * argc,char * argv[])118 wet_module_init(struct weston_compositor *compositor,
119 int *argc, char *argv[])
120 {
121 char *watchdog_time_env;
122 struct wl_event_loop *loop;
123 int32_t watchdog_time_conv;
124 struct systemd_notifier *notifier;
125
126 notifier = zalloc(sizeof *notifier);
127 if (notifier == NULL)
128 return -1;
129
130 if (!weston_compositor_add_destroy_listener_once(compositor,
131 ¬ifier->compositor_destroy_listener,
132 weston_compositor_destroy_listener)) {
133 free(notifier);
134 return 0;
135 }
136
137 if (add_systemd_sockets(compositor) < 0)
138 return -1;
139
140 sd_notify(0, "READY=1");
141
142 /* 'WATCHDOG_USEC' is environment variable that is set
143 * by systemd to transfer 'WatchdogSec' watchdog timeout
144 * setting from service file.*/
145 watchdog_time_env = getenv("WATCHDOG_USEC");
146 if (!watchdog_time_env)
147 return 0;
148
149 if (!safe_strtoint(watchdog_time_env, &watchdog_time_conv))
150 return 0;
151
152 /* Convert 'WATCHDOG_USEC' to milliseconds and notify
153 * systemd every half of that time.*/
154 watchdog_time_conv /= 1000 * 2;
155 if (watchdog_time_conv <= 0)
156 return 0;
157
158 notifier->watchdog_time = watchdog_time_conv;
159
160 loop = wl_display_get_event_loop(compositor->wl_display);
161 notifier->watchdog_source =
162 wl_event_loop_add_timer(loop, watchdog_handler, notifier);
163 wl_event_source_timer_update(notifier->watchdog_source,
164 notifier->watchdog_time);
165
166 return 0;
167 }
168
169