Test: Ensure wildcards may only appear last in a string literal
[lttng-tools.git] / src / bin / lttng-sessiond / jul.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>
f20baf8e 23#include <common/sessiond-comm/jul.h>
0475c50c 24
f263b7fd
JD
25#include <common/compat/endian.h>
26
0475c50c 27#include "jul.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{
37 struct jul_event *event;
38 const struct jul_ht_key *key;
39
40 assert(node);
41 assert(_key);
42
43 event = caa_container_of(node, struct jul_event, node.node);
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{
65 struct jul_event *event;
66 const struct jul_ht_key *key;
67
68 assert(node);
69 assert(_key);
70
71 event = caa_container_of(node, struct jul_event, node.node);
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/*
96 * Add unique JUL event based on the event name and loglevel.
97 */
98static void add_unique_jul_event(struct lttng_ht *ht, struct jul_event *event)
99{
100 struct cds_lfht_node *node_ptr;
101 struct jul_ht_key key;
102
103 assert(ht);
104 assert(ht->ht);
105 assert(event);
106
107 key.name = event->name;
108 key.loglevel = event->loglevel;
109
110 node_ptr = cds_lfht_add_unique(ht->ht,
111 ht->hash_fct(event->node.key, lttng_ht_seed),
112 ht_match_event, &key, &event->node.node);
113 assert(node_ptr == &event->node.node);
114}
115
0475c50c 116/*
428de77a 117 * URCU delayed JUL event reclaim.
0475c50c
DG
118 */
119static void destroy_event_jul_rcu(struct rcu_head *head)
120{
121 struct lttng_ht_node_str *node =
122 caa_container_of(head, struct lttng_ht_node_str, head);
123 struct jul_event *event =
124 caa_container_of(node, struct jul_event, node);
125
126 free(event);
127}
128
f20baf8e 129/*
428de77a 130 * URCU delayed JUL app reclaim.
f20baf8e
DG
131 */
132static void destroy_app_jul_rcu(struct rcu_head *head)
133{
134 struct lttng_ht_node_ulong *node =
135 caa_container_of(head, struct lttng_ht_node_ulong, head);
136 struct jul_app *app =
137 caa_container_of(node, struct jul_app, node);
138
139 free(app);
140}
141
142/*
428de77a
MD
143 * Communication with Java agent. Send the message header to the given
144 * socket in big endian.
f20baf8e
DG
145 *
146 * Return 0 on success or else a negative errno message of sendmsg() op.
147 */
148static int send_header(struct lttcomm_sock *sock, uint64_t data_size,
149 uint32_t cmd, uint32_t cmd_version)
150{
151 int ret;
152 ssize_t size;
153 struct lttcomm_jul_hdr msg;
154
155 assert(sock);
156
53efb85a 157 memset(&msg, 0, sizeof(msg));
f20baf8e
DG
158 msg.data_size = htobe64(data_size);
159 msg.cmd = htobe32(cmd);
160 msg.cmd_version = htobe32(cmd_version);
161
162 size = sock->ops->sendmsg(sock, &msg, sizeof(msg), 0);
163 if (size < sizeof(msg)) {
164 ret = -errno;
165 goto error;
166 }
167 ret = 0;
168
169error:
170 return ret;
171}
172
173/*
174 * Communication call with the Java agent. Send the payload to the given
175 * socket. The header MUST be sent prior to this call.
176 *
177 * Return 0 on success or else a negative errno value of sendmsg() op.
178 */
179static int send_payload(struct lttcomm_sock *sock, void *data,
180 size_t size)
181{
182 int ret;
183 ssize_t len;
184
185 assert(sock);
186 assert(data);
187
188 len = sock->ops->sendmsg(sock, data, size, 0);
189 if (len < size) {
190 ret = -errno;
191 goto error;
192 }
193 ret = 0;
194
195error:
196 return ret;
197}
198
199/*
200 * Communication call with the Java agent. Receive reply from the agent using
201 * the given socket.
202 *
203 * Return 0 on success or else a negative errno value from recvmsg() op.
204 */
205static int recv_reply(struct lttcomm_sock *sock, void *buf, size_t size)
206{
207 int ret;
208 ssize_t len;
209
210 assert(sock);
211 assert(buf);
212
213 len = sock->ops->recvmsg(sock, buf, size, 0);
214 if (len < size) {
215 ret = -errno;
216 goto error;
217 }
218 ret = 0;
219
220error:
221 return ret;
222}
223
3c6a091f
DG
224
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
DG
231 */
232static ssize_t list_events(struct jul_app *app, struct lttng_event **events)
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;
238 struct lttcomm_jul_list_reply *reply = NULL;
239 struct lttcomm_jul_list_reply_hdr reply_hdr;
240
241 assert(app);
242 assert(app->sock);
243 assert(events);
244
245 DBG2("JUL listing events for app pid: %d and socket %d", app->pid,
246 app->sock->fd);
247
248 ret = send_header(app->sock, 0, JUL_CMD_LIST, 0);
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)) {
260 case JUL_RET_CODE_SUCCESS:
261 data_size = be32toh(reply_hdr.data_size) + sizeof(*reply);
262 break;
263 default:
264 ERR("Java agent returned an unknown code: %" PRIu32,
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;
291 strncpy(tmp_events[i].name, reply->payload + offset,
292 sizeof(tmp_events[i].name));
293 tmp_events[i].pid = app->pid;
294 tmp_events[i].enabled = -1;
295 len = strlen(reply->payload + offset) + 1;
296 }
297
298 *events = tmp_events;
299
300 free(reply);
301 return nb_event;
302
303error_io:
304 ret = LTTNG_ERR_UST_LIST_FAIL;
305error:
306 free(reply);
307 free(tmp_events);
308 return -ret;
309
310}
311
f20baf8e 312/*
428de77a 313 * Internal enable JUL event on a JUL application. This function
f20baf8e
DG
314 * communicates with the Java agent to enable a given event (Logger name).
315 *
316 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
317 */
318static int enable_event(struct jul_app *app, struct jul_event *event)
319{
320 int ret;
321 uint64_t data_size;
322 struct lttcomm_jul_enable msg;
323 struct lttcomm_jul_generic_reply reply;
324
325 assert(app);
326 assert(app->sock);
327 assert(event);
328
329 DBG2("JUL enabling event %s for app pid: %d and socket %d", event->name,
330 app->pid, app->sock->fd);
331
332 data_size = sizeof(msg);
333
334 ret = send_header(app->sock, data_size, JUL_CMD_ENABLE, 0);
335 if (ret < 0) {
336 goto error_io;
337 }
338
53efb85a 339 memset(&msg, 0, sizeof(msg));
b2064f54
DG
340 msg.loglevel = event->loglevel;
341 msg.loglevel_type = event->loglevel_type;
f20baf8e
DG
342 strncpy(msg.name, event->name, sizeof(msg.name));
343 ret = send_payload(app->sock, &msg, sizeof(msg));
344 if (ret < 0) {
345 goto error_io;
346 }
347
348 ret = recv_reply(app->sock, &reply, sizeof(reply));
349 if (ret < 0) {
350 goto error_io;
351 }
352
353 switch (be32toh(reply.ret_code)) {
354 case JUL_RET_CODE_SUCCESS:
355 break;
356 case JUL_RET_CODE_UNKNOWN_NAME:
357 ret = LTTNG_ERR_UST_EVENT_NOT_FOUND;
358 goto error;
359 default:
360 ERR("Java agent returned an unknown code: %" PRIu32,
361 be32toh(reply.ret_code));
362 ret = LTTNG_ERR_FATAL;
363 goto error;
364 }
365
366 return LTTNG_OK;
367
368error_io:
369 ret = LTTNG_ERR_UST_ENABLE_FAIL;
370error:
371 return ret;
372}
373
374/*
375 * Internal disable JUL event call on a JUL application. This function
376 * communicates with the Java agent to disable a given event (Logger name).
377 *
378 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
379 */
380static int disable_event(struct jul_app *app, struct jul_event *event)
381{
382 int ret;
383 uint64_t data_size;
384 struct lttcomm_jul_disable msg;
385 struct lttcomm_jul_generic_reply reply;
386
387 assert(app);
388 assert(app->sock);
389 assert(event);
390
391 DBG2("JUL disabling event %s for app pid: %d and socket %d", event->name,
392 app->pid, app->sock->fd);
393
394 data_size = sizeof(msg);
395
396 ret = send_header(app->sock, data_size, JUL_CMD_DISABLE, 0);
397 if (ret < 0) {
398 goto error_io;
399 }
400
53efb85a 401 memset(&msg, 0, sizeof(msg));
f20baf8e
DG
402 strncpy(msg.name, event->name, sizeof(msg.name));
403 ret = send_payload(app->sock, &msg, sizeof(msg));
404 if (ret < 0) {
405 goto error_io;
406 }
407
408 ret = recv_reply(app->sock, &reply, sizeof(reply));
409 if (ret < 0) {
410 goto error_io;
411 }
412
413 switch (be32toh(reply.ret_code)) {
414 case JUL_RET_CODE_SUCCESS:
415 break;
416 case JUL_RET_CODE_UNKNOWN_NAME:
417 ret = LTTNG_ERR_UST_EVENT_NOT_FOUND;
418 goto error;
419 default:
420 ERR("Java agent returned an unknown code: %" PRIu32,
421 be32toh(reply.ret_code));
422 ret = LTTNG_ERR_FATAL;
423 goto error;
424 }
425
426 return LTTNG_OK;
427
428error_io:
429 ret = LTTNG_ERR_UST_DISABLE_FAIL;
430error:
431 return ret;
432}
433
1b500e7a
DG
434/*
435 * Send back the registration DONE command to a given JUL application.
436 *
437 * Return 0 on success or else a negative value.
438 */
439int jul_send_registration_done(struct jul_app *app)
440{
441 assert(app);
442 assert(app->sock);
443
444 DBG("JUL sending registration done to app socket %d", app->sock->fd);
445
446 return send_header(app->sock, 0, JUL_CMD_REG_DONE, 0);
447}
448
f20baf8e
DG
449/*
450 * Enable JUL event on every JUL applications registered with the session
451 * daemon.
452 *
453 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
454 */
455int jul_enable_event(struct jul_event *event)
456{
457 int ret;
458 struct jul_app *app;
459 struct lttng_ht_iter iter;
460
461 assert(event);
462
463 rcu_read_lock();
464
465 cds_lfht_for_each_entry(jul_apps_ht_by_sock->ht, &iter.iter, app,
466 node.node) {
467 /* Enable event on JUL application through TCP socket. */
468 ret = enable_event(app, event);
469 if (ret != LTTNG_OK) {
470 goto error;
471 }
f20baf8e
DG
472 }
473
3c6a091f 474 event->enabled = 1;
f20baf8e
DG
475 ret = LTTNG_OK;
476
477error:
478 rcu_read_unlock();
479 return ret;
480}
481
482/*
483 * Disable JUL event on every JUL applications registered with the session
484 * daemon.
485 *
486 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
487 */
488int jul_disable_event(struct jul_event *event)
489{
490 int ret;
491 struct jul_app *app;
492 struct lttng_ht_iter iter;
493
494 assert(event);
495
496 rcu_read_lock();
497
498 cds_lfht_for_each_entry(jul_apps_ht_by_sock->ht, &iter.iter, app,
499 node.node) {
500 /* Enable event on JUL application through TCP socket. */
501 ret = disable_event(app, event);
502 if (ret != LTTNG_OK) {
503 goto error;
504 }
f20baf8e
DG
505 }
506
3c6a091f 507 event->enabled = 0;
f20baf8e
DG
508 ret = LTTNG_OK;
509
510error:
511 rcu_read_unlock();
512 return ret;
513}
514
515/*
516 * Ask every java agent for the list of possible event (logger name). Events is
517 * allocated with the events of every JUL application.
518 *
519 * Return the number of events or else a negative value.
520 */
521int jul_list_events(struct lttng_event **events)
522{
523 int ret;
524 size_t nbmem, count = 0;
525 struct jul_app *app;
aae6255e 526 struct lttng_event *tmp_events = NULL;
f20baf8e
DG
527 struct lttng_ht_iter iter;
528
529 assert(events);
530
531 nbmem = UST_APP_EVENT_LIST_SIZE;
532 tmp_events = zmalloc(nbmem * sizeof(*tmp_events));
533 if (!tmp_events) {
534 PERROR("zmalloc jul list events");
535 ret = -ENOMEM;
536 goto error;
537 }
538
539 rcu_read_lock();
540 cds_lfht_for_each_entry(jul_apps_ht_by_sock->ht, &iter.iter, app,
541 node.node) {
542 ssize_t nb_ev;
543 struct lttng_event *jul_events;
544
545 nb_ev = list_events(app, &jul_events);
546 if (nb_ev < 0) {
547 ret = nb_ev;
428de77a 548 goto error_unlock;
f20baf8e
DG
549 }
550
53efb85a 551 if (count + nb_ev > nbmem) {
f20baf8e 552 /* In case the realloc fails, we free the memory */
53efb85a
MD
553 struct lttng_event *new_tmp_events;
554 size_t new_nbmem;
555
556 new_nbmem = max_t(size_t, count + nb_ev, nbmem << 1);
557 DBG2("Reallocating JUL event list from %zu to %zu entries",
558 nbmem, new_nbmem);
559 new_tmp_events = realloc(tmp_events,
560 new_nbmem * sizeof(*new_tmp_events));
561 if (!new_tmp_events) {
f20baf8e 562 PERROR("realloc JUL events");
f20baf8e 563 ret = -ENOMEM;
c36441a9 564 free(jul_events);
428de77a 565 goto error_unlock;
f20baf8e 566 }
53efb85a
MD
567 /* Zero the new memory */
568 memset(new_tmp_events + nbmem, 0,
569 (new_nbmem - nbmem) * sizeof(*new_tmp_events));
570 nbmem = new_nbmem;
571 tmp_events = new_tmp_events;
f20baf8e 572 }
53efb85a
MD
573 memcpy(tmp_events + count, jul_events,
574 nb_ev * sizeof(*tmp_events));
f20baf8e
DG
575 free(jul_events);
576 count += nb_ev;
577 }
578 rcu_read_unlock();
579
580 ret = count;
581 *events = tmp_events;
aae6255e 582 return ret;
f20baf8e 583
428de77a
MD
584error_unlock:
585 rcu_read_unlock();
f20baf8e 586error:
aae6255e 587 free(tmp_events);
f20baf8e
DG
588 return ret;
589}
590
591/*
592 * Create a JUL app object using the given PID.
593 *
594 * Return newly allocated object or else NULL on error.
595 */
596struct jul_app *jul_create_app(pid_t pid, struct lttcomm_sock *sock)
597{
598 struct jul_app *app;
599
600 assert(sock);
601
602 app = zmalloc(sizeof(*app));
603 if (!app) {
604 PERROR("zmalloc JUL create");
605 goto error;
606 }
607
608 app->pid = pid;
609 app->sock = sock;
f20baf8e
DG
610 lttng_ht_node_init_ulong(&app->node, (unsigned long) app->sock->fd);
611
612error:
613 return app;
614}
615
616/*
617 * Lookup JUL app by socket in the global hash table.
618 *
619 * RCU read side lock MUST be acquired.
620 *
621 * Return object if found else NULL.
622 */
623struct jul_app *jul_find_app_by_sock(int sock)
624{
625 struct lttng_ht_node_ulong *node;
626 struct lttng_ht_iter iter;
627 struct jul_app *app;
628
629 assert(sock >= 0);
630
631 lttng_ht_lookup(jul_apps_ht_by_sock, (void *)((unsigned long) sock), &iter);
632 node = lttng_ht_iter_get_node_ulong(&iter);
633 if (node == NULL) {
634 goto error;
635 }
636 app = caa_container_of(node, struct jul_app, node);
637
638 DBG3("JUL app pid %d found by sock %d.", app->pid, sock);
639 return app;
640
641error:
642 DBG3("JUL app NOT found by sock %d.", sock);
643 return NULL;
644}
645
646/*
647 * Add JUL application object to a given hash table.
648 */
649void jul_add_app(struct jul_app *app)
650{
651 assert(app);
652
653 DBG3("JUL adding app sock: %d and pid: %d to ht", app->sock->fd, app->pid);
654
655 rcu_read_lock();
656 lttng_ht_add_unique_ulong(jul_apps_ht_by_sock, &app->node);
657 rcu_read_unlock();
658}
659
f20baf8e
DG
660/*
661 * Delete JUL application from the global hash table.
662 */
663void jul_delete_app(struct jul_app *app)
664{
665 int ret;
666 struct lttng_ht_iter iter;
667
668 assert(app);
669
670 DBG3("JUL deleting app pid: %d and sock: %d", app->pid, app->sock->fd);
671
672 iter.iter.node = &app->node.node;
673 rcu_read_lock();
674 ret = lttng_ht_del(jul_apps_ht_by_sock, &iter);
675 rcu_read_unlock();
676 assert(!ret);
677}
678
679/*
680 * Destroy a JUL application object by detaching it from its corresponding UST
428de77a
MD
681 * app if one is connected by closing the socket. Finally, perform a
682 * delayed memory reclaim.
f20baf8e
DG
683 */
684void jul_destroy_app(struct jul_app *app)
685{
686 assert(app);
687
688 if (app->sock) {
689 app->sock->ops->close(app->sock);
690 lttcomm_destroy_sock(app->sock);
691 }
692
693 call_rcu(&app->node.head, destroy_app_jul_rcu);
694}
695
0475c50c
DG
696/*
697 * Initialize an already allocated JUL domain object.
698 *
699 * Return 0 on success or else a negative errno value.
700 */
701int jul_init_domain(struct jul_domain *dom)
702{
703 int ret;
704
705 assert(dom);
706
707 dom->events = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
708 if (!dom->events) {
709 ret = -ENOMEM;
710 goto error;
711 }
712
713 return 0;
714
715error:
716 return ret;
717}
718
719/*
720 * Create a newly allocated JUL event data structure. If name is valid, it's
721 * copied into the created event.
722 *
723 * Return a new object else NULL on error.
724 */
be6a6276
DG
725struct jul_event *jul_create_event(const char *name,
726 struct lttng_filter_bytecode *filter)
0475c50c
DG
727{
728 struct jul_event *event;
729
730 DBG3("JUL create new event with name %s", name);
731
732 event = zmalloc(sizeof(*event));
733 if (!event) {
734 goto error;
735 }
736
737 if (name) {
738 strncpy(event->name, name, sizeof(event->name));
739 event->name[sizeof(event->name) - 1] = '\0';
f20baf8e 740 lttng_ht_node_init_str(&event->node, event->name);
0475c50c
DG
741 }
742
be6a6276
DG
743 if (filter) {
744 event->filter = filter;
745 }
746
0475c50c
DG
747error:
748 return event;
749}
750
751/*
752 * Unique add of a JUL event to a given domain.
753 */
754void jul_add_event(struct jul_event *event, struct jul_domain *dom)
755{
756 assert(event);
757 assert(dom);
758 assert(dom->events);
759
760 DBG3("JUL adding event %s to domain", event->name);
761
f20baf8e 762 rcu_read_lock();
4a4ab2c3 763 add_unique_jul_event(dom->events, event);
f20baf8e 764 rcu_read_unlock();
3c6a091f 765 dom->being_used = 1;
0475c50c
DG
766}
767
768/*
4a4ab2c3
DG
769 * Find a JUL event in the given domain using name and loglevel.
770 *
771 * RCU read side lock MUST be acquired.
772 *
773 * Return object if found else NULL.
774 */
775struct jul_event *jul_find_event_by_name(const char *name,
776 struct jul_domain *dom)
777{
778 struct lttng_ht_node_str *node;
779 struct lttng_ht_iter iter;
780 struct lttng_ht *ht;
781 struct jul_ht_key key;
782
783 assert(name);
784 assert(dom);
785 assert(dom->events);
786
787 ht = dom->events;
788 key.name = name;
789
790 cds_lfht_lookup(ht->ht, ht->hash_fct((void *) name, lttng_ht_seed),
791 ht_match_event_by_name, &key, &iter.iter);
792 node = lttng_ht_iter_get_node_str(&iter);
793 if (node == NULL) {
794 goto error;
795 }
796
797 DBG3("JUL event found %s by name.", name);
798 return caa_container_of(node, struct jul_event, node);
799
800error:
801 DBG3("JUL NOT found by name %s.", name);
802 return NULL;
803}
804
805/*
806 * Find a JUL event in the given domain using name and loglevel.
0475c50c
DG
807 *
808 * RCU read side lock MUST be acquired.
809 *
810 * Return object if found else NULL.
811 */
4a4ab2c3
DG
812struct jul_event *jul_find_event(const char *name,
813 enum lttng_loglevel_jul loglevel, struct jul_domain *dom)
0475c50c
DG
814{
815 struct lttng_ht_node_str *node;
816 struct lttng_ht_iter iter;
4a4ab2c3
DG
817 struct lttng_ht *ht;
818 struct jul_ht_key key;
0475c50c
DG
819
820 assert(name);
821 assert(dom);
822 assert(dom->events);
823
4a4ab2c3
DG
824 ht = dom->events;
825 key.name = name;
826 key.loglevel = loglevel;
827
828 cds_lfht_lookup(ht->ht, ht->hash_fct((void *) name, lttng_ht_seed),
829 ht_match_event, &key, &iter.iter);
0475c50c
DG
830 node = lttng_ht_iter_get_node_str(&iter);
831 if (node == NULL) {
832 goto error;
833 }
834
4a4ab2c3 835 DBG3("JUL event found %s.", name);
0475c50c
DG
836 return caa_container_of(node, struct jul_event, node);
837
838error:
4a4ab2c3 839 DBG3("JUL NOT found %s.", name);
0475c50c
DG
840 return NULL;
841}
842
0475c50c 843/*
428de77a
MD
844 * Free given JUL event. This event must not be globally visible at this
845 * point (only expected to be used on failure just after event
846 * creation). After this call, the pointer is not usable anymore.
0475c50c
DG
847 */
848void jul_destroy_event(struct jul_event *event)
849{
850 assert(event);
851
852 free(event);
853}
854
855/*
856 * Destroy a JUL domain completely. Note that the given pointer is NOT freed
428de77a 857 * thus a reference to static or stack data can be passed to this function.
0475c50c
DG
858 */
859void jul_destroy_domain(struct jul_domain *dom)
860{
861 struct lttng_ht_node_str *node;
862 struct lttng_ht_iter iter;
863
864 assert(dom);
865
866 DBG3("JUL destroy domain");
867
868 /*
869 * Just ignore if no events hash table exists. This is possible if for
870 * instance a JUL domain object was allocated but not initialized.
871 */
872 if (!dom->events) {
873 return;
874 }
875
876 rcu_read_lock();
877 cds_lfht_for_each_entry(dom->events->ht, &iter.iter, node, node) {
878 int ret;
29c0fd4d
DG
879 struct jul_event *event;
880
881 /*
882 * When destroying an event, we have to try to disable it on the agent
883 * side so the event stops generating data. The return value is not
884 * important since we have to continue anyway destroying the object.
885 */
886 event = caa_container_of(node, struct jul_event, node);
887 (void) jul_disable_event(event);
0475c50c
DG
888
889 ret = lttng_ht_del(dom->events, &iter);
890 assert(!ret);
891 call_rcu(&node->head, destroy_event_jul_rcu);
892 }
893 rcu_read_unlock();
894
f20baf8e
DG
895 lttng_ht_destroy(dom->events);
896}
897
898/*
899 * Initialize JUL subsystem.
900 */
901int jul_init(void)
902{
903 jul_apps_ht_by_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
904 if (!jul_apps_ht_by_sock) {
905 return -1;
906 }
907
908 return 0;
909}
910
911/*
912 * Update a JUL application (given socket) using the given domain.
913 *
914 * Note that this function is most likely to be used with a tracing session
915 * thus the caller should make sure to hold the appropriate lock(s).
916 */
917void jul_update(struct jul_domain *domain, int sock)
918{
919 int ret;
920 struct jul_app *app;
921 struct jul_event *event;
922 struct lttng_ht_iter iter;
923
924 assert(domain);
925 assert(sock >= 0);
926
927 DBG("JUL updating app socket %d", sock);
928
929 rcu_read_lock();
930 cds_lfht_for_each_entry(domain->events->ht, &iter.iter, event, node.node) {
931 /* Skip event if disabled. */
932 if (!event->enabled) {
933 continue;
934 }
935
936 app = jul_find_app_by_sock(sock);
937 /*
938 * We are in the registration path thus if the application is gone,
939 * there is a serious code flow error.
940 */
941 assert(app);
942
943 ret = enable_event(app, event);
944 if (ret != LTTNG_OK) {
945 DBG2("JUL update unable to enable event %s on app pid: %d sock %d",
946 event->name, app->pid, app->sock->fd);
947 /* Let's try the others here and don't assume the app is dead. */
948 continue;
949 }
950 }
951 rcu_read_unlock();
0475c50c 952}
This page took 0.064885 seconds and 5 git commands to generate.