Docs: sessiond: document the role of an application's two sockets
[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
28ab034a 10#include "health-sessiond.hpp"
c9e313bc
SM
11#include "manage-apps.hpp"
12#include "testpoint.hpp"
c9e313bc 13#include "thread.hpp"
28ab034a 14#include "utils.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{
8a00688e 46 int i, ret, err = -1;
7649924e 47 ssize_t size_ret;
8a00688e 48 uint32_t nb_fd;
7649924e 49 struct lttng_poll_event events;
7966af57 50 struct thread_notifiers *notifiers = (thread_notifiers *) data;
28ab034a 51 const auto thread_quit_pipe_fd = lttng_pipe_get_readfd(notifiers->quit_pipe);
7649924e
JG
52
53 DBG("[thread] Manage application started");
54
55 rcu_register_thread();
56 rcu_thread_online();
57
412d7227 58 health_register(the_health_sessiond, HEALTH_SESSIOND_TYPE_APP_MANAGE);
7649924e
JG
59
60 if (testpoint(sessiond_thread_manage_apps)) {
61 goto error_testpoint;
62 }
63
64 health_code_update();
65
66 ret = lttng_poll_create(&events, 2, LTTNG_CLOEXEC);
67 if (ret < 0) {
68 goto error_poll_create;
69 }
70
28ab034a 71 ret = lttng_poll_add(&events, notifiers->apps_cmd_pipe_read_fd, LPOLLIN | LPOLLRDHUP);
7649924e
JG
72 if (ret < 0) {
73 goto error;
74 }
75
1524f98c 76 ret = lttng_poll_add(&events, thread_quit_pipe_fd, LPOLLIN);
7649924e
JG
77 if (ret < 0) {
78 goto error;
79 }
80
81 if (testpoint(sessiond_thread_manage_apps_before_loop)) {
82 goto error;
83 }
84
85 health_code_update();
86
cd9adb8b 87 while (true) {
7649924e
JG
88 DBG("Apps thread polling");
89
90 /* Inifinite blocking call, waiting for transmission */
91 restart:
92 health_poll_entry();
93 ret = lttng_poll_wait(&events, -1);
28ab034a 94 DBG("Apps thread return from poll on %d fds", LTTNG_POLL_GETNB(&events));
7649924e
JG
95 health_poll_exit();
96 if (ret < 0) {
97 /*
98 * Restart interrupted system call.
99 */
100 if (errno == EINTR) {
101 goto restart;
102 }
103 goto error;
104 }
105
106 nb_fd = ret;
107
108 for (i = 0; i < nb_fd; i++) {
109 /* Fetch once the poll data */
8a00688e
MJ
110 const auto revents = LTTNG_POLL_GETEV(&events, i);
111 const auto pollfd = LTTNG_POLL_GETFD(&events, i);
7649924e
JG
112
113 health_code_update();
114
8a00688e
MJ
115 /* Activity on thread quit pipe, exiting. */
116 if (pollfd == thread_quit_pipe_fd) {
117 DBG("Activity on thread quit pipe");
7649924e
JG
118 err = 0;
119 goto exit;
8a00688e
MJ
120 }
121
122 if (pollfd == notifiers->apps_cmd_pipe_read_fd) {
7649924e
JG
123 /* Inspect the apps cmd pipe */
124 if (revents & LPOLLIN) {
125 int sock;
126
127 /* Empty pipe */
28ab034a
JG
128 size_ret = lttng_read(notifiers->apps_cmd_pipe_read_fd,
129 &sock,
130 sizeof(sock));
7649924e
JG
131 if (size_ret < sizeof(sock)) {
132 PERROR("read apps cmd pipe");
133 goto error;
134 }
135
136 health_code_update();
137
138 /*
139 * Since this is a command socket (write then read),
140 * we only monitor the error events of the socket.
141 */
1524f98c 142 ret = lttng_poll_add(&events, sock, LPOLLRDHUP);
7649924e
JG
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 {
28ab034a
JG
170 ERR("Unexpected poll events %u for sock %d",
171 revents,
172 pollfd);
7649924e
JG
173 goto error;
174 }
175 }
176
177 health_code_update();
178 }
179 }
180
181exit:
182error:
183 lttng_poll_clean(&events);
184error_poll_create:
185error_testpoint:
186
187 /*
188 * We don't clean the UST app hash table here since already registered
189 * applications can still be controlled so let them be until the session
190 * daemon dies or the applications stop.
191 */
192
193 if (err) {
194 health_error();
195 ERR("Health error occurred in %s", __func__);
196 }
412d7227 197 health_unregister(the_health_sessiond);
7649924e
JG
198 DBG("Application communication apps thread cleanup complete");
199 rcu_thread_offline();
200 rcu_unregister_thread();
cd9adb8b 201 return nullptr;
7649924e
JG
202}
203
204static bool shutdown_application_management_thread(void *data)
205{
7966af57 206 struct thread_notifiers *notifiers = (thread_notifiers *) data;
7649924e
JG
207 const int write_fd = lttng_pipe_get_writefd(notifiers->quit_pipe);
208
209 return notify_thread_pipe(write_fd) == 1;
210}
211
212bool launch_application_management_thread(int apps_cmd_pipe_read_fd)
213{
214 struct lttng_pipe *quit_pipe;
cd9adb8b 215 struct thread_notifiers *notifiers = nullptr;
7649924e
JG
216 struct lttng_thread *thread;
217
64803277 218 notifiers = zmalloc<thread_notifiers>();
7649924e 219 if (!notifiers) {
21fa020e
JG
220 goto error_alloc;
221 }
222 quit_pipe = lttng_pipe_open(FD_CLOEXEC);
223 if (!quit_pipe) {
7649924e
JG
224 goto error;
225 }
226 notifiers->quit_pipe = quit_pipe;
227 notifiers->apps_cmd_pipe_read_fd = apps_cmd_pipe_read_fd;
228
229 thread = lttng_thread_create("UST application management",
28ab034a
JG
230 thread_application_management,
231 shutdown_application_management_thread,
232 cleanup_application_management_thread,
233 notifiers);
7649924e
JG
234 if (!thread) {
235 goto error;
236 }
237
238 lttng_thread_put(thread);
239 return true;
240error:
241 cleanup_application_management_thread(notifiers);
21fa020e 242error_alloc:
7649924e
JG
243 return false;
244}
This page took 0.060471 seconds and 4 git commands to generate.