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"
37 struct thread_notifiers
{
38 struct lttng_pipe
*quit_pipe
;
39 struct ust_cmd_queue
*ust_cmd_queue
;
45 * Creates the application socket.
47 static int create_application_socket(void)
51 const mode_t old_umask
= umask(0);
53 /* Create the application unix socket */
54 apps_sock
= lttcomm_create_unix_sock(config
.apps_unix_sock_path
.value
);
56 ERR("Create unix sock failed: %s", config
.apps_unix_sock_path
.value
);
61 /* Set the cloexec flag */
62 ret
= utils_set_fd_cloexec(apps_sock
);
64 ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). "
65 "Continuing but note that the consumer daemon will have a "
66 "reference to this socket on exec()", apps_sock
);
69 /* File permission MUST be 666 */
70 ret
= chmod(config
.apps_unix_sock_path
.value
,
71 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
73 PERROR("Set file permissions failed on %s",
74 config
.apps_unix_sock_path
.value
);
78 DBG3("Session daemon application socket created (fd = %d) ", apps_sock
);
86 * Notify UST applications using the shm mmap futex.
88 static int notify_ust_apps(int active
, bool is_root
)
92 DBG("Notifying applications of session daemon state: %d", active
);
94 /* See shm.c for this call implying mmap, shm and futex calls */
95 wait_shm_mmap
= shm_ust_get_mmap(config
.wait_shm_path
.value
, is_root
);
96 if (wait_shm_mmap
== NULL
) {
100 /* Wake waiting process */
101 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
103 /* Apps notified successfully */
110 static void cleanup_application_registration_thread(void *data
)
112 struct thread_notifiers
*notifiers
= data
;
114 lttng_pipe_destroy(notifiers
->quit_pipe
);
118 static void set_thread_status(struct thread_notifiers
*notifiers
, bool running
)
120 DBG("Marking application registration thread's state as %s", running
? "running" : "error");
121 notifiers
->running
= running
;
122 sem_post(¬ifiers
->ready
);
125 static bool wait_thread_status(struct thread_notifiers
*notifiers
)
127 DBG("Waiting for application registration thread to be ready");
128 sem_wait(¬ifiers
->ready
);
129 if (notifiers
->running
) {
130 DBG("Application registration thread is ready");
132 ERR("Initialization of application registration thread failed");
135 return notifiers
->running
;
138 static void thread_init_cleanup(void *data
)
140 struct thread_notifiers
*notifiers
= data
;
142 set_thread_status(notifiers
, false);
146 * This thread manage application registration.
148 static void *thread_application_registration(void *data
)
150 int sock
= -1, i
, ret
, pollfd
, err
= -1;
152 uint32_t revents
, nb_fd
;
153 struct lttng_poll_event events
;
155 * Gets allocated in this thread, enqueued to a global queue, dequeued
156 * and freed in the manage apps thread.
158 struct ust_command
*ust_cmd
= NULL
;
159 const bool is_root
= (getuid() == 0);
160 struct thread_notifiers
*notifiers
= data
;
161 const int quit_pipe_read_fd
= lttng_pipe_get_readfd(
162 notifiers
->quit_pipe
);
164 DBG("[thread] Manage application registration started");
166 pthread_cleanup_push(thread_init_cleanup
, NULL
);
167 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG
);
169 apps_sock
= create_application_socket();
174 ret
= lttcomm_listen_unix_sock(apps_sock
);
179 set_thread_status(notifiers
, true);
180 pthread_cleanup_pop(0);
182 if (testpoint(sessiond_thread_registration_apps
)) {
183 goto error_create_poll
;
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
, apps_sock
, LPOLLIN
| LPOLLRDHUP
);
201 /* Add the application registration socket */
202 ret
= lttng_poll_add(&events
, quit_pipe_read_fd
, LPOLLIN
| LPOLLRDHUP
);
207 /* Notify all applications to register */
208 ret
= notify_ust_apps(1, is_root
);
210 ERR("Failed to notify applications or create the wait shared memory.\n"
211 "Execution continues but there might be problem for already\n"
212 "running applications that wishes to register.");
216 DBG("Accepting application registration");
218 /* Inifinite blocking call, waiting for transmission */
221 ret
= lttng_poll_wait(&events
, -1);
225 * Restart interrupted system call.
227 if (errno
== EINTR
) {
235 for (i
= 0; i
< nb_fd
; i
++) {
236 health_code_update();
238 /* Fetch once the poll data */
239 revents
= LTTNG_POLL_GETEV(&events
, i
);
240 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
242 /* Thread quit pipe has been closed. Killing thread. */
243 if (pollfd
== quit_pipe_read_fd
) {
247 /* Event on the registration socket */
248 if (revents
& LPOLLIN
) {
249 sock
= lttcomm_accept_unix_sock(apps_sock
);
255 * Set socket timeout for both receiving and ending.
256 * app_socket_timeout is in seconds, whereas
257 * lttcomm_setsockopt_rcv_timeout and
258 * lttcomm_setsockopt_snd_timeout expect msec as
261 if (config
.app_socket_timeout
>= 0) {
262 (void) lttcomm_setsockopt_rcv_timeout(sock
,
263 config
.app_socket_timeout
* 1000);
264 (void) lttcomm_setsockopt_snd_timeout(sock
,
265 config
.app_socket_timeout
* 1000);
269 * Set the CLOEXEC flag. Return code is useless because
270 * either way, the show must go on.
272 (void) utils_set_fd_cloexec(sock
);
274 /* Create UST registration command for enqueuing */
275 ust_cmd
= zmalloc(sizeof(struct ust_command
));
276 if (ust_cmd
== NULL
) {
277 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(¬ifiers
->ust_cmd_queue
->head
,
331 ¬ifiers
->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(¬ifiers
->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 if (apps_sock
>= 0) {
356 ret
= close(apps_sock
);
366 lttng_fd_put(LTTNG_FD_APPS
, 1);
368 unlink(config
.apps_unix_sock_path
.value
);
371 lttng_poll_clean(&events
);
374 DBG("UST Registration thread cleanup complete");
377 ERR("Health error occurred in %s", __func__
);
379 health_unregister(health_sessiond
);
383 static bool shutdown_application_registration_thread(void *data
)
385 struct thread_notifiers
*notifiers
= data
;
386 const int write_fd
= lttng_pipe_get_writefd(notifiers
->quit_pipe
);
388 return notify_thread_pipe(write_fd
) == 1;
391 struct lttng_thread
*launch_application_registration_thread(
392 struct ust_cmd_queue
*cmd_queue
)
394 struct lttng_pipe
*quit_pipe
;
395 struct thread_notifiers
*notifiers
= NULL
;
396 struct lttng_thread
*thread
;
398 notifiers
= zmalloc(sizeof(*notifiers
));
402 quit_pipe
= lttng_pipe_open(FD_CLOEXEC
);
406 notifiers
->quit_pipe
= quit_pipe
;
407 notifiers
->ust_cmd_queue
= cmd_queue
;
408 sem_init(¬ifiers
->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
,
418 if (!wait_thread_status(notifiers
)) {
419 lttng_thread_put(thread
);
424 cleanup_application_registration_thread(notifiers
);