Commit | Line | Data |
---|---|---|
173fca4f | 1 | /* |
ab5be9fa MJ |
2 | * Copyright (C) 2012 David Goulet <dgoulet@efficios.com> |
3 | * Copyright (C) 2018 Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
173fca4f | 4 | * |
ab5be9fa | 5 | * SPDX-License-Identifier: GPL-2.0-only |
173fca4f | 6 | * |
173fca4f JG |
7 | */ |
8 | ||
c9e313bc | 9 | #include "health-sessiond.hpp" |
28ab034a JG |
10 | #include "lttng-sessiond.hpp" |
11 | #include "thread.hpp" | |
12 | #include "utils.hpp" | |
13 | ||
c9e313bc | 14 | #include <common/error.hpp> |
28ab034a | 15 | #include <common/macros.hpp> |
c9e313bc | 16 | #include <common/pipe.hpp> |
28ab034a JG |
17 | #include <common/utils.hpp> |
18 | ||
671e39d7 | 19 | #include <fcntl.h> |
173fca4f JG |
20 | #include <inttypes.h> |
21 | #include <sys/stat.h> | |
173fca4f | 22 | |
f1494934 | 23 | namespace { |
c78d8e86 JG |
24 | struct thread_notifiers { |
25 | struct lttng_pipe *quit_pipe; | |
26 | sem_t ready; | |
27 | }; | |
f1494934 | 28 | } /* namespace */ |
c78d8e86 | 29 | |
28ab034a | 30 | static void mark_thread_as_ready(struct thread_notifiers *notifiers) |
173fca4f | 31 | { |
c78d8e86 JG |
32 | DBG("Marking health management thread as ready"); |
33 | sem_post(¬ifiers->ready); | |
34 | } | |
173fca4f | 35 | |
28ab034a | 36 | static void wait_until_thread_is_ready(struct thread_notifiers *notifiers) |
c78d8e86 JG |
37 | { |
38 | DBG("Waiting for health management thread to be ready"); | |
39 | sem_wait(¬ifiers->ready); | |
40 | DBG("Health management thread is ready"); | |
41 | } | |
42 | ||
43 | static void cleanup_health_management_thread(void *data) | |
44 | { | |
7966af57 | 45 | struct thread_notifiers *notifiers = (thread_notifiers *) data; |
c78d8e86 JG |
46 | |
47 | lttng_pipe_destroy(notifiers->quit_pipe); | |
48 | sem_destroy(¬ifiers->ready); | |
49 | free(notifiers); | |
173fca4f JG |
50 | } |
51 | ||
52 | /* | |
53 | * Thread managing health check socket. | |
54 | */ | |
c78d8e86 | 55 | static void *thread_manage_health(void *data) |
173fca4f JG |
56 | { |
57 | const bool is_root = (getuid() == 0); | |
8a00688e MJ |
58 | int sock = -1, new_sock = -1, ret, i, err = -1; |
59 | uint32_t nb_fd; | |
173fca4f JG |
60 | struct lttng_poll_event events; |
61 | struct health_comm_msg msg; | |
62 | struct health_comm_reply reply; | |
63 | /* Thread-specific quit pipe. */ | |
7966af57 | 64 | struct thread_notifiers *notifiers = (thread_notifiers *) data; |
28ab034a | 65 | const auto thread_quit_pipe_fd = lttng_pipe_get_readfd(notifiers->quit_pipe); |
173fca4f JG |
66 | |
67 | DBG("[thread] Manage health check started"); | |
68 | ||
69 | rcu_register_thread(); | |
70 | ||
71 | /* | |
72 | * Created with a size of two for: | |
8a00688e | 73 | * - health client socket |
173fca4f JG |
74 | * - thread quit pipe |
75 | */ | |
76 | ret = lttng_poll_create(&events, 2, LTTNG_CLOEXEC); | |
77 | if (ret < 0) { | |
78 | goto error; | |
79 | } | |
80 | ||
81 | /* Create unix socket */ | |
412d7227 | 82 | sock = lttcomm_create_unix_sock(the_config.health_unix_sock_path.value); |
173fca4f JG |
83 | if (sock < 0) { |
84 | ERR("Unable to create health check Unix socket"); | |
85 | goto error; | |
86 | } | |
87 | ||
88 | if (is_root) { | |
89 | /* lttng health client socket path permissions */ | |
28ab59d0 JR |
90 | gid_t gid; |
91 | ||
412d7227 | 92 | ret = utils_get_group_id(the_config.tracing_group_name.value, true, &gid); |
28ab59d0 JR |
93 | if (ret) { |
94 | /* Default to root group. */ | |
95 | gid = 0; | |
96 | } | |
97 | ||
412d7227 | 98 | ret = chown(the_config.health_unix_sock_path.value, 0, gid); |
173fca4f | 99 | if (ret < 0) { |
412d7227 | 100 | ERR("Unable to set group on %s", the_config.health_unix_sock_path.value); |
173fca4f JG |
101 | PERROR("chown"); |
102 | goto error; | |
103 | } | |
104 | ||
412d7227 | 105 | ret = chmod(the_config.health_unix_sock_path.value, |
28ab034a | 106 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); |
173fca4f | 107 | if (ret < 0) { |
28ab034a JG |
108 | ERR("Unable to set permissions on %s", |
109 | the_config.health_unix_sock_path.value); | |
173fca4f JG |
110 | PERROR("chmod"); |
111 | goto error; | |
112 | } | |
113 | } | |
114 | ||
115 | /* | |
116 | * Set the CLOEXEC flag. Return code is useless because either way, the | |
117 | * show must go on. | |
118 | */ | |
119 | (void) utils_set_fd_cloexec(sock); | |
120 | ||
121 | ret = lttcomm_listen_unix_sock(sock); | |
122 | if (ret < 0) { | |
123 | goto error; | |
124 | } | |
125 | ||
1524f98c | 126 | ret = lttng_poll_add(&events, thread_quit_pipe_fd, LPOLLIN); |
173fca4f JG |
127 | if (ret < 0) { |
128 | goto error; | |
129 | } | |
130 | ||
8a00688e | 131 | /* Add the health client socket. */ |
173fca4f JG |
132 | ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLPRI); |
133 | if (ret < 0) { | |
134 | goto error; | |
135 | } | |
136 | ||
c78d8e86 | 137 | mark_thread_as_ready(notifiers); |
cd9adb8b | 138 | while (true) { |
173fca4f JG |
139 | DBG("Health check ready"); |
140 | ||
141 | /* Infinite blocking call, waiting for transmission */ | |
28ab034a | 142 | restart: |
173fca4f JG |
143 | ret = lttng_poll_wait(&events, -1); |
144 | if (ret < 0) { | |
145 | /* | |
146 | * Restart interrupted system call. | |
147 | */ | |
148 | if (errno == EINTR) { | |
149 | goto restart; | |
150 | } | |
151 | goto error; | |
152 | } | |
153 | ||
154 | nb_fd = ret; | |
155 | ||
156 | for (i = 0; i < nb_fd; i++) { | |
157 | /* Fetch once the poll data */ | |
8a00688e MJ |
158 | const auto revents = LTTNG_POLL_GETEV(&events, i); |
159 | const auto pollfd = LTTNG_POLL_GETFD(&events, i); | |
160 | ||
161 | /* Activity on thread quit pipe, exiting. */ | |
162 | if (pollfd == thread_quit_pipe_fd) { | |
163 | DBG("Activity on thread quit pipe"); | |
173fca4f JG |
164 | err = 0; |
165 | goto exit; | |
166 | } | |
8a00688e MJ |
167 | |
168 | /* Event on the health client socket. */ | |
169 | if (revents & LPOLLIN) { | |
170 | continue; | |
171 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
172 | ERR("Health socket poll error"); | |
173 | goto error; | |
174 | } else { | |
175 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); | |
176 | goto error; | |
177 | } | |
173fca4f JG |
178 | } |
179 | ||
180 | new_sock = lttcomm_accept_unix_sock(sock); | |
181 | if (new_sock < 0) { | |
182 | goto error; | |
183 | } | |
184 | ||
185 | /* | |
186 | * Set the CLOEXEC flag. Return code is useless because either way, the | |
187 | * show must go on. | |
188 | */ | |
189 | (void) utils_set_fd_cloexec(new_sock); | |
190 | ||
191 | DBG("Receiving data from client for health..."); | |
28ab034a | 192 | ret = lttcomm_recv_unix_sock(new_sock, (void *) &msg, sizeof(msg)); |
173fca4f JG |
193 | if (ret <= 0) { |
194 | DBG("Nothing recv() from client... continuing"); | |
195 | ret = close(new_sock); | |
196 | if (ret) { | |
197 | PERROR("close"); | |
198 | } | |
199 | continue; | |
200 | } | |
201 | ||
202 | rcu_thread_online(); | |
203 | ||
204 | memset(&reply, 0, sizeof(reply)); | |
205 | for (i = 0; i < NR_HEALTH_SESSIOND_TYPES; i++) { | |
206 | /* | |
207 | * health_check_state returns 0 if health is | |
208 | * bad. | |
209 | */ | |
412d7227 | 210 | if (!health_check_state(the_health_sessiond, i)) { |
173fca4f JG |
211 | reply.ret_code |= 1ULL << i; |
212 | } | |
213 | } | |
214 | ||
215 | DBG2("Health check return value %" PRIx64, reply.ret_code); | |
216 | ||
28ab034a | 217 | ret = lttcomm_send_unix_sock(new_sock, (void *) &reply, sizeof(reply)); |
173fca4f JG |
218 | if (ret < 0) { |
219 | ERR("Failed to send health data back to client"); | |
220 | } | |
221 | ||
222 | /* End of transmission */ | |
223 | ret = close(new_sock); | |
224 | if (ret) { | |
225 | PERROR("close"); | |
226 | } | |
227 | } | |
228 | ||
229 | exit: | |
230 | error: | |
231 | if (err) { | |
232 | ERR("Health error occurred in %s", __func__); | |
233 | } | |
234 | DBG("Health check thread dying"); | |
412d7227 | 235 | unlink(the_config.health_unix_sock_path.value); |
173fca4f JG |
236 | if (sock >= 0) { |
237 | ret = close(sock); | |
238 | if (ret) { | |
239 | PERROR("close"); | |
240 | } | |
241 | } | |
242 | ||
243 | lttng_poll_clean(&events); | |
244 | rcu_unregister_thread(); | |
cd9adb8b | 245 | return nullptr; |
173fca4f JG |
246 | } |
247 | ||
c78d8e86 | 248 | static bool shutdown_health_management_thread(void *data) |
173fca4f | 249 | { |
28ab034a | 250 | struct thread_notifiers *notifiers = (thread_notifiers *) data; |
c78d8e86 | 251 | const int write_fd = lttng_pipe_get_writefd(notifiers->quit_pipe); |
173fca4f | 252 | |
c78d8e86 | 253 | return notify_thread_pipe(write_fd) == 1; |
173fca4f JG |
254 | } |
255 | ||
cd9adb8b | 256 | bool launch_health_management_thread() |
173fca4f | 257 | { |
c78d8e86 | 258 | struct thread_notifiers *notifiers; |
173fca4f | 259 | struct lttng_thread *thread; |
173fca4f | 260 | |
64803277 | 261 | notifiers = zmalloc<thread_notifiers>(); |
c78d8e86 | 262 | if (!notifiers) { |
21fa020e | 263 | goto error_alloc; |
173fca4f JG |
264 | } |
265 | ||
c78d8e86 JG |
266 | sem_init(¬ifiers->ready, 0, 0); |
267 | notifiers->quit_pipe = lttng_pipe_open(FD_CLOEXEC); | |
268 | if (!notifiers->quit_pipe) { | |
269 | goto error; | |
270 | } | |
173fca4f | 271 | thread = lttng_thread_create("Health management", |
28ab034a JG |
272 | thread_manage_health, |
273 | shutdown_health_management_thread, | |
274 | cleanup_health_management_thread, | |
275 | notifiers); | |
173fca4f JG |
276 | if (!thread) { |
277 | goto error; | |
278 | } | |
c78d8e86 JG |
279 | |
280 | wait_until_thread_is_ready(notifiers); | |
173fca4f JG |
281 | lttng_thread_put(thread); |
282 | return true; | |
283 | error: | |
c78d8e86 | 284 | cleanup_health_management_thread(notifiers); |
21fa020e | 285 | error_alloc: |
173fca4f JG |
286 | return false; |
287 | } |