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