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 | /* | |
428de77a | 30 | * URCU delayed JUL event reclaim. |
0475c50c DG |
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 | 42 | /* |
428de77a | 43 | * URCU delayed JUL app reclaim. |
f20baf8e DG |
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 | /* | |
428de77a MD |
56 | * Communication with Java agent. Send the message header to the given |
57 | * socket in big endian. | |
f20baf8e DG |
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 | /* | |
428de77a | 138 | * Internal event listing for a given app. Populate events. |
3c6a091f DG |
139 | * |
140 | * Return number of element in the list or else a negative LTTNG_ERR* code. | |
428de77a MD |
141 | * On success, the caller is responsible for freeing the memory |
142 | * allocated for "events". | |
3c6a091f DG |
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 | ||
f20baf8e | 224 | /* |
428de77a | 225 | * Internal enable JUL event on a JUL application. This function |
f20baf8e DG |
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 | } | |
f20baf8e DG |
365 | } |
366 | ||
3c6a091f | 367 | event->enabled = 1; |
f20baf8e DG |
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 | } | |
f20baf8e DG |
398 | } |
399 | ||
3c6a091f | 400 | event->enabled = 0; |
f20baf8e DG |
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; | |
aae6255e | 419 | struct lttng_event *tmp_events = NULL; |
f20baf8e DG |
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; | |
428de77a | 441 | goto error_unlock; |
f20baf8e DG |
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"); | |
f20baf8e | 454 | ret = -ENOMEM; |
c36441a9 | 455 | free(jul_events); |
428de77a | 456 | goto error_unlock; |
f20baf8e DG |
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; | |
aae6255e | 469 | return ret; |
f20baf8e | 470 | |
428de77a MD |
471 | error_unlock: |
472 | rcu_read_unlock(); | |
f20baf8e | 473 | error: |
aae6255e | 474 | free(tmp_events); |
f20baf8e DG |
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; | |
f20baf8e DG |
497 | lttng_ht_node_init_ulong(&app->node, (unsigned long) app->sock->fd); |
498 | ||
499 | error: | |
500 | return app; | |
501 | } | |
502 | ||
503 | /* | |
504 | * Lookup JUL app by socket in the global hash table. | |
505 | * | |
506 | * RCU read side lock MUST be acquired. | |
507 | * | |
508 | * Return object if found else NULL. | |
509 | */ | |
510 | struct jul_app *jul_find_app_by_sock(int sock) | |
511 | { | |
512 | struct lttng_ht_node_ulong *node; | |
513 | struct lttng_ht_iter iter; | |
514 | struct jul_app *app; | |
515 | ||
516 | assert(sock >= 0); | |
517 | ||
518 | lttng_ht_lookup(jul_apps_ht_by_sock, (void *)((unsigned long) sock), &iter); | |
519 | node = lttng_ht_iter_get_node_ulong(&iter); | |
520 | if (node == NULL) { | |
521 | goto error; | |
522 | } | |
523 | app = caa_container_of(node, struct jul_app, node); | |
524 | ||
525 | DBG3("JUL app pid %d found by sock %d.", app->pid, sock); | |
526 | return app; | |
527 | ||
528 | error: | |
529 | DBG3("JUL app NOT found by sock %d.", sock); | |
530 | return NULL; | |
531 | } | |
532 | ||
533 | /* | |
534 | * Add JUL application object to a given hash table. | |
535 | */ | |
536 | void jul_add_app(struct jul_app *app) | |
537 | { | |
538 | assert(app); | |
539 | ||
540 | DBG3("JUL adding app sock: %d and pid: %d to ht", app->sock->fd, app->pid); | |
541 | ||
542 | rcu_read_lock(); | |
543 | lttng_ht_add_unique_ulong(jul_apps_ht_by_sock, &app->node); | |
544 | rcu_read_unlock(); | |
545 | } | |
546 | ||
f20baf8e DG |
547 | /* |
548 | * Delete JUL application from the global hash table. | |
549 | */ | |
550 | void jul_delete_app(struct jul_app *app) | |
551 | { | |
552 | int ret; | |
553 | struct lttng_ht_iter iter; | |
554 | ||
555 | assert(app); | |
556 | ||
557 | DBG3("JUL deleting app pid: %d and sock: %d", app->pid, app->sock->fd); | |
558 | ||
559 | iter.iter.node = &app->node.node; | |
560 | rcu_read_lock(); | |
561 | ret = lttng_ht_del(jul_apps_ht_by_sock, &iter); | |
562 | rcu_read_unlock(); | |
563 | assert(!ret); | |
564 | } | |
565 | ||
566 | /* | |
567 | * Destroy a JUL application object by detaching it from its corresponding UST | |
428de77a MD |
568 | * app if one is connected by closing the socket. Finally, perform a |
569 | * delayed memory reclaim. | |
f20baf8e DG |
570 | */ |
571 | void jul_destroy_app(struct jul_app *app) | |
572 | { | |
573 | assert(app); | |
574 | ||
575 | if (app->sock) { | |
576 | app->sock->ops->close(app->sock); | |
577 | lttcomm_destroy_sock(app->sock); | |
578 | } | |
579 | ||
580 | call_rcu(&app->node.head, destroy_app_jul_rcu); | |
581 | } | |
582 | ||
0475c50c DG |
583 | /* |
584 | * Initialize an already allocated JUL domain object. | |
585 | * | |
586 | * Return 0 on success or else a negative errno value. | |
587 | */ | |
588 | int jul_init_domain(struct jul_domain *dom) | |
589 | { | |
590 | int ret; | |
591 | ||
592 | assert(dom); | |
593 | ||
594 | dom->events = lttng_ht_new(0, LTTNG_HT_TYPE_STRING); | |
595 | if (!dom->events) { | |
596 | ret = -ENOMEM; | |
597 | goto error; | |
598 | } | |
599 | ||
600 | return 0; | |
601 | ||
602 | error: | |
603 | return ret; | |
604 | } | |
605 | ||
606 | /* | |
607 | * Create a newly allocated JUL event data structure. If name is valid, it's | |
608 | * copied into the created event. | |
609 | * | |
610 | * Return a new object else NULL on error. | |
611 | */ | |
612 | struct jul_event *jul_create_event(const char *name) | |
613 | { | |
614 | struct jul_event *event; | |
615 | ||
616 | DBG3("JUL create new event with name %s", name); | |
617 | ||
618 | event = zmalloc(sizeof(*event)); | |
619 | if (!event) { | |
620 | goto error; | |
621 | } | |
622 | ||
623 | if (name) { | |
624 | strncpy(event->name, name, sizeof(event->name)); | |
625 | event->name[sizeof(event->name) - 1] = '\0'; | |
f20baf8e | 626 | lttng_ht_node_init_str(&event->node, event->name); |
0475c50c DG |
627 | } |
628 | ||
629 | error: | |
630 | return event; | |
631 | } | |
632 | ||
633 | /* | |
634 | * Unique add of a JUL event to a given domain. | |
635 | */ | |
636 | void jul_add_event(struct jul_event *event, struct jul_domain *dom) | |
637 | { | |
638 | assert(event); | |
639 | assert(dom); | |
640 | assert(dom->events); | |
641 | ||
642 | DBG3("JUL adding event %s to domain", event->name); | |
643 | ||
f20baf8e | 644 | rcu_read_lock(); |
0475c50c | 645 | lttng_ht_add_unique_str(dom->events, &event->node); |
f20baf8e | 646 | rcu_read_unlock(); |
3c6a091f | 647 | dom->being_used = 1; |
0475c50c DG |
648 | } |
649 | ||
650 | /* | |
651 | * Find a JUL event in the given domain using name. | |
652 | * | |
653 | * RCU read side lock MUST be acquired. | |
654 | * | |
655 | * Return object if found else NULL. | |
656 | */ | |
657 | struct jul_event *jul_find_by_name(const char *name, struct jul_domain *dom) | |
658 | { | |
659 | struct lttng_ht_node_str *node; | |
660 | struct lttng_ht_iter iter; | |
661 | ||
662 | assert(name); | |
663 | assert(dom); | |
664 | assert(dom->events); | |
665 | ||
666 | lttng_ht_lookup(dom->events, (void *)name, &iter); | |
667 | node = lttng_ht_iter_get_node_str(&iter); | |
668 | if (node == NULL) { | |
669 | goto error; | |
670 | } | |
671 | ||
672 | DBG3("JUL found by name %s in domain.", name); | |
673 | return caa_container_of(node, struct jul_event, node); | |
674 | ||
675 | error: | |
676 | DBG3("JUL NOT found by name %s in domain.", name); | |
677 | return NULL; | |
678 | } | |
679 | ||
680 | /* | |
681 | * Delete JUL event from given domain. Events hash table MUST be initialized. | |
682 | */ | |
683 | void jul_delete_event(struct jul_event *event, struct jul_domain *dom) | |
684 | { | |
685 | int ret; | |
686 | struct lttng_ht_iter iter; | |
687 | ||
688 | assert(event); | |
689 | assert(dom); | |
690 | assert(dom->events); | |
691 | ||
692 | DBG3("JUL deleting event %s from domain", event->name); | |
693 | ||
694 | iter.iter.node = &event->node.node; | |
695 | rcu_read_lock(); | |
696 | ret = lttng_ht_del(dom->events, &iter); | |
697 | rcu_read_unlock(); | |
698 | assert(!ret); | |
699 | } | |
700 | ||
701 | /* | |
428de77a MD |
702 | * Free given JUL event. This event must not be globally visible at this |
703 | * point (only expected to be used on failure just after event | |
704 | * creation). After this call, the pointer is not usable anymore. | |
0475c50c DG |
705 | */ |
706 | void jul_destroy_event(struct jul_event *event) | |
707 | { | |
708 | assert(event); | |
709 | ||
710 | free(event); | |
711 | } | |
712 | ||
713 | /* | |
714 | * Destroy a JUL domain completely. Note that the given pointer is NOT freed | |
428de77a | 715 | * thus a reference to static or stack data can be passed to this function. |
0475c50c DG |
716 | */ |
717 | void jul_destroy_domain(struct jul_domain *dom) | |
718 | { | |
719 | struct lttng_ht_node_str *node; | |
720 | struct lttng_ht_iter iter; | |
721 | ||
722 | assert(dom); | |
723 | ||
724 | DBG3("JUL destroy domain"); | |
725 | ||
726 | /* | |
727 | * Just ignore if no events hash table exists. This is possible if for | |
728 | * instance a JUL domain object was allocated but not initialized. | |
729 | */ | |
730 | if (!dom->events) { | |
731 | return; | |
732 | } | |
733 | ||
734 | rcu_read_lock(); | |
735 | cds_lfht_for_each_entry(dom->events->ht, &iter.iter, node, node) { | |
736 | int ret; | |
737 | ||
738 | ret = lttng_ht_del(dom->events, &iter); | |
739 | assert(!ret); | |
740 | call_rcu(&node->head, destroy_event_jul_rcu); | |
741 | } | |
742 | rcu_read_unlock(); | |
743 | ||
f20baf8e DG |
744 | lttng_ht_destroy(dom->events); |
745 | } | |
746 | ||
747 | /* | |
748 | * Initialize JUL subsystem. | |
749 | */ | |
750 | int jul_init(void) | |
751 | { | |
752 | jul_apps_ht_by_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); | |
753 | if (!jul_apps_ht_by_sock) { | |
754 | return -1; | |
755 | } | |
756 | ||
757 | return 0; | |
758 | } | |
759 | ||
760 | /* | |
761 | * Update a JUL application (given socket) using the given domain. | |
762 | * | |
763 | * Note that this function is most likely to be used with a tracing session | |
764 | * thus the caller should make sure to hold the appropriate lock(s). | |
765 | */ | |
766 | void jul_update(struct jul_domain *domain, int sock) | |
767 | { | |
768 | int ret; | |
769 | struct jul_app *app; | |
770 | struct jul_event *event; | |
771 | struct lttng_ht_iter iter; | |
772 | ||
773 | assert(domain); | |
774 | assert(sock >= 0); | |
775 | ||
776 | DBG("JUL updating app socket %d", sock); | |
777 | ||
778 | rcu_read_lock(); | |
779 | cds_lfht_for_each_entry(domain->events->ht, &iter.iter, event, node.node) { | |
780 | /* Skip event if disabled. */ | |
781 | if (!event->enabled) { | |
782 | continue; | |
783 | } | |
784 | ||
785 | app = jul_find_app_by_sock(sock); | |
786 | /* | |
787 | * We are in the registration path thus if the application is gone, | |
788 | * there is a serious code flow error. | |
789 | */ | |
790 | assert(app); | |
791 | ||
792 | ret = enable_event(app, event); | |
793 | if (ret != LTTNG_OK) { | |
794 | DBG2("JUL update unable to enable event %s on app pid: %d sock %d", | |
795 | event->name, app->pid, app->sock->fd); | |
796 | /* Let's try the others here and don't assume the app is dead. */ | |
797 | continue; | |
798 | } | |
799 | } | |
800 | rcu_read_unlock(); | |
0475c50c | 801 | } |