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.
20 #include <common/common.h>
21 #include <common/utils.h>
24 #include "lttng-sessiond.h"
25 #include "ust-thread.h"
26 #include "health-sessiond.h"
27 #include "testpoint.h"
30 * This thread manage application notify communication.
32 void *ust_thread_manage_notify(void *data
)
34 int i
, ret
, pollfd
, err
= -1;
36 uint32_t revents
, nb_fd
;
37 struct lttng_poll_event events
;
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
= sessiond_set_thread_pollset(&events
, 2);
55 goto error_poll_create
;
58 /* Add notify pipe to the pollset. */
59 ret
= lttng_poll_add(&events
, apps_cmd_notify_pipe
[0],
60 LPOLLIN
| LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
68 DBG3("[ust-thread] Manage notify polling");
70 /* Inifinite blocking call, waiting for transmission */
73 ret
= lttng_poll_wait(&events
, -1);
74 DBG3("[ust-thread] Manage notify return from poll on %d fds",
75 LTTNG_POLL_GETNB(&events
));
79 * Restart interrupted system call.
89 for (i
= 0; i
< nb_fd
; i
++) {
92 /* Fetch once the poll data */
93 revents
= LTTNG_POLL_GETEV(&events
, i
);
94 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
97 /* No activity for this FD (poll implementation). */
101 /* Thread quit pipe has been closed. Killing thread. */
102 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
108 /* Inspect the apps cmd pipe */
109 if (pollfd
== apps_cmd_notify_pipe
[0]) {
112 if (revents
& LPOLLIN
) {
113 /* Get socket from dispatch thread. */
114 size_ret
= lttng_read(apps_cmd_notify_pipe
[0],
115 &sock
, sizeof(sock
));
116 if (size_ret
< sizeof(sock
)) {
117 PERROR("read apps notify pipe");
120 health_code_update();
122 ret
= lttng_poll_add(&events
, sock
,
123 LPOLLIN
| LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
126 * It's possible we've reached the max poll fd allowed.
127 * Let's close the socket but continue normal execution.
131 PERROR("close notify socket %d", sock
);
133 lttng_fd_put(LTTNG_FD_APPS
, 1);
136 DBG3("UST thread notify added sock %d to pollset", sock
);
137 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
138 ERR("Apps notify command pipe error");
141 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
146 * At this point, we know that a registered application
147 * triggered the event.
149 if (revents
& (LPOLLIN
| LPOLLPRI
)) {
150 ret
= ust_app_recv_notify(pollfd
);
152 /* Removing from the poll set */
153 ret
= lttng_poll_del(&events
, pollfd
);
158 /* The socket is closed after a grace period here. */
159 ust_app_notify_sock_unregister(pollfd
);
161 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
162 /* Removing from the poll set */
163 ret
= lttng_poll_del(&events
, pollfd
);
168 /* The socket is closed after a grace period here. */
169 ust_app_notify_sock_unregister(pollfd
);
171 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
174 health_code_update();
181 lttng_poll_clean(&events
);
184 utils_close_pipe(apps_cmd_notify_pipe
);
185 apps_cmd_notify_pipe
[0] = apps_cmd_notify_pipe
[1] = -1;
186 DBG("Application notify communication apps thread cleanup complete");
189 ERR("Health error occurred in %s", __func__
);
191 health_unregister(health_sessiond
);
192 rcu_thread_offline();
193 rcu_unregister_thread();