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; | |
9474416f | 201 | uint32_t major_version, minor_version; |
f20baf8e | 202 | ssize_t size; |
fefd409b | 203 | enum lttng_domain_type domain; |
022d91ba DG |
204 | struct agent_app *app; |
205 | struct agent_register_msg msg; | |
f20baf8e DG |
206 | struct lttcomm_sock *new_sock; |
207 | ||
208 | assert(reg_sock); | |
209 | ||
210 | new_sock = reg_sock->ops->accept(reg_sock); | |
211 | if (!new_sock) { | |
212 | ret = -ENOTCONN; | |
213 | goto error; | |
214 | } | |
215 | ||
216 | size = new_sock->ops->recvmsg(new_sock, &msg, sizeof(msg), 0); | |
217 | if (size < sizeof(msg)) { | |
79865500 | 218 | ret = -EINVAL; |
f20baf8e DG |
219 | goto error_socket; |
220 | } | |
fefd409b | 221 | domain = be32toh(msg.domain); |
f20baf8e | 222 | pid = be32toh(msg.pid); |
9474416f DG |
223 | major_version = be32toh(msg.major_version); |
224 | minor_version = be32toh(msg.minor_version); | |
225 | ||
226 | /* Test communication protocol version of the registring agent. */ | |
227 | if (major_version != AGENT_MAJOR_VERSION) { | |
228 | ret = -EINVAL; | |
229 | goto error_socket; | |
230 | } | |
231 | if (minor_version != AGENT_MINOR_VERSION) { | |
232 | ret = -EINVAL; | |
233 | goto error_socket; | |
234 | } | |
f20baf8e | 235 | |
fefd409b DG |
236 | DBG2("[agent-thread] New registration for pid %d domain %d on socket %d", |
237 | pid, domain, new_sock->fd); | |
f20baf8e | 238 | |
fefd409b | 239 | app = agent_create_app(pid, domain, new_sock); |
f20baf8e DG |
240 | if (!app) { |
241 | ret = -ENOMEM; | |
242 | goto error_socket; | |
243 | } | |
244 | ||
245 | /* | |
246 | * Add before assigning the socket value to the UST app so it can be found | |
247 | * concurrently. | |
248 | */ | |
022d91ba | 249 | agent_add_app(app); |
f20baf8e DG |
250 | |
251 | /* | |
022d91ba DG |
252 | * We don't need to attach the agent app to the app. If we ever do so, we |
253 | * should consider both registration order of agent before app and app | |
254 | * before agent. | |
f20baf8e | 255 | */ |
f20baf8e | 256 | |
1b500e7a DG |
257 | if (r_app) { |
258 | *r_app = app; | |
259 | } | |
260 | ||
f20baf8e DG |
261 | return new_sock->fd; |
262 | ||
263 | error_socket: | |
264 | new_sock->ops->close(new_sock); | |
265 | lttcomm_destroy_sock(new_sock); | |
266 | error: | |
267 | return ret; | |
268 | } | |
269 | ||
4d076222 DG |
270 | /* |
271 | * This thread manage application notify communication. | |
272 | */ | |
022d91ba | 273 | void *agent_thread_manage_registration(void *data) |
4d076222 DG |
274 | { |
275 | int i, ret, pollfd; | |
276 | uint32_t revents, nb_fd; | |
277 | struct lttng_poll_event events; | |
278 | struct lttcomm_sock *reg_sock; | |
279 | ||
022d91ba | 280 | DBG("[agent-thread] Manage agent application registration."); |
4d076222 DG |
281 | |
282 | rcu_register_thread(); | |
283 | rcu_thread_online(); | |
284 | ||
022d91ba DG |
285 | /* Agent initialization call MUST be called before starting the thread. */ |
286 | assert(agent_apps_ht_by_sock); | |
f20baf8e | 287 | |
4d076222 DG |
288 | /* Create pollset with size 2, quit pipe and socket. */ |
289 | ret = sessiond_set_thread_pollset(&events, 2); | |
290 | if (ret < 0) { | |
291 | goto error_poll_create; | |
292 | } | |
293 | ||
294 | reg_sock = init_tcp_socket(); | |
295 | if (!reg_sock) { | |
296 | goto error_tcp_socket; | |
297 | } | |
298 | ||
299 | /* Add create valid TCP socket to poll set. */ | |
300 | ret = lttng_poll_add(&events, reg_sock->fd, | |
301 | LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP); | |
302 | if (ret < 0) { | |
303 | goto error; | |
304 | } | |
305 | ||
306 | while (1) { | |
022d91ba | 307 | DBG3("[agent-thread] Manage agent polling on %d fds", |
4d076222 DG |
308 | LTTNG_POLL_GETNB(&events)); |
309 | ||
310 | /* Inifinite blocking call, waiting for transmission */ | |
311 | restart: | |
312 | ret = lttng_poll_wait(&events, -1); | |
313 | if (ret < 0) { | |
314 | /* | |
315 | * Restart interrupted system call. | |
316 | */ | |
317 | if (errno == EINTR) { | |
318 | goto restart; | |
319 | } | |
320 | goto error; | |
321 | } | |
322 | nb_fd = ret; | |
022d91ba | 323 | DBG3("[agent-thread] %d fd ready", nb_fd); |
4d076222 DG |
324 | |
325 | for (i = 0; i < nb_fd; i++) { | |
326 | /* Fetch once the poll data */ | |
327 | revents = LTTNG_POLL_GETEV(&events, i); | |
328 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
329 | ||
330 | /* Thread quit pipe has been closed. Killing thread. */ | |
331 | ret = sessiond_check_thread_quit_pipe(pollfd, revents); | |
332 | if (ret) { | |
333 | goto exit; | |
334 | } | |
335 | ||
336 | /* | |
337 | * Check first if this is a POLLERR since POLLIN is also included | |
338 | * in an error value thus checking first. | |
339 | */ | |
340 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
341 | /* Removing from the poll set */ | |
342 | ret = lttng_poll_del(&events, pollfd); | |
343 | if (ret < 0) { | |
344 | goto error; | |
345 | } | |
346 | ||
022d91ba | 347 | destroy_agent_app(pollfd); |
f20baf8e DG |
348 | } else if (revents & (LPOLLIN)) { |
349 | int new_fd; | |
022d91ba | 350 | struct agent_app *app = NULL; |
f20baf8e | 351 | |
022d91ba | 352 | /* Pollin event of agent app socket should NEVER happen. */ |
f20baf8e DG |
353 | assert(pollfd == reg_sock->fd); |
354 | ||
1b500e7a | 355 | new_fd = handle_registration(reg_sock, &app); |
f20baf8e | 356 | if (new_fd < 0) { |
022d91ba | 357 | WARN("[agent-thread] agent registration failed. Ignoring."); |
f20baf8e DG |
358 | /* Somehow the communication failed. Just continue. */ |
359 | continue; | |
360 | } | |
1b500e7a DG |
361 | /* Should not have a NULL app on success. */ |
362 | assert(app); | |
f20baf8e DG |
363 | |
364 | /* Only add poll error event to only detect shutdown. */ | |
365 | ret = lttng_poll_add(&events, new_fd, | |
366 | LPOLLERR | LPOLLHUP | LPOLLRDHUP); | |
367 | if (ret < 0) { | |
022d91ba | 368 | destroy_agent_app(new_fd); |
f20baf8e DG |
369 | continue; |
370 | } | |
371 | ||
372 | /* Update newly registered app. */ | |
fefd409b | 373 | update_agent_app(app); |
1b500e7a DG |
374 | |
375 | /* On failure, the poll will detect it and clean it up. */ | |
022d91ba | 376 | (void) agent_send_registration_done(app); |
4d076222 DG |
377 | } else { |
378 | ERR("Unknown poll events %u for sock %d", revents, pollfd); | |
379 | continue; | |
380 | } | |
381 | } | |
382 | } | |
383 | ||
384 | exit: | |
f20baf8e DG |
385 | /* Whatever happens, try to delete it and exit. */ |
386 | (void) lttng_poll_del(&events, reg_sock->fd); | |
4d076222 DG |
387 | error: |
388 | destroy_tcp_socket(reg_sock); | |
389 | error_tcp_socket: | |
390 | lttng_poll_clean(&events); | |
391 | error_poll_create: | |
022d91ba | 392 | DBG("[agent-thread] is cleaning up and stopping."); |
4d076222 | 393 | |
022d91ba DG |
394 | if (agent_apps_ht_by_sock) { |
395 | clean_agent_apps_ht(); | |
396 | lttng_ht_destroy(agent_apps_ht_by_sock); | |
f20baf8e DG |
397 | } |
398 | ||
4d076222 DG |
399 | rcu_thread_offline(); |
400 | rcu_unregister_thread(); | |
401 | return NULL; | |
402 | } |