Fix: illegal memory access in list_events
[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 if (lttng_strncpy(tmp_events[i].name, reply->payload + offset,
357 sizeof(tmp_events[i].name))) {
358 ret = LTTNG_ERR_INVALID;
359 goto error;
360 }
361 tmp_events[i].pid = app->pid;
362 tmp_events[i].enabled = -1;
363 len = strlen(reply->payload + offset) + 1;
364 }
365
366 *events = tmp_events;
367
368 free(reply);
369 return nb_event;
370
371 error_io:
372 ret = LTTNG_ERR_UST_LIST_FAIL;
373 error:
374 free(reply);
375 free(tmp_events);
376 return -ret;
377
378 }
379
380 /*
381 * Internal enable agent event on a agent application. This function
382 * communicates with the agent to enable a given event.
383 *
384 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
385 */
386 static int enable_event(struct agent_app *app, struct agent_event *event)
387 {
388 int ret;
389 char *bytes_to_send;
390 uint64_t data_size;
391 size_t filter_expression_length;
392 uint32_t reply_ret_code;
393 struct lttcomm_agent_enable_event msg;
394 struct lttcomm_agent_generic_reply reply;
395
396 assert(app);
397 assert(app->sock);
398 assert(event);
399
400 DBG2("Agent enabling event %s for app pid: %d and socket %d", event->name,
401 app->pid, app->sock->fd);
402
403 /*
404 * Calculate the payload's size, which is the fixed-size struct followed
405 * by the variable-length filter expression (+1 for the ending \0).
406 */
407 if (!event->filter_expression) {
408 filter_expression_length = 0;
409 } else {
410 filter_expression_length = strlen(event->filter_expression) + 1;
411 }
412 data_size = sizeof(msg) + filter_expression_length;
413
414 memset(&msg, 0, sizeof(msg));
415 msg.loglevel_value = htobe32(event->loglevel_value);
416 msg.loglevel_type = htobe32(event->loglevel_type);
417 if (lttng_strncpy(msg.name, event->name, sizeof(msg.name))) {
418 ret = LTTNG_ERR_INVALID;
419 goto error;
420 }
421 msg.filter_expression_length = htobe32(filter_expression_length);
422
423 ret = send_header(app->sock, data_size, AGENT_CMD_ENABLE, 0);
424 if (ret < 0) {
425 goto error_io;
426 }
427
428 bytes_to_send = zmalloc(data_size);
429 if (!bytes_to_send) {
430 ret = LTTNG_ERR_NOMEM;
431 goto error;
432 }
433
434 memcpy(bytes_to_send, &msg, sizeof(msg));
435 if (filter_expression_length > 0) {
436 memcpy(bytes_to_send + sizeof(msg), event->filter_expression,
437 filter_expression_length);
438 }
439
440 ret = send_payload(app->sock, bytes_to_send, data_size);
441 free(bytes_to_send);
442 if (ret < 0) {
443 goto error_io;
444 }
445
446 ret = recv_reply(app->sock, &reply, sizeof(reply));
447 if (ret < 0) {
448 goto error_io;
449 }
450
451 reply_ret_code = be32toh(reply.ret_code);
452 log_reply_code(reply_ret_code);
453 switch (reply_ret_code) {
454 case AGENT_RET_CODE_SUCCESS:
455 break;
456 case AGENT_RET_CODE_UNKNOWN_NAME:
457 ret = LTTNG_ERR_UST_EVENT_NOT_FOUND;
458 goto error;
459 default:
460 ret = LTTNG_ERR_UNK;
461 goto error;
462 }
463
464 return LTTNG_OK;
465
466 error_io:
467 ret = LTTNG_ERR_UST_ENABLE_FAIL;
468 error:
469 return ret;
470 }
471
472 /*
473 * Send Pascal-style string. Size is sent as a 32-bit big endian integer.
474 */
475 static
476 int send_pstring(struct lttcomm_sock *sock, const char *str, uint32_t len)
477 {
478 int ret;
479 uint32_t len_be;
480
481 len_be = htobe32(len);
482 ret = send_payload(sock, &len_be, sizeof(len_be));
483 if (ret) {
484 goto end;
485 }
486
487 ret = send_payload(sock, str, len);
488 if (ret) {
489 goto end;
490 }
491 end:
492 return ret;
493 }
494
495 /*
496 * Internal enable application context on an agent application. This function
497 * communicates with the agent to enable a given application context.
498 *
499 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
500 */
501 static int app_context_op(struct agent_app *app,
502 struct agent_app_ctx *ctx, enum lttcomm_agent_command cmd)
503 {
504 int ret;
505 uint32_t reply_ret_code;
506 struct lttcomm_agent_generic_reply reply;
507 size_t app_ctx_provider_name_len, app_ctx_name_len, data_size;
508
509 assert(app);
510 assert(app->sock);
511 assert(ctx);
512 assert(cmd == AGENT_CMD_APP_CTX_ENABLE ||
513 cmd == AGENT_CMD_APP_CTX_DISABLE);
514
515 DBG2("Agent %s application %s:%s for app pid: %d and socket %d",
516 cmd == AGENT_CMD_APP_CTX_ENABLE ? "enabling" : "disabling",
517 ctx->provider_name, ctx->ctx_name,
518 app->pid, app->sock->fd);
519
520 /*
521 * Calculate the payload's size, which consists of the size (u32, BE)
522 * of the provider name, the NULL-terminated provider name string, the
523 * size (u32, BE) of the context name, followed by the NULL-terminated
524 * context name string.
525 */
526 app_ctx_provider_name_len = strlen(ctx->provider_name) + 1;
527 app_ctx_name_len = strlen(ctx->ctx_name) + 1;
528 data_size = sizeof(uint32_t) + app_ctx_provider_name_len +
529 sizeof(uint32_t) + app_ctx_name_len;
530
531 ret = send_header(app->sock, data_size, cmd, 0);
532 if (ret < 0) {
533 goto error_io;
534 }
535
536 if (app_ctx_provider_name_len > UINT32_MAX ||
537 app_ctx_name_len > UINT32_MAX) {
538 ERR("Application context name > MAX_UINT32");
539 ret = LTTNG_ERR_INVALID;
540 goto error;
541 }
542
543 ret = send_pstring(app->sock, ctx->provider_name,
544 (uint32_t) app_ctx_provider_name_len);
545 if (ret < 0) {
546 goto error_io;
547 }
548
549 ret = send_pstring(app->sock, ctx->ctx_name,
550 (uint32_t) app_ctx_name_len);
551 if (ret < 0) {
552 goto error_io;
553 }
554
555 ret = recv_reply(app->sock, &reply, sizeof(reply));
556 if (ret < 0) {
557 goto error_io;
558 }
559
560 reply_ret_code = be32toh(reply.ret_code);
561 log_reply_code(reply_ret_code);
562 switch (reply_ret_code) {
563 case AGENT_RET_CODE_SUCCESS:
564 break;
565 default:
566 ret = LTTNG_ERR_UNK;
567 goto error;
568 }
569
570 return LTTNG_OK;
571
572 error_io:
573 ret = LTTNG_ERR_UST_ENABLE_FAIL;
574 error:
575 return ret;
576 }
577
578 /*
579 * Internal disable agent event call on a agent application. This function
580 * communicates with the agent to disable a given event.
581 *
582 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
583 */
584 static int disable_event(struct agent_app *app, struct agent_event *event)
585 {
586 int ret;
587 uint64_t data_size;
588 uint32_t reply_ret_code;
589 struct lttcomm_agent_disable_event msg;
590 struct lttcomm_agent_generic_reply reply;
591
592 assert(app);
593 assert(app->sock);
594 assert(event);
595
596 DBG2("Agent disabling event %s for app pid: %d and socket %d", event->name,
597 app->pid, app->sock->fd);
598
599 data_size = sizeof(msg);
600 memset(&msg, 0, sizeof(msg));
601 if (lttng_strncpy(msg.name, event->name, sizeof(msg.name))) {
602 ret = LTTNG_ERR_INVALID;
603 goto error;
604 }
605
606 ret = send_header(app->sock, data_size, AGENT_CMD_DISABLE, 0);
607 if (ret < 0) {
608 goto error_io;
609 }
610
611 ret = send_payload(app->sock, &msg, sizeof(msg));
612 if (ret < 0) {
613 goto error_io;
614 }
615
616 ret = recv_reply(app->sock, &reply, sizeof(reply));
617 if (ret < 0) {
618 goto error_io;
619 }
620
621 reply_ret_code = be32toh(reply.ret_code);
622 log_reply_code(reply_ret_code);
623 switch (reply_ret_code) {
624 case AGENT_RET_CODE_SUCCESS:
625 break;
626 case AGENT_RET_CODE_UNKNOWN_NAME:
627 ret = LTTNG_ERR_UST_EVENT_NOT_FOUND;
628 goto error;
629 default:
630 ret = LTTNG_ERR_UNK;
631 goto error;
632 }
633
634 return LTTNG_OK;
635
636 error_io:
637 ret = LTTNG_ERR_UST_DISABLE_FAIL;
638 error:
639 return ret;
640 }
641
642 /*
643 * Send back the registration DONE command to a given agent application.
644 *
645 * Return 0 on success or else a negative value.
646 */
647 int agent_send_registration_done(struct agent_app *app)
648 {
649 assert(app);
650 assert(app->sock);
651
652 DBG("Agent sending registration done to app socket %d", app->sock->fd);
653
654 return send_header(app->sock, 0, AGENT_CMD_REG_DONE, 0);
655 }
656
657 /*
658 * Enable agent event on every agent applications registered with the session
659 * daemon.
660 *
661 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
662 */
663 int agent_enable_event(struct agent_event *event,
664 enum lttng_domain_type domain)
665 {
666 int ret;
667 struct agent_app *app;
668 struct lttng_ht_iter iter;
669
670 assert(event);
671
672 rcu_read_lock();
673
674 cds_lfht_for_each_entry(agent_apps_ht_by_sock->ht, &iter.iter, app,
675 node.node) {
676 if (app->domain != domain) {
677 continue;
678 }
679
680 /* Enable event on agent application through TCP socket. */
681 ret = enable_event(app, event);
682 if (ret != LTTNG_OK) {
683 goto error;
684 }
685 }
686
687 event->enabled = 1;
688 ret = LTTNG_OK;
689
690 error:
691 rcu_read_unlock();
692 return ret;
693 }
694
695 static
696 void destroy_app_ctx(struct agent_app_ctx *ctx)
697 {
698 free(ctx->provider_name);
699 free(ctx->ctx_name);
700 free(ctx);
701 }
702
703 static
704 struct agent_app_ctx *create_app_ctx(struct lttng_event_context *ctx)
705 {
706 struct agent_app_ctx *agent_ctx = NULL;
707
708 if (!ctx) {
709 goto end;
710 }
711
712 assert(ctx->ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT);
713 agent_ctx = zmalloc(sizeof(*ctx));
714 if (!agent_ctx) {
715 goto end;
716 }
717
718 agent_ctx->provider_name = strdup(ctx->u.app_ctx.provider_name);
719 agent_ctx->ctx_name = strdup(ctx->u.app_ctx.ctx_name);
720 if (!agent_ctx->provider_name || !agent_ctx->ctx_name) {
721 destroy_app_ctx(agent_ctx);
722 agent_ctx = NULL;
723 }
724 end:
725 return agent_ctx;
726 }
727
728 /*
729 * Enable agent context on every agent applications registered with the session
730 * daemon.
731 *
732 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
733 */
734 int agent_enable_context(struct lttng_event_context *ctx,
735 enum lttng_domain_type domain)
736 {
737 int ret;
738 struct agent_app *app;
739 struct lttng_ht_iter iter;
740
741 assert(ctx);
742 if (ctx->ctx != LTTNG_EVENT_CONTEXT_APP_CONTEXT) {
743 ret = LTTNG_ERR_INVALID;
744 goto error;
745 }
746
747 rcu_read_lock();
748
749 cds_lfht_for_each_entry(agent_apps_ht_by_sock->ht, &iter.iter, app,
750 node.node) {
751 struct agent_app_ctx *agent_ctx;
752
753 if (app->domain != domain) {
754 continue;
755 }
756
757 agent_ctx = create_app_ctx(ctx);
758 if (!agent_ctx) {
759 ret = LTTNG_ERR_NOMEM;
760 goto error_unlock;
761 }
762
763 /* Enable event on agent application through TCP socket. */
764 ret = app_context_op(app, agent_ctx, AGENT_CMD_APP_CTX_ENABLE);
765 destroy_app_ctx(agent_ctx);
766 if (ret != LTTNG_OK) {
767 goto error_unlock;
768 }
769 }
770
771 ret = LTTNG_OK;
772
773 error_unlock:
774 rcu_read_unlock();
775 error:
776 return ret;
777 }
778
779 /*
780 * Disable agent event on every agent application registered with the session
781 * daemon.
782 *
783 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
784 */
785 int agent_disable_event(struct agent_event *event,
786 enum lttng_domain_type domain)
787 {
788 int ret = LTTNG_OK;
789 struct agent_app *app;
790 struct lttng_ht_iter iter;
791
792 assert(event);
793 if (!event->enabled) {
794 goto end;
795 }
796
797 rcu_read_lock();
798
799 cds_lfht_for_each_entry(agent_apps_ht_by_sock->ht, &iter.iter, app,
800 node.node) {
801 if (app->domain != domain) {
802 continue;
803 }
804
805 /* Enable event on agent application through TCP socket. */
806 ret = disable_event(app, event);
807 if (ret != LTTNG_OK) {
808 goto error;
809 }
810 }
811
812 event->enabled = 0;
813
814 error:
815 rcu_read_unlock();
816 end:
817 return ret;
818 }
819
820 /*
821 * Disable agent context on every agent application registered with the session
822 * daemon.
823 *
824 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
825 */
826 int disable_context(struct agent_app_ctx *ctx, enum lttng_domain_type domain)
827 {
828 int ret = LTTNG_OK;
829 struct agent_app *app;
830 struct lttng_ht_iter iter;
831
832 assert(ctx);
833
834 rcu_read_lock();
835 DBG2("Disabling agent application context %s:%s",
836 ctx->provider_name, ctx->ctx_name);
837 cds_lfht_for_each_entry(agent_apps_ht_by_sock->ht, &iter.iter, app,
838 node.node) {
839 if (app->domain != domain) {
840 continue;
841 }
842
843 ret = app_context_op(app, ctx, AGENT_CMD_APP_CTX_DISABLE);
844 if (ret != LTTNG_OK) {
845 goto end;
846 }
847 }
848 end:
849 rcu_read_unlock();
850 return ret;
851 }
852
853 /*
854 * Ask every agent for the list of possible event. Events is allocated with the
855 * events of every agent application.
856 *
857 * Return the number of events or else a negative value.
858 */
859 int agent_list_events(struct lttng_event **events,
860 enum lttng_domain_type domain)
861 {
862 int ret;
863 size_t nbmem, count = 0;
864 struct agent_app *app;
865 struct lttng_event *tmp_events = NULL;
866 struct lttng_ht_iter iter;
867
868 assert(events);
869
870 DBG2("Agent listing events for domain %d", domain);
871
872 nbmem = UST_APP_EVENT_LIST_SIZE;
873 tmp_events = zmalloc(nbmem * sizeof(*tmp_events));
874 if (!tmp_events) {
875 PERROR("zmalloc agent list events");
876 ret = -ENOMEM;
877 goto error;
878 }
879
880 rcu_read_lock();
881 cds_lfht_for_each_entry(agent_apps_ht_by_sock->ht, &iter.iter, app,
882 node.node) {
883 ssize_t nb_ev;
884 struct lttng_event *agent_events;
885
886 /* Skip domain not asked by the list. */
887 if (app->domain != domain) {
888 continue;
889 }
890
891 nb_ev = list_events(app, &agent_events);
892 if (nb_ev < 0) {
893 ret = nb_ev;
894 goto error_unlock;
895 }
896
897 if (count + nb_ev > nbmem) {
898 /* In case the realloc fails, we free the memory */
899 struct lttng_event *new_tmp_events;
900 size_t new_nbmem;
901
902 new_nbmem = max_t(size_t, count + nb_ev, nbmem << 1);
903 DBG2("Reallocating agent event list from %zu to %zu entries",
904 nbmem, new_nbmem);
905 new_tmp_events = realloc(tmp_events,
906 new_nbmem * sizeof(*new_tmp_events));
907 if (!new_tmp_events) {
908 PERROR("realloc agent events");
909 ret = -ENOMEM;
910 free(agent_events);
911 goto error_unlock;
912 }
913 /* Zero the new memory */
914 memset(new_tmp_events + nbmem, 0,
915 (new_nbmem - nbmem) * sizeof(*new_tmp_events));
916 nbmem = new_nbmem;
917 tmp_events = new_tmp_events;
918 }
919 memcpy(tmp_events + count, agent_events,
920 nb_ev * sizeof(*tmp_events));
921 free(agent_events);
922 count += nb_ev;
923 }
924 rcu_read_unlock();
925
926 ret = count;
927 *events = tmp_events;
928 return ret;
929
930 error_unlock:
931 rcu_read_unlock();
932 error:
933 free(tmp_events);
934 return ret;
935 }
936
937 /*
938 * Create a agent app object using the given PID.
939 *
940 * Return newly allocated object or else NULL on error.
941 */
942 struct agent_app *agent_create_app(pid_t pid, enum lttng_domain_type domain,
943 struct lttcomm_sock *sock)
944 {
945 struct agent_app *app;
946
947 assert(sock);
948
949 app = zmalloc(sizeof(*app));
950 if (!app) {
951 PERROR("zmalloc agent create");
952 goto error;
953 }
954
955 app->pid = pid;
956 app->domain = domain;
957 app->sock = sock;
958 lttng_ht_node_init_ulong(&app->node, (unsigned long) app->sock->fd);
959
960 error:
961 return app;
962 }
963
964 /*
965 * Lookup agent app by socket in the global hash table.
966 *
967 * RCU read side lock MUST be acquired.
968 *
969 * Return object if found else NULL.
970 */
971 struct agent_app *agent_find_app_by_sock(int sock)
972 {
973 struct lttng_ht_node_ulong *node;
974 struct lttng_ht_iter iter;
975 struct agent_app *app;
976
977 assert(sock >= 0);
978
979 lttng_ht_lookup(agent_apps_ht_by_sock, (void *)((unsigned long) sock), &iter);
980 node = lttng_ht_iter_get_node_ulong(&iter);
981 if (node == NULL) {
982 goto error;
983 }
984 app = caa_container_of(node, struct agent_app, node);
985
986 DBG3("Agent app pid %d found by sock %d.", app->pid, sock);
987 return app;
988
989 error:
990 DBG3("Agent app NOT found by sock %d.", sock);
991 return NULL;
992 }
993
994 /*
995 * Add agent application object to the global hash table.
996 */
997 void agent_add_app(struct agent_app *app)
998 {
999 assert(app);
1000
1001 DBG3("Agent adding app sock: %d and pid: %d to ht", app->sock->fd, app->pid);
1002 lttng_ht_add_unique_ulong(agent_apps_ht_by_sock, &app->node);
1003 }
1004
1005 /*
1006 * Delete agent application from the global hash table.
1007 *
1008 * rcu_read_lock() must be held by the caller.
1009 */
1010 void agent_delete_app(struct agent_app *app)
1011 {
1012 int ret;
1013 struct lttng_ht_iter iter;
1014
1015 assert(app);
1016
1017 DBG3("Agent deleting app pid: %d and sock: %d", app->pid, app->sock->fd);
1018
1019 iter.iter.node = &app->node.node;
1020 ret = lttng_ht_del(agent_apps_ht_by_sock, &iter);
1021 assert(!ret);
1022 }
1023
1024 /*
1025 * Destroy an agent application object by detaching it from its corresponding
1026 * UST app if one is connected by closing the socket. Finally, perform a
1027 * delayed memory reclaim.
1028 */
1029 void agent_destroy_app(struct agent_app *app)
1030 {
1031 assert(app);
1032
1033 if (app->sock) {
1034 app->sock->ops->close(app->sock);
1035 lttcomm_destroy_sock(app->sock);
1036 }
1037
1038 call_rcu(&app->node.head, destroy_app_agent_rcu);
1039 }
1040
1041 /*
1042 * Initialize an already allocated agent object.
1043 *
1044 * Return 0 on success or else a negative errno value.
1045 */
1046 int agent_init(struct agent *agt)
1047 {
1048 int ret;
1049
1050 assert(agt);
1051
1052 agt->events = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
1053 if (!agt->events) {
1054 ret = -ENOMEM;
1055 goto error;
1056 }
1057 lttng_ht_node_init_u64(&agt->node, agt->domain);
1058
1059 CDS_INIT_LIST_HEAD(&agt->app_ctx_list);
1060 return 0;
1061
1062 error:
1063 return ret;
1064 }
1065
1066 /*
1067 * Add agent object to the given hash table.
1068 */
1069 void agent_add(struct agent *agt, struct lttng_ht *ht)
1070 {
1071 assert(agt);
1072 assert(ht);
1073
1074 DBG3("Agent adding from domain %d", agt->domain);
1075
1076 lttng_ht_add_unique_u64(ht, &agt->node);
1077 }
1078
1079 /*
1080 * Create an agent object for the given domain.
1081 *
1082 * Return the allocated agent or NULL on error.
1083 */
1084 struct agent *agent_create(enum lttng_domain_type domain)
1085 {
1086 int ret;
1087 struct agent *agt;
1088
1089 agt = zmalloc(sizeof(struct agent));
1090 if (!agt) {
1091 goto error;
1092 }
1093 agt->domain = domain;
1094
1095 ret = agent_init(agt);
1096 if (ret < 0) {
1097 free(agt);
1098 agt = NULL;
1099 goto error;
1100 }
1101
1102 error:
1103 return agt;
1104 }
1105
1106 /*
1107 * Create a newly allocated agent event data structure.
1108 * Ownership of filter_expression is taken.
1109 *
1110 * Return a new object else NULL on error.
1111 */
1112 struct agent_event *agent_create_event(const char *name,
1113 enum lttng_loglevel_type loglevel_type, int loglevel_value,
1114 struct lttng_filter_bytecode *filter, char *filter_expression)
1115 {
1116 struct agent_event *event = NULL;
1117
1118 DBG3("Agent create new event with name %s, loglevel type %d, \
1119 loglevel value %d and filter %s",
1120 name, loglevel_type, loglevel_value,
1121 filter_expression ? filter_expression : "NULL");
1122
1123 if (!name) {
1124 ERR("Failed to create agent event; no name provided.");
1125 goto error;
1126 }
1127
1128 event = zmalloc(sizeof(*event));
1129 if (!event) {
1130 goto error;
1131 }
1132
1133 strncpy(event->name, name, sizeof(event->name));
1134 event->name[sizeof(event->name) - 1] = '\0';
1135 lttng_ht_node_init_str(&event->node, event->name);
1136
1137 event->loglevel_value = loglevel_value;
1138 event->loglevel_type = loglevel_type;
1139 event->filter = filter;
1140 event->filter_expression = filter_expression;
1141 error:
1142 return event;
1143 }
1144
1145 /*
1146 * Unique add of a agent event to an agent object.
1147 */
1148 void agent_add_event(struct agent_event *event, struct agent *agt)
1149 {
1150 assert(event);
1151 assert(agt);
1152 assert(agt->events);
1153
1154 DBG3("Agent adding event %s", event->name);
1155 add_unique_agent_event(agt->events, event);
1156 agt->being_used = 1;
1157 }
1158
1159 /*
1160 * Unique add of a agent context to an agent object.
1161 */
1162 int agent_add_context(struct lttng_event_context *ctx, struct agent *agt)
1163 {
1164 int ret = LTTNG_OK;
1165 struct agent_app_ctx *agent_ctx = NULL;
1166
1167 assert(ctx);
1168 assert(agt);
1169 assert(agt->events);
1170 assert(ctx->ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT);
1171
1172 agent_ctx = create_app_ctx(ctx);
1173 if (!agent_ctx) {
1174 ret = LTTNG_ERR_NOMEM;
1175 goto end;
1176 }
1177
1178 DBG3("Agent adding context %s:%s", ctx->u.app_ctx.provider_name,
1179 ctx->u.app_ctx.ctx_name);
1180 cds_list_add_tail_rcu(&agent_ctx->list_node, &agt->app_ctx_list);
1181 end:
1182 return ret;
1183 }
1184
1185 /*
1186 * Find multiple agent events sharing the given name.
1187 *
1188 * RCU read side lock MUST be acquired. It must be held for the
1189 * duration of the iteration.
1190 *
1191 * Sets the given iterator.
1192 */
1193 void agent_find_events_by_name(const char *name, struct agent *agt,
1194 struct lttng_ht_iter* iter)
1195 {
1196 struct lttng_ht *ht;
1197 struct agent_ht_key key;
1198
1199 assert(name);
1200 assert(agt);
1201 assert(agt->events);
1202 assert(iter);
1203
1204 ht = agt->events;
1205 key.name = name;
1206
1207 cds_lfht_lookup(ht->ht, ht->hash_fct((void *) name, lttng_ht_seed),
1208 ht_match_event_by_name, &key, &iter->iter);
1209 }
1210
1211 /*
1212 * Get the next agent event duplicate by name. This should be called
1213 * after a call to agent_find_events_by_name() to iterate on events.
1214 *
1215 * The RCU read lock must be held during the iteration and for as long
1216 * as the object the iterator points to remains in use.
1217 */
1218 void agent_event_next_duplicate(const char *name,
1219 struct agent *agt, struct lttng_ht_iter* iter)
1220 {
1221 struct agent_ht_key key;
1222
1223 key.name = name;
1224
1225 cds_lfht_next_duplicate(agt->events->ht, ht_match_event_by_name,
1226 &key, &iter->iter);
1227 }
1228
1229 /*
1230 * Find a agent event in the given agent using name, loglevel and filter.
1231 *
1232 * RCU read side lock MUST be acquired. It must be kept for as long as
1233 * the returned agent_event is used.
1234 *
1235 * Return object if found else NULL.
1236 */
1237 struct agent_event *agent_find_event(const char *name,
1238 enum lttng_loglevel_type loglevel_type, int loglevel_value,
1239 char *filter_expression, struct agent *agt)
1240 {
1241 struct lttng_ht_node_str *node;
1242 struct lttng_ht_iter iter;
1243 struct lttng_ht *ht;
1244 struct agent_ht_key key;
1245
1246 assert(name);
1247 assert(agt);
1248 assert(agt->events);
1249
1250 ht = agt->events;
1251 key.name = name;
1252 key.loglevel_value = loglevel_value;
1253 key.loglevel_type = loglevel_type;
1254 key.filter_expression = filter_expression;
1255
1256 cds_lfht_lookup(ht->ht, ht->hash_fct((void *) name, lttng_ht_seed),
1257 ht_match_event, &key, &iter.iter);
1258 node = lttng_ht_iter_get_node_str(&iter);
1259 if (node == NULL) {
1260 goto error;
1261 }
1262
1263 DBG3("Agent event found %s.", name);
1264 return caa_container_of(node, struct agent_event, node);
1265
1266 error:
1267 DBG3("Agent event NOT found %s.", name);
1268 return NULL;
1269 }
1270
1271 /*
1272 * Free given agent event. This event must not be globally visible at this
1273 * point (only expected to be used on failure just after event creation). After
1274 * this call, the pointer is not usable anymore.
1275 */
1276 void agent_destroy_event(struct agent_event *event)
1277 {
1278 assert(event);
1279
1280 free(event->filter);
1281 free(event->filter_expression);
1282 free(event->exclusion);
1283 free(event);
1284 }
1285
1286 static
1287 void destroy_app_ctx_rcu(struct rcu_head *head)
1288 {
1289 struct agent_app_ctx *ctx =
1290 caa_container_of(head, struct agent_app_ctx, rcu_node);
1291
1292 destroy_app_ctx(ctx);
1293 }
1294
1295 /*
1296 * Destroy an agent completely.
1297 */
1298 void agent_destroy(struct agent *agt)
1299 {
1300 struct lttng_ht_node_str *node;
1301 struct lttng_ht_iter iter;
1302 struct agent_app_ctx *ctx;
1303
1304 assert(agt);
1305
1306 DBG3("Agent destroy");
1307
1308 rcu_read_lock();
1309 cds_lfht_for_each_entry(agt->events->ht, &iter.iter, node, node) {
1310 int ret;
1311 struct agent_event *event;
1312
1313 /*
1314 * When destroying an event, we have to try to disable it on the
1315 * agent side so the event stops generating data. The return
1316 * value is not important since we have to continue anyway
1317 * destroying the object.
1318 */
1319 event = caa_container_of(node, struct agent_event, node);
1320 (void) agent_disable_event(event, agt->domain);
1321
1322 ret = lttng_ht_del(agt->events, &iter);
1323 assert(!ret);
1324 call_rcu(&node->head, destroy_event_agent_rcu);
1325 }
1326
1327 cds_list_for_each_entry_rcu(ctx, &agt->app_ctx_list, list_node) {
1328 (void) disable_context(ctx, agt->domain);
1329 cds_list_del(&ctx->list_node);
1330 call_rcu(&ctx->rcu_node, destroy_app_ctx_rcu);
1331 }
1332 rcu_read_unlock();
1333 ht_cleanup_push(agt->events);
1334 free(agt);
1335 }
1336
1337 /*
1338 * Allocate agent_apps_ht_by_sock.
1339 */
1340 int agent_app_ht_alloc(void)
1341 {
1342 int ret = 0;
1343
1344 agent_apps_ht_by_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
1345 if (!agent_apps_ht_by_sock) {
1346 ret = -1;
1347 }
1348
1349 return ret;
1350 }
1351
1352 /*
1353 * Destroy a agent application by socket.
1354 */
1355 void agent_destroy_app_by_sock(int sock)
1356 {
1357 struct agent_app *app;
1358
1359 assert(sock >= 0);
1360
1361 /*
1362 * Not finding an application is a very important error that should NEVER
1363 * happen. The hash table deletion is ONLY done through this call when the
1364 * main sessiond thread is torn down.
1365 */
1366 rcu_read_lock();
1367 app = agent_find_app_by_sock(sock);
1368 assert(app);
1369
1370 /* RCU read side lock is assumed to be held by this function. */
1371 agent_delete_app(app);
1372
1373 /* The application is freed in a RCU call but the socket is closed here. */
1374 agent_destroy_app(app);
1375 rcu_read_unlock();
1376 }
1377
1378 /*
1379 * Clean-up the agent app hash table and destroy it.
1380 */
1381 void agent_app_ht_clean(void)
1382 {
1383 struct lttng_ht_node_ulong *node;
1384 struct lttng_ht_iter iter;
1385
1386 if (!agent_apps_ht_by_sock) {
1387 return;
1388 }
1389 rcu_read_lock();
1390 cds_lfht_for_each_entry(agent_apps_ht_by_sock->ht, &iter.iter, node, node) {
1391 struct agent_app *app;
1392
1393 app = caa_container_of(node, struct agent_app, node);
1394 agent_destroy_app_by_sock(app->sock->fd);
1395 }
1396 rcu_read_unlock();
1397
1398 lttng_ht_destroy(agent_apps_ht_by_sock);
1399 }
1400
1401 /*
1402 * Update a agent application (given socket) using the given agent.
1403 *
1404 * Note that this function is most likely to be used with a tracing session
1405 * thus the caller should make sure to hold the appropriate lock(s).
1406 */
1407 void agent_update(struct agent *agt, int sock)
1408 {
1409 int ret;
1410 struct agent_app *app;
1411 struct agent_event *event;
1412 struct lttng_ht_iter iter;
1413 struct agent_app_ctx *ctx;
1414
1415 assert(agt);
1416 assert(sock >= 0);
1417
1418 DBG("Agent updating app socket %d", sock);
1419
1420 rcu_read_lock();
1421 app = agent_find_app_by_sock(sock);
1422 /*
1423 * We are in the registration path thus if the application is gone,
1424 * there is a serious code flow error.
1425 */
1426 assert(app);
1427 cds_lfht_for_each_entry(agt->events->ht, &iter.iter, event, node.node) {
1428 /* Skip event if disabled. */
1429 if (!event->enabled) {
1430 continue;
1431 }
1432
1433 ret = enable_event(app, event);
1434 if (ret != LTTNG_OK) {
1435 DBG2("Agent update unable to enable event %s on app pid: %d sock %d",
1436 event->name, app->pid, app->sock->fd);
1437 /* Let's try the others here and don't assume the app is dead. */
1438 continue;
1439 }
1440 }
1441
1442 cds_list_for_each_entry_rcu(ctx, &agt->app_ctx_list, list_node) {
1443 ret = app_context_op(app, ctx, AGENT_CMD_APP_CTX_ENABLE);
1444 if (ret != LTTNG_OK) {
1445 DBG2("Agent update unable to add application context %s:%s on app pid: %d sock %d",
1446 ctx->provider_name, ctx->ctx_name,
1447 app->pid, app->sock->fd);
1448 continue;
1449 }
1450 }
1451
1452 rcu_read_unlock();
1453 }
This page took 0.091596 seconds and 5 git commands to generate.