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>
6 * SPDX-License-Identifier: GPL-2.0-only
10 #include "fd-limit.hpp"
11 #include "health-sessiond.hpp"
12 #include "lttng-sessiond.hpp"
13 #include "register.hpp"
14 #include "testpoint.hpp"
18 #include <common/futex.hpp>
19 #include <common/macros.hpp>
20 #include <common/shm.hpp>
21 #include <common/utils.hpp>
30 struct lttng_pipe
*quit_pipe
;
31 struct ust_cmd_queue
*ust_cmd_queue
;
34 int application_socket
;
39 * Creates the application socket.
41 static int create_application_socket()
46 /* Create the application unix socket */
47 apps_sock
= lttcomm_create_unix_sock(the_config
.apps_unix_sock_path
.value
);
49 ERR("Create unix sock failed: %s", the_config
.apps_unix_sock_path
.value
);
54 /* Set the cloexec flag */
55 ret
= utils_set_fd_cloexec(apps_sock
);
57 ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). "
58 "Continuing but note that the consumer daemon will have a "
59 "reference to this socket on exec()",
63 /* File permission MUST be 666 */
64 ret
= chmod(the_config
.apps_unix_sock_path
.value
,
65 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
67 PERROR("Set file permissions failed on %s", the_config
.apps_unix_sock_path
.value
);
68 goto error_close_socket
;
71 DBG3("Session daemon application socket created (fd = %d) ", apps_sock
);
76 if (close(apps_sock
)) {
77 PERROR("Failed to close application socket in error path");
85 * Notify UST applications using the shm mmap futex.
87 static int notify_ust_apps(int active
, bool is_root
)
91 DBG("Notifying applications of session daemon state: %d", active
);
93 /* See shm.c for this call implying mmap, shm and futex calls */
94 wait_shm_mmap
= shm_ust_get_mmap(the_config
.wait_shm_path
.value
, is_root
);
95 if (wait_shm_mmap
== nullptr) {
99 /* Wake waiting process */
100 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
102 /* Apps notified successfully */
109 static void cleanup_application_registration_thread(void *data
)
111 struct thread_state
*thread_state
= (struct thread_state
*) data
;
117 lttng_pipe_destroy(thread_state
->quit_pipe
);
121 static void set_thread_status(struct thread_state
*thread_state
, bool running
)
123 DBG("Marking application registration thread's state as %s", running
? "running" : "error");
124 thread_state
->running
= running
;
125 sem_post(&thread_state
->ready
);
128 static bool wait_thread_status(struct thread_state
*thread_state
)
130 DBG("Waiting for application registration thread to be ready");
131 sem_wait(&thread_state
->ready
);
132 if (thread_state
->running
) {
133 DBG("Application registration thread is ready");
135 ERR("Initialization of application registration thread failed");
138 return thread_state
->running
;
141 static void thread_init_cleanup(void *data
)
143 struct thread_state
*thread_state
= (struct thread_state
*) data
;
145 set_thread_status(thread_state
, false);
149 * This thread manage application registration.
151 static void *thread_application_registration(void *data
)
153 int sock
= -1, i
, ret
, err
= -1;
155 struct lttng_poll_event events
;
157 * Gets allocated in this thread, enqueued to a global queue, dequeued
158 * and freed in the manage apps thread.
160 struct ust_command
*ust_cmd
= nullptr;
161 const bool is_root
= (getuid() == 0);
162 struct thread_state
*thread_state
= (struct thread_state
*) data
;
163 const int application_socket
= thread_state
->application_socket
;
164 const auto thread_quit_pipe_fd
= lttng_pipe_get_readfd(thread_state
->quit_pipe
);
166 DBG("[thread] Manage application registration started");
168 pthread_cleanup_push(thread_init_cleanup
, thread_state
);
169 health_register(the_health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG
);
171 ret
= lttcomm_listen_unix_sock(application_socket
);
177 * Pass 2 as size here for the thread quit pipe and apps_sock. Nothing
178 * more will be added to this poll set.
180 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
182 goto error_create_poll
;
185 /* Add the application registration socket */
186 ret
= lttng_poll_add(&events
, application_socket
, LPOLLIN
| LPOLLRDHUP
);
191 /* Add the application registration socket */
192 ret
= lttng_poll_add(&events
, thread_quit_pipe_fd
, LPOLLIN
| LPOLLRDHUP
);
197 set_thread_status(thread_state
, true);
198 pthread_cleanup_pop(0);
200 if (testpoint(sessiond_thread_registration_apps
)) {
205 DBG("Accepting application registration");
207 /* Inifinite blocking call, waiting for transmission */
210 ret
= lttng_poll_wait(&events
, -1);
214 * Restart interrupted system call.
216 if (errno
== EINTR
) {
224 for (i
= 0; i
< nb_fd
; i
++) {
225 health_code_update();
227 /* Fetch once the poll data */
228 const auto revents
= LTTNG_POLL_GETEV(&events
, i
);
229 const auto pollfd
= LTTNG_POLL_GETFD(&events
, i
);
231 /* Activity on thread quit pipe, closing. */
232 if (pollfd
== thread_quit_pipe_fd
) {
237 /* Event on the registration socket. */
238 if (revents
& LPOLLIN
) {
239 sock
= lttcomm_accept_unix_sock(application_socket
);
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
251 if (the_config
.app_socket_timeout
>= 0) {
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);
259 * Set the CLOEXEC flag. Return code is useless because
260 * either way, the show must go on.
262 (void) utils_set_fd_cloexec(sock
);
264 /* Create UST registration command for enqueuing */
265 ust_cmd
= zmalloc
<ust_command
>();
266 if (ust_cmd
== nullptr) {
267 PERROR("ust command zmalloc");
277 * Using message-based transmissions to ensure we don't
278 * have to deal with partially received messages.
280 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
282 ERR("Exhausted file descriptors allowed for applications.");
292 health_code_update();
293 ret
= ust_app_recv_registration(sock
, &ust_cmd
->reg_msg
);
296 /* Close socket of the application. */
301 lttng_fd_put(LTTNG_FD_APPS
, 1);
305 health_code_update();
307 ust_cmd
->sock
= sock
;
310 DBG("UST registration received with pid:%d ppid:%d uid:%d"
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
,
317 ust_cmd
->reg_msg
.name
,
318 ust_cmd
->reg_msg
.major
,
319 ust_cmd
->reg_msg
.minor
);
322 * Lock free enqueue the registration request. The red pill
323 * has been taken! This apps will be part of the *system*.
325 cds_wfcq_head_ptr_t head
;
326 head
.h
= &thread_state
->ust_cmd_queue
->head
;
328 head
, &thread_state
->ust_cmd_queue
->tail
, &ust_cmd
->node
);
331 * Wake the registration queue futex. Implicit memory
332 * barrier with the exchange in cds_wfcq_enqueue.
334 futex_nto1_wake(&thread_state
->ust_cmd_queue
->futex
);
335 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
336 ERR("Register apps socket poll error");
339 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
347 /* Notify that the registration thread is gone */
348 notify_ust_apps(0, is_root
);
350 ret
= close(application_socket
);
352 PERROR("Failed to close application registration socket");
357 PERROR("Failed to close application socket");
359 lttng_fd_put(LTTNG_FD_APPS
, 1);
361 unlink(the_config
.apps_unix_sock_path
.value
);
364 lttng_poll_clean(&events
);
367 DBG("UST Registration thread cleanup complete");
370 ERR("Health error occurred in %s", __func__
);
372 health_unregister(the_health_sessiond
);
376 static bool shutdown_application_registration_thread(void *data
)
378 struct thread_state
*thread_state
= (struct thread_state
*) data
;
379 const int write_fd
= lttng_pipe_get_writefd(thread_state
->quit_pipe
);
381 return notify_thread_pipe(write_fd
) == 1;
384 struct lttng_thread
*launch_application_registration_thread(struct ust_cmd_queue
*cmd_queue
)
387 struct lttng_pipe
*quit_pipe
;
388 struct thread_state
*thread_state
= nullptr;
389 struct lttng_thread
*thread
= nullptr;
390 const bool is_root
= (getuid() == 0);
391 int application_socket
= -1;
393 thread_state
= zmalloc
<struct thread_state
>();
397 quit_pipe
= lttng_pipe_open(FD_CLOEXEC
);
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) {
407 thread_state
->application_socket
= application_socket
;
408 sem_init(&thread_state
->ready
, 0, 0);
410 thread
= lttng_thread_create("UST application registration",
411 thread_application_registration
,
412 shutdown_application_registration_thread
,
413 cleanup_application_registration_thread
,
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.
423 application_socket
= -1;
424 if (!wait_thread_status(thread_state
)) {
425 thread_state
= nullptr;
429 /* Notify all applications to register. */
430 ret
= notify_ust_apps(1, is_root
);
432 ERR("Failed to notify applications or create the wait shared memory.\n"
433 "Execution continues but there might be problems for already\n"
434 "running applications that wishes to register.");
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");