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