2 * Copyright (C) 2013 - David Goulet <dgoulet@efficios.com>
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.
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
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.
21 #include <common/common.h>
22 #include <common/sessiond-comm/sessiond-comm.h>
23 #include <common/uri.h>
24 #include <common/utils.h>
26 #include <common/compat/endian.h>
29 #include "jul-thread.h"
30 #include "lttng-sessiond.h"
35 * Note that there is not port here. It's set after this URI is parsed so we
36 * can let the user define a custom one. However, localhost is ALWAYS the
37 * default listening address.
39 static const char *default_reg_uri
=
40 "tcp://" DEFAULT_NETWORK_VIEWER_BIND_ADDRESS
;
43 * Update JUL application using the given socket. This is done just after
44 * registration was successful.
46 * This is a quite heavy call in terms of locking since the session list lock
47 * AND session lock are acquired.
49 static void update_jul_app(int sock
)
51 struct ltt_session
*session
, *stmp
;
52 struct ltt_session_list
*list
;
54 list
= session_get_list();
58 cds_list_for_each_entry_safe(session
, stmp
, &list
->head
, list
) {
59 session_lock(session
);
60 if (session
->ust_session
) {
61 jul_update(&session
->ust_session
->domain_jul
, sock
);
63 session_unlock(session
);
65 session_unlock_list();
69 * Destroy a JUL application by socket.
71 static void destroy_jul_app(int sock
)
78 * Not finding an application is a very important error that should NEVER
79 * happen. The hash table deletion is ONLY done through this call even on
83 app
= jul_find_app_by_sock(sock
);
87 /* RCU read side lock is taken in this function call. */
90 /* The application is freed in a RCU call but the socket is closed here. */
95 * Cleanup remaining JUL apps in the hash table. This should only be called in
96 * the exit path of the thread.
98 static void clean_jul_apps_ht(void)
100 struct lttng_ht_node_ulong
*node
;
101 struct lttng_ht_iter iter
;
103 DBG3("[jul-thread] Cleaning JUL apps ht");
106 cds_lfht_for_each_entry(jul_apps_ht_by_sock
->ht
, &iter
.iter
, node
, node
) {
109 app
= caa_container_of(node
, struct jul_app
, node
);
110 destroy_jul_app(app
->sock
->fd
);
116 * Create and init socket from uri.
118 static struct lttcomm_sock
*init_tcp_socket(void)
121 struct lttng_uri
*uri
= NULL
;
122 struct lttcomm_sock
*sock
= NULL
;
125 * This should never fail since the URI is hardcoded and the port is set
126 * before this thread is launched.
128 ret
= uri_parse(default_reg_uri
, &uri
);
130 assert(jul_tcp_port
);
131 uri
->port
= jul_tcp_port
;
133 sock
= lttcomm_alloc_sock_from_uri(uri
);
136 ERR("[jul-thread] JUL allocating TCP socket");
140 ret
= lttcomm_create_sock(sock
);
145 ret
= sock
->ops
->bind(sock
);
147 WARN("Another session daemon is using this JUL port. JUL support "
148 "will be deactivated to prevent interfering with the tracing.");
152 ret
= sock
->ops
->listen(sock
, -1);
157 DBG("[jul-thread] Listening on TCP port %u and socket %d", jul_tcp_port
,
164 lttcomm_destroy_sock(sock
);
170 * Close and destroy the given TCP socket.
172 static void destroy_tcp_socket(struct lttcomm_sock
*sock
)
176 DBG3("[jul-thread] Destroy TCP socket on port %u", jul_tcp_port
);
178 /* This will return gracefully if fd is invalid. */
179 sock
->ops
->close(sock
);
180 lttcomm_destroy_sock(sock
);
184 * Handle a new JUL registration using the reg socket. After that, a new JUL
185 * application is added to the global hash table and attach to an UST app
186 * object. If r_app is not NULL, the created app is set to the pointer.
188 * Return the new FD created upon accept() on success or else a negative errno
191 static int handle_registration(struct lttcomm_sock
*reg_sock
,
192 struct jul_app
**r_app
)
198 struct jul_register_msg msg
;
199 struct lttcomm_sock
*new_sock
;
203 new_sock
= reg_sock
->ops
->accept(reg_sock
);
209 size
= new_sock
->ops
->recvmsg(new_sock
, &msg
, sizeof(msg
), 0);
210 if (size
< sizeof(msg
)) {
214 pid
= be32toh(msg
.pid
);
216 DBG2("[jul-thread] New registration for pid %d on socket %d", pid
,
219 app
= jul_create_app(pid
, new_sock
);
226 * Add before assigning the socket value to the UST app so it can be found
232 * We don't need to attach the JUL app to the app. If we ever do
233 * so, we should consider both registration order of JUL before
234 * app and app before JUL.
244 new_sock
->ops
->close(new_sock
);
245 lttcomm_destroy_sock(new_sock
);
251 * This thread manage application notify communication.
253 void *jul_thread_manage_registration(void *data
)
256 uint32_t revents
, nb_fd
;
257 struct lttng_poll_event events
;
258 struct lttcomm_sock
*reg_sock
;
260 DBG("[jul-thread] Manage JUL application registration.");
262 rcu_register_thread();
265 /* JUL initialization call MUST be called before starting the thread. */
266 assert(jul_apps_ht_by_sock
);
268 /* Create pollset with size 2, quit pipe and socket. */
269 ret
= sessiond_set_thread_pollset(&events
, 2);
271 goto error_poll_create
;
274 reg_sock
= init_tcp_socket();
276 goto error_tcp_socket
;
279 /* Add create valid TCP socket to poll set. */
280 ret
= lttng_poll_add(&events
, reg_sock
->fd
,
281 LPOLLIN
| LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
287 DBG3("[jul-thread] Manage JUL polling on %d fds",
288 LTTNG_POLL_GETNB(&events
));
290 /* Inifinite blocking call, waiting for transmission */
292 ret
= lttng_poll_wait(&events
, -1);
295 * Restart interrupted system call.
297 if (errno
== EINTR
) {
303 DBG3("[jul-thread] %d fd ready", nb_fd
);
305 for (i
= 0; i
< nb_fd
; i
++) {
306 /* Fetch once the poll data */
307 revents
= LTTNG_POLL_GETEV(&events
, i
);
308 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
310 /* Thread quit pipe has been closed. Killing thread. */
311 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
317 * Check first if this is a POLLERR since POLLIN is also included
318 * in an error value thus checking first.
320 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
321 /* Removing from the poll set */
322 ret
= lttng_poll_del(&events
, pollfd
);
327 destroy_jul_app(pollfd
);
328 } else if (revents
& (LPOLLIN
)) {
330 struct jul_app
*app
= NULL
;
332 /* Pollin event of JUL app socket should NEVER happen. */
333 assert(pollfd
== reg_sock
->fd
);
335 new_fd
= handle_registration(reg_sock
, &app
);
337 WARN("[jul-thread] JUL registration failed. Ignoring.");
338 /* Somehow the communication failed. Just continue. */
341 /* Should not have a NULL app on success. */
344 /* Only add poll error event to only detect shutdown. */
345 ret
= lttng_poll_add(&events
, new_fd
,
346 LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
348 destroy_jul_app(new_fd
);
352 /* Update newly registered app. */
353 update_jul_app(new_fd
);
355 /* On failure, the poll will detect it and clean it up. */
356 (void) jul_send_registration_done(app
);
358 ERR("Unknown poll events %u for sock %d", revents
, pollfd
);
365 /* Whatever happens, try to delete it and exit. */
366 (void) lttng_poll_del(&events
, reg_sock
->fd
);
368 destroy_tcp_socket(reg_sock
);
370 lttng_poll_clean(&events
);
372 DBG("[jul-thread] is cleaning up and stopping.");
374 if (jul_apps_ht_by_sock
) {
376 lttng_ht_destroy(jul_apps_ht_by_sock
);
379 rcu_thread_offline();
380 rcu_unregister_thread();