Teardown the notification thread after the sessiond clean-up
[lttng-tools.git] / src / bin / lttng-sessiond / agent.c
CommitLineData
0475c50c
DG
1/*
2 * Copyright (C) 2013 - David Goulet <dgoulet@efficios.com>
bdf64013 3 * Copyright (C) 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
0475c50c
DG
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License, version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 51
16 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
6c1c0768 19#define _LGPL_SOURCE
0475c50c 20#include <assert.h>
f20baf8e 21#include <urcu/uatomic.h>
bdf64013 22#include <urcu/rculist.h>
0475c50c
DG
23
24#include <common/common.h>
022d91ba 25#include <common/sessiond-comm/agent.h>
0475c50c 26
f263b7fd
JD
27#include <common/compat/endian.h>
28
022d91ba 29#include "agent.h"
f20baf8e 30#include "ust-app.h"
0475c50c 31#include "utils.h"
6712db61 32#include "common/error.h"
23c2bd47
JG
33
34#define AGENT_RET_CODE_INDEX(code) (code - AGENT_RET_CODE_SUCCESS)
35
bdf64013
JG
36/*
37 * Agent application context representation.
38 */
39struct agent_app_ctx {
40 char *provider_name;
41 char *ctx_name;
42
43 /* agent_app_ctx are part of the agent app_ctx_list. */
44 struct cds_list_head list_node;
45
46 /* For call_rcu teardown. */
47 struct rcu_head rcu_node;
48};
49
23c2bd47
JG
50/*
51 * Human readable agent return code.
52 */
53static const char *error_string_array[] = {
54 [ AGENT_RET_CODE_INDEX(AGENT_RET_CODE_SUCCESS) ] = "Success",
55 [ AGENT_RET_CODE_INDEX(AGENT_RET_CODE_INVALID) ] = "Invalid command",
56 [ AGENT_RET_CODE_INDEX(AGENT_RET_CODE_UNKNOWN_NAME) ] = "Unknown logger name",
57
58 /* Last element */
59 [ AGENT_RET_CODE_INDEX(AGENT_RET_CODE_NR) ] = "Unknown code",
60};
61
62static
63void log_reply_code(uint32_t in_reply_ret_code)
64{
65 int level = PRINT_DBG3;
66 /*
67 * reply_ret_code and in_reply_ret_code are kept separate to have a
68 * sanitized value (used to retrieve the human readable string) and the
69 * original value which is logged as-is.
70 */
71 uint32_t reply_ret_code = in_reply_ret_code;
72
73 if (reply_ret_code < AGENT_RET_CODE_SUCCESS ||
74 reply_ret_code >= AGENT_RET_CODE_NR) {
75 reply_ret_code = AGENT_RET_CODE_NR;
76 level = PRINT_ERR;
77 }
78
79 LOG(level, "Agent replied with retcode: %s (%"PRIu32")",
80 error_string_array[AGENT_RET_CODE_INDEX(
81 reply_ret_code)],
82 in_reply_ret_code);
83}
0475c50c 84
4a4ab2c3
DG
85/*
86 * Match function for the events hash table lookup by name.
87 */
88static int ht_match_event_by_name(struct cds_lfht_node *node,
89 const void *_key)
90{
022d91ba
DG
91 struct agent_event *event;
92 const struct agent_ht_key *key;
4a4ab2c3
DG
93
94 assert(node);
95 assert(_key);
96
022d91ba 97 event = caa_container_of(node, struct agent_event, node.node);
4a4ab2c3
DG
98 key = _key;
99
100 /* Match 1 elements of the key: name. */
101
102 /* Event name */
103 if (strncmp(event->name, key->name, sizeof(event->name)) != 0) {
104 goto no_match;
105 }
106 /* Match. */
107 return 1;
108
109no_match:
110 return 0;
111}
112
113/*
114 * Match function for the events hash table lookup by name and loglevel.
115 */
116static int ht_match_event(struct cds_lfht_node *node,
117 const void *_key)
118{
022d91ba
DG
119 struct agent_event *event;
120 const struct agent_ht_key *key;
19a97244 121 int ll_match;
4a4ab2c3
DG
122
123 assert(node);
124 assert(_key);
125
022d91ba 126 event = caa_container_of(node, struct agent_event, node.node);
4a4ab2c3
DG
127 key = _key;
128
129 /* Match 2 elements of the key: name and loglevel. */
130
131 /* Event name */
132 if (strncmp(event->name, key->name, sizeof(event->name)) != 0) {
133 goto no_match;
134 }
135
a9319624 136 /* Event loglevel value and type. */
19a97244
PP
137 ll_match = loglevels_match(event->loglevel_type,
138 event->loglevel_value, key->loglevel_type,
139 key->loglevel_value, LTTNG_EVENT_LOGLEVEL_ALL);
140
141 if (!ll_match) {
4a4ab2c3
DG
142 goto no_match;
143 }
a9319624 144
6b10b3b0 145 /* Filter expression */
d42bc3c8 146 if (!!event->filter_expression != !!key->filter_expression) {
71e147d0 147 /* One has a filter expression, the other does not */
6b10b3b0
AM
148 goto no_match;
149 }
150
71e147d0
PP
151 if (event->filter_expression) {
152 if (strncmp(event->filter_expression, key->filter_expression,
153 strlen(event->filter_expression)) != 0) {
154 goto no_match;
155 }
156 }
157
4a4ab2c3
DG
158 return 1;
159
160no_match:
161 return 0;
162}
163
164/*
022d91ba 165 * Add unique agent event based on the event name and loglevel.
4a4ab2c3 166 */
022d91ba
DG
167static void add_unique_agent_event(struct lttng_ht *ht,
168 struct agent_event *event)
4a4ab2c3
DG
169{
170 struct cds_lfht_node *node_ptr;
022d91ba 171 struct agent_ht_key key;
4a4ab2c3
DG
172
173 assert(ht);
174 assert(ht->ht);
175 assert(event);
176
177 key.name = event->name;
2106efa0 178 key.loglevel_value = event->loglevel_value;
a9319624 179 key.loglevel_type = event->loglevel_type;
6b10b3b0 180 key.filter_expression = event->filter_expression;
4a4ab2c3
DG
181
182 node_ptr = cds_lfht_add_unique(ht->ht,
183 ht->hash_fct(event->node.key, lttng_ht_seed),
184 ht_match_event, &key, &event->node.node);
185 assert(node_ptr == &event->node.node);
186}
187
0475c50c 188/*
022d91ba 189 * URCU delayed agent event reclaim.
0475c50c 190 */
022d91ba 191static void destroy_event_agent_rcu(struct rcu_head *head)
0475c50c
DG
192{
193 struct lttng_ht_node_str *node =
194 caa_container_of(head, struct lttng_ht_node_str, head);
022d91ba
DG
195 struct agent_event *event =
196 caa_container_of(node, struct agent_event, node);
0475c50c 197
992febc7 198 agent_destroy_event(event);
0475c50c
DG
199}
200
f20baf8e 201/*
022d91ba 202 * URCU delayed agent app reclaim.
f20baf8e 203 */
022d91ba 204static void destroy_app_agent_rcu(struct rcu_head *head)
f20baf8e
DG
205{
206 struct lttng_ht_node_ulong *node =
207 caa_container_of(head, struct lttng_ht_node_ulong, head);
022d91ba
DG
208 struct agent_app *app =
209 caa_container_of(node, struct agent_app, node);
f20baf8e
DG
210
211 free(app);
212}
213
214/*
022d91ba
DG
215 * Communication with the agent. Send the message header to the given socket in
216 * big endian.
f20baf8e
DG
217 *
218 * Return 0 on success or else a negative errno message of sendmsg() op.
219 */
220static int send_header(struct lttcomm_sock *sock, uint64_t data_size,
221 uint32_t cmd, uint32_t cmd_version)
222{
223 int ret;
224 ssize_t size;
022d91ba 225 struct lttcomm_agent_hdr msg;
f20baf8e
DG
226
227 assert(sock);
228
53efb85a 229 memset(&msg, 0, sizeof(msg));
f20baf8e
DG
230 msg.data_size = htobe64(data_size);
231 msg.cmd = htobe32(cmd);
232 msg.cmd_version = htobe32(cmd_version);
233
234 size = sock->ops->sendmsg(sock, &msg, sizeof(msg), 0);
235 if (size < sizeof(msg)) {
236 ret = -errno;
237 goto error;
238 }
239 ret = 0;
240
241error:
242 return ret;
243}
244
245/*
022d91ba
DG
246 * Communication call with the agent. Send the payload to the given socket. The
247 * header MUST be sent prior to this call.
f20baf8e
DG
248 *
249 * Return 0 on success or else a negative errno value of sendmsg() op.
250 */
bdf64013 251static int send_payload(struct lttcomm_sock *sock, const void *data,
f20baf8e
DG
252 size_t size)
253{
254 int ret;
255 ssize_t len;
256
257 assert(sock);
258 assert(data);
259
260 len = sock->ops->sendmsg(sock, data, size, 0);
261 if (len < size) {
262 ret = -errno;
263 goto error;
264 }
265 ret = 0;
266
267error:
268 return ret;
269}
270
271/*
022d91ba
DG
272 * Communication call with the agent. Receive reply from the agent using the
273 * given socket.
f20baf8e
DG
274 *
275 * Return 0 on success or else a negative errno value from recvmsg() op.
276 */
277static int recv_reply(struct lttcomm_sock *sock, void *buf, size_t size)
278{
279 int ret;
280 ssize_t len;
281
282 assert(sock);
283 assert(buf);
284
285 len = sock->ops->recvmsg(sock, buf, size, 0);
286 if (len < size) {
287 ret = -errno;
288 goto error;
289 }
290 ret = 0;
291
292error:
293 return ret;
294}
295
3c6a091f 296/*
428de77a 297 * Internal event listing for a given app. Populate events.
3c6a091f
DG
298 *
299 * Return number of element in the list or else a negative LTTNG_ERR* code.
428de77a
MD
300 * On success, the caller is responsible for freeing the memory
301 * allocated for "events".
3c6a091f 302 */
022d91ba 303static ssize_t list_events(struct agent_app *app, struct lttng_event **events)
3c6a091f
DG
304{
305 int ret, i, len = 0, offset = 0;
306 uint32_t nb_event;
307 size_t data_size;
d84af1a4 308 uint32_t reply_ret_code;
3c6a091f 309 struct lttng_event *tmp_events = NULL;
022d91ba
DG
310 struct lttcomm_agent_list_reply *reply = NULL;
311 struct lttcomm_agent_list_reply_hdr reply_hdr;
3c6a091f
DG
312
313 assert(app);
314 assert(app->sock);
315 assert(events);
316
022d91ba 317 DBG2("Agent listing events for app pid: %d and socket %d", app->pid,
3c6a091f
DG
318 app->sock->fd);
319
022d91ba 320 ret = send_header(app->sock, 0, AGENT_CMD_LIST, 0);
3c6a091f
DG
321 if (ret < 0) {
322 goto error_io;
323 }
324
325 /* Get list header so we know how much we'll receive. */
326 ret = recv_reply(app->sock, &reply_hdr, sizeof(reply_hdr));
327 if (ret < 0) {
328 goto error_io;
329 }
330
d84af1a4
JG
331 reply_ret_code = be32toh(reply_hdr.ret_code);
332 log_reply_code(reply_ret_code);
333 switch (reply_ret_code) {
022d91ba 334 case AGENT_RET_CODE_SUCCESS:
3c6a091f
DG
335 data_size = be32toh(reply_hdr.data_size) + sizeof(*reply);
336 break;
337 default:
3bd9aaeb 338 ret = LTTNG_ERR_UNK;
3c6a091f
DG
339 goto error;
340 }
341
342 reply = zmalloc(data_size);
343 if (!reply) {
344 ret = LTTNG_ERR_NOMEM;
345 goto error;
346 }
347
348 /* Get the list with the appropriate data size. */
349 ret = recv_reply(app->sock, reply, data_size);
350 if (ret < 0) {
351 goto error_io;
352 }
353
354 nb_event = be32toh(reply->nb_event);
355 tmp_events = zmalloc(sizeof(*tmp_events) * nb_event);
356 if (!tmp_events) {
357 ret = LTTNG_ERR_NOMEM;
358 goto error;
359 }
360
361 for (i = 0; i < nb_event; i++) {
362 offset += len;
0a85e7a3
MD
363 if (lttng_strncpy(tmp_events[i].name, reply->payload + offset,
364 sizeof(tmp_events[i].name))) {
365 ret = LTTNG_ERR_INVALID;
366 goto error;
367 }
3c6a091f
DG
368 tmp_events[i].pid = app->pid;
369 tmp_events[i].enabled = -1;
370 len = strlen(reply->payload + offset) + 1;
371 }
372
373 *events = tmp_events;
374
375 free(reply);
376 return nb_event;
377
378error_io:
379 ret = LTTNG_ERR_UST_LIST_FAIL;
380error:
381 free(reply);
382 free(tmp_events);
383 return -ret;
384
385}
386
f20baf8e 387/*
022d91ba
DG
388 * Internal enable agent event on a agent application. This function
389 * communicates with the agent to enable a given event.
f20baf8e
DG
390 *
391 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
392 */
022d91ba 393static int enable_event(struct agent_app *app, struct agent_event *event)
f20baf8e
DG
394{
395 int ret;
a0ba721c 396 char *bytes_to_send;
f20baf8e 397 uint64_t data_size;
a0ba721c 398 size_t filter_expression_length;
f4f9d4db 399 uint32_t reply_ret_code;
bdf64013 400 struct lttcomm_agent_enable_event msg;
022d91ba 401 struct lttcomm_agent_generic_reply reply;
f20baf8e
DG
402
403 assert(app);
404 assert(app->sock);
405 assert(event);
406
022d91ba 407 DBG2("Agent enabling event %s for app pid: %d and socket %d", event->name,
f20baf8e
DG
408 app->pid, app->sock->fd);
409
a0ba721c
AM
410 /*
411 * Calculate the payload's size, which is the fixed-size struct followed
412 * by the variable-length filter expression (+1 for the ending \0).
413 */
414 if (!event->filter_expression) {
415 filter_expression_length = 0;
416 } else {
417 filter_expression_length = strlen(event->filter_expression) + 1;
418 }
419 data_size = sizeof(msg) + filter_expression_length;
f20baf8e 420
53efb85a 421 memset(&msg, 0, sizeof(msg));
a9bfd666
JG
422 msg.loglevel_value = htobe32(event->loglevel_value);
423 msg.loglevel_type = htobe32(event->loglevel_type);
bb45c03e
MD
424 if (lttng_strncpy(msg.name, event->name, sizeof(msg.name))) {
425 ret = LTTNG_ERR_INVALID;
426 goto error;
427 }
a9bfd666 428 msg.filter_expression_length = htobe32(filter_expression_length);
a0ba721c 429
bb45c03e
MD
430 ret = send_header(app->sock, data_size, AGENT_CMD_ENABLE, 0);
431 if (ret < 0) {
432 goto error_io;
433 }
434
a0ba721c
AM
435 bytes_to_send = zmalloc(data_size);
436 if (!bytes_to_send) {
437 ret = LTTNG_ERR_NOMEM;
438 goto error;
439 }
440
441 memcpy(bytes_to_send, &msg, sizeof(msg));
442 if (filter_expression_length > 0) {
443 memcpy(bytes_to_send + sizeof(msg), event->filter_expression,
444 filter_expression_length);
445 }
446
447 ret = send_payload(app->sock, bytes_to_send, data_size);
448 free(bytes_to_send);
f20baf8e
DG
449 if (ret < 0) {
450 goto error_io;
451 }
452
453 ret = recv_reply(app->sock, &reply, sizeof(reply));
454 if (ret < 0) {
455 goto error_io;
456 }
457
f4f9d4db
JG
458 reply_ret_code = be32toh(reply.ret_code);
459 log_reply_code(reply_ret_code);
460 switch (reply_ret_code) {
022d91ba 461 case AGENT_RET_CODE_SUCCESS:
f20baf8e 462 break;
022d91ba 463 case AGENT_RET_CODE_UNKNOWN_NAME:
f20baf8e
DG
464 ret = LTTNG_ERR_UST_EVENT_NOT_FOUND;
465 goto error;
466 default:
3bd9aaeb 467 ret = LTTNG_ERR_UNK;
f20baf8e
DG
468 goto error;
469 }
470
471 return LTTNG_OK;
472
473error_io:
474 ret = LTTNG_ERR_UST_ENABLE_FAIL;
475error:
476 return ret;
477}
478
bdf64013
JG
479/*
480 * Send Pascal-style string. Size is sent as a 32-bit big endian integer.
481 */
482static
483int send_pstring(struct lttcomm_sock *sock, const char *str, uint32_t len)
484{
485 int ret;
486 uint32_t len_be;
487
488 len_be = htobe32(len);
489 ret = send_payload(sock, &len_be, sizeof(len_be));
490 if (ret) {
491 goto end;
492 }
493
494 ret = send_payload(sock, str, len);
495 if (ret) {
496 goto end;
497 }
498end:
499 return ret;
500}
501
502/*
503 * Internal enable application context on an agent application. This function
504 * communicates with the agent to enable a given application context.
505 *
506 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
507 */
508static int app_context_op(struct agent_app *app,
509 struct agent_app_ctx *ctx, enum lttcomm_agent_command cmd)
510{
511 int ret;
512 uint32_t reply_ret_code;
513 struct lttcomm_agent_generic_reply reply;
514 size_t app_ctx_provider_name_len, app_ctx_name_len, data_size;
515
516 assert(app);
517 assert(app->sock);
518 assert(ctx);
519 assert(cmd == AGENT_CMD_APP_CTX_ENABLE ||
520 cmd == AGENT_CMD_APP_CTX_DISABLE);
521
522 DBG2("Agent %s application %s:%s for app pid: %d and socket %d",
523 cmd == AGENT_CMD_APP_CTX_ENABLE ? "enabling" : "disabling",
524 ctx->provider_name, ctx->ctx_name,
525 app->pid, app->sock->fd);
526
527 /*
528 * Calculate the payload's size, which consists of the size (u32, BE)
529 * of the provider name, the NULL-terminated provider name string, the
530 * size (u32, BE) of the context name, followed by the NULL-terminated
531 * context name string.
532 */
533 app_ctx_provider_name_len = strlen(ctx->provider_name) + 1;
534 app_ctx_name_len = strlen(ctx->ctx_name) + 1;
535 data_size = sizeof(uint32_t) + app_ctx_provider_name_len +
536 sizeof(uint32_t) + app_ctx_name_len;
537
538 ret = send_header(app->sock, data_size, cmd, 0);
539 if (ret < 0) {
540 goto error_io;
541 }
542
543 if (app_ctx_provider_name_len > UINT32_MAX ||
544 app_ctx_name_len > UINT32_MAX) {
545 ERR("Application context name > MAX_UINT32");
546 ret = LTTNG_ERR_INVALID;
547 goto error;
548 }
549
550 ret = send_pstring(app->sock, ctx->provider_name,
551 (uint32_t) app_ctx_provider_name_len);
552 if (ret < 0) {
553 goto error_io;
554 }
555
556 ret = send_pstring(app->sock, ctx->ctx_name,
557 (uint32_t) app_ctx_name_len);
558 if (ret < 0) {
559 goto error_io;
560 }
561
562 ret = recv_reply(app->sock, &reply, sizeof(reply));
563 if (ret < 0) {
564 goto error_io;
565 }
566
567 reply_ret_code = be32toh(reply.ret_code);
568 log_reply_code(reply_ret_code);
569 switch (reply_ret_code) {
570 case AGENT_RET_CODE_SUCCESS:
571 break;
572 default:
573 ret = LTTNG_ERR_UNK;
574 goto error;
575 }
576
577 return LTTNG_OK;
578
579error_io:
580 ret = LTTNG_ERR_UST_ENABLE_FAIL;
581error:
582 return ret;
583}
584
f20baf8e 585/*
022d91ba
DG
586 * Internal disable agent event call on a agent application. This function
587 * communicates with the agent to disable a given event.
f20baf8e
DG
588 *
589 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
590 */
022d91ba 591static int disable_event(struct agent_app *app, struct agent_event *event)
f20baf8e
DG
592{
593 int ret;
594 uint64_t data_size;
517992f8 595 uint32_t reply_ret_code;
bdf64013 596 struct lttcomm_agent_disable_event msg;
022d91ba 597 struct lttcomm_agent_generic_reply reply;
f20baf8e
DG
598
599 assert(app);
600 assert(app->sock);
601 assert(event);
602
022d91ba 603 DBG2("Agent disabling event %s for app pid: %d and socket %d", event->name,
f20baf8e
DG
604 app->pid, app->sock->fd);
605
606 data_size = sizeof(msg);
63be730a
MD
607 memset(&msg, 0, sizeof(msg));
608 if (lttng_strncpy(msg.name, event->name, sizeof(msg.name))) {
609 ret = LTTNG_ERR_INVALID;
610 goto error;
611 }
f20baf8e 612
022d91ba 613 ret = send_header(app->sock, data_size, AGENT_CMD_DISABLE, 0);
f20baf8e
DG
614 if (ret < 0) {
615 goto error_io;
616 }
617
f20baf8e
DG
618 ret = send_payload(app->sock, &msg, sizeof(msg));
619 if (ret < 0) {
620 goto error_io;
621 }
622
623 ret = recv_reply(app->sock, &reply, sizeof(reply));
624 if (ret < 0) {
625 goto error_io;
626 }
627
517992f8
JG
628 reply_ret_code = be32toh(reply.ret_code);
629 log_reply_code(reply_ret_code);
630 switch (reply_ret_code) {
022d91ba
DG
631 case AGENT_RET_CODE_SUCCESS:
632 break;
633 case AGENT_RET_CODE_UNKNOWN_NAME:
634 ret = LTTNG_ERR_UST_EVENT_NOT_FOUND;
635 goto error;
636 default:
3bd9aaeb 637 ret = LTTNG_ERR_UNK;
022d91ba 638 goto error;
f20baf8e
DG
639 }
640
641 return LTTNG_OK;
642
643error_io:
644 ret = LTTNG_ERR_UST_DISABLE_FAIL;
645error:
646 return ret;
647}
648
1b500e7a 649/*
022d91ba 650 * Send back the registration DONE command to a given agent application.
1b500e7a
DG
651 *
652 * Return 0 on success or else a negative value.
653 */
022d91ba 654int agent_send_registration_done(struct agent_app *app)
1b500e7a
DG
655{
656 assert(app);
657 assert(app->sock);
658
022d91ba 659 DBG("Agent sending registration done to app socket %d", app->sock->fd);
1b500e7a 660
b26d1f5c 661 return send_header(app->sock, 0, AGENT_CMD_REG_DONE, 0);
1b500e7a
DG
662}
663
f20baf8e 664/*
022d91ba 665 * Enable agent event on every agent applications registered with the session
f20baf8e
DG
666 * daemon.
667 *
668 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
669 */
fefd409b
DG
670int agent_enable_event(struct agent_event *event,
671 enum lttng_domain_type domain)
f20baf8e
DG
672{
673 int ret;
022d91ba 674 struct agent_app *app;
f20baf8e
DG
675 struct lttng_ht_iter iter;
676
677 assert(event);
678
679 rcu_read_lock();
680
022d91ba 681 cds_lfht_for_each_entry(agent_apps_ht_by_sock->ht, &iter.iter, app,
f20baf8e 682 node.node) {
fefd409b
DG
683 if (app->domain != domain) {
684 continue;
685 }
686
022d91ba 687 /* Enable event on agent application through TCP socket. */
f20baf8e
DG
688 ret = enable_event(app, event);
689 if (ret != LTTNG_OK) {
690 goto error;
691 }
f20baf8e
DG
692 }
693
3c6a091f 694 event->enabled = 1;
f20baf8e
DG
695 ret = LTTNG_OK;
696
697error:
698 rcu_read_unlock();
699 return ret;
700}
701
bdf64013
JG
702static
703void destroy_app_ctx(struct agent_app_ctx *ctx)
704{
705 free(ctx->provider_name);
706 free(ctx->ctx_name);
707 free(ctx);
708}
709
710static
711struct agent_app_ctx *create_app_ctx(struct lttng_event_context *ctx)
712{
713 struct agent_app_ctx *agent_ctx = NULL;
714
715 if (!ctx) {
716 goto end;
717 }
718
719 assert(ctx->ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT);
720 agent_ctx = zmalloc(sizeof(*ctx));
721 if (!agent_ctx) {
722 goto end;
723 }
724
725 agent_ctx->provider_name = strdup(ctx->u.app_ctx.provider_name);
726 agent_ctx->ctx_name = strdup(ctx->u.app_ctx.ctx_name);
727 if (!agent_ctx->provider_name || !agent_ctx->ctx_name) {
728 destroy_app_ctx(agent_ctx);
729 agent_ctx = NULL;
730 }
731end:
732 return agent_ctx;
733}
734
735/*
736 * Enable agent context on every agent applications registered with the session
737 * daemon.
738 *
739 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
740 */
741int agent_enable_context(struct lttng_event_context *ctx,
742 enum lttng_domain_type domain)
743{
744 int ret;
745 struct agent_app *app;
746 struct lttng_ht_iter iter;
747
748 assert(ctx);
749 if (ctx->ctx != LTTNG_EVENT_CONTEXT_APP_CONTEXT) {
750 ret = LTTNG_ERR_INVALID;
751 goto error;
752 }
753
754 rcu_read_lock();
755
756 cds_lfht_for_each_entry(agent_apps_ht_by_sock->ht, &iter.iter, app,
757 node.node) {
758 struct agent_app_ctx *agent_ctx;
759
760 if (app->domain != domain) {
761 continue;
762 }
763
764 agent_ctx = create_app_ctx(ctx);
765 if (!agent_ctx) {
a33c2651 766 ret = LTTNG_ERR_NOMEM;
bdf64013
JG
767 goto error_unlock;
768 }
769
770 /* Enable event on agent application through TCP socket. */
771 ret = app_context_op(app, agent_ctx, AGENT_CMD_APP_CTX_ENABLE);
c12618b5 772 destroy_app_ctx(agent_ctx);
bdf64013 773 if (ret != LTTNG_OK) {
bdf64013
JG
774 goto error_unlock;
775 }
776 }
777
778 ret = LTTNG_OK;
779
780error_unlock:
781 rcu_read_unlock();
782error:
783 return ret;
784}
785
f20baf8e 786/*
bdf64013 787 * Disable agent event on every agent application registered with the session
f20baf8e
DG
788 * daemon.
789 *
790 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
791 */
fefd409b
DG
792int agent_disable_event(struct agent_event *event,
793 enum lttng_domain_type domain)
f20baf8e 794{
f1bc0129 795 int ret = LTTNG_OK;
022d91ba 796 struct agent_app *app;
f20baf8e
DG
797 struct lttng_ht_iter iter;
798
799 assert(event);
f1bc0129
JG
800 if (!event->enabled) {
801 goto end;
802 }
f20baf8e
DG
803
804 rcu_read_lock();
805
022d91ba 806 cds_lfht_for_each_entry(agent_apps_ht_by_sock->ht, &iter.iter, app,
f20baf8e 807 node.node) {
fefd409b
DG
808 if (app->domain != domain) {
809 continue;
810 }
811
022d91ba 812 /* Enable event on agent application through TCP socket. */
f20baf8e
DG
813 ret = disable_event(app, event);
814 if (ret != LTTNG_OK) {
815 goto error;
816 }
f20baf8e
DG
817 }
818
3c6a091f 819 event->enabled = 0;
f20baf8e
DG
820
821error:
822 rcu_read_unlock();
f1bc0129 823end:
f20baf8e
DG
824 return ret;
825}
826
bdf64013
JG
827/*
828 * Disable agent context on every agent application registered with the session
829 * daemon.
830 *
831 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
832 */
833int disable_context(struct agent_app_ctx *ctx, enum lttng_domain_type domain)
834{
835 int ret = LTTNG_OK;
836 struct agent_app *app;
837 struct lttng_ht_iter iter;
838
839 assert(ctx);
840
841 rcu_read_lock();
842 DBG2("Disabling agent application context %s:%s",
843 ctx->provider_name, ctx->ctx_name);
844 cds_lfht_for_each_entry(agent_apps_ht_by_sock->ht, &iter.iter, app,
845 node.node) {
846 if (app->domain != domain) {
847 continue;
848 }
849
850 ret = app_context_op(app, ctx, AGENT_CMD_APP_CTX_DISABLE);
851 if (ret != LTTNG_OK) {
852 goto end;
853 }
854 }
855end:
856 rcu_read_unlock();
857 return ret;
858}
859
f20baf8e 860/*
022d91ba
DG
861 * Ask every agent for the list of possible event. Events is allocated with the
862 * events of every agent application.
f20baf8e
DG
863 *
864 * Return the number of events or else a negative value.
865 */
f60140a1
DG
866int agent_list_events(struct lttng_event **events,
867 enum lttng_domain_type domain)
f20baf8e
DG
868{
869 int ret;
870 size_t nbmem, count = 0;
022d91ba 871 struct agent_app *app;
aae6255e 872 struct lttng_event *tmp_events = NULL;
f20baf8e
DG
873 struct lttng_ht_iter iter;
874
875 assert(events);
876
0e115563
DG
877 DBG2("Agent listing events for domain %d", domain);
878
f20baf8e
DG
879 nbmem = UST_APP_EVENT_LIST_SIZE;
880 tmp_events = zmalloc(nbmem * sizeof(*tmp_events));
881 if (!tmp_events) {
022d91ba 882 PERROR("zmalloc agent list events");
f20baf8e
DG
883 ret = -ENOMEM;
884 goto error;
885 }
886
887 rcu_read_lock();
022d91ba 888 cds_lfht_for_each_entry(agent_apps_ht_by_sock->ht, &iter.iter, app,
f20baf8e
DG
889 node.node) {
890 ssize_t nb_ev;
022d91ba 891 struct lttng_event *agent_events;
f20baf8e 892
f60140a1
DG
893 /* Skip domain not asked by the list. */
894 if (app->domain != domain) {
895 continue;
896 }
897
022d91ba 898 nb_ev = list_events(app, &agent_events);
f20baf8e
DG
899 if (nb_ev < 0) {
900 ret = nb_ev;
428de77a 901 goto error_unlock;
f20baf8e
DG
902 }
903
53efb85a 904 if (count + nb_ev > nbmem) {
f20baf8e 905 /* In case the realloc fails, we free the memory */
53efb85a
MD
906 struct lttng_event *new_tmp_events;
907 size_t new_nbmem;
908
909 new_nbmem = max_t(size_t, count + nb_ev, nbmem << 1);
022d91ba 910 DBG2("Reallocating agent event list from %zu to %zu entries",
53efb85a
MD
911 nbmem, new_nbmem);
912 new_tmp_events = realloc(tmp_events,
913 new_nbmem * sizeof(*new_tmp_events));
914 if (!new_tmp_events) {
022d91ba 915 PERROR("realloc agent events");
f20baf8e 916 ret = -ENOMEM;
022d91ba 917 free(agent_events);
428de77a 918 goto error_unlock;
f20baf8e 919 }
53efb85a
MD
920 /* Zero the new memory */
921 memset(new_tmp_events + nbmem, 0,
922 (new_nbmem - nbmem) * sizeof(*new_tmp_events));
923 nbmem = new_nbmem;
924 tmp_events = new_tmp_events;
f20baf8e 925 }
022d91ba 926 memcpy(tmp_events + count, agent_events,
53efb85a 927 nb_ev * sizeof(*tmp_events));
022d91ba 928 free(agent_events);
f20baf8e
DG
929 count += nb_ev;
930 }
931 rcu_read_unlock();
932
933 ret = count;
934 *events = tmp_events;
aae6255e 935 return ret;
f20baf8e 936
428de77a
MD
937error_unlock:
938 rcu_read_unlock();
f20baf8e 939error:
aae6255e 940 free(tmp_events);
f20baf8e
DG
941 return ret;
942}
943
944/*
022d91ba 945 * Create a agent app object using the given PID.
f20baf8e
DG
946 *
947 * Return newly allocated object or else NULL on error.
948 */
fefd409b
DG
949struct agent_app *agent_create_app(pid_t pid, enum lttng_domain_type domain,
950 struct lttcomm_sock *sock)
f20baf8e 951{
022d91ba 952 struct agent_app *app;
f20baf8e
DG
953
954 assert(sock);
955
956 app = zmalloc(sizeof(*app));
957 if (!app) {
022d91ba 958 PERROR("zmalloc agent create");
f20baf8e
DG
959 goto error;
960 }
961
962 app->pid = pid;
fefd409b 963 app->domain = domain;
f20baf8e 964 app->sock = sock;
f20baf8e
DG
965 lttng_ht_node_init_ulong(&app->node, (unsigned long) app->sock->fd);
966
967error:
968 return app;
969}
970
971/*
022d91ba 972 * Lookup agent app by socket in the global hash table.
f20baf8e
DG
973 *
974 * RCU read side lock MUST be acquired.
975 *
976 * Return object if found else NULL.
977 */
022d91ba 978struct agent_app *agent_find_app_by_sock(int sock)
f20baf8e
DG
979{
980 struct lttng_ht_node_ulong *node;
981 struct lttng_ht_iter iter;
022d91ba 982 struct agent_app *app;
f20baf8e
DG
983
984 assert(sock >= 0);
985
022d91ba 986 lttng_ht_lookup(agent_apps_ht_by_sock, (void *)((unsigned long) sock), &iter);
f20baf8e
DG
987 node = lttng_ht_iter_get_node_ulong(&iter);
988 if (node == NULL) {
989 goto error;
990 }
022d91ba 991 app = caa_container_of(node, struct agent_app, node);
f20baf8e 992
022d91ba 993 DBG3("Agent app pid %d found by sock %d.", app->pid, sock);
f20baf8e
DG
994 return app;
995
996error:
022d91ba 997 DBG3("Agent app NOT found by sock %d.", sock);
f20baf8e
DG
998 return NULL;
999}
1000
1001/*
022d91ba 1002 * Add agent application object to the global hash table.
f20baf8e 1003 */
022d91ba 1004void agent_add_app(struct agent_app *app)
f20baf8e
DG
1005{
1006 assert(app);
1007
022d91ba 1008 DBG3("Agent adding app sock: %d and pid: %d to ht", app->sock->fd, app->pid);
022d91ba 1009 lttng_ht_add_unique_ulong(agent_apps_ht_by_sock, &app->node);
f20baf8e
DG
1010}
1011
f20baf8e 1012/*
022d91ba 1013 * Delete agent application from the global hash table.
d558f236
JG
1014 *
1015 * rcu_read_lock() must be held by the caller.
f20baf8e 1016 */
022d91ba 1017void agent_delete_app(struct agent_app *app)
f20baf8e
DG
1018{
1019 int ret;
1020 struct lttng_ht_iter iter;
1021
1022 assert(app);
1023
022d91ba 1024 DBG3("Agent deleting app pid: %d and sock: %d", app->pid, app->sock->fd);
f20baf8e
DG
1025
1026 iter.iter.node = &app->node.node;
022d91ba 1027 ret = lttng_ht_del(agent_apps_ht_by_sock, &iter);
f20baf8e
DG
1028 assert(!ret);
1029}
1030
1031/*
e785906c 1032 * Destroy an agent application object by detaching it from its corresponding
022d91ba 1033 * UST app if one is connected by closing the socket. Finally, perform a
428de77a 1034 * delayed memory reclaim.
f20baf8e 1035 */
022d91ba 1036void agent_destroy_app(struct agent_app *app)
f20baf8e
DG
1037{
1038 assert(app);
1039
1040 if (app->sock) {
1041 app->sock->ops->close(app->sock);
1042 lttcomm_destroy_sock(app->sock);
1043 }
1044
022d91ba 1045 call_rcu(&app->node.head, destroy_app_agent_rcu);
f20baf8e
DG
1046}
1047
0475c50c 1048/*
022d91ba 1049 * Initialize an already allocated agent object.
0475c50c
DG
1050 *
1051 * Return 0 on success or else a negative errno value.
1052 */
022d91ba 1053int agent_init(struct agent *agt)
0475c50c
DG
1054{
1055 int ret;
1056
022d91ba 1057 assert(agt);
0475c50c 1058
022d91ba
DG
1059 agt->events = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
1060 if (!agt->events) {
0475c50c
DG
1061 ret = -ENOMEM;
1062 goto error;
1063 }
fefd409b 1064 lttng_ht_node_init_u64(&agt->node, agt->domain);
0475c50c 1065
bdf64013 1066 CDS_INIT_LIST_HEAD(&agt->app_ctx_list);
0475c50c
DG
1067 return 0;
1068
1069error:
1070 return ret;
1071}
1072
fefd409b
DG
1073/*
1074 * Add agent object to the given hash table.
1075 */
1076void agent_add(struct agent *agt, struct lttng_ht *ht)
1077{
1078 assert(agt);
1079 assert(ht);
1080
1081 DBG3("Agent adding from domain %d", agt->domain);
1082
fefd409b 1083 lttng_ht_add_unique_u64(ht, &agt->node);
fefd409b
DG
1084}
1085
1086/*
1087 * Create an agent object for the given domain.
1088 *
1089 * Return the allocated agent or NULL on error.
1090 */
1091struct agent *agent_create(enum lttng_domain_type domain)
1092{
1093 int ret;
1094 struct agent *agt;
1095
3b5f70d4 1096 agt = zmalloc(sizeof(struct agent));
fefd409b
DG
1097 if (!agt) {
1098 goto error;
1099 }
1100 agt->domain = domain;
1101
1102 ret = agent_init(agt);
1103 if (ret < 0) {
1104 free(agt);
988ae332 1105 agt = NULL;
fefd409b
DG
1106 goto error;
1107 }
1108
1109error:
1110 return agt;
1111}
1112
0475c50c 1113/*
51755dc8
JG
1114 * Create a newly allocated agent event data structure.
1115 * Ownership of filter_expression is taken.
0475c50c
DG
1116 *
1117 * Return a new object else NULL on error.
1118 */
022d91ba 1119struct agent_event *agent_create_event(const char *name,
a9319624 1120 enum lttng_loglevel_type loglevel_type, int loglevel_value,
51755dc8 1121 struct lttng_filter_bytecode *filter, char *filter_expression)
0475c50c 1122{
51755dc8 1123 struct agent_event *event = NULL;
0475c50c 1124
6b10b3b0
AM
1125 DBG3("Agent create new event with name %s, loglevel type %d, \
1126 loglevel value %d and filter %s",
40111ba1
JG
1127 name, loglevel_type, loglevel_value,
1128 filter_expression ? filter_expression : "NULL");
0475c50c 1129
51755dc8
JG
1130 if (!name) {
1131 ERR("Failed to create agent event; no name provided.");
0475c50c
DG
1132 goto error;
1133 }
1134
51755dc8
JG
1135 event = zmalloc(sizeof(*event));
1136 if (!event) {
1137 goto error;
0475c50c
DG
1138 }
1139
51755dc8
JG
1140 strncpy(event->name, name, sizeof(event->name));
1141 event->name[sizeof(event->name) - 1] = '\0';
1142 lttng_ht_node_init_str(&event->node, event->name);
be6a6276 1143
2106efa0 1144 event->loglevel_value = loglevel_value;
51755dc8
JG
1145 event->loglevel_type = loglevel_type;
1146 event->filter = filter;
1147 event->filter_expression = filter_expression;
0475c50c
DG
1148error:
1149 return event;
1150}
1151
1152/*
022d91ba 1153 * Unique add of a agent event to an agent object.
0475c50c 1154 */
022d91ba 1155void agent_add_event(struct agent_event *event, struct agent *agt)
0475c50c
DG
1156{
1157 assert(event);
022d91ba
DG
1158 assert(agt);
1159 assert(agt->events);
0475c50c 1160
022d91ba 1161 DBG3("Agent adding event %s", event->name);
022d91ba 1162 add_unique_agent_event(agt->events, event);
022d91ba 1163 agt->being_used = 1;
0475c50c
DG
1164}
1165
bdf64013
JG
1166/*
1167 * Unique add of a agent context to an agent object.
1168 */
1169int agent_add_context(struct lttng_event_context *ctx, struct agent *agt)
1170{
1171 int ret = LTTNG_OK;
1172 struct agent_app_ctx *agent_ctx = NULL;
1173
1174 assert(ctx);
1175 assert(agt);
1176 assert(agt->events);
1177 assert(ctx->ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT);
1178
1179 agent_ctx = create_app_ctx(ctx);
1180 if (!agent_ctx) {
1181 ret = LTTNG_ERR_NOMEM;
1182 goto end;
1183 }
1184
1185 DBG3("Agent adding context %s:%s", ctx->u.app_ctx.provider_name,
1186 ctx->u.app_ctx.ctx_name);
1187 cds_list_add_tail_rcu(&agent_ctx->list_node, &agt->app_ctx_list);
1188end:
1189 return ret;
1190}
1191
0475c50c 1192/*
e261a6cc 1193 * Find multiple agent events sharing the given name.
4a4ab2c3 1194 *
e261a6cc
PP
1195 * RCU read side lock MUST be acquired. It must be held for the
1196 * duration of the iteration.
4a4ab2c3 1197 *
e261a6cc 1198 * Sets the given iterator.
4a4ab2c3 1199 */
e261a6cc
PP
1200void agent_find_events_by_name(const char *name, struct agent *agt,
1201 struct lttng_ht_iter* iter)
4a4ab2c3 1202{
4a4ab2c3 1203 struct lttng_ht *ht;
022d91ba 1204 struct agent_ht_key key;
4a4ab2c3
DG
1205
1206 assert(name);
022d91ba
DG
1207 assert(agt);
1208 assert(agt->events);
e261a6cc 1209 assert(iter);
4a4ab2c3 1210
022d91ba 1211 ht = agt->events;
4a4ab2c3
DG
1212 key.name = name;
1213
1214 cds_lfht_lookup(ht->ht, ht->hash_fct((void *) name, lttng_ht_seed),
e261a6cc
PP
1215 ht_match_event_by_name, &key, &iter->iter);
1216}
4a4ab2c3 1217
e261a6cc
PP
1218/*
1219 * Get the next agent event duplicate by name. This should be called
1220 * after a call to agent_find_events_by_name() to iterate on events.
1221 *
1222 * The RCU read lock must be held during the iteration and for as long
1223 * as the object the iterator points to remains in use.
1224 */
1225void agent_event_next_duplicate(const char *name,
1226 struct agent *agt, struct lttng_ht_iter* iter)
1227{
1228 struct agent_ht_key key;
4a4ab2c3 1229
e261a6cc
PP
1230 key.name = name;
1231
1232 cds_lfht_next_duplicate(agt->events->ht, ht_match_event_by_name,
1233 &key, &iter->iter);
4a4ab2c3
DG
1234}
1235
1236/*
6b10b3b0 1237 * Find a agent event in the given agent using name, loglevel and filter.
0475c50c 1238 *
1e17eae2
JG
1239 * RCU read side lock MUST be acquired. It must be kept for as long as
1240 * the returned agent_event is used.
0475c50c
DG
1241 *
1242 * Return object if found else NULL.
1243 */
a9319624
PP
1244struct agent_event *agent_find_event(const char *name,
1245 enum lttng_loglevel_type loglevel_type, int loglevel_value,
6b10b3b0 1246 char *filter_expression, struct agent *agt)
0475c50c
DG
1247{
1248 struct lttng_ht_node_str *node;
1249 struct lttng_ht_iter iter;
4a4ab2c3 1250 struct lttng_ht *ht;
022d91ba 1251 struct agent_ht_key key;
0475c50c
DG
1252
1253 assert(name);
022d91ba
DG
1254 assert(agt);
1255 assert(agt->events);
0475c50c 1256
022d91ba 1257 ht = agt->events;
4a4ab2c3 1258 key.name = name;
2106efa0 1259 key.loglevel_value = loglevel_value;
a9319624 1260 key.loglevel_type = loglevel_type;
6b10b3b0 1261 key.filter_expression = filter_expression;
4a4ab2c3
DG
1262
1263 cds_lfht_lookup(ht->ht, ht->hash_fct((void *) name, lttng_ht_seed),
1264 ht_match_event, &key, &iter.iter);
0475c50c
DG
1265 node = lttng_ht_iter_get_node_str(&iter);
1266 if (node == NULL) {
1267 goto error;
1268 }
1269
022d91ba
DG
1270 DBG3("Agent event found %s.", name);
1271 return caa_container_of(node, struct agent_event, node);
0475c50c
DG
1272
1273error:
a51e817b 1274 DBG3("Agent event NOT found %s.", name);
0475c50c
DG
1275 return NULL;
1276}
1277
0475c50c 1278/*
022d91ba
DG
1279 * Free given agent event. This event must not be globally visible at this
1280 * point (only expected to be used on failure just after event creation). After
1281 * this call, the pointer is not usable anymore.
0475c50c 1282 */
022d91ba 1283void agent_destroy_event(struct agent_event *event)
0475c50c
DG
1284{
1285 assert(event);
1286
971da06a 1287 free(event->filter);
8404118c 1288 free(event->filter_expression);
51755dc8 1289 free(event->exclusion);
0475c50c
DG
1290 free(event);
1291}
1292
bdf64013
JG
1293static
1294void destroy_app_ctx_rcu(struct rcu_head *head)
1295{
1296 struct agent_app_ctx *ctx =
1297 caa_container_of(head, struct agent_app_ctx, rcu_node);
1298
1299 destroy_app_ctx(ctx);
1300}
1301
0475c50c 1302/*
35ed21a5 1303 * Destroy an agent completely.
0475c50c 1304 */
022d91ba 1305void agent_destroy(struct agent *agt)
0475c50c
DG
1306{
1307 struct lttng_ht_node_str *node;
1308 struct lttng_ht_iter iter;
bdf64013 1309 struct agent_app_ctx *ctx;
0475c50c 1310
022d91ba 1311 assert(agt);
0475c50c 1312
022d91ba 1313 DBG3("Agent destroy");
0475c50c 1314
0475c50c 1315 rcu_read_lock();
022d91ba 1316 cds_lfht_for_each_entry(agt->events->ht, &iter.iter, node, node) {
0475c50c 1317 int ret;
022d91ba 1318 struct agent_event *event;
29c0fd4d
DG
1319
1320 /*
bdf64013
JG
1321 * When destroying an event, we have to try to disable it on the
1322 * agent side so the event stops generating data. The return
1323 * value is not important since we have to continue anyway
1324 * destroying the object.
29c0fd4d 1325 */
022d91ba
DG
1326 event = caa_container_of(node, struct agent_event, node);
1327 (void) agent_disable_event(event, agt->domain);
0475c50c 1328
022d91ba 1329 ret = lttng_ht_del(agt->events, &iter);
0475c50c 1330 assert(!ret);
022d91ba 1331 call_rcu(&node->head, destroy_event_agent_rcu);
0475c50c 1332 }
0475c50c 1333
bdf64013
JG
1334 cds_list_for_each_entry_rcu(ctx, &agt->app_ctx_list, list_node) {
1335 (void) disable_context(ctx, agt->domain);
1336 cds_list_del(&ctx->list_node);
1337 call_rcu(&ctx->rcu_node, destroy_app_ctx_rcu);
1338 }
1339 rcu_read_unlock();
f8be902b 1340 ht_cleanup_push(agt->events);
35ed21a5 1341 free(agt);
f20baf8e
DG
1342}
1343
1344/*
6a4e4039 1345 * Allocate agent_apps_ht_by_sock.
f20baf8e 1346 */
6a4e4039 1347int agent_app_ht_alloc(void)
f20baf8e 1348{
6a4e4039
JG
1349 int ret = 0;
1350
022d91ba
DG
1351 agent_apps_ht_by_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
1352 if (!agent_apps_ht_by_sock) {
6a4e4039 1353 ret = -1;
f20baf8e
DG
1354 }
1355
6a4e4039
JG
1356 return ret;
1357}
1358
1359/*
1360 * Destroy a agent application by socket.
1361 */
1362void agent_destroy_app_by_sock(int sock)
1363{
1364 struct agent_app *app;
1365
1366 assert(sock >= 0);
1367
1368 /*
1369 * Not finding an application is a very important error that should NEVER
1370 * happen. The hash table deletion is ONLY done through this call when the
1371 * main sessiond thread is torn down.
1372 */
1373 rcu_read_lock();
1374 app = agent_find_app_by_sock(sock);
1375 assert(app);
1376
1377 /* RCU read side lock is assumed to be held by this function. */
1378 agent_delete_app(app);
1379
1380 /* The application is freed in a RCU call but the socket is closed here. */
1381 agent_destroy_app(app);
1382 rcu_read_unlock();
1383}
1384
1385/*
1386 * Clean-up the agent app hash table and destroy it.
1387 */
1388void agent_app_ht_clean(void)
1389{
1390 struct lttng_ht_node_ulong *node;
1391 struct lttng_ht_iter iter;
1392
a433283e
JG
1393 if (!agent_apps_ht_by_sock) {
1394 return;
1395 }
6a4e4039
JG
1396 rcu_read_lock();
1397 cds_lfht_for_each_entry(agent_apps_ht_by_sock->ht, &iter.iter, node, node) {
1398 struct agent_app *app;
1399
1400 app = caa_container_of(node, struct agent_app, node);
1401 agent_destroy_app_by_sock(app->sock->fd);
1402 }
1403 rcu_read_unlock();
1404
1405 lttng_ht_destroy(agent_apps_ht_by_sock);
f20baf8e
DG
1406}
1407
1408/*
022d91ba 1409 * Update a agent application (given socket) using the given agent.
f20baf8e
DG
1410 *
1411 * Note that this function is most likely to be used with a tracing session
1412 * thus the caller should make sure to hold the appropriate lock(s).
1413 */
022d91ba 1414void agent_update(struct agent *agt, int sock)
f20baf8e
DG
1415{
1416 int ret;
022d91ba
DG
1417 struct agent_app *app;
1418 struct agent_event *event;
f20baf8e 1419 struct lttng_ht_iter iter;
bdf64013 1420 struct agent_app_ctx *ctx;
f20baf8e 1421
022d91ba 1422 assert(agt);
f20baf8e
DG
1423 assert(sock >= 0);
1424
022d91ba 1425 DBG("Agent updating app socket %d", sock);
f20baf8e
DG
1426
1427 rcu_read_lock();
bdf64013
JG
1428 app = agent_find_app_by_sock(sock);
1429 /*
1430 * We are in the registration path thus if the application is gone,
1431 * there is a serious code flow error.
1432 */
1433 assert(app);
022d91ba 1434 cds_lfht_for_each_entry(agt->events->ht, &iter.iter, event, node.node) {
f20baf8e
DG
1435 /* Skip event if disabled. */
1436 if (!event->enabled) {
1437 continue;
1438 }
1439
f20baf8e
DG
1440 ret = enable_event(app, event);
1441 if (ret != LTTNG_OK) {
022d91ba 1442 DBG2("Agent update unable to enable event %s on app pid: %d sock %d",
f20baf8e
DG
1443 event->name, app->pid, app->sock->fd);
1444 /* Let's try the others here and don't assume the app is dead. */
1445 continue;
1446 }
1447 }
bdf64013
JG
1448
1449 cds_list_for_each_entry_rcu(ctx, &agt->app_ctx_list, list_node) {
1450 ret = app_context_op(app, ctx, AGENT_CMD_APP_CTX_ENABLE);
1451 if (ret != LTTNG_OK) {
1452 DBG2("Agent update unable to add application context %s:%s on app pid: %d sock %d",
1453 ctx->provider_name, ctx->ctx_name,
1454 app->pid, app->sock->fd);
1455 continue;
1456 }
1457 }
1458
f20baf8e 1459 rcu_read_unlock();
0475c50c 1460}
This page took 0.120535 seconds and 4 git commands to generate.