2 * Copyright (C) 2013 - David Goulet <dgoulet@efficios.com>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include <common/common.h>
22 #include <common/utils.h>
25 #include "lttng-sessiond.h"
26 #include "notify-apps.h"
27 #include "health-sessiond.h"
28 #include "testpoint.h"
32 struct thread_notifiers
{
33 struct lttng_pipe
*quit_pipe
;
34 int apps_cmd_notify_pipe_read_fd
;
38 * This thread manage application notify communication.
40 static void *thread_application_notification(void *data
)
42 int i
, ret
, pollfd
, err
= -1;
44 uint32_t revents
, nb_fd
;
45 struct lttng_poll_event events
;
46 struct thread_notifiers
*notifiers
= data
;
47 const int quit_pipe_read_fd
= lttng_pipe_get_readfd(notifiers
->quit_pipe
);
49 DBG("[ust-thread] Manage application notify command");
51 rcu_register_thread();
54 health_register(health_sessiond
,
55 HEALTH_SESSIOND_TYPE_APP_MANAGE_NOTIFY
);
57 if (testpoint(sessiond_thread_app_manage_notify
)) {
63 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
65 goto error_poll_create
;
68 /* Add notify pipe to the pollset. */
69 ret
= lttng_poll_add(&events
, notifiers
->apps_cmd_notify_pipe_read_fd
,
70 LPOLLIN
| LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
75 ret
= lttng_poll_add(&events
, quit_pipe_read_fd
,
84 DBG3("[ust-thread] Manage notify polling");
86 /* Inifinite blocking call, waiting for transmission */
89 ret
= lttng_poll_wait(&events
, -1);
90 DBG3("[ust-thread] Manage notify return from poll on %d fds",
91 LTTNG_POLL_GETNB(&events
));
95 * Restart interrupted system call.
105 for (i
= 0; i
< nb_fd
; i
++) {
106 health_code_update();
108 /* Fetch once the poll data */
109 revents
= LTTNG_POLL_GETEV(&events
, i
);
110 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
112 /* Thread quit pipe has been closed. Killing thread. */
113 if (pollfd
== quit_pipe_read_fd
) {
116 } else if (pollfd
== notifiers
->apps_cmd_notify_pipe_read_fd
) {
117 /* Inspect the apps cmd pipe */
120 if (revents
& LPOLLIN
) {
121 /* Get socket from dispatch thread. */
122 size_ret
= lttng_read(notifiers
->apps_cmd_notify_pipe_read_fd
,
123 &sock
, sizeof(sock
));
124 if (size_ret
< sizeof(sock
)) {
125 PERROR("read apps notify pipe");
128 health_code_update();
130 ret
= lttng_poll_add(&events
, sock
,
131 LPOLLIN
| LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
134 * It's possible we've reached the max poll fd allowed.
135 * Let's close the socket but continue normal execution.
139 PERROR("close notify socket %d", sock
);
141 lttng_fd_put(LTTNG_FD_APPS
, 1);
144 DBG3("UST thread notify added sock %d to pollset", sock
);
145 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
146 ERR("Apps notify command pipe error");
149 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
154 * At this point, we know that a registered application
155 * triggered the event.
157 if (revents
& (LPOLLIN
| LPOLLPRI
)) {
158 ret
= ust_app_recv_notify(pollfd
);
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 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
170 /* Removing from the poll set */
171 ret
= lttng_poll_del(&events
, pollfd
);
176 /* The socket is closed after a grace period here. */
177 ust_app_notify_sock_unregister(pollfd
);
179 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
182 health_code_update();
189 lttng_poll_clean(&events
);
193 DBG("Application notify communication apps thread cleanup complete");
196 ERR("Health error occurred in %s", __func__
);
198 health_unregister(health_sessiond
);
199 rcu_thread_offline();
200 rcu_unregister_thread();
204 static bool shutdown_application_notification_thread(void *data
)
206 struct thread_notifiers
*notifiers
= data
;
207 const int write_fd
= lttng_pipe_get_writefd(notifiers
->quit_pipe
);
209 return notify_thread_pipe(write_fd
) == 1;
212 static void cleanup_application_notification_thread(void *data
)
214 struct thread_notifiers
*notifiers
= data
;
216 lttng_pipe_destroy(notifiers
->quit_pipe
);
220 bool launch_application_notification_thread(int apps_cmd_notify_pipe_read_fd
)
222 struct lttng_thread
*thread
;
223 struct thread_notifiers
*notifiers
;
224 struct lttng_pipe
*quit_pipe
;
226 notifiers
= zmalloc(sizeof(*notifiers
));
230 notifiers
->apps_cmd_notify_pipe_read_fd
= apps_cmd_notify_pipe_read_fd
;
232 quit_pipe
= lttng_pipe_open(FD_CLOEXEC
);
236 notifiers
->quit_pipe
= quit_pipe
;
238 thread
= lttng_thread_create("Application notification",
239 thread_application_notification
,
240 shutdown_application_notification_thread
,
241 cleanup_application_notification_thread
,
246 lttng_thread_put(thread
);
249 cleanup_application_notification_thread(notifiers
);