Commit | Line | Data |
---|---|---|
4d076222 | 1 | /* |
ab5be9fa | 2 | * Copyright (C) 2013 David Goulet <dgoulet@efficios.com> |
4d076222 | 3 | * |
ab5be9fa | 4 | * SPDX-License-Identifier: GPL-2.0-only |
4d076222 | 5 | * |
4d076222 DG |
6 | */ |
7 | ||
6c1c0768 | 8 | #define _LGPL_SOURCE |
4d076222 | 9 | |
c9e313bc SM |
10 | #include "agent-thread.hpp" |
11 | #include "agent.hpp" | |
28ab034a | 12 | #include "fd-limit.hpp" |
c9e313bc SM |
13 | #include "lttng-sessiond.hpp" |
14 | #include "session.hpp" | |
c9e313bc | 15 | #include "thread.hpp" |
28ab034a JG |
16 | #include "utils.hpp" |
17 | ||
18 | #include <common/common.hpp> | |
19 | #include <common/compat/endian.hpp> | |
20 | #include <common/sessiond-comm/sessiond-comm.hpp> | |
56047f5a | 21 | #include <common/urcu.hpp> |
28ab034a JG |
22 | #include <common/uri.hpp> |
23 | #include <common/utils.hpp> | |
4d076222 | 24 | |
671e39d7 MJ |
25 | #include <fcntl.h> |
26 | ||
f1494934 | 27 | namespace { |
c78d8e86 JG |
28 | struct thread_notifiers { |
29 | struct lttng_pipe *quit_pipe; | |
30 | sem_t ready; | |
31 | }; | |
32 | ||
733c9165 JG |
33 | struct agent_app_id { |
34 | pid_t pid; | |
35 | enum lttng_domain_type domain; | |
36 | }; | |
37 | ||
38 | struct agent_protocol_version { | |
39 | unsigned int major, minor; | |
40 | }; | |
41 | ||
f1494934 | 42 | int agent_tracing_enabled = -1; |
f28f9e44 | 43 | |
4d076222 DG |
44 | /* |
45 | * Note that there is not port here. It's set after this URI is parsed so we | |
46 | * can let the user define a custom one. However, localhost is ALWAYS the | |
47 | * default listening address. | |
48 | */ | |
f1494934 JG |
49 | const char *default_reg_uri = "tcp://" DEFAULT_NETWORK_VIEWER_BIND_ADDRESS; |
50 | } /* namespace */ | |
4d076222 | 51 | |
f20baf8e | 52 | /* |
022d91ba | 53 | * Update agent application using the given socket. This is done just after |
f20baf8e DG |
54 | * registration was successful. |
55 | * | |
733c9165 JG |
56 | * This will acquire the various sessions' lock; none must be held by the |
57 | * caller. | |
58 | * The caller must hold the session list lock. | |
f20baf8e | 59 | */ |
733c9165 | 60 | static void update_agent_app(const struct agent_app *app) |
f20baf8e DG |
61 | { |
62 | struct ltt_session *session, *stmp; | |
63 | struct ltt_session_list *list; | |
44760c20 JR |
64 | struct agent *trigger_agent; |
65 | struct lttng_ht_iter iter; | |
f20baf8e DG |
66 | |
67 | list = session_get_list(); | |
a0377dfe | 68 | LTTNG_ASSERT(list); |
f20baf8e | 69 | |
28ab034a | 70 | cds_list_for_each_entry_safe (session, stmp, &list->head, list) { |
e32d7f27 JG |
71 | if (!session_get(session)) { |
72 | continue; | |
73 | } | |
74 | ||
f20baf8e DG |
75 | session_lock(session); |
76 | if (session->ust_session) { | |
733c9165 | 77 | const struct agent *agt; |
fefd409b | 78 | |
56047f5a | 79 | lttng::urcu::read_lock_guard read_lock; |
fefd409b DG |
80 | agt = trace_ust_find_agent(session->ust_session, app->domain); |
81 | if (agt) { | |
733c9165 | 82 | agent_update(agt, app); |
fefd409b | 83 | } |
f20baf8e DG |
84 | } |
85 | session_unlock(session); | |
e32d7f27 | 86 | session_put(session); |
f20baf8e | 87 | } |
44760c20 | 88 | |
56047f5a JG |
89 | { |
90 | /* | |
91 | * We are protected against the addition of new events by the session | |
92 | * list lock being held. | |
93 | */ | |
94 | lttng::urcu::read_lock_guard read_lock; | |
95 | ||
96 | cds_lfht_for_each_entry ( | |
97 | the_trigger_agents_ht_by_domain->ht, &iter.iter, trigger_agent, node.node) { | |
98 | agent_update(trigger_agent, app); | |
99 | } | |
44760c20 | 100 | } |
f20baf8e DG |
101 | } |
102 | ||
4d076222 DG |
103 | /* |
104 | * Create and init socket from uri. | |
105 | */ | |
cd9adb8b | 106 | static struct lttcomm_sock *init_tcp_socket() |
4d076222 DG |
107 | { |
108 | int ret; | |
cd9adb8b JG |
109 | struct lttng_uri *uri = nullptr; |
110 | struct lttcomm_sock *sock = nullptr; | |
2288467f JG |
111 | unsigned int port; |
112 | bool bind_succeeded = false; | |
4d076222 DG |
113 | |
114 | /* | |
115 | * This should never fail since the URI is hardcoded and the port is set | |
116 | * before this thread is launched. | |
117 | */ | |
118 | ret = uri_parse(default_reg_uri, &uri); | |
a0377dfe FD |
119 | LTTNG_ASSERT(ret); |
120 | LTTNG_ASSERT(the_config.agent_tcp_port.begin > 0); | |
412d7227 | 121 | uri->port = the_config.agent_tcp_port.begin; |
4d076222 DG |
122 | |
123 | sock = lttcomm_alloc_sock_from_uri(uri); | |
124 | uri_free(uri); | |
cd9adb8b | 125 | if (sock == nullptr) { |
bd0514a5 | 126 | ERR("agent allocating TCP socket"); |
4d076222 DG |
127 | goto error; |
128 | } | |
129 | ||
130 | ret = lttcomm_create_sock(sock); | |
131 | if (ret < 0) { | |
132 | goto error; | |
133 | } | |
134 | ||
28ab034a JG |
135 | for (port = the_config.agent_tcp_port.begin; port <= the_config.agent_tcp_port.end; |
136 | port++) { | |
2288467f JG |
137 | ret = lttcomm_sock_set_port(sock, (uint16_t) port); |
138 | if (ret) { | |
28ab034a | 139 | ERR("Failed to set port %u on socket", port); |
2288467f JG |
140 | goto error; |
141 | } | |
bd0514a5 | 142 | DBG3("Trying to bind on port %u", port); |
2288467f JG |
143 | ret = sock->ops->bind(sock); |
144 | if (!ret) { | |
145 | bind_succeeded = true; | |
146 | break; | |
147 | } | |
148 | ||
149 | if (errno == EADDRINUSE) { | |
28ab034a | 150 | DBG("Failed to bind to port %u since it is already in use", port); |
2288467f JG |
151 | } else { |
152 | PERROR("Failed to bind to port %u", port); | |
153 | goto error; | |
154 | } | |
155 | } | |
156 | ||
157 | if (!bind_succeeded) { | |
28ab034a | 158 | if (the_config.agent_tcp_port.begin == the_config.agent_tcp_port.end) { |
2288467f | 159 | WARN("Another process is already using the agent port %i. " |
412d7227 | 160 | "Agent support will be deactivated.", |
28ab034a | 161 | the_config.agent_tcp_port.begin); |
2288467f JG |
162 | goto error; |
163 | } else { | |
164 | WARN("All ports in the range [%i, %i] are already in use. " | |
412d7227 | 165 | "Agent support will be deactivated.", |
28ab034a JG |
166 | the_config.agent_tcp_port.begin, |
167 | the_config.agent_tcp_port.end); | |
2288467f JG |
168 | goto error; |
169 | } | |
4d076222 DG |
170 | } |
171 | ||
172 | ret = sock->ops->listen(sock, -1); | |
173 | if (ret < 0) { | |
174 | goto error; | |
175 | } | |
176 | ||
28ab034a | 177 | DBG("Listening on TCP port %u and socket %d", port, sock->fd); |
4d076222 DG |
178 | |
179 | return sock; | |
180 | ||
181 | error: | |
182 | if (sock) { | |
183 | lttcomm_destroy_sock(sock); | |
184 | } | |
cd9adb8b | 185 | return nullptr; |
4d076222 DG |
186 | } |
187 | ||
188 | /* | |
189 | * Close and destroy the given TCP socket. | |
190 | */ | |
191 | static void destroy_tcp_socket(struct lttcomm_sock *sock) | |
192 | { | |
2288467f JG |
193 | int ret; |
194 | uint16_t port; | |
195 | ||
a0377dfe | 196 | LTTNG_ASSERT(sock); |
4d076222 | 197 | |
2288467f JG |
198 | ret = lttcomm_sock_get_port(sock, &port); |
199 | if (ret) { | |
bd0514a5 | 200 | ERR("Failed to get port of agent TCP socket"); |
2288467f JG |
201 | port = 0; |
202 | } | |
203 | ||
28ab034a | 204 | DBG3("Destroy TCP socket on port %" PRIu16, port); |
4d076222 DG |
205 | |
206 | /* This will return gracefully if fd is invalid. */ | |
207 | sock->ops->close(sock); | |
208 | lttcomm_destroy_sock(sock); | |
209 | } | |
210 | ||
733c9165 JG |
211 | static const char *domain_type_str(enum lttng_domain_type domain_type) |
212 | { | |
213 | switch (domain_type) { | |
214 | case LTTNG_DOMAIN_NONE: | |
215 | return "none"; | |
216 | case LTTNG_DOMAIN_KERNEL: | |
217 | return "kernel"; | |
218 | case LTTNG_DOMAIN_UST: | |
219 | return "ust"; | |
220 | case LTTNG_DOMAIN_JUL: | |
221 | return "jul"; | |
222 | case LTTNG_DOMAIN_LOG4J: | |
223 | return "log4j"; | |
224 | case LTTNG_DOMAIN_PYTHON: | |
225 | return "python"; | |
226 | default: | |
227 | return "unknown"; | |
228 | } | |
229 | } | |
230 | ||
28ab034a | 231 | static bool is_agent_protocol_version_supported(const struct agent_protocol_version *version) |
733c9165 JG |
232 | { |
233 | const bool is_supported = version->major == AGENT_MAJOR_VERSION && | |
28ab034a | 234 | version->minor == AGENT_MINOR_VERSION; |
733c9165 JG |
235 | |
236 | if (!is_supported) { | |
237 | WARN("Refusing agent connection: unsupported protocol version %ui.%ui, expected %i.%i", | |
28ab034a JG |
238 | version->major, |
239 | version->minor, | |
240 | AGENT_MAJOR_VERSION, | |
241 | AGENT_MINOR_VERSION); | |
733c9165 JG |
242 | } |
243 | ||
244 | return is_supported; | |
245 | } | |
246 | ||
f20baf8e | 247 | /* |
733c9165 | 248 | * Handle a new agent connection on the registration socket. |
f20baf8e | 249 | * |
733c9165 JG |
250 | * Returns 0 on success, or else a negative errno value. |
251 | * On success, the resulting socket is returned through `agent_app_socket` | |
252 | * and the application's reported id is updated through `agent_app_id`. | |
f20baf8e | 253 | */ |
28ab034a JG |
254 | static int accept_agent_connection(struct lttcomm_sock *reg_sock, |
255 | struct agent_app_id *agent_app_id, | |
256 | struct lttcomm_sock **agent_app_socket) | |
f20baf8e DG |
257 | { |
258 | int ret; | |
733c9165 | 259 | struct agent_protocol_version agent_version; |
f20baf8e | 260 | ssize_t size; |
022d91ba | 261 | struct agent_register_msg msg; |
f20baf8e DG |
262 | struct lttcomm_sock *new_sock; |
263 | ||
a0377dfe | 264 | LTTNG_ASSERT(reg_sock); |
f20baf8e DG |
265 | |
266 | new_sock = reg_sock->ops->accept(reg_sock); | |
267 | if (!new_sock) { | |
268 | ret = -ENOTCONN; | |
733c9165 | 269 | goto end; |
f20baf8e DG |
270 | } |
271 | ||
272 | size = new_sock->ops->recvmsg(new_sock, &msg, sizeof(msg), 0); | |
273 | if (size < sizeof(msg)) { | |
733c9165 JG |
274 | if (size < 0) { |
275 | PERROR("Failed to register new agent application"); | |
276 | } else if (size != 0) { | |
277 | ERR("Failed to register new agent application: invalid registration message length: expected length = %zu, message length = %zd", | |
28ab034a JG |
278 | sizeof(msg), |
279 | size); | |
733c9165 JG |
280 | } else { |
281 | DBG("Failed to register new agent application: connection closed"); | |
282 | } | |
79865500 | 283 | ret = -EINVAL; |
733c9165 | 284 | goto error_close_socket; |
9474416f | 285 | } |
f20baf8e | 286 | |
28ab034a | 287 | agent_version = (struct agent_protocol_version){ |
733c9165 JG |
288 | be32toh(msg.major_version), |
289 | be32toh(msg.minor_version), | |
290 | }; | |
f20baf8e | 291 | |
733c9165 JG |
292 | /* Test communication protocol version of the registering agent. */ |
293 | if (!is_agent_protocol_version_supported(&agent_version)) { | |
294 | ret = -EINVAL; | |
295 | goto error_close_socket; | |
f20baf8e DG |
296 | } |
297 | ||
28ab034a | 298 | *agent_app_id = (struct agent_app_id){ |
733c9165 | 299 | .pid = (pid_t) be32toh(msg.pid), |
7966af57 | 300 | .domain = (lttng_domain_type) be32toh(msg.domain), |
733c9165 | 301 | }; |
f20baf8e | 302 | |
733c9165 | 303 | DBG2("New registration for agent application: pid = %ld, domain = %s, socket fd = %d", |
28ab034a JG |
304 | (long) agent_app_id->pid, |
305 | domain_type_str(agent_app_id->domain), | |
306 | new_sock->fd); | |
1b500e7a | 307 | |
733c9165 | 308 | *agent_app_socket = new_sock; |
cd9adb8b | 309 | new_sock = nullptr; |
733c9165 JG |
310 | ret = 0; |
311 | goto end; | |
f20baf8e | 312 | |
733c9165 | 313 | error_close_socket: |
f20baf8e DG |
314 | new_sock->ops->close(new_sock); |
315 | lttcomm_destroy_sock(new_sock); | |
733c9165 | 316 | end: |
f20baf8e DG |
317 | return ret; |
318 | } | |
319 | ||
cd9adb8b | 320 | bool agent_tracing_is_enabled() |
f28f9e44 JG |
321 | { |
322 | int enabled; | |
323 | ||
324 | enabled = uatomic_read(&agent_tracing_enabled); | |
a0377dfe | 325 | LTTNG_ASSERT(enabled != -1); |
f28f9e44 JG |
326 | return enabled == 1; |
327 | } | |
328 | ||
2288467f JG |
329 | /* |
330 | * Write agent TCP port using the rundir. | |
331 | */ | |
332 | static int write_agent_port(uint16_t port) | |
333 | { | |
28ab034a | 334 | return utils_create_pid_file((pid_t) port, the_config.agent_port_file_path.value); |
2288467f JG |
335 | } |
336 | ||
28ab034a | 337 | static void mark_thread_as_ready(struct thread_notifiers *notifiers) |
c78d8e86 JG |
338 | { |
339 | DBG("Marking agent management thread as ready"); | |
340 | sem_post(¬ifiers->ready); | |
341 | } | |
342 | ||
28ab034a | 343 | static void wait_until_thread_is_ready(struct thread_notifiers *notifiers) |
c78d8e86 JG |
344 | { |
345 | DBG("Waiting for agent management thread to be ready"); | |
346 | sem_wait(¬ifiers->ready); | |
347 | DBG("Agent management thread is ready"); | |
348 | } | |
349 | ||
4d076222 DG |
350 | /* |
351 | * This thread manage application notify communication. | |
352 | */ | |
8a7e4590 | 353 | static void *thread_agent_management(void *data) |
4d076222 | 354 | { |
8a00688e MJ |
355 | int i, ret; |
356 | uint32_t nb_fd; | |
4d076222 DG |
357 | struct lttng_poll_event events; |
358 | struct lttcomm_sock *reg_sock; | |
7966af57 | 359 | struct thread_notifiers *notifiers = (thread_notifiers *) data; |
28ab034a | 360 | const auto thread_quit_pipe_fd = lttng_pipe_get_readfd(notifiers->quit_pipe); |
4d076222 | 361 | |
bd0514a5 | 362 | DBG("Manage agent application registration."); |
4d076222 DG |
363 | |
364 | rcu_register_thread(); | |
365 | rcu_thread_online(); | |
366 | ||
022d91ba | 367 | /* Agent initialization call MUST be called before starting the thread. */ |
a0377dfe | 368 | LTTNG_ASSERT(the_agent_apps_ht_by_sock); |
f20baf8e | 369 | |
8a7e4590 JG |
370 | /* Create pollset with size 2, quit pipe and registration socket. */ |
371 | ret = lttng_poll_create(&events, 2, LTTNG_CLOEXEC); | |
4d076222 DG |
372 | if (ret < 0) { |
373 | goto error_poll_create; | |
374 | } | |
375 | ||
1524f98c | 376 | ret = lttng_poll_add(&events, thread_quit_pipe_fd, LPOLLIN); |
8a7e4590 JG |
377 | if (ret < 0) { |
378 | goto error_tcp_socket; | |
379 | } | |
380 | ||
4d076222 | 381 | reg_sock = init_tcp_socket(); |
2288467f JG |
382 | if (reg_sock) { |
383 | uint16_t port; | |
384 | ||
cc3b9644 | 385 | ret = lttcomm_sock_get_port(reg_sock, &port); |
a0377dfe | 386 | LTTNG_ASSERT(ret == 0); |
2288467f JG |
387 | |
388 | ret = write_agent_port(port); | |
389 | if (ret) { | |
bd0514a5 | 390 | ERR("Failed to create agent port file: agent tracing will be unavailable"); |
2288467f | 391 | /* Don't prevent the launch of the sessiond on error. */ |
c78d8e86 | 392 | mark_thread_as_ready(notifiers); |
2288467f JG |
393 | goto error; |
394 | } | |
395 | } else { | |
396 | /* Don't prevent the launch of the sessiond on error. */ | |
c78d8e86 | 397 | mark_thread_as_ready(notifiers); |
2288467f JG |
398 | goto error_tcp_socket; |
399 | } | |
f28f9e44 JG |
400 | |
401 | /* | |
402 | * Signal that the agent thread is ready. The command thread | |
403 | * may start to query whether or not agent tracing is enabled. | |
404 | */ | |
2288467f | 405 | uatomic_set(&agent_tracing_enabled, 1); |
c78d8e86 | 406 | mark_thread_as_ready(notifiers); |
4d076222 | 407 | |
733c9165 | 408 | /* Add TCP socket to the poll set. */ |
1524f98c | 409 | ret = lttng_poll_add(&events, reg_sock->fd, LPOLLIN | LPOLLRDHUP); |
4d076222 DG |
410 | if (ret < 0) { |
411 | goto error; | |
412 | } | |
413 | ||
cd9adb8b | 414 | while (true) { |
bd0514a5 | 415 | DBG3("Manage agent polling"); |
4d076222 DG |
416 | |
417 | /* Inifinite blocking call, waiting for transmission */ | |
28ab034a | 418 | restart: |
4d076222 | 419 | ret = lttng_poll_wait(&events, -1); |
28ab034a | 420 | DBG3("Manage agent return from poll on %d fds", LTTNG_POLL_GETNB(&events)); |
4d076222 DG |
421 | if (ret < 0) { |
422 | /* | |
423 | * Restart interrupted system call. | |
424 | */ | |
425 | if (errno == EINTR) { | |
426 | goto restart; | |
427 | } | |
428 | goto error; | |
429 | } | |
430 | nb_fd = ret; | |
bd0514a5 | 431 | DBG3("%d fd ready", nb_fd); |
4d076222 DG |
432 | |
433 | for (i = 0; i < nb_fd; i++) { | |
434 | /* Fetch once the poll data */ | |
8a00688e MJ |
435 | const auto revents = LTTNG_POLL_GETEV(&events, i); |
436 | const auto pollfd = LTTNG_POLL_GETFD(&events, i); | |
4d076222 | 437 | |
8a00688e MJ |
438 | /* Activity on thread quit pipe, exiting. */ |
439 | if (pollfd == thread_quit_pipe_fd) { | |
440 | DBG("Activity on thread quit pipe"); | |
4d076222 DG |
441 | goto exit; |
442 | } | |
443 | ||
733c9165 | 444 | /* Activity on the registration socket. */ |
03e43155 | 445 | if (revents & LPOLLIN) { |
733c9165 | 446 | struct agent_app_id new_app_id; |
cd9adb8b | 447 | struct agent_app *new_app = nullptr; |
733c9165 JG |
448 | struct lttcomm_sock *new_app_socket; |
449 | int new_app_socket_fd; | |
f20baf8e | 450 | |
a0377dfe | 451 | LTTNG_ASSERT(pollfd == reg_sock->fd); |
733c9165 JG |
452 | |
453 | ret = accept_agent_connection( | |
454 | reg_sock, &new_app_id, &new_app_socket); | |
455 | if (ret < 0) { | |
456 | /* Errors are already logged. */ | |
f20baf8e DG |
457 | continue; |
458 | } | |
459 | ||
03e43155 | 460 | /* |
733c9165 JG |
461 | * new_app_socket's ownership has been |
462 | * transferred to the new agent app. | |
03e43155 | 463 | */ |
28ab034a JG |
464 | new_app = agent_create_app( |
465 | new_app_id.pid, new_app_id.domain, new_app_socket); | |
733c9165 | 466 | if (!new_app) { |
28ab034a | 467 | new_app_socket->ops->close(new_app_socket); |
733c9165 JG |
468 | continue; |
469 | } | |
470 | new_app_socket_fd = new_app_socket->fd; | |
cd9adb8b | 471 | new_app_socket = nullptr; |
733c9165 JG |
472 | |
473 | /* | |
474 | * Since this is a command socket (write then | |
475 | * read), only add poll error event to only | |
476 | * detect shutdown. | |
477 | */ | |
1524f98c | 478 | ret = lttng_poll_add(&events, new_app_socket_fd, LPOLLRDHUP); |
f20baf8e | 479 | if (ret < 0) { |
733c9165 | 480 | agent_destroy_app(new_app); |
f20baf8e DG |
481 | continue; |
482 | } | |
483 | ||
733c9165 JG |
484 | /* |
485 | * Prevent sessions from being modified while | |
486 | * the agent application's configuration is | |
487 | * updated. | |
488 | */ | |
489 | session_lock_list(); | |
490 | ||
491 | /* | |
492 | * Update the newly registered applications's | |
493 | * configuration. | |
494 | */ | |
495 | update_agent_app(new_app); | |
1b500e7a | 496 | |
733c9165 | 497 | ret = agent_send_registration_done(new_app); |
03e43155 | 498 | if (ret < 0) { |
733c9165 JG |
499 | agent_destroy_app(new_app); |
500 | /* Removing from the poll set. */ | |
28ab034a | 501 | ret = lttng_poll_del(&events, new_app_socket_fd); |
03e43155 | 502 | if (ret < 0) { |
733c9165 | 503 | session_unlock_list(); |
03e43155 MD |
504 | goto error; |
505 | } | |
03e43155 MD |
506 | continue; |
507 | } | |
733c9165 JG |
508 | |
509 | /* Publish the new agent app. */ | |
510 | agent_add_app(new_app); | |
511 | ||
512 | session_unlock_list(); | |
03e43155 MD |
513 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
514 | /* Removing from the poll set */ | |
515 | ret = lttng_poll_del(&events, pollfd); | |
516 | if (ret < 0) { | |
517 | goto error; | |
518 | } | |
519 | agent_destroy_app_by_sock(pollfd); | |
4d076222 | 520 | } else { |
03e43155 MD |
521 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); |
522 | goto error; | |
4d076222 DG |
523 | } |
524 | } | |
525 | } | |
526 | ||
527 | exit: | |
f20baf8e DG |
528 | /* Whatever happens, try to delete it and exit. */ |
529 | (void) lttng_poll_del(&events, reg_sock->fd); | |
4d076222 DG |
530 | error: |
531 | destroy_tcp_socket(reg_sock); | |
532 | error_tcp_socket: | |
533 | lttng_poll_clean(&events); | |
534 | error_poll_create: | |
2288467f | 535 | uatomic_set(&agent_tracing_enabled, 0); |
bd0514a5 | 536 | DBG("Cleaning up and stopping."); |
4d076222 DG |
537 | rcu_thread_offline(); |
538 | rcu_unregister_thread(); | |
cd9adb8b | 539 | return nullptr; |
4d076222 | 540 | } |
8a7e4590 JG |
541 | |
542 | static bool shutdown_agent_management_thread(void *data) | |
543 | { | |
7966af57 | 544 | struct thread_notifiers *notifiers = (thread_notifiers *) data; |
c78d8e86 | 545 | const int write_fd = lttng_pipe_get_writefd(notifiers->quit_pipe); |
8a7e4590 JG |
546 | |
547 | return notify_thread_pipe(write_fd) == 1; | |
548 | } | |
549 | ||
5b093681 JG |
550 | static void cleanup_agent_management_thread(void *data) |
551 | { | |
7966af57 | 552 | struct thread_notifiers *notifiers = (thread_notifiers *) data; |
5b093681 | 553 | |
c78d8e86 JG |
554 | lttng_pipe_destroy(notifiers->quit_pipe); |
555 | sem_destroy(¬ifiers->ready); | |
556 | free(notifiers); | |
5b093681 JG |
557 | } |
558 | ||
cd9adb8b | 559 | bool launch_agent_management_thread() |
8a7e4590 | 560 | { |
c78d8e86 | 561 | struct thread_notifiers *notifiers; |
8a7e4590 JG |
562 | struct lttng_thread *thread; |
563 | ||
64803277 | 564 | notifiers = zmalloc<thread_notifiers>(); |
c78d8e86 | 565 | if (!notifiers) { |
21fa020e | 566 | goto error_alloc; |
c78d8e86 JG |
567 | } |
568 | ||
569 | sem_init(¬ifiers->ready, 0, 0); | |
570 | notifiers->quit_pipe = lttng_pipe_open(FD_CLOEXEC); | |
571 | if (!notifiers->quit_pipe) { | |
8a7e4590 JG |
572 | goto error; |
573 | } | |
574 | thread = lttng_thread_create("Agent management", | |
28ab034a JG |
575 | thread_agent_management, |
576 | shutdown_agent_management_thread, | |
577 | cleanup_agent_management_thread, | |
578 | notifiers); | |
8a7e4590 JG |
579 | if (!thread) { |
580 | goto error; | |
581 | } | |
c78d8e86 | 582 | wait_until_thread_is_ready(notifiers); |
8a7e4590 JG |
583 | lttng_thread_put(thread); |
584 | return true; | |
585 | error: | |
c78d8e86 | 586 | cleanup_agent_management_thread(notifiers); |
21fa020e | 587 | error_alloc: |
8a7e4590 JG |
588 | return false; |
589 | } |