Fix: syscall event rule: emission sites not compared in is_equal
[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 "health-sessiond.hpp"
11 #include "manage-apps.hpp"
12 #include "testpoint.hpp"
13 #include "thread.hpp"
14 #include "utils.hpp"
15
16 #include <fcntl.h>
17
18 namespace {
19 struct thread_notifiers {
20 struct lttng_pipe *quit_pipe;
21 int apps_cmd_pipe_read_fd;
22 };
23 } /* namespace */
24
25 static void cleanup_application_management_thread(void *data)
26 {
27 struct thread_notifiers *notifiers = (thread_notifiers *) data;
28
29 lttng_pipe_destroy(notifiers->quit_pipe);
30 free(notifiers);
31 }
32
33 /*
34 * This thread receives application command sockets (FDs) on the
35 * apps_cmd_pipe and waits (polls) on them until they are closed
36 * or an error occurs.
37 *
38 * At that point, it flushes the data (tracing and metadata) associated
39 * with this application and tears down ust app sessions and other
40 * associated data structures through ust_app_unregister_by_socket().
41 *
42 * Note that this thread never sends commands to the applications
43 * through the command sockets; it merely listens for hang-ups
44 * and errors on those sockets and cleans-up as they occur.
45 */
46 static void *thread_application_management(void *data)
47 {
48 int i, ret, err = -1;
49 ssize_t size_ret;
50 uint32_t nb_fd;
51 struct lttng_poll_event events;
52 struct thread_notifiers *notifiers = (thread_notifiers *) data;
53 const auto thread_quit_pipe_fd = lttng_pipe_get_readfd(notifiers->quit_pipe);
54
55 DBG("[thread] Manage application started");
56
57 rcu_register_thread();
58 rcu_thread_online();
59
60 health_register(the_health_sessiond, HEALTH_SESSIOND_TYPE_APP_MANAGE);
61
62 if (testpoint(sessiond_thread_manage_apps)) {
63 goto error_testpoint;
64 }
65
66 health_code_update();
67
68 ret = lttng_poll_create(&events, 2, LTTNG_CLOEXEC);
69 if (ret < 0) {
70 goto error_poll_create;
71 }
72
73 ret = lttng_poll_add(&events, notifiers->apps_cmd_pipe_read_fd, 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 (true) {
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", LTTNG_POLL_GETNB(&events));
97 health_poll_exit();
98 if (ret < 0) {
99 /*
100 * Restart interrupted system call.
101 */
102 if (errno == EINTR) {
103 goto restart;
104 }
105 goto error;
106 }
107
108 nb_fd = ret;
109
110 for (i = 0; i < nb_fd; i++) {
111 /* Fetch once the poll data */
112 const auto revents = LTTNG_POLL_GETEV(&events, i);
113 const auto pollfd = LTTNG_POLL_GETFD(&events, i);
114
115 health_code_update();
116
117 /* Activity on thread quit pipe, exiting. */
118 if (pollfd == thread_quit_pipe_fd) {
119 DBG("Activity on thread quit pipe");
120 err = 0;
121 goto exit;
122 }
123
124 if (pollfd == notifiers->apps_cmd_pipe_read_fd) {
125 /* Inspect the apps cmd pipe */
126 if (revents & LPOLLIN) {
127 int sock;
128
129 /* Empty pipe */
130 size_ret = lttng_read(notifiers->apps_cmd_pipe_read_fd,
131 &sock,
132 sizeof(sock));
133 if (size_ret < sizeof(sock)) {
134 PERROR("read apps cmd pipe");
135 goto error;
136 }
137
138 health_code_update();
139
140 /*
141 * Since this is a command socket (write then read),
142 * we only monitor the error events of the socket.
143 */
144 ret = lttng_poll_add(&events, sock, LPOLLRDHUP);
145 if (ret < 0) {
146 goto error;
147 }
148
149 DBG("Apps with sock %d added to poll set", sock);
150 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
151 ERR("Apps command pipe error");
152 goto error;
153 } else {
154 ERR("Unknown poll events %u for sock %d", revents, pollfd);
155 goto error;
156 }
157 } else {
158 /*
159 * At this point, we know that a registered application made
160 * the event at poll_wait.
161 */
162 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
163 /* Removing from the poll set */
164 ret = lttng_poll_del(&events, pollfd);
165 if (ret < 0) {
166 goto error;
167 }
168
169 /* Socket closed on remote end. */
170 ust_app_unregister_by_socket(pollfd);
171 } else {
172 ERR("Unexpected poll events %u for sock %d",
173 revents,
174 pollfd);
175 goto error;
176 }
177 }
178
179 health_code_update();
180 }
181 }
182
183 exit:
184 error:
185 lttng_poll_clean(&events);
186 error_poll_create:
187 error_testpoint:
188
189 /*
190 * We don't clean the UST app hash table here since already registered
191 * applications can still be controlled so let them be until the session
192 * daemon dies or the applications stop.
193 */
194
195 if (err) {
196 health_error();
197 ERR("Health error occurred in %s", __func__);
198 }
199 health_unregister(the_health_sessiond);
200 DBG("Application communication apps thread cleanup complete");
201 rcu_thread_offline();
202 rcu_unregister_thread();
203 return nullptr;
204 }
205
206 static bool shutdown_application_management_thread(void *data)
207 {
208 struct thread_notifiers *notifiers = (thread_notifiers *) data;
209 const int write_fd = lttng_pipe_get_writefd(notifiers->quit_pipe);
210
211 return notify_thread_pipe(write_fd) == 1;
212 }
213
214 bool launch_application_management_thread(int apps_cmd_pipe_read_fd)
215 {
216 struct lttng_pipe *quit_pipe;
217 struct thread_notifiers *notifiers = nullptr;
218 struct lttng_thread *thread;
219
220 notifiers = zmalloc<thread_notifiers>();
221 if (!notifiers) {
222 goto error_alloc;
223 }
224 quit_pipe = lttng_pipe_open(FD_CLOEXEC);
225 if (!quit_pipe) {
226 goto error;
227 }
228 notifiers->quit_pipe = quit_pipe;
229 notifiers->apps_cmd_pipe_read_fd = apps_cmd_pipe_read_fd;
230
231 thread = lttng_thread_create("UST application management",
232 thread_application_management,
233 shutdown_application_management_thread,
234 cleanup_application_management_thread,
235 notifiers);
236 if (!thread) {
237 goto error;
238 }
239
240 lttng_thread_put(thread);
241 return true;
242 error:
243 cleanup_application_management_thread(notifiers);
244 error_alloc:
245 return false;
246 }
This page took 0.034809 seconds and 5 git commands to generate.