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 "ust-thread.h"
27 #include "health-sessiond.h"
28 #include "testpoint.h"
31 * This thread manage application notify communication.
33 void *ust_thread_manage_notify(void *data
)
35 int i
, ret
, pollfd
, err
= -1;
37 uint32_t revents
, nb_fd
;
38 struct lttng_poll_event events
;
40 DBG("[ust-thread] Manage application notify command");
42 rcu_register_thread();
45 health_register(health_sessiond
,
46 HEALTH_SESSIOND_TYPE_APP_MANAGE_NOTIFY
);
48 if (testpoint(sessiond_thread_app_manage_notify
)) {
54 ret
= sessiond_set_thread_pollset(&events
, 2);
56 goto error_poll_create
;
59 /* Add notify pipe to the pollset. */
60 ret
= lttng_poll_add(&events
, apps_cmd_notify_pipe
[0], LPOLLIN
| LPOLLERR
);
68 DBG3("[ust-thread] Manage notify polling on %d fds",
69 LTTNG_POLL_GETNB(&events
));
71 /* Inifinite blocking call, waiting for transmission */
74 ret
= lttng_poll_wait(&events
, -1);
78 * Restart interrupted system call.
88 for (i
= 0; i
< nb_fd
; i
++) {
91 /* Fetch once the poll data */
92 revents
= LTTNG_POLL_GETEV(&events
, i
);
93 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
96 /* No activity for this FD (poll implementation). */
100 /* Thread quit pipe has been closed. Killing thread. */
101 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
107 /* Inspect the apps cmd pipe */
108 if (pollfd
== apps_cmd_notify_pipe
[0]) {
111 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
112 ERR("Apps notify command pipe error");
114 } else if (!(revents
& LPOLLIN
)) {
115 /* No POLLIN and not a catched error, stop the thread. */
116 ERR("Notify command pipe failed. revent: %u", revents
);
120 /* Get socket from dispatch thread. */
121 size_ret
= lttng_read(apps_cmd_notify_pipe
[0],
122 &sock
, sizeof(sock
));
123 if (size_ret
< sizeof(sock
)) {
124 PERROR("read apps notify pipe");
127 health_code_update();
129 ret
= lttng_poll_add(&events
, sock
,
130 LPOLLIN
| LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
133 * It's possible we've reached the max poll fd allowed.
134 * Let's close the socket but continue normal execution.
138 PERROR("close notify socket %d", sock
);
140 lttng_fd_put(LTTNG_FD_APPS
, 1);
143 DBG3("UST thread notify added sock %d to pollset", sock
);
146 * At this point, we know that a registered application
147 * triggered the event.
149 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
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
);
158 } else if (revents
& (LPOLLIN
| LPOLLPRI
)) {
159 ret
= ust_app_recv_notify(pollfd
);
162 * If the notification failed either the application is
163 * dead or an internal error happened. In both cases,
164 * we can only continue here. If the application is
165 * dead, an unregistration will follow or else the
166 * application will notice that we are not responding
167 * on that socket and will close it.
172 ERR("Unknown poll events %u for sock %d", revents
, pollfd
);
175 health_code_update();
182 lttng_poll_clean(&events
);
185 utils_close_pipe(apps_cmd_notify_pipe
);
186 apps_cmd_notify_pipe
[0] = apps_cmd_notify_pipe
[1] = -1;
187 DBG("Application notify communication apps thread cleanup complete");
190 ERR("Health error occurred in %s", __func__
);
192 health_unregister(health_sessiond
);
193 rcu_thread_offline();
194 rcu_unregister_thread();