Commit | Line | Data |
---|---|---|
d0b96690 | 1 | /* |
ab5be9fa | 2 | * Copyright (C) 2013 David Goulet <dgoulet@efficios.com> |
d0b96690 | 3 | * |
ab5be9fa | 4 | * SPDX-License-Identifier: GPL-2.0-only |
d0b96690 | 5 | * |
d0b96690 | 6 | */ |
890d8fe4 | 7 | |
6c1c0768 | 8 | #define _LGPL_SOURCE |
d0b96690 | 9 | |
c9e313bc | 10 | #include "fd-limit.hpp" |
28ab034a | 11 | #include "health-sessiond.hpp" |
c9e313bc SM |
12 | #include "lttng-sessiond.hpp" |
13 | #include "notify-apps.hpp" | |
c9e313bc | 14 | #include "testpoint.hpp" |
c9e313bc | 15 | #include "thread.hpp" |
28ab034a JG |
16 | #include "utils.hpp" |
17 | ||
18 | #include <common/common.hpp> | |
19 | #include <common/utils.hpp> | |
971a61c6 | 20 | |
f1494934 | 21 | namespace { |
971a61c6 JG |
22 | struct thread_notifiers { |
23 | struct lttng_pipe *quit_pipe; | |
24 | int apps_cmd_notify_pipe_read_fd; | |
25 | }; | |
f1494934 | 26 | } /* namespace */ |
d0b96690 DG |
27 | |
28 | /* | |
29 | * This thread manage application notify communication. | |
30 | */ | |
971a61c6 | 31 | static void *thread_application_notification(void *data) |
d0b96690 | 32 | { |
8a00688e | 33 | int i, ret, err = -1; |
6cd525e8 | 34 | ssize_t size_ret; |
8a00688e | 35 | uint32_t nb_fd; |
d0b96690 | 36 | struct lttng_poll_event events; |
7966af57 | 37 | struct thread_notifiers *notifiers = (thread_notifiers *) data; |
8a00688e | 38 | const auto thread_quit_pipe_fd = lttng_pipe_get_readfd(notifiers->quit_pipe); |
d0b96690 DG |
39 | |
40 | DBG("[ust-thread] Manage application notify command"); | |
41 | ||
42 | rcu_register_thread(); | |
43 | rcu_thread_online(); | |
44 | ||
28ab034a | 45 | health_register(the_health_sessiond, HEALTH_SESSIOND_TYPE_APP_MANAGE_NOTIFY); |
380e8d6f | 46 | |
9ad42ec1 MD |
47 | if (testpoint(sessiond_thread_app_manage_notify)) { |
48 | goto error_testpoint; | |
49 | } | |
50 | ||
380e8d6f MD |
51 | health_code_update(); |
52 | ||
971a61c6 | 53 | ret = lttng_poll_create(&events, 2, LTTNG_CLOEXEC); |
d0b96690 DG |
54 | if (ret < 0) { |
55 | goto error_poll_create; | |
56 | } | |
57 | ||
58 | /* Add notify pipe to the pollset. */ | |
28ab034a JG |
59 | ret = lttng_poll_add( |
60 | &events, notifiers->apps_cmd_notify_pipe_read_fd, LPOLLIN | LPOLLRDHUP); | |
d0b96690 DG |
61 | if (ret < 0) { |
62 | goto error; | |
63 | } | |
64 | ||
1524f98c | 65 | ret = lttng_poll_add(&events, thread_quit_pipe_fd, LPOLLIN); |
971a61c6 JG |
66 | if (ret < 0) { |
67 | goto error; | |
68 | } | |
69 | ||
380e8d6f MD |
70 | health_code_update(); |
71 | ||
cd9adb8b | 72 | while (true) { |
7fa2082e | 73 | DBG3("[ust-thread] Manage notify polling"); |
d0b96690 DG |
74 | |
75 | /* Inifinite blocking call, waiting for transmission */ | |
28ab034a | 76 | restart: |
380e8d6f | 77 | health_poll_entry(); |
d0b96690 | 78 | ret = lttng_poll_wait(&events, -1); |
7fa2082e | 79 | DBG3("[ust-thread] Manage notify return from poll on %d fds", |
28ab034a | 80 | LTTNG_POLL_GETNB(&events)); |
380e8d6f | 81 | health_poll_exit(); |
d0b96690 DG |
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++) { | |
380e8d6f MD |
95 | health_code_update(); |
96 | ||
d0b96690 | 97 | /* Fetch once the poll data */ |
8a00688e MJ |
98 | const auto revents = LTTNG_POLL_GETEV(&events, i); |
99 | const auto pollfd = LTTNG_POLL_GETFD(&events, i); | |
d0b96690 | 100 | |
8a00688e MJ |
101 | /* Activity on thread quit pipe, exiting. */ |
102 | if (pollfd == thread_quit_pipe_fd) { | |
103 | DBG("Activity on thread quit pipe"); | |
380e8d6f | 104 | err = 0; |
d0b96690 | 105 | goto exit; |
8a00688e MJ |
106 | } |
107 | ||
108 | if (pollfd == notifiers->apps_cmd_notify_pipe_read_fd) { | |
971a61c6 | 109 | /* Inspect the apps cmd pipe */ |
d0b96690 DG |
110 | int sock; |
111 | ||
03e43155 MD |
112 | if (revents & LPOLLIN) { |
113 | /* Get socket from dispatch thread. */ | |
28ab034a JG |
114 | size_ret = |
115 | lttng_read(notifiers->apps_cmd_notify_pipe_read_fd, | |
116 | &sock, | |
117 | sizeof(sock)); | |
03e43155 MD |
118 | if (size_ret < sizeof(sock)) { |
119 | PERROR("read apps notify pipe"); | |
120 | goto error; | |
121 | } | |
122 | health_code_update(); | |
123 | ||
1524f98c | 124 | ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLRDHUP); |
03e43155 MD |
125 | if (ret < 0) { |
126 | /* | |
28ab034a JG |
127 | * It's possible we've reached the max poll fd |
128 | * allowed. Let's close the socket but continue | |
129 | * normal execution. | |
03e43155 MD |
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)) { | |
d0b96690 DG |
140 | ERR("Apps notify command pipe error"); |
141 | goto error; | |
03e43155 | 142 | } else { |
28ab034a JG |
143 | ERR("Unexpected poll events %u for sock %d", |
144 | revents, | |
145 | pollfd); | |
d0b96690 DG |
146 | goto error; |
147 | } | |
d0b96690 DG |
148 | } else { |
149 | /* | |
150 | * At this point, we know that a registered application | |
151 | * triggered the event. | |
152 | */ | |
03e43155 MD |
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 | ||
28ab034a JG |
162 | /* The socket is closed after a grace period here. |
163 | */ | |
03e43155 MD |
164 | ust_app_notify_sock_unregister(pollfd); |
165 | } | |
166 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
d0b96690 DG |
167 | /* Removing from the poll set */ |
168 | ret = lttng_poll_del(&events, pollfd); | |
169 | if (ret < 0) { | |
170 | goto error; | |
171 | } | |
172 | ||
d88aee68 DG |
173 | /* The socket is closed after a grace period here. */ |
174 | ust_app_notify_sock_unregister(pollfd); | |
d0b96690 | 175 | } else { |
28ab034a JG |
176 | ERR("Unexpected poll events %u for sock %d", |
177 | revents, | |
178 | pollfd); | |
03e43155 | 179 | goto error; |
d0b96690 | 180 | } |
380e8d6f | 181 | health_code_update(); |
d0b96690 DG |
182 | } |
183 | } | |
184 | } | |
185 | ||
186 | exit: | |
187 | error: | |
188 | lttng_poll_clean(&events); | |
189 | error_poll_create: | |
9ad42ec1 | 190 | error_testpoint: |
971a61c6 | 191 | |
d0b96690 | 192 | DBG("Application notify communication apps thread cleanup complete"); |
380e8d6f MD |
193 | if (err) { |
194 | health_error(); | |
195 | ERR("Health error occurred in %s", __func__); | |
196 | } | |
412d7227 | 197 | health_unregister(the_health_sessiond); |
d0b96690 DG |
198 | rcu_thread_offline(); |
199 | rcu_unregister_thread(); | |
cd9adb8b | 200 | return nullptr; |
d0b96690 | 201 | } |
971a61c6 JG |
202 | |
203 | static bool shutdown_application_notification_thread(void *data) | |
204 | { | |
7966af57 | 205 | struct thread_notifiers *notifiers = (thread_notifiers *) data; |
971a61c6 JG |
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 | { | |
7966af57 | 213 | struct thread_notifiers *notifiers = (thread_notifiers *) data; |
971a61c6 JG |
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 | ||
64803277 | 225 | notifiers = zmalloc<thread_notifiers>(); |
971a61c6 | 226 | if (!notifiers) { |
21fa020e | 227 | goto error_alloc; |
971a61c6 JG |
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", | |
28ab034a JG |
238 | thread_application_notification, |
239 | shutdown_application_notification_thread, | |
240 | cleanup_application_notification_thread, | |
241 | notifiers); | |
971a61c6 JG |
242 | if (!thread) { |
243 | goto error; | |
244 | } | |
245 | lttng_thread_put(thread); | |
246 | return true; | |
247 | error: | |
248 | cleanup_application_notification_thread(notifiers); | |
21fa020e | 249 | error_alloc: |
971a61c6 JG |
250 | return false; |
251 | } |