Cleanup: ust_session_id unused by buffer_reg_uid_consumer_channel_key
[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
6c1c0768 18#define _LGPL_SOURCE
4d076222
DG
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"
6a4e4039 30#include "agent.h"
4d076222 31#include "lttng-sessiond.h"
f20baf8e
DG
32#include "session.h"
33#include "utils.h"
4d076222 34
f28f9e44
JG
35static int agent_tracing_enabled = -1;
36
4d076222
DG
37/*
38 * Note that there is not port here. It's set after this URI is parsed so we
39 * can let the user define a custom one. However, localhost is ALWAYS the
40 * default listening address.
41 */
fa91dc52
MD
42static const char *default_reg_uri =
43 "tcp://" DEFAULT_NETWORK_VIEWER_BIND_ADDRESS;
4d076222 44
f20baf8e 45/*
022d91ba 46 * Update agent application using the given socket. This is done just after
f20baf8e
DG
47 * registration was successful.
48 *
49 * This is a quite heavy call in terms of locking since the session list lock
50 * AND session lock are acquired.
51 */
fefd409b 52static void update_agent_app(struct agent_app *app)
f20baf8e
DG
53{
54 struct ltt_session *session, *stmp;
55 struct ltt_session_list *list;
56
57 list = session_get_list();
58 assert(list);
59
60 session_lock_list();
61 cds_list_for_each_entry_safe(session, stmp, &list->head, list) {
62 session_lock(session);
63 if (session->ust_session) {
fefd409b
DG
64 struct agent *agt;
65
4da703ad 66 rcu_read_lock();
fefd409b
DG
67 agt = trace_ust_find_agent(session->ust_session, app->domain);
68 if (agt) {
69 agent_update(agt, app->sock->fd);
70 }
4da703ad 71 rcu_read_unlock();
f20baf8e
DG
72 }
73 session_unlock(session);
74 }
75 session_unlock_list();
76}
77
4d076222
DG
78/*
79 * Create and init socket from uri.
80 */
81static struct lttcomm_sock *init_tcp_socket(void)
82{
83 int ret;
84 struct lttng_uri *uri = NULL;
85 struct lttcomm_sock *sock = NULL;
2288467f
JG
86 unsigned int port;
87 bool bind_succeeded = false;
4d076222
DG
88
89 /*
90 * This should never fail since the URI is hardcoded and the port is set
91 * before this thread is launched.
92 */
93 ret = uri_parse(default_reg_uri, &uri);
94 assert(ret);
2288467f
JG
95 assert(config.agent_tcp_port.begin > 0);
96 uri->port = config.agent_tcp_port.begin;
4d076222
DG
97
98 sock = lttcomm_alloc_sock_from_uri(uri);
99 uri_free(uri);
100 if (sock == NULL) {
022d91ba 101 ERR("[agent-thread] agent allocating TCP socket");
4d076222
DG
102 goto error;
103 }
104
105 ret = lttcomm_create_sock(sock);
106 if (ret < 0) {
107 goto error;
108 }
109
2288467f
JG
110 for (port = config.agent_tcp_port.begin;
111 port <= config.agent_tcp_port.end; port++) {
112 ret = lttcomm_sock_set_port(sock, (uint16_t) port);
113 if (ret) {
114 ERR("[agent-thread] Failed to set port %u on socket",
115 port);
116 goto error;
117 }
118 DBG3("[agent-thread] Trying to bind on port %u", port);
119 ret = sock->ops->bind(sock);
120 if (!ret) {
121 bind_succeeded = true;
122 break;
123 }
124
125 if (errno == EADDRINUSE) {
126 DBG("Failed to bind to port %u since it is already in use",
127 port);
128 } else {
129 PERROR("Failed to bind to port %u", port);
130 goto error;
131 }
132 }
133
134 if (!bind_succeeded) {
135 if (config.agent_tcp_port.begin == config.agent_tcp_port.end) {
136 WARN("Another process is already using the agent port %i. "
137 "Agent support will be deactivated.",
138 config.agent_tcp_port.begin);
139 goto error;
140 } else {
141 WARN("All ports in the range [%i, %i] are already in use. "
142 "Agent support will be deactivated.",
143 config.agent_tcp_port.begin,
144 config.agent_tcp_port.end);
145 goto error;
146 }
4d076222
DG
147 }
148
149 ret = sock->ops->listen(sock, -1);
150 if (ret < 0) {
151 goto error;
152 }
153
022d91ba 154 DBG("[agent-thread] Listening on TCP port %u and socket %d",
2288467f 155 port, sock->fd);
4d076222
DG
156
157 return sock;
158
159error:
160 if (sock) {
161 lttcomm_destroy_sock(sock);
162 }
163 return NULL;
164}
165
166/*
167 * Close and destroy the given TCP socket.
168 */
169static void destroy_tcp_socket(struct lttcomm_sock *sock)
170{
2288467f
JG
171 int ret;
172 uint16_t port;
173
4d076222
DG
174 assert(sock);
175
2288467f
JG
176 ret = lttcomm_sock_get_port(sock, &port);
177 if (ret) {
178 ERR("[agent-thread] Failed to get port of agent TCP socket");
179 port = 0;
180 }
181
182 DBG3("[agent-thread] Destroy TCP socket on port %" PRIu16,
183 port);
4d076222
DG
184
185 /* This will return gracefully if fd is invalid. */
186 sock->ops->close(sock);
187 lttcomm_destroy_sock(sock);
188}
189
f20baf8e 190/*
022d91ba
DG
191 * Handle a new agent registration using the reg socket. After that, a new
192 * agent application is added to the global hash table and attach to an UST app
1b500e7a 193 * object. If r_app is not NULL, the created app is set to the pointer.
f20baf8e
DG
194 *
195 * Return the new FD created upon accept() on success or else a negative errno
196 * value.
197 */
1b500e7a 198static int handle_registration(struct lttcomm_sock *reg_sock,
022d91ba 199 struct agent_app **r_app)
f20baf8e
DG
200{
201 int ret;
202 pid_t pid;
9474416f 203 uint32_t major_version, minor_version;
f20baf8e 204 ssize_t size;
fefd409b 205 enum lttng_domain_type domain;
022d91ba
DG
206 struct agent_app *app;
207 struct agent_register_msg msg;
f20baf8e
DG
208 struct lttcomm_sock *new_sock;
209
210 assert(reg_sock);
211
212 new_sock = reg_sock->ops->accept(reg_sock);
213 if (!new_sock) {
214 ret = -ENOTCONN;
215 goto error;
216 }
217
218 size = new_sock->ops->recvmsg(new_sock, &msg, sizeof(msg), 0);
219 if (size < sizeof(msg)) {
79865500 220 ret = -EINVAL;
f20baf8e
DG
221 goto error_socket;
222 }
fefd409b 223 domain = be32toh(msg.domain);
f20baf8e 224 pid = be32toh(msg.pid);
9474416f
DG
225 major_version = be32toh(msg.major_version);
226 minor_version = be32toh(msg.minor_version);
227
228 /* Test communication protocol version of the registring agent. */
229 if (major_version != AGENT_MAJOR_VERSION) {
230 ret = -EINVAL;
231 goto error_socket;
232 }
233 if (minor_version != AGENT_MINOR_VERSION) {
234 ret = -EINVAL;
235 goto error_socket;
236 }
f20baf8e 237
fefd409b
DG
238 DBG2("[agent-thread] New registration for pid %d domain %d on socket %d",
239 pid, domain, new_sock->fd);
f20baf8e 240
fefd409b 241 app = agent_create_app(pid, domain, new_sock);
f20baf8e
DG
242 if (!app) {
243 ret = -ENOMEM;
244 goto error_socket;
245 }
246
247 /*
248 * Add before assigning the socket value to the UST app so it can be found
249 * concurrently.
250 */
022d91ba 251 agent_add_app(app);
f20baf8e
DG
252
253 /*
022d91ba
DG
254 * We don't need to attach the agent app to the app. If we ever do so, we
255 * should consider both registration order of agent before app and app
256 * before agent.
f20baf8e 257 */
f20baf8e 258
1b500e7a
DG
259 if (r_app) {
260 *r_app = app;
261 }
262
f20baf8e
DG
263 return new_sock->fd;
264
265error_socket:
266 new_sock->ops->close(new_sock);
267 lttcomm_destroy_sock(new_sock);
268error:
269 return ret;
270}
271
f28f9e44
JG
272bool agent_tracing_is_enabled(void)
273{
274 int enabled;
275
276 enabled = uatomic_read(&agent_tracing_enabled);
277 assert(enabled != -1);
278 return enabled == 1;
279}
280
2288467f
JG
281/*
282 * Write agent TCP port using the rundir.
283 */
284static int write_agent_port(uint16_t port)
285{
286 return utils_create_pid_file((pid_t) port,
287 config.agent_port_file_path.value);
288}
289
4d076222
DG
290/*
291 * This thread manage application notify communication.
292 */
022d91ba 293void *agent_thread_manage_registration(void *data)
4d076222
DG
294{
295 int i, ret, pollfd;
296 uint32_t revents, nb_fd;
297 struct lttng_poll_event events;
298 struct lttcomm_sock *reg_sock;
299
022d91ba 300 DBG("[agent-thread] Manage agent application registration.");
4d076222
DG
301
302 rcu_register_thread();
303 rcu_thread_online();
304
022d91ba
DG
305 /* Agent initialization call MUST be called before starting the thread. */
306 assert(agent_apps_ht_by_sock);
f20baf8e 307
4d076222
DG
308 /* Create pollset with size 2, quit pipe and socket. */
309 ret = sessiond_set_thread_pollset(&events, 2);
310 if (ret < 0) {
04fd2d2e 311 sessiond_notify_ready();
4d076222
DG
312 goto error_poll_create;
313 }
314
315 reg_sock = init_tcp_socket();
2288467f
JG
316 if (reg_sock) {
317 uint16_t port;
318
319 assert(lttcomm_sock_get_port(reg_sock, &port) == 0);
320
321 ret = write_agent_port(port);
322 if (ret) {
323 ERR("[agent-thread] Failed to create agent port file: agent tracing will be unavailable");
324 /* Don't prevent the launch of the sessiond on error. */
325 sessiond_notify_ready();
326 goto error;
327 }
328 } else {
329 /* Don't prevent the launch of the sessiond on error. */
330 sessiond_notify_ready();
331 goto error_tcp_socket;
332 }
f28f9e44
JG
333
334 /*
335 * Signal that the agent thread is ready. The command thread
336 * may start to query whether or not agent tracing is enabled.
337 */
2288467f 338 uatomic_set(&agent_tracing_enabled, 1);
7eac7803 339 sessiond_notify_ready();
4d076222 340
427392b4 341 /* Add TCP socket to poll set. */
4d076222
DG
342 ret = lttng_poll_add(&events, reg_sock->fd,
343 LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP);
344 if (ret < 0) {
345 goto error;
346 }
347
348 while (1) {
546f19b5 349 DBG3("[agent-thread] Manage agent polling");
4d076222
DG
350
351 /* Inifinite blocking call, waiting for transmission */
352restart:
353 ret = lttng_poll_wait(&events, -1);
7fa2082e
MD
354 DBG3("[agent-thread] Manage agent return from poll on %d fds",
355 LTTNG_POLL_GETNB(&events));
4d076222
DG
356 if (ret < 0) {
357 /*
358 * Restart interrupted system call.
359 */
360 if (errno == EINTR) {
361 goto restart;
362 }
363 goto error;
364 }
365 nb_fd = ret;
022d91ba 366 DBG3("[agent-thread] %d fd ready", nb_fd);
4d076222
DG
367
368 for (i = 0; i < nb_fd; i++) {
369 /* Fetch once the poll data */
370 revents = LTTNG_POLL_GETEV(&events, i);
371 pollfd = LTTNG_POLL_GETFD(&events, i);
372
fd20dac9
MD
373 if (!revents) {
374 /* No activity for this FD (poll implementation). */
375 continue;
376 }
377
4d076222
DG
378 /* Thread quit pipe has been closed. Killing thread. */
379 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
380 if (ret) {
381 goto exit;
382 }
383
03e43155 384 if (revents & LPOLLIN) {
f20baf8e 385 int new_fd;
022d91ba 386 struct agent_app *app = NULL;
f20baf8e 387
f20baf8e 388 assert(pollfd == reg_sock->fd);
1b500e7a 389 new_fd = handle_registration(reg_sock, &app);
f20baf8e 390 if (new_fd < 0) {
f20baf8e
DG
391 continue;
392 }
1b500e7a
DG
393 /* Should not have a NULL app on success. */
394 assert(app);
f20baf8e 395
03e43155
MD
396 /*
397 * Since this is a command socket (write then read),
398 * only add poll error event to only detect shutdown.
399 */
f20baf8e
DG
400 ret = lttng_poll_add(&events, new_fd,
401 LPOLLERR | LPOLLHUP | LPOLLRDHUP);
402 if (ret < 0) {
6a4e4039 403 agent_destroy_app_by_sock(new_fd);
f20baf8e
DG
404 continue;
405 }
406
407 /* Update newly registered app. */
fefd409b 408 update_agent_app(app);
1b500e7a
DG
409
410 /* On failure, the poll will detect it and clean it up. */
03e43155
MD
411 ret = agent_send_registration_done(app);
412 if (ret < 0) {
413 /* Removing from the poll set */
414 ret = lttng_poll_del(&events, new_fd);
415 if (ret < 0) {
416 goto error;
417 }
418 agent_destroy_app_by_sock(new_fd);
419 continue;
420 }
421 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
422 /* Removing from the poll set */
423 ret = lttng_poll_del(&events, pollfd);
424 if (ret < 0) {
425 goto error;
426 }
427 agent_destroy_app_by_sock(pollfd);
4d076222 428 } else {
03e43155
MD
429 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
430 goto error;
4d076222
DG
431 }
432 }
433 }
434
435exit:
f20baf8e
DG
436 /* Whatever happens, try to delete it and exit. */
437 (void) lttng_poll_del(&events, reg_sock->fd);
4d076222
DG
438error:
439 destroy_tcp_socket(reg_sock);
440error_tcp_socket:
441 lttng_poll_clean(&events);
442error_poll_create:
2288467f 443 uatomic_set(&agent_tracing_enabled, 0);
022d91ba 444 DBG("[agent-thread] is cleaning up and stopping.");
4d076222
DG
445
446 rcu_thread_offline();
447 rcu_unregister_thread();
448 return NULL;
449}
This page took 0.059773 seconds and 4 git commands to generate.