e8c42e4bd7fa6f2b8b7394e9af43461be4b26fb4
[lttng-tools.git] / src / bin / lttng-sessiond / health.c
1 /*
2 * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com>
3 * Copyright (C) 2018 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License, version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 51
16 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #include "lttng-sessiond.h"
20 #include "health-sessiond.h"
21 #include <common/macros.h>
22 #include <common/error.h>
23 #include <common/utils.h>
24 #include <common/pipe.h>
25 #include <inttypes.h>
26 #include <sys/stat.h>
27 #include "utils.h"
28 #include "thread.h"
29
30 struct thread_notifiers {
31 struct lttng_pipe *quit_pipe;
32 sem_t ready;
33 };
34
35 static
36 void mark_thread_as_ready(struct thread_notifiers *notifiers)
37 {
38 DBG("Marking health management thread as ready");
39 sem_post(&notifiers->ready);
40 }
41
42 static
43 void wait_until_thread_is_ready(struct thread_notifiers *notifiers)
44 {
45 DBG("Waiting for health management thread to be ready");
46 sem_wait(&notifiers->ready);
47 DBG("Health management thread is ready");
48 }
49
50 static void cleanup_health_management_thread(void *data)
51 {
52 struct thread_notifiers *notifiers = data;
53
54 lttng_pipe_destroy(notifiers->quit_pipe);
55 sem_destroy(&notifiers->ready);
56 free(notifiers);
57 }
58
59 /*
60 * Thread managing health check socket.
61 */
62 static void *thread_manage_health(void *data)
63 {
64 const bool is_root = (getuid() == 0);
65 int sock = -1, new_sock = -1, ret, i, pollfd, err = -1;
66 uint32_t revents, nb_fd;
67 struct lttng_poll_event events;
68 struct health_comm_msg msg;
69 struct health_comm_reply reply;
70 /* Thread-specific quit pipe. */
71 struct thread_notifiers *notifiers = data;
72 const int quit_pipe_read_fd = lttng_pipe_get_readfd(
73 notifiers->quit_pipe);
74
75 DBG("[thread] Manage health check started");
76
77 rcu_register_thread();
78
79 /*
80 * Created with a size of two for:
81 * - client socket
82 * - thread quit pipe
83 */
84 ret = lttng_poll_create(&events, 2, LTTNG_CLOEXEC);
85 if (ret < 0) {
86 goto error;
87 }
88
89 /* Create unix socket */
90 sock = lttcomm_create_unix_sock(config.health_unix_sock_path.value);
91 if (sock < 0) {
92 ERR("Unable to create health check Unix socket");
93 goto error;
94 }
95
96 if (is_root) {
97 /* lttng health client socket path permissions */
98 ret = chown(config.health_unix_sock_path.value, 0,
99 utils_get_group_id(config.tracing_group_name.value));
100 if (ret < 0) {
101 ERR("Unable to set group on %s", config.health_unix_sock_path.value);
102 PERROR("chown");
103 goto error;
104 }
105
106 ret = chmod(config.health_unix_sock_path.value,
107 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
108 if (ret < 0) {
109 ERR("Unable to set permissions on %s", config.health_unix_sock_path.value);
110 PERROR("chmod");
111 goto error;
112 }
113 }
114
115 /*
116 * Set the CLOEXEC flag. Return code is useless because either way, the
117 * show must go on.
118 */
119 (void) utils_set_fd_cloexec(sock);
120
121 ret = lttcomm_listen_unix_sock(sock);
122 if (ret < 0) {
123 goto error;
124 }
125
126 ret = lttng_poll_add(&events, quit_pipe_read_fd, LPOLLIN | LPOLLERR);
127 if (ret < 0) {
128 goto error;
129 }
130
131 /* Add the application registration socket */
132 ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLPRI);
133 if (ret < 0) {
134 goto error;
135 }
136
137 mark_thread_as_ready(notifiers);
138 while (1) {
139 DBG("Health check ready");
140
141 /* Infinite blocking call, waiting for transmission */
142 restart:
143 ret = lttng_poll_wait(&events, -1);
144 if (ret < 0) {
145 /*
146 * Restart interrupted system call.
147 */
148 if (errno == EINTR) {
149 goto restart;
150 }
151 goto error;
152 }
153
154 nb_fd = ret;
155
156 for (i = 0; i < nb_fd; i++) {
157 /* Fetch once the poll data */
158 revents = LTTNG_POLL_GETEV(&events, i);
159 pollfd = LTTNG_POLL_GETFD(&events, i);
160
161 if (!revents) {
162 /* No activity for this FD (poll implementation). */
163 continue;
164 }
165
166 /* Event on the registration socket */
167 if (pollfd == sock) {
168 if (revents & LPOLLIN) {
169 continue;
170 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
171 ERR("Health socket poll error");
172 goto error;
173 } else {
174 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
175 goto error;
176 }
177 } else {
178 /* Event on the thread's quit pipe. */
179 err = 0;
180 goto exit;
181 }
182 }
183
184 new_sock = lttcomm_accept_unix_sock(sock);
185 if (new_sock < 0) {
186 goto error;
187 }
188
189 /*
190 * Set the CLOEXEC flag. Return code is useless because either way, the
191 * show must go on.
192 */
193 (void) utils_set_fd_cloexec(new_sock);
194
195 DBG("Receiving data from client for health...");
196 ret = lttcomm_recv_unix_sock(new_sock, (void *)&msg, sizeof(msg));
197 if (ret <= 0) {
198 DBG("Nothing recv() from client... continuing");
199 ret = close(new_sock);
200 if (ret) {
201 PERROR("close");
202 }
203 continue;
204 }
205
206 rcu_thread_online();
207
208 memset(&reply, 0, sizeof(reply));
209 for (i = 0; i < NR_HEALTH_SESSIOND_TYPES; i++) {
210 /*
211 * health_check_state returns 0 if health is
212 * bad.
213 */
214 if (!health_check_state(health_sessiond, i)) {
215 reply.ret_code |= 1ULL << i;
216 }
217 }
218
219 DBG2("Health check return value %" PRIx64, reply.ret_code);
220
221 ret = lttcomm_send_unix_sock(new_sock, (void *) &reply,
222 sizeof(reply));
223 if (ret < 0) {
224 ERR("Failed to send health data back to client");
225 }
226
227 /* End of transmission */
228 ret = close(new_sock);
229 if (ret) {
230 PERROR("close");
231 }
232 }
233
234 exit:
235 error:
236 if (err) {
237 ERR("Health error occurred in %s", __func__);
238 }
239 DBG("Health check thread dying");
240 unlink(config.health_unix_sock_path.value);
241 if (sock >= 0) {
242 ret = close(sock);
243 if (ret) {
244 PERROR("close");
245 }
246 }
247
248 lttng_poll_clean(&events);
249 rcu_unregister_thread();
250 return NULL;
251 }
252
253 static bool shutdown_health_management_thread(void *data)
254 {
255 struct thread_notifiers *notifiers = data;
256 const int write_fd = lttng_pipe_get_writefd(notifiers->quit_pipe);
257
258 return notify_thread_pipe(write_fd) == 1;
259 }
260
261 bool launch_health_management_thread(void)
262 {
263 struct thread_notifiers *notifiers;
264 struct lttng_thread *thread;
265
266 notifiers = zmalloc(sizeof(*notifiers));
267 if (!notifiers) {
268 goto error;
269 }
270
271 sem_init(&notifiers->ready, 0, 0);
272 notifiers->quit_pipe = lttng_pipe_open(FD_CLOEXEC);
273 if (!notifiers->quit_pipe) {
274 goto error;
275 }
276 thread = lttng_thread_create("Health management",
277 thread_manage_health,
278 shutdown_health_management_thread,
279 cleanup_health_management_thread,
280 notifiers);
281 if (!thread) {
282 goto error;
283 }
284
285 wait_until_thread_is_ready(notifiers);
286 lttng_thread_put(thread);
287 return true;
288 error:
289 cleanup_health_management_thread(notifiers);
290 return false;
291 }
This page took 0.03528 seconds and 4 git commands to generate.