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