Fix: missing type definitions in mi-lttng-3.0.xsd
[lttng-tools.git] / src / bin / lttng-sessiond / agent-thread.c
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 _LGPL_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 <common/compat/endian.h>
27
28 #include "fd-limit.h"
29 #include "agent-thread.h"
30 #include "agent.h"
31 #include "lttng-sessiond.h"
32 #include "session.h"
33 #include "utils.h"
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 */
40 static const char *default_reg_uri =
41 "tcp://" DEFAULT_NETWORK_VIEWER_BIND_ADDRESS;
42
43 /*
44 * Update agent application using the given socket. This is done just after
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 */
50 static void update_agent_app(struct agent_app *app)
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) {
62 struct agent *agt;
63
64 rcu_read_lock();
65 agt = trace_ust_find_agent(session->ust_session, app->domain);
66 if (agt) {
67 agent_update(agt, app->sock->fd);
68 }
69 rcu_read_unlock();
70 }
71 session_unlock(session);
72 }
73 session_unlock_list();
74 }
75
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);
91 assert(config.agent_tcp_port);
92 uri->port = config.agent_tcp_port;
93
94 sock = lttcomm_alloc_sock_from_uri(uri);
95 uri_free(uri);
96 if (sock == NULL) {
97 ERR("[agent-thread] agent allocating TCP socket");
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) {
108 WARN("Another session daemon is using this agent port. Agent support "
109 "will be deactivated to prevent interfering with the tracing.");
110 goto error;
111 }
112
113 ret = sock->ops->listen(sock, -1);
114 if (ret < 0) {
115 goto error;
116 }
117
118 DBG("[agent-thread] Listening on TCP port %u and socket %d",
119 config.agent_tcp_port, sock->fd);
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
137 DBG3("[agent-thread] Destroy TCP socket on port %u", config.agent_tcp_port);
138
139 /* This will return gracefully if fd is invalid. */
140 sock->ops->close(sock);
141 lttcomm_destroy_sock(sock);
142 }
143
144 /*
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
147 * object. If r_app is not NULL, the created app is set to the pointer.
148 *
149 * Return the new FD created upon accept() on success or else a negative errno
150 * value.
151 */
152 static int handle_registration(struct lttcomm_sock *reg_sock,
153 struct agent_app **r_app)
154 {
155 int ret;
156 pid_t pid;
157 uint32_t major_version, minor_version;
158 ssize_t size;
159 enum lttng_domain_type domain;
160 struct agent_app *app;
161 struct agent_register_msg msg;
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)) {
174 ret = -EINVAL;
175 goto error_socket;
176 }
177 domain = be32toh(msg.domain);
178 pid = be32toh(msg.pid);
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 }
191
192 DBG2("[agent-thread] New registration for pid %d domain %d on socket %d",
193 pid, domain, new_sock->fd);
194
195 app = agent_create_app(pid, domain, new_sock);
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 */
205 agent_add_app(app);
206
207 /*
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.
211 */
212
213 if (r_app) {
214 *r_app = app;
215 }
216
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
226 /*
227 * This thread manage application notify communication.
228 */
229 void *agent_thread_manage_registration(void *data)
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
236 DBG("[agent-thread] Manage agent application registration.");
237
238 rcu_register_thread();
239 rcu_thread_online();
240
241 /* Agent initialization call MUST be called before starting the thread. */
242 assert(agent_apps_ht_by_sock);
243
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
255 /* Add TCP socket to poll set. */
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) {
263 DBG3("[agent-thread] Manage agent polling");
264
265 /* Inifinite blocking call, waiting for transmission */
266 restart:
267 ret = lttng_poll_wait(&events, -1);
268 DBG3("[agent-thread] Manage agent return from poll on %d fds",
269 LTTNG_POLL_GETNB(&events));
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;
280 DBG3("[agent-thread] %d fd ready", nb_fd);
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
287 if (!revents) {
288 /* No activity for this FD (poll implementation). */
289 continue;
290 }
291
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
298 if (revents & LPOLLIN) {
299 int new_fd;
300 struct agent_app *app = NULL;
301
302 assert(pollfd == reg_sock->fd);
303 new_fd = handle_registration(reg_sock, &app);
304 if (new_fd < 0) {
305 continue;
306 }
307 /* Should not have a NULL app on success. */
308 assert(app);
309
310 /*
311 * Since this is a command socket (write then read),
312 * only add poll error event to only detect shutdown.
313 */
314 ret = lttng_poll_add(&events, new_fd,
315 LPOLLERR | LPOLLHUP | LPOLLRDHUP);
316 if (ret < 0) {
317 agent_destroy_app_by_sock(new_fd);
318 continue;
319 }
320
321 /* Update newly registered app. */
322 update_agent_app(app);
323
324 /* On failure, the poll will detect it and clean it up. */
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);
342 } else {
343 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
344 goto error;
345 }
346 }
347 }
348
349 exit:
350 /* Whatever happens, try to delete it and exit. */
351 (void) lttng_poll_del(&events, reg_sock->fd);
352 error:
353 destroy_tcp_socket(reg_sock);
354 error_tcp_socket:
355 lttng_poll_clean(&events);
356 error_poll_create:
357 DBG("[agent-thread] is cleaning up and stopping.");
358
359 rcu_thread_offline();
360 rcu_unregister_thread();
361 return NULL;
362 }
This page took 0.041782 seconds and 4 git commands to generate.