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