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