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