sessiond: add loglevels_match()
[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;
19a97244 106 int ll_match;
4a4ab2c3
DG
107
108 assert(node);
109 assert(_key);
110
022d91ba 111 event = caa_container_of(node, struct agent_event, node.node);
4a4ab2c3
DG
112 key = _key;
113
114 /* Match 2 elements of the key: name and loglevel. */
115
116 /* Event name */
117 if (strncmp(event->name, key->name, sizeof(event->name)) != 0) {
118 goto no_match;
119 }
120
a9319624 121 /* Event loglevel value and type. */
19a97244
PP
122 ll_match = loglevels_match(event->loglevel_type,
123 event->loglevel_value, key->loglevel_type,
124 key->loglevel_value, LTTNG_EVENT_LOGLEVEL_ALL);
125
126 if (!ll_match) {
4a4ab2c3
DG
127 goto no_match;
128 }
a9319624 129
4a4ab2c3
DG
130 return 1;
131
132no_match:
133 return 0;
134}
135
136/*
022d91ba 137 * Add unique agent event based on the event name and loglevel.
4a4ab2c3 138 */
022d91ba
DG
139static void add_unique_agent_event(struct lttng_ht *ht,
140 struct agent_event *event)
4a4ab2c3
DG
141{
142 struct cds_lfht_node *node_ptr;
022d91ba 143 struct agent_ht_key key;
4a4ab2c3
DG
144
145 assert(ht);
146 assert(ht->ht);
147 assert(event);
148
149 key.name = event->name;
2106efa0 150 key.loglevel_value = event->loglevel_value;
a9319624 151 key.loglevel_type = event->loglevel_type;
4a4ab2c3
DG
152
153 node_ptr = cds_lfht_add_unique(ht->ht,
154 ht->hash_fct(event->node.key, lttng_ht_seed),
155 ht_match_event, &key, &event->node.node);
156 assert(node_ptr == &event->node.node);
157}
158
0475c50c 159/*
022d91ba 160 * URCU delayed agent event reclaim.
0475c50c 161 */
022d91ba 162static void destroy_event_agent_rcu(struct rcu_head *head)
0475c50c
DG
163{
164 struct lttng_ht_node_str *node =
165 caa_container_of(head, struct lttng_ht_node_str, head);
022d91ba
DG
166 struct agent_event *event =
167 caa_container_of(node, struct agent_event, node);
0475c50c 168
992febc7 169 agent_destroy_event(event);
0475c50c
DG
170}
171
f20baf8e 172/*
022d91ba 173 * URCU delayed agent app reclaim.
f20baf8e 174 */
022d91ba 175static void destroy_app_agent_rcu(struct rcu_head *head)
f20baf8e
DG
176{
177 struct lttng_ht_node_ulong *node =
178 caa_container_of(head, struct lttng_ht_node_ulong, head);
022d91ba
DG
179 struct agent_app *app =
180 caa_container_of(node, struct agent_app, node);
f20baf8e
DG
181
182 free(app);
183}
184
185/*
022d91ba
DG
186 * Communication with the agent. Send the message header to the given socket in
187 * big endian.
f20baf8e
DG
188 *
189 * Return 0 on success or else a negative errno message of sendmsg() op.
190 */
191static int send_header(struct lttcomm_sock *sock, uint64_t data_size,
192 uint32_t cmd, uint32_t cmd_version)
193{
194 int ret;
195 ssize_t size;
022d91ba 196 struct lttcomm_agent_hdr msg;
f20baf8e
DG
197
198 assert(sock);
199
53efb85a 200 memset(&msg, 0, sizeof(msg));
f20baf8e
DG
201 msg.data_size = htobe64(data_size);
202 msg.cmd = htobe32(cmd);
203 msg.cmd_version = htobe32(cmd_version);
204
205 size = sock->ops->sendmsg(sock, &msg, sizeof(msg), 0);
206 if (size < sizeof(msg)) {
207 ret = -errno;
208 goto error;
209 }
210 ret = 0;
211
212error:
213 return ret;
214}
215
216/*
022d91ba
DG
217 * Communication call with the agent. Send the payload to the given socket. The
218 * header MUST be sent prior to this call.
f20baf8e
DG
219 *
220 * Return 0 on success or else a negative errno value of sendmsg() op.
221 */
222static int send_payload(struct lttcomm_sock *sock, void *data,
223 size_t size)
224{
225 int ret;
226 ssize_t len;
227
228 assert(sock);
229 assert(data);
230
231 len = sock->ops->sendmsg(sock, data, size, 0);
232 if (len < size) {
233 ret = -errno;
234 goto error;
235 }
236 ret = 0;
237
238error:
239 return ret;
240}
241
242/*
022d91ba
DG
243 * Communication call with the agent. Receive reply from the agent using the
244 * given socket.
f20baf8e
DG
245 *
246 * Return 0 on success or else a negative errno value from recvmsg() op.
247 */
248static int recv_reply(struct lttcomm_sock *sock, void *buf, size_t size)
249{
250 int ret;
251 ssize_t len;
252
253 assert(sock);
254 assert(buf);
255
256 len = sock->ops->recvmsg(sock, buf, size, 0);
257 if (len < size) {
258 ret = -errno;
259 goto error;
260 }
261 ret = 0;
262
263error:
264 return ret;
265}
266
3c6a091f 267/*
428de77a 268 * Internal event listing for a given app. Populate events.
3c6a091f
DG
269 *
270 * Return number of element in the list or else a negative LTTNG_ERR* code.
428de77a
MD
271 * On success, the caller is responsible for freeing the memory
272 * allocated for "events".
3c6a091f 273 */
022d91ba 274static ssize_t list_events(struct agent_app *app, struct lttng_event **events)
3c6a091f
DG
275{
276 int ret, i, len = 0, offset = 0;
277 uint32_t nb_event;
278 size_t data_size;
d84af1a4 279 uint32_t reply_ret_code;
3c6a091f 280 struct lttng_event *tmp_events = NULL;
022d91ba
DG
281 struct lttcomm_agent_list_reply *reply = NULL;
282 struct lttcomm_agent_list_reply_hdr reply_hdr;
3c6a091f
DG
283
284 assert(app);
285 assert(app->sock);
286 assert(events);
287
022d91ba 288 DBG2("Agent listing events for app pid: %d and socket %d", app->pid,
3c6a091f
DG
289 app->sock->fd);
290
022d91ba 291 ret = send_header(app->sock, 0, AGENT_CMD_LIST, 0);
3c6a091f
DG
292 if (ret < 0) {
293 goto error_io;
294 }
295
296 /* Get list header so we know how much we'll receive. */
297 ret = recv_reply(app->sock, &reply_hdr, sizeof(reply_hdr));
298 if (ret < 0) {
299 goto error_io;
300 }
301
d84af1a4
JG
302 reply_ret_code = be32toh(reply_hdr.ret_code);
303 log_reply_code(reply_ret_code);
304 switch (reply_ret_code) {
022d91ba 305 case AGENT_RET_CODE_SUCCESS:
3c6a091f
DG
306 data_size = be32toh(reply_hdr.data_size) + sizeof(*reply);
307 break;
308 default:
3bd9aaeb 309 ret = LTTNG_ERR_UNK;
3c6a091f
DG
310 goto error;
311 }
312
313 reply = zmalloc(data_size);
314 if (!reply) {
315 ret = LTTNG_ERR_NOMEM;
316 goto error;
317 }
318
319 /* Get the list with the appropriate data size. */
320 ret = recv_reply(app->sock, reply, data_size);
321 if (ret < 0) {
322 goto error_io;
323 }
324
325 nb_event = be32toh(reply->nb_event);
326 tmp_events = zmalloc(sizeof(*tmp_events) * nb_event);
327 if (!tmp_events) {
328 ret = LTTNG_ERR_NOMEM;
329 goto error;
330 }
331
332 for (i = 0; i < nb_event; i++) {
333 offset += len;
334 strncpy(tmp_events[i].name, reply->payload + offset,
335 sizeof(tmp_events[i].name));
336 tmp_events[i].pid = app->pid;
337 tmp_events[i].enabled = -1;
338 len = strlen(reply->payload + offset) + 1;
339 }
340
341 *events = tmp_events;
342
343 free(reply);
344 return nb_event;
345
346error_io:
347 ret = LTTNG_ERR_UST_LIST_FAIL;
348error:
349 free(reply);
350 free(tmp_events);
351 return -ret;
352
353}
354
f20baf8e 355/*
022d91ba
DG
356 * Internal enable agent event on a agent application. This function
357 * communicates with the agent to enable a given event.
f20baf8e
DG
358 *
359 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
360 */
022d91ba 361static int enable_event(struct agent_app *app, struct agent_event *event)
f20baf8e
DG
362{
363 int ret;
364 uint64_t data_size;
f4f9d4db 365 uint32_t reply_ret_code;
022d91ba
DG
366 struct lttcomm_agent_enable msg;
367 struct lttcomm_agent_generic_reply reply;
f20baf8e
DG
368
369 assert(app);
370 assert(app->sock);
371 assert(event);
372
022d91ba 373 DBG2("Agent enabling event %s for app pid: %d and socket %d", event->name,
f20baf8e
DG
374 app->pid, app->sock->fd);
375
376 data_size = sizeof(msg);
377
022d91ba 378 ret = send_header(app->sock, data_size, AGENT_CMD_ENABLE, 0);
f20baf8e
DG
379 if (ret < 0) {
380 goto error_io;
381 }
382
53efb85a 383 memset(&msg, 0, sizeof(msg));
2106efa0 384 msg.loglevel_value = event->loglevel_value;
b2064f54 385 msg.loglevel_type = event->loglevel_type;
f20baf8e
DG
386 strncpy(msg.name, event->name, sizeof(msg.name));
387 ret = send_payload(app->sock, &msg, sizeof(msg));
388 if (ret < 0) {
389 goto error_io;
390 }
391
392 ret = recv_reply(app->sock, &reply, sizeof(reply));
393 if (ret < 0) {
394 goto error_io;
395 }
396
f4f9d4db
JG
397 reply_ret_code = be32toh(reply.ret_code);
398 log_reply_code(reply_ret_code);
399 switch (reply_ret_code) {
022d91ba 400 case AGENT_RET_CODE_SUCCESS:
f20baf8e 401 break;
022d91ba 402 case AGENT_RET_CODE_UNKNOWN_NAME:
f20baf8e
DG
403 ret = LTTNG_ERR_UST_EVENT_NOT_FOUND;
404 goto error;
405 default:
3bd9aaeb 406 ret = LTTNG_ERR_UNK;
f20baf8e
DG
407 goto error;
408 }
409
410 return LTTNG_OK;
411
412error_io:
413 ret = LTTNG_ERR_UST_ENABLE_FAIL;
414error:
415 return ret;
416}
417
418/*
022d91ba
DG
419 * Internal disable agent event call on a agent application. This function
420 * communicates with the agent to disable a given event.
f20baf8e
DG
421 *
422 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
423 */
022d91ba 424static int disable_event(struct agent_app *app, struct agent_event *event)
f20baf8e
DG
425{
426 int ret;
427 uint64_t data_size;
517992f8 428 uint32_t reply_ret_code;
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
517992f8
JG
458 reply_ret_code = be32toh(reply.ret_code);
459 log_reply_code(reply_ret_code);
460 switch (reply_ret_code) {
022d91ba
DG
461 case AGENT_RET_CODE_SUCCESS:
462 break;
463 case AGENT_RET_CODE_UNKNOWN_NAME:
464 ret = LTTNG_ERR_UST_EVENT_NOT_FOUND;
465 goto error;
466 default:
3bd9aaeb 467 ret = LTTNG_ERR_UNK;
022d91ba 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);
022d91ba 722 lttng_ht_add_unique_ulong(agent_apps_ht_by_sock, &app->node);
f20baf8e
DG
723}
724
f20baf8e 725/*
022d91ba 726 * Delete agent application from the global hash table.
d558f236
JG
727 *
728 * rcu_read_lock() must be held by the caller.
f20baf8e 729 */
022d91ba 730void agent_delete_app(struct agent_app *app)
f20baf8e
DG
731{
732 int ret;
733 struct lttng_ht_iter iter;
734
735 assert(app);
736
022d91ba 737 DBG3("Agent deleting app pid: %d and sock: %d", app->pid, app->sock->fd);
f20baf8e
DG
738
739 iter.iter.node = &app->node.node;
022d91ba 740 ret = lttng_ht_del(agent_apps_ht_by_sock, &iter);
f20baf8e
DG
741 assert(!ret);
742}
743
744/*
e785906c 745 * Destroy an agent application object by detaching it from its corresponding
022d91ba 746 * UST app if one is connected by closing the socket. Finally, perform a
428de77a 747 * delayed memory reclaim.
f20baf8e 748 */
022d91ba 749void agent_destroy_app(struct agent_app *app)
f20baf8e
DG
750{
751 assert(app);
752
753 if (app->sock) {
754 app->sock->ops->close(app->sock);
755 lttcomm_destroy_sock(app->sock);
756 }
757
022d91ba 758 call_rcu(&app->node.head, destroy_app_agent_rcu);
f20baf8e
DG
759}
760
0475c50c 761/*
022d91ba 762 * Initialize an already allocated agent object.
0475c50c
DG
763 *
764 * Return 0 on success or else a negative errno value.
765 */
022d91ba 766int agent_init(struct agent *agt)
0475c50c
DG
767{
768 int ret;
769
022d91ba 770 assert(agt);
0475c50c 771
022d91ba
DG
772 agt->events = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
773 if (!agt->events) {
0475c50c
DG
774 ret = -ENOMEM;
775 goto error;
776 }
fefd409b 777 lttng_ht_node_init_u64(&agt->node, agt->domain);
0475c50c
DG
778
779 return 0;
780
781error:
782 return ret;
783}
784
fefd409b
DG
785/*
786 * Add agent object to the given hash table.
787 */
788void agent_add(struct agent *agt, struct lttng_ht *ht)
789{
790 assert(agt);
791 assert(ht);
792
793 DBG3("Agent adding from domain %d", agt->domain);
794
fefd409b 795 lttng_ht_add_unique_u64(ht, &agt->node);
fefd409b
DG
796}
797
798/*
799 * Create an agent object for the given domain.
800 *
801 * Return the allocated agent or NULL on error.
802 */
803struct agent *agent_create(enum lttng_domain_type domain)
804{
805 int ret;
806 struct agent *agt;
807
3b5f70d4 808 agt = zmalloc(sizeof(struct agent));
fefd409b
DG
809 if (!agt) {
810 goto error;
811 }
812 agt->domain = domain;
813
814 ret = agent_init(agt);
815 if (ret < 0) {
816 free(agt);
988ae332 817 agt = NULL;
fefd409b
DG
818 goto error;
819 }
820
821error:
822 return agt;
823}
824
0475c50c 825/*
51755dc8
JG
826 * Create a newly allocated agent event data structure.
827 * Ownership of filter_expression is taken.
0475c50c
DG
828 *
829 * Return a new object else NULL on error.
830 */
022d91ba 831struct agent_event *agent_create_event(const char *name,
a9319624 832 enum lttng_loglevel_type loglevel_type, int loglevel_value,
51755dc8 833 struct lttng_filter_bytecode *filter, char *filter_expression)
0475c50c 834{
51755dc8 835 struct agent_event *event = NULL;
0475c50c 836
a9319624
PP
837 DBG3("Agent create new event with name %s, loglevel type %d and loglevel value %d",
838 name, loglevel_type, loglevel_value);
0475c50c 839
51755dc8
JG
840 if (!name) {
841 ERR("Failed to create agent event; no name provided.");
0475c50c
DG
842 goto error;
843 }
844
51755dc8
JG
845 event = zmalloc(sizeof(*event));
846 if (!event) {
847 goto error;
0475c50c
DG
848 }
849
51755dc8
JG
850 strncpy(event->name, name, sizeof(event->name));
851 event->name[sizeof(event->name) - 1] = '\0';
852 lttng_ht_node_init_str(&event->node, event->name);
be6a6276 853
2106efa0 854 event->loglevel_value = loglevel_value;
51755dc8
JG
855 event->loglevel_type = loglevel_type;
856 event->filter = filter;
857 event->filter_expression = filter_expression;
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);
022d91ba 872 add_unique_agent_event(agt->events, event);
022d91ba 873 agt->being_used = 1;
0475c50c
DG
874}
875
876/*
022d91ba 877 * Find a agent event in the given agent using name.
4a4ab2c3
DG
878 *
879 * RCU read side lock MUST be acquired.
880 *
881 * Return object if found else NULL.
882 */
022d91ba
DG
883struct agent_event *agent_find_event_by_name(const char *name,
884 struct agent *agt)
4a4ab2c3
DG
885{
886 struct lttng_ht_node_str *node;
887 struct lttng_ht_iter iter;
888 struct lttng_ht *ht;
022d91ba 889 struct agent_ht_key key;
4a4ab2c3
DG
890
891 assert(name);
022d91ba
DG
892 assert(agt);
893 assert(agt->events);
4a4ab2c3 894
022d91ba 895 ht = agt->events;
4a4ab2c3
DG
896 key.name = name;
897
898 cds_lfht_lookup(ht->ht, ht->hash_fct((void *) name, lttng_ht_seed),
899 ht_match_event_by_name, &key, &iter.iter);
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 by name.", name);
906 return caa_container_of(node, struct agent_event, node);
4a4ab2c3
DG
907
908error:
022d91ba 909 DBG3("Agent NOT found by name %s.", name);
4a4ab2c3
DG
910 return NULL;
911}
912
913/*
022d91ba 914 * Find a agent event in the given agent using name and loglevel.
0475c50c
DG
915 *
916 * RCU read side lock MUST be acquired.
917 *
918 * Return object if found else NULL.
919 */
a9319624
PP
920struct agent_event *agent_find_event(const char *name,
921 enum lttng_loglevel_type loglevel_type, int loglevel_value,
022d91ba 922 struct agent *agt)
0475c50c
DG
923{
924 struct lttng_ht_node_str *node;
925 struct lttng_ht_iter iter;
4a4ab2c3 926 struct lttng_ht *ht;
022d91ba 927 struct agent_ht_key key;
0475c50c
DG
928
929 assert(name);
022d91ba
DG
930 assert(agt);
931 assert(agt->events);
0475c50c 932
022d91ba 933 ht = agt->events;
4a4ab2c3 934 key.name = name;
2106efa0 935 key.loglevel_value = loglevel_value;
a9319624 936 key.loglevel_type = loglevel_type;
4a4ab2c3
DG
937
938 cds_lfht_lookup(ht->ht, ht->hash_fct((void *) name, lttng_ht_seed),
939 ht_match_event, &key, &iter.iter);
0475c50c
DG
940 node = lttng_ht_iter_get_node_str(&iter);
941 if (node == NULL) {
942 goto error;
943 }
944
022d91ba
DG
945 DBG3("Agent event found %s.", name);
946 return caa_container_of(node, struct agent_event, node);
0475c50c
DG
947
948error:
a51e817b 949 DBG3("Agent event NOT found %s.", name);
0475c50c
DG
950 return NULL;
951}
952
0475c50c 953/*
022d91ba
DG
954 * Free given agent event. This event must not be globally visible at this
955 * point (only expected to be used on failure just after event creation). After
956 * this call, the pointer is not usable anymore.
0475c50c 957 */
022d91ba 958void agent_destroy_event(struct agent_event *event)
0475c50c
DG
959{
960 assert(event);
961
971da06a 962 free(event->filter);
8404118c 963 free(event->filter_expression);
51755dc8 964 free(event->exclusion);
0475c50c
DG
965 free(event);
966}
967
968/*
35ed21a5 969 * Destroy an agent completely.
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 979
0475c50c 980 rcu_read_lock();
022d91ba 981 cds_lfht_for_each_entry(agt->events->ht, &iter.iter, node, node) {
0475c50c 982 int ret;
022d91ba 983 struct agent_event *event;
29c0fd4d
DG
984
985 /*
986 * When destroying an event, we have to try to disable it on the agent
987 * side so the event stops generating data. The return value is not
988 * important since we have to continue anyway destroying the object.
989 */
022d91ba
DG
990 event = caa_container_of(node, struct agent_event, node);
991 (void) agent_disable_event(event, agt->domain);
0475c50c 992
022d91ba 993 ret = lttng_ht_del(agt->events, &iter);
0475c50c 994 assert(!ret);
022d91ba 995 call_rcu(&node->head, destroy_event_agent_rcu);
0475c50c
DG
996 }
997 rcu_read_unlock();
998
f8be902b 999 ht_cleanup_push(agt->events);
35ed21a5 1000 free(agt);
f20baf8e
DG
1001}
1002
1003/*
6a4e4039 1004 * Allocate agent_apps_ht_by_sock.
f20baf8e 1005 */
6a4e4039 1006int agent_app_ht_alloc(void)
f20baf8e 1007{
6a4e4039
JG
1008 int ret = 0;
1009
022d91ba
DG
1010 agent_apps_ht_by_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
1011 if (!agent_apps_ht_by_sock) {
6a4e4039 1012 ret = -1;
f20baf8e
DG
1013 }
1014
6a4e4039
JG
1015 return ret;
1016}
1017
1018/*
1019 * Destroy a agent application by socket.
1020 */
1021void agent_destroy_app_by_sock(int sock)
1022{
1023 struct agent_app *app;
1024
1025 assert(sock >= 0);
1026
1027 /*
1028 * Not finding an application is a very important error that should NEVER
1029 * happen. The hash table deletion is ONLY done through this call when the
1030 * main sessiond thread is torn down.
1031 */
1032 rcu_read_lock();
1033 app = agent_find_app_by_sock(sock);
1034 assert(app);
1035
1036 /* RCU read side lock is assumed to be held by this function. */
1037 agent_delete_app(app);
1038
1039 /* The application is freed in a RCU call but the socket is closed here. */
1040 agent_destroy_app(app);
1041 rcu_read_unlock();
1042}
1043
1044/*
1045 * Clean-up the agent app hash table and destroy it.
1046 */
1047void agent_app_ht_clean(void)
1048{
1049 struct lttng_ht_node_ulong *node;
1050 struct lttng_ht_iter iter;
1051
a433283e
JG
1052 if (!agent_apps_ht_by_sock) {
1053 return;
1054 }
6a4e4039
JG
1055 rcu_read_lock();
1056 cds_lfht_for_each_entry(agent_apps_ht_by_sock->ht, &iter.iter, node, node) {
1057 struct agent_app *app;
1058
1059 app = caa_container_of(node, struct agent_app, node);
1060 agent_destroy_app_by_sock(app->sock->fd);
1061 }
1062 rcu_read_unlock();
1063
1064 lttng_ht_destroy(agent_apps_ht_by_sock);
f20baf8e
DG
1065}
1066
1067/*
022d91ba 1068 * Update a agent application (given socket) using the given agent.
f20baf8e
DG
1069 *
1070 * Note that this function is most likely to be used with a tracing session
1071 * thus the caller should make sure to hold the appropriate lock(s).
1072 */
022d91ba 1073void agent_update(struct agent *agt, int sock)
f20baf8e
DG
1074{
1075 int ret;
022d91ba
DG
1076 struct agent_app *app;
1077 struct agent_event *event;
f20baf8e
DG
1078 struct lttng_ht_iter iter;
1079
022d91ba 1080 assert(agt);
f20baf8e
DG
1081 assert(sock >= 0);
1082
022d91ba 1083 DBG("Agent updating app socket %d", sock);
f20baf8e
DG
1084
1085 rcu_read_lock();
022d91ba 1086 cds_lfht_for_each_entry(agt->events->ht, &iter.iter, event, node.node) {
f20baf8e
DG
1087 /* Skip event if disabled. */
1088 if (!event->enabled) {
1089 continue;
1090 }
1091
022d91ba 1092 app = agent_find_app_by_sock(sock);
f20baf8e
DG
1093 /*
1094 * We are in the registration path thus if the application is gone,
1095 * there is a serious code flow error.
1096 */
1097 assert(app);
1098
1099 ret = enable_event(app, event);
1100 if (ret != LTTNG_OK) {
022d91ba 1101 DBG2("Agent update unable to enable event %s on app pid: %d sock %d",
f20baf8e
DG
1102 event->name, app->pid, app->sock->fd);
1103 /* Let's try the others here and don't assume the app is dead. */
1104 continue;
1105 }
1106 }
1107 rcu_read_unlock();
0475c50c 1108}
This page took 0.142137 seconds and 4 git commands to generate.