vscode: Add configurations to run the executables under the debugger
[lttng-tools.git] / src / bin / lttng-consumerd / health-consumerd.cpp
1 /*
2 * Copyright (C) 2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #define _LGPL_SOURCE
9 #include "health-consumerd.hpp"
10 #include "lttng-consumerd.hpp"
11
12 #include <common/common.hpp>
13 #include <common/compat/poll.hpp>
14 #include <common/consumer/consumer-timer.hpp>
15 #include <common/consumer/consumer.hpp>
16 #include <common/defaults.hpp>
17 #include <common/sessiond-comm/sessiond-comm.hpp>
18 #include <common/utils.hpp>
19
20 #include <fcntl.h>
21 #include <getopt.h>
22 #include <grp.h>
23 #include <inttypes.h>
24 #include <limits.h>
25 #include <poll.h>
26 #include <pthread.h>
27 #include <signal.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <sys/ipc.h>
32 #include <sys/mman.h>
33 #include <sys/resource.h>
34 #include <sys/shm.h>
35 #include <sys/socket.h>
36 #include <sys/stat.h>
37 #include <sys/types.h>
38 #include <ulimit.h>
39 #include <unistd.h>
40 #include <urcu/compiler.h>
41 #include <urcu/list.h>
42
43 /* Global health check unix path */
44 static char health_unix_sock_path[PATH_MAX];
45
46 int health_quit_pipe[2] = { -1, -1 };
47
48 /*
49 * Send data on a unix socket using the liblttsessiondcomm API.
50 *
51 * Return lttcomm error code.
52 */
53 static int send_unix_sock(int sock, void *buf, size_t len)
54 {
55 /* Check valid length */
56 if (len == 0) {
57 return -1;
58 }
59
60 return lttcomm_send_unix_sock(sock, buf, len);
61 }
62
63 static int setup_health_path()
64 {
65 int is_root, ret = 0;
66 enum lttng_consumer_type type;
67 const char *home_path;
68
69 type = lttng_consumer_get_type();
70 is_root = !getuid();
71
72 if (is_root) {
73 if (strlen(health_unix_sock_path) != 0) {
74 goto end;
75 }
76 switch (type) {
77 case LTTNG_CONSUMER_KERNEL:
78 snprintf(health_unix_sock_path,
79 sizeof(health_unix_sock_path),
80 DEFAULT_GLOBAL_KCONSUMER_HEALTH_UNIX_SOCK);
81 break;
82 case LTTNG_CONSUMER64_UST:
83 snprintf(health_unix_sock_path,
84 sizeof(health_unix_sock_path),
85 DEFAULT_GLOBAL_USTCONSUMER64_HEALTH_UNIX_SOCK);
86 break;
87 case LTTNG_CONSUMER32_UST:
88 snprintf(health_unix_sock_path,
89 sizeof(health_unix_sock_path),
90 DEFAULT_GLOBAL_USTCONSUMER32_HEALTH_UNIX_SOCK);
91 break;
92 default:
93 ret = -EINVAL;
94 goto end;
95 }
96 } else {
97 home_path = utils_get_home_dir();
98 if (home_path == nullptr) {
99 /* TODO: Add --socket PATH option */
100 ERR("Can't get HOME directory for sockets creation.");
101 ret = -EPERM;
102 goto end;
103 }
104
105 /* Set health check Unix path */
106 if (strlen(health_unix_sock_path) != 0) {
107 goto end;
108 }
109 switch (type) {
110 case LTTNG_CONSUMER_KERNEL:
111 snprintf(health_unix_sock_path,
112 sizeof(health_unix_sock_path),
113 DEFAULT_HOME_KCONSUMER_HEALTH_UNIX_SOCK,
114 home_path);
115 break;
116 case LTTNG_CONSUMER64_UST:
117 snprintf(health_unix_sock_path,
118 sizeof(health_unix_sock_path),
119 DEFAULT_HOME_USTCONSUMER64_HEALTH_UNIX_SOCK,
120 home_path);
121 break;
122 case LTTNG_CONSUMER32_UST:
123 snprintf(health_unix_sock_path,
124 sizeof(health_unix_sock_path),
125 DEFAULT_HOME_USTCONSUMER32_HEALTH_UNIX_SOCK,
126 home_path);
127 break;
128 default:
129 ret = -EINVAL;
130 goto end;
131 }
132 }
133 end:
134 return ret;
135 }
136
137 /*
138 * Thread managing health check socket.
139 */
140 void *thread_manage_health_consumerd(void *data __attribute__((unused)))
141 {
142 int sock = -1, new_sock = -1, ret, i, err = -1;
143 uint32_t nb_fd;
144 struct lttng_poll_event events;
145 struct health_comm_msg msg;
146 struct health_comm_reply reply;
147 int is_root;
148
149 DBG("[thread] Manage health check started");
150
151 setup_health_path();
152
153 rcu_register_thread();
154
155 /* We might hit an error path before this is created. */
156 lttng_poll_init(&events);
157
158 /* Create unix socket */
159 sock = lttcomm_create_unix_sock(health_unix_sock_path);
160 if (sock < 0) {
161 ERR("Unable to create health check Unix socket");
162 err = -1;
163 goto error;
164 }
165
166 is_root = !getuid();
167 if (is_root) {
168 /* lttng health client socket path permissions */
169 gid_t gid;
170
171 ret = utils_get_group_id(tracing_group_name, true, &gid);
172 if (ret) {
173 /* Default to root group. */
174 gid = 0;
175 }
176
177 ret = chown(health_unix_sock_path, 0, gid);
178 if (ret < 0) {
179 ERR("Unable to set group on %s", health_unix_sock_path);
180 PERROR("chown");
181 err = -1;
182 goto error;
183 }
184
185 ret = chmod(health_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
186 if (ret < 0) {
187 ERR("Unable to set permissions on %s", health_unix_sock_path);
188 PERROR("chmod");
189 err = -1;
190 goto error;
191 }
192 }
193
194 /*
195 * Set the CLOEXEC flag. Return code is useless because either way, the
196 * show must go on.
197 */
198 (void) utils_set_fd_cloexec(sock);
199
200 ret = lttcomm_listen_unix_sock(sock);
201 if (ret < 0) {
202 goto error;
203 }
204
205 /* Size is set to 2 for the quit pipe and registration socket. */
206 ret = lttng_poll_create(&events, 2, LTTNG_CLOEXEC);
207 if (ret < 0) {
208 ERR("Poll set creation failed");
209 goto error;
210 }
211
212 ret = lttng_poll_add(&events, health_quit_pipe[0], LPOLLIN);
213 if (ret < 0) {
214 goto error;
215 }
216
217 /* Add the application registration socket */
218 ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLPRI);
219 if (ret < 0) {
220 goto error;
221 }
222
223 /* Perform prior memory accesses before decrementing ready */
224 cmm_smp_mb__before_uatomic_dec();
225 uatomic_dec(&lttng_consumer_ready);
226
227 while (true) {
228 DBG("Health check ready");
229
230 /* Inifinite blocking call, waiting for transmission */
231 restart:
232 ret = lttng_poll_wait(&events, -1);
233 if (ret < 0) {
234 /*
235 * Restart interrupted system call.
236 */
237 if (errno == EINTR) {
238 goto restart;
239 }
240 goto error;
241 }
242
243 nb_fd = ret;
244
245 for (i = 0; i < nb_fd; i++) {
246 /* Fetch once the poll data */
247 const auto revents = LTTNG_POLL_GETEV(&events, i);
248 const auto pollfd = LTTNG_POLL_GETFD(&events, i);
249
250 /* Activity on health quit pipe, exiting. */
251 if (pollfd == health_quit_pipe[0]) {
252 DBG("Activity on health quit pipe");
253 err = 0;
254 goto exit;
255 }
256
257 /* Event on the registration socket */
258 if (pollfd == sock) {
259 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP) &&
260 !(revents & LPOLLIN)) {
261 ERR("Health socket poll error");
262 goto error;
263 }
264 }
265 }
266
267 new_sock = lttcomm_accept_unix_sock(sock);
268 if (new_sock < 0) {
269 goto error;
270 }
271
272 /*
273 * Set the CLOEXEC flag. Return code is useless because either way, the
274 * show must go on.
275 */
276 (void) utils_set_fd_cloexec(new_sock);
277
278 DBG("Receiving data from client for health...");
279 ret = lttcomm_recv_unix_sock(new_sock, (void *) &msg, sizeof(msg));
280 if (ret <= 0) {
281 DBG("Nothing recv() from client... continuing");
282 ret = close(new_sock);
283 if (ret) {
284 PERROR("close");
285 }
286 new_sock = -1;
287 continue;
288 }
289
290 rcu_thread_online();
291
292 LTTNG_ASSERT(msg.cmd == HEALTH_CMD_CHECK);
293
294 memset(&reply, 0, sizeof(reply));
295 for (i = 0; i < NR_HEALTH_CONSUMERD_TYPES; i++) {
296 /*
297 * health_check_state return 0 if thread is in
298 * error.
299 */
300 if (!health_check_state(health_consumerd, i)) {
301 reply.ret_code |= 1ULL << i;
302 }
303 }
304
305 DBG("Health check return value %" PRIx64, reply.ret_code);
306
307 ret = send_unix_sock(new_sock, (void *) &reply, sizeof(reply));
308 if (ret < 0) {
309 ERR("Failed to send health data back to client");
310 }
311
312 /* End of transmission */
313 ret = close(new_sock);
314 if (ret) {
315 PERROR("close");
316 }
317 new_sock = -1;
318 }
319
320 exit:
321 error:
322 if (err) {
323 ERR("Health error occurred in %s", __func__);
324 }
325 DBG("Health check thread dying");
326 unlink(health_unix_sock_path);
327 if (sock >= 0) {
328 ret = close(sock);
329 if (ret) {
330 PERROR("close");
331 }
332 }
333
334 lttng_poll_clean(&events);
335
336 rcu_unregister_thread();
337 return nullptr;
338 }
This page took 0.036793 seconds and 5 git commands to generate.