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
= "tcp://localhost";
40 * Update JUL application using the given socket. This is done just after
41 * registration was successful.
43 * This is a quite heavy call in terms of locking since the session list lock
44 * AND session lock are acquired.
46 static void update_jul_app(int sock
)
48 struct ltt_session
*session
, *stmp
;
49 struct ltt_session_list
*list
;
51 list
= session_get_list();
55 cds_list_for_each_entry_safe(session
, stmp
, &list
->head
, list
) {
56 session_lock(session
);
57 if (session
->ust_session
) {
58 jul_update(&session
->ust_session
->domain_jul
, sock
);
60 session_unlock(session
);
62 session_unlock_list();
66 * Destroy a JUL application by socket.
68 static void destroy_jul_app(int sock
)
75 * Not finding an application is a very important error that should NEVER
76 * happen. The hash table deletion is ONLY done through this call even on
80 app
= jul_find_app_by_sock(sock
);
84 /* RCU read side lock is taken in this function call. */
87 /* The application is freed in a RCU call but the socket is closed here. */
92 * Cleanup remaining JUL apps in the hash table. This should only be called in
93 * the exit path of the thread.
95 static void clean_jul_apps_ht(void)
97 struct lttng_ht_node_ulong
*node
;
98 struct lttng_ht_iter iter
;
100 DBG3("[jul-thread] Cleaning JUL apps ht");
103 cds_lfht_for_each_entry(jul_apps_ht_by_sock
->ht
, &iter
.iter
, node
, node
) {
106 app
= caa_container_of(node
, struct jul_app
, node
);
107 destroy_jul_app(app
->sock
->fd
);
113 * Create and init socket from uri.
115 static struct lttcomm_sock
*init_tcp_socket(void)
118 struct lttng_uri
*uri
= NULL
;
119 struct lttcomm_sock
*sock
= NULL
;
122 * This should never fail since the URI is hardcoded and the port is set
123 * before this thread is launched.
125 ret
= uri_parse(default_reg_uri
, &uri
);
127 assert(jul_tcp_port
);
128 uri
->port
= jul_tcp_port
;
130 sock
= lttcomm_alloc_sock_from_uri(uri
);
133 ERR("[jul-thread] JUL allocating TCP socket");
137 ret
= lttcomm_create_sock(sock
);
142 ret
= sock
->ops
->bind(sock
);
144 WARN("An other session daemon is using this JUL port. JUL support "
145 "will be deactivated not interfering with the tracing.");
149 ret
= sock
->ops
->listen(sock
, -1);
154 DBG("[jul-thread] Listening on TCP port %u and socket %d", jul_tcp_port
,
161 lttcomm_destroy_sock(sock
);
167 * Close and destroy the given TCP socket.
169 static void destroy_tcp_socket(struct lttcomm_sock
*sock
)
173 DBG3("[jul-thread] Destroy TCP socket on port %u", jul_tcp_port
);
175 /* This will return gracefully if fd is invalid. */
176 sock
->ops
->close(sock
);
177 lttcomm_destroy_sock(sock
);
181 * Handle a new JUL registration using the reg socket. After that, a new JUL
182 * application is added to the global hash table and attach to an UST app
183 * object. If r_app is not NULL, the created app is set to the pointer.
185 * Return the new FD created upon accept() on success or else a negative errno
188 static int handle_registration(struct lttcomm_sock
*reg_sock
,
189 struct jul_app
**r_app
)
195 struct jul_register_msg msg
;
196 struct lttcomm_sock
*new_sock
;
200 new_sock
= reg_sock
->ops
->accept(reg_sock
);
206 size
= new_sock
->ops
->recvmsg(new_sock
, &msg
, sizeof(msg
), 0);
207 if (size
< sizeof(msg
)) {
211 pid
= be32toh(msg
.pid
);
213 DBG2("[jul-thread] New registration for pid %d on socket %d", pid
,
216 app
= jul_create_app(pid
, new_sock
);
223 * Add before assigning the socket value to the UST app so it can be found
229 * We don't need to attach the JUL app to the app. If we ever do
230 * so, we should consider both registration order of JUL before
231 * app and app before JUL.
241 new_sock
->ops
->close(new_sock
);
242 lttcomm_destroy_sock(new_sock
);
248 * This thread manage application notify communication.
250 void *jul_thread_manage_registration(void *data
)
253 uint32_t revents
, nb_fd
;
254 struct lttng_poll_event events
;
255 struct lttcomm_sock
*reg_sock
;
257 DBG("[jul-thread] Manage JUL application registration.");
259 rcu_register_thread();
262 /* JUL initialization call MUST be called before starting the thread. */
263 assert(jul_apps_ht_by_sock
);
265 /* Create pollset with size 2, quit pipe and socket. */
266 ret
= sessiond_set_thread_pollset(&events
, 2);
268 goto error_poll_create
;
271 reg_sock
= init_tcp_socket();
273 goto error_tcp_socket
;
276 /* Add create valid TCP socket to poll set. */
277 ret
= lttng_poll_add(&events
, reg_sock
->fd
,
278 LPOLLIN
| LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
284 DBG3("[jul-thread] Manage JUL polling on %d fds",
285 LTTNG_POLL_GETNB(&events
));
287 /* Inifinite blocking call, waiting for transmission */
289 ret
= lttng_poll_wait(&events
, -1);
292 * Restart interrupted system call.
294 if (errno
== EINTR
) {
300 DBG3("[jul-thread] %d fd ready", nb_fd
);
302 for (i
= 0; i
< nb_fd
; i
++) {
303 /* Fetch once the poll data */
304 revents
= LTTNG_POLL_GETEV(&events
, i
);
305 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
307 /* Thread quit pipe has been closed. Killing thread. */
308 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
314 * Check first if this is a POLLERR since POLLIN is also included
315 * in an error value thus checking first.
317 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
318 /* Removing from the poll set */
319 ret
= lttng_poll_del(&events
, pollfd
);
324 destroy_jul_app(pollfd
);
325 } else if (revents
& (LPOLLIN
)) {
327 struct jul_app
*app
= NULL
;
329 /* Pollin event of JUL app socket should NEVER happen. */
330 assert(pollfd
== reg_sock
->fd
);
332 new_fd
= handle_registration(reg_sock
, &app
);
334 WARN("[jul-thread] JUL registration failed. Ignoring.");
335 /* Somehow the communication failed. Just continue. */
338 /* Should not have a NULL app on success. */
341 /* Only add poll error event to only detect shutdown. */
342 ret
= lttng_poll_add(&events
, new_fd
,
343 LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
345 destroy_jul_app(new_fd
);
349 /* Update newly registered app. */
350 update_jul_app(new_fd
);
352 /* On failure, the poll will detect it and clean it up. */
353 (void) jul_send_registration_done(app
);
355 ERR("Unknown poll events %u for sock %d", revents
, pollfd
);
362 /* Whatever happens, try to delete it and exit. */
363 (void) lttng_poll_del(&events
, reg_sock
->fd
);
365 destroy_tcp_socket(reg_sock
);
367 lttng_poll_clean(&events
);
369 DBG("[jul-thread] is cleaning up and stopping.");
371 if (jul_apps_ht_by_sock
) {
373 lttng_ht_destroy(jul_apps_ht_by_sock
);
376 rcu_thread_offline();
377 rcu_unregister_thread();
This page took 0.038567 seconds and 4 git commands to generate.