Tests: Add test to check shared-memory FD leaks after relayd dies
[lttng-tools.git] / src / bin / lttng-sessiond / register.cpp
1 /*
2 * Copyright (C) 2011 EfficiOS Inc.
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * Copyright (C) 2013 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 *
6 * SPDX-License-Identifier: GPL-2.0-only
7 *
8 */
9
10 #include <stddef.h>
11 #include <stdlib.h>
12 #include <urcu.h>
13 #include <common/futex.hpp>
14 #include <common/macros.hpp>
15 #include <common/shm.hpp>
16 #include <common/utils.hpp>
17 #include <sys/stat.h>
18
19 #include "register.hpp"
20 #include "lttng-sessiond.hpp"
21 #include "testpoint.hpp"
22 #include "health-sessiond.hpp"
23 #include "fd-limit.hpp"
24 #include "utils.hpp"
25 #include "thread.hpp"
26
27 namespace {
28 struct thread_state {
29 struct lttng_pipe *quit_pipe;
30 struct ust_cmd_queue *ust_cmd_queue;
31 sem_t ready;
32 bool running;
33 int application_socket;
34 };
35 } /* namespace */
36
37 /*
38 * Creates the application socket.
39 */
40 static int create_application_socket(void)
41 {
42 int ret = 0;
43 int apps_sock;
44 const mode_t old_umask = umask(0);
45
46 /* Create the application unix socket */
47 apps_sock = lttcomm_create_unix_sock(
48 the_config.apps_unix_sock_path.value);
49 if (apps_sock < 0) {
50 ERR("Create unix sock failed: %s",
51 the_config.apps_unix_sock_path.value);
52 ret = -1;
53 goto end;
54 }
55
56 /* Set the cloexec flag */
57 ret = utils_set_fd_cloexec(apps_sock);
58 if (ret < 0) {
59 ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). "
60 "Continuing but note that the consumer daemon will have a "
61 "reference to this socket on exec()", apps_sock);
62 }
63
64 /* File permission MUST be 666 */
65 ret = chmod(the_config.apps_unix_sock_path.value,
66 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH |
67 S_IWOTH);
68 if (ret < 0) {
69 PERROR("Set file permissions failed on %s",
70 the_config.apps_unix_sock_path.value);
71 goto error_close_socket;
72 }
73
74 DBG3("Session daemon application socket created (fd = %d) ", apps_sock);
75 ret = apps_sock;
76 end:
77 umask(old_umask);
78 return ret;
79 error_close_socket:
80 if (close(apps_sock)) {
81 PERROR("Failed to close application socket in error path");
82 }
83 apps_sock = -1;
84 ret = -1;
85 goto end;
86 }
87
88 /*
89 * Notify UST applications using the shm mmap futex.
90 */
91 static int notify_ust_apps(int active, bool is_root)
92 {
93 char *wait_shm_mmap;
94
95 DBG("Notifying applications of session daemon state: %d", active);
96
97 /* See shm.c for this call implying mmap, shm and futex calls */
98 wait_shm_mmap = shm_ust_get_mmap(
99 the_config.wait_shm_path.value, is_root);
100 if (wait_shm_mmap == NULL) {
101 goto error;
102 }
103
104 /* Wake waiting process */
105 futex_wait_update((int32_t *) wait_shm_mmap, active);
106
107 /* Apps notified successfully */
108 return 0;
109
110 error:
111 return -1;
112 }
113
114 static void cleanup_application_registration_thread(void *data)
115 {
116 struct thread_state *thread_state = (struct thread_state *) data;
117
118 if (!data) {
119 return;
120 }
121
122 lttng_pipe_destroy(thread_state->quit_pipe);
123 free(thread_state);
124 }
125
126 static void set_thread_status(struct thread_state *thread_state, bool running)
127 {
128 DBG("Marking application registration thread's state as %s", running ? "running" : "error");
129 thread_state->running = running;
130 sem_post(&thread_state->ready);
131 }
132
133 static bool wait_thread_status(struct thread_state *thread_state)
134 {
135 DBG("Waiting for application registration thread to be ready");
136 sem_wait(&thread_state->ready);
137 if (thread_state->running) {
138 DBG("Application registration thread is ready");
139 } else {
140 ERR("Initialization of application registration thread failed");
141 }
142
143 return thread_state->running;
144 }
145
146 static void thread_init_cleanup(void *data)
147 {
148 struct thread_state *thread_state = (struct thread_state *) data;
149
150 set_thread_status(thread_state, false);
151 }
152
153 /*
154 * This thread manage application registration.
155 */
156 static void *thread_application_registration(void *data)
157 {
158 int sock = -1, i, ret, pollfd, err = -1;
159 uint32_t revents, nb_fd;
160 struct lttng_poll_event events;
161 /*
162 * Gets allocated in this thread, enqueued to a global queue, dequeued
163 * and freed in the manage apps thread.
164 */
165 struct ust_command *ust_cmd = NULL;
166 const bool is_root = (getuid() == 0);
167 struct thread_state *thread_state = (struct thread_state *) data;
168 const int application_socket = thread_state->application_socket;
169 const int quit_pipe_read_fd = lttng_pipe_get_readfd(
170 thread_state->quit_pipe);
171
172 DBG("[thread] Manage application registration started");
173
174 pthread_cleanup_push(thread_init_cleanup, thread_state);
175 health_register(the_health_sessiond, HEALTH_SESSIOND_TYPE_APP_REG);
176
177 ret = lttcomm_listen_unix_sock(application_socket);
178 if (ret < 0) {
179 goto error_listen;
180 }
181
182 /*
183 * Pass 2 as size here for the thread quit pipe and apps_sock. Nothing
184 * more will be added to this poll set.
185 */
186 ret = lttng_poll_create(&events, 2, LTTNG_CLOEXEC);
187 if (ret < 0) {
188 goto error_create_poll;
189 }
190
191 /* Add the application registration socket */
192 ret = lttng_poll_add(&events, application_socket, LPOLLIN | LPOLLRDHUP);
193 if (ret < 0) {
194 goto error_poll_add;
195 }
196
197 /* Add the application registration socket */
198 ret = lttng_poll_add(&events, quit_pipe_read_fd, LPOLLIN | LPOLLRDHUP);
199 if (ret < 0) {
200 goto error_poll_add;
201 }
202
203 set_thread_status(thread_state, true);
204 pthread_cleanup_pop(0);
205
206 if (testpoint(sessiond_thread_registration_apps)) {
207 goto error_poll_add;
208 }
209
210 while (1) {
211 DBG("Accepting application registration");
212
213 /* Inifinite blocking call, waiting for transmission */
214 restart:
215 health_poll_entry();
216 ret = lttng_poll_wait(&events, -1);
217 health_poll_exit();
218 if (ret < 0) {
219 /*
220 * Restart interrupted system call.
221 */
222 if (errno == EINTR) {
223 goto restart;
224 }
225 goto error;
226 }
227
228 nb_fd = ret;
229
230 for (i = 0; i < nb_fd; i++) {
231 health_code_update();
232
233 /* Fetch once the poll data */
234 revents = LTTNG_POLL_GETEV(&events, i);
235 pollfd = LTTNG_POLL_GETFD(&events, i);
236
237 /* Thread quit pipe has been closed. Killing thread. */
238 if (pollfd == quit_pipe_read_fd) {
239 err = 0;
240 goto exit;
241 } else {
242 /* Event on the registration socket */
243 if (revents & LPOLLIN) {
244 sock = lttcomm_accept_unix_sock(application_socket);
245 if (sock < 0) {
246 goto error;
247 }
248
249 /*
250 * Set socket timeout for both receiving and ending.
251 * app_socket_timeout is in seconds, whereas
252 * lttcomm_setsockopt_rcv_timeout and
253 * lttcomm_setsockopt_snd_timeout expect msec as
254 * parameter.
255 */
256 if (the_config.app_socket_timeout >= 0) {
257 (void) lttcomm_setsockopt_rcv_timeout(sock,
258 the_config.app_socket_timeout * 1000);
259 (void) lttcomm_setsockopt_snd_timeout(sock,
260 the_config.app_socket_timeout * 1000);
261 }
262
263 /*
264 * Set the CLOEXEC flag. Return code is useless because
265 * either way, the show must go on.
266 */
267 (void) utils_set_fd_cloexec(sock);
268
269 /* Create UST registration command for enqueuing */
270 ust_cmd = zmalloc<ust_command>();
271 if (ust_cmd == NULL) {
272 PERROR("ust command zmalloc");
273 ret = close(sock);
274 if (ret) {
275 PERROR("close");
276 }
277 sock = -1;
278 goto error;
279 }
280
281 /*
282 * Using message-based transmissions to ensure we don't
283 * have to deal with partially received messages.
284 */
285 ret = lttng_fd_get(LTTNG_FD_APPS, 1);
286 if (ret < 0) {
287 ERR("Exhausted file descriptors allowed for applications.");
288 free(ust_cmd);
289 ret = close(sock);
290 if (ret) {
291 PERROR("close");
292 }
293 sock = -1;
294 continue;
295 }
296
297 health_code_update();
298 ret = ust_app_recv_registration(sock, &ust_cmd->reg_msg);
299 if (ret < 0) {
300 free(ust_cmd);
301 /* Close socket of the application. */
302 ret = close(sock);
303 if (ret) {
304 PERROR("close");
305 }
306 lttng_fd_put(LTTNG_FD_APPS, 1);
307 sock = -1;
308 continue;
309 }
310 health_code_update();
311
312 ust_cmd->sock = sock;
313 sock = -1;
314
315 DBG("UST registration received with pid:%d ppid:%d uid:%d"
316 " gid:%d sock:%d name:%s (version %d.%d)",
317 ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid,
318 ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid,
319 ust_cmd->sock, ust_cmd->reg_msg.name,
320 ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor);
321
322 /*
323 * Lock free enqueue the registration request. The red pill
324 * has been taken! This apps will be part of the *system*.
325 */
326 cds_wfcq_head_ptr_t head;
327 head.h = &thread_state->ust_cmd_queue->head;
328 cds_wfcq_enqueue(head,
329 &thread_state->ust_cmd_queue->tail,
330 &ust_cmd->node);
331
332 /*
333 * Wake the registration queue futex. Implicit memory
334 * barrier with the exchange in cds_wfcq_enqueue.
335 */
336 futex_nto1_wake(&thread_state->ust_cmd_queue->futex);
337 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
338 ERR("Register apps socket poll error");
339 goto error;
340 } else {
341 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
342 goto error;
343 }
344 }
345 }
346 }
347
348 exit:
349 error:
350 /* Notify that the registration thread is gone */
351 notify_ust_apps(0, is_root);
352
353 ret = close(application_socket);
354 if (ret) {
355 PERROR("Failed to close application registration socket");
356 }
357 if (sock >= 0) {
358 ret = close(sock);
359 if (ret) {
360 PERROR("Failed to close application socket");
361 }
362 lttng_fd_put(LTTNG_FD_APPS, 1);
363 }
364 unlink(the_config.apps_unix_sock_path.value);
365
366 error_poll_add:
367 lttng_poll_clean(&events);
368 error_listen:
369 error_create_poll:
370 DBG("UST Registration thread cleanup complete");
371 if (err) {
372 health_error();
373 ERR("Health error occurred in %s", __func__);
374 }
375 health_unregister(the_health_sessiond);
376 return NULL;
377 }
378
379 static bool shutdown_application_registration_thread(void *data)
380 {
381 struct thread_state *thread_state = (struct thread_state *) data;
382 const int write_fd = lttng_pipe_get_writefd(thread_state->quit_pipe);
383
384 return notify_thread_pipe(write_fd) == 1;
385 }
386
387 struct lttng_thread *launch_application_registration_thread(
388 struct ust_cmd_queue *cmd_queue)
389 {
390 int ret;
391 struct lttng_pipe *quit_pipe;
392 struct thread_state *thread_state = NULL;
393 struct lttng_thread *thread = NULL;
394 const bool is_root = (getuid() == 0);
395 int application_socket = -1;
396
397 thread_state = zmalloc<struct thread_state>();
398 if (!thread_state) {
399 goto error_alloc;
400 }
401 quit_pipe = lttng_pipe_open(FD_CLOEXEC);
402 if (!quit_pipe) {
403 goto error;
404 }
405 thread_state->quit_pipe = quit_pipe;
406 thread_state->ust_cmd_queue = cmd_queue;
407 application_socket = create_application_socket();
408 if (application_socket < 0) {
409 goto error;
410 }
411 thread_state->application_socket = application_socket;
412 sem_init(&thread_state->ready, 0, 0);
413
414 thread = lttng_thread_create("UST application registration",
415 thread_application_registration,
416 shutdown_application_registration_thread,
417 cleanup_application_registration_thread,
418 thread_state);
419 if (!thread) {
420 goto error;
421 }
422 /*
423 * The application registration thread now owns the application socket
424 * and the global thread state. The thread state is used to wait for
425 * the thread's status, but its ownership now belongs to the thread.
426 */
427 application_socket = -1;
428 if (!wait_thread_status(thread_state)) {
429 thread_state = NULL;
430 goto error;
431 }
432
433 /* Notify all applications to register. */
434 ret = notify_ust_apps(1, is_root);
435 if (ret < 0) {
436 ERR("Failed to notify applications or create the wait shared memory.\n"
437 "Execution continues but there might be problems for already\n"
438 "running applications that wishes to register.");
439 }
440
441 return thread;
442 error:
443 lttng_thread_put(thread);
444 cleanup_application_registration_thread(thread_state);
445 if (application_socket >= 0) {
446 if (close(application_socket)) {
447 PERROR("Failed to close application registration socket");
448 }
449 }
450 error_alloc:
451 return NULL;
452 }
This page took 0.037628 seconds and 4 git commands to generate.