Fix: poll: show the correct number of fds
[lttng-tools.git] / src / bin / lttng-sessiond / jul-thread.c
CommitLineData
4d076222
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>
20
21#include <common/common.h>
22#include <common/sessiond-comm/sessiond-comm.h>
23#include <common/uri.h>
24#include <common/utils.h>
25
26#include "fd-limit.h"
27#include "jul-thread.h"
28#include "lttng-sessiond.h"
f20baf8e
DG
29#include "session.h"
30#include "utils.h"
4d076222
DG
31
32/*
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.
36 */
d359cebc
MD
37static const char *default_reg_uri =
38 "tcp://" DEFAULT_NETWORK_VIEWER_BIND_ADDRESS;
4d076222 39
f20baf8e
DG
40/*
41 * Update JUL application using the given socket. This is done just after
42 * registration was successful.
43 *
44 * This is a quite heavy call in terms of locking since the session list lock
45 * AND session lock are acquired.
46 */
47static void update_jul_app(int sock)
48{
49 struct ltt_session *session, *stmp;
50 struct ltt_session_list *list;
51
52 list = session_get_list();
53 assert(list);
54
55 session_lock_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);
60 }
61 session_unlock(session);
62 }
63 session_unlock_list();
64}
65
66/*
67 * Destroy a JUL application by socket.
68 */
69static void destroy_jul_app(int sock)
70{
71 struct jul_app *app;
72
73 assert(sock >= 0);
74
75 /*
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
78 * thread cleanup.
79 */
80 rcu_read_lock();
81 app = jul_find_app_by_sock(sock);
82 assert(app);
83 rcu_read_unlock();
84
85 /* RCU read side lock is taken in this function call. */
86 jul_delete_app(app);
87
88 /* The application is freed in a RCU call but the socket is closed here. */
89 jul_destroy_app(app);
90}
91
92/*
93 * Cleanup remaining JUL apps in the hash table. This should only be called in
94 * the exit path of the thread.
95 */
96static void clean_jul_apps_ht(void)
97{
98 struct lttng_ht_node_ulong *node;
99 struct lttng_ht_iter iter;
100
101 DBG3("[jul-thread] Cleaning JUL apps ht");
102
103 rcu_read_lock();
104 cds_lfht_for_each_entry(jul_apps_ht_by_sock->ht, &iter.iter, node, node) {
105 struct jul_app *app;
106
107 app = caa_container_of(node, struct jul_app, node);
108 destroy_jul_app(app->sock->fd);
109 }
110 rcu_read_unlock();
111}
112
4d076222
DG
113/*
114 * Create and init socket from uri.
115 */
116static struct lttcomm_sock *init_tcp_socket(void)
117{
118 int ret;
119 struct lttng_uri *uri = NULL;
120 struct lttcomm_sock *sock = NULL;
121
122 /*
123 * This should never fail since the URI is hardcoded and the port is set
124 * before this thread is launched.
125 */
126 ret = uri_parse(default_reg_uri, &uri);
127 assert(ret);
128 assert(jul_tcp_port);
129 uri->port = jul_tcp_port;
130
131 sock = lttcomm_alloc_sock_from_uri(uri);
132 uri_free(uri);
133 if (sock == NULL) {
134 ERR("[jul-thread] JUL allocating TCP socket");
135 goto error;
136 }
137
138 ret = lttcomm_create_sock(sock);
139 if (ret < 0) {
140 goto error;
141 }
142
143 ret = sock->ops->bind(sock);
144 if (ret < 0) {
0cafaca7
DG
145 WARN("An other session daemon is using this JUL port. JUL support "
146 "will be deactivated not interfering with the tracing.");
4d076222
DG
147 goto error;
148 }
149
150 ret = sock->ops->listen(sock, -1);
151 if (ret < 0) {
152 goto error;
153 }
154
155 DBG("[jul-thread] Listening on TCP port %u and socket %d", jul_tcp_port,
156 sock->fd);
157
158 return sock;
159
160error:
161 if (sock) {
162 lttcomm_destroy_sock(sock);
163 }
164 return NULL;
165}
166
167/*
168 * Close and destroy the given TCP socket.
169 */
170static void destroy_tcp_socket(struct lttcomm_sock *sock)
171{
172 assert(sock);
173
174 DBG3("[jul-thread] Destroy TCP socket on port %u", jul_tcp_port);
175
176 /* This will return gracefully if fd is invalid. */
177 sock->ops->close(sock);
178 lttcomm_destroy_sock(sock);
179}
180
f20baf8e
DG
181/*
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
cf9787ee 184 * object. If r_app is not NULL, the created app is set to the pointer.
f20baf8e
DG
185 *
186 * Return the new FD created upon accept() on success or else a negative errno
187 * value.
188 */
cf9787ee
DG
189static int handle_registration(struct lttcomm_sock *reg_sock,
190 struct jul_app **r_app)
f20baf8e
DG
191{
192 int ret;
193 pid_t pid;
194 ssize_t size;
195 struct jul_app *app;
196 struct jul_register_msg msg;
197 struct lttcomm_sock *new_sock;
198
199 assert(reg_sock);
200
201 new_sock = reg_sock->ops->accept(reg_sock);
202 if (!new_sock) {
203 ret = -ENOTCONN;
204 goto error;
205 }
206
207 size = new_sock->ops->recvmsg(new_sock, &msg, sizeof(msg), 0);
208 if (size < sizeof(msg)) {
e37c37c6 209 ret = -EINVAL;
f20baf8e
DG
210 goto error_socket;
211 }
212 pid = be32toh(msg.pid);
213
214 DBG2("[jul-thread] New registration for pid %d on socket %d", pid,
215 new_sock->fd);
216
217 app = jul_create_app(pid, new_sock);
218 if (!app) {
219 ret = -ENOMEM;
220 goto error_socket;
221 }
222
223 /*
224 * Add before assigning the socket value to the UST app so it can be found
225 * concurrently.
226 */
227 jul_add_app(app);
228
229 /*
6c509840
MD
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.
f20baf8e 233 */
f20baf8e 234
cf9787ee
DG
235 if (r_app) {
236 *r_app = app;
237 }
238
f20baf8e
DG
239 return new_sock->fd;
240
241error_socket:
242 new_sock->ops->close(new_sock);
243 lttcomm_destroy_sock(new_sock);
244error:
245 return ret;
246}
247
4d076222
DG
248/*
249 * This thread manage application notify communication.
250 */
251void *jul_thread_manage_registration(void *data)
252{
253 int i, ret, pollfd;
254 uint32_t revents, nb_fd;
255 struct lttng_poll_event events;
256 struct lttcomm_sock *reg_sock;
257
258 DBG("[jul-thread] Manage JUL application registration.");
259
260 rcu_register_thread();
261 rcu_thread_online();
262
f20baf8e
DG
263 /* JUL initialization call MUST be called before starting the thread. */
264 assert(jul_apps_ht_by_sock);
265
4d076222
DG
266 /* Create pollset with size 2, quit pipe and socket. */
267 ret = sessiond_set_thread_pollset(&events, 2);
268 if (ret < 0) {
269 goto error_poll_create;
270 }
271
272 reg_sock = init_tcp_socket();
273 if (!reg_sock) {
274 goto error_tcp_socket;
275 }
276
277 /* Add create valid TCP socket to poll set. */
278 ret = lttng_poll_add(&events, reg_sock->fd,
279 LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP);
280 if (ret < 0) {
281 goto error;
282 }
283
284 while (1) {
ec4c7666 285 DBG3("[jul-thread] Manage JUL polling");
4d076222
DG
286
287 /* Inifinite blocking call, waiting for transmission */
288restart:
289 ret = lttng_poll_wait(&events, -1);
ec4c7666
MD
290 DBG3("[jul-thread] Manage agent return from poll on %d fds",
291 LTTNG_POLL_GETNB(&events));
4d076222
DG
292 if (ret < 0) {
293 /*
294 * Restart interrupted system call.
295 */
296 if (errno == EINTR) {
297 goto restart;
298 }
299 goto error;
300 }
301 nb_fd = ret;
f20baf8e 302 DBG3("[jul-thread] %d fd ready", nb_fd);
4d076222
DG
303
304 for (i = 0; i < nb_fd; i++) {
305 /* Fetch once the poll data */
306 revents = LTTNG_POLL_GETEV(&events, i);
307 pollfd = LTTNG_POLL_GETFD(&events, i);
308
f0567343
MD
309 if (!revents) {
310 /* No activity for this FD (poll implementation). */
311 continue;
312 }
313
4d076222
DG
314 /* Thread quit pipe has been closed. Killing thread. */
315 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
316 if (ret) {
317 goto exit;
318 }
319
320 /*
321 * Check first if this is a POLLERR since POLLIN is also included
322 * in an error value thus checking first.
323 */
324 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
325 /* Removing from the poll set */
326 ret = lttng_poll_del(&events, pollfd);
327 if (ret < 0) {
328 goto error;
329 }
330
f20baf8e
DG
331 destroy_jul_app(pollfd);
332 } else if (revents & (LPOLLIN)) {
333 int new_fd;
cf9787ee 334 struct jul_app *app = NULL;
f20baf8e
DG
335
336 /* Pollin event of JUL app socket should NEVER happen. */
337 assert(pollfd == reg_sock->fd);
338
cf9787ee 339 new_fd = handle_registration(reg_sock, &app);
f20baf8e
DG
340 if (new_fd < 0) {
341 WARN("[jul-thread] JUL registration failed. Ignoring.");
342 /* Somehow the communication failed. Just continue. */
343 continue;
344 }
cf9787ee
DG
345 /* Should not have a NULL app on success. */
346 assert(app);
f20baf8e
DG
347
348 /* Only add poll error event to only detect shutdown. */
349 ret = lttng_poll_add(&events, new_fd,
350 LPOLLERR | LPOLLHUP | LPOLLRDHUP);
351 if (ret < 0) {
352 destroy_jul_app(new_fd);
353 continue;
354 }
355
356 /* Update newly registered app. */
357 update_jul_app(new_fd);
cf9787ee
DG
358
359 /* On failure, the poll will detect it and clean it up. */
360 (void) jul_send_registration_done(app);
4d076222
DG
361 } else {
362 ERR("Unknown poll events %u for sock %d", revents, pollfd);
363 continue;
364 }
365 }
366 }
367
368exit:
f20baf8e
DG
369 /* Whatever happens, try to delete it and exit. */
370 (void) lttng_poll_del(&events, reg_sock->fd);
4d076222
DG
371error:
372 destroy_tcp_socket(reg_sock);
373error_tcp_socket:
374 lttng_poll_clean(&events);
375error_poll_create:
376 DBG("[jul-thread] is cleaning up and stopping.");
377
f20baf8e
DG
378 if (jul_apps_ht_by_sock) {
379 clean_jul_apps_ht();
380 lttng_ht_destroy(jul_apps_ht_by_sock);
381 }
382
4d076222
DG
383 rcu_thread_offline();
384 rcu_unregister_thread();
385 return NULL;
386}
This page took 0.040892 seconds and 4 git commands to generate.