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 *
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 _GNU_SOURCE
19 #define _LGPL_SOURCE
20 #include <assert.h>
21 #include <urcu/uatomic.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
32 /*
33 * Match function for the events hash table lookup by name.
34 */
35 static int ht_match_event_by_name(struct cds_lfht_node *node,
36 const void *_key)
37 {
38 struct agent_event *event;
39 const struct agent_ht_key *key;
40
41 assert(node);
42 assert(_key);
43
44 event = caa_container_of(node, struct agent_event, node.node);
45 key = _key;
46
47 /* Match 1 elements of the key: name. */
48
49 /* Event name */
50 if (strncmp(event->name, key->name, sizeof(event->name)) != 0) {
51 goto no_match;
52 }
53 /* Match. */
54 return 1;
55
56 no_match:
57 return 0;
58 }
59
60 /*
61 * Match function for the events hash table lookup by name and loglevel.
62 */
63 static int ht_match_event(struct cds_lfht_node *node,
64 const void *_key)
65 {
66 struct agent_event *event;
67 const struct agent_ht_key *key;
68 int ll_match;
69
70 assert(node);
71 assert(_key);
72
73 event = caa_container_of(node, struct agent_event, node.node);
74 key = _key;
75
76 /* Match 2 elements of the key: name and loglevel. */
77
78 /* Event name */
79 if (strncmp(event->name, key->name, sizeof(event->name)) != 0) {
80 goto no_match;
81 }
82
83 /* Event loglevel value and type. */
84 ll_match = loglevels_match(event->loglevel_type,
85 event->loglevel_value, key->loglevel_type,
86 key->loglevel_value, LTTNG_EVENT_LOGLEVEL_ALL);
87
88 if (!ll_match) {
89 goto no_match;
90 }
91
92 /* Filter expression */
93 if (strncmp(event->filter_expression, key->filter_expression,
94 strlen(event->filter_expression)) != 0) {
95 goto no_match;
96 }
97
98 return 1;
99
100 no_match:
101 return 0;
102 }
103
104 /*
105 * Add unique agent event based on the event name and loglevel.
106 */
107 static void add_unique_agent_event(struct lttng_ht *ht,
108 struct agent_event *event)
109 {
110 struct cds_lfht_node *node_ptr;
111 struct agent_ht_key key;
112
113 assert(ht);
114 assert(ht->ht);
115 assert(event);
116
117 key.name = event->name;
118 key.loglevel_value = event->loglevel_value;
119 key.loglevel_type = event->loglevel_type;
120 key.filter_expression = event->filter_expression;
121
122 node_ptr = cds_lfht_add_unique(ht->ht,
123 ht->hash_fct(event->node.key, lttng_ht_seed),
124 ht_match_event, &key, &event->node.node);
125 assert(node_ptr == &event->node.node);
126 }
127
128 /*
129 * URCU delayed agent event reclaim.
130 */
131 static void destroy_event_agent_rcu(struct rcu_head *head)
132 {
133 struct lttng_ht_node_str *node =
134 caa_container_of(head, struct lttng_ht_node_str, head);
135 struct agent_event *event =
136 caa_container_of(node, struct agent_event, node);
137
138 agent_destroy_event(event);
139 }
140
141 /*
142 * URCU delayed agent app reclaim.
143 */
144 static void destroy_app_agent_rcu(struct rcu_head *head)
145 {
146 struct lttng_ht_node_ulong *node =
147 caa_container_of(head, struct lttng_ht_node_ulong, head);
148 struct agent_app *app =
149 caa_container_of(node, struct agent_app, node);
150
151 free(app);
152 }
153
154 /*
155 * Communication with the agent. Send the message header to the given socket in
156 * big endian.
157 *
158 * Return 0 on success or else a negative errno message of sendmsg() op.
159 */
160 static int send_header(struct lttcomm_sock *sock, uint64_t data_size,
161 uint32_t cmd, uint32_t cmd_version)
162 {
163 int ret;
164 ssize_t size;
165 struct lttcomm_agent_hdr msg;
166
167 assert(sock);
168
169 memset(&msg, 0, sizeof(msg));
170 msg.data_size = htobe64(data_size);
171 msg.cmd = htobe32(cmd);
172 msg.cmd_version = htobe32(cmd_version);
173
174 size = sock->ops->sendmsg(sock, &msg, sizeof(msg), 0);
175 if (size < sizeof(msg)) {
176 ret = -errno;
177 goto error;
178 }
179 ret = 0;
180
181 error:
182 return ret;
183 }
184
185 /*
186 * Communication call with the agent. Send the payload to the given socket. The
187 * header MUST be sent prior to this call.
188 *
189 * Return 0 on success or else a negative errno value of sendmsg() op.
190 */
191 static int send_payload(struct lttcomm_sock *sock, void *data,
192 size_t size)
193 {
194 int ret;
195 ssize_t len;
196
197 assert(sock);
198 assert(data);
199
200 len = sock->ops->sendmsg(sock, data, size, 0);
201 if (len < size) {
202 ret = -errno;
203 goto error;
204 }
205 ret = 0;
206
207 error:
208 return ret;
209 }
210
211 /*
212 * Communication call with the agent. Receive reply from the agent using the
213 * given socket.
214 *
215 * Return 0 on success or else a negative errno value from recvmsg() op.
216 */
217 static int recv_reply(struct lttcomm_sock *sock, void *buf, size_t size)
218 {
219 int ret;
220 ssize_t len;
221
222 assert(sock);
223 assert(buf);
224
225 len = sock->ops->recvmsg(sock, buf, size, 0);
226 if (len < size) {
227 ret = -errno;
228 goto error;
229 }
230 ret = 0;
231
232 error:
233 return ret;
234 }
235
236 /*
237 * Internal event listing for a given app. Populate events.
238 *
239 * Return number of element in the list or else a negative LTTNG_ERR* code.
240 * On success, the caller is responsible for freeing the memory
241 * allocated for "events".
242 */
243 static ssize_t list_events(struct agent_app *app, struct lttng_event **events)
244 {
245 int ret, i, len = 0, offset = 0;
246 uint32_t nb_event;
247 size_t data_size;
248 struct lttng_event *tmp_events = NULL;
249 struct lttcomm_agent_list_reply *reply = NULL;
250 struct lttcomm_agent_list_reply_hdr reply_hdr;
251
252 assert(app);
253 assert(app->sock);
254 assert(events);
255
256 DBG2("Agent listing events for app pid: %d and socket %d", app->pid,
257 app->sock->fd);
258
259 ret = send_header(app->sock, 0, AGENT_CMD_LIST, 0);
260 if (ret < 0) {
261 goto error_io;
262 }
263
264 /* Get list header so we know how much we'll receive. */
265 ret = recv_reply(app->sock, &reply_hdr, sizeof(reply_hdr));
266 if (ret < 0) {
267 goto error_io;
268 }
269
270 switch (be32toh(reply_hdr.ret_code)) {
271 case AGENT_RET_CODE_SUCCESS:
272 data_size = be32toh(reply_hdr.data_size) + sizeof(*reply);
273 break;
274 default:
275 ERR("Agent returned an unknown code: %" PRIu32,
276 be32toh(reply_hdr.ret_code));
277 ret = LTTNG_ERR_FATAL;
278 goto error;
279 }
280
281 reply = zmalloc(data_size);
282 if (!reply) {
283 ret = LTTNG_ERR_NOMEM;
284 goto error;
285 }
286
287 /* Get the list with the appropriate data size. */
288 ret = recv_reply(app->sock, reply, data_size);
289 if (ret < 0) {
290 goto error_io;
291 }
292
293 nb_event = be32toh(reply->nb_event);
294 tmp_events = zmalloc(sizeof(*tmp_events) * nb_event);
295 if (!tmp_events) {
296 ret = LTTNG_ERR_NOMEM;
297 goto error;
298 }
299
300 for (i = 0; i < nb_event; i++) {
301 offset += len;
302 strncpy(tmp_events[i].name, reply->payload + offset,
303 sizeof(tmp_events[i].name));
304 tmp_events[i].pid = app->pid;
305 tmp_events[i].enabled = -1;
306 len = strlen(reply->payload + offset) + 1;
307 }
308
309 *events = tmp_events;
310
311 free(reply);
312 return nb_event;
313
314 error_io:
315 ret = LTTNG_ERR_UST_LIST_FAIL;
316 error:
317 free(reply);
318 free(tmp_events);
319 return -ret;
320
321 }
322
323 /*
324 * Internal enable agent event on a agent application. This function
325 * communicates with the agent to enable a given event.
326 *
327 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
328 */
329 static int enable_event(struct agent_app *app, struct agent_event *event)
330 {
331 int ret;
332 uint64_t data_size;
333 struct lttcomm_agent_enable msg;
334 struct lttcomm_agent_generic_reply reply;
335
336 assert(app);
337 assert(app->sock);
338 assert(event);
339
340 DBG2("Agent enabling event %s for app pid: %d and socket %d", event->name,
341 app->pid, app->sock->fd);
342
343 data_size = sizeof(msg);
344
345 ret = send_header(app->sock, data_size, AGENT_CMD_ENABLE, 0);
346 if (ret < 0) {
347 goto error_io;
348 }
349
350 memset(&msg, 0, sizeof(msg));
351 msg.loglevel_value = event->loglevel_value;
352 msg.loglevel_type = event->loglevel_type;
353 strncpy(msg.name, event->name, sizeof(msg.name));
354 ret = send_payload(app->sock, &msg, sizeof(msg));
355 if (ret < 0) {
356 goto error_io;
357 }
358
359 ret = recv_reply(app->sock, &reply, sizeof(reply));
360 if (ret < 0) {
361 goto error_io;
362 }
363
364 switch (be32toh(reply.ret_code)) {
365 case AGENT_RET_CODE_SUCCESS:
366 break;
367 case AGENT_RET_CODE_UNKNOWN_NAME:
368 ret = LTTNG_ERR_UST_EVENT_NOT_FOUND;
369 goto error;
370 default:
371 ERR("Agent returned an unknown code: %" PRIu32,
372 be32toh(reply.ret_code));
373 ret = LTTNG_ERR_FATAL;
374 goto error;
375 }
376
377 return LTTNG_OK;
378
379 error_io:
380 ret = LTTNG_ERR_UST_ENABLE_FAIL;
381 error:
382 return ret;
383 }
384
385 /*
386 * Internal disable agent event call on a agent application. This function
387 * communicates with the agent to disable a given event.
388 *
389 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
390 */
391 static int disable_event(struct agent_app *app, struct agent_event *event)
392 {
393 int ret;
394 uint64_t data_size;
395 struct lttcomm_agent_disable msg;
396 struct lttcomm_agent_generic_reply reply;
397
398 assert(app);
399 assert(app->sock);
400 assert(event);
401
402 DBG2("Agent disabling event %s for app pid: %d and socket %d", event->name,
403 app->pid, app->sock->fd);
404
405 data_size = sizeof(msg);
406 memset(&msg, 0, sizeof(msg));
407 if (lttng_strncpy(msg.name, event->name, sizeof(msg.name))) {
408 ret = LTTNG_ERR_INVALID;
409 goto error;
410 }
411
412 ret = send_header(app->sock, data_size, AGENT_CMD_DISABLE, 0);
413 if (ret < 0) {
414 goto error_io;
415 }
416
417 ret = send_payload(app->sock, &msg, sizeof(msg));
418 if (ret < 0) {
419 goto error_io;
420 }
421
422 ret = recv_reply(app->sock, &reply, sizeof(reply));
423 if (ret < 0) {
424 goto error_io;
425 }
426
427 switch (be32toh(reply.ret_code)) {
428 case AGENT_RET_CODE_SUCCESS:
429 break;
430 case AGENT_RET_CODE_UNKNOWN_NAME:
431 ret = LTTNG_ERR_UST_EVENT_NOT_FOUND;
432 goto error;
433 default:
434 ERR("Agent returned an unknown code: %" PRIu32,
435 be32toh(reply.ret_code));
436 ret = LTTNG_ERR_FATAL;
437 goto error;
438 }
439
440 return LTTNG_OK;
441
442 error_io:
443 ret = LTTNG_ERR_UST_DISABLE_FAIL;
444 error:
445 return ret;
446 }
447
448 /*
449 * Send back the registration DONE command to a given agent application.
450 *
451 * Return 0 on success or else a negative value.
452 */
453 int agent_send_registration_done(struct agent_app *app)
454 {
455 assert(app);
456 assert(app->sock);
457
458 DBG("Agent sending registration done to app socket %d", app->sock->fd);
459
460 return send_header(app->sock, 0, AGENT_CMD_REG_DONE, 0);
461 }
462
463 /*
464 * Enable agent event on every agent applications registered with the session
465 * daemon.
466 *
467 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
468 */
469 int agent_enable_event(struct agent_event *event,
470 enum lttng_domain_type domain)
471 {
472 int ret;
473 struct agent_app *app;
474 struct lttng_ht_iter iter;
475
476 assert(event);
477
478 rcu_read_lock();
479
480 cds_lfht_for_each_entry(agent_apps_ht_by_sock->ht, &iter.iter, app,
481 node.node) {
482 if (app->domain != domain) {
483 continue;
484 }
485
486 /* Enable event on agent application through TCP socket. */
487 ret = enable_event(app, event);
488 if (ret != LTTNG_OK) {
489 goto error;
490 }
491 }
492
493 event->enabled = 1;
494 ret = LTTNG_OK;
495
496 error:
497 rcu_read_unlock();
498 return ret;
499 }
500
501 /*
502 * Disable agent event on every agent applications registered with the session
503 * daemon.
504 *
505 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
506 */
507 int agent_disable_event(struct agent_event *event,
508 enum lttng_domain_type domain)
509 {
510 int ret = LTTNG_OK;
511 struct agent_app *app;
512 struct lttng_ht_iter iter;
513
514 assert(event);
515 if (!event->enabled) {
516 goto end;
517 }
518
519 rcu_read_lock();
520
521 cds_lfht_for_each_entry(agent_apps_ht_by_sock->ht, &iter.iter, app,
522 node.node) {
523 if (app->domain != domain) {
524 continue;
525 }
526
527 /* Enable event on agent application through TCP socket. */
528 ret = disable_event(app, event);
529 if (ret != LTTNG_OK) {
530 goto error;
531 }
532 }
533
534 event->enabled = 0;
535
536 error:
537 rcu_read_unlock();
538 end:
539 return ret;
540 }
541
542 /*
543 * Ask every agent for the list of possible event. Events is allocated with the
544 * events of every agent application.
545 *
546 * Return the number of events or else a negative value.
547 */
548 int agent_list_events(struct lttng_event **events,
549 enum lttng_domain_type domain)
550 {
551 int ret;
552 size_t nbmem, count = 0;
553 struct agent_app *app;
554 struct lttng_event *tmp_events = NULL;
555 struct lttng_ht_iter iter;
556
557 assert(events);
558
559 DBG2("Agent listing events for domain %d", domain);
560
561 nbmem = UST_APP_EVENT_LIST_SIZE;
562 tmp_events = zmalloc(nbmem * sizeof(*tmp_events));
563 if (!tmp_events) {
564 PERROR("zmalloc agent list events");
565 ret = -ENOMEM;
566 goto error;
567 }
568
569 rcu_read_lock();
570 cds_lfht_for_each_entry(agent_apps_ht_by_sock->ht, &iter.iter, app,
571 node.node) {
572 ssize_t nb_ev;
573 struct lttng_event *agent_events;
574
575 /* Skip domain not asked by the list. */
576 if (app->domain != domain) {
577 continue;
578 }
579
580 nb_ev = list_events(app, &agent_events);
581 if (nb_ev < 0) {
582 ret = nb_ev;
583 goto error_unlock;
584 }
585
586 if (count + nb_ev > nbmem) {
587 /* In case the realloc fails, we free the memory */
588 struct lttng_event *new_tmp_events;
589 size_t new_nbmem;
590
591 new_nbmem = max_t(size_t, count + nb_ev, nbmem << 1);
592 DBG2("Reallocating agent event list from %zu to %zu entries",
593 nbmem, new_nbmem);
594 new_tmp_events = realloc(tmp_events,
595 new_nbmem * sizeof(*new_tmp_events));
596 if (!new_tmp_events) {
597 PERROR("realloc agent events");
598 ret = -ENOMEM;
599 free(agent_events);
600 goto error_unlock;
601 }
602 /* Zero the new memory */
603 memset(new_tmp_events + nbmem, 0,
604 (new_nbmem - nbmem) * sizeof(*new_tmp_events));
605 nbmem = new_nbmem;
606 tmp_events = new_tmp_events;
607 }
608 memcpy(tmp_events + count, agent_events,
609 nb_ev * sizeof(*tmp_events));
610 free(agent_events);
611 count += nb_ev;
612 }
613 rcu_read_unlock();
614
615 ret = count;
616 *events = tmp_events;
617 return ret;
618
619 error_unlock:
620 rcu_read_unlock();
621 error:
622 free(tmp_events);
623 return ret;
624 }
625
626 /*
627 * Create a agent app object using the given PID.
628 *
629 * Return newly allocated object or else NULL on error.
630 */
631 struct agent_app *agent_create_app(pid_t pid, enum lttng_domain_type domain,
632 struct lttcomm_sock *sock)
633 {
634 struct agent_app *app;
635
636 assert(sock);
637
638 app = zmalloc(sizeof(*app));
639 if (!app) {
640 PERROR("zmalloc agent create");
641 goto error;
642 }
643
644 app->pid = pid;
645 app->domain = domain;
646 app->sock = sock;
647 lttng_ht_node_init_ulong(&app->node, (unsigned long) app->sock->fd);
648
649 error:
650 return app;
651 }
652
653 /*
654 * Lookup agent app by socket in the global hash table.
655 *
656 * RCU read side lock MUST be acquired.
657 *
658 * Return object if found else NULL.
659 */
660 struct agent_app *agent_find_app_by_sock(int sock)
661 {
662 struct lttng_ht_node_ulong *node;
663 struct lttng_ht_iter iter;
664 struct agent_app *app;
665
666 assert(sock >= 0);
667
668 lttng_ht_lookup(agent_apps_ht_by_sock, (void *)((unsigned long) sock), &iter);
669 node = lttng_ht_iter_get_node_ulong(&iter);
670 if (node == NULL) {
671 goto error;
672 }
673 app = caa_container_of(node, struct agent_app, node);
674
675 DBG3("Agent app pid %d found by sock %d.", app->pid, sock);
676 return app;
677
678 error:
679 DBG3("Agent app NOT found by sock %d.", sock);
680 return NULL;
681 }
682
683 /*
684 * Add agent application object to the global hash table.
685 */
686 void agent_add_app(struct agent_app *app)
687 {
688 assert(app);
689
690 DBG3("Agent adding app sock: %d and pid: %d to ht", app->sock->fd, app->pid);
691
692 rcu_read_lock();
693 lttng_ht_add_unique_ulong(agent_apps_ht_by_sock, &app->node);
694 rcu_read_unlock();
695 }
696
697 /*
698 * Delete agent application from the global hash table.
699 *
700 * rcu_read_lock() must be held by the caller.
701 */
702 void agent_delete_app(struct agent_app *app)
703 {
704 int ret;
705 struct lttng_ht_iter iter;
706
707 assert(app);
708
709 DBG3("Agent deleting app pid: %d and sock: %d", app->pid, app->sock->fd);
710
711 iter.iter.node = &app->node.node;
712 ret = lttng_ht_del(agent_apps_ht_by_sock, &iter);
713 assert(!ret);
714 }
715
716 /*
717 * Destroy a agent application object by detaching it from its corresponding
718 * UST app if one is connected by closing the socket. Finally, perform a
719 * delayed memory reclaim.
720 */
721 void agent_destroy_app(struct agent_app *app)
722 {
723 assert(app);
724
725 if (app->sock) {
726 app->sock->ops->close(app->sock);
727 lttcomm_destroy_sock(app->sock);
728 }
729
730 call_rcu(&app->node.head, destroy_app_agent_rcu);
731 }
732
733 /*
734 * Initialize an already allocated agent object.
735 *
736 * Return 0 on success or else a negative errno value.
737 */
738 int agent_init(struct agent *agt)
739 {
740 int ret;
741
742 assert(agt);
743
744 agt->events = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
745 if (!agt->events) {
746 ret = -ENOMEM;
747 goto error;
748 }
749 lttng_ht_node_init_u64(&agt->node, agt->domain);
750
751 return 0;
752
753 error:
754 return ret;
755 }
756
757 /*
758 * Add agent object to the given hash table.
759 */
760 void agent_add(struct agent *agt, struct lttng_ht *ht)
761 {
762 assert(agt);
763 assert(ht);
764
765 DBG3("Agent adding from domain %d", agt->domain);
766
767 rcu_read_lock();
768 lttng_ht_add_unique_u64(ht, &agt->node);
769 rcu_read_unlock();
770 }
771
772 /*
773 * Create an agent object for the given domain.
774 *
775 * Return the allocated agent or NULL on error.
776 */
777 struct agent *agent_create(enum lttng_domain_type domain)
778 {
779 int ret;
780 struct agent *agt;
781
782 agt = zmalloc(sizeof(*agt));
783 if (!agt) {
784 goto error;
785 }
786 agt->domain = domain;
787
788 ret = agent_init(agt);
789 if (ret < 0) {
790 free(agt);
791 agt = NULL;
792 goto error;
793 }
794
795 error:
796 return agt;
797 }
798
799 /*
800 * Create a newly allocated agent event data structure.
801 * Ownership of filter_expression is taken.
802 *
803 * Return a new object else NULL on error.
804 */
805 struct agent_event *agent_create_event(const char *name,
806 enum lttng_loglevel_type loglevel_type, int loglevel_value,
807 struct lttng_filter_bytecode *filter, char *filter_expression)
808 {
809 struct agent_event *event = NULL;
810
811 DBG3("Agent create new event with name %s, loglevel type %d, \
812 loglevel value %d and filter %s",
813 name, loglevel_type, loglevel_value,
814 filter_expression ? filter_expression : "NULL");
815
816 if (!name) {
817 ERR("Failed to create agent event; no name provided.");
818 goto error;
819 }
820
821 event = zmalloc(sizeof(*event));
822 if (!event) {
823 goto error;
824 }
825
826 strncpy(event->name, name, sizeof(event->name));
827 event->name[sizeof(event->name) - 1] = '\0';
828 lttng_ht_node_init_str(&event->node, event->name);
829
830 event->loglevel_value = loglevel_value;
831 event->loglevel_type = loglevel_type;
832 event->filter = filter;
833 event->filter_expression = filter_expression;
834 error:
835 return event;
836 }
837
838 /*
839 * Unique add of a agent event to an agent object.
840 */
841 void agent_add_event(struct agent_event *event, struct agent *agt)
842 {
843 assert(event);
844 assert(agt);
845 assert(agt->events);
846
847 DBG3("Agent adding event %s", event->name);
848
849 rcu_read_lock();
850 add_unique_agent_event(agt->events, event);
851 rcu_read_unlock();
852 agt->being_used = 1;
853 }
854
855 /*
856 * Find multiple agent events sharing the given name.
857 *
858 * RCU read side lock MUST be acquired. It must be held for the
859 * duration of the iteration.
860 *
861 * Sets the given iterator.
862 */
863 void agent_find_events_by_name(const char *name, struct agent *agt,
864 struct lttng_ht_iter* iter)
865 {
866 struct lttng_ht *ht;
867 struct agent_ht_key key;
868
869 assert(name);
870 assert(agt);
871 assert(agt->events);
872 assert(iter);
873
874 ht = agt->events;
875 key.name = name;
876
877 cds_lfht_lookup(ht->ht, ht->hash_fct((void *) name, lttng_ht_seed),
878 ht_match_event_by_name, &key, &iter->iter);
879 }
880
881 /*
882 * Get the next agent event duplicate by name. This should be called
883 * after a call to agent_find_events_by_name() to iterate on events.
884 *
885 * The RCU read lock must be held during the iteration and for as long
886 * as the object the iterator points to remains in use.
887 */
888 void agent_event_next_duplicate(const char *name,
889 struct agent *agt, struct lttng_ht_iter* iter)
890 {
891 struct agent_ht_key key;
892
893 key.name = name;
894
895 cds_lfht_next_duplicate(agt->events->ht, ht_match_event_by_name,
896 &key, &iter->iter);
897 }
898
899 /*
900 * Find a agent event in the given agent using name, loglevel and filter.
901 *
902 * RCU read side lock MUST be acquired.
903 *
904 * Return object if found else NULL.
905 */
906 struct agent_event *agent_find_event(const char *name,
907 enum lttng_loglevel_type loglevel_type, int loglevel_value,
908 char *filter_expression, struct agent *agt)
909 {
910 struct lttng_ht_node_str *node;
911 struct lttng_ht_iter iter;
912 struct lttng_ht *ht;
913 struct agent_ht_key key;
914
915 assert(name);
916 assert(agt);
917 assert(agt->events);
918
919 ht = agt->events;
920 key.name = name;
921 key.loglevel_value = loglevel_value;
922 key.loglevel_type = loglevel_type;
923 key.filter_expression = filter_expression;
924
925 cds_lfht_lookup(ht->ht, ht->hash_fct((void *) name, lttng_ht_seed),
926 ht_match_event, &key, &iter.iter);
927 node = lttng_ht_iter_get_node_str(&iter);
928 if (node == NULL) {
929 goto error;
930 }
931
932 DBG3("Agent event found %s.", name);
933 return caa_container_of(node, struct agent_event, node);
934
935 error:
936 DBG3("Agent event NOT found %s.", name);
937 return NULL;
938 }
939
940 /*
941 * Free given agent event. This event must not be globally visible at this
942 * point (only expected to be used on failure just after event creation). After
943 * this call, the pointer is not usable anymore.
944 */
945 void agent_destroy_event(struct agent_event *event)
946 {
947 assert(event);
948
949 free(event->filter);
950 free(event->filter_expression);
951 free(event->exclusion);
952 free(event);
953 }
954
955 /*
956 * Destroy an agent completely.
957 */
958 void agent_destroy(struct agent *agt)
959 {
960 struct lttng_ht_node_str *node;
961 struct lttng_ht_iter iter;
962
963 assert(agt);
964
965 DBG3("Agent destroy");
966
967 /*
968 * Just ignore if no events hash table exists. This is possible if for
969 * instance an agent object was allocated but not initialized.
970 */
971 if (!agt->events) {
972 return;
973 }
974
975 rcu_read_lock();
976 cds_lfht_for_each_entry(agt->events->ht, &iter.iter, node, node) {
977 int ret;
978 struct agent_event *event;
979
980 /*
981 * When destroying an event, we have to try to disable it on the agent
982 * side so the event stops generating data. The return value is not
983 * important since we have to continue anyway destroying the object.
984 */
985 event = caa_container_of(node, struct agent_event, node);
986 (void) agent_disable_event(event, agt->domain);
987
988 ret = lttng_ht_del(agt->events, &iter);
989 assert(!ret);
990 call_rcu(&node->head, destroy_event_agent_rcu);
991 }
992 rcu_read_unlock();
993
994 ht_cleanup_push(agt->events);
995 free(agt);
996 }
997
998 /*
999 * Allocate agent_apps_ht_by_sock.
1000 */
1001 int agent_app_ht_alloc(void)
1002 {
1003 int ret = 0;
1004
1005 agent_apps_ht_by_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
1006 if (!agent_apps_ht_by_sock) {
1007 ret = -1;
1008 }
1009
1010 return ret;
1011 }
1012
1013 /*
1014 * Destroy a agent application by socket.
1015 */
1016 void agent_destroy_app_by_sock(int sock)
1017 {
1018 struct agent_app *app;
1019
1020 assert(sock >= 0);
1021
1022 /*
1023 * Not finding an application is a very important error that should NEVER
1024 * happen. The hash table deletion is ONLY done through this call when the
1025 * main sessiond thread is torn down.
1026 */
1027 rcu_read_lock();
1028 app = agent_find_app_by_sock(sock);
1029 assert(app);
1030
1031 /* RCU read side lock is assumed to be held by this function. */
1032 agent_delete_app(app);
1033
1034 /* The application is freed in a RCU call but the socket is closed here. */
1035 agent_destroy_app(app);
1036 rcu_read_unlock();
1037 }
1038
1039 /*
1040 * Clean-up the agent app hash table and destroy it.
1041 */
1042 void agent_app_ht_clean(void)
1043 {
1044 struct lttng_ht_node_ulong *node;
1045 struct lttng_ht_iter iter;
1046
1047 if (!agent_apps_ht_by_sock) {
1048 return;
1049 }
1050 rcu_read_lock();
1051 cds_lfht_for_each_entry(agent_apps_ht_by_sock->ht, &iter.iter, node, node) {
1052 struct agent_app *app;
1053
1054 app = caa_container_of(node, struct agent_app, node);
1055 agent_destroy_app_by_sock(app->sock->fd);
1056 }
1057 rcu_read_unlock();
1058
1059 lttng_ht_destroy(agent_apps_ht_by_sock);
1060 }
1061
1062 /*
1063 * Update a agent application (given socket) using the given agent.
1064 *
1065 * Note that this function is most likely to be used with a tracing session
1066 * thus the caller should make sure to hold the appropriate lock(s).
1067 */
1068 void agent_update(struct agent *agt, int sock)
1069 {
1070 int ret;
1071 struct agent_app *app;
1072 struct agent_event *event;
1073 struct lttng_ht_iter iter;
1074
1075 assert(agt);
1076 assert(sock >= 0);
1077
1078 DBG("Agent updating app socket %d", sock);
1079
1080 rcu_read_lock();
1081 cds_lfht_for_each_entry(agt->events->ht, &iter.iter, event, node.node) {
1082 /* Skip event if disabled. */
1083 if (!event->enabled) {
1084 continue;
1085 }
1086
1087 app = agent_find_app_by_sock(sock);
1088 /*
1089 * We are in the registration path thus if the application is gone,
1090 * there is a serious code flow error.
1091 */
1092 assert(app);
1093
1094 ret = enable_event(app, event);
1095 if (ret != LTTNG_OK) {
1096 DBG2("Agent update unable to enable event %s on app pid: %d sock %d",
1097 event->name, app->pid, app->sock->fd);
1098 /* Let's try the others here and don't assume the app is dead. */
1099 continue;
1100 }
1101 }
1102 rcu_read_unlock();
1103 }
This page took 0.052345 seconds and 4 git commands to generate.