2 * Copyright (C) 2013 David Goulet <dgoulet@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
10 #include <common/common.hpp>
11 #include <common/utils.hpp>
13 #include "fd-limit.hpp"
14 #include "lttng-sessiond.hpp"
15 #include "notify-apps.hpp"
16 #include "health-sessiond.hpp"
17 #include "testpoint.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
,
46 HEALTH_SESSIOND_TYPE_APP_MANAGE_NOTIFY
);
48 if (testpoint(sessiond_thread_app_manage_notify
)) {
54 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
56 goto error_poll_create
;
59 /* Add notify pipe to the pollset. */
60 ret
= lttng_poll_add(&events
, notifiers
->apps_cmd_notify_pipe_read_fd
,
61 LPOLLIN
| LPOLLRDHUP
);
66 ret
= lttng_poll_add(&events
, thread_quit_pipe_fd
, LPOLLIN
);
74 DBG3("[ust-thread] Manage notify polling");
76 /* Inifinite blocking call, waiting for transmission */
79 ret
= lttng_poll_wait(&events
, -1);
80 DBG3("[ust-thread] Manage notify return from poll on %d fds",
81 LTTNG_POLL_GETNB(&events
));
85 * Restart interrupted system call.
95 for (i
= 0; i
< nb_fd
; i
++) {
98 /* Fetch once the poll data */
99 const auto revents
= LTTNG_POLL_GETEV(&events
, i
);
100 const auto pollfd
= LTTNG_POLL_GETFD(&events
, i
);
102 /* Activity on thread quit pipe, exiting. */
103 if (pollfd
== thread_quit_pipe_fd
) {
104 DBG("Activity on thread quit pipe");
109 if (pollfd
== notifiers
->apps_cmd_notify_pipe_read_fd
) {
110 /* Inspect the apps cmd pipe */
113 if (revents
& LPOLLIN
) {
114 /* Get socket from dispatch thread. */
115 size_ret
= lttng_read(notifiers
->apps_cmd_notify_pipe_read_fd
,
116 &sock
, sizeof(sock
));
117 if (size_ret
< sizeof(sock
)) {
118 PERROR("read apps notify pipe");
121 health_code_update();
123 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLRDHUP
);
126 * It's possible we've reached the max poll fd allowed.
127 * Let's close the socket but continue normal execution.
131 PERROR("close notify socket %d", sock
);
133 lttng_fd_put(LTTNG_FD_APPS
, 1);
136 DBG3("UST thread notify added sock %d to pollset", sock
);
137 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
138 ERR("Apps notify command pipe error");
141 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
146 * At this point, we know that a registered application
147 * triggered the event.
149 if (revents
& (LPOLLIN
| LPOLLPRI
)) {
150 ret
= ust_app_recv_notify(pollfd
);
152 /* Removing from the poll set */
153 ret
= lttng_poll_del(&events
, pollfd
);
158 /* The socket is closed after a grace period here. */
159 ust_app_notify_sock_unregister(pollfd
);
161 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
162 /* Removing from the poll set */
163 ret
= lttng_poll_del(&events
, pollfd
);
168 /* The socket is closed after a grace period here. */
169 ust_app_notify_sock_unregister(pollfd
);
171 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
174 health_code_update();
181 lttng_poll_clean(&events
);
185 DBG("Application notify communication apps thread cleanup complete");
188 ERR("Health error occurred in %s", __func__
);
190 health_unregister(the_health_sessiond
);
191 rcu_thread_offline();
192 rcu_unregister_thread();
196 static bool shutdown_application_notification_thread(void *data
)
198 struct thread_notifiers
*notifiers
= (thread_notifiers
*) data
;
199 const int write_fd
= lttng_pipe_get_writefd(notifiers
->quit_pipe
);
201 return notify_thread_pipe(write_fd
) == 1;
204 static void cleanup_application_notification_thread(void *data
)
206 struct thread_notifiers
*notifiers
= (thread_notifiers
*) data
;
208 lttng_pipe_destroy(notifiers
->quit_pipe
);
212 bool launch_application_notification_thread(int apps_cmd_notify_pipe_read_fd
)
214 struct lttng_thread
*thread
;
215 struct thread_notifiers
*notifiers
;
216 struct lttng_pipe
*quit_pipe
;
218 notifiers
= zmalloc
<thread_notifiers
>();
222 notifiers
->apps_cmd_notify_pipe_read_fd
= apps_cmd_notify_pipe_read_fd
;
224 quit_pipe
= lttng_pipe_open(FD_CLOEXEC
);
228 notifiers
->quit_pipe
= quit_pipe
;
230 thread
= lttng_thread_create("Application notification",
231 thread_application_notification
,
232 shutdown_application_notification_thread
,
233 cleanup_application_notification_thread
,
238 lttng_thread_put(thread
);
241 cleanup_application_notification_thread(notifiers
);