2 * Copyright (C) 2013 David Goulet <dgoulet@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
11 #include <common/common.h>
12 #include <common/utils.h>
15 #include "lttng-sessiond.h"
16 #include "notify-apps.h"
17 #include "health-sessiond.h"
18 #include "testpoint.h"
22 struct thread_notifiers
{
23 struct lttng_pipe
*quit_pipe
;
24 int apps_cmd_notify_pipe_read_fd
;
28 * This thread manage application notify communication.
30 static void *thread_application_notification(void *data
)
32 int i
, ret
, pollfd
, err
= -1;
34 uint32_t revents
, nb_fd
;
35 struct lttng_poll_event events
;
36 struct thread_notifiers
*notifiers
= data
;
37 const int quit_pipe_read_fd
= lttng_pipe_get_readfd(notifiers
->quit_pipe
);
39 DBG("[ust-thread] Manage application notify command");
41 rcu_register_thread();
44 health_register(health_sessiond
,
45 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. */
59 ret
= lttng_poll_add(&events
, notifiers
->apps_cmd_notify_pipe_read_fd
,
60 LPOLLIN
| LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
65 ret
= lttng_poll_add(&events
, quit_pipe_read_fd
,
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 revents
= LTTNG_POLL_GETEV(&events
, i
);
100 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
102 /* Thread quit pipe has been closed. Killing thread. */
103 if (pollfd
== quit_pipe_read_fd
) {
106 } else if (pollfd
== notifiers
->apps_cmd_notify_pipe_read_fd
) {
107 /* Inspect the apps cmd pipe */
110 if (revents
& LPOLLIN
) {
111 /* Get socket from dispatch thread. */
112 size_ret
= lttng_read(notifiers
->apps_cmd_notify_pipe_read_fd
,
113 &sock
, sizeof(sock
));
114 if (size_ret
< sizeof(sock
)) {
115 PERROR("read apps notify pipe");
118 health_code_update();
120 ret
= lttng_poll_add(&events
, sock
,
121 LPOLLIN
| LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
124 * It's possible we've reached the max poll fd allowed.
125 * Let's close the socket but continue normal execution.
129 PERROR("close notify socket %d", sock
);
131 lttng_fd_put(LTTNG_FD_APPS
, 1);
134 DBG3("UST thread notify added sock %d to pollset", sock
);
135 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
136 ERR("Apps notify command pipe error");
139 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
144 * At this point, we know that a registered application
145 * triggered the event.
147 if (revents
& (LPOLLIN
| LPOLLPRI
)) {
148 ret
= ust_app_recv_notify(pollfd
);
150 /* Removing from the poll set */
151 ret
= lttng_poll_del(&events
, pollfd
);
156 /* The socket is closed after a grace period here. */
157 ust_app_notify_sock_unregister(pollfd
);
159 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
160 /* Removing from the poll set */
161 ret
= lttng_poll_del(&events
, pollfd
);
166 /* The socket is closed after a grace period here. */
167 ust_app_notify_sock_unregister(pollfd
);
169 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
172 health_code_update();
179 lttng_poll_clean(&events
);
183 DBG("Application notify communication apps thread cleanup complete");
186 ERR("Health error occurred in %s", __func__
);
188 health_unregister(health_sessiond
);
189 rcu_thread_offline();
190 rcu_unregister_thread();
194 static bool shutdown_application_notification_thread(void *data
)
196 struct thread_notifiers
*notifiers
= data
;
197 const int write_fd
= lttng_pipe_get_writefd(notifiers
->quit_pipe
);
199 return notify_thread_pipe(write_fd
) == 1;
202 static void cleanup_application_notification_thread(void *data
)
204 struct thread_notifiers
*notifiers
= data
;
206 lttng_pipe_destroy(notifiers
->quit_pipe
);
210 bool launch_application_notification_thread(int apps_cmd_notify_pipe_read_fd
)
212 struct lttng_thread
*thread
;
213 struct thread_notifiers
*notifiers
;
214 struct lttng_pipe
*quit_pipe
;
216 notifiers
= zmalloc(sizeof(*notifiers
));
220 notifiers
->apps_cmd_notify_pipe_read_fd
= apps_cmd_notify_pipe_read_fd
;
222 quit_pipe
= lttng_pipe_open(FD_CLOEXEC
);
226 notifiers
->quit_pipe
= quit_pipe
;
228 thread
= lttng_thread_create("Application notification",
229 thread_application_notification
,
230 shutdown_application_notification_thread
,
231 cleanup_application_notification_thread
,
236 lttng_thread_put(thread
);
239 cleanup_application_notification_thread(notifiers
);