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