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