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