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