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