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
)
33 int i
, ret
, pollfd
, err
= -1;
35 uint32_t revents
, nb_fd
;
36 struct lttng_poll_event events
;
37 struct thread_notifiers
*notifiers
= (thread_notifiers
*) data
;
38 const int quit_pipe_read_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
| LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
66 ret
= lttng_poll_add(&events
, quit_pipe_read_fd
,
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 revents
= LTTNG_POLL_GETEV(&events
, i
);
101 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
103 /* Thread quit pipe has been closed. Killing thread. */
104 if (pollfd
== quit_pipe_read_fd
) {
107 } else if (pollfd
== notifiers
->apps_cmd_notify_pipe_read_fd
) {
108 /* Inspect the apps cmd pipe */
111 if (revents
& LPOLLIN
) {
112 /* Get socket from dispatch thread. */
113 size_ret
= lttng_read(notifiers
->apps_cmd_notify_pipe_read_fd
,
114 &sock
, sizeof(sock
));
115 if (size_ret
< sizeof(sock
)) {
116 PERROR("read apps notify pipe");
119 health_code_update();
121 ret
= lttng_poll_add(&events
, sock
,
122 LPOLLIN
| LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
125 * It's possible we've reached the max poll fd allowed.
126 * Let's close the socket but continue normal execution.
130 PERROR("close notify socket %d", sock
);
132 lttng_fd_put(LTTNG_FD_APPS
, 1);
135 DBG3("UST thread notify added sock %d to pollset", sock
);
136 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
137 ERR("Apps notify command pipe error");
140 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
145 * At this point, we know that a registered application
146 * triggered the event.
148 if (revents
& (LPOLLIN
| LPOLLPRI
)) {
149 ret
= ust_app_recv_notify(pollfd
);
151 /* Removing from the poll set */
152 ret
= lttng_poll_del(&events
, pollfd
);
157 /* The socket is closed after a grace period here. */
158 ust_app_notify_sock_unregister(pollfd
);
160 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
161 /* Removing from the poll set */
162 ret
= lttng_poll_del(&events
, pollfd
);
167 /* The socket is closed after a grace period here. */
168 ust_app_notify_sock_unregister(pollfd
);
170 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
173 health_code_update();
180 lttng_poll_clean(&events
);
184 DBG("Application notify communication apps thread cleanup complete");
187 ERR("Health error occurred in %s", __func__
);
189 health_unregister(the_health_sessiond
);
190 rcu_thread_offline();
191 rcu_unregister_thread();
195 static bool shutdown_application_notification_thread(void *data
)
197 struct thread_notifiers
*notifiers
= (thread_notifiers
*) data
;
198 const int write_fd
= lttng_pipe_get_writefd(notifiers
->quit_pipe
);
200 return notify_thread_pipe(write_fd
) == 1;
203 static void cleanup_application_notification_thread(void *data
)
205 struct thread_notifiers
*notifiers
= (thread_notifiers
*) data
;
207 lttng_pipe_destroy(notifiers
->quit_pipe
);
211 bool launch_application_notification_thread(int apps_cmd_notify_pipe_read_fd
)
213 struct lttng_thread
*thread
;
214 struct thread_notifiers
*notifiers
;
215 struct lttng_pipe
*quit_pipe
;
217 notifiers
= zmalloc
<thread_notifiers
>();
221 notifiers
->apps_cmd_notify_pipe_read_fd
= apps_cmd_notify_pipe_read_fd
;
223 quit_pipe
= lttng_pipe_open(FD_CLOEXEC
);
227 notifiers
->quit_pipe
= quit_pipe
;
229 thread
= lttng_thread_create("Application notification",
230 thread_application_notification
,
231 shutdown_application_notification_thread
,
232 cleanup_application_notification_thread
,
237 lttng_thread_put(thread
);
240 cleanup_application_notification_thread(notifiers
);