Clean-up: modernize pretty_xml.cpp
[lttng-tools.git] / src / bin / lttng-sessiond / health.cpp
1 /*
2 * Copyright (C) 2012 David Goulet <dgoulet@efficios.com>
3 * Copyright (C) 2018 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 *
5 * SPDX-License-Identifier: GPL-2.0-only
6 *
7 */
8
9 #include "health-sessiond.hpp"
10 #include "lttng-sessiond.hpp"
11 #include "thread.hpp"
12 #include "utils.hpp"
13
14 #include <common/error.hpp>
15 #include <common/macros.hpp>
16 #include <common/pipe.hpp>
17 #include <common/utils.hpp>
18
19 #include <fcntl.h>
20 #include <inttypes.h>
21 #include <sys/stat.h>
22
23 namespace {
24 struct thread_notifiers {
25 struct lttng_pipe *quit_pipe;
26 sem_t ready;
27 };
28 } /* namespace */
29
30 static void mark_thread_as_ready(struct thread_notifiers *notifiers)
31 {
32 DBG("Marking health management thread as ready");
33 sem_post(&notifiers->ready);
34 }
35
36 static void wait_until_thread_is_ready(struct thread_notifiers *notifiers)
37 {
38 DBG("Waiting for health management thread to be ready");
39 sem_wait(&notifiers->ready);
40 DBG("Health management thread is ready");
41 }
42
43 static void cleanup_health_management_thread(void *data)
44 {
45 struct thread_notifiers *notifiers = (thread_notifiers *) data;
46
47 lttng_pipe_destroy(notifiers->quit_pipe);
48 sem_destroy(&notifiers->ready);
49 free(notifiers);
50 }
51
52 /*
53 * Thread managing health check socket.
54 */
55 static void *thread_manage_health(void *data)
56 {
57 const bool is_root = (getuid() == 0);
58 int sock = -1, new_sock = -1, ret, i, err = -1;
59 uint32_t nb_fd;
60 struct lttng_poll_event events;
61 struct health_comm_msg msg;
62 struct health_comm_reply reply;
63 /* Thread-specific quit pipe. */
64 struct thread_notifiers *notifiers = (thread_notifiers *) data;
65 const auto thread_quit_pipe_fd = lttng_pipe_get_readfd(notifiers->quit_pipe);
66
67 DBG("[thread] Manage health check started");
68
69 rcu_register_thread();
70
71 /*
72 * Created with a size of two for:
73 * - health client socket
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 */
82 sock = lttcomm_create_unix_sock(the_config.health_unix_sock_path.value);
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 */
90 gid_t gid;
91
92 ret = utils_get_group_id(the_config.tracing_group_name.value, true, &gid);
93 if (ret) {
94 /* Default to root group. */
95 gid = 0;
96 }
97
98 ret = chown(the_config.health_unix_sock_path.value, 0, gid);
99 if (ret < 0) {
100 ERR("Unable to set group on %s", the_config.health_unix_sock_path.value);
101 PERROR("chown");
102 goto error;
103 }
104
105 ret = chmod(the_config.health_unix_sock_path.value,
106 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
107 if (ret < 0) {
108 ERR("Unable to set permissions on %s",
109 the_config.health_unix_sock_path.value);
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
126 ret = lttng_poll_add(&events, thread_quit_pipe_fd, LPOLLIN);
127 if (ret < 0) {
128 goto error;
129 }
130
131 /* Add the health client socket. */
132 ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLPRI);
133 if (ret < 0) {
134 goto error;
135 }
136
137 mark_thread_as_ready(notifiers);
138 while (true) {
139 DBG("Health check ready");
140
141 /* Infinite blocking call, waiting for transmission */
142 restart:
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 */
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");
164 err = 0;
165 goto exit;
166 }
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 }
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...");
192 ret = lttcomm_recv_unix_sock(new_sock, (void *) &msg, sizeof(msg));
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 */
210 if (!health_check_state(the_health_sessiond, i)) {
211 reply.ret_code |= 1ULL << i;
212 }
213 }
214
215 DBG2("Health check return value %" PRIx64, reply.ret_code);
216
217 ret = lttcomm_send_unix_sock(new_sock, (void *) &reply, sizeof(reply));
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");
235 unlink(the_config.health_unix_sock_path.value);
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();
245 return nullptr;
246 }
247
248 static bool shutdown_health_management_thread(void *data)
249 {
250 struct thread_notifiers *notifiers = (thread_notifiers *) data;
251 const int write_fd = lttng_pipe_get_writefd(notifiers->quit_pipe);
252
253 return notify_thread_pipe(write_fd) == 1;
254 }
255
256 bool launch_health_management_thread()
257 {
258 struct thread_notifiers *notifiers;
259 struct lttng_thread *thread;
260
261 notifiers = zmalloc<thread_notifiers>();
262 if (!notifiers) {
263 goto error_alloc;
264 }
265
266 sem_init(&notifiers->ready, 0, 0);
267 notifiers->quit_pipe = lttng_pipe_open(FD_CLOEXEC);
268 if (!notifiers->quit_pipe) {
269 goto error;
270 }
271 thread = lttng_thread_create("Health management",
272 thread_manage_health,
273 shutdown_health_management_thread,
274 cleanup_health_management_thread,
275 notifiers);
276 if (!thread) {
277 goto error;
278 }
279
280 wait_until_thread_is_ready(notifiers);
281 lttng_thread_put(thread);
282 return true;
283 error:
284 cleanup_health_management_thread(notifiers);
285 error_alloc:
286 return false;
287 }
This page took 0.035991 seconds and 5 git commands to generate.