Cleanup: enforce const-correctness in notification thread
[lttng-tools.git] / src / bin / lttng-consumerd / health-consumerd.c
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
18 #define _LGPL_SOURCE
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>
39 #include <urcu/compiler.h>
40 #include <ulimit.h>
41 #include <inttypes.h>
42
43 #include <common/defaults.h>
44 #include <common/common.h>
45 #include <common/consumer/consumer.h>
46 #include <common/consumer/consumer-timer.h>
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 {
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
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),
136 DEFAULT_HOME_KCONSUMER_HEALTH_UNIX_SOCK, home_path);
137 break;
138 case LTTNG_CONSUMER64_UST:
139 snprintf(health_unix_sock_path, sizeof(health_unix_sock_path),
140 DEFAULT_HOME_USTCONSUMER64_HEALTH_UNIX_SOCK, home_path);
141 break;
142 case LTTNG_CONSUMER32_UST:
143 snprintf(health_unix_sock_path, sizeof(health_unix_sock_path),
144 DEFAULT_HOME_USTCONSUMER32_HEALTH_UNIX_SOCK, home_path);
145 break;
146 default:
147 ret = -EINVAL;
148 goto end;
149 }
150 }
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;
165 int is_root;
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");
180 err = -1;
181 goto error;
182 }
183
184 is_root = !getuid();
185 if (is_root) {
186 /* lttng health client socket path permissions */
187 ret = chown(health_unix_sock_path, 0,
188 utils_get_group_id(tracing_group_name));
189 if (ret < 0) {
190 ERR("Unable to set group on %s", health_unix_sock_path);
191 PERROR("chown");
192 err = -1;
193 goto error;
194 }
195
196 ret = chmod(health_unix_sock_path,
197 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
198 if (ret < 0) {
199 ERR("Unable to set permissions on %s", health_unix_sock_path);
200 PERROR("chmod");
201 err = -1;
202 goto error;
203 }
204 }
205
206 /*
207 * Set the CLOEXEC flag. Return code is useless because either way, the
208 * show must go on.
209 */
210 (void) utils_set_fd_cloexec(sock);
211
212 ret = lttcomm_listen_unix_sock(sock);
213 if (ret < 0) {
214 goto error;
215 }
216
217 /* Size is set to 1 for the consumer_channel pipe */
218 ret = lttng_poll_create(&events, 2, LTTNG_CLOEXEC);
219 if (ret < 0) {
220 ERR("Poll set creation failed");
221 goto error;
222 }
223
224 ret = lttng_poll_add(&events, health_quit_pipe[0], LPOLLIN);
225 if (ret < 0) {
226 goto error;
227 }
228
229 /* Add the application registration socket */
230 ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLPRI);
231 if (ret < 0) {
232 goto error;
233 }
234
235 /* Perform prior memory accesses before decrementing ready */
236 cmm_smp_mb__before_uatomic_dec();
237 uatomic_dec(&lttng_consumer_ready);
238
239 while (1) {
240 DBG("Health check ready");
241
242 /* Inifinite blocking call, waiting for transmission */
243 restart:
244 ret = lttng_poll_wait(&events, -1);
245 if (ret < 0) {
246 /*
247 * Restart interrupted system call.
248 */
249 if (errno == EINTR) {
250 goto restart;
251 }
252 goto error;
253 }
254
255 nb_fd = ret;
256
257 for (i = 0; i < nb_fd; i++) {
258 /* Fetch once the poll data */
259 revents = LTTNG_POLL_GETEV(&events, i);
260 pollfd = LTTNG_POLL_GETFD(&events, i);
261
262 if (!revents) {
263 /* No activity for this FD (poll implementation). */
264 continue;
265 }
266
267 /* Thread quit pipe has been closed. Killing thread. */
268 ret = check_health_quit_pipe(pollfd, revents);
269 if (ret) {
270 err = 0;
271 goto exit;
272 }
273
274 /* Event on the registration socket */
275 if (pollfd == sock) {
276 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)
277 && !(revents & LPOLLIN)) {
278 ERR("Health socket poll error");
279 goto error;
280 }
281 }
282 }
283
284 new_sock = lttcomm_accept_unix_sock(sock);
285 if (new_sock < 0) {
286 goto error;
287 }
288
289 /*
290 * Set the CLOEXEC flag. Return code is useless because either way, the
291 * show must go on.
292 */
293 (void) utils_set_fd_cloexec(new_sock);
294
295 DBG("Receiving data from client for health...");
296 ret = lttcomm_recv_unix_sock(new_sock, (void *)&msg, sizeof(msg));
297 if (ret <= 0) {
298 DBG("Nothing recv() from client... continuing");
299 ret = close(new_sock);
300 if (ret) {
301 PERROR("close");
302 }
303 new_sock = -1;
304 continue;
305 }
306
307 rcu_thread_online();
308
309 assert(msg.cmd == HEALTH_CMD_CHECK);
310
311 memset(&reply, 0, sizeof(reply));
312 for (i = 0; i < NR_HEALTH_CONSUMERD_TYPES; i++) {
313 /*
314 * health_check_state return 0 if thread is in
315 * error.
316 */
317 if (!health_check_state(health_consumerd, i)) {
318 reply.ret_code |= 1ULL << i;
319 }
320 }
321
322 DBG("Health check return value %" PRIx64, reply.ret_code);
323
324 ret = send_unix_sock(new_sock, (void *) &reply, sizeof(reply));
325 if (ret < 0) {
326 ERR("Failed to send health data back to client");
327 }
328
329 /* End of transmission */
330 ret = close(new_sock);
331 if (ret) {
332 PERROR("close");
333 }
334 new_sock = -1;
335 }
336
337 exit:
338 error:
339 if (err) {
340 ERR("Health error occurred in %s", __func__);
341 }
342 DBG("Health check thread dying");
343 unlink(health_unix_sock_path);
344 if (sock >= 0) {
345 ret = close(sock);
346 if (ret) {
347 PERROR("close");
348 }
349 }
350
351 lttng_poll_clean(&events);
352
353 rcu_unregister_thread();
354 return NULL;
355 }
This page took 0.040535 seconds and 4 git commands to generate.