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