Refactoring: use an opaque lttng_tracker_id type
[lttng-tools.git] / src / bin / lttng-sessiond / agent.c
CommitLineData
0475c50c
DG
1/*
2 * Copyright (C) 2013 - David Goulet <dgoulet@efficios.com>
bdf64013 3 * Copyright (C) 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
0475c50c
DG
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License, version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 51
16 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
6c1c0768 19#define _LGPL_SOURCE
0475c50c 20#include <assert.h>
f20baf8e 21#include <urcu/uatomic.h>
bdf64013 22#include <urcu/rculist.h>
0475c50c
DG
23
24#include <common/common.h>
022d91ba 25#include <common/sessiond-comm/agent.h>
0475c50c 26
f263b7fd
JD
27#include <common/compat/endian.h>
28
022d91ba 29#include "agent.h"
f20baf8e 30#include "ust-app.h"
0475c50c 31#include "utils.h"
6712db61 32#include "common/error.h"
23c2bd47
JG
33
34#define AGENT_RET_CODE_INDEX(code) (code - AGENT_RET_CODE_SUCCESS)
35
bdf64013
JG
36/*
37 * Agent application context representation.
38 */
39struct agent_app_ctx {
40 char *provider_name;
41 char *ctx_name;
42
43 /* agent_app_ctx are part of the agent app_ctx_list. */
44 struct cds_list_head list_node;
45
46 /* For call_rcu teardown. */
47 struct rcu_head rcu_node;
48};
49
23c2bd47
JG
50/*
51 * Human readable agent return code.
52 */
53static const char *error_string_array[] = {
54 [ AGENT_RET_CODE_INDEX(AGENT_RET_CODE_SUCCESS) ] = "Success",
55 [ AGENT_RET_CODE_INDEX(AGENT_RET_CODE_INVALID) ] = "Invalid command",
56 [ AGENT_RET_CODE_INDEX(AGENT_RET_CODE_UNKNOWN_NAME) ] = "Unknown logger name",
57
58 /* Last element */
59 [ AGENT_RET_CODE_INDEX(AGENT_RET_CODE_NR) ] = "Unknown code",
60};
61
62static
63void log_reply_code(uint32_t in_reply_ret_code)
64{
65 int level = PRINT_DBG3;
66 /*
67 * reply_ret_code and in_reply_ret_code are kept separate to have a
68 * sanitized value (used to retrieve the human readable string) and the
69 * original value which is logged as-is.
70 */
71 uint32_t reply_ret_code = in_reply_ret_code;
72
73 if (reply_ret_code < AGENT_RET_CODE_SUCCESS ||
74 reply_ret_code >= AGENT_RET_CODE_NR) {
75 reply_ret_code = AGENT_RET_CODE_NR;
76 level = PRINT_ERR;
77 }
78
79 LOG(level, "Agent replied with retcode: %s (%"PRIu32")",
80 error_string_array[AGENT_RET_CODE_INDEX(
81 reply_ret_code)],
82 in_reply_ret_code);
83}
0475c50c 84
4a4ab2c3
DG
85/*
86 * Match function for the events hash table lookup by name.
87 */
88static int ht_match_event_by_name(struct cds_lfht_node *node,
89 const void *_key)
90{
022d91ba
DG
91 struct agent_event *event;
92 const struct agent_ht_key *key;
4a4ab2c3
DG
93
94 assert(node);
95 assert(_key);
96
022d91ba 97 event = caa_container_of(node, struct agent_event, node.node);
4a4ab2c3
DG
98 key = _key;
99
100 /* Match 1 elements of the key: name. */
101
102 /* Event name */
103 if (strncmp(event->name, key->name, sizeof(event->name)) != 0) {
104 goto no_match;
105 }
106 /* Match. */
107 return 1;
108
109no_match:
110 return 0;
111}
112
113/*
114 * Match function for the events hash table lookup by name and loglevel.
115 */
116static int ht_match_event(struct cds_lfht_node *node,
117 const void *_key)
118{
022d91ba
DG
119 struct agent_event *event;
120 const struct agent_ht_key *key;
19a97244 121 int ll_match;
4a4ab2c3
DG
122
123 assert(node);
124 assert(_key);
125
022d91ba 126 event = caa_container_of(node, struct agent_event, node.node);
4a4ab2c3
DG
127 key = _key;
128
129 /* Match 2 elements of the key: name and loglevel. */
130
131 /* Event name */
132 if (strncmp(event->name, key->name, sizeof(event->name)) != 0) {
133 goto no_match;
134 }
135
a9319624 136 /* Event loglevel value and type. */
19a97244
PP
137 ll_match = loglevels_match(event->loglevel_type,
138 event->loglevel_value, key->loglevel_type,
139 key->loglevel_value, LTTNG_EVENT_LOGLEVEL_ALL);
140
141 if (!ll_match) {
4a4ab2c3
DG
142 goto no_match;
143 }
a9319624 144
6b10b3b0 145 /* Filter expression */
d42bc3c8 146 if (!!event->filter_expression != !!key->filter_expression) {
71e147d0 147 /* One has a filter expression, the other does not */
6b10b3b0
AM
148 goto no_match;
149 }
150
71e147d0
PP
151 if (event->filter_expression) {
152 if (strncmp(event->filter_expression, key->filter_expression,
153 strlen(event->filter_expression)) != 0) {
154 goto no_match;
155 }
156 }
157
4a4ab2c3
DG
158 return 1;
159
160no_match:
161 return 0;
162}
163
164/*
022d91ba 165 * Add unique agent event based on the event name and loglevel.
4a4ab2c3 166 */
022d91ba
DG
167static void add_unique_agent_event(struct lttng_ht *ht,
168 struct agent_event *event)
4a4ab2c3
DG
169{
170 struct cds_lfht_node *node_ptr;
022d91ba 171 struct agent_ht_key key;
4a4ab2c3
DG
172
173 assert(ht);
174 assert(ht->ht);
175 assert(event);
176
177 key.name = event->name;
2106efa0 178 key.loglevel_value = event->loglevel_value;
a9319624 179 key.loglevel_type = event->loglevel_type;
6b10b3b0 180 key.filter_expression = event->filter_expression;
4a4ab2c3
DG
181
182 node_ptr = cds_lfht_add_unique(ht->ht,
183 ht->hash_fct(event->node.key, lttng_ht_seed),
184 ht_match_event, &key, &event->node.node);
185 assert(node_ptr == &event->node.node);
186}
187
0475c50c 188/*
022d91ba 189 * URCU delayed agent event reclaim.
0475c50c 190 */
022d91ba 191static void destroy_event_agent_rcu(struct rcu_head *head)
0475c50c
DG
192{
193 struct lttng_ht_node_str *node =
194 caa_container_of(head, struct lttng_ht_node_str, head);
022d91ba
DG
195 struct agent_event *event =
196 caa_container_of(node, struct agent_event, node);
0475c50c 197
992febc7 198 agent_destroy_event(event);
0475c50c
DG
199}
200
f20baf8e 201/*
022d91ba 202 * URCU delayed agent app reclaim.
f20baf8e 203 */
022d91ba 204static void destroy_app_agent_rcu(struct rcu_head *head)
f20baf8e
DG
205{
206 struct lttng_ht_node_ulong *node =
207 caa_container_of(head, struct lttng_ht_node_ulong, head);
022d91ba
DG
208 struct agent_app *app =
209 caa_container_of(node, struct agent_app, node);
f20baf8e
DG
210
211 free(app);
212}
213
214/*
022d91ba
DG
215 * Communication with the agent. Send the message header to the given socket in
216 * big endian.
f20baf8e
DG
217 *
218 * Return 0 on success or else a negative errno message of sendmsg() op.
219 */
220static int send_header(struct lttcomm_sock *sock, uint64_t data_size,
221 uint32_t cmd, uint32_t cmd_version)
222{
223 int ret;
224 ssize_t size;
022d91ba 225 struct lttcomm_agent_hdr msg;
f20baf8e
DG
226
227 assert(sock);
228
53efb85a 229 memset(&msg, 0, sizeof(msg));
f20baf8e
DG
230 msg.data_size = htobe64(data_size);
231 msg.cmd = htobe32(cmd);
232 msg.cmd_version = htobe32(cmd_version);
233
234 size = sock->ops->sendmsg(sock, &msg, sizeof(msg), 0);
235 if (size < sizeof(msg)) {
236 ret = -errno;
237 goto error;
238 }
239 ret = 0;
240
241error:
242 return ret;
243}
244
245/*
022d91ba
DG
246 * Communication call with the agent. Send the payload to the given socket. The
247 * header MUST be sent prior to this call.
f20baf8e
DG
248 *
249 * Return 0 on success or else a negative errno value of sendmsg() op.
250 */
bdf64013 251static int send_payload(struct lttcomm_sock *sock, const void *data,
f20baf8e
DG
252 size_t size)
253{
254 int ret;
255 ssize_t len;
256
257 assert(sock);
258 assert(data);
259
260 len = sock->ops->sendmsg(sock, data, size, 0);
261 if (len < size) {
262 ret = -errno;
263 goto error;
264 }
265 ret = 0;
266
267error:
268 return ret;
269}
270
271/*
022d91ba
DG
272 * Communication call with the agent. Receive reply from the agent using the
273 * given socket.
f20baf8e
DG
274 *
275 * Return 0 on success or else a negative errno value from recvmsg() op.
276 */
277static int recv_reply(struct lttcomm_sock *sock, void *buf, size_t size)
278{
279 int ret;
280 ssize_t len;
281
282 assert(sock);
283 assert(buf);
284
285 len = sock->ops->recvmsg(sock, buf, size, 0);
286 if (len < size) {
287 ret = -errno;
288 goto error;
289 }
290 ret = 0;
291
292error:
293 return ret;
294}
295
3c6a091f 296/*
428de77a 297 * Internal event listing for a given app. Populate events.
3c6a091f
DG
298 *
299 * Return number of element in the list or else a negative LTTNG_ERR* code.
428de77a
MD
300 * On success, the caller is responsible for freeing the memory
301 * allocated for "events".
3c6a091f 302 */
022d91ba 303static ssize_t list_events(struct agent_app *app, struct lttng_event **events)
3c6a091f
DG
304{
305 int ret, i, len = 0, offset = 0;
306 uint32_t nb_event;
307 size_t data_size;
d84af1a4 308 uint32_t reply_ret_code;
3c6a091f 309 struct lttng_event *tmp_events = NULL;
022d91ba
DG
310 struct lttcomm_agent_list_reply *reply = NULL;
311 struct lttcomm_agent_list_reply_hdr reply_hdr;
3c6a091f
DG
312
313 assert(app);
314 assert(app->sock);
315 assert(events);
316
022d91ba 317 DBG2("Agent listing events for app pid: %d and socket %d", app->pid,
3c6a091f
DG
318 app->sock->fd);
319
022d91ba 320 ret = send_header(app->sock, 0, AGENT_CMD_LIST, 0);
3c6a091f
DG
321 if (ret < 0) {
322 goto error_io;
323 }
324
325 /* Get list header so we know how much we'll receive. */
326 ret = recv_reply(app->sock, &reply_hdr, sizeof(reply_hdr));
327 if (ret < 0) {
328 goto error_io;
329 }
330
d84af1a4
JG
331 reply_ret_code = be32toh(reply_hdr.ret_code);
332 log_reply_code(reply_ret_code);
333 switch (reply_ret_code) {
022d91ba 334 case AGENT_RET_CODE_SUCCESS:
3c6a091f
DG
335 data_size = be32toh(reply_hdr.data_size) + sizeof(*reply);
336 break;
337 default:
3bd9aaeb 338 ret = LTTNG_ERR_UNK;
3c6a091f
DG
339 goto error;
340 }
341
342 reply = zmalloc(data_size);
343 if (!reply) {
344 ret = LTTNG_ERR_NOMEM;
345 goto error;
346 }
347
348 /* Get the list with the appropriate data size. */
349 ret = recv_reply(app->sock, reply, data_size);
350 if (ret < 0) {
351 goto error_io;
352 }
353
354 nb_event = be32toh(reply->nb_event);
355 tmp_events = zmalloc(sizeof(*tmp_events) * nb_event);
356 if (!tmp_events) {
357 ret = LTTNG_ERR_NOMEM;
358 goto error;
359 }
360
361 for (i = 0; i < nb_event; i++) {
362 offset += len;
0a85e7a3
MD
363 if (lttng_strncpy(tmp_events[i].name, reply->payload + offset,
364 sizeof(tmp_events[i].name))) {
365 ret = LTTNG_ERR_INVALID;
366 goto error;
367 }
3c6a091f
DG
368 tmp_events[i].pid = app->pid;
369 tmp_events[i].enabled = -1;
370 len = strlen(reply->payload + offset) + 1;
371 }
372
373 *events = tmp_events;
374
375 free(reply);
376 return nb_event;
377
378error_io:
379 ret = LTTNG_ERR_UST_LIST_FAIL;
380error:
381 free(reply);
382 free(tmp_events);
383 return -ret;
384
385}
386
f20baf8e 387/*
022d91ba
DG
388 * Internal enable agent event on a agent application. This function
389 * communicates with the agent to enable a given event.
f20baf8e
DG
390 *
391 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
392 */
022d91ba 393static int enable_event(struct agent_app *app, struct agent_event *event)
f20baf8e
DG
394{
395 int ret;
a0ba721c 396 char *bytes_to_send;
f20baf8e 397 uint64_t data_size;
a0ba721c 398 size_t filter_expression_length;
f4f9d4db 399 uint32_t reply_ret_code;
bdf64013 400 struct lttcomm_agent_enable_event msg;
022d91ba 401 struct lttcomm_agent_generic_reply reply;
f20baf8e
DG
402
403 assert(app);
404 assert(app->sock);
405 assert(event);
406
022d91ba 407 DBG2("Agent enabling event %s for app pid: %d and socket %d", event->name,
f20baf8e
DG
408 app->pid, app->sock->fd);
409
a0ba721c
AM
410 /*
411 * Calculate the payload's size, which is the fixed-size struct followed
412 * by the variable-length filter expression (+1 for the ending \0).
413 */
414 if (!event->filter_expression) {
415 filter_expression_length = 0;
416 } else {
417 filter_expression_length = strlen(event->filter_expression) + 1;
418 }
419 data_size = sizeof(msg) + filter_expression_length;
f20baf8e 420
53efb85a 421 memset(&msg, 0, sizeof(msg));
a9bfd666
JG
422 msg.loglevel_value = htobe32(event->loglevel_value);
423 msg.loglevel_type = htobe32(event->loglevel_type);
bb45c03e
MD
424 if (lttng_strncpy(msg.name, event->name, sizeof(msg.name))) {
425 ret = LTTNG_ERR_INVALID;
426 goto error;
427 }
a9bfd666 428 msg.filter_expression_length = htobe32(filter_expression_length);
a0ba721c 429
bb45c03e
MD
430 ret = send_header(app->sock, data_size, AGENT_CMD_ENABLE, 0);
431 if (ret < 0) {
432 goto error_io;
433 }
434
a0ba721c
AM
435 bytes_to_send = zmalloc(data_size);
436 if (!bytes_to_send) {
437 ret = LTTNG_ERR_NOMEM;
438 goto error;
439 }
440
441 memcpy(bytes_to_send, &msg, sizeof(msg));
442 if (filter_expression_length > 0) {
443 memcpy(bytes_to_send + sizeof(msg), event->filter_expression,
444 filter_expression_length);
445 }
446
447 ret = send_payload(app->sock, bytes_to_send, data_size);
448 free(bytes_to_send);
f20baf8e
DG
449 if (ret < 0) {
450 goto error_io;
451 }
452
453 ret = recv_reply(app->sock, &reply, sizeof(reply));
454 if (ret < 0) {
455 goto error_io;
456 }
457
f4f9d4db
JG
458 reply_ret_code = be32toh(reply.ret_code);
459 log_reply_code(reply_ret_code);
460 switch (reply_ret_code) {
022d91ba 461 case AGENT_RET_CODE_SUCCESS:
f20baf8e 462 break;
022d91ba 463 case AGENT_RET_CODE_UNKNOWN_NAME:
f20baf8e
DG
464 ret = LTTNG_ERR_UST_EVENT_NOT_FOUND;
465 goto error;
466 default:
3bd9aaeb 467 ret = LTTNG_ERR_UNK;
f20baf8e
DG
468 goto error;
469 }
470
471 return LTTNG_OK;
472
473error_io:
474 ret = LTTNG_ERR_UST_ENABLE_FAIL;
475error:
476 return ret;
477}
478
bdf64013
JG
479/*
480 * Send Pascal-style string. Size is sent as a 32-bit big endian integer.
481 */
482static
483int send_pstring(struct lttcomm_sock *sock, const char *str, uint32_t len)
484{
485 int ret;
486 uint32_t len_be;
487
488 len_be = htobe32(len);
489 ret = send_payload(sock, &len_be, sizeof(len_be));
490 if (ret) {
491 goto end;
492 }
493
494 ret = send_payload(sock, str, len);
495 if (ret) {
496 goto end;
497 }
498end:
499 return ret;
500}
501
502/*
503 * Internal enable application context on an agent application. This function
504 * communicates with the agent to enable a given application context.
505 *
506 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
507 */
508static int app_context_op(struct agent_app *app,
509 struct agent_app_ctx *ctx, enum lttcomm_agent_command cmd)
510{
511 int ret;
512 uint32_t reply_ret_code;
513 struct lttcomm_agent_generic_reply reply;
514 size_t app_ctx_provider_name_len, app_ctx_name_len, data_size;
515
516 assert(app);
517 assert(app->sock);
518 assert(ctx);
519 assert(cmd == AGENT_CMD_APP_CTX_ENABLE ||
520 cmd == AGENT_CMD_APP_CTX_DISABLE);
521
522 DBG2("Agent %s application %s:%s for app pid: %d and socket %d",
523 cmd == AGENT_CMD_APP_CTX_ENABLE ? "enabling" : "disabling",
524 ctx->provider_name, ctx->ctx_name,
525 app->pid, app->sock->fd);
526
527 /*
528 * Calculate the payload's size, which consists of the size (u32, BE)
529 * of the provider name, the NULL-terminated provider name string, the
530 * size (u32, BE) of the context name, followed by the NULL-terminated
531 * context name string.
532 */
533 app_ctx_provider_name_len = strlen(ctx->provider_name) + 1;
534 app_ctx_name_len = strlen(ctx->ctx_name) + 1;
535 data_size = sizeof(uint32_t) + app_ctx_provider_name_len +
536 sizeof(uint32_t) + app_ctx_name_len;
537
538 ret = send_header(app->sock, data_size, cmd, 0);
539 if (ret < 0) {
540 goto error_io;
541 }
542
543 if (app_ctx_provider_name_len > UINT32_MAX ||
544 app_ctx_name_len > UINT32_MAX) {
545 ERR("Application context name > MAX_UINT32");
546 ret = LTTNG_ERR_INVALID;
547 goto error;
548 }
549
550 ret = send_pstring(app->sock, ctx->provider_name,
551 (uint32_t) app_ctx_provider_name_len);
552 if (ret < 0) {
553 goto error_io;
554 }
555
556 ret = send_pstring(app->sock, ctx->ctx_name,
557 (uint32_t) app_ctx_name_len);
558 if (ret < 0) {
559 goto error_io;
560 }
561
562 ret = recv_reply(app->sock, &reply, sizeof(reply));
563 if (ret < 0) {
564 goto error_io;
565 }
566
567 reply_ret_code = be32toh(reply.ret_code);
568 log_reply_code(reply_ret_code);
569 switch (reply_ret_code) {
570 case AGENT_RET_CODE_SUCCESS:
571 break;
572 default:
573 ret = LTTNG_ERR_UNK;
574 goto error;
575 }
576
577 return LTTNG_OK;
578
579error_io:
580 ret = LTTNG_ERR_UST_ENABLE_FAIL;
581error:
582 return ret;
583}
584
f20baf8e 585/*
022d91ba
DG
586 * Internal disable agent event call on a agent application. This function
587 * communicates with the agent to disable a given event.
f20baf8e
DG
588 *
589 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
590 */
022d91ba 591static int disable_event(struct agent_app *app, struct agent_event *event)
f20baf8e
DG
592{
593 int ret;
594 uint64_t data_size;
517992f8 595 uint32_t reply_ret_code;
bdf64013 596 struct lttcomm_agent_disable_event msg;
022d91ba 597 struct lttcomm_agent_generic_reply reply;
f20baf8e
DG
598
599 assert(app);
600 assert(app->sock);
601 assert(event);
602
022d91ba 603 DBG2("Agent disabling event %s for app pid: %d and socket %d", event->name,
f20baf8e
DG
604 app->pid, app->sock->fd);
605
606 data_size = sizeof(msg);
63be730a
MD
607 memset(&msg, 0, sizeof(msg));
608 if (lttng_strncpy(msg.name, event->name, sizeof(msg.name))) {
609 ret = LTTNG_ERR_INVALID;
610 goto error;
611 }
f20baf8e 612
022d91ba 613 ret = send_header(app->sock, data_size, AGENT_CMD_DISABLE, 0);
f20baf8e
DG
614 if (ret < 0) {
615 goto error_io;
616 }
617
f20baf8e
DG
618 ret = send_payload(app->sock, &msg, sizeof(msg));
619 if (ret < 0) {
620 goto error_io;
621 }
622
623 ret = recv_reply(app->sock, &reply, sizeof(reply));
624 if (ret < 0) {
625 goto error_io;
626 }
627
517992f8
JG
628 reply_ret_code = be32toh(reply.ret_code);
629 log_reply_code(reply_ret_code);
630 switch (reply_ret_code) {
022d91ba
DG
631 case AGENT_RET_CODE_SUCCESS:
632 break;
633 case AGENT_RET_CODE_UNKNOWN_NAME:
634 ret = LTTNG_ERR_UST_EVENT_NOT_FOUND;
635 goto error;
636 default:
3bd9aaeb 637 ret = LTTNG_ERR_UNK;
022d91ba 638 goto error;
f20baf8e
DG
639 }
640
641 return LTTNG_OK;
642
643error_io:
644 ret = LTTNG_ERR_UST_DISABLE_FAIL;
645error:
646 return ret;
647}
648
1b500e7a 649/*
022d91ba 650 * Send back the registration DONE command to a given agent application.
1b500e7a
DG
651 *
652 * Return 0 on success or else a negative value.
653 */
022d91ba 654int agent_send_registration_done(struct agent_app *app)
1b500e7a
DG
655{
656 assert(app);
657 assert(app->sock);
658
022d91ba 659 DBG("Agent sending registration done to app socket %d", app->sock->fd);
1b500e7a 660
b26d1f5c 661 return send_header(app->sock, 0, AGENT_CMD_REG_DONE, 0);
1b500e7a
DG
662}
663
f20baf8e 664/*
022d91ba 665 * Enable agent event on every agent applications registered with the session
f20baf8e
DG
666 * daemon.
667 *
668 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
669 */
fefd409b
DG
670int agent_enable_event(struct agent_event *event,
671 enum lttng_domain_type domain)
f20baf8e
DG
672{
673 int ret;
022d91ba 674 struct agent_app *app;
f20baf8e
DG
675 struct lttng_ht_iter iter;
676
677 assert(event);
678
679 rcu_read_lock();
680
022d91ba 681 cds_lfht_for_each_entry(agent_apps_ht_by_sock->ht, &iter.iter, app,
f20baf8e 682 node.node) {
fefd409b
DG
683 if (app->domain != domain) {
684 continue;
685 }
686
022d91ba 687 /* Enable event on agent application through TCP socket. */
f20baf8e
DG
688 ret = enable_event(app, event);
689 if (ret != LTTNG_OK) {
690 goto error;
691 }
f20baf8e
DG
692 }
693
3c6a091f 694 event->enabled = 1;
f20baf8e
DG
695 ret = LTTNG_OK;
696
697error:
698 rcu_read_unlock();
699 return ret;
700}
701
bdf64013
JG
702static
703void destroy_app_ctx(struct agent_app_ctx *ctx)
704{
705 free(ctx->provider_name);
706 free(ctx->ctx_name);
707 free(ctx);
708}
709
710static
df4f5a87 711struct agent_app_ctx *create_app_ctx(const struct lttng_event_context *ctx)
bdf64013
JG
712{
713 struct agent_app_ctx *agent_ctx = NULL;
714
715 if (!ctx) {
716 goto end;
717 }
718
719 assert(ctx->ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT);
720 agent_ctx = zmalloc(sizeof(*ctx));
721 if (!agent_ctx) {
722 goto end;
723 }
724
725 agent_ctx->provider_name = strdup(ctx->u.app_ctx.provider_name);
726 agent_ctx->ctx_name = strdup(ctx->u.app_ctx.ctx_name);
727 if (!agent_ctx->provider_name || !agent_ctx->ctx_name) {
728 destroy_app_ctx(agent_ctx);
729 agent_ctx = NULL;
730 }
731end:
732 return agent_ctx;
733}
734
735/*
736 * Enable agent context on every agent applications registered with the session
737 * daemon.
738 *
739 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
740 */
df4f5a87 741int agent_enable_context(const struct lttng_event_context *ctx,
bdf64013
JG
742 enum lttng_domain_type domain)
743{
744 int ret;
745 struct agent_app *app;
746 struct lttng_ht_iter iter;
747
748 assert(ctx);
749 if (ctx->ctx != LTTNG_EVENT_CONTEXT_APP_CONTEXT) {
750 ret = LTTNG_ERR_INVALID;
751 goto error;
752 }
753
754 rcu_read_lock();
755
756 cds_lfht_for_each_entry(agent_apps_ht_by_sock->ht, &iter.iter, app,
757 node.node) {
758 struct agent_app_ctx *agent_ctx;
759
760 if (app->domain != domain) {
761 continue;
762 }
763
764 agent_ctx = create_app_ctx(ctx);
765 if (!agent_ctx) {
a33c2651 766 ret = LTTNG_ERR_NOMEM;
bdf64013
JG
767 goto error_unlock;
768 }
769
770 /* Enable event on agent application through TCP socket. */
771 ret = app_context_op(app, agent_ctx, AGENT_CMD_APP_CTX_ENABLE);
c12618b5 772 destroy_app_ctx(agent_ctx);
bdf64013 773 if (ret != LTTNG_OK) {
bdf64013
JG
774 goto error_unlock;
775 }
776 }
777
778 ret = LTTNG_OK;
779
780error_unlock:
781 rcu_read_unlock();
782error:
783 return ret;
784}
785
f20baf8e 786/*
bdf64013 787 * Disable agent event on every agent application registered with the session
f20baf8e
DG
788 * daemon.
789 *
790 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
791 */
fefd409b
DG
792int agent_disable_event(struct agent_event *event,
793 enum lttng_domain_type domain)
f20baf8e 794{
f1bc0129 795 int ret = LTTNG_OK;
022d91ba 796 struct agent_app *app;
f20baf8e
DG
797 struct lttng_ht_iter iter;
798
799 assert(event);
f1bc0129
JG
800 if (!event->enabled) {
801 goto end;
802 }
f20baf8e
DG
803
804 rcu_read_lock();
805
022d91ba 806 cds_lfht_for_each_entry(agent_apps_ht_by_sock->ht, &iter.iter, app,
f20baf8e 807 node.node) {
fefd409b
DG
808 if (app->domain != domain) {
809 continue;
810 }
811
022d91ba 812 /* Enable event on agent application through TCP socket. */
f20baf8e
DG
813 ret = disable_event(app, event);
814 if (ret != LTTNG_OK) {
815 goto error;
816 }
f20baf8e
DG
817 }
818
3c6a091f 819 event->enabled = 0;
f20baf8e
DG
820
821error:
822 rcu_read_unlock();
f1bc0129 823end:
f20baf8e
DG
824 return ret;
825}
826
bdf64013
JG
827/*
828 * Disable agent context on every agent application registered with the session
829 * daemon.
830 *
831 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
832 */
46440d0c
SM
833static int disable_context(struct agent_app_ctx *ctx,
834 enum lttng_domain_type domain)
bdf64013
JG
835{
836 int ret = LTTNG_OK;
837 struct agent_app *app;
838 struct lttng_ht_iter iter;
839
840 assert(ctx);
841
842 rcu_read_lock();
843 DBG2("Disabling agent application context %s:%s",
844 ctx->provider_name, ctx->ctx_name);
845 cds_lfht_for_each_entry(agent_apps_ht_by_sock->ht, &iter.iter, app,
846 node.node) {
847 if (app->domain != domain) {
848 continue;
849 }
850
851 ret = app_context_op(app, ctx, AGENT_CMD_APP_CTX_DISABLE);
852 if (ret != LTTNG_OK) {
853 goto end;
854 }
855 }
856end:
857 rcu_read_unlock();
858 return ret;
859}
860
f20baf8e 861/*
022d91ba
DG
862 * Ask every agent for the list of possible event. Events is allocated with the
863 * events of every agent application.
f20baf8e
DG
864 *
865 * Return the number of events or else a negative value.
866 */
f60140a1
DG
867int agent_list_events(struct lttng_event **events,
868 enum lttng_domain_type domain)
f20baf8e
DG
869{
870 int ret;
871 size_t nbmem, count = 0;
022d91ba 872 struct agent_app *app;
aae6255e 873 struct lttng_event *tmp_events = NULL;
f20baf8e
DG
874 struct lttng_ht_iter iter;
875
876 assert(events);
877
0e115563
DG
878 DBG2("Agent listing events for domain %d", domain);
879
f20baf8e
DG
880 nbmem = UST_APP_EVENT_LIST_SIZE;
881 tmp_events = zmalloc(nbmem * sizeof(*tmp_events));
882 if (!tmp_events) {
022d91ba 883 PERROR("zmalloc agent list events");
f20baf8e
DG
884 ret = -ENOMEM;
885 goto error;
886 }
887
888 rcu_read_lock();
022d91ba 889 cds_lfht_for_each_entry(agent_apps_ht_by_sock->ht, &iter.iter, app,
f20baf8e
DG
890 node.node) {
891 ssize_t nb_ev;
022d91ba 892 struct lttng_event *agent_events;
f20baf8e 893
f60140a1
DG
894 /* Skip domain not asked by the list. */
895 if (app->domain != domain) {
896 continue;
897 }
898
022d91ba 899 nb_ev = list_events(app, &agent_events);
f20baf8e
DG
900 if (nb_ev < 0) {
901 ret = nb_ev;
428de77a 902 goto error_unlock;
f20baf8e
DG
903 }
904
53efb85a 905 if (count + nb_ev > nbmem) {
f20baf8e 906 /* In case the realloc fails, we free the memory */
53efb85a
MD
907 struct lttng_event *new_tmp_events;
908 size_t new_nbmem;
909
910 new_nbmem = max_t(size_t, count + nb_ev, nbmem << 1);
022d91ba 911 DBG2("Reallocating agent event list from %zu to %zu entries",
53efb85a
MD
912 nbmem, new_nbmem);
913 new_tmp_events = realloc(tmp_events,
914 new_nbmem * sizeof(*new_tmp_events));
915 if (!new_tmp_events) {
022d91ba 916 PERROR("realloc agent events");
f20baf8e 917 ret = -ENOMEM;
022d91ba 918 free(agent_events);
428de77a 919 goto error_unlock;
f20baf8e 920 }
53efb85a
MD
921 /* Zero the new memory */
922 memset(new_tmp_events + nbmem, 0,
923 (new_nbmem - nbmem) * sizeof(*new_tmp_events));
924 nbmem = new_nbmem;
925 tmp_events = new_tmp_events;
f20baf8e 926 }
022d91ba 927 memcpy(tmp_events + count, agent_events,
53efb85a 928 nb_ev * sizeof(*tmp_events));
022d91ba 929 free(agent_events);
f20baf8e
DG
930 count += nb_ev;
931 }
932 rcu_read_unlock();
933
934 ret = count;
935 *events = tmp_events;
aae6255e 936 return ret;
f20baf8e 937
428de77a
MD
938error_unlock:
939 rcu_read_unlock();
f20baf8e 940error:
aae6255e 941 free(tmp_events);
f20baf8e
DG
942 return ret;
943}
944
945/*
022d91ba 946 * Create a agent app object using the given PID.
f20baf8e
DG
947 *
948 * Return newly allocated object or else NULL on error.
949 */
fefd409b
DG
950struct agent_app *agent_create_app(pid_t pid, enum lttng_domain_type domain,
951 struct lttcomm_sock *sock)
f20baf8e 952{
022d91ba 953 struct agent_app *app;
f20baf8e
DG
954
955 assert(sock);
956
957 app = zmalloc(sizeof(*app));
958 if (!app) {
022d91ba 959 PERROR("zmalloc agent create");
f20baf8e
DG
960 goto error;
961 }
962
963 app->pid = pid;
fefd409b 964 app->domain = domain;
f20baf8e 965 app->sock = sock;
f20baf8e
DG
966 lttng_ht_node_init_ulong(&app->node, (unsigned long) app->sock->fd);
967
968error:
969 return app;
970}
971
972/*
022d91ba 973 * Lookup agent app by socket in the global hash table.
f20baf8e
DG
974 *
975 * RCU read side lock MUST be acquired.
976 *
977 * Return object if found else NULL.
978 */
022d91ba 979struct agent_app *agent_find_app_by_sock(int sock)
f20baf8e
DG
980{
981 struct lttng_ht_node_ulong *node;
982 struct lttng_ht_iter iter;
022d91ba 983 struct agent_app *app;
f20baf8e
DG
984
985 assert(sock >= 0);
986
022d91ba 987 lttng_ht_lookup(agent_apps_ht_by_sock, (void *)((unsigned long) sock), &iter);
f20baf8e
DG
988 node = lttng_ht_iter_get_node_ulong(&iter);
989 if (node == NULL) {
990 goto error;
991 }
022d91ba 992 app = caa_container_of(node, struct agent_app, node);
f20baf8e 993
022d91ba 994 DBG3("Agent app pid %d found by sock %d.", app->pid, sock);
f20baf8e
DG
995 return app;
996
997error:
022d91ba 998 DBG3("Agent app NOT found by sock %d.", sock);
f20baf8e
DG
999 return NULL;
1000}
1001
1002/*
022d91ba 1003 * Add agent application object to the global hash table.
f20baf8e 1004 */
022d91ba 1005void agent_add_app(struct agent_app *app)
f20baf8e
DG
1006{
1007 assert(app);
1008
022d91ba 1009 DBG3("Agent adding app sock: %d and pid: %d to ht", app->sock->fd, app->pid);
022d91ba 1010 lttng_ht_add_unique_ulong(agent_apps_ht_by_sock, &app->node);
f20baf8e
DG
1011}
1012
f20baf8e 1013/*
022d91ba 1014 * Delete agent application from the global hash table.
d558f236
JG
1015 *
1016 * rcu_read_lock() must be held by the caller.
f20baf8e 1017 */
022d91ba 1018void agent_delete_app(struct agent_app *app)
f20baf8e
DG
1019{
1020 int ret;
1021 struct lttng_ht_iter iter;
1022
1023 assert(app);
1024
022d91ba 1025 DBG3("Agent deleting app pid: %d and sock: %d", app->pid, app->sock->fd);
f20baf8e
DG
1026
1027 iter.iter.node = &app->node.node;
022d91ba 1028 ret = lttng_ht_del(agent_apps_ht_by_sock, &iter);
f20baf8e
DG
1029 assert(!ret);
1030}
1031
1032/*
e785906c 1033 * Destroy an agent application object by detaching it from its corresponding
022d91ba 1034 * UST app if one is connected by closing the socket. Finally, perform a
428de77a 1035 * delayed memory reclaim.
f20baf8e 1036 */
022d91ba 1037void agent_destroy_app(struct agent_app *app)
f20baf8e
DG
1038{
1039 assert(app);
1040
1041 if (app->sock) {
1042 app->sock->ops->close(app->sock);
1043 lttcomm_destroy_sock(app->sock);
1044 }
1045
022d91ba 1046 call_rcu(&app->node.head, destroy_app_agent_rcu);
f20baf8e
DG
1047}
1048
0475c50c 1049/*
022d91ba 1050 * Initialize an already allocated agent object.
0475c50c
DG
1051 *
1052 * Return 0 on success or else a negative errno value.
1053 */
022d91ba 1054int agent_init(struct agent *agt)
0475c50c
DG
1055{
1056 int ret;
1057
022d91ba 1058 assert(agt);
0475c50c 1059
022d91ba
DG
1060 agt->events = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
1061 if (!agt->events) {
0475c50c
DG
1062 ret = -ENOMEM;
1063 goto error;
1064 }
fefd409b 1065 lttng_ht_node_init_u64(&agt->node, agt->domain);
0475c50c 1066
bdf64013 1067 CDS_INIT_LIST_HEAD(&agt->app_ctx_list);
0475c50c
DG
1068 return 0;
1069
1070error:
1071 return ret;
1072}
1073
fefd409b
DG
1074/*
1075 * Add agent object to the given hash table.
1076 */
1077void agent_add(struct agent *agt, struct lttng_ht *ht)
1078{
1079 assert(agt);
1080 assert(ht);
1081
1082 DBG3("Agent adding from domain %d", agt->domain);
1083
fefd409b 1084 lttng_ht_add_unique_u64(ht, &agt->node);
fefd409b
DG
1085}
1086
1087/*
1088 * Create an agent object for the given domain.
1089 *
1090 * Return the allocated agent or NULL on error.
1091 */
1092struct agent *agent_create(enum lttng_domain_type domain)
1093{
1094 int ret;
1095 struct agent *agt;
1096
3b5f70d4 1097 agt = zmalloc(sizeof(struct agent));
fefd409b
DG
1098 if (!agt) {
1099 goto error;
1100 }
1101 agt->domain = domain;
1102
1103 ret = agent_init(agt);
1104 if (ret < 0) {
1105 free(agt);
988ae332 1106 agt = NULL;
fefd409b
DG
1107 goto error;
1108 }
1109
1110error:
1111 return agt;
1112}
1113
0475c50c 1114/*
51755dc8
JG
1115 * Create a newly allocated agent event data structure.
1116 * Ownership of filter_expression is taken.
0475c50c
DG
1117 *
1118 * Return a new object else NULL on error.
1119 */
022d91ba 1120struct agent_event *agent_create_event(const char *name,
a9319624 1121 enum lttng_loglevel_type loglevel_type, int loglevel_value,
51755dc8 1122 struct lttng_filter_bytecode *filter, char *filter_expression)
0475c50c 1123{
51755dc8 1124 struct agent_event *event = NULL;
0475c50c 1125
6b10b3b0
AM
1126 DBG3("Agent create new event with name %s, loglevel type %d, \
1127 loglevel value %d and filter %s",
40111ba1
JG
1128 name, loglevel_type, loglevel_value,
1129 filter_expression ? filter_expression : "NULL");
0475c50c 1130
51755dc8
JG
1131 if (!name) {
1132 ERR("Failed to create agent event; no name provided.");
0475c50c
DG
1133 goto error;
1134 }
1135
51755dc8
JG
1136 event = zmalloc(sizeof(*event));
1137 if (!event) {
1138 goto error;
0475c50c
DG
1139 }
1140
51755dc8
JG
1141 strncpy(event->name, name, sizeof(event->name));
1142 event->name[sizeof(event->name) - 1] = '\0';
1143 lttng_ht_node_init_str(&event->node, event->name);
be6a6276 1144
2106efa0 1145 event->loglevel_value = loglevel_value;
51755dc8
JG
1146 event->loglevel_type = loglevel_type;
1147 event->filter = filter;
1148 event->filter_expression = filter_expression;
0475c50c
DG
1149error:
1150 return event;
1151}
1152
1153/*
022d91ba 1154 * Unique add of a agent event to an agent object.
0475c50c 1155 */
022d91ba 1156void agent_add_event(struct agent_event *event, struct agent *agt)
0475c50c
DG
1157{
1158 assert(event);
022d91ba
DG
1159 assert(agt);
1160 assert(agt->events);
0475c50c 1161
022d91ba 1162 DBG3("Agent adding event %s", event->name);
022d91ba 1163 add_unique_agent_event(agt->events, event);
022d91ba 1164 agt->being_used = 1;
0475c50c
DG
1165}
1166
bdf64013
JG
1167/*
1168 * Unique add of a agent context to an agent object.
1169 */
df4f5a87 1170int agent_add_context(const struct lttng_event_context *ctx, struct agent *agt)
bdf64013
JG
1171{
1172 int ret = LTTNG_OK;
1173 struct agent_app_ctx *agent_ctx = NULL;
1174
1175 assert(ctx);
1176 assert(agt);
1177 assert(agt->events);
1178 assert(ctx->ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT);
1179
1180 agent_ctx = create_app_ctx(ctx);
1181 if (!agent_ctx) {
1182 ret = LTTNG_ERR_NOMEM;
1183 goto end;
1184 }
1185
1186 DBG3("Agent adding context %s:%s", ctx->u.app_ctx.provider_name,
1187 ctx->u.app_ctx.ctx_name);
1188 cds_list_add_tail_rcu(&agent_ctx->list_node, &agt->app_ctx_list);
1189end:
1190 return ret;
1191}
1192
0475c50c 1193/*
e261a6cc 1194 * Find multiple agent events sharing the given name.
4a4ab2c3 1195 *
e261a6cc
PP
1196 * RCU read side lock MUST be acquired. It must be held for the
1197 * duration of the iteration.
4a4ab2c3 1198 *
e261a6cc 1199 * Sets the given iterator.
4a4ab2c3 1200 */
e261a6cc
PP
1201void agent_find_events_by_name(const char *name, struct agent *agt,
1202 struct lttng_ht_iter* iter)
4a4ab2c3 1203{
4a4ab2c3 1204 struct lttng_ht *ht;
022d91ba 1205 struct agent_ht_key key;
4a4ab2c3
DG
1206
1207 assert(name);
022d91ba
DG
1208 assert(agt);
1209 assert(agt->events);
e261a6cc 1210 assert(iter);
4a4ab2c3 1211
022d91ba 1212 ht = agt->events;
4a4ab2c3
DG
1213 key.name = name;
1214
1215 cds_lfht_lookup(ht->ht, ht->hash_fct((void *) name, lttng_ht_seed),
e261a6cc
PP
1216 ht_match_event_by_name, &key, &iter->iter);
1217}
4a4ab2c3 1218
e261a6cc
PP
1219/*
1220 * Get the next agent event duplicate by name. This should be called
1221 * after a call to agent_find_events_by_name() to iterate on events.
1222 *
1223 * The RCU read lock must be held during the iteration and for as long
1224 * as the object the iterator points to remains in use.
1225 */
1226void agent_event_next_duplicate(const char *name,
1227 struct agent *agt, struct lttng_ht_iter* iter)
1228{
1229 struct agent_ht_key key;
4a4ab2c3 1230
e261a6cc
PP
1231 key.name = name;
1232
1233 cds_lfht_next_duplicate(agt->events->ht, ht_match_event_by_name,
1234 &key, &iter->iter);
4a4ab2c3
DG
1235}
1236
1237/*
6b10b3b0 1238 * Find a agent event in the given agent using name, loglevel and filter.
0475c50c 1239 *
1e17eae2
JG
1240 * RCU read side lock MUST be acquired. It must be kept for as long as
1241 * the returned agent_event is used.
0475c50c
DG
1242 *
1243 * Return object if found else NULL.
1244 */
a9319624
PP
1245struct agent_event *agent_find_event(const char *name,
1246 enum lttng_loglevel_type loglevel_type, int loglevel_value,
6b10b3b0 1247 char *filter_expression, struct agent *agt)
0475c50c
DG
1248{
1249 struct lttng_ht_node_str *node;
1250 struct lttng_ht_iter iter;
4a4ab2c3 1251 struct lttng_ht *ht;
022d91ba 1252 struct agent_ht_key key;
0475c50c
DG
1253
1254 assert(name);
022d91ba
DG
1255 assert(agt);
1256 assert(agt->events);
0475c50c 1257
022d91ba 1258 ht = agt->events;
4a4ab2c3 1259 key.name = name;
2106efa0 1260 key.loglevel_value = loglevel_value;
a9319624 1261 key.loglevel_type = loglevel_type;
6b10b3b0 1262 key.filter_expression = filter_expression;
4a4ab2c3
DG
1263
1264 cds_lfht_lookup(ht->ht, ht->hash_fct((void *) name, lttng_ht_seed),
1265 ht_match_event, &key, &iter.iter);
0475c50c
DG
1266 node = lttng_ht_iter_get_node_str(&iter);
1267 if (node == NULL) {
1268 goto error;
1269 }
1270
022d91ba
DG
1271 DBG3("Agent event found %s.", name);
1272 return caa_container_of(node, struct agent_event, node);
0475c50c
DG
1273
1274error:
a51e817b 1275 DBG3("Agent event NOT found %s.", name);
0475c50c
DG
1276 return NULL;
1277}
1278
0475c50c 1279/*
022d91ba
DG
1280 * Free given agent event. This event must not be globally visible at this
1281 * point (only expected to be used on failure just after event creation). After
1282 * this call, the pointer is not usable anymore.
0475c50c 1283 */
022d91ba 1284void agent_destroy_event(struct agent_event *event)
0475c50c
DG
1285{
1286 assert(event);
1287
971da06a 1288 free(event->filter);
8404118c 1289 free(event->filter_expression);
51755dc8 1290 free(event->exclusion);
0475c50c
DG
1291 free(event);
1292}
1293
bdf64013
JG
1294static
1295void destroy_app_ctx_rcu(struct rcu_head *head)
1296{
1297 struct agent_app_ctx *ctx =
1298 caa_container_of(head, struct agent_app_ctx, rcu_node);
1299
1300 destroy_app_ctx(ctx);
1301}
1302
0475c50c 1303/*
35ed21a5 1304 * Destroy an agent completely.
0475c50c 1305 */
022d91ba 1306void agent_destroy(struct agent *agt)
0475c50c
DG
1307{
1308 struct lttng_ht_node_str *node;
1309 struct lttng_ht_iter iter;
bdf64013 1310 struct agent_app_ctx *ctx;
0475c50c 1311
022d91ba 1312 assert(agt);
0475c50c 1313
022d91ba 1314 DBG3("Agent destroy");
0475c50c 1315
0475c50c 1316 rcu_read_lock();
022d91ba 1317 cds_lfht_for_each_entry(agt->events->ht, &iter.iter, node, node) {
0475c50c 1318 int ret;
022d91ba 1319 struct agent_event *event;
29c0fd4d
DG
1320
1321 /*
bdf64013
JG
1322 * When destroying an event, we have to try to disable it on the
1323 * agent side so the event stops generating data. The return
1324 * value is not important since we have to continue anyway
1325 * destroying the object.
29c0fd4d 1326 */
022d91ba
DG
1327 event = caa_container_of(node, struct agent_event, node);
1328 (void) agent_disable_event(event, agt->domain);
0475c50c 1329
022d91ba 1330 ret = lttng_ht_del(agt->events, &iter);
0475c50c 1331 assert(!ret);
022d91ba 1332 call_rcu(&node->head, destroy_event_agent_rcu);
0475c50c 1333 }
0475c50c 1334
bdf64013
JG
1335 cds_list_for_each_entry_rcu(ctx, &agt->app_ctx_list, list_node) {
1336 (void) disable_context(ctx, agt->domain);
1337 cds_list_del(&ctx->list_node);
1338 call_rcu(&ctx->rcu_node, destroy_app_ctx_rcu);
1339 }
1340 rcu_read_unlock();
f8be902b 1341 ht_cleanup_push(agt->events);
35ed21a5 1342 free(agt);
f20baf8e
DG
1343}
1344
1345/*
6a4e4039 1346 * Allocate agent_apps_ht_by_sock.
f20baf8e 1347 */
6a4e4039 1348int agent_app_ht_alloc(void)
f20baf8e 1349{
6a4e4039
JG
1350 int ret = 0;
1351
022d91ba
DG
1352 agent_apps_ht_by_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
1353 if (!agent_apps_ht_by_sock) {
6a4e4039 1354 ret = -1;
f20baf8e
DG
1355 }
1356
6a4e4039
JG
1357 return ret;
1358}
1359
1360/*
1361 * Destroy a agent application by socket.
1362 */
1363void agent_destroy_app_by_sock(int sock)
1364{
1365 struct agent_app *app;
1366
1367 assert(sock >= 0);
1368
1369 /*
1370 * Not finding an application is a very important error that should NEVER
1371 * happen. The hash table deletion is ONLY done through this call when the
1372 * main sessiond thread is torn down.
1373 */
1374 rcu_read_lock();
1375 app = agent_find_app_by_sock(sock);
1376 assert(app);
1377
1378 /* RCU read side lock is assumed to be held by this function. */
1379 agent_delete_app(app);
1380
1381 /* The application is freed in a RCU call but the socket is closed here. */
1382 agent_destroy_app(app);
1383 rcu_read_unlock();
1384}
1385
1386/*
1387 * Clean-up the agent app hash table and destroy it.
1388 */
1389void agent_app_ht_clean(void)
1390{
1391 struct lttng_ht_node_ulong *node;
1392 struct lttng_ht_iter iter;
1393
a433283e
JG
1394 if (!agent_apps_ht_by_sock) {
1395 return;
1396 }
6a4e4039
JG
1397 rcu_read_lock();
1398 cds_lfht_for_each_entry(agent_apps_ht_by_sock->ht, &iter.iter, node, node) {
1399 struct agent_app *app;
1400
1401 app = caa_container_of(node, struct agent_app, node);
1402 agent_destroy_app_by_sock(app->sock->fd);
1403 }
1404 rcu_read_unlock();
1405
1406 lttng_ht_destroy(agent_apps_ht_by_sock);
f20baf8e
DG
1407}
1408
1409/*
022d91ba 1410 * Update a agent application (given socket) using the given agent.
f20baf8e
DG
1411 *
1412 * Note that this function is most likely to be used with a tracing session
1413 * thus the caller should make sure to hold the appropriate lock(s).
1414 */
022d91ba 1415void agent_update(struct agent *agt, int sock)
f20baf8e
DG
1416{
1417 int ret;
022d91ba
DG
1418 struct agent_app *app;
1419 struct agent_event *event;
f20baf8e 1420 struct lttng_ht_iter iter;
bdf64013 1421 struct agent_app_ctx *ctx;
f20baf8e 1422
022d91ba 1423 assert(agt);
f20baf8e
DG
1424 assert(sock >= 0);
1425
022d91ba 1426 DBG("Agent updating app socket %d", sock);
f20baf8e
DG
1427
1428 rcu_read_lock();
bdf64013
JG
1429 app = agent_find_app_by_sock(sock);
1430 /*
1431 * We are in the registration path thus if the application is gone,
1432 * there is a serious code flow error.
1433 */
1434 assert(app);
022d91ba 1435 cds_lfht_for_each_entry(agt->events->ht, &iter.iter, event, node.node) {
f20baf8e
DG
1436 /* Skip event if disabled. */
1437 if (!event->enabled) {
1438 continue;
1439 }
1440
f20baf8e
DG
1441 ret = enable_event(app, event);
1442 if (ret != LTTNG_OK) {
022d91ba 1443 DBG2("Agent update unable to enable event %s on app pid: %d sock %d",
f20baf8e
DG
1444 event->name, app->pid, app->sock->fd);
1445 /* Let's try the others here and don't assume the app is dead. */
1446 continue;
1447 }
1448 }
bdf64013
JG
1449
1450 cds_list_for_each_entry_rcu(ctx, &agt->app_ctx_list, list_node) {
1451 ret = app_context_op(app, ctx, AGENT_CMD_APP_CTX_ENABLE);
1452 if (ret != LTTNG_OK) {
1453 DBG2("Agent update unable to add application context %s:%s on app pid: %d sock %d",
1454 ctx->provider_name, ctx->ctx_name,
1455 app->pid, app->sock->fd);
1456 continue;
1457 }
1458 }
1459
f20baf8e 1460 rcu_read_unlock();
0475c50c 1461}
This page took 0.112979 seconds and 4 git commands to generate.