Fix: include loglevel type in agent event's primary key
[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 /* Event loglevel value and type. */
121 if (event->loglevel_type == key->loglevel_type) {
122 /* Same loglevel type. */
123 if (key->loglevel_type != LTTNG_EVENT_LOGLEVEL_ALL) {
124 /*
125 * Loglevel value must also match since the loglevel
126 * type is not all.
127 */
128 if (event->loglevel_value != key->loglevel_value) {
129 goto no_match;
130 }
131 }
132 } else {
133 /* Loglevel type is different: no match. */
134 goto no_match;
135 }
136
137 return 1;
138
139 no_match:
140 return 0;
141 }
142
143 /*
144 * Add unique agent event based on the event name and loglevel.
145 */
146 static void add_unique_agent_event(struct lttng_ht *ht,
147 struct agent_event *event)
148 {
149 struct cds_lfht_node *node_ptr;
150 struct agent_ht_key key;
151
152 assert(ht);
153 assert(ht->ht);
154 assert(event);
155
156 key.name = event->name;
157 key.loglevel_value = event->loglevel_value;
158 key.loglevel_type = event->loglevel_type;
159
160 node_ptr = cds_lfht_add_unique(ht->ht,
161 ht->hash_fct(event->node.key, lttng_ht_seed),
162 ht_match_event, &key, &event->node.node);
163 assert(node_ptr == &event->node.node);
164 }
165
166 /*
167 * URCU delayed agent event reclaim.
168 */
169 static void destroy_event_agent_rcu(struct rcu_head *head)
170 {
171 struct lttng_ht_node_str *node =
172 caa_container_of(head, struct lttng_ht_node_str, head);
173 struct agent_event *event =
174 caa_container_of(node, struct agent_event, node);
175
176 agent_destroy_event(event);
177 }
178
179 /*
180 * URCU delayed agent app reclaim.
181 */
182 static void destroy_app_agent_rcu(struct rcu_head *head)
183 {
184 struct lttng_ht_node_ulong *node =
185 caa_container_of(head, struct lttng_ht_node_ulong, head);
186 struct agent_app *app =
187 caa_container_of(node, struct agent_app, node);
188
189 free(app);
190 }
191
192 /*
193 * Communication with the agent. Send the message header to the given socket in
194 * big endian.
195 *
196 * Return 0 on success or else a negative errno message of sendmsg() op.
197 */
198 static int send_header(struct lttcomm_sock *sock, uint64_t data_size,
199 uint32_t cmd, uint32_t cmd_version)
200 {
201 int ret;
202 ssize_t size;
203 struct lttcomm_agent_hdr msg;
204
205 assert(sock);
206
207 memset(&msg, 0, sizeof(msg));
208 msg.data_size = htobe64(data_size);
209 msg.cmd = htobe32(cmd);
210 msg.cmd_version = htobe32(cmd_version);
211
212 size = sock->ops->sendmsg(sock, &msg, sizeof(msg), 0);
213 if (size < sizeof(msg)) {
214 ret = -errno;
215 goto error;
216 }
217 ret = 0;
218
219 error:
220 return ret;
221 }
222
223 /*
224 * Communication call with the agent. Send the payload to the given socket. The
225 * header MUST be sent prior to this call.
226 *
227 * Return 0 on success or else a negative errno value of sendmsg() op.
228 */
229 static int send_payload(struct lttcomm_sock *sock, void *data,
230 size_t size)
231 {
232 int ret;
233 ssize_t len;
234
235 assert(sock);
236 assert(data);
237
238 len = sock->ops->sendmsg(sock, data, size, 0);
239 if (len < size) {
240 ret = -errno;
241 goto error;
242 }
243 ret = 0;
244
245 error:
246 return ret;
247 }
248
249 /*
250 * Communication call with the agent. Receive reply from the agent using the
251 * given socket.
252 *
253 * Return 0 on success or else a negative errno value from recvmsg() op.
254 */
255 static int recv_reply(struct lttcomm_sock *sock, void *buf, size_t size)
256 {
257 int ret;
258 ssize_t len;
259
260 assert(sock);
261 assert(buf);
262
263 len = sock->ops->recvmsg(sock, buf, size, 0);
264 if (len < size) {
265 ret = -errno;
266 goto error;
267 }
268 ret = 0;
269
270 error:
271 return ret;
272 }
273
274 /*
275 * Internal event listing for a given app. Populate events.
276 *
277 * Return number of element in the list or else a negative LTTNG_ERR* code.
278 * On success, the caller is responsible for freeing the memory
279 * allocated for "events".
280 */
281 static ssize_t list_events(struct agent_app *app, struct lttng_event **events)
282 {
283 int ret, i, len = 0, offset = 0;
284 uint32_t nb_event;
285 size_t data_size;
286 uint32_t reply_ret_code;
287 struct lttng_event *tmp_events = NULL;
288 struct lttcomm_agent_list_reply *reply = NULL;
289 struct lttcomm_agent_list_reply_hdr reply_hdr;
290
291 assert(app);
292 assert(app->sock);
293 assert(events);
294
295 DBG2("Agent listing events for app pid: %d and socket %d", app->pid,
296 app->sock->fd);
297
298 ret = send_header(app->sock, 0, AGENT_CMD_LIST, 0);
299 if (ret < 0) {
300 goto error_io;
301 }
302
303 /* Get list header so we know how much we'll receive. */
304 ret = recv_reply(app->sock, &reply_hdr, sizeof(reply_hdr));
305 if (ret < 0) {
306 goto error_io;
307 }
308
309 reply_ret_code = be32toh(reply_hdr.ret_code);
310 log_reply_code(reply_ret_code);
311 switch (reply_ret_code) {
312 case AGENT_RET_CODE_SUCCESS:
313 data_size = be32toh(reply_hdr.data_size) + sizeof(*reply);
314 break;
315 default:
316 ret = LTTNG_ERR_UNK;
317 goto error;
318 }
319
320 reply = zmalloc(data_size);
321 if (!reply) {
322 ret = LTTNG_ERR_NOMEM;
323 goto error;
324 }
325
326 /* Get the list with the appropriate data size. */
327 ret = recv_reply(app->sock, reply, data_size);
328 if (ret < 0) {
329 goto error_io;
330 }
331
332 nb_event = be32toh(reply->nb_event);
333 tmp_events = zmalloc(sizeof(*tmp_events) * nb_event);
334 if (!tmp_events) {
335 ret = LTTNG_ERR_NOMEM;
336 goto error;
337 }
338
339 for (i = 0; i < nb_event; i++) {
340 offset += len;
341 strncpy(tmp_events[i].name, reply->payload + offset,
342 sizeof(tmp_events[i].name));
343 tmp_events[i].pid = app->pid;
344 tmp_events[i].enabled = -1;
345 len = strlen(reply->payload + offset) + 1;
346 }
347
348 *events = tmp_events;
349
350 free(reply);
351 return nb_event;
352
353 error_io:
354 ret = LTTNG_ERR_UST_LIST_FAIL;
355 error:
356 free(reply);
357 free(tmp_events);
358 return -ret;
359
360 }
361
362 /*
363 * Internal enable agent event on a agent application. This function
364 * communicates with the agent to enable a given event.
365 *
366 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
367 */
368 static int enable_event(struct agent_app *app, struct agent_event *event)
369 {
370 int ret;
371 uint64_t data_size;
372 uint32_t reply_ret_code;
373 struct lttcomm_agent_enable msg;
374 struct lttcomm_agent_generic_reply reply;
375
376 assert(app);
377 assert(app->sock);
378 assert(event);
379
380 DBG2("Agent enabling event %s for app pid: %d and socket %d", event->name,
381 app->pid, app->sock->fd);
382
383 data_size = sizeof(msg);
384
385 ret = send_header(app->sock, data_size, AGENT_CMD_ENABLE, 0);
386 if (ret < 0) {
387 goto error_io;
388 }
389
390 memset(&msg, 0, sizeof(msg));
391 msg.loglevel_value = event->loglevel_value;
392 msg.loglevel_type = event->loglevel_type;
393 strncpy(msg.name, event->name, sizeof(msg.name));
394 ret = send_payload(app->sock, &msg, sizeof(msg));
395 if (ret < 0) {
396 goto error_io;
397 }
398
399 ret = recv_reply(app->sock, &reply, sizeof(reply));
400 if (ret < 0) {
401 goto error_io;
402 }
403
404 reply_ret_code = be32toh(reply.ret_code);
405 log_reply_code(reply_ret_code);
406 switch (reply_ret_code) {
407 case AGENT_RET_CODE_SUCCESS:
408 break;
409 case AGENT_RET_CODE_UNKNOWN_NAME:
410 ret = LTTNG_ERR_UST_EVENT_NOT_FOUND;
411 goto error;
412 default:
413 ret = LTTNG_ERR_UNK;
414 goto error;
415 }
416
417 return LTTNG_OK;
418
419 error_io:
420 ret = LTTNG_ERR_UST_ENABLE_FAIL;
421 error:
422 return ret;
423 }
424
425 /*
426 * Internal disable agent event call on a agent application. This function
427 * communicates with the agent to disable a given event.
428 *
429 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
430 */
431 static int disable_event(struct agent_app *app, struct agent_event *event)
432 {
433 int ret;
434 uint64_t data_size;
435 uint32_t reply_ret_code;
436 struct lttcomm_agent_disable msg;
437 struct lttcomm_agent_generic_reply reply;
438
439 assert(app);
440 assert(app->sock);
441 assert(event);
442
443 DBG2("Agent disabling event %s for app pid: %d and socket %d", event->name,
444 app->pid, app->sock->fd);
445
446 data_size = sizeof(msg);
447
448 ret = send_header(app->sock, data_size, AGENT_CMD_DISABLE, 0);
449 if (ret < 0) {
450 goto error_io;
451 }
452
453 memset(&msg, 0, sizeof(msg));
454 strncpy(msg.name, event->name, sizeof(msg.name));
455 ret = send_payload(app->sock, &msg, sizeof(msg));
456 if (ret < 0) {
457 goto error_io;
458 }
459
460 ret = recv_reply(app->sock, &reply, sizeof(reply));
461 if (ret < 0) {
462 goto error_io;
463 }
464
465 reply_ret_code = be32toh(reply.ret_code);
466 log_reply_code(reply_ret_code);
467 switch (reply_ret_code) {
468 case AGENT_RET_CODE_SUCCESS:
469 break;
470 case AGENT_RET_CODE_UNKNOWN_NAME:
471 ret = LTTNG_ERR_UST_EVENT_NOT_FOUND;
472 goto error;
473 default:
474 ret = LTTNG_ERR_UNK;
475 goto error;
476 }
477
478 return LTTNG_OK;
479
480 error_io:
481 ret = LTTNG_ERR_UST_DISABLE_FAIL;
482 error:
483 return ret;
484 }
485
486 /*
487 * Send back the registration DONE command to a given agent application.
488 *
489 * Return 0 on success or else a negative value.
490 */
491 int agent_send_registration_done(struct agent_app *app)
492 {
493 assert(app);
494 assert(app->sock);
495
496 DBG("Agent sending registration done to app socket %d", app->sock->fd);
497
498 return send_header(app->sock, 0, AGENT_CMD_REG_DONE, 0);
499 }
500
501 /*
502 * Enable agent event on every agent applications registered with the session
503 * daemon.
504 *
505 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
506 */
507 int agent_enable_event(struct agent_event *event,
508 enum lttng_domain_type domain)
509 {
510 int ret;
511 struct agent_app *app;
512 struct lttng_ht_iter iter;
513
514 assert(event);
515
516 rcu_read_lock();
517
518 cds_lfht_for_each_entry(agent_apps_ht_by_sock->ht, &iter.iter, app,
519 node.node) {
520 if (app->domain != domain) {
521 continue;
522 }
523
524 /* Enable event on agent application through TCP socket. */
525 ret = enable_event(app, event);
526 if (ret != LTTNG_OK) {
527 goto error;
528 }
529 }
530
531 event->enabled = 1;
532 ret = LTTNG_OK;
533
534 error:
535 rcu_read_unlock();
536 return ret;
537 }
538
539 /*
540 * Disable agent event on every agent applications registered with the session
541 * daemon.
542 *
543 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
544 */
545 int agent_disable_event(struct agent_event *event,
546 enum lttng_domain_type domain)
547 {
548 int ret = LTTNG_OK;
549 struct agent_app *app;
550 struct lttng_ht_iter iter;
551
552 assert(event);
553 if (!event->enabled) {
554 goto end;
555 }
556
557 rcu_read_lock();
558
559 cds_lfht_for_each_entry(agent_apps_ht_by_sock->ht, &iter.iter, app,
560 node.node) {
561 if (app->domain != domain) {
562 continue;
563 }
564
565 /* Enable event on agent application through TCP socket. */
566 ret = disable_event(app, event);
567 if (ret != LTTNG_OK) {
568 goto error;
569 }
570 }
571
572 event->enabled = 0;
573
574 error:
575 rcu_read_unlock();
576 end:
577 return ret;
578 }
579
580 /*
581 * Ask every agent for the list of possible event. Events is allocated with the
582 * events of every agent application.
583 *
584 * Return the number of events or else a negative value.
585 */
586 int agent_list_events(struct lttng_event **events,
587 enum lttng_domain_type domain)
588 {
589 int ret;
590 size_t nbmem, count = 0;
591 struct agent_app *app;
592 struct lttng_event *tmp_events = NULL;
593 struct lttng_ht_iter iter;
594
595 assert(events);
596
597 DBG2("Agent listing events for domain %d", domain);
598
599 nbmem = UST_APP_EVENT_LIST_SIZE;
600 tmp_events = zmalloc(nbmem * sizeof(*tmp_events));
601 if (!tmp_events) {
602 PERROR("zmalloc agent list events");
603 ret = -ENOMEM;
604 goto error;
605 }
606
607 rcu_read_lock();
608 cds_lfht_for_each_entry(agent_apps_ht_by_sock->ht, &iter.iter, app,
609 node.node) {
610 ssize_t nb_ev;
611 struct lttng_event *agent_events;
612
613 /* Skip domain not asked by the list. */
614 if (app->domain != domain) {
615 continue;
616 }
617
618 nb_ev = list_events(app, &agent_events);
619 if (nb_ev < 0) {
620 ret = nb_ev;
621 goto error_unlock;
622 }
623
624 if (count + nb_ev > nbmem) {
625 /* In case the realloc fails, we free the memory */
626 struct lttng_event *new_tmp_events;
627 size_t new_nbmem;
628
629 new_nbmem = max_t(size_t, count + nb_ev, nbmem << 1);
630 DBG2("Reallocating agent event list from %zu to %zu entries",
631 nbmem, new_nbmem);
632 new_tmp_events = realloc(tmp_events,
633 new_nbmem * sizeof(*new_tmp_events));
634 if (!new_tmp_events) {
635 PERROR("realloc agent events");
636 ret = -ENOMEM;
637 free(agent_events);
638 goto error_unlock;
639 }
640 /* Zero the new memory */
641 memset(new_tmp_events + nbmem, 0,
642 (new_nbmem - nbmem) * sizeof(*new_tmp_events));
643 nbmem = new_nbmem;
644 tmp_events = new_tmp_events;
645 }
646 memcpy(tmp_events + count, agent_events,
647 nb_ev * sizeof(*tmp_events));
648 free(agent_events);
649 count += nb_ev;
650 }
651 rcu_read_unlock();
652
653 ret = count;
654 *events = tmp_events;
655 return ret;
656
657 error_unlock:
658 rcu_read_unlock();
659 error:
660 free(tmp_events);
661 return ret;
662 }
663
664 /*
665 * Create a agent app object using the given PID.
666 *
667 * Return newly allocated object or else NULL on error.
668 */
669 struct agent_app *agent_create_app(pid_t pid, enum lttng_domain_type domain,
670 struct lttcomm_sock *sock)
671 {
672 struct agent_app *app;
673
674 assert(sock);
675
676 app = zmalloc(sizeof(*app));
677 if (!app) {
678 PERROR("zmalloc agent create");
679 goto error;
680 }
681
682 app->pid = pid;
683 app->domain = domain;
684 app->sock = sock;
685 lttng_ht_node_init_ulong(&app->node, (unsigned long) app->sock->fd);
686
687 error:
688 return app;
689 }
690
691 /*
692 * Lookup agent app by socket in the global hash table.
693 *
694 * RCU read side lock MUST be acquired.
695 *
696 * Return object if found else NULL.
697 */
698 struct agent_app *agent_find_app_by_sock(int sock)
699 {
700 struct lttng_ht_node_ulong *node;
701 struct lttng_ht_iter iter;
702 struct agent_app *app;
703
704 assert(sock >= 0);
705
706 lttng_ht_lookup(agent_apps_ht_by_sock, (void *)((unsigned long) sock), &iter);
707 node = lttng_ht_iter_get_node_ulong(&iter);
708 if (node == NULL) {
709 goto error;
710 }
711 app = caa_container_of(node, struct agent_app, node);
712
713 DBG3("Agent app pid %d found by sock %d.", app->pid, sock);
714 return app;
715
716 error:
717 DBG3("Agent app NOT found by sock %d.", sock);
718 return NULL;
719 }
720
721 /*
722 * Add agent application object to the global hash table.
723 */
724 void agent_add_app(struct agent_app *app)
725 {
726 assert(app);
727
728 DBG3("Agent adding app sock: %d and pid: %d to ht", app->sock->fd, app->pid);
729 lttng_ht_add_unique_ulong(agent_apps_ht_by_sock, &app->node);
730 }
731
732 /*
733 * Delete agent application from the global hash table.
734 *
735 * rcu_read_lock() must be held by the caller.
736 */
737 void agent_delete_app(struct agent_app *app)
738 {
739 int ret;
740 struct lttng_ht_iter iter;
741
742 assert(app);
743
744 DBG3("Agent deleting app pid: %d and sock: %d", app->pid, app->sock->fd);
745
746 iter.iter.node = &app->node.node;
747 ret = lttng_ht_del(agent_apps_ht_by_sock, &iter);
748 assert(!ret);
749 }
750
751 /*
752 * Destroy an agent application object by detaching it from its corresponding
753 * UST app if one is connected by closing the socket. Finally, perform a
754 * delayed memory reclaim.
755 */
756 void agent_destroy_app(struct agent_app *app)
757 {
758 assert(app);
759
760 if (app->sock) {
761 app->sock->ops->close(app->sock);
762 lttcomm_destroy_sock(app->sock);
763 }
764
765 call_rcu(&app->node.head, destroy_app_agent_rcu);
766 }
767
768 /*
769 * Initialize an already allocated agent object.
770 *
771 * Return 0 on success or else a negative errno value.
772 */
773 int agent_init(struct agent *agt)
774 {
775 int ret;
776
777 assert(agt);
778
779 agt->events = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
780 if (!agt->events) {
781 ret = -ENOMEM;
782 goto error;
783 }
784 lttng_ht_node_init_u64(&agt->node, agt->domain);
785
786 return 0;
787
788 error:
789 return ret;
790 }
791
792 /*
793 * Add agent object to the given hash table.
794 */
795 void agent_add(struct agent *agt, struct lttng_ht *ht)
796 {
797 assert(agt);
798 assert(ht);
799
800 DBG3("Agent adding from domain %d", agt->domain);
801
802 lttng_ht_add_unique_u64(ht, &agt->node);
803 }
804
805 /*
806 * Create an agent object for the given domain.
807 *
808 * Return the allocated agent or NULL on error.
809 */
810 struct agent *agent_create(enum lttng_domain_type domain)
811 {
812 int ret;
813 struct agent *agt;
814
815 agt = zmalloc(sizeof(struct agent));
816 if (!agt) {
817 goto error;
818 }
819 agt->domain = domain;
820
821 ret = agent_init(agt);
822 if (ret < 0) {
823 free(agt);
824 agt = NULL;
825 goto error;
826 }
827
828 error:
829 return agt;
830 }
831
832 /*
833 * Create a newly allocated agent event data structure.
834 * Ownership of filter_expression is taken.
835 *
836 * Return a new object else NULL on error.
837 */
838 struct agent_event *agent_create_event(const char *name,
839 enum lttng_loglevel_type loglevel_type, int loglevel_value,
840 struct lttng_filter_bytecode *filter, char *filter_expression)
841 {
842 struct agent_event *event = NULL;
843
844 DBG3("Agent create new event with name %s, loglevel type %d and loglevel value %d",
845 name, loglevel_type, loglevel_value);
846
847 if (!name) {
848 ERR("Failed to create agent event; no name provided.");
849 goto error;
850 }
851
852 event = zmalloc(sizeof(*event));
853 if (!event) {
854 goto error;
855 }
856
857 strncpy(event->name, name, sizeof(event->name));
858 event->name[sizeof(event->name) - 1] = '\0';
859 lttng_ht_node_init_str(&event->node, event->name);
860
861 event->loglevel_value = loglevel_value;
862 event->loglevel_type = loglevel_type;
863 event->filter = filter;
864 event->filter_expression = filter_expression;
865 error:
866 return event;
867 }
868
869 /*
870 * Unique add of a agent event to an agent object.
871 */
872 void agent_add_event(struct agent_event *event, struct agent *agt)
873 {
874 assert(event);
875 assert(agt);
876 assert(agt->events);
877
878 DBG3("Agent adding event %s", event->name);
879 add_unique_agent_event(agt->events, event);
880 agt->being_used = 1;
881 }
882
883 /*
884 * Find a agent event in the given agent using name.
885 *
886 * RCU read side lock MUST be acquired.
887 *
888 * Return object if found else NULL.
889 */
890 struct agent_event *agent_find_event_by_name(const char *name,
891 struct agent *agt)
892 {
893 struct lttng_ht_node_str *node;
894 struct lttng_ht_iter iter;
895 struct lttng_ht *ht;
896 struct agent_ht_key key;
897
898 assert(name);
899 assert(agt);
900 assert(agt->events);
901
902 ht = agt->events;
903 key.name = name;
904
905 cds_lfht_lookup(ht->ht, ht->hash_fct((void *) name, lttng_ht_seed),
906 ht_match_event_by_name, &key, &iter.iter);
907 node = lttng_ht_iter_get_node_str(&iter);
908 if (node == NULL) {
909 goto error;
910 }
911
912 DBG3("Agent event found %s by name.", name);
913 return caa_container_of(node, struct agent_event, node);
914
915 error:
916 DBG3("Agent NOT found by name %s.", name);
917 return NULL;
918 }
919
920 /*
921 * Find a agent event in the given agent using name and loglevel.
922 *
923 * RCU read side lock MUST be acquired.
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.051943 seconds and 5 git commands to generate.