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();
126 if (pollfd
== quit_pipe_read_fd
) {
129 } else if (pollfd
== notifiers
->apps_cmd_pipe_read_fd
) {
130 /* Inspect the apps cmd pipe */
131 if (revents
& LPOLLIN
) {
135 size_ret
= lttng_read(
136 notifiers
->apps_cmd_pipe_read_fd
,
137 &sock
, sizeof(sock
));
138 if (size_ret
< sizeof(sock
)) {
139 PERROR("read apps cmd pipe");
143 health_code_update();
146 * Since this is a command socket (write then read),
147 * we only monitor the error events of the socket.
149 ret
= lttng_poll_add(&events
, sock
,
150 LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
155 DBG("Apps with sock %d added to poll set", sock
);
156 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
157 ERR("Apps command pipe error");
160 ERR("Unknown poll events %u for sock %d", revents
, pollfd
);
165 * At this point, we know that a registered application made
166 * the event at poll_wait.
168 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
169 /* Removing from the poll set */
170 ret
= lttng_poll_del(&events
, pollfd
);
175 /* Socket closed on remote end. */
176 ust_app_unregister(pollfd
);
178 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
183 health_code_update();
189 lttng_poll_clean(&events
);
194 * We don't clean the UST app hash table here since already registered
195 * applications can still be controlled so let them be until the session
196 * daemon dies or the applications stop.
201 ERR("Health error occurred in %s", __func__
);
203 health_unregister(health_sessiond
);
204 DBG("Application communication apps thread cleanup complete");
205 rcu_thread_offline();
206 rcu_unregister_thread();
210 static bool shutdown_application_management_thread(void *data
)
212 struct thread_notifiers
*notifiers
= data
;
213 const int write_fd
= lttng_pipe_get_writefd(notifiers
->quit_pipe
);
215 return notify_thread_pipe(write_fd
) == 1;
218 bool launch_application_management_thread(int apps_cmd_pipe_read_fd
)
220 struct lttng_pipe
*quit_pipe
;
221 struct thread_notifiers
*notifiers
= NULL
;
222 struct lttng_thread
*thread
;
224 notifiers
= zmalloc(sizeof(*notifiers
));
228 quit_pipe
= lttng_pipe_open(FD_CLOEXEC
);
232 notifiers
->quit_pipe
= quit_pipe
;
233 notifiers
->apps_cmd_pipe_read_fd
= apps_cmd_pipe_read_fd
;
235 thread
= lttng_thread_create("UST application management",
236 thread_application_management
,
237 shutdown_application_management_thread
,
238 cleanup_application_management_thread
,
244 lttng_thread_put(thread
);
247 cleanup_application_management_thread(notifiers
);