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