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