Tests: Add test to check shared-memory FD leaks after relayd dies
[lttng-tools.git] / src / bin / lttng-sessiond / notify-apps.cpp
1 /*
2 * Copyright (C) 2013 David Goulet <dgoulet@efficios.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #define _LGPL_SOURCE
9
10 #include "fd-limit.hpp"
11 #include "health-sessiond.hpp"
12 #include "lttng-sessiond.hpp"
13 #include "notify-apps.hpp"
14 #include "testpoint.hpp"
15 #include "thread.hpp"
16 #include "utils.hpp"
17
18 #include <common/common.hpp>
19 #include <common/utils.hpp>
20
21 namespace {
22 struct thread_notifiers {
23 struct lttng_pipe *quit_pipe;
24 int apps_cmd_notify_pipe_read_fd;
25 };
26 } /* namespace */
27
28 /*
29 * This thread manage application notify communication.
30 */
31 static void *thread_application_notification(void *data)
32 {
33 int i, ret, err = -1;
34 ssize_t size_ret;
35 uint32_t nb_fd;
36 struct lttng_poll_event events;
37 struct thread_notifiers *notifiers = (thread_notifiers *) data;
38 const auto thread_quit_pipe_fd = lttng_pipe_get_readfd(notifiers->quit_pipe);
39
40 DBG("[ust-thread] Manage application notify command");
41
42 rcu_register_thread();
43 rcu_thread_online();
44
45 health_register(the_health_sessiond, HEALTH_SESSIOND_TYPE_APP_MANAGE_NOTIFY);
46
47 if (testpoint(sessiond_thread_app_manage_notify)) {
48 goto error_testpoint;
49 }
50
51 health_code_update();
52
53 ret = lttng_poll_create(&events, 2, LTTNG_CLOEXEC);
54 if (ret < 0) {
55 goto error_poll_create;
56 }
57
58 /* Add notify pipe to the pollset. */
59 ret = lttng_poll_add(
60 &events, notifiers->apps_cmd_notify_pipe_read_fd, LPOLLIN | LPOLLRDHUP);
61 if (ret < 0) {
62 goto error;
63 }
64
65 ret = lttng_poll_add(&events, thread_quit_pipe_fd, LPOLLIN);
66 if (ret < 0) {
67 goto error;
68 }
69
70 health_code_update();
71
72 while (true) {
73 DBG3("[ust-thread] Manage notify polling");
74
75 /* Inifinite blocking call, waiting for transmission */
76 restart:
77 health_poll_entry();
78 ret = lttng_poll_wait(&events, -1);
79 DBG3("[ust-thread] Manage notify return from poll on %d fds",
80 LTTNG_POLL_GETNB(&events));
81 health_poll_exit();
82 if (ret < 0) {
83 /*
84 * Restart interrupted system call.
85 */
86 if (errno == EINTR) {
87 goto restart;
88 }
89 goto error;
90 }
91
92 nb_fd = ret;
93
94 for (i = 0; i < nb_fd; i++) {
95 health_code_update();
96
97 /* Fetch once the poll data */
98 const auto revents = LTTNG_POLL_GETEV(&events, i);
99 const auto pollfd = LTTNG_POLL_GETFD(&events, i);
100
101 /* Activity on thread quit pipe, exiting. */
102 if (pollfd == thread_quit_pipe_fd) {
103 DBG("Activity on thread quit pipe");
104 err = 0;
105 goto exit;
106 }
107
108 if (pollfd == notifiers->apps_cmd_notify_pipe_read_fd) {
109 /* Inspect the apps cmd pipe */
110 int sock;
111
112 if (revents & LPOLLIN) {
113 /* Get socket from dispatch thread. */
114 size_ret =
115 lttng_read(notifiers->apps_cmd_notify_pipe_read_fd,
116 &sock,
117 sizeof(sock));
118 if (size_ret < sizeof(sock)) {
119 PERROR("read apps notify pipe");
120 goto error;
121 }
122 health_code_update();
123
124 ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLRDHUP);
125 if (ret < 0) {
126 /*
127 * It's possible we've reached the max poll fd
128 * allowed. Let's close the socket but continue
129 * normal execution.
130 */
131 ret = close(sock);
132 if (ret) {
133 PERROR("close notify socket %d", sock);
134 }
135 lttng_fd_put(LTTNG_FD_APPS, 1);
136 continue;
137 }
138 DBG3("UST thread notify added sock %d to pollset", sock);
139 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
140 ERR("Apps notify command pipe error");
141 goto error;
142 } else {
143 ERR("Unexpected poll events %u for sock %d",
144 revents,
145 pollfd);
146 goto error;
147 }
148 } else {
149 /*
150 * At this point, we know that a registered application
151 * triggered the event.
152 */
153 if (revents & (LPOLLIN | LPOLLPRI)) {
154 ret = ust_app_recv_notify(pollfd);
155 if (ret < 0) {
156 /* Removing from the poll set */
157 ret = lttng_poll_del(&events, pollfd);
158 if (ret < 0) {
159 goto error;
160 }
161
162 /* The socket is closed after a grace period here.
163 */
164 ust_app_notify_sock_unregister(pollfd);
165 }
166 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
167 /* Removing from the poll set */
168 ret = lttng_poll_del(&events, pollfd);
169 if (ret < 0) {
170 goto error;
171 }
172
173 /* The socket is closed after a grace period here. */
174 ust_app_notify_sock_unregister(pollfd);
175 } else {
176 ERR("Unexpected poll events %u for sock %d",
177 revents,
178 pollfd);
179 goto error;
180 }
181 health_code_update();
182 }
183 }
184 }
185
186 exit:
187 error:
188 lttng_poll_clean(&events);
189 error_poll_create:
190 error_testpoint:
191
192 DBG("Application notify communication apps thread cleanup complete");
193 if (err) {
194 health_error();
195 ERR("Health error occurred in %s", __func__);
196 }
197 health_unregister(the_health_sessiond);
198 rcu_thread_offline();
199 rcu_unregister_thread();
200 return nullptr;
201 }
202
203 static bool shutdown_application_notification_thread(void *data)
204 {
205 struct thread_notifiers *notifiers = (thread_notifiers *) data;
206 const int write_fd = lttng_pipe_get_writefd(notifiers->quit_pipe);
207
208 return notify_thread_pipe(write_fd) == 1;
209 }
210
211 static void cleanup_application_notification_thread(void *data)
212 {
213 struct thread_notifiers *notifiers = (thread_notifiers *) data;
214
215 lttng_pipe_destroy(notifiers->quit_pipe);
216 free(notifiers);
217 }
218
219 bool launch_application_notification_thread(int apps_cmd_notify_pipe_read_fd)
220 {
221 struct lttng_thread *thread;
222 struct thread_notifiers *notifiers;
223 struct lttng_pipe *quit_pipe;
224
225 notifiers = zmalloc<thread_notifiers>();
226 if (!notifiers) {
227 goto error_alloc;
228 }
229 notifiers->apps_cmd_notify_pipe_read_fd = apps_cmd_notify_pipe_read_fd;
230
231 quit_pipe = lttng_pipe_open(FD_CLOEXEC);
232 if (!quit_pipe) {
233 goto error;
234 }
235 notifiers->quit_pipe = quit_pipe;
236
237 thread = lttng_thread_create("Application notification",
238 thread_application_notification,
239 shutdown_application_notification_thread,
240 cleanup_application_notification_thread,
241 notifiers);
242 if (!thread) {
243 goto error;
244 }
245 lttng_thread_put(thread);
246 return true;
247 error:
248 cleanup_application_notification_thread(notifiers);
249 error_alloc:
250 return false;
251 }
This page took 0.033042 seconds and 4 git commands to generate.