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