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