2 * Copyright (C) 2011 EfficiOS Inc.
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * Copyright (C) 2013 Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * SPDX-License-Identifier: GPL-2.0-only
10 #include "manage-apps.hpp"
11 #include "testpoint.hpp"
12 #include "health-sessiond.hpp"
17 struct thread_notifiers
{
18 struct lttng_pipe
*quit_pipe
;
19 int apps_cmd_pipe_read_fd
;
23 static void cleanup_application_management_thread(void *data
)
25 struct thread_notifiers
*notifiers
= (thread_notifiers
*) data
;
27 lttng_pipe_destroy(notifiers
->quit_pipe
);
32 * This thread receives application command sockets (FDs) on the
33 * apps_cmd_pipe and waits (polls) on them until they are closed
36 * At that point, it flushes the data (tracing and metadata) associated
37 * with this application and tears down ust app sessions and other
38 * associated data structures through ust_app_unregister().
40 * Note that this thread never sends commands to the applications
41 * through the command sockets; it merely listens for hang-ups
42 * and errors on those sockets and cleans-up as they occur.
44 static void *thread_application_management(void *data
)
46 int i
, ret
, pollfd
, err
= -1;
48 uint32_t revents
, nb_fd
;
49 struct lttng_poll_event events
;
50 struct thread_notifiers
*notifiers
= (thread_notifiers
*) data
;
51 const int quit_pipe_read_fd
= lttng_pipe_get_readfd(
52 notifiers
->quit_pipe
);
54 DBG("[thread] Manage application started");
56 rcu_register_thread();
59 health_register(the_health_sessiond
, HEALTH_SESSIOND_TYPE_APP_MANAGE
);
61 if (testpoint(sessiond_thread_manage_apps
)) {
67 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
69 goto error_poll_create
;
72 ret
= lttng_poll_add(&events
, notifiers
->apps_cmd_pipe_read_fd
,
73 LPOLLIN
| LPOLLRDHUP
);
78 ret
= lttng_poll_add(&events
, quit_pipe_read_fd
, LPOLLIN
| LPOLLERR
);
83 if (testpoint(sessiond_thread_manage_apps_before_loop
)) {
90 DBG("Apps thread polling");
92 /* Inifinite blocking call, waiting for transmission */
95 ret
= lttng_poll_wait(&events
, -1);
96 DBG("Apps thread return from poll on %d fds",
97 LTTNG_POLL_GETNB(&events
));
101 * Restart interrupted system call.
103 if (errno
== EINTR
) {
111 for (i
= 0; i
< nb_fd
; i
++) {
112 /* Fetch once the poll data */
113 revents
= LTTNG_POLL_GETEV(&events
, i
);
114 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
116 health_code_update();
118 if (pollfd
== quit_pipe_read_fd
) {
121 } else if (pollfd
== notifiers
->apps_cmd_pipe_read_fd
) {
122 /* Inspect the apps cmd pipe */
123 if (revents
& LPOLLIN
) {
127 size_ret
= lttng_read(
128 notifiers
->apps_cmd_pipe_read_fd
,
129 &sock
, sizeof(sock
));
130 if (size_ret
< sizeof(sock
)) {
131 PERROR("read apps cmd pipe");
135 health_code_update();
138 * Since this is a command socket (write then read),
139 * we only monitor the error events of the socket.
141 ret
= lttng_poll_add(&events
, sock
,
142 LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
147 DBG("Apps with sock %d added to poll set", sock
);
148 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
149 ERR("Apps command pipe error");
152 ERR("Unknown poll events %u for sock %d", revents
, pollfd
);
157 * At this point, we know that a registered application made
158 * the event at poll_wait.
160 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
161 /* Removing from the poll set */
162 ret
= lttng_poll_del(&events
, pollfd
);
167 /* Socket closed on remote end. */
168 ust_app_unregister(pollfd
);
170 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
175 health_code_update();
181 lttng_poll_clean(&events
);
186 * We don't clean the UST app hash table here since already registered
187 * applications can still be controlled so let them be until the session
188 * daemon dies or the applications stop.
193 ERR("Health error occurred in %s", __func__
);
195 health_unregister(the_health_sessiond
);
196 DBG("Application communication apps thread cleanup complete");
197 rcu_thread_offline();
198 rcu_unregister_thread();
202 static bool shutdown_application_management_thread(void *data
)
204 struct thread_notifiers
*notifiers
= (thread_notifiers
*) data
;
205 const int write_fd
= lttng_pipe_get_writefd(notifiers
->quit_pipe
);
207 return notify_thread_pipe(write_fd
) == 1;
210 bool launch_application_management_thread(int apps_cmd_pipe_read_fd
)
212 struct lttng_pipe
*quit_pipe
;
213 struct thread_notifiers
*notifiers
= NULL
;
214 struct lttng_thread
*thread
;
216 notifiers
= zmalloc
<thread_notifiers
>();
220 quit_pipe
= lttng_pipe_open(FD_CLOEXEC
);
224 notifiers
->quit_pipe
= quit_pipe
;
225 notifiers
->apps_cmd_pipe_read_fd
= apps_cmd_pipe_read_fd
;
227 thread
= lttng_thread_create("UST application management",
228 thread_application_management
,
229 shutdown_application_management_thread
,
230 cleanup_application_management_thread
,
236 lttng_thread_put(thread
);
239 cleanup_application_management_thread(notifiers
);