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>
24 struct thread_notifiers
{
25 struct lttng_pipe
*quit_pipe
;
26 int apps_cmd_notify_pipe_read_fd
;
31 * This thread manage application notify communication.
33 static void *thread_application_notification(void *data
)
38 struct lttng_poll_event events
;
39 struct thread_notifiers
*notifiers
= (thread_notifiers
*) data
;
40 const auto thread_quit_pipe_fd
= lttng_pipe_get_readfd(notifiers
->quit_pipe
);
42 DBG("[ust-thread] Manage application notify command");
44 rcu_register_thread();
47 health_register(the_health_sessiond
, HEALTH_SESSIOND_TYPE_APP_MANAGE_NOTIFY
);
49 if (testpoint(sessiond_thread_app_manage_notify
)) {
55 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
57 goto error_poll_create
;
60 /* Add notify pipe to the pollset. */
62 &events
, notifiers
->apps_cmd_notify_pipe_read_fd
, LPOLLIN
| LPOLLRDHUP
);
67 ret
= lttng_poll_add(&events
, thread_quit_pipe_fd
, LPOLLIN
);
75 DBG3("[ust-thread] Manage notify polling");
77 /* Inifinite blocking call, waiting for transmission */
80 ret
= lttng_poll_wait(&events
, -1);
81 DBG3("[ust-thread] Manage notify return from poll on %d fds",
82 LTTNG_POLL_GETNB(&events
));
86 * Restart interrupted system call.
96 for (i
= 0; i
< nb_fd
; i
++) {
99 /* Fetch once the poll data */
100 const auto revents
= LTTNG_POLL_GETEV(&events
, i
);
101 const auto pollfd
= LTTNG_POLL_GETFD(&events
, i
);
103 /* Activity on thread quit pipe, exiting. */
104 if (pollfd
== thread_quit_pipe_fd
) {
105 DBG("Activity on thread quit pipe");
110 if (pollfd
== notifiers
->apps_cmd_notify_pipe_read_fd
) {
111 /* Inspect the apps cmd pipe */
114 if (revents
& LPOLLIN
) {
115 /* Get socket from dispatch thread. */
117 lttng_read(notifiers
->apps_cmd_notify_pipe_read_fd
,
120 if (size_ret
< sizeof(sock
)) {
121 PERROR("read apps notify pipe");
124 health_code_update();
126 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLRDHUP
);
129 * It's possible we've reached the max poll fd
130 * allowed. Let's close the socket but continue
135 PERROR("close notify socket %d", sock
);
137 lttng_fd_put(LTTNG_FD_APPS
, 1);
140 DBG3("UST thread notify added sock %d to pollset", sock
);
141 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
142 ERR("Apps notify command pipe error");
145 ERR("Unexpected poll events %u for sock %d",
152 * At this point, we know that a registered application
153 * triggered the event.
155 if (revents
& (LPOLLIN
| LPOLLPRI
)) {
156 ret
= ust_app_recv_notify(pollfd
);
158 /* Removing from the poll set */
159 ret
= lttng_poll_del(&events
, pollfd
);
164 /* The socket is closed after a grace period here.
166 ust_app_notify_sock_unregister(pollfd
);
168 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
169 /* Removing from the poll set */
170 ret
= lttng_poll_del(&events
, pollfd
);
175 /* The socket is closed after a grace period here. */
176 ust_app_notify_sock_unregister(pollfd
);
178 ERR("Unexpected poll events %u for sock %d",
183 health_code_update();
190 lttng_poll_clean(&events
);
194 DBG("Application notify communication apps thread cleanup complete");
197 ERR("Health error occurred in %s", __func__
);
199 health_unregister(the_health_sessiond
);
200 rcu_thread_offline();
201 rcu_unregister_thread();
205 static bool shutdown_application_notification_thread(void *data
)
207 struct thread_notifiers
*notifiers
= (thread_notifiers
*) data
;
208 const int write_fd
= lttng_pipe_get_writefd(notifiers
->quit_pipe
);
210 return notify_thread_pipe(write_fd
) == 1;
213 static void cleanup_application_notification_thread(void *data
)
215 struct thread_notifiers
*notifiers
= (thread_notifiers
*) data
;
217 lttng_pipe_destroy(notifiers
->quit_pipe
);
221 bool launch_application_notification_thread(int apps_cmd_notify_pipe_read_fd
)
223 struct lttng_thread
*thread
;
224 struct thread_notifiers
*notifiers
;
225 struct lttng_pipe
*quit_pipe
;
227 notifiers
= zmalloc
<thread_notifiers
>();
231 notifiers
->apps_cmd_notify_pipe_read_fd
= apps_cmd_notify_pipe_read_fd
;
233 quit_pipe
= lttng_pipe_open(FD_CLOEXEC
);
237 notifiers
->quit_pipe
= quit_pipe
;
239 thread
= lttng_thread_create("Application notification",
240 thread_application_notification
,
241 shutdown_application_notification_thread
,
242 cleanup_application_notification_thread
,
247 lttng_thread_put(thread
);
250 cleanup_application_notification_thread(notifiers
);