2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
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.h"
11 #include "testpoint.h"
12 #include "health-sessiond.h"
16 struct thread_notifiers
{
17 struct lttng_pipe
*quit_pipe
;
18 int apps_cmd_pipe_read_fd
;
21 static void cleanup_application_management_thread(void *data
)
23 struct thread_notifiers
*notifiers
= (thread_notifiers
*) data
;
25 lttng_pipe_destroy(notifiers
->quit_pipe
);
30 * This thread receives application command sockets (FDs) on the
31 * apps_cmd_pipe and waits (polls) on them until they are closed
34 * At that point, it flushes the data (tracing and metadata) associated
35 * with this application and tears down ust app sessions and other
36 * associated data structures through ust_app_unregister().
38 * Note that this thread never sends commands to the applications
39 * through the command sockets; it merely listens for hang-ups
40 * and errors on those sockets and cleans-up as they occur.
42 static void *thread_application_management(void *data
)
44 int i
, ret
, pollfd
, err
= -1;
46 uint32_t revents
, nb_fd
;
47 struct lttng_poll_event events
;
48 struct thread_notifiers
*notifiers
= (thread_notifiers
*) data
;
49 const int quit_pipe_read_fd
= lttng_pipe_get_readfd(
50 notifiers
->quit_pipe
);
52 DBG("[thread] Manage application started");
54 rcu_register_thread();
57 health_register(the_health_sessiond
, HEALTH_SESSIOND_TYPE_APP_MANAGE
);
59 if (testpoint(sessiond_thread_manage_apps
)) {
65 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
67 goto error_poll_create
;
70 ret
= lttng_poll_add(&events
, notifiers
->apps_cmd_pipe_read_fd
,
71 LPOLLIN
| LPOLLRDHUP
);
76 ret
= lttng_poll_add(&events
, quit_pipe_read_fd
, LPOLLIN
| LPOLLERR
);
81 if (testpoint(sessiond_thread_manage_apps_before_loop
)) {
88 DBG("Apps thread polling");
90 /* Inifinite blocking call, waiting for transmission */
93 ret
= lttng_poll_wait(&events
, -1);
94 DBG("Apps thread return from poll on %d fds",
95 LTTNG_POLL_GETNB(&events
));
99 * Restart interrupted system call.
101 if (errno
== EINTR
) {
109 for (i
= 0; i
< nb_fd
; i
++) {
110 /* Fetch once the poll data */
111 revents
= LTTNG_POLL_GETEV(&events
, i
);
112 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
114 health_code_update();
116 if (pollfd
== quit_pipe_read_fd
) {
119 } else if (pollfd
== notifiers
->apps_cmd_pipe_read_fd
) {
120 /* Inspect the apps cmd pipe */
121 if (revents
& LPOLLIN
) {
125 size_ret
= lttng_read(
126 notifiers
->apps_cmd_pipe_read_fd
,
127 &sock
, sizeof(sock
));
128 if (size_ret
< sizeof(sock
)) {
129 PERROR("read apps cmd pipe");
133 health_code_update();
136 * Since this is a command socket (write then read),
137 * we only monitor the error events of the socket.
139 ret
= lttng_poll_add(&events
, sock
,
140 LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
145 DBG("Apps with sock %d added to poll set", sock
);
146 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
147 ERR("Apps command pipe error");
150 ERR("Unknown poll events %u for sock %d", revents
, pollfd
);
155 * At this point, we know that a registered application made
156 * the event at poll_wait.
158 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
159 /* Removing from the poll set */
160 ret
= lttng_poll_del(&events
, pollfd
);
165 /* Socket closed on remote end. */
166 ust_app_unregister(pollfd
);
168 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
173 health_code_update();
179 lttng_poll_clean(&events
);
184 * We don't clean the UST app hash table here since already registered
185 * applications can still be controlled so let them be until the session
186 * daemon dies or the applications stop.
191 ERR("Health error occurred in %s", __func__
);
193 health_unregister(the_health_sessiond
);
194 DBG("Application communication apps thread cleanup complete");
195 rcu_thread_offline();
196 rcu_unregister_thread();
200 static bool shutdown_application_management_thread(void *data
)
202 struct thread_notifiers
*notifiers
= (thread_notifiers
*) data
;
203 const int write_fd
= lttng_pipe_get_writefd(notifiers
->quit_pipe
);
205 return notify_thread_pipe(write_fd
) == 1;
208 bool launch_application_management_thread(int apps_cmd_pipe_read_fd
)
210 struct lttng_pipe
*quit_pipe
;
211 struct thread_notifiers
*notifiers
= NULL
;
212 struct lttng_thread
*thread
;
214 notifiers
= (thread_notifiers
*) zmalloc(sizeof(*notifiers
));
218 quit_pipe
= lttng_pipe_open(FD_CLOEXEC
);
222 notifiers
->quit_pipe
= quit_pipe
;
223 notifiers
->apps_cmd_pipe_read_fd
= apps_cmd_pipe_read_fd
;
225 thread
= lttng_thread_create("UST application management",
226 thread_application_management
,
227 shutdown_application_management_thread
,
228 cleanup_application_management_thread
,
234 lttng_thread_put(thread
);
237 cleanup_application_management_thread(notifiers
);