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
;
43 * Creates the application socket.
45 static int create_application_socket(void)
49 const mode_t old_umask
= umask(0);
51 /* Create the application unix socket */
52 apps_sock
= lttcomm_create_unix_sock(config
.apps_unix_sock_path
.value
);
54 ERR("Create unix sock failed: %s", config
.apps_unix_sock_path
.value
);
59 /* Set the cloexec flag */
60 ret
= utils_set_fd_cloexec(apps_sock
);
62 ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). "
63 "Continuing but note that the consumer daemon will have a "
64 "reference to this socket on exec()", apps_sock
);
67 /* File permission MUST be 666 */
68 ret
= chmod(config
.apps_unix_sock_path
.value
,
69 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
71 PERROR("Set file permissions failed on %s",
72 config
.apps_unix_sock_path
.value
);
76 DBG3("Session daemon application socket created (fd = %d) ", apps_sock
);
84 * Notify UST applications using the shm mmap futex.
86 static int notify_ust_apps(int active
, bool is_root
)
90 DBG("Notifying applications of session daemon state: %d", active
);
92 /* See shm.c for this call implying mmap, shm and futex calls */
93 wait_shm_mmap
= shm_ust_get_mmap(config
.wait_shm_path
.value
, is_root
);
94 if (wait_shm_mmap
== NULL
) {
98 /* Wake waiting process */
99 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
101 /* Apps notified successfully */
108 static void cleanup_application_registration_thread(void *data
)
110 struct thread_notifiers
*notifiers
= data
;
112 lttng_pipe_destroy(notifiers
->quit_pipe
);
117 * This thread manage application registration.
119 static void *thread_application_registration(void *data
)
121 int sock
= -1, i
, ret
, pollfd
, err
= -1;
123 uint32_t revents
, nb_fd
;
124 struct lttng_poll_event events
;
126 * Gets allocated in this thread, enqueued to a global queue, dequeued
127 * and freed in the manage apps thread.
129 struct ust_command
*ust_cmd
= NULL
;
130 const bool is_root
= (getuid() == 0);
131 struct thread_notifiers
*notifiers
= data
;
132 const int quit_pipe_read_fd
= lttng_pipe_get_readfd(
133 notifiers
->quit_pipe
);
135 DBG("[thread] Manage application registration started");
137 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG
);
139 if (testpoint(sessiond_thread_registration_apps
)) {
140 goto error_testpoint
;
143 apps_sock
= create_application_socket();
148 ret
= lttcomm_listen_unix_sock(apps_sock
);
154 * Pass 2 as size here for the thread quit pipe and apps_sock. Nothing
155 * more will be added to this poll set.
157 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
159 goto error_create_poll
;
162 /* Add the application registration socket */
163 ret
= lttng_poll_add(&events
, apps_sock
, LPOLLIN
| LPOLLRDHUP
);
168 /* Add the application registration socket */
169 ret
= lttng_poll_add(&events
, quit_pipe_read_fd
, LPOLLIN
| LPOLLRDHUP
);
174 /* Notify all applications to register */
175 ret
= notify_ust_apps(1, is_root
);
177 ERR("Failed to notify applications or create the wait shared memory.\n"
178 "Execution continues but there might be problem for already\n"
179 "running applications that wishes to register.");
183 DBG("Accepting application registration");
185 /* Inifinite blocking call, waiting for transmission */
188 ret
= lttng_poll_wait(&events
, -1);
192 * Restart interrupted system call.
194 if (errno
== EINTR
) {
202 for (i
= 0; i
< nb_fd
; i
++) {
203 health_code_update();
205 /* Fetch once the poll data */
206 revents
= LTTNG_POLL_GETEV(&events
, i
);
207 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
210 /* No activity for this FD (poll implementation). */
214 /* Thread quit pipe has been closed. Killing thread. */
215 if (pollfd
== quit_pipe_read_fd
) {
219 /* Event on the registration socket */
220 if (revents
& LPOLLIN
) {
221 sock
= lttcomm_accept_unix_sock(apps_sock
);
227 * Set socket timeout for both receiving and ending.
228 * app_socket_timeout is in seconds, whereas
229 * lttcomm_setsockopt_rcv_timeout and
230 * lttcomm_setsockopt_snd_timeout expect msec as
233 if (config
.app_socket_timeout
>= 0) {
234 (void) lttcomm_setsockopt_rcv_timeout(sock
,
235 config
.app_socket_timeout
* 1000);
236 (void) lttcomm_setsockopt_snd_timeout(sock
,
237 config
.app_socket_timeout
* 1000);
241 * Set the CLOEXEC flag. Return code is useless because
242 * either way, the show must go on.
244 (void) utils_set_fd_cloexec(sock
);
246 /* Create UST registration command for enqueuing */
247 ust_cmd
= zmalloc(sizeof(struct ust_command
));
248 if (ust_cmd
== NULL
) {
249 PERROR("ust command zmalloc");
258 * Using message-based transmissions to ensure we don't
259 * have to deal with partially received messages.
261 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
263 ERR("Exhausted file descriptors allowed for applications.");
273 health_code_update();
274 ret
= ust_app_recv_registration(sock
, &ust_cmd
->reg_msg
);
277 /* Close socket of the application. */
282 lttng_fd_put(LTTNG_FD_APPS
, 1);
286 health_code_update();
288 ust_cmd
->sock
= sock
;
291 DBG("UST registration received with pid:%d ppid:%d uid:%d"
292 " gid:%d sock:%d name:%s (version %d.%d)",
293 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
294 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
295 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
296 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
299 * Lock free enqueue the registration request. The red pill
300 * has been taken! This apps will be part of the *system*.
302 cds_wfcq_enqueue(¬ifiers
->ust_cmd_queue
->head
,
303 ¬ifiers
->ust_cmd_queue
->tail
,
307 * Wake the registration queue futex. Implicit memory
308 * barrier with the exchange in cds_wfcq_enqueue.
310 futex_nto1_wake(¬ifiers
->ust_cmd_queue
->futex
);
311 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
312 ERR("Register apps socket poll error");
315 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
324 /* Notify that the registration thread is gone */
325 notify_ust_apps(0, is_root
);
327 if (apps_sock
>= 0) {
328 ret
= close(apps_sock
);
338 lttng_fd_put(LTTNG_FD_APPS
, 1);
340 unlink(config
.apps_unix_sock_path
.value
);
343 lttng_poll_clean(&events
);
347 DBG("UST Registration thread cleanup complete");
350 ERR("Health error occurred in %s", __func__
);
352 health_unregister(health_sessiond
);
356 static bool shutdown_application_registration_thread(void *data
)
358 struct thread_notifiers
*notifiers
= data
;
359 const int write_fd
= lttng_pipe_get_writefd(notifiers
->quit_pipe
);
361 return notify_thread_pipe(write_fd
) == 1;
364 bool launch_application_registration_thread(
365 struct ust_cmd_queue
*cmd_queue
)
367 struct lttng_pipe
*quit_pipe
;
368 struct thread_notifiers
*notifiers
= NULL
;
369 struct lttng_thread
*thread
;
371 quit_pipe
= lttng_pipe_open(FD_CLOEXEC
);
376 notifiers
= zmalloc(sizeof(*notifiers
));
380 notifiers
->quit_pipe
= quit_pipe
;
381 notifiers
->ust_cmd_queue
= cmd_queue
;
383 thread
= lttng_thread_create("UST application registration",
384 thread_application_registration
,
385 shutdown_application_registration_thread
,
386 cleanup_application_registration_thread
,
391 lttng_thread_put(thread
);
394 cleanup_application_registration_thread(notifiers
);