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