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