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>
27 #include "jul-thread.h"
28 #include "lttng-sessiond.h"
33 * Note that there is not port here. It's set after this URI is parsed so we
34 * can let the user define a custom one. However, localhost is ALWAYS the
35 * default listening address.
37 static const char *default_reg_uri
=
38 "tcp://" DEFAULT_NETWORK_VIEWER_BIND_ADDRESS
;
41 * Update JUL application using the given socket. This is done just after
42 * registration was successful.
44 * This is a quite heavy call in terms of locking since the session list lock
45 * AND session lock are acquired.
47 static void update_jul_app(int sock
)
49 struct ltt_session
*session
, *stmp
;
50 struct ltt_session_list
*list
;
52 list
= session_get_list();
56 cds_list_for_each_entry_safe(session
, stmp
, &list
->head
, list
) {
57 session_lock(session
);
58 if (session
->ust_session
) {
59 jul_update(&session
->ust_session
->domain_jul
, sock
);
61 session_unlock(session
);
63 session_unlock_list();
67 * Destroy a JUL application by socket.
69 static void destroy_jul_app(int sock
)
76 * Not finding an application is a very important error that should NEVER
77 * happen. The hash table deletion is ONLY done through this call even on
81 app
= jul_find_app_by_sock(sock
);
85 /* RCU read side lock is taken in this function call. */
88 /* The application is freed in a RCU call but the socket is closed here. */
93 * Cleanup remaining JUL apps in the hash table. This should only be called in
94 * the exit path of the thread.
96 static void clean_jul_apps_ht(void)
98 struct lttng_ht_node_ulong
*node
;
99 struct lttng_ht_iter iter
;
101 DBG3("[jul-thread] Cleaning JUL apps ht");
104 cds_lfht_for_each_entry(jul_apps_ht_by_sock
->ht
, &iter
.iter
, node
, node
) {
107 app
= caa_container_of(node
, struct jul_app
, node
);
108 destroy_jul_app(app
->sock
->fd
);
114 * Create and init socket from uri.
116 static struct lttcomm_sock
*init_tcp_socket(void)
119 struct lttng_uri
*uri
= NULL
;
120 struct lttcomm_sock
*sock
= NULL
;
123 * This should never fail since the URI is hardcoded and the port is set
124 * before this thread is launched.
126 ret
= uri_parse(default_reg_uri
, &uri
);
128 assert(jul_tcp_port
);
129 uri
->port
= jul_tcp_port
;
131 sock
= lttcomm_alloc_sock_from_uri(uri
);
134 ERR("[jul-thread] JUL allocating TCP socket");
138 ret
= lttcomm_create_sock(sock
);
143 ret
= sock
->ops
->bind(sock
);
145 WARN("An other session daemon is using this JUL port. JUL support "
146 "will be deactivated not interfering with the tracing.");
150 ret
= sock
->ops
->listen(sock
, -1);
155 DBG("[jul-thread] Listening on TCP port %u and socket %d", jul_tcp_port
,
162 lttcomm_destroy_sock(sock
);
168 * Close and destroy the given TCP socket.
170 static void destroy_tcp_socket(struct lttcomm_sock
*sock
)
174 DBG3("[jul-thread] Destroy TCP socket on port %u", jul_tcp_port
);
176 /* This will return gracefully if fd is invalid. */
177 sock
->ops
->close(sock
);
178 lttcomm_destroy_sock(sock
);
182 * Handle a new JUL registration using the reg socket. After that, a new JUL
183 * application is added to the global hash table and attach to an UST app
184 * object. If r_app is not NULL, the created app is set to the pointer.
186 * Return the new FD created upon accept() on success or else a negative errno
189 static int handle_registration(struct lttcomm_sock
*reg_sock
,
190 struct jul_app
**r_app
)
196 struct jul_register_msg msg
;
197 struct lttcomm_sock
*new_sock
;
201 new_sock
= reg_sock
->ops
->accept(reg_sock
);
207 size
= new_sock
->ops
->recvmsg(new_sock
, &msg
, sizeof(msg
), 0);
208 if (size
< sizeof(msg
)) {
212 pid
= be32toh(msg
.pid
);
214 DBG2("[jul-thread] New registration for pid %d on socket %d", pid
,
217 app
= jul_create_app(pid
, new_sock
);
224 * Add before assigning the socket value to the UST app so it can be found
230 * We don't need to attach the JUL app to the app. If we ever do
231 * so, we should consider both registration order of JUL before
232 * app and app before JUL.
242 new_sock
->ops
->close(new_sock
);
243 lttcomm_destroy_sock(new_sock
);
249 * This thread manage application notify communication.
251 void *jul_thread_manage_registration(void *data
)
254 uint32_t revents
, nb_fd
;
255 struct lttng_poll_event events
;
256 struct lttcomm_sock
*reg_sock
;
258 DBG("[jul-thread] Manage JUL application registration.");
260 rcu_register_thread();
263 /* JUL initialization call MUST be called before starting the thread. */
264 assert(jul_apps_ht_by_sock
);
266 /* Create pollset with size 2, quit pipe and socket. */
267 ret
= sessiond_set_thread_pollset(&events
, 2);
269 goto error_poll_create
;
272 reg_sock
= init_tcp_socket();
274 goto error_tcp_socket
;
277 /* Add create valid TCP socket to poll set. */
278 ret
= lttng_poll_add(&events
, reg_sock
->fd
,
279 LPOLLIN
| LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
285 DBG3("[jul-thread] Manage JUL polling on %d fds",
286 LTTNG_POLL_GETNB(&events
));
288 /* Inifinite blocking call, waiting for transmission */
290 ret
= lttng_poll_wait(&events
, -1);
293 * Restart interrupted system call.
295 if (errno
== EINTR
) {
301 DBG3("[jul-thread] %d fd ready", nb_fd
);
303 for (i
= 0; i
< nb_fd
; i
++) {
304 /* Fetch once the poll data */
305 revents
= LTTNG_POLL_GETEV(&events
, i
);
306 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
308 /* Thread quit pipe has been closed. Killing thread. */
309 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
315 * Check first if this is a POLLERR since POLLIN is also included
316 * in an error value thus checking first.
318 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
319 /* Removing from the poll set */
320 ret
= lttng_poll_del(&events
, pollfd
);
325 destroy_jul_app(pollfd
);
326 } else if (revents
& (LPOLLIN
)) {
328 struct jul_app
*app
= NULL
;
330 /* Pollin event of JUL app socket should NEVER happen. */
331 assert(pollfd
== reg_sock
->fd
);
333 new_fd
= handle_registration(reg_sock
, &app
);
335 WARN("[jul-thread] JUL registration failed. Ignoring.");
336 /* Somehow the communication failed. Just continue. */
339 /* Should not have a NULL app on success. */
342 /* Only add poll error event to only detect shutdown. */
343 ret
= lttng_poll_add(&events
, new_fd
,
344 LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
346 destroy_jul_app(new_fd
);
350 /* Update newly registered app. */
351 update_jul_app(new_fd
);
353 /* On failure, the poll will detect it and clean it up. */
354 (void) jul_send_registration_done(app
);
356 ERR("Unknown poll events %u for sock %d", revents
, pollfd
);
363 /* Whatever happens, try to delete it and exit. */
364 (void) lttng_poll_del(&events
, reg_sock
->fd
);
366 destroy_tcp_socket(reg_sock
);
368 lttng_poll_clean(&events
);
370 DBG("[jul-thread] is cleaning up and stopping.");
372 if (jul_apps_ht_by_sock
) {
374 lttng_ht_destroy(jul_apps_ht_by_sock
);
377 rcu_thread_offline();
378 rcu_unregister_thread();