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