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