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
);
75 goto error_close_socket
;
78 DBG3("Session daemon application socket created (fd = %d) ", apps_sock
);
84 if (close(apps_sock
)) {
85 PERROR("Failed to close application socket in error path");
93 * Notify UST applications using the shm mmap futex.
95 static int notify_ust_apps(int active
, bool is_root
)
99 DBG("Notifying applications of session daemon state: %d", active
);
101 /* See shm.c for this call implying mmap, shm and futex calls */
102 wait_shm_mmap
= shm_ust_get_mmap(config
.wait_shm_path
.value
, is_root
);
103 if (wait_shm_mmap
== NULL
) {
107 /* Wake waiting process */
108 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
110 /* Apps notified successfully */
117 static void cleanup_application_registration_thread(void *data
)
119 struct thread_notifiers
*notifiers
= data
;
121 lttng_pipe_destroy(notifiers
->quit_pipe
);
125 static void set_thread_status(struct thread_notifiers
*notifiers
, bool running
)
127 DBG("Marking application registration thread's state as %s", running
? "running" : "error");
128 notifiers
->running
= running
;
129 sem_post(¬ifiers
->ready
);
132 static bool wait_thread_status(struct thread_notifiers
*notifiers
)
134 DBG("Waiting for application registration thread to be ready");
135 sem_wait(¬ifiers
->ready
);
136 if (notifiers
->running
) {
137 DBG("Application registration thread is ready");
139 ERR("Initialization of application registration thread failed");
142 return notifiers
->running
;
145 static void thread_init_cleanup(void *data
)
147 struct thread_notifiers
*notifiers
= data
;
149 set_thread_status(notifiers
, false);
153 * This thread manage application registration.
155 static void *thread_application_registration(void *data
)
157 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_notifiers
*notifiers
= data
;
168 const int quit_pipe_read_fd
= lttng_pipe_get_readfd(
169 notifiers
->quit_pipe
);
171 DBG("[thread] Manage application registration started");
173 pthread_cleanup_push(thread_init_cleanup
, NULL
);
174 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG
);
176 apps_sock
= create_application_socket();
181 ret
= lttcomm_listen_unix_sock(apps_sock
);
186 set_thread_status(notifiers
, true);
187 pthread_cleanup_pop(0);
189 if (testpoint(sessiond_thread_registration_apps
)) {
190 goto error_create_poll
;
194 * Pass 2 as size here for the thread quit pipe and apps_sock. Nothing
195 * more will be added to this poll set.
197 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
199 goto error_create_poll
;
202 /* Add the application registration socket */
203 ret
= lttng_poll_add(&events
, apps_sock
, LPOLLIN
| LPOLLRDHUP
);
208 /* Add the application registration socket */
209 ret
= lttng_poll_add(&events
, quit_pipe_read_fd
, LPOLLIN
| LPOLLRDHUP
);
214 /* Notify all applications to register */
215 ret
= notify_ust_apps(1, is_root
);
217 ERR("Failed to notify applications or create the wait shared memory.\n"
218 "Execution continues but there might be problem for already\n"
219 "running applications that wishes to register.");
223 DBG("Accepting application registration");
225 /* Inifinite blocking call, waiting for transmission */
228 ret
= lttng_poll_wait(&events
, -1);
232 * Restart interrupted system call.
234 if (errno
== EINTR
) {
242 for (i
= 0; i
< nb_fd
; i
++) {
243 health_code_update();
245 /* Fetch once the poll data */
246 revents
= LTTNG_POLL_GETEV(&events
, i
);
247 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
249 /* Thread quit pipe has been closed. Killing thread. */
250 if (pollfd
== quit_pipe_read_fd
) {
254 /* Event on the registration socket */
255 if (revents
& LPOLLIN
) {
256 sock
= lttcomm_accept_unix_sock(apps_sock
);
262 * Set socket timeout for both receiving and ending.
263 * app_socket_timeout is in seconds, whereas
264 * lttcomm_setsockopt_rcv_timeout and
265 * lttcomm_setsockopt_snd_timeout expect msec as
268 if (config
.app_socket_timeout
>= 0) {
269 (void) lttcomm_setsockopt_rcv_timeout(sock
,
270 config
.app_socket_timeout
* 1000);
271 (void) lttcomm_setsockopt_snd_timeout(sock
,
272 config
.app_socket_timeout
* 1000);
276 * Set the CLOEXEC flag. Return code is useless because
277 * either way, the show must go on.
279 (void) utils_set_fd_cloexec(sock
);
281 /* Create UST registration command for enqueuing */
282 ust_cmd
= zmalloc(sizeof(struct ust_command
));
283 if (ust_cmd
== NULL
) {
284 PERROR("ust command zmalloc");
293 * Using message-based transmissions to ensure we don't
294 * have to deal with partially received messages.
296 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
298 ERR("Exhausted file descriptors allowed for applications.");
308 health_code_update();
309 ret
= ust_app_recv_registration(sock
, &ust_cmd
->reg_msg
);
312 /* Close socket of the application. */
317 lttng_fd_put(LTTNG_FD_APPS
, 1);
321 health_code_update();
323 ust_cmd
->sock
= sock
;
326 DBG("UST registration received with pid:%d ppid:%d uid:%d"
327 " gid:%d sock:%d name:%s (version %d.%d)",
328 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
329 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
330 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
331 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
334 * Lock free enqueue the registration request. The red pill
335 * has been taken! This apps will be part of the *system*.
337 cds_wfcq_enqueue(¬ifiers
->ust_cmd_queue
->head
,
338 ¬ifiers
->ust_cmd_queue
->tail
,
342 * Wake the registration queue futex. Implicit memory
343 * barrier with the exchange in cds_wfcq_enqueue.
345 futex_nto1_wake(¬ifiers
->ust_cmd_queue
->futex
);
346 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
347 ERR("Register apps socket poll error");
350 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
359 /* Notify that the registration thread is gone */
360 notify_ust_apps(0, is_root
);
362 if (apps_sock
>= 0) {
363 ret
= close(apps_sock
);
373 lttng_fd_put(LTTNG_FD_APPS
, 1);
375 unlink(config
.apps_unix_sock_path
.value
);
378 lttng_poll_clean(&events
);
381 DBG("UST Registration thread cleanup complete");
384 ERR("Health error occurred in %s", __func__
);
386 health_unregister(health_sessiond
);
390 static bool shutdown_application_registration_thread(void *data
)
392 struct thread_notifiers
*notifiers
= data
;
393 const int write_fd
= lttng_pipe_get_writefd(notifiers
->quit_pipe
);
395 return notify_thread_pipe(write_fd
) == 1;
398 struct lttng_thread
*launch_application_registration_thread(
399 struct ust_cmd_queue
*cmd_queue
)
401 struct lttng_pipe
*quit_pipe
;
402 struct thread_notifiers
*notifiers
= NULL
;
403 struct lttng_thread
*thread
;
405 notifiers
= zmalloc(sizeof(*notifiers
));
409 quit_pipe
= lttng_pipe_open(FD_CLOEXEC
);
413 notifiers
->quit_pipe
= quit_pipe
;
414 notifiers
->ust_cmd_queue
= cmd_queue
;
415 sem_init(¬ifiers
->ready
, 0, 0);
417 thread
= lttng_thread_create("UST application registration",
418 thread_application_registration
,
419 shutdown_application_registration_thread
,
420 cleanup_application_registration_thread
,
425 if (!wait_thread_status(notifiers
)) {
426 lttng_thread_put(thread
);
431 cleanup_application_registration_thread(notifiers
);