Fix: miscellaneous memory handling fixes
[lttng-tools.git] / src / bin / lttng-sessiond / jul.c
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>
20 #include <urcu/uatomic.h>
21
22 #include <common/common.h>
23 #include <common/sessiond-comm/jul.h>
24
25 #include "jul.h"
26 #include "ust-app.h"
27 #include "utils.h"
28
29 /*
30 * URCU delayed JUL event reclaim.
31 */
32 static void destroy_event_jul_rcu(struct rcu_head *head)
33 {
34 struct lttng_ht_node_str *node =
35 caa_container_of(head, struct lttng_ht_node_str, head);
36 struct jul_event *event =
37 caa_container_of(node, struct jul_event, node);
38
39 free(event);
40 }
41
42 /*
43 * URCU delayed JUL app reclaim.
44 */
45 static void destroy_app_jul_rcu(struct rcu_head *head)
46 {
47 struct lttng_ht_node_ulong *node =
48 caa_container_of(head, struct lttng_ht_node_ulong, head);
49 struct jul_app *app =
50 caa_container_of(node, struct jul_app, node);
51
52 free(app);
53 }
54
55 /*
56 * Communication with Java agent. Send the message header to the given
57 * socket in big endian.
58 *
59 * Return 0 on success or else a negative errno message of sendmsg() op.
60 */
61 static int send_header(struct lttcomm_sock *sock, uint64_t data_size,
62 uint32_t cmd, uint32_t cmd_version)
63 {
64 int ret;
65 ssize_t size;
66 struct lttcomm_jul_hdr msg;
67
68 assert(sock);
69
70 memset(&msg, 0, sizeof(msg));
71 msg.data_size = htobe64(data_size);
72 msg.cmd = htobe32(cmd);
73 msg.cmd_version = htobe32(cmd_version);
74
75 size = sock->ops->sendmsg(sock, &msg, sizeof(msg), 0);
76 if (size < sizeof(msg)) {
77 ret = -errno;
78 goto error;
79 }
80 ret = 0;
81
82 error:
83 return ret;
84 }
85
86 /*
87 * Communication call with the Java agent. Send the payload to the given
88 * socket. The header MUST be sent prior to this call.
89 *
90 * Return 0 on success or else a negative errno value of sendmsg() op.
91 */
92 static int send_payload(struct lttcomm_sock *sock, void *data,
93 size_t size)
94 {
95 int ret;
96 ssize_t len;
97
98 assert(sock);
99 assert(data);
100
101 len = sock->ops->sendmsg(sock, data, size, 0);
102 if (len < size) {
103 ret = -errno;
104 goto error;
105 }
106 ret = 0;
107
108 error:
109 return ret;
110 }
111
112 /*
113 * Communication call with the Java agent. Receive reply from the agent using
114 * the given socket.
115 *
116 * Return 0 on success or else a negative errno value from recvmsg() op.
117 */
118 static int recv_reply(struct lttcomm_sock *sock, void *buf, size_t size)
119 {
120 int ret;
121 ssize_t len;
122
123 assert(sock);
124 assert(buf);
125
126 len = sock->ops->recvmsg(sock, buf, size, 0);
127 if (len < size) {
128 ret = -errno;
129 goto error;
130 }
131 ret = 0;
132
133 error:
134 return ret;
135 }
136
137
138 /*
139 * Internal event listing for a given app. Populate events.
140 *
141 * Return number of element in the list or else a negative LTTNG_ERR* code.
142 * On success, the caller is responsible for freeing the memory
143 * allocated for "events".
144 */
145 static ssize_t list_events(struct jul_app *app, struct lttng_event **events)
146 {
147 int ret, i, len = 0, offset = 0;
148 uint32_t nb_event;
149 size_t data_size;
150 struct lttng_event *tmp_events = NULL;
151 struct lttcomm_jul_list_reply *reply = NULL;
152 struct lttcomm_jul_list_reply_hdr reply_hdr;
153
154 assert(app);
155 assert(app->sock);
156 assert(events);
157
158 DBG2("JUL listing events for app pid: %d and socket %d", app->pid,
159 app->sock->fd);
160
161 ret = send_header(app->sock, 0, JUL_CMD_LIST, 0);
162 if (ret < 0) {
163 goto error_io;
164 }
165
166 /* Get list header so we know how much we'll receive. */
167 ret = recv_reply(app->sock, &reply_hdr, sizeof(reply_hdr));
168 if (ret < 0) {
169 goto error_io;
170 }
171
172 switch (be32toh(reply_hdr.ret_code)) {
173 case JUL_RET_CODE_SUCCESS:
174 data_size = be32toh(reply_hdr.data_size) + sizeof(*reply);
175 break;
176 default:
177 ERR("Java agent returned an unknown code: %" PRIu32,
178 be32toh(reply_hdr.ret_code));
179 ret = LTTNG_ERR_FATAL;
180 goto error;
181 }
182
183 reply = zmalloc(data_size);
184 if (!reply) {
185 ret = LTTNG_ERR_NOMEM;
186 goto error;
187 }
188
189 /* Get the list with the appropriate data size. */
190 ret = recv_reply(app->sock, reply, data_size);
191 if (ret < 0) {
192 goto error_io;
193 }
194
195 nb_event = be32toh(reply->nb_event);
196 tmp_events = zmalloc(sizeof(*tmp_events) * nb_event);
197 if (!tmp_events) {
198 ret = LTTNG_ERR_NOMEM;
199 goto error;
200 }
201
202 for (i = 0; i < nb_event; i++) {
203 offset += len;
204 strncpy(tmp_events[i].name, reply->payload + offset,
205 sizeof(tmp_events[i].name));
206 tmp_events[i].pid = app->pid;
207 tmp_events[i].enabled = -1;
208 len = strlen(reply->payload + offset) + 1;
209 }
210
211 *events = tmp_events;
212
213 free(reply);
214 return nb_event;
215
216 error_io:
217 ret = LTTNG_ERR_UST_LIST_FAIL;
218 error:
219 free(reply);
220 free(tmp_events);
221 return -ret;
222
223 }
224
225 /*
226 * Internal enable JUL event on a JUL application. This function
227 * communicates with the Java agent to enable a given event (Logger name).
228 *
229 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
230 */
231 static int enable_event(struct jul_app *app, struct jul_event *event)
232 {
233 int ret;
234 uint64_t data_size;
235 struct lttcomm_jul_enable msg;
236 struct lttcomm_jul_generic_reply reply;
237
238 assert(app);
239 assert(app->sock);
240 assert(event);
241
242 DBG2("JUL enabling event %s for app pid: %d and socket %d", event->name,
243 app->pid, app->sock->fd);
244
245 data_size = sizeof(msg);
246
247 ret = send_header(app->sock, data_size, JUL_CMD_ENABLE, 0);
248 if (ret < 0) {
249 goto error_io;
250 }
251
252 memset(&msg, 0, sizeof(msg));
253 msg.loglevel = event->loglevel;
254 msg.loglevel_type = event->loglevel_type;
255 strncpy(msg.name, event->name, sizeof(msg.name));
256 ret = send_payload(app->sock, &msg, sizeof(msg));
257 if (ret < 0) {
258 goto error_io;
259 }
260
261 ret = recv_reply(app->sock, &reply, sizeof(reply));
262 if (ret < 0) {
263 goto error_io;
264 }
265
266 switch (be32toh(reply.ret_code)) {
267 case JUL_RET_CODE_SUCCESS:
268 break;
269 case JUL_RET_CODE_UNKNOWN_NAME:
270 ret = LTTNG_ERR_UST_EVENT_NOT_FOUND;
271 goto error;
272 default:
273 ERR("Java agent returned an unknown code: %" PRIu32,
274 be32toh(reply.ret_code));
275 ret = LTTNG_ERR_FATAL;
276 goto error;
277 }
278
279 return LTTNG_OK;
280
281 error_io:
282 ret = LTTNG_ERR_UST_ENABLE_FAIL;
283 error:
284 return ret;
285 }
286
287 /*
288 * Internal disable JUL event call on a JUL application. This function
289 * communicates with the Java agent to disable a given event (Logger name).
290 *
291 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
292 */
293 static int disable_event(struct jul_app *app, struct jul_event *event)
294 {
295 int ret;
296 uint64_t data_size;
297 struct lttcomm_jul_disable msg;
298 struct lttcomm_jul_generic_reply reply;
299
300 assert(app);
301 assert(app->sock);
302 assert(event);
303
304 DBG2("JUL disabling event %s for app pid: %d and socket %d", event->name,
305 app->pid, app->sock->fd);
306
307 data_size = sizeof(msg);
308
309 ret = send_header(app->sock, data_size, JUL_CMD_DISABLE, 0);
310 if (ret < 0) {
311 goto error_io;
312 }
313
314 memset(&msg, 0, sizeof(msg));
315 strncpy(msg.name, event->name, sizeof(msg.name));
316 ret = send_payload(app->sock, &msg, sizeof(msg));
317 if (ret < 0) {
318 goto error_io;
319 }
320
321 ret = recv_reply(app->sock, &reply, sizeof(reply));
322 if (ret < 0) {
323 goto error_io;
324 }
325
326 switch (be32toh(reply.ret_code)) {
327 case JUL_RET_CODE_SUCCESS:
328 break;
329 case JUL_RET_CODE_UNKNOWN_NAME:
330 ret = LTTNG_ERR_UST_EVENT_NOT_FOUND;
331 goto error;
332 default:
333 ERR("Java agent returned an unknown code: %" PRIu32,
334 be32toh(reply.ret_code));
335 ret = LTTNG_ERR_FATAL;
336 goto error;
337 }
338
339 return LTTNG_OK;
340
341 error_io:
342 ret = LTTNG_ERR_UST_DISABLE_FAIL;
343 error:
344 return ret;
345 }
346
347 /*
348 * Enable JUL event on every JUL applications registered with the session
349 * daemon.
350 *
351 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
352 */
353 int jul_enable_event(struct jul_event *event)
354 {
355 int ret;
356 struct jul_app *app;
357 struct lttng_ht_iter iter;
358
359 assert(event);
360
361 rcu_read_lock();
362
363 cds_lfht_for_each_entry(jul_apps_ht_by_sock->ht, &iter.iter, app,
364 node.node) {
365 /* Enable event on JUL application through TCP socket. */
366 ret = enable_event(app, event);
367 if (ret != LTTNG_OK) {
368 goto error;
369 }
370 }
371
372 event->enabled = 1;
373 ret = LTTNG_OK;
374
375 error:
376 rcu_read_unlock();
377 return ret;
378 }
379
380 /*
381 * Disable JUL event on every JUL applications registered with the session
382 * daemon.
383 *
384 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
385 */
386 int jul_disable_event(struct jul_event *event)
387 {
388 int ret;
389 struct jul_app *app;
390 struct lttng_ht_iter iter;
391
392 assert(event);
393
394 rcu_read_lock();
395
396 cds_lfht_for_each_entry(jul_apps_ht_by_sock->ht, &iter.iter, app,
397 node.node) {
398 /* Enable event on JUL application through TCP socket. */
399 ret = disable_event(app, event);
400 if (ret != LTTNG_OK) {
401 goto error;
402 }
403 }
404
405 event->enabled = 0;
406 ret = LTTNG_OK;
407
408 error:
409 rcu_read_unlock();
410 return ret;
411 }
412
413 /*
414 * Ask every java agent for the list of possible event (logger name). Events is
415 * allocated with the events of every JUL application.
416 *
417 * Return the number of events or else a negative value.
418 */
419 int jul_list_events(struct lttng_event **events)
420 {
421 int ret;
422 size_t nbmem, count = 0;
423 struct jul_app *app;
424 struct lttng_event *tmp_events = NULL;
425 struct lttng_ht_iter iter;
426
427 assert(events);
428
429 nbmem = UST_APP_EVENT_LIST_SIZE;
430 tmp_events = zmalloc(nbmem * sizeof(*tmp_events));
431 if (!tmp_events) {
432 PERROR("zmalloc jul list events");
433 ret = -ENOMEM;
434 goto error;
435 }
436
437 rcu_read_lock();
438 cds_lfht_for_each_entry(jul_apps_ht_by_sock->ht, &iter.iter, app,
439 node.node) {
440 ssize_t nb_ev;
441 struct lttng_event *jul_events;
442
443 nb_ev = list_events(app, &jul_events);
444 if (nb_ev < 0) {
445 ret = nb_ev;
446 goto error_unlock;
447 }
448
449 if (count + nb_ev > nbmem) {
450 /* In case the realloc fails, we free the memory */
451 struct lttng_event *new_tmp_events;
452 size_t new_nbmem;
453
454 new_nbmem = max_t(size_t, count + nb_ev, nbmem << 1);
455 DBG2("Reallocating JUL event list from %zu to %zu entries",
456 nbmem, new_nbmem);
457 new_tmp_events = realloc(tmp_events,
458 new_nbmem * sizeof(*new_tmp_events));
459 if (!new_tmp_events) {
460 PERROR("realloc JUL events");
461 ret = -ENOMEM;
462 free(jul_events);
463 goto error_unlock;
464 }
465 /* Zero the new memory */
466 memset(new_tmp_events + nbmem, 0,
467 (new_nbmem - nbmem) * sizeof(*new_tmp_events));
468 nbmem = new_nbmem;
469 tmp_events = new_tmp_events;
470 }
471 memcpy(tmp_events + count, jul_events,
472 nb_ev * sizeof(*tmp_events));
473 free(jul_events);
474 count += nb_ev;
475 }
476 rcu_read_unlock();
477
478 ret = count;
479 *events = tmp_events;
480 return ret;
481
482 error_unlock:
483 rcu_read_unlock();
484 error:
485 free(tmp_events);
486 return ret;
487 }
488
489 /*
490 * Create a JUL app object using the given PID.
491 *
492 * Return newly allocated object or else NULL on error.
493 */
494 struct jul_app *jul_create_app(pid_t pid, struct lttcomm_sock *sock)
495 {
496 struct jul_app *app;
497
498 assert(sock);
499
500 app = zmalloc(sizeof(*app));
501 if (!app) {
502 PERROR("zmalloc JUL create");
503 goto error;
504 }
505
506 app->pid = pid;
507 app->sock = sock;
508 lttng_ht_node_init_ulong(&app->node, (unsigned long) app->sock->fd);
509
510 error:
511 return app;
512 }
513
514 /*
515 * Lookup JUL app by socket in the global hash table.
516 *
517 * RCU read side lock MUST be acquired.
518 *
519 * Return object if found else NULL.
520 */
521 struct jul_app *jul_find_app_by_sock(int sock)
522 {
523 struct lttng_ht_node_ulong *node;
524 struct lttng_ht_iter iter;
525 struct jul_app *app;
526
527 assert(sock >= 0);
528
529 lttng_ht_lookup(jul_apps_ht_by_sock, (void *)((unsigned long) sock), &iter);
530 node = lttng_ht_iter_get_node_ulong(&iter);
531 if (node == NULL) {
532 goto error;
533 }
534 app = caa_container_of(node, struct jul_app, node);
535
536 DBG3("JUL app pid %d found by sock %d.", app->pid, sock);
537 return app;
538
539 error:
540 DBG3("JUL app NOT found by sock %d.", sock);
541 return NULL;
542 }
543
544 /*
545 * Add JUL application object to a given hash table.
546 */
547 void jul_add_app(struct jul_app *app)
548 {
549 assert(app);
550
551 DBG3("JUL adding app sock: %d and pid: %d to ht", app->sock->fd, app->pid);
552
553 rcu_read_lock();
554 lttng_ht_add_unique_ulong(jul_apps_ht_by_sock, &app->node);
555 rcu_read_unlock();
556 }
557
558 /*
559 * Delete JUL application from the global hash table.
560 */
561 void jul_delete_app(struct jul_app *app)
562 {
563 int ret;
564 struct lttng_ht_iter iter;
565
566 assert(app);
567
568 DBG3("JUL deleting app pid: %d and sock: %d", app->pid, app->sock->fd);
569
570 iter.iter.node = &app->node.node;
571 rcu_read_lock();
572 ret = lttng_ht_del(jul_apps_ht_by_sock, &iter);
573 rcu_read_unlock();
574 assert(!ret);
575 }
576
577 /*
578 * Destroy a JUL application object by detaching it from its corresponding UST
579 * app if one is connected by closing the socket. Finally, perform a
580 * delayed memory reclaim.
581 */
582 void jul_destroy_app(struct jul_app *app)
583 {
584 assert(app);
585
586 if (app->sock) {
587 app->sock->ops->close(app->sock);
588 lttcomm_destroy_sock(app->sock);
589 }
590
591 call_rcu(&app->node.head, destroy_app_jul_rcu);
592 }
593
594 /*
595 * Initialize an already allocated JUL domain object.
596 *
597 * Return 0 on success or else a negative errno value.
598 */
599 int jul_init_domain(struct jul_domain *dom)
600 {
601 int ret;
602
603 assert(dom);
604
605 dom->events = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
606 if (!dom->events) {
607 ret = -ENOMEM;
608 goto error;
609 }
610
611 return 0;
612
613 error:
614 return ret;
615 }
616
617 /*
618 * Create a newly allocated JUL event data structure. If name is valid, it's
619 * copied into the created event.
620 *
621 * Return a new object else NULL on error.
622 */
623 struct jul_event *jul_create_event(const char *name)
624 {
625 struct jul_event *event;
626
627 DBG3("JUL create new event with name %s", name);
628
629 event = zmalloc(sizeof(*event));
630 if (!event) {
631 goto error;
632 }
633
634 if (name) {
635 strncpy(event->name, name, sizeof(event->name));
636 event->name[sizeof(event->name) - 1] = '\0';
637 lttng_ht_node_init_str(&event->node, event->name);
638 }
639
640 error:
641 return event;
642 }
643
644 /*
645 * Unique add of a JUL event to a given domain.
646 */
647 void jul_add_event(struct jul_event *event, struct jul_domain *dom)
648 {
649 assert(event);
650 assert(dom);
651 assert(dom->events);
652
653 DBG3("JUL adding event %s to domain", event->name);
654
655 rcu_read_lock();
656 lttng_ht_add_unique_str(dom->events, &event->node);
657 rcu_read_unlock();
658 dom->being_used = 1;
659 }
660
661 /*
662 * Find a JUL event in the given domain using name.
663 *
664 * RCU read side lock MUST be acquired.
665 *
666 * Return object if found else NULL.
667 */
668 struct jul_event *jul_find_by_name(const char *name, struct jul_domain *dom)
669 {
670 struct lttng_ht_node_str *node;
671 struct lttng_ht_iter iter;
672
673 assert(name);
674 assert(dom);
675 assert(dom->events);
676
677 lttng_ht_lookup(dom->events, (void *)name, &iter);
678 node = lttng_ht_iter_get_node_str(&iter);
679 if (node == NULL) {
680 goto error;
681 }
682
683 DBG3("JUL found by name %s in domain.", name);
684 return caa_container_of(node, struct jul_event, node);
685
686 error:
687 DBG3("JUL NOT found by name %s in domain.", name);
688 return NULL;
689 }
690
691 /*
692 * Free given JUL event. This event must not be globally visible at this
693 * point (only expected to be used on failure just after event
694 * creation). After this call, the pointer is not usable anymore.
695 */
696 void jul_destroy_event(struct jul_event *event)
697 {
698 assert(event);
699
700 free(event);
701 }
702
703 /*
704 * Destroy a JUL domain completely. Note that the given pointer is NOT freed
705 * thus a reference to static or stack data can be passed to this function.
706 */
707 void jul_destroy_domain(struct jul_domain *dom)
708 {
709 struct lttng_ht_node_str *node;
710 struct lttng_ht_iter iter;
711
712 assert(dom);
713
714 DBG3("JUL destroy domain");
715
716 /*
717 * Just ignore if no events hash table exists. This is possible if for
718 * instance a JUL domain object was allocated but not initialized.
719 */
720 if (!dom->events) {
721 return;
722 }
723
724 rcu_read_lock();
725 cds_lfht_for_each_entry(dom->events->ht, &iter.iter, node, node) {
726 int ret;
727
728 ret = lttng_ht_del(dom->events, &iter);
729 assert(!ret);
730 call_rcu(&node->head, destroy_event_jul_rcu);
731 }
732 rcu_read_unlock();
733
734 lttng_ht_destroy(dom->events);
735 }
736
737 /*
738 * Initialize JUL subsystem.
739 */
740 int jul_init(void)
741 {
742 jul_apps_ht_by_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
743 if (!jul_apps_ht_by_sock) {
744 return -1;
745 }
746
747 return 0;
748 }
749
750 /*
751 * Update a JUL application (given socket) using the given domain.
752 *
753 * Note that this function is most likely to be used with a tracing session
754 * thus the caller should make sure to hold the appropriate lock(s).
755 */
756 void jul_update(struct jul_domain *domain, int sock)
757 {
758 int ret;
759 struct jul_app *app;
760 struct jul_event *event;
761 struct lttng_ht_iter iter;
762
763 assert(domain);
764 assert(sock >= 0);
765
766 DBG("JUL updating app socket %d", sock);
767
768 rcu_read_lock();
769 cds_lfht_for_each_entry(domain->events->ht, &iter.iter, event, node.node) {
770 /* Skip event if disabled. */
771 if (!event->enabled) {
772 continue;
773 }
774
775 app = jul_find_app_by_sock(sock);
776 /*
777 * We are in the registration path thus if the application is gone,
778 * there is a serious code flow error.
779 */
780 assert(app);
781
782 ret = enable_event(app, event);
783 if (ret != LTTNG_OK) {
784 DBG2("JUL update unable to enable event %s on app pid: %d sock %d",
785 event->name, app->pid, app->sock->fd);
786 /* Let's try the others here and don't assume the app is dead. */
787 continue;
788 }
789 }
790 rcu_read_unlock();
791 }
This page took 0.04521 seconds and 4 git commands to generate.