Commit | Line | Data |
---|---|---|
6c71277b MD |
1 | /* |
2 | * lttng-ctl-health.c | |
3 | * | |
4 | * Linux Trace Toolkit Health Control Library | |
5 | * | |
a4eb26f0 | 6 | * Copyright (C) 2011 EfficiOS Inc. |
6c71277b MD |
7 | * Copyright (C) 2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
8 | * | |
ab5be9fa | 9 | * SPDX-License-Identifier: LGPL-2.1-only |
6c71277b | 10 | * |
6c71277b MD |
11 | */ |
12 | ||
6c1c0768 | 13 | #define _LGPL_SOURCE |
6c71277b MD |
14 | #include <unistd.h> |
15 | #include <sys/types.h> | |
16 | #include <stdint.h> | |
17 | #include <limits.h> | |
18 | #include <errno.h> | |
73d8fe3e | 19 | #include <string.h> |
6c71277b MD |
20 | #include <lttng/health-internal.h> |
21 | ||
22 | #include <bin/lttng-sessiond/health-sessiond.h> | |
23 | #include <bin/lttng-consumerd/health-consumerd.h> | |
24 | #include <bin/lttng-relayd/health-relayd.h> | |
25 | #include <common/defaults.h> | |
26 | #include <common/utils.h> | |
27 | ||
28 | #include "lttng-ctl-helper.h" | |
29 | ||
30 | enum health_component { | |
31 | HEALTH_COMPONENT_SESSIOND, | |
32 | HEALTH_COMPONENT_CONSUMERD, | |
33 | HEALTH_COMPONENT_RELAYD, | |
34 | ||
35 | NR_HEALTH_COMPONENT, | |
36 | }; | |
37 | ||
38 | struct lttng_health_thread { | |
39 | struct lttng_health *p; | |
40 | int state; | |
41 | }; | |
42 | ||
43 | struct lttng_health { | |
44 | enum health_component component; | |
45 | uint64_t state; | |
46 | unsigned int nr_threads; | |
47 | char health_sock_path[PATH_MAX]; | |
48 | /* For consumer health only */ | |
49 | enum lttng_health_consumerd consumerd_type; | |
50 | struct lttng_health_thread thread[]; | |
51 | }; | |
52 | ||
53 | static | |
54 | const char *sessiond_thread_name[NR_HEALTH_SESSIOND_TYPES] = { | |
55 | [ HEALTH_SESSIOND_TYPE_CMD ] = "Session daemon command", | |
56 | [ HEALTH_SESSIOND_TYPE_APP_MANAGE ] = "Session daemon application manager", | |
57 | [ HEALTH_SESSIOND_TYPE_APP_REG ] = "Session daemon application registration", | |
58 | [ HEALTH_SESSIOND_TYPE_KERNEL ] = "Session daemon kernel", | |
59 | [ HEALTH_SESSIOND_TYPE_CONSUMER ] = "Session daemon consumer manager", | |
60 | [ HEALTH_SESSIOND_TYPE_HT_CLEANUP ] = "Session daemon hash table cleanup", | |
61 | [ HEALTH_SESSIOND_TYPE_APP_MANAGE_NOTIFY ] = "Session daemon application notification manager", | |
62 | [ HEALTH_SESSIOND_TYPE_APP_REG_DISPATCH ] = "Session daemon application registration dispatcher", | |
db66e574 | 63 | [ HEALTH_SESSIOND_TYPE_ROTATION ] = "Session daemon rotation manager", |
d086f507 | 64 | [ HEALTH_SESSIOND_TYPE_TIMER ] = "Session daemon timer manager", |
6c71277b MD |
65 | }; |
66 | ||
67 | static | |
68 | const char *consumerd_thread_name[NR_HEALTH_CONSUMERD_TYPES] = { | |
69 | [ HEALTH_CONSUMERD_TYPE_CHANNEL ] = "Consumer daemon channel", | |
70 | [ HEALTH_CONSUMERD_TYPE_METADATA ] = "Consumer daemon metadata", | |
71 | [ HEALTH_CONSUMERD_TYPE_DATA ] = "Consumer daemon data", | |
72 | [ HEALTH_CONSUMERD_TYPE_SESSIOND ] = "Consumer daemon session daemon command manager", | |
73 | [ HEALTH_CONSUMERD_TYPE_METADATA_TIMER ] = "Consumer daemon metadata timer", | |
74 | }; | |
75 | ||
76 | static | |
77 | const char *relayd_thread_name[NR_HEALTH_RELAYD_TYPES] = { | |
78 | [ HEALTH_RELAYD_TYPE_DISPATCHER ] = "Relay daemon dispatcher", | |
79 | [ HEALTH_RELAYD_TYPE_WORKER ] = "Relay daemon worker", | |
80 | [ HEALTH_RELAYD_TYPE_LISTENER ] = "Relay daemon listener", | |
e7b0d526 MD |
81 | [ HEALTH_RELAYD_TYPE_LIVE_DISPATCHER ] = "Relay daemon live dispatcher", |
82 | [ HEALTH_RELAYD_TYPE_LIVE_WORKER ] = "Relay daemon live worker", | |
83 | [ HEALTH_RELAYD_TYPE_LIVE_LISTENER ] = "Relay daemon live listener", | |
6c71277b MD |
84 | }; |
85 | ||
86 | static | |
87 | const char **thread_name[NR_HEALTH_COMPONENT] = { | |
88 | [ HEALTH_COMPONENT_SESSIOND ] = sessiond_thread_name, | |
89 | [ HEALTH_COMPONENT_CONSUMERD] = consumerd_thread_name, | |
90 | [ HEALTH_COMPONENT_RELAYD ] = relayd_thread_name, | |
91 | }; | |
92 | ||
93 | /* | |
94 | * Set health socket path. | |
95 | * | |
1da0efb1 | 96 | * Returns 0 on success or a negative errno. |
6c71277b MD |
97 | */ |
98 | static | |
99 | int set_health_socket_path(struct lttng_health *lh, | |
100 | int tracing_group) | |
101 | { | |
102 | uid_t uid; | |
103 | const char *home; | |
104 | int ret; | |
105 | /* Global and home format strings */ | |
106 | const char *global_str, *home_str; | |
107 | ||
108 | switch (lh->component) { | |
109 | case HEALTH_COMPONENT_SESSIOND: | |
110 | global_str = DEFAULT_GLOBAL_HEALTH_UNIX_SOCK; | |
111 | home_str = DEFAULT_HOME_HEALTH_UNIX_SOCK; | |
112 | break; | |
113 | case HEALTH_COMPONENT_CONSUMERD: | |
114 | switch (lh->consumerd_type) { | |
115 | case LTTNG_HEALTH_CONSUMERD_UST_32: | |
116 | global_str = DEFAULT_GLOBAL_USTCONSUMER32_HEALTH_UNIX_SOCK; | |
117 | home_str = DEFAULT_HOME_USTCONSUMER32_HEALTH_UNIX_SOCK; | |
118 | break; | |
119 | case LTTNG_HEALTH_CONSUMERD_UST_64: | |
120 | global_str = DEFAULT_GLOBAL_USTCONSUMER64_HEALTH_UNIX_SOCK; | |
121 | home_str = DEFAULT_HOME_USTCONSUMER64_HEALTH_UNIX_SOCK; | |
122 | break; | |
123 | case LTTNG_HEALTH_CONSUMERD_KERNEL: | |
124 | global_str = DEFAULT_GLOBAL_KCONSUMER_HEALTH_UNIX_SOCK; | |
125 | home_str = DEFAULT_HOME_KCONSUMER_HEALTH_UNIX_SOCK; | |
126 | break; | |
127 | default: | |
128 | return -EINVAL; | |
129 | } | |
130 | break; | |
131 | case HEALTH_COMPONENT_RELAYD: | |
132 | if (lh->health_sock_path[0] == '\0') { | |
133 | return -EINVAL; | |
134 | } else { | |
135 | return 0; | |
136 | } | |
137 | break; /* Unreached */ | |
138 | default: | |
139 | return -EINVAL; | |
140 | } | |
141 | ||
142 | uid = getuid(); | |
143 | ||
144 | if (uid == 0 || tracing_group) { | |
1da0efb1 | 145 | ret = lttng_strncpy(lh->health_sock_path, |
6c71277b MD |
146 | global_str, |
147 | sizeof(lh->health_sock_path)); | |
1da0efb1 | 148 | return ret == 0 ? 0 : -EINVAL; |
6c71277b MD |
149 | } |
150 | ||
151 | /* | |
152 | * With GNU C < 2.1, snprintf returns -1 if the target buffer | |
153 | * is too small; With GNU C >= 2.1, snprintf returns the | |
154 | * required size (excluding closing null). | |
155 | */ | |
156 | home = utils_get_home_dir(); | |
157 | if (home == NULL) { | |
158 | /* Fallback in /tmp */ | |
159 | home = "/tmp"; | |
160 | } | |
161 | ||
162 | ret = snprintf(lh->health_sock_path, sizeof(lh->health_sock_path), | |
163 | home_str, home); | |
164 | if ((ret < 0) || (ret >= sizeof(lh->health_sock_path))) { | |
165 | return -ENOMEM; | |
166 | } | |
167 | ||
168 | return 0; | |
169 | } | |
170 | ||
171 | static | |
172 | struct lttng_health *lttng_health_create(enum health_component hc, | |
173 | unsigned int nr_threads) | |
174 | { | |
175 | struct lttng_health *lh; | |
176 | int i; | |
177 | ||
178 | lh = zmalloc(sizeof(*lh) + sizeof(lh->thread[0]) * nr_threads); | |
179 | if (!lh) { | |
180 | return NULL; | |
181 | } | |
182 | ||
183 | lh->component = hc; | |
184 | lh->state = UINT64_MAX; /* All bits in error initially */ | |
185 | lh->nr_threads = nr_threads; | |
186 | for (i = 0; i < nr_threads; i++) { | |
187 | lh->thread[i].p = lh; | |
188 | } | |
189 | return lh; | |
190 | } | |
191 | ||
192 | struct lttng_health *lttng_health_create_sessiond(void) | |
193 | { | |
194 | struct lttng_health *lh; | |
195 | ||
196 | lh = lttng_health_create(HEALTH_COMPONENT_SESSIOND, | |
197 | NR_HEALTH_SESSIOND_TYPES); | |
198 | if (!lh) { | |
199 | return NULL; | |
200 | } | |
201 | return lh; | |
202 | } | |
203 | ||
204 | struct lttng_health * | |
205 | lttng_health_create_consumerd(enum lttng_health_consumerd consumerd) | |
206 | { | |
207 | struct lttng_health *lh; | |
208 | ||
209 | lh = lttng_health_create(HEALTH_COMPONENT_CONSUMERD, | |
210 | NR_HEALTH_CONSUMERD_TYPES); | |
211 | if (!lh) { | |
212 | return NULL; | |
213 | } | |
214 | lh->consumerd_type = consumerd; | |
215 | return lh; | |
216 | } | |
217 | ||
218 | struct lttng_health *lttng_health_create_relayd(const char *path) | |
219 | { | |
1da0efb1 JG |
220 | int ret; |
221 | struct lttng_health *lh = NULL; | |
6c71277b MD |
222 | |
223 | if (!path) { | |
1da0efb1 | 224 | goto error; |
6c71277b MD |
225 | } |
226 | ||
227 | lh = lttng_health_create(HEALTH_COMPONENT_RELAYD, | |
228 | NR_HEALTH_RELAYD_TYPES); | |
229 | if (!lh) { | |
1da0efb1 | 230 | goto error; |
6c71277b | 231 | } |
1da0efb1 JG |
232 | |
233 | ret = lttng_strncpy(lh->health_sock_path, path ?: "", | |
234 | sizeof(lh->health_sock_path)); | |
235 | if (ret) { | |
236 | goto error; | |
237 | } | |
238 | ||
6c71277b | 239 | return lh; |
1da0efb1 JG |
240 | |
241 | error: | |
242 | free(lh); | |
243 | return NULL; | |
6c71277b MD |
244 | } |
245 | ||
246 | void lttng_health_destroy(struct lttng_health *lh) | |
247 | { | |
248 | free(lh); | |
249 | } | |
250 | ||
251 | int lttng_health_query(struct lttng_health *health) | |
252 | { | |
253 | int sock, ret, i, tracing_group; | |
254 | struct health_comm_msg msg; | |
255 | struct health_comm_reply reply; | |
256 | ||
257 | if (!health) { | |
258 | return -EINVAL; | |
259 | } | |
260 | ||
261 | tracing_group = lttng_check_tracing_group(); | |
262 | retry: | |
263 | ret = set_health_socket_path(health, tracing_group); | |
264 | if (ret) { | |
265 | goto error; | |
266 | } | |
71d01237 | 267 | /* Connect to component */ |
6c71277b MD |
268 | sock = lttcomm_connect_unix_sock(health->health_sock_path); |
269 | if (sock < 0) { | |
270 | if (tracing_group) { | |
271 | /* For tracing group, fallback to per-user */ | |
272 | tracing_group = 0; | |
273 | goto retry; | |
274 | } | |
275 | ret = -1; | |
276 | goto error; | |
277 | } | |
278 | ||
53efb85a | 279 | memset(&msg, 0, sizeof(msg)); |
6c71277b MD |
280 | msg.cmd = HEALTH_CMD_CHECK; |
281 | ||
282 | ret = lttcomm_send_unix_sock(sock, (void *)&msg, sizeof(msg)); | |
283 | if (ret < 0) { | |
284 | ret = -1; | |
285 | goto close_error; | |
286 | } | |
287 | ||
288 | ret = lttcomm_recv_unix_sock(sock, (void *)&reply, sizeof(reply)); | |
289 | if (ret < 0) { | |
290 | ret = -1; | |
291 | goto close_error; | |
292 | } | |
293 | ||
294 | health->state = reply.ret_code; | |
295 | for (i = 0; i < health->nr_threads; i++) { | |
296 | if (health->state & (1ULL << i)) { | |
297 | health->thread[i].state = -1; | |
298 | } else { | |
299 | health->thread[i].state = 0; | |
300 | } | |
301 | } | |
302 | ||
303 | close_error: | |
304 | { | |
305 | int closeret; | |
306 | ||
307 | closeret = close(sock); | |
308 | assert(!closeret); | |
309 | } | |
310 | ||
311 | error: | |
312 | if (ret >= 0) | |
313 | ret = 0; | |
314 | return ret; | |
315 | } | |
316 | ||
317 | int lttng_health_state(const struct lttng_health *health) | |
318 | { | |
319 | if (!health) { | |
320 | return -EINVAL; | |
321 | } | |
322 | ||
323 | if (health->state == 0) { | |
324 | return 0; | |
325 | } else { | |
326 | return -1; | |
327 | } | |
328 | } | |
329 | ||
330 | int lttng_health_get_nr_threads(const struct lttng_health *health) | |
331 | { | |
332 | if (!health) { | |
333 | return -EINVAL; | |
334 | } | |
335 | return health->nr_threads; | |
336 | } | |
337 | ||
338 | const struct lttng_health_thread * | |
339 | lttng_health_get_thread(const struct lttng_health *health, | |
340 | unsigned int nth_thread) | |
341 | { | |
342 | if (!health || nth_thread >= health->nr_threads) { | |
343 | return NULL; | |
344 | } | |
345 | return &health->thread[nth_thread]; | |
346 | } | |
347 | ||
348 | int lttng_health_thread_state(const struct lttng_health_thread *thread) | |
349 | { | |
350 | if (!thread) { | |
351 | return -EINVAL; | |
352 | } | |
353 | return thread->state; | |
354 | } | |
355 | ||
356 | const char *lttng_health_thread_name(const struct lttng_health_thread *thread) | |
357 | { | |
358 | unsigned int nr; | |
359 | ||
360 | if (!thread) { | |
361 | return NULL; | |
362 | } | |
363 | nr = thread - &thread->p->thread[0]; | |
364 | return thread_name[thread->p->component][nr]; | |
365 | } |