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
13 #include <common/futex.hpp>
14 #include <common/macros.hpp>
15 #include <common/shm.hpp>
16 #include <common/utils.hpp>
19 #include "register.hpp"
20 #include "lttng-sessiond.hpp"
21 #include "testpoint.hpp"
22 #include "health-sessiond.hpp"
23 #include "fd-limit.hpp"
29 struct lttng_pipe
*quit_pipe
;
30 struct ust_cmd_queue
*ust_cmd_queue
;
33 int application_socket
;
38 * Creates the application socket.
40 static int create_application_socket(void)
44 const mode_t old_umask
= umask(0);
46 /* Create the application unix socket */
47 apps_sock
= lttcomm_create_unix_sock(
48 the_config
.apps_unix_sock_path
.value
);
50 ERR("Create unix sock failed: %s",
51 the_config
.apps_unix_sock_path
.value
);
56 /* Set the cloexec flag */
57 ret
= utils_set_fd_cloexec(apps_sock
);
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
);
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
|
69 PERROR("Set file permissions failed on %s",
70 the_config
.apps_unix_sock_path
.value
);
71 goto error_close_socket
;
74 DBG3("Session daemon application socket created (fd = %d) ", apps_sock
);
80 if (close(apps_sock
)) {
81 PERROR("Failed to close application socket in error path");
89 * Notify UST applications using the shm mmap futex.
91 static int notify_ust_apps(int active
, bool is_root
)
95 DBG("Notifying applications of session daemon state: %d", active
);
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
) {
104 /* Wake waiting process */
105 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
107 /* Apps notified successfully */
114 static void cleanup_application_registration_thread(void *data
)
116 struct thread_state
*thread_state
= (struct thread_state
*) data
;
122 lttng_pipe_destroy(thread_state
->quit_pipe
);
126 static void set_thread_status(struct thread_state
*thread_state
, bool running
)
128 DBG("Marking application registration thread's state as %s", running
? "running" : "error");
129 thread_state
->running
= running
;
130 sem_post(&thread_state
->ready
);
133 static bool wait_thread_status(struct thread_state
*thread_state
)
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");
140 ERR("Initialization of application registration thread failed");
143 return thread_state
->running
;
146 static void thread_init_cleanup(void *data
)
148 struct thread_state
*thread_state
= (struct thread_state
*) data
;
150 set_thread_status(thread_state
, false);
154 * This thread manage application registration.
156 static void *thread_application_registration(void *data
)
158 int sock
= -1, i
, ret
, pollfd
, err
= -1;
159 uint32_t revents
, nb_fd
;
160 struct lttng_poll_event events
;
162 * Gets allocated in this thread, enqueued to a global queue, dequeued
163 * and freed in the manage apps thread.
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
);
172 DBG("[thread] Manage application registration started");
174 pthread_cleanup_push(thread_init_cleanup
, thread_state
);
175 health_register(the_health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG
);
177 ret
= lttcomm_listen_unix_sock(application_socket
);
183 * Pass 2 as size here for the thread quit pipe and apps_sock. Nothing
184 * more will be added to this poll set.
186 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
188 goto error_create_poll
;
191 /* Add the application registration socket */
192 ret
= lttng_poll_add(&events
, application_socket
, LPOLLIN
| LPOLLRDHUP
);
197 /* Add the application registration socket */
198 ret
= lttng_poll_add(&events
, quit_pipe_read_fd
, LPOLLIN
| LPOLLRDHUP
);
203 set_thread_status(thread_state
, true);
204 pthread_cleanup_pop(0);
206 if (testpoint(sessiond_thread_registration_apps
)) {
211 DBG("Accepting application registration");
213 /* Inifinite blocking call, waiting for transmission */
216 ret
= lttng_poll_wait(&events
, -1);
220 * Restart interrupted system call.
222 if (errno
== EINTR
) {
230 for (i
= 0; i
< nb_fd
; i
++) {
231 health_code_update();
233 /* Fetch once the poll data */
234 revents
= LTTNG_POLL_GETEV(&events
, i
);
235 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
237 /* Thread quit pipe has been closed. Killing thread. */
238 if (pollfd
== quit_pipe_read_fd
) {
242 /* Event on the registration socket */
243 if (revents
& LPOLLIN
) {
244 sock
= lttcomm_accept_unix_sock(application_socket
);
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
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);
264 * Set the CLOEXEC flag. Return code is useless because
265 * either way, the show must go on.
267 (void) utils_set_fd_cloexec(sock
);
269 /* Create UST registration command for enqueuing */
270 ust_cmd
= zmalloc
<ust_command
>();
271 if (ust_cmd
== NULL
) {
272 PERROR("ust command zmalloc");
282 * Using message-based transmissions to ensure we don't
283 * have to deal with partially received messages.
285 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
287 ERR("Exhausted file descriptors allowed for applications.");
297 health_code_update();
298 ret
= ust_app_recv_registration(sock
, &ust_cmd
->reg_msg
);
301 /* Close socket of the application. */
306 lttng_fd_put(LTTNG_FD_APPS
, 1);
310 health_code_update();
312 ust_cmd
->sock
= sock
;
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
);
323 * Lock free enqueue the registration request. The red pill
324 * has been taken! This apps will be part of the *system*.
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
,
333 * Wake the registration queue futex. Implicit memory
334 * barrier with the exchange in cds_wfcq_enqueue.
336 futex_nto1_wake(&thread_state
->ust_cmd_queue
->futex
);
337 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
338 ERR("Register apps socket poll error");
341 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
350 /* Notify that the registration thread is gone */
351 notify_ust_apps(0, is_root
);
353 ret
= close(application_socket
);
355 PERROR("Failed to close application registration socket");
360 PERROR("Failed to close application socket");
362 lttng_fd_put(LTTNG_FD_APPS
, 1);
364 unlink(the_config
.apps_unix_sock_path
.value
);
367 lttng_poll_clean(&events
);
370 DBG("UST Registration thread cleanup complete");
373 ERR("Health error occurred in %s", __func__
);
375 health_unregister(the_health_sessiond
);
379 static bool shutdown_application_registration_thread(void *data
)
381 struct thread_state
*thread_state
= (struct thread_state
*) data
;
382 const int write_fd
= lttng_pipe_get_writefd(thread_state
->quit_pipe
);
384 return notify_thread_pipe(write_fd
) == 1;
387 struct lttng_thread
*launch_application_registration_thread(
388 struct ust_cmd_queue
*cmd_queue
)
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;
397 thread_state
= zmalloc
<struct thread_state
>();
401 quit_pipe
= lttng_pipe_open(FD_CLOEXEC
);
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) {
411 thread_state
->application_socket
= application_socket
;
412 sem_init(&thread_state
->ready
, 0, 0);
414 thread
= lttng_thread_create("UST application registration",
415 thread_application_registration
,
416 shutdown_application_registration_thread
,
417 cleanup_application_registration_thread
,
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.
427 application_socket
= -1;
428 if (!wait_thread_status(thread_state
)) {
433 /* Notify all applications to register. */
434 ret
= notify_ust_apps(1, is_root
);
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.");
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");