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