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