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.
20 #include "manage-apps.h"
21 #include "testpoint.h"
22 #include "health-sessiond.h"
26 struct thread_notifiers
{
27 struct lttng_pipe
*quit_pipe
;
28 int apps_cmd_pipe_read_fd
;
31 static void cleanup_application_management_thread(void *data
)
33 struct thread_notifiers
*notifiers
= data
;
35 lttng_pipe_destroy(notifiers
->quit_pipe
);
40 * This thread receives application command sockets (FDs) on the
41 * apps_cmd_pipe and waits (polls) on them until they are closed
44 * At that point, it flushes the data (tracing and metadata) associated
45 * with this application and tears down ust app sessions and other
46 * associated data structures through ust_app_unregister().
48 * Note that this thread never sends commands to the applications
49 * through the command sockets; it merely listens for hang-ups
50 * and errors on those sockets and cleans-up as they occur.
52 static void *thread_application_management(void *data
)
54 int i
, ret
, pollfd
, err
= -1;
56 uint32_t revents
, nb_fd
;
57 struct lttng_poll_event events
;
58 struct thread_notifiers
*notifiers
= data
;
59 const int quit_pipe_read_fd
= lttng_pipe_get_readfd(
60 notifiers
->quit_pipe
);
62 DBG("[thread] Manage application started");
64 rcu_register_thread();
67 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_MANAGE
);
69 if (testpoint(sessiond_thread_manage_apps
)) {
75 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
77 goto error_poll_create
;
80 ret
= lttng_poll_add(&events
, notifiers
->apps_cmd_pipe_read_fd
,
81 LPOLLIN
| LPOLLRDHUP
);
86 ret
= lttng_poll_add(&events
, quit_pipe_read_fd
, LPOLLIN
| LPOLLERR
);
91 if (testpoint(sessiond_thread_manage_apps_before_loop
)) {
98 DBG("Apps thread polling");
100 /* Inifinite blocking call, waiting for transmission */
103 ret
= lttng_poll_wait(&events
, -1);
104 DBG("Apps thread return from poll on %d fds",
105 LTTNG_POLL_GETNB(&events
));
109 * Restart interrupted system call.
111 if (errno
== EINTR
) {
119 for (i
= 0; i
< nb_fd
; i
++) {
120 /* Fetch once the poll data */
121 revents
= LTTNG_POLL_GETEV(&events
, i
);
122 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
124 health_code_update();
127 /* No activity for this FD (poll implementation). */
131 if (pollfd
== quit_pipe_read_fd
) {
134 } else if (pollfd
== notifiers
->apps_cmd_pipe_read_fd
) {
135 /* Inspect the apps cmd pipe */
136 if (revents
& LPOLLIN
) {
140 size_ret
= lttng_read(
141 notifiers
->apps_cmd_pipe_read_fd
,
142 &sock
, sizeof(sock
));
143 if (size_ret
< sizeof(sock
)) {
144 PERROR("read apps cmd pipe");
148 health_code_update();
151 * Since this is a command socket (write then read),
152 * we only monitor the error events of the socket.
154 ret
= lttng_poll_add(&events
, sock
,
155 LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
160 DBG("Apps with sock %d added to poll set", sock
);
161 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
162 ERR("Apps command pipe error");
165 ERR("Unknown poll events %u for sock %d", revents
, pollfd
);
170 * At this point, we know that a registered application made
171 * the event at poll_wait.
173 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
174 /* Removing from the poll set */
175 ret
= lttng_poll_del(&events
, pollfd
);
180 /* Socket closed on remote end. */
181 ust_app_unregister(pollfd
);
183 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
188 health_code_update();
194 lttng_poll_clean(&events
);
199 * We don't clean the UST app hash table here since already registered
200 * applications can still be controlled so let them be until the session
201 * daemon dies or the applications stop.
206 ERR("Health error occurred in %s", __func__
);
208 health_unregister(health_sessiond
);
209 DBG("Application communication apps thread cleanup complete");
210 rcu_thread_offline();
211 rcu_unregister_thread();
215 static bool shutdown_application_management_thread(void *data
)
217 struct thread_notifiers
*notifiers
= data
;
218 const int write_fd
= lttng_pipe_get_writefd(notifiers
->quit_pipe
);
220 return notify_thread_pipe(write_fd
) == 1;
223 bool launch_application_management_thread(int apps_cmd_pipe_read_fd
)
225 struct lttng_pipe
*quit_pipe
;
226 struct thread_notifiers
*notifiers
= NULL
;
227 struct lttng_thread
*thread
;
229 notifiers
= zmalloc(sizeof(*notifiers
));
233 quit_pipe
= lttng_pipe_open(FD_CLOEXEC
);
237 notifiers
->quit_pipe
= quit_pipe
;
238 notifiers
->apps_cmd_pipe_read_fd
= apps_cmd_pipe_read_fd
;
240 thread
= lttng_thread_create("UST application management",
241 thread_application_management
,
242 shutdown_application_management_thread
,
243 cleanup_application_management_thread
,
249 lttng_thread_put(thread
);
252 cleanup_application_management_thread(notifiers
);