2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include <common/futex.h>
24 #include <common/macros.h>
25 #include <common/utils.h>
29 #include "lttng-sessiond.h"
30 #include "testpoint.h"
31 #include "health-sessiond.h"
38 struct lttng_pipe
*quit_pipe
;
39 struct ust_cmd_queue
*ust_cmd_queue
;
42 int application_socket
;
46 * Creates the application socket.
48 static int create_application_socket(void)
52 const mode_t old_umask
= umask(0);
54 /* Create the application unix socket */
55 apps_sock
= lttcomm_create_unix_sock(config
.apps_unix_sock_path
.value
);
57 ERR("Create unix sock failed: %s", config
.apps_unix_sock_path
.value
);
62 /* Set the cloexec flag */
63 ret
= utils_set_fd_cloexec(apps_sock
);
65 ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). "
66 "Continuing but note that the consumer daemon will have a "
67 "reference to this socket on exec()", apps_sock
);
70 /* File permission MUST be 666 */
71 ret
= chmod(config
.apps_unix_sock_path
.value
,
72 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
74 PERROR("Set file permissions failed on %s",
75 config
.apps_unix_sock_path
.value
);
76 goto error_close_socket
;
79 DBG3("Session daemon application socket created (fd = %d) ", apps_sock
);
85 if (close(apps_sock
)) {
86 PERROR("Failed to close application socket in error path");
94 * Notify UST applications using the shm mmap futex.
96 static int notify_ust_apps(int active
, bool is_root
)
100 DBG("Notifying applications of session daemon state: %d", active
);
102 /* See shm.c for this call implying mmap, shm and futex calls */
103 wait_shm_mmap
= shm_ust_get_mmap(config
.wait_shm_path
.value
, is_root
);
104 if (wait_shm_mmap
== NULL
) {
108 /* Wake waiting process */
109 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
111 /* Apps notified successfully */
118 static void cleanup_application_registration_thread(void *data
)
120 struct thread_state
*thread_state
= data
;
126 lttng_pipe_destroy(thread_state
->quit_pipe
);
130 static void set_thread_status(struct thread_state
*thread_state
, bool running
)
132 DBG("Marking application registration thread's state as %s", running
? "running" : "error");
133 thread_state
->running
= running
;
134 sem_post(&thread_state
->ready
);
137 static bool wait_thread_status(struct thread_state
*thread_state
)
139 DBG("Waiting for application registration thread to be ready");
140 sem_wait(&thread_state
->ready
);
141 if (thread_state
->running
) {
142 DBG("Application registration thread is ready");
144 ERR("Initialization of application registration thread failed");
147 return thread_state
->running
;
150 static void thread_init_cleanup(void *data
)
152 struct thread_state
*thread_state
= data
;
154 set_thread_status(thread_state
, false);
158 * This thread manage application registration.
160 static void *thread_application_registration(void *data
)
162 int sock
= -1, i
, ret
, pollfd
, err
= -1;
163 uint32_t revents
, nb_fd
;
164 struct lttng_poll_event events
;
166 * Gets allocated in this thread, enqueued to a global queue, dequeued
167 * and freed in the manage apps thread.
169 struct ust_command
*ust_cmd
= NULL
;
170 const bool is_root
= (getuid() == 0);
171 struct thread_state
*thread_state
= data
;
172 const int application_socket
= thread_state
->application_socket
;
173 const int quit_pipe_read_fd
= lttng_pipe_get_readfd(
174 thread_state
->quit_pipe
);
176 DBG("[thread] Manage application registration started");
178 pthread_cleanup_push(thread_init_cleanup
, thread_state
);
179 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG
);
181 ret
= lttcomm_listen_unix_sock(application_socket
);
187 * Pass 2 as size here for the thread quit pipe and apps_sock. Nothing
188 * more will be added to this poll set.
190 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
192 goto error_create_poll
;
195 /* Add the application registration socket */
196 ret
= lttng_poll_add(&events
, application_socket
, LPOLLIN
| LPOLLRDHUP
);
201 /* Add the application registration socket */
202 ret
= lttng_poll_add(&events
, quit_pipe_read_fd
, LPOLLIN
| LPOLLRDHUP
);
207 set_thread_status(thread_state
, true);
208 pthread_cleanup_pop(0);
210 if (testpoint(sessiond_thread_registration_apps
)) {
215 DBG("Accepting application registration");
217 /* Inifinite blocking call, waiting for transmission */
220 ret
= lttng_poll_wait(&events
, -1);
224 * Restart interrupted system call.
226 if (errno
== EINTR
) {
234 for (i
= 0; i
< nb_fd
; i
++) {
235 health_code_update();
237 /* Fetch once the poll data */
238 revents
= LTTNG_POLL_GETEV(&events
, i
);
239 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
241 /* Thread quit pipe has been closed. Killing thread. */
242 if (pollfd
== quit_pipe_read_fd
) {
246 /* Event on the registration socket */
247 if (revents
& LPOLLIN
) {
248 sock
= lttcomm_accept_unix_sock(application_socket
);
254 * Set socket timeout for both receiving and ending.
255 * app_socket_timeout is in seconds, whereas
256 * lttcomm_setsockopt_rcv_timeout and
257 * lttcomm_setsockopt_snd_timeout expect msec as
260 if (config
.app_socket_timeout
>= 0) {
261 (void) lttcomm_setsockopt_rcv_timeout(sock
,
262 config
.app_socket_timeout
* 1000);
263 (void) lttcomm_setsockopt_snd_timeout(sock
,
264 config
.app_socket_timeout
* 1000);
268 * Set the CLOEXEC flag. Return code is useless because
269 * either way, the show must go on.
271 (void) utils_set_fd_cloexec(sock
);
273 /* Create UST registration command for enqueuing */
274 ust_cmd
= zmalloc(sizeof(struct ust_command
));
275 if (ust_cmd
== NULL
) {
276 PERROR("ust command zmalloc");
286 * Using message-based transmissions to ensure we don't
287 * have to deal with partially received messages.
289 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
291 ERR("Exhausted file descriptors allowed for applications.");
301 health_code_update();
302 ret
= ust_app_recv_registration(sock
, &ust_cmd
->reg_msg
);
305 /* Close socket of the application. */
310 lttng_fd_put(LTTNG_FD_APPS
, 1);
314 health_code_update();
316 ust_cmd
->sock
= sock
;
319 DBG("UST registration received with pid:%d ppid:%d uid:%d"
320 " gid:%d sock:%d name:%s (version %d.%d)",
321 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
322 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
323 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
324 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
327 * Lock free enqueue the registration request. The red pill
328 * has been taken! This apps will be part of the *system*.
330 cds_wfcq_enqueue(&thread_state
->ust_cmd_queue
->head
,
331 &thread_state
->ust_cmd_queue
->tail
,
335 * Wake the registration queue futex. Implicit memory
336 * barrier with the exchange in cds_wfcq_enqueue.
338 futex_nto1_wake(&thread_state
->ust_cmd_queue
->futex
);
339 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
340 ERR("Register apps socket poll error");
343 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
352 /* Notify that the registration thread is gone */
353 notify_ust_apps(0, is_root
);
355 ret
= close(application_socket
);
357 PERROR("Failed to close application registration socket");
362 PERROR("Failed to close application socket");
364 lttng_fd_put(LTTNG_FD_APPS
, 1);
366 unlink(config
.apps_unix_sock_path
.value
);
369 lttng_poll_clean(&events
);
372 DBG("UST Registration thread cleanup complete");
375 ERR("Health error occurred in %s", __func__
);
377 health_unregister(health_sessiond
);
381 static bool shutdown_application_registration_thread(void *data
)
383 struct thread_state
*thread_state
= data
;
384 const int write_fd
= lttng_pipe_get_writefd(thread_state
->quit_pipe
);
386 return notify_thread_pipe(write_fd
) == 1;
389 struct lttng_thread
*launch_application_registration_thread(
390 struct ust_cmd_queue
*cmd_queue
)
393 struct lttng_pipe
*quit_pipe
;
394 struct thread_state
*thread_state
= NULL
;
395 struct lttng_thread
*thread
= NULL
;
396 const bool is_root
= (getuid() == 0);
397 int application_socket
= -1;
399 thread_state
= zmalloc(sizeof(*thread_state
));
403 quit_pipe
= lttng_pipe_open(FD_CLOEXEC
);
407 thread_state
->quit_pipe
= quit_pipe
;
408 thread_state
->ust_cmd_queue
= cmd_queue
;
409 application_socket
= create_application_socket();
410 if (application_socket
< 0) {
413 thread_state
->application_socket
= application_socket
;
414 sem_init(&thread_state
->ready
, 0, 0);
416 thread
= lttng_thread_create("UST application registration",
417 thread_application_registration
,
418 shutdown_application_registration_thread
,
419 cleanup_application_registration_thread
,
425 * The application registration thread now owns the application socket
426 * and the global thread state. The thread state is used to wait for
427 * the thread's status, but its ownership now belongs to the thread.
429 application_socket
= -1;
430 if (!wait_thread_status(thread_state
)) {
435 /* Notify all applications to register. */
436 ret
= notify_ust_apps(1, is_root
);
438 ERR("Failed to notify applications or create the wait shared memory.\n"
439 "Execution continues but there might be problems for already\n"
440 "running applications that wishes to register.");
445 lttng_thread_put(thread
);
446 cleanup_application_registration_thread(thread_state
);
447 if (application_socket
>= 0) {
448 if (close(application_socket
)) {
449 PERROR("Failed to close application registration socket");