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