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