Fix: agent may not be ready on launch
[lttng-tools.git] / src / bin / lttng-sessiond / agent-thread.c
1 /*
2 * Copyright (C) 2013 - David Goulet <dgoulet@efficios.com>
3 *
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.
7 *
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
11 * more details.
12 *
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.
16 */
17
18 #define _LGPL_SOURCE
19 #include <assert.h>
20
21 #include <common/common.h>
22 #include <common/sessiond-comm/sessiond-comm.h>
23 #include <common/uri.h>
24 #include <common/utils.h>
25
26 #include <common/compat/endian.h>
27
28 #include "fd-limit.h"
29 #include "agent-thread.h"
30 #include "agent.h"
31 #include "lttng-sessiond.h"
32 #include "session.h"
33 #include "utils.h"
34
35 /*
36 * Note that there is not port here. It's set after this URI is parsed so we
37 * can let the user define a custom one. However, localhost is ALWAYS the
38 * default listening address.
39 */
40 static const char *default_reg_uri =
41 "tcp://" DEFAULT_NETWORK_VIEWER_BIND_ADDRESS;
42
43 /*
44 * Update agent application using the given socket. This is done just after
45 * registration was successful.
46 *
47 * This is a quite heavy call in terms of locking since the session list lock
48 * AND session lock are acquired.
49 */
50 static void update_agent_app(struct agent_app *app)
51 {
52 struct ltt_session *session, *stmp;
53 struct ltt_session_list *list;
54
55 list = session_get_list();
56 assert(list);
57
58 session_lock_list();
59 cds_list_for_each_entry_safe(session, stmp, &list->head, list) {
60 session_lock(session);
61 if (session->ust_session) {
62 struct agent *agt;
63
64 rcu_read_lock();
65 agt = trace_ust_find_agent(session->ust_session, app->domain);
66 if (agt) {
67 agent_update(agt, app->sock->fd);
68 }
69 rcu_read_unlock();
70 }
71 session_unlock(session);
72 }
73 session_unlock_list();
74 }
75
76 /*
77 * Create and init socket from uri.
78 */
79 static struct lttcomm_sock *init_tcp_socket(void)
80 {
81 int ret;
82 struct lttng_uri *uri = NULL;
83 struct lttcomm_sock *sock = NULL;
84
85 /*
86 * This should never fail since the URI is hardcoded and the port is set
87 * before this thread is launched.
88 */
89 ret = uri_parse(default_reg_uri, &uri);
90 assert(ret);
91 assert(config.agent_tcp_port);
92 uri->port = config.agent_tcp_port;
93
94 sock = lttcomm_alloc_sock_from_uri(uri);
95 uri_free(uri);
96 if (sock == NULL) {
97 ERR("[agent-thread] agent allocating TCP socket");
98 goto error;
99 }
100
101 ret = lttcomm_create_sock(sock);
102 if (ret < 0) {
103 goto error;
104 }
105
106 ret = sock->ops->bind(sock);
107 if (ret < 0) {
108 WARN("Another session daemon is using this agent port. Agent support "
109 "will be deactivated to prevent interfering with the tracing.");
110 goto error;
111 }
112
113 ret = sock->ops->listen(sock, -1);
114 if (ret < 0) {
115 goto error;
116 }
117
118 DBG("[agent-thread] Listening on TCP port %u and socket %d",
119 config.agent_tcp_port, sock->fd);
120
121 return sock;
122
123 error:
124 if (sock) {
125 lttcomm_destroy_sock(sock);
126 }
127 return NULL;
128 }
129
130 /*
131 * Close and destroy the given TCP socket.
132 */
133 static void destroy_tcp_socket(struct lttcomm_sock *sock)
134 {
135 assert(sock);
136
137 DBG3("[agent-thread] Destroy TCP socket on port %u", config.agent_tcp_port);
138
139 /* This will return gracefully if fd is invalid. */
140 sock->ops->close(sock);
141 lttcomm_destroy_sock(sock);
142 }
143
144 /*
145 * Handle a new agent registration using the reg socket. After that, a new
146 * agent application is added to the global hash table and attach to an UST app
147 * object. If r_app is not NULL, the created app is set to the pointer.
148 *
149 * Return the new FD created upon accept() on success or else a negative errno
150 * value.
151 */
152 static int handle_registration(struct lttcomm_sock *reg_sock,
153 struct agent_app **r_app)
154 {
155 int ret;
156 pid_t pid;
157 uint32_t major_version, minor_version;
158 ssize_t size;
159 enum lttng_domain_type domain;
160 struct agent_app *app;
161 struct agent_register_msg msg;
162 struct lttcomm_sock *new_sock;
163
164 assert(reg_sock);
165
166 new_sock = reg_sock->ops->accept(reg_sock);
167 if (!new_sock) {
168 ret = -ENOTCONN;
169 goto error;
170 }
171
172 size = new_sock->ops->recvmsg(new_sock, &msg, sizeof(msg), 0);
173 if (size < sizeof(msg)) {
174 ret = -EINVAL;
175 goto error_socket;
176 }
177 domain = be32toh(msg.domain);
178 pid = be32toh(msg.pid);
179 major_version = be32toh(msg.major_version);
180 minor_version = be32toh(msg.minor_version);
181
182 /* Test communication protocol version of the registring agent. */
183 if (major_version != AGENT_MAJOR_VERSION) {
184 ret = -EINVAL;
185 goto error_socket;
186 }
187 if (minor_version != AGENT_MINOR_VERSION) {
188 ret = -EINVAL;
189 goto error_socket;
190 }
191
192 DBG2("[agent-thread] New registration for pid %d domain %d on socket %d",
193 pid, domain, new_sock->fd);
194
195 app = agent_create_app(pid, domain, new_sock);
196 if (!app) {
197 ret = -ENOMEM;
198 goto error_socket;
199 }
200
201 /*
202 * Add before assigning the socket value to the UST app so it can be found
203 * concurrently.
204 */
205 agent_add_app(app);
206
207 /*
208 * We don't need to attach the agent app to the app. If we ever do so, we
209 * should consider both registration order of agent before app and app
210 * before agent.
211 */
212
213 if (r_app) {
214 *r_app = app;
215 }
216
217 return new_sock->fd;
218
219 error_socket:
220 new_sock->ops->close(new_sock);
221 lttcomm_destroy_sock(new_sock);
222 error:
223 return ret;
224 }
225
226 /*
227 * This thread manage application notify communication.
228 */
229 void *agent_thread_manage_registration(void *data)
230 {
231 int i, ret, pollfd;
232 uint32_t revents, nb_fd;
233 struct lttng_poll_event events;
234 struct lttcomm_sock *reg_sock;
235
236 DBG("[agent-thread] Manage agent application registration.");
237
238 rcu_register_thread();
239 rcu_thread_online();
240
241 /* Agent initialization call MUST be called before starting the thread. */
242 assert(agent_apps_ht_by_sock);
243
244 /* Create pollset with size 2, quit pipe and socket. */
245 ret = sessiond_set_thread_pollset(&events, 2);
246 if (ret < 0) {
247 goto error_poll_create;
248 }
249
250 reg_sock = init_tcp_socket();
251 sessiond_notify_ready();
252 if (!reg_sock) {
253 goto error_tcp_socket;
254 }
255
256 /* Add TCP socket to poll set. */
257 ret = lttng_poll_add(&events, reg_sock->fd,
258 LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP);
259 if (ret < 0) {
260 goto error;
261 }
262
263 while (1) {
264 DBG3("[agent-thread] Manage agent polling");
265
266 /* Inifinite blocking call, waiting for transmission */
267 restart:
268 ret = lttng_poll_wait(&events, -1);
269 DBG3("[agent-thread] Manage agent return from poll on %d fds",
270 LTTNG_POLL_GETNB(&events));
271 if (ret < 0) {
272 /*
273 * Restart interrupted system call.
274 */
275 if (errno == EINTR) {
276 goto restart;
277 }
278 goto error;
279 }
280 nb_fd = ret;
281 DBG3("[agent-thread] %d fd ready", nb_fd);
282
283 for (i = 0; i < nb_fd; i++) {
284 /* Fetch once the poll data */
285 revents = LTTNG_POLL_GETEV(&events, i);
286 pollfd = LTTNG_POLL_GETFD(&events, i);
287
288 if (!revents) {
289 /* No activity for this FD (poll implementation). */
290 continue;
291 }
292
293 /* Thread quit pipe has been closed. Killing thread. */
294 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
295 if (ret) {
296 goto exit;
297 }
298
299 if (revents & LPOLLIN) {
300 int new_fd;
301 struct agent_app *app = NULL;
302
303 assert(pollfd == reg_sock->fd);
304 new_fd = handle_registration(reg_sock, &app);
305 if (new_fd < 0) {
306 continue;
307 }
308 /* Should not have a NULL app on success. */
309 assert(app);
310
311 /*
312 * Since this is a command socket (write then read),
313 * only add poll error event to only detect shutdown.
314 */
315 ret = lttng_poll_add(&events, new_fd,
316 LPOLLERR | LPOLLHUP | LPOLLRDHUP);
317 if (ret < 0) {
318 agent_destroy_app_by_sock(new_fd);
319 continue;
320 }
321
322 /* Update newly registered app. */
323 update_agent_app(app);
324
325 /* On failure, the poll will detect it and clean it up. */
326 ret = agent_send_registration_done(app);
327 if (ret < 0) {
328 /* Removing from the poll set */
329 ret = lttng_poll_del(&events, new_fd);
330 if (ret < 0) {
331 goto error;
332 }
333 agent_destroy_app_by_sock(new_fd);
334 continue;
335 }
336 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
337 /* Removing from the poll set */
338 ret = lttng_poll_del(&events, pollfd);
339 if (ret < 0) {
340 goto error;
341 }
342 agent_destroy_app_by_sock(pollfd);
343 } else {
344 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
345 goto error;
346 }
347 }
348 }
349
350 exit:
351 /* Whatever happens, try to delete it and exit. */
352 (void) lttng_poll_del(&events, reg_sock->fd);
353 error:
354 destroy_tcp_socket(reg_sock);
355 error_tcp_socket:
356 lttng_poll_clean(&events);
357 error_poll_create:
358 DBG("[agent-thread] is cleaning up and stopping.");
359
360 rcu_thread_offline();
361 rcu_unregister_thread();
362 return NULL;
363 }
This page took 0.039088 seconds and 6 git commands to generate.