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 | ||
6c1c0768 | 18 | #define _LGPL_SOURCE |
4d076222 DG |
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" |
6a4e4039 | 30 | #include "agent.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 | ||
4d076222 DG |
76 | /* |
77 | * Create and init socket from uri. | |
78 | */ | |
79 | static struct lttcomm_sock *init_tcp_socket(void) | |
80 | { | |
81 | int ret; | |
82 | struct lttng_uri *uri = NULL; | |
83 | struct lttcomm_sock *sock = NULL; | |
84 | ||
85 | /* | |
86 | * This should never fail since the URI is hardcoded and the port is set | |
87 | * before this thread is launched. | |
88 | */ | |
89 | ret = uri_parse(default_reg_uri, &uri); | |
90 | assert(ret); | |
022d91ba DG |
91 | assert(agent_tcp_port); |
92 | uri->port = agent_tcp_port; | |
4d076222 DG |
93 | |
94 | sock = lttcomm_alloc_sock_from_uri(uri); | |
95 | uri_free(uri); | |
96 | if (sock == NULL) { | |
022d91ba | 97 | ERR("[agent-thread] agent allocating TCP socket"); |
4d076222 DG |
98 | goto error; |
99 | } | |
100 | ||
101 | ret = lttcomm_create_sock(sock); | |
102 | if (ret < 0) { | |
103 | goto error; | |
104 | } | |
105 | ||
106 | ret = sock->ops->bind(sock); | |
107 | if (ret < 0) { | |
022d91ba | 108 | WARN("Another session daemon is using this agent port. Agent support " |
5368d366 | 109 | "will be deactivated to prevent interfering with the tracing."); |
4d076222 DG |
110 | goto error; |
111 | } | |
112 | ||
113 | ret = sock->ops->listen(sock, -1); | |
114 | if (ret < 0) { | |
115 | goto error; | |
116 | } | |
117 | ||
022d91ba DG |
118 | DBG("[agent-thread] Listening on TCP port %u and socket %d", |
119 | agent_tcp_port, sock->fd); | |
4d076222 DG |
120 | |
121 | return sock; | |
122 | ||
123 | error: | |
124 | if (sock) { | |
125 | lttcomm_destroy_sock(sock); | |
126 | } | |
127 | return NULL; | |
128 | } | |
129 | ||
130 | /* | |
131 | * Close and destroy the given TCP socket. | |
132 | */ | |
133 | static void destroy_tcp_socket(struct lttcomm_sock *sock) | |
134 | { | |
135 | assert(sock); | |
136 | ||
022d91ba | 137 | DBG3("[agent-thread] Destroy TCP socket on port %u", agent_tcp_port); |
4d076222 DG |
138 | |
139 | /* This will return gracefully if fd is invalid. */ | |
140 | sock->ops->close(sock); | |
141 | lttcomm_destroy_sock(sock); | |
142 | } | |
143 | ||
f20baf8e | 144 | /* |
022d91ba DG |
145 | * Handle a new agent registration using the reg socket. After that, a new |
146 | * agent application is added to the global hash table and attach to an UST app | |
1b500e7a | 147 | * object. If r_app is not NULL, the created app is set to the pointer. |
f20baf8e DG |
148 | * |
149 | * Return the new FD created upon accept() on success or else a negative errno | |
150 | * value. | |
151 | */ | |
1b500e7a | 152 | static int handle_registration(struct lttcomm_sock *reg_sock, |
022d91ba | 153 | struct agent_app **r_app) |
f20baf8e DG |
154 | { |
155 | int ret; | |
156 | pid_t pid; | |
9474416f | 157 | uint32_t major_version, minor_version; |
f20baf8e | 158 | ssize_t size; |
fefd409b | 159 | enum lttng_domain_type domain; |
022d91ba DG |
160 | struct agent_app *app; |
161 | struct agent_register_msg msg; | |
f20baf8e DG |
162 | struct lttcomm_sock *new_sock; |
163 | ||
164 | assert(reg_sock); | |
165 | ||
166 | new_sock = reg_sock->ops->accept(reg_sock); | |
167 | if (!new_sock) { | |
168 | ret = -ENOTCONN; | |
169 | goto error; | |
170 | } | |
171 | ||
172 | size = new_sock->ops->recvmsg(new_sock, &msg, sizeof(msg), 0); | |
173 | if (size < sizeof(msg)) { | |
79865500 | 174 | ret = -EINVAL; |
f20baf8e DG |
175 | goto error_socket; |
176 | } | |
fefd409b | 177 | domain = be32toh(msg.domain); |
f20baf8e | 178 | pid = be32toh(msg.pid); |
9474416f DG |
179 | major_version = be32toh(msg.major_version); |
180 | minor_version = be32toh(msg.minor_version); | |
181 | ||
182 | /* Test communication protocol version of the registring agent. */ | |
183 | if (major_version != AGENT_MAJOR_VERSION) { | |
184 | ret = -EINVAL; | |
185 | goto error_socket; | |
186 | } | |
187 | if (minor_version != AGENT_MINOR_VERSION) { | |
188 | ret = -EINVAL; | |
189 | goto error_socket; | |
190 | } | |
f20baf8e | 191 | |
fefd409b DG |
192 | DBG2("[agent-thread] New registration for pid %d domain %d on socket %d", |
193 | pid, domain, new_sock->fd); | |
f20baf8e | 194 | |
fefd409b | 195 | app = agent_create_app(pid, domain, new_sock); |
f20baf8e DG |
196 | if (!app) { |
197 | ret = -ENOMEM; | |
198 | goto error_socket; | |
199 | } | |
200 | ||
201 | /* | |
202 | * Add before assigning the socket value to the UST app so it can be found | |
203 | * concurrently. | |
204 | */ | |
022d91ba | 205 | agent_add_app(app); |
f20baf8e DG |
206 | |
207 | /* | |
022d91ba DG |
208 | * We don't need to attach the agent app to the app. If we ever do so, we |
209 | * should consider both registration order of agent before app and app | |
210 | * before agent. | |
f20baf8e | 211 | */ |
f20baf8e | 212 | |
1b500e7a DG |
213 | if (r_app) { |
214 | *r_app = app; | |
215 | } | |
216 | ||
f20baf8e DG |
217 | return new_sock->fd; |
218 | ||
219 | error_socket: | |
220 | new_sock->ops->close(new_sock); | |
221 | lttcomm_destroy_sock(new_sock); | |
222 | error: | |
223 | return ret; | |
224 | } | |
225 | ||
4d076222 DG |
226 | /* |
227 | * This thread manage application notify communication. | |
228 | */ | |
022d91ba | 229 | void *agent_thread_manage_registration(void *data) |
4d076222 DG |
230 | { |
231 | int i, ret, pollfd; | |
232 | uint32_t revents, nb_fd; | |
233 | struct lttng_poll_event events; | |
234 | struct lttcomm_sock *reg_sock; | |
235 | ||
022d91ba | 236 | DBG("[agent-thread] Manage agent application registration."); |
4d076222 DG |
237 | |
238 | rcu_register_thread(); | |
239 | rcu_thread_online(); | |
240 | ||
022d91ba DG |
241 | /* Agent initialization call MUST be called before starting the thread. */ |
242 | assert(agent_apps_ht_by_sock); | |
f20baf8e | 243 | |
4d076222 DG |
244 | /* Create pollset with size 2, quit pipe and socket. */ |
245 | ret = sessiond_set_thread_pollset(&events, 2); | |
246 | if (ret < 0) { | |
247 | goto error_poll_create; | |
248 | } | |
249 | ||
250 | reg_sock = init_tcp_socket(); | |
251 | if (!reg_sock) { | |
252 | goto error_tcp_socket; | |
253 | } | |
254 | ||
427392b4 | 255 | /* Add TCP socket to poll set. */ |
4d076222 DG |
256 | ret = lttng_poll_add(&events, reg_sock->fd, |
257 | LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP); | |
258 | if (ret < 0) { | |
259 | goto error; | |
260 | } | |
261 | ||
262 | while (1) { | |
546f19b5 | 263 | DBG3("[agent-thread] Manage agent polling"); |
4d076222 DG |
264 | |
265 | /* Inifinite blocking call, waiting for transmission */ | |
266 | restart: | |
267 | ret = lttng_poll_wait(&events, -1); | |
7fa2082e MD |
268 | DBG3("[agent-thread] Manage agent return from poll on %d fds", |
269 | LTTNG_POLL_GETNB(&events)); | |
4d076222 DG |
270 | if (ret < 0) { |
271 | /* | |
272 | * Restart interrupted system call. | |
273 | */ | |
274 | if (errno == EINTR) { | |
275 | goto restart; | |
276 | } | |
277 | goto error; | |
278 | } | |
279 | nb_fd = ret; | |
022d91ba | 280 | DBG3("[agent-thread] %d fd ready", nb_fd); |
4d076222 DG |
281 | |
282 | for (i = 0; i < nb_fd; i++) { | |
283 | /* Fetch once the poll data */ | |
284 | revents = LTTNG_POLL_GETEV(&events, i); | |
285 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
286 | ||
fd20dac9 MD |
287 | if (!revents) { |
288 | /* No activity for this FD (poll implementation). */ | |
289 | continue; | |
290 | } | |
291 | ||
4d076222 DG |
292 | /* Thread quit pipe has been closed. Killing thread. */ |
293 | ret = sessiond_check_thread_quit_pipe(pollfd, revents); | |
294 | if (ret) { | |
295 | goto exit; | |
296 | } | |
297 | ||
03e43155 | 298 | if (revents & LPOLLIN) { |
f20baf8e | 299 | int new_fd; |
022d91ba | 300 | struct agent_app *app = NULL; |
f20baf8e | 301 | |
f20baf8e | 302 | assert(pollfd == reg_sock->fd); |
1b500e7a | 303 | new_fd = handle_registration(reg_sock, &app); |
f20baf8e | 304 | if (new_fd < 0) { |
f20baf8e DG |
305 | continue; |
306 | } | |
1b500e7a DG |
307 | /* Should not have a NULL app on success. */ |
308 | assert(app); | |
f20baf8e | 309 | |
03e43155 MD |
310 | /* |
311 | * Since this is a command socket (write then read), | |
312 | * only add poll error event to only detect shutdown. | |
313 | */ | |
f20baf8e DG |
314 | ret = lttng_poll_add(&events, new_fd, |
315 | LPOLLERR | LPOLLHUP | LPOLLRDHUP); | |
316 | if (ret < 0) { | |
6a4e4039 | 317 | agent_destroy_app_by_sock(new_fd); |
f20baf8e DG |
318 | continue; |
319 | } | |
320 | ||
321 | /* Update newly registered app. */ | |
fefd409b | 322 | update_agent_app(app); |
1b500e7a DG |
323 | |
324 | /* On failure, the poll will detect it and clean it up. */ | |
03e43155 MD |
325 | ret = agent_send_registration_done(app); |
326 | if (ret < 0) { | |
327 | /* Removing from the poll set */ | |
328 | ret = lttng_poll_del(&events, new_fd); | |
329 | if (ret < 0) { | |
330 | goto error; | |
331 | } | |
332 | agent_destroy_app_by_sock(new_fd); | |
333 | continue; | |
334 | } | |
335 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
336 | /* Removing from the poll set */ | |
337 | ret = lttng_poll_del(&events, pollfd); | |
338 | if (ret < 0) { | |
339 | goto error; | |
340 | } | |
341 | agent_destroy_app_by_sock(pollfd); | |
4d076222 | 342 | } else { |
03e43155 MD |
343 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); |
344 | goto error; | |
4d076222 DG |
345 | } |
346 | } | |
347 | } | |
348 | ||
349 | exit: | |
f20baf8e DG |
350 | /* Whatever happens, try to delete it and exit. */ |
351 | (void) lttng_poll_del(&events, reg_sock->fd); | |
4d076222 DG |
352 | error: |
353 | destroy_tcp_socket(reg_sock); | |
354 | error_tcp_socket: | |
355 | lttng_poll_clean(&events); | |
356 | error_poll_create: | |
022d91ba | 357 | DBG("[agent-thread] is cleaning up and stopping."); |
4d076222 DG |
358 | |
359 | rcu_thread_offline(); | |
360 | rcu_unregister_thread(); | |
361 | return NULL; | |
362 | } |