ea46f05277ca9aad79297afda111a2d1be461181
[lttng-tools.git] / src / bin / lttng-sessiond / manage-apps.cpp
1 /*
2 * Copyright (C) 2011 EfficiOS Inc.
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * Copyright (C) 2013 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 *
6 * SPDX-License-Identifier: GPL-2.0-only
7 *
8 */
9
10 #include "manage-apps.hpp"
11 #include "testpoint.hpp"
12 #include "health-sessiond.hpp"
13 #include "utils.hpp"
14 #include "thread.hpp"
15
16 namespace {
17 struct thread_notifiers {
18 struct lttng_pipe *quit_pipe;
19 int apps_cmd_pipe_read_fd;
20 };
21 } /* namespace */
22
23 static void cleanup_application_management_thread(void *data)
24 {
25 struct thread_notifiers *notifiers = (thread_notifiers *) data;
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 */
44 static void *thread_application_management(void *data)
45 {
46 int i, ret, err = -1;
47 ssize_t size_ret;
48 uint32_t nb_fd;
49 struct lttng_poll_event events;
50 struct thread_notifiers *notifiers = (thread_notifiers *) data;
51 const auto thread_quit_pipe_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
59 health_register(the_health_sessiond, HEALTH_SESSIOND_TYPE_APP_MANAGE);
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, thread_quit_pipe_fd, LPOLLIN);
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 const auto revents = LTTNG_POLL_GETEV(&events, i);
114 const auto pollfd = LTTNG_POLL_GETFD(&events, i);
115
116 health_code_update();
117
118 /* Activity on thread quit pipe, exiting. */
119 if (pollfd == thread_quit_pipe_fd) {
120 DBG("Activity on thread quit pipe");
121 err = 0;
122 goto exit;
123 }
124
125 if (pollfd == notifiers->apps_cmd_pipe_read_fd) {
126 /* Inspect the apps cmd pipe */
127 if (revents & LPOLLIN) {
128 int sock;
129
130 /* Empty pipe */
131 size_ret = lttng_read(
132 notifiers->apps_cmd_pipe_read_fd,
133 &sock, sizeof(sock));
134 if (size_ret < sizeof(sock)) {
135 PERROR("read apps cmd pipe");
136 goto error;
137 }
138
139 health_code_update();
140
141 /*
142 * Since this is a command socket (write then read),
143 * we only monitor the error events of the socket.
144 */
145 ret = lttng_poll_add(&events, sock, LPOLLRDHUP);
146 if (ret < 0) {
147 goto error;
148 }
149
150 DBG("Apps with sock %d added to poll set", sock);
151 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
152 ERR("Apps command pipe error");
153 goto error;
154 } else {
155 ERR("Unknown poll events %u for sock %d", revents, pollfd);
156 goto error;
157 }
158 } else {
159 /*
160 * At this point, we know that a registered application made
161 * the event at poll_wait.
162 */
163 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
164 /* Removing from the poll set */
165 ret = lttng_poll_del(&events, pollfd);
166 if (ret < 0) {
167 goto error;
168 }
169
170 /* Socket closed on remote end. */
171 ust_app_unregister(pollfd);
172 } else {
173 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
174 goto error;
175 }
176 }
177
178 health_code_update();
179 }
180 }
181
182 exit:
183 error:
184 lttng_poll_clean(&events);
185 error_poll_create:
186 error_testpoint:
187
188 /*
189 * We don't clean the UST app hash table here since already registered
190 * applications can still be controlled so let them be until the session
191 * daemon dies or the applications stop.
192 */
193
194 if (err) {
195 health_error();
196 ERR("Health error occurred in %s", __func__);
197 }
198 health_unregister(the_health_sessiond);
199 DBG("Application communication apps thread cleanup complete");
200 rcu_thread_offline();
201 rcu_unregister_thread();
202 return NULL;
203 }
204
205 static bool shutdown_application_management_thread(void *data)
206 {
207 struct thread_notifiers *notifiers = (thread_notifiers *) data;
208 const int write_fd = lttng_pipe_get_writefd(notifiers->quit_pipe);
209
210 return notify_thread_pipe(write_fd) == 1;
211 }
212
213 bool launch_application_management_thread(int apps_cmd_pipe_read_fd)
214 {
215 struct lttng_pipe *quit_pipe;
216 struct thread_notifiers *notifiers = NULL;
217 struct lttng_thread *thread;
218
219 notifiers = zmalloc<thread_notifiers>();
220 if (!notifiers) {
221 goto error_alloc;
222 }
223 quit_pipe = lttng_pipe_open(FD_CLOEXEC);
224 if (!quit_pipe) {
225 goto error;
226 }
227 notifiers->quit_pipe = quit_pipe;
228 notifiers->apps_cmd_pipe_read_fd = apps_cmd_pipe_read_fd;
229
230 thread = lttng_thread_create("UST application management",
231 thread_application_management,
232 shutdown_application_management_thread,
233 cleanup_application_management_thread,
234 notifiers);
235 if (!thread) {
236 goto error;
237 }
238
239 lttng_thread_put(thread);
240 return true;
241 error:
242 cleanup_application_management_thread(notifiers);
243 error_alloc:
244 return false;
245 }
This page took 0.033393 seconds and 4 git commands to generate.