2 * Copyright (C) 2013 David Goulet <dgoulet@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
10 #include "fd-limit.hpp"
11 #include "health-sessiond.hpp"
12 #include "lttng-sessiond.hpp"
13 #include "notify-apps.hpp"
14 #include "testpoint.hpp"
18 #include <common/common.hpp>
19 #include <common/utils.hpp>
22 struct thread_notifiers
{
23 struct lttng_pipe
*quit_pipe
;
24 int apps_cmd_notify_pipe_read_fd
;
29 * This thread manage application notify communication.
31 static void *thread_application_notification(void *data
)
36 struct lttng_poll_event events
;
37 struct thread_notifiers
*notifiers
= (thread_notifiers
*) data
;
38 const auto thread_quit_pipe_fd
= lttng_pipe_get_readfd(notifiers
->quit_pipe
);
40 DBG("[ust-thread] Manage application notify command");
42 rcu_register_thread();
45 health_register(the_health_sessiond
, HEALTH_SESSIOND_TYPE_APP_MANAGE_NOTIFY
);
47 if (testpoint(sessiond_thread_app_manage_notify
)) {
53 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
55 goto error_poll_create
;
58 /* Add notify pipe to the pollset. */
60 &events
, notifiers
->apps_cmd_notify_pipe_read_fd
, LPOLLIN
| LPOLLRDHUP
);
65 ret
= lttng_poll_add(&events
, thread_quit_pipe_fd
, LPOLLIN
);
73 DBG3("[ust-thread] Manage notify polling");
75 /* Inifinite blocking call, waiting for transmission */
78 ret
= lttng_poll_wait(&events
, -1);
79 DBG3("[ust-thread] Manage notify return from poll on %d fds",
80 LTTNG_POLL_GETNB(&events
));
84 * Restart interrupted system call.
94 for (i
= 0; i
< nb_fd
; i
++) {
97 /* Fetch once the poll data */
98 const auto revents
= LTTNG_POLL_GETEV(&events
, i
);
99 const auto pollfd
= LTTNG_POLL_GETFD(&events
, i
);
101 /* Activity on thread quit pipe, exiting. */
102 if (pollfd
== thread_quit_pipe_fd
) {
103 DBG("Activity on thread quit pipe");
108 if (pollfd
== notifiers
->apps_cmd_notify_pipe_read_fd
) {
109 /* Inspect the apps cmd pipe */
112 if (revents
& LPOLLIN
) {
113 /* Get socket from dispatch thread. */
115 lttng_read(notifiers
->apps_cmd_notify_pipe_read_fd
,
118 if (size_ret
< sizeof(sock
)) {
119 PERROR("read apps notify pipe");
122 health_code_update();
124 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLRDHUP
);
127 * It's possible we've reached the max poll fd
128 * allowed. Let's close the socket but continue
133 PERROR("close notify socket %d", sock
);
135 lttng_fd_put(LTTNG_FD_APPS
, 1);
138 DBG3("UST thread notify added sock %d to pollset", sock
);
139 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
140 ERR("Apps notify command pipe error");
143 ERR("Unexpected poll events %u for sock %d",
150 * At this point, we know that a registered application
151 * triggered the event.
153 if (revents
& (LPOLLIN
| LPOLLPRI
)) {
154 ret
= ust_app_recv_notify(pollfd
);
156 /* Removing from the poll set */
157 ret
= lttng_poll_del(&events
, pollfd
);
162 /* The socket is closed after a grace period here.
164 ust_app_notify_sock_unregister(pollfd
);
166 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
167 /* Removing from the poll set */
168 ret
= lttng_poll_del(&events
, pollfd
);
173 /* The socket is closed after a grace period here. */
174 ust_app_notify_sock_unregister(pollfd
);
176 ERR("Unexpected poll events %u for sock %d",
181 health_code_update();
188 lttng_poll_clean(&events
);
192 DBG("Application notify communication apps thread cleanup complete");
195 ERR("Health error occurred in %s", __func__
);
197 health_unregister(the_health_sessiond
);
198 rcu_thread_offline();
199 rcu_unregister_thread();
203 static bool shutdown_application_notification_thread(void *data
)
205 struct thread_notifiers
*notifiers
= (thread_notifiers
*) data
;
206 const int write_fd
= lttng_pipe_get_writefd(notifiers
->quit_pipe
);
208 return notify_thread_pipe(write_fd
) == 1;
211 static void cleanup_application_notification_thread(void *data
)
213 struct thread_notifiers
*notifiers
= (thread_notifiers
*) data
;
215 lttng_pipe_destroy(notifiers
->quit_pipe
);
219 bool launch_application_notification_thread(int apps_cmd_notify_pipe_read_fd
)
221 struct lttng_thread
*thread
;
222 struct thread_notifiers
*notifiers
;
223 struct lttng_pipe
*quit_pipe
;
225 notifiers
= zmalloc
<thread_notifiers
>();
229 notifiers
->apps_cmd_notify_pipe_read_fd
= apps_cmd_notify_pipe_read_fd
;
231 quit_pipe
= lttng_pipe_open(FD_CLOEXEC
);
235 notifiers
->quit_pipe
= quit_pipe
;
237 thread
= lttng_thread_create("Application notification",
238 thread_application_notification
,
239 shutdown_application_notification_thread
,
240 cleanup_application_notification_thread
,
245 lttng_thread_put(thread
);
248 cleanup_application_notification_thread(notifiers
);