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