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
;
44 * Creates the application socket.
46 static int create_application_socket(void)
50 const mode_t old_umask
= umask(0);
52 /* Create the application unix socket */
53 apps_sock
= lttcomm_create_unix_sock(config
.apps_unix_sock_path
.value
);
55 ERR("Create unix sock failed: %s", config
.apps_unix_sock_path
.value
);
60 /* Set the cloexec flag */
61 ret
= utils_set_fd_cloexec(apps_sock
);
63 ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). "
64 "Continuing but note that the consumer daemon will have a "
65 "reference to this socket on exec()", apps_sock
);
68 /* File permission MUST be 666 */
69 ret
= chmod(config
.apps_unix_sock_path
.value
,
70 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
72 PERROR("Set file permissions failed on %s",
73 config
.apps_unix_sock_path
.value
);
77 DBG3("Session daemon application socket created (fd = %d) ", apps_sock
);
85 * Notify UST applications using the shm mmap futex.
87 static int notify_ust_apps(int active
, bool is_root
)
91 DBG("Notifying applications of session daemon state: %d", active
);
93 /* See shm.c for this call implying mmap, shm and futex calls */
94 wait_shm_mmap
= shm_ust_get_mmap(config
.wait_shm_path
.value
, is_root
);
95 if (wait_shm_mmap
== NULL
) {
99 /* Wake waiting process */
100 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
102 /* Apps notified successfully */
109 static void cleanup_application_registration_thread(void *data
)
111 struct thread_notifiers
*notifiers
= data
;
113 lttng_pipe_destroy(notifiers
->quit_pipe
);
118 void mark_thread_as_ready(struct thread_notifiers
*notifiers
)
120 DBG("Marking application registration thread as ready");
121 sem_post(¬ifiers
->ready
);
125 void wait_until_thread_is_ready(struct thread_notifiers
*notifiers
)
127 DBG("Waiting for application registration thread to be ready");
128 sem_wait(¬ifiers
->ready
);
129 DBG("Application registration thread is ready");
133 * This thread manage application registration.
135 static void *thread_application_registration(void *data
)
137 int sock
= -1, i
, ret
, pollfd
, err
= -1;
139 uint32_t revents
, nb_fd
;
140 struct lttng_poll_event events
;
142 * Gets allocated in this thread, enqueued to a global queue, dequeued
143 * and freed in the manage apps thread.
145 struct ust_command
*ust_cmd
= NULL
;
146 const bool is_root
= (getuid() == 0);
147 struct thread_notifiers
*notifiers
= data
;
148 const int quit_pipe_read_fd
= lttng_pipe_get_readfd(
149 notifiers
->quit_pipe
);
151 DBG("[thread] Manage application registration started");
153 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG
);
155 if (testpoint(sessiond_thread_registration_apps
)) {
156 goto error_testpoint
;
159 apps_sock
= create_application_socket();
164 ret
= lttcomm_listen_unix_sock(apps_sock
);
169 mark_thread_as_ready(notifiers
);
172 * Pass 2 as size here for the thread quit pipe and apps_sock. Nothing
173 * more will be added to this poll set.
175 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
177 goto error_create_poll
;
180 /* Add the application registration socket */
181 ret
= lttng_poll_add(&events
, apps_sock
, LPOLLIN
| LPOLLRDHUP
);
186 /* Add the application registration socket */
187 ret
= lttng_poll_add(&events
, quit_pipe_read_fd
, LPOLLIN
| LPOLLRDHUP
);
192 /* Notify all applications to register */
193 ret
= notify_ust_apps(1, is_root
);
195 ERR("Failed to notify applications or create the wait shared memory.\n"
196 "Execution continues but there might be problem for already\n"
197 "running applications that wishes to register.");
201 DBG("Accepting application registration");
203 /* Inifinite blocking call, waiting for transmission */
206 ret
= lttng_poll_wait(&events
, -1);
210 * Restart interrupted system call.
212 if (errno
== EINTR
) {
220 for (i
= 0; i
< nb_fd
; i
++) {
221 health_code_update();
223 /* Fetch once the poll data */
224 revents
= LTTNG_POLL_GETEV(&events
, i
);
225 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
228 /* No activity for this FD (poll implementation). */
232 /* Thread quit pipe has been closed. Killing thread. */
233 if (pollfd
== quit_pipe_read_fd
) {
237 /* Event on the registration socket */
238 if (revents
& LPOLLIN
) {
239 sock
= lttcomm_accept_unix_sock(apps_sock
);
245 * Set socket timeout for both receiving and ending.
246 * app_socket_timeout is in seconds, whereas
247 * lttcomm_setsockopt_rcv_timeout and
248 * lttcomm_setsockopt_snd_timeout expect msec as
251 if (config
.app_socket_timeout
>= 0) {
252 (void) lttcomm_setsockopt_rcv_timeout(sock
,
253 config
.app_socket_timeout
* 1000);
254 (void) lttcomm_setsockopt_snd_timeout(sock
,
255 config
.app_socket_timeout
* 1000);
259 * Set the CLOEXEC flag. Return code is useless because
260 * either way, the show must go on.
262 (void) utils_set_fd_cloexec(sock
);
264 /* Create UST registration command for enqueuing */
265 ust_cmd
= zmalloc(sizeof(struct ust_command
));
266 if (ust_cmd
== NULL
) {
267 PERROR("ust command zmalloc");
276 * Using message-based transmissions to ensure we don't
277 * have to deal with partially received messages.
279 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
281 ERR("Exhausted file descriptors allowed for applications.");
291 health_code_update();
292 ret
= ust_app_recv_registration(sock
, &ust_cmd
->reg_msg
);
295 /* Close socket of the application. */
300 lttng_fd_put(LTTNG_FD_APPS
, 1);
304 health_code_update();
306 ust_cmd
->sock
= sock
;
309 DBG("UST registration received with pid:%d ppid:%d uid:%d"
310 " gid:%d sock:%d name:%s (version %d.%d)",
311 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
312 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
313 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
314 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
317 * Lock free enqueue the registration request. The red pill
318 * has been taken! This apps will be part of the *system*.
320 cds_wfcq_enqueue(¬ifiers
->ust_cmd_queue
->head
,
321 ¬ifiers
->ust_cmd_queue
->tail
,
325 * Wake the registration queue futex. Implicit memory
326 * barrier with the exchange in cds_wfcq_enqueue.
328 futex_nto1_wake(¬ifiers
->ust_cmd_queue
->futex
);
329 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
330 ERR("Register apps socket poll error");
333 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
342 /* Notify that the registration thread is gone */
343 notify_ust_apps(0, is_root
);
345 if (apps_sock
>= 0) {
346 ret
= close(apps_sock
);
356 lttng_fd_put(LTTNG_FD_APPS
, 1);
358 unlink(config
.apps_unix_sock_path
.value
);
361 lttng_poll_clean(&events
);
365 DBG("UST Registration thread cleanup complete");
368 ERR("Health error occurred in %s", __func__
);
370 health_unregister(health_sessiond
);
374 static bool shutdown_application_registration_thread(void *data
)
376 struct thread_notifiers
*notifiers
= data
;
377 const int write_fd
= lttng_pipe_get_writefd(notifiers
->quit_pipe
);
379 return notify_thread_pipe(write_fd
) == 1;
382 struct lttng_thread
*launch_application_registration_thread(
383 struct ust_cmd_queue
*cmd_queue
)
385 struct lttng_pipe
*quit_pipe
;
386 struct thread_notifiers
*notifiers
= NULL
;
387 struct lttng_thread
*thread
;
389 quit_pipe
= lttng_pipe_open(FD_CLOEXEC
);
394 notifiers
= zmalloc(sizeof(*notifiers
));
398 notifiers
->quit_pipe
= quit_pipe
;
399 notifiers
->ust_cmd_queue
= cmd_queue
;
400 sem_init(¬ifiers
->ready
, 0, 0);
402 thread
= lttng_thread_create("UST application registration",
403 thread_application_registration
,
404 shutdown_application_registration_thread
,
405 cleanup_application_registration_thread
,
410 wait_until_thread_is_ready(notifiers
);
413 cleanup_application_registration_thread(notifiers
);