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