Fix: sessiond: ODR violation results in memory corruption
[lttng-tools.git] / src / bin / lttng-sessiond / manage-apps.cpp
CommitLineData
7649924e 1/*
21cf9b6b 2 * Copyright (C) 2011 EfficiOS Inc.
ab5be9fa
MJ
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * Copyright (C) 2013 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7649924e 5 *
ab5be9fa 6 * SPDX-License-Identifier: GPL-2.0-only
7649924e 7 *
7649924e
JG
8 */
9
c9e313bc
SM
10#include "manage-apps.hpp"
11#include "testpoint.hpp"
12#include "health-sessiond.hpp"
13#include "utils.hpp"
14#include "thread.hpp"
7649924e 15
f1494934 16namespace {
7649924e
JG
17struct thread_notifiers {
18 struct lttng_pipe *quit_pipe;
19 int apps_cmd_pipe_read_fd;
20};
f1494934 21} /* namespace */
7649924e
JG
22
23static void cleanup_application_management_thread(void *data)
24{
7966af57 25 struct thread_notifiers *notifiers = (thread_notifiers *) data;
7649924e
JG
26
27 lttng_pipe_destroy(notifiers->quit_pipe);
28 free(notifiers);
29}
30
31/*
32 * This thread receives application command sockets (FDs) on the
33 * apps_cmd_pipe and waits (polls) on them until they are closed
34 * or an error occurs.
35 *
36 * At that point, it flushes the data (tracing and metadata) associated
37 * with this application and tears down ust app sessions and other
38 * associated data structures through ust_app_unregister().
39 *
40 * Note that this thread never sends commands to the applications
41 * through the command sockets; it merely listens for hang-ups
42 * and errors on those sockets and cleans-up as they occur.
43 */
44static void *thread_application_management(void *data)
45{
46 int i, ret, pollfd, err = -1;
47 ssize_t size_ret;
48 uint32_t revents, nb_fd;
49 struct lttng_poll_event events;
7966af57 50 struct thread_notifiers *notifiers = (thread_notifiers *) data;
7649924e
JG
51 const int quit_pipe_read_fd = lttng_pipe_get_readfd(
52 notifiers->quit_pipe);
53
54 DBG("[thread] Manage application started");
55
56 rcu_register_thread();
57 rcu_thread_online();
58
412d7227 59 health_register(the_health_sessiond, HEALTH_SESSIOND_TYPE_APP_MANAGE);
7649924e
JG
60
61 if (testpoint(sessiond_thread_manage_apps)) {
62 goto error_testpoint;
63 }
64
65 health_code_update();
66
67 ret = lttng_poll_create(&events, 2, LTTNG_CLOEXEC);
68 if (ret < 0) {
69 goto error_poll_create;
70 }
71
72 ret = lttng_poll_add(&events, notifiers->apps_cmd_pipe_read_fd,
73 LPOLLIN | LPOLLRDHUP);
74 if (ret < 0) {
75 goto error;
76 }
77
78 ret = lttng_poll_add(&events, quit_pipe_read_fd, LPOLLIN | LPOLLERR);
79 if (ret < 0) {
80 goto error;
81 }
82
83 if (testpoint(sessiond_thread_manage_apps_before_loop)) {
84 goto error;
85 }
86
87 health_code_update();
88
89 while (1) {
90 DBG("Apps thread polling");
91
92 /* Inifinite blocking call, waiting for transmission */
93 restart:
94 health_poll_entry();
95 ret = lttng_poll_wait(&events, -1);
96 DBG("Apps thread return from poll on %d fds",
97 LTTNG_POLL_GETNB(&events));
98 health_poll_exit();
99 if (ret < 0) {
100 /*
101 * Restart interrupted system call.
102 */
103 if (errno == EINTR) {
104 goto restart;
105 }
106 goto error;
107 }
108
109 nb_fd = ret;
110
111 for (i = 0; i < nb_fd; i++) {
112 /* Fetch once the poll data */
113 revents = LTTNG_POLL_GETEV(&events, i);
114 pollfd = LTTNG_POLL_GETFD(&events, i);
115
116 health_code_update();
117
7649924e
JG
118 if (pollfd == quit_pipe_read_fd) {
119 err = 0;
120 goto exit;
121 } else if (pollfd == notifiers->apps_cmd_pipe_read_fd) {
122 /* Inspect the apps cmd pipe */
123 if (revents & LPOLLIN) {
124 int sock;
125
126 /* Empty pipe */
127 size_ret = lttng_read(
128 notifiers->apps_cmd_pipe_read_fd,
129 &sock, sizeof(sock));
130 if (size_ret < sizeof(sock)) {
131 PERROR("read apps cmd pipe");
132 goto error;
133 }
134
135 health_code_update();
136
137 /*
138 * Since this is a command socket (write then read),
139 * we only monitor the error events of the socket.
140 */
141 ret = lttng_poll_add(&events, sock,
142 LPOLLERR | LPOLLHUP | LPOLLRDHUP);
143 if (ret < 0) {
144 goto error;
145 }
146
147 DBG("Apps with sock %d added to poll set", sock);
148 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
149 ERR("Apps command pipe error");
150 goto error;
151 } else {
152 ERR("Unknown poll events %u for sock %d", revents, pollfd);
153 goto error;
154 }
155 } else {
156 /*
157 * At this point, we know that a registered application made
158 * the event at poll_wait.
159 */
160 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
161 /* Removing from the poll set */
162 ret = lttng_poll_del(&events, pollfd);
163 if (ret < 0) {
164 goto error;
165 }
166
167 /* Socket closed on remote end. */
168 ust_app_unregister(pollfd);
169 } else {
170 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
171 goto error;
172 }
173 }
174
175 health_code_update();
176 }
177 }
178
179exit:
180error:
181 lttng_poll_clean(&events);
182error_poll_create:
183error_testpoint:
184
185 /*
186 * We don't clean the UST app hash table here since already registered
187 * applications can still be controlled so let them be until the session
188 * daemon dies or the applications stop.
189 */
190
191 if (err) {
192 health_error();
193 ERR("Health error occurred in %s", __func__);
194 }
412d7227 195 health_unregister(the_health_sessiond);
7649924e
JG
196 DBG("Application communication apps thread cleanup complete");
197 rcu_thread_offline();
198 rcu_unregister_thread();
199 return NULL;
200}
201
202static bool shutdown_application_management_thread(void *data)
203{
7966af57 204 struct thread_notifiers *notifiers = (thread_notifiers *) data;
7649924e
JG
205 const int write_fd = lttng_pipe_get_writefd(notifiers->quit_pipe);
206
207 return notify_thread_pipe(write_fd) == 1;
208}
209
210bool launch_application_management_thread(int apps_cmd_pipe_read_fd)
211{
212 struct lttng_pipe *quit_pipe;
213 struct thread_notifiers *notifiers = NULL;
214 struct lttng_thread *thread;
215
64803277 216 notifiers = zmalloc<thread_notifiers>();
7649924e 217 if (!notifiers) {
21fa020e
JG
218 goto error_alloc;
219 }
220 quit_pipe = lttng_pipe_open(FD_CLOEXEC);
221 if (!quit_pipe) {
7649924e
JG
222 goto error;
223 }
224 notifiers->quit_pipe = quit_pipe;
225 notifiers->apps_cmd_pipe_read_fd = apps_cmd_pipe_read_fd;
226
227 thread = lttng_thread_create("UST application management",
228 thread_application_management,
229 shutdown_application_management_thread,
230 cleanup_application_management_thread,
231 notifiers);
232 if (!thread) {
233 goto error;
234 }
235
236 lttng_thread_put(thread);
237 return true;
238error:
239 cleanup_application_management_thread(notifiers);
21fa020e 240error_alloc:
7649924e
JG
241 return false;
242}
This page took 0.053672 seconds and 4 git commands to generate.