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