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