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