9474a8560a7b540fd3bb22284300cee6f64c28be
[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 gid_t gid;
99
100 ret = utils_get_group_id(config.tracing_group_name.value, true, &gid);
101 if (ret) {
102 /* Default to root group. */
103 gid = 0;
104 }
105
106 ret = chown(config.health_unix_sock_path.value, 0, gid);
107 if (ret < 0) {
108 ERR("Unable to set group on %s", config.health_unix_sock_path.value);
109 PERROR("chown");
110 goto error;
111 }
112
113 ret = chmod(config.health_unix_sock_path.value,
114 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
115 if (ret < 0) {
116 ERR("Unable to set permissions on %s", config.health_unix_sock_path.value);
117 PERROR("chmod");
118 goto error;
119 }
120 }
121
122 /*
123 * Set the CLOEXEC flag. Return code is useless because either way, the
124 * show must go on.
125 */
126 (void) utils_set_fd_cloexec(sock);
127
128 ret = lttcomm_listen_unix_sock(sock);
129 if (ret < 0) {
130 goto error;
131 }
132
133 ret = lttng_poll_add(&events, quit_pipe_read_fd, LPOLLIN | LPOLLERR);
134 if (ret < 0) {
135 goto error;
136 }
137
138 /* Add the application registration socket */
139 ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLPRI);
140 if (ret < 0) {
141 goto error;
142 }
143
144 mark_thread_as_ready(notifiers);
145 while (1) {
146 DBG("Health check ready");
147
148 /* Infinite blocking call, waiting for transmission */
149 restart:
150 ret = lttng_poll_wait(&events, -1);
151 if (ret < 0) {
152 /*
153 * Restart interrupted system call.
154 */
155 if (errno == EINTR) {
156 goto restart;
157 }
158 goto error;
159 }
160
161 nb_fd = ret;
162
163 for (i = 0; i < nb_fd; i++) {
164 /* Fetch once the poll data */
165 revents = LTTNG_POLL_GETEV(&events, i);
166 pollfd = LTTNG_POLL_GETFD(&events, i);
167
168 if (!revents) {
169 /* No activity for this FD (poll implementation). */
170 continue;
171 }
172
173 /* Event on the registration socket */
174 if (pollfd == sock) {
175 if (revents & LPOLLIN) {
176 continue;
177 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
178 ERR("Health socket poll error");
179 goto error;
180 } else {
181 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
182 goto error;
183 }
184 } else {
185 /* Event on the thread's quit pipe. */
186 err = 0;
187 goto exit;
188 }
189 }
190
191 new_sock = lttcomm_accept_unix_sock(sock);
192 if (new_sock < 0) {
193 goto error;
194 }
195
196 /*
197 * Set the CLOEXEC flag. Return code is useless because either way, the
198 * show must go on.
199 */
200 (void) utils_set_fd_cloexec(new_sock);
201
202 DBG("Receiving data from client for health...");
203 ret = lttcomm_recv_unix_sock(new_sock, (void *)&msg, sizeof(msg));
204 if (ret <= 0) {
205 DBG("Nothing recv() from client... continuing");
206 ret = close(new_sock);
207 if (ret) {
208 PERROR("close");
209 }
210 continue;
211 }
212
213 rcu_thread_online();
214
215 memset(&reply, 0, sizeof(reply));
216 for (i = 0; i < NR_HEALTH_SESSIOND_TYPES; i++) {
217 /*
218 * health_check_state returns 0 if health is
219 * bad.
220 */
221 if (!health_check_state(health_sessiond, i)) {
222 reply.ret_code |= 1ULL << i;
223 }
224 }
225
226 DBG2("Health check return value %" PRIx64, reply.ret_code);
227
228 ret = lttcomm_send_unix_sock(new_sock, (void *) &reply,
229 sizeof(reply));
230 if (ret < 0) {
231 ERR("Failed to send health data back to client");
232 }
233
234 /* End of transmission */
235 ret = close(new_sock);
236 if (ret) {
237 PERROR("close");
238 }
239 }
240
241 exit:
242 error:
243 if (err) {
244 ERR("Health error occurred in %s", __func__);
245 }
246 DBG("Health check thread dying");
247 unlink(config.health_unix_sock_path.value);
248 if (sock >= 0) {
249 ret = close(sock);
250 if (ret) {
251 PERROR("close");
252 }
253 }
254
255 lttng_poll_clean(&events);
256 rcu_unregister_thread();
257 return NULL;
258 }
259
260 static bool shutdown_health_management_thread(void *data)
261 {
262 struct thread_notifiers *notifiers = data;
263 const int write_fd = lttng_pipe_get_writefd(notifiers->quit_pipe);
264
265 return notify_thread_pipe(write_fd) == 1;
266 }
267
268 bool launch_health_management_thread(void)
269 {
270 struct thread_notifiers *notifiers;
271 struct lttng_thread *thread;
272
273 notifiers = zmalloc(sizeof(*notifiers));
274 if (!notifiers) {
275 goto error_alloc;
276 }
277
278 sem_init(&notifiers->ready, 0, 0);
279 notifiers->quit_pipe = lttng_pipe_open(FD_CLOEXEC);
280 if (!notifiers->quit_pipe) {
281 goto error;
282 }
283 thread = lttng_thread_create("Health management",
284 thread_manage_health,
285 shutdown_health_management_thread,
286 cleanup_health_management_thread,
287 notifiers);
288 if (!thread) {
289 goto error;
290 }
291
292 wait_until_thread_is_ready(notifiers);
293 lttng_thread_put(thread);
294 return true;
295 error:
296 cleanup_health_management_thread(notifiers);
297 error_alloc:
298 return false;
299 }
This page took 0.034103 seconds and 3 git commands to generate.