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) { | |
5b06ad8b DG |
144 | WARN("An other session daemon is using this JUL port. JUL support " |
145 | "will be deactivated not interfering with the tracing."); | |
4d076222 DG |
146 | goto error; |
147 | } | |
148 | ||
149 | ret = sock->ops->listen(sock, -1); | |
150 | if (ret < 0) { | |
151 | goto error; | |
152 | } | |
153 | ||
154 | DBG("[jul-thread] Listening on TCP port %u and socket %d", jul_tcp_port, | |
155 | sock->fd); | |
156 | ||
157 | return sock; | |
158 | ||
159 | error: | |
160 | if (sock) { | |
161 | lttcomm_destroy_sock(sock); | |
162 | } | |
163 | return NULL; | |
164 | } | |
165 | ||
166 | /* | |
167 | * Close and destroy the given TCP socket. | |
168 | */ | |
169 | static void destroy_tcp_socket(struct lttcomm_sock *sock) | |
170 | { | |
171 | assert(sock); | |
172 | ||
173 | DBG3("[jul-thread] Destroy TCP socket on port %u", jul_tcp_port); | |
174 | ||
175 | /* This will return gracefully if fd is invalid. */ | |
176 | sock->ops->close(sock); | |
177 | lttcomm_destroy_sock(sock); | |
178 | } | |
179 | ||
f20baf8e DG |
180 | /* |
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. | |
184 | * | |
185 | * Return the new FD created upon accept() on success or else a negative errno | |
186 | * value. | |
187 | */ | |
188 | static int handle_registration(struct lttcomm_sock *reg_sock) | |
189 | { | |
190 | int ret; | |
191 | pid_t pid; | |
192 | ssize_t size; | |
193 | struct jul_app *app; | |
194 | struct jul_register_msg msg; | |
195 | struct lttcomm_sock *new_sock; | |
196 | ||
197 | assert(reg_sock); | |
198 | ||
199 | new_sock = reg_sock->ops->accept(reg_sock); | |
200 | if (!new_sock) { | |
201 | ret = -ENOTCONN; | |
202 | goto error; | |
203 | } | |
204 | ||
205 | size = new_sock->ops->recvmsg(new_sock, &msg, sizeof(msg), 0); | |
206 | if (size < sizeof(msg)) { | |
207 | ret = -errno; | |
208 | goto error_socket; | |
209 | } | |
210 | pid = be32toh(msg.pid); | |
211 | ||
212 | DBG2("[jul-thread] New registration for pid %d on socket %d", pid, | |
213 | new_sock->fd); | |
214 | ||
215 | app = jul_create_app(pid, new_sock); | |
216 | if (!app) { | |
217 | ret = -ENOMEM; | |
218 | goto error_socket; | |
219 | } | |
220 | ||
221 | /* | |
222 | * Add before assigning the socket value to the UST app so it can be found | |
223 | * concurrently. | |
224 | */ | |
225 | jul_add_app(app); | |
226 | ||
227 | /* | |
6c509840 MD |
228 | * We don't need to attach the JUL app to the app. If we ever do |
229 | * so, we should consider both registration order of JUL before | |
230 | * app and app before JUL. | |
f20baf8e | 231 | */ |
f20baf8e DG |
232 | |
233 | return new_sock->fd; | |
234 | ||
235 | error_socket: | |
236 | new_sock->ops->close(new_sock); | |
237 | lttcomm_destroy_sock(new_sock); | |
238 | error: | |
239 | return ret; | |
240 | } | |
241 | ||
4d076222 DG |
242 | /* |
243 | * This thread manage application notify communication. | |
244 | */ | |
245 | void *jul_thread_manage_registration(void *data) | |
246 | { | |
247 | int i, ret, pollfd; | |
248 | uint32_t revents, nb_fd; | |
249 | struct lttng_poll_event events; | |
250 | struct lttcomm_sock *reg_sock; | |
251 | ||
252 | DBG("[jul-thread] Manage JUL application registration."); | |
253 | ||
254 | rcu_register_thread(); | |
255 | rcu_thread_online(); | |
256 | ||
f20baf8e DG |
257 | /* JUL initialization call MUST be called before starting the thread. */ |
258 | assert(jul_apps_ht_by_sock); | |
259 | ||
4d076222 DG |
260 | /* Create pollset with size 2, quit pipe and socket. */ |
261 | ret = sessiond_set_thread_pollset(&events, 2); | |
262 | if (ret < 0) { | |
263 | goto error_poll_create; | |
264 | } | |
265 | ||
266 | reg_sock = init_tcp_socket(); | |
267 | if (!reg_sock) { | |
268 | goto error_tcp_socket; | |
269 | } | |
270 | ||
271 | /* Add create valid TCP socket to poll set. */ | |
272 | ret = lttng_poll_add(&events, reg_sock->fd, | |
273 | LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP); | |
274 | if (ret < 0) { | |
275 | goto error; | |
276 | } | |
277 | ||
278 | while (1) { | |
279 | DBG3("[jul-thread] Manage JUL polling on %d fds", | |
280 | LTTNG_POLL_GETNB(&events)); | |
281 | ||
282 | /* Inifinite blocking call, waiting for transmission */ | |
283 | restart: | |
284 | ret = lttng_poll_wait(&events, -1); | |
285 | if (ret < 0) { | |
286 | /* | |
287 | * Restart interrupted system call. | |
288 | */ | |
289 | if (errno == EINTR) { | |
290 | goto restart; | |
291 | } | |
292 | goto error; | |
293 | } | |
294 | nb_fd = ret; | |
f20baf8e | 295 | DBG3("[jul-thread] %d fd ready", nb_fd); |
4d076222 DG |
296 | |
297 | for (i = 0; i < nb_fd; i++) { | |
298 | /* Fetch once the poll data */ | |
299 | revents = LTTNG_POLL_GETEV(&events, i); | |
300 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
301 | ||
302 | /* Thread quit pipe has been closed. Killing thread. */ | |
303 | ret = sessiond_check_thread_quit_pipe(pollfd, revents); | |
304 | if (ret) { | |
305 | goto exit; | |
306 | } | |
307 | ||
308 | /* | |
309 | * Check first if this is a POLLERR since POLLIN is also included | |
310 | * in an error value thus checking first. | |
311 | */ | |
312 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
313 | /* Removing from the poll set */ | |
314 | ret = lttng_poll_del(&events, pollfd); | |
315 | if (ret < 0) { | |
316 | goto error; | |
317 | } | |
318 | ||
f20baf8e DG |
319 | destroy_jul_app(pollfd); |
320 | } else if (revents & (LPOLLIN)) { | |
321 | int new_fd; | |
322 | ||
323 | /* Pollin event of JUL app socket should NEVER happen. */ | |
324 | assert(pollfd == reg_sock->fd); | |
325 | ||
326 | new_fd = handle_registration(reg_sock); | |
327 | if (new_fd < 0) { | |
328 | WARN("[jul-thread] JUL registration failed. Ignoring."); | |
329 | /* Somehow the communication failed. Just continue. */ | |
330 | continue; | |
331 | } | |
332 | ||
333 | /* Only add poll error event to only detect shutdown. */ | |
334 | ret = lttng_poll_add(&events, new_fd, | |
335 | LPOLLERR | LPOLLHUP | LPOLLRDHUP); | |
336 | if (ret < 0) { | |
337 | destroy_jul_app(new_fd); | |
338 | continue; | |
339 | } | |
340 | ||
341 | /* Update newly registered app. */ | |
342 | update_jul_app(new_fd); | |
4d076222 DG |
343 | } else { |
344 | ERR("Unknown poll events %u for sock %d", revents, pollfd); | |
345 | continue; | |
346 | } | |
347 | } | |
348 | } | |
349 | ||
350 | exit: | |
f20baf8e DG |
351 | /* Whatever happens, try to delete it and exit. */ |
352 | (void) lttng_poll_del(&events, reg_sock->fd); | |
4d076222 DG |
353 | error: |
354 | destroy_tcp_socket(reg_sock); | |
355 | error_tcp_socket: | |
356 | lttng_poll_clean(&events); | |
357 | error_poll_create: | |
358 | DBG("[jul-thread] is cleaning up and stopping."); | |
359 | ||
f20baf8e DG |
360 | if (jul_apps_ht_by_sock) { |
361 | clean_jul_apps_ht(); | |
362 | lttng_ht_destroy(jul_apps_ht_by_sock); | |
363 | } | |
364 | ||
4d076222 DG |
365 | rcu_thread_offline(); |
366 | rcu_unregister_thread(); | |
367 | return NULL; | |
368 | } |