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