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