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