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.h>
14 #include <common/macros.h>
15 #include <common/utils.h>
19 #include "lttng-sessiond.h"
20 #include "testpoint.h"
21 #include "health-sessiond.h"
28 struct lttng_pipe
*quit_pipe
;
29 struct ust_cmd_queue
*ust_cmd_queue
;
32 int application_socket
;
36 * Creates the application socket.
38 static int create_application_socket(void)
43 /* Create the application unix socket */
44 apps_sock
= lttcomm_create_unix_sock(config
.apps_unix_sock_path
.value
);
46 ERR("Create unix sock failed: %s", config
.apps_unix_sock_path
.value
);
51 /* Set the cloexec flag */
52 ret
= utils_set_fd_cloexec(apps_sock
);
54 ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). "
55 "Continuing but note that the consumer daemon will have a "
56 "reference to this socket on exec()", apps_sock
);
59 /* File permission MUST be 666 */
60 ret
= chmod(config
.apps_unix_sock_path
.value
,
61 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
63 PERROR("Set file permissions failed on %s",
64 config
.apps_unix_sock_path
.value
);
65 goto error_close_socket
;
68 DBG3("Session daemon application socket created (fd = %d) ", apps_sock
);
73 if (close(apps_sock
)) {
74 PERROR("Failed to close application socket in error path");
82 * Notify UST applications using the shm mmap futex.
84 static int notify_ust_apps(int active
, bool is_root
)
88 DBG("Notifying applications of session daemon state: %d", active
);
90 /* See shm.c for this call implying mmap, shm and futex calls */
91 wait_shm_mmap
= shm_ust_get_mmap(config
.wait_shm_path
.value
, is_root
);
92 if (wait_shm_mmap
== NULL
) {
96 /* Wake waiting process */
97 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
99 /* Apps notified successfully */
106 static void cleanup_application_registration_thread(void *data
)
108 struct thread_state
*thread_state
= data
;
114 lttng_pipe_destroy(thread_state
->quit_pipe
);
118 static void set_thread_status(struct thread_state
*thread_state
, bool running
)
120 DBG("Marking application registration thread's state as %s", running
? "running" : "error");
121 thread_state
->running
= running
;
122 sem_post(&thread_state
->ready
);
125 static bool wait_thread_status(struct thread_state
*thread_state
)
127 DBG("Waiting for application registration thread to be ready");
128 sem_wait(&thread_state
->ready
);
129 if (thread_state
->running
) {
130 DBG("Application registration thread is ready");
132 ERR("Initialization of application registration thread failed");
135 return thread_state
->running
;
138 static void thread_init_cleanup(void *data
)
140 struct thread_state
*thread_state
= data
;
142 set_thread_status(thread_state
, false);
146 * This thread manage application registration.
148 static void *thread_application_registration(void *data
)
150 int sock
= -1, i
, ret
, pollfd
, err
= -1;
151 uint32_t revents
, nb_fd
;
152 struct lttng_poll_event events
;
154 * Gets allocated in this thread, enqueued to a global queue, dequeued
155 * and freed in the manage apps thread.
157 struct ust_command
*ust_cmd
= NULL
;
158 const bool is_root
= (getuid() == 0);
159 struct thread_state
*thread_state
= data
;
160 const int application_socket
= thread_state
->application_socket
;
161 const int quit_pipe_read_fd
= lttng_pipe_get_readfd(
162 thread_state
->quit_pipe
);
164 DBG("[thread] Manage application registration started");
166 pthread_cleanup_push(thread_init_cleanup
, thread_state
);
167 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG
);
169 ret
= lttcomm_listen_unix_sock(application_socket
);
175 * Pass 2 as size here for the thread quit pipe and apps_sock. Nothing
176 * more will be added to this poll set.
178 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
180 goto error_create_poll
;
183 /* Add the application registration socket */
184 ret
= lttng_poll_add(&events
, application_socket
, LPOLLIN
| LPOLLRDHUP
);
189 /* Add the application registration socket */
190 ret
= lttng_poll_add(&events
, quit_pipe_read_fd
, LPOLLIN
| LPOLLRDHUP
);
195 set_thread_status(thread_state
, true);
196 pthread_cleanup_pop(0);
198 if (testpoint(sessiond_thread_registration_apps
)) {
203 DBG("Accepting application registration");
205 /* Inifinite blocking call, waiting for transmission */
208 ret
= lttng_poll_wait(&events
, -1);
212 * Restart interrupted system call.
214 if (errno
== EINTR
) {
222 for (i
= 0; i
< nb_fd
; i
++) {
223 health_code_update();
225 /* Fetch once the poll data */
226 revents
= LTTNG_POLL_GETEV(&events
, i
);
227 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
229 /* Thread quit pipe has been closed. Killing thread. */
230 if (pollfd
== quit_pipe_read_fd
) {
234 /* Event on the registration socket */
235 if (revents
& LPOLLIN
) {
236 sock
= lttcomm_accept_unix_sock(application_socket
);
242 * Set socket timeout for both receiving and ending.
243 * app_socket_timeout is in seconds, whereas
244 * lttcomm_setsockopt_rcv_timeout and
245 * lttcomm_setsockopt_snd_timeout expect msec as
248 if (config
.app_socket_timeout
>= 0) {
249 (void) lttcomm_setsockopt_rcv_timeout(sock
,
250 config
.app_socket_timeout
* 1000);
251 (void) lttcomm_setsockopt_snd_timeout(sock
,
252 config
.app_socket_timeout
* 1000);
256 * Set the CLOEXEC flag. Return code is useless because
257 * either way, the show must go on.
259 (void) utils_set_fd_cloexec(sock
);
261 /* Create UST registration command for enqueuing */
262 ust_cmd
= zmalloc(sizeof(struct ust_command
));
263 if (ust_cmd
== NULL
) {
264 PERROR("ust command zmalloc");
274 * Using message-based transmissions to ensure we don't
275 * have to deal with partially received messages.
277 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
279 ERR("Exhausted file descriptors allowed for applications.");
289 health_code_update();
290 ret
= ust_app_recv_registration(sock
, &ust_cmd
->reg_msg
);
293 /* Close socket of the application. */
298 lttng_fd_put(LTTNG_FD_APPS
, 1);
302 health_code_update();
304 ust_cmd
->sock
= sock
;
307 DBG("UST registration received with pid:%d ppid:%d uid:%d"
308 " gid:%d sock:%d name:%s (version %d.%d)",
309 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
310 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
311 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
312 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
315 * Lock free enqueue the registration request. The red pill
316 * has been taken! This apps will be part of the *system*.
318 cds_wfcq_enqueue(&thread_state
->ust_cmd_queue
->head
,
319 &thread_state
->ust_cmd_queue
->tail
,
323 * Wake the registration queue futex. Implicit memory
324 * barrier with the exchange in cds_wfcq_enqueue.
326 futex_nto1_wake(&thread_state
->ust_cmd_queue
->futex
);
327 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
328 ERR("Register apps socket poll error");
331 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
340 /* Notify that the registration thread is gone */
341 notify_ust_apps(0, is_root
);
343 ret
= close(application_socket
);
345 PERROR("Failed to close application registration socket");
350 PERROR("Failed to close application socket");
352 lttng_fd_put(LTTNG_FD_APPS
, 1);
354 unlink(config
.apps_unix_sock_path
.value
);
357 lttng_poll_clean(&events
);
360 DBG("UST Registration thread cleanup complete");
363 ERR("Health error occurred in %s", __func__
);
365 health_unregister(health_sessiond
);
369 static bool shutdown_application_registration_thread(void *data
)
371 struct thread_state
*thread_state
= data
;
372 const int write_fd
= lttng_pipe_get_writefd(thread_state
->quit_pipe
);
374 return notify_thread_pipe(write_fd
) == 1;
377 struct lttng_thread
*launch_application_registration_thread(
378 struct ust_cmd_queue
*cmd_queue
)
381 struct lttng_pipe
*quit_pipe
;
382 struct thread_state
*thread_state
= NULL
;
383 struct lttng_thread
*thread
= NULL
;
384 const bool is_root
= (getuid() == 0);
385 int application_socket
= -1;
387 thread_state
= zmalloc(sizeof(*thread_state
));
391 quit_pipe
= lttng_pipe_open(FD_CLOEXEC
);
395 thread_state
->quit_pipe
= quit_pipe
;
396 thread_state
->ust_cmd_queue
= cmd_queue
;
397 application_socket
= create_application_socket();
398 if (application_socket
< 0) {
401 thread_state
->application_socket
= application_socket
;
402 sem_init(&thread_state
->ready
, 0, 0);
404 thread
= lttng_thread_create("UST application registration",
405 thread_application_registration
,
406 shutdown_application_registration_thread
,
407 cleanup_application_registration_thread
,
413 * The application registration thread now owns the application socket
414 * and the global thread state. The thread state is used to wait for
415 * the thread's status, but its ownership now belongs to the thread.
417 application_socket
= -1;
418 if (!wait_thread_status(thread_state
)) {
423 /* Notify all applications to register. */
424 ret
= notify_ust_apps(1, is_root
);
426 ERR("Failed to notify applications or create the wait shared memory.\n"
427 "Execution continues but there might be problems for already\n"
428 "running applications that wishes to register.");
433 lttng_thread_put(thread
);
434 cleanup_application_registration_thread(thread_state
);
435 if (application_socket
>= 0) {
436 if (close(application_socket
)) {
437 PERROR("Failed to close application registration socket");