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