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"
21 struct thread_notifiers
{
22 struct lttng_pipe
*quit_pipe
;
23 int apps_cmd_notify_pipe_read_fd
;
27 * This thread manage application notify communication.
29 static void *thread_application_notification(void *data
)
31 int i
, ret
, pollfd
, err
= -1;
33 uint32_t revents
, nb_fd
;
34 struct lttng_poll_event events
;
35 struct thread_notifiers
*notifiers
= (thread_notifiers
*) data
;
36 const int quit_pipe_read_fd
= lttng_pipe_get_readfd(notifiers
->quit_pipe
);
38 DBG("[ust-thread] Manage application notify command");
40 rcu_register_thread();
43 health_register(the_health_sessiond
,
44 HEALTH_SESSIOND_TYPE_APP_MANAGE_NOTIFY
);
46 if (testpoint(sessiond_thread_app_manage_notify
)) {
52 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
54 goto error_poll_create
;
57 /* Add notify pipe to the pollset. */
58 ret
= lttng_poll_add(&events
, notifiers
->apps_cmd_notify_pipe_read_fd
,
59 LPOLLIN
| LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
64 ret
= lttng_poll_add(&events
, quit_pipe_read_fd
,
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 revents
= LTTNG_POLL_GETEV(&events
, i
);
99 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
101 /* Thread quit pipe has been closed. Killing thread. */
102 if (pollfd
== quit_pipe_read_fd
) {
105 } else if (pollfd
== notifiers
->apps_cmd_notify_pipe_read_fd
) {
106 /* Inspect the apps cmd pipe */
109 if (revents
& LPOLLIN
) {
110 /* Get socket from dispatch thread. */
111 size_ret
= lttng_read(notifiers
->apps_cmd_notify_pipe_read_fd
,
112 &sock
, sizeof(sock
));
113 if (size_ret
< sizeof(sock
)) {
114 PERROR("read apps notify pipe");
117 health_code_update();
119 ret
= lttng_poll_add(&events
, sock
,
120 LPOLLIN
| LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
123 * It's possible we've reached the max poll fd allowed.
124 * Let's close the socket but continue normal execution.
128 PERROR("close notify socket %d", sock
);
130 lttng_fd_put(LTTNG_FD_APPS
, 1);
133 DBG3("UST thread notify added sock %d to pollset", sock
);
134 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
135 ERR("Apps notify command pipe error");
138 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
143 * At this point, we know that a registered application
144 * triggered the event.
146 if (revents
& (LPOLLIN
| LPOLLPRI
)) {
147 ret
= ust_app_recv_notify(pollfd
);
149 /* Removing from the poll set */
150 ret
= lttng_poll_del(&events
, pollfd
);
155 /* The socket is closed after a grace period here. */
156 ust_app_notify_sock_unregister(pollfd
);
158 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
159 /* Removing from the poll set */
160 ret
= lttng_poll_del(&events
, pollfd
);
165 /* The socket is closed after a grace period here. */
166 ust_app_notify_sock_unregister(pollfd
);
168 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
171 health_code_update();
178 lttng_poll_clean(&events
);
182 DBG("Application notify communication apps thread cleanup complete");
185 ERR("Health error occurred in %s", __func__
);
187 health_unregister(the_health_sessiond
);
188 rcu_thread_offline();
189 rcu_unregister_thread();
193 static bool shutdown_application_notification_thread(void *data
)
195 struct thread_notifiers
*notifiers
= (thread_notifiers
*) data
;
196 const int write_fd
= lttng_pipe_get_writefd(notifiers
->quit_pipe
);
198 return notify_thread_pipe(write_fd
) == 1;
201 static void cleanup_application_notification_thread(void *data
)
203 struct thread_notifiers
*notifiers
= (thread_notifiers
*) data
;
205 lttng_pipe_destroy(notifiers
->quit_pipe
);
209 bool launch_application_notification_thread(int apps_cmd_notify_pipe_read_fd
)
211 struct lttng_thread
*thread
;
212 struct thread_notifiers
*notifiers
;
213 struct lttng_pipe
*quit_pipe
;
215 notifiers
= zmalloc
<thread_notifiers
>();
219 notifiers
->apps_cmd_notify_pipe_read_fd
= apps_cmd_notify_pipe_read_fd
;
221 quit_pipe
= lttng_pipe_open(FD_CLOEXEC
);
225 notifiers
->quit_pipe
= quit_pipe
;
227 thread
= lttng_thread_create("Application notification",
228 thread_application_notification
,
229 shutdown_application_notification_thread
,
230 cleanup_application_notification_thread
,
235 lttng_thread_put(thread
);
238 cleanup_application_notification_thread(notifiers
);