Commit | Line | Data |
---|---|---|
826d496d MD |
1 | /* |
2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> | |
0fdd1e2c | 3 | * Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
26296c48 | 4 | * 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com> |
fac6795d | 5 | * |
d14d33bf AM |
6 | * This program is free software; you can redistribute it and/or modify |
7 | * it under the terms of the GNU General Public License, version 2 only, | |
8 | * as published by the Free Software Foundation. | |
91d76f53 | 9 | * |
d14d33bf AM |
10 | * This program is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
91d76f53 | 14 | * |
d14d33bf AM |
15 | * You should have received a copy of the GNU General Public License along |
16 | * with this program; if not, write to the Free Software Foundation, Inc., | |
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
fac6795d DG |
18 | */ |
19 | ||
6c1c0768 | 20 | #define _LGPL_SOURCE |
fac6795d DG |
21 | #include <getopt.h> |
22 | #include <grp.h> | |
23 | #include <limits.h> | |
0bb7724a | 24 | #include <paths.h> |
fac6795d DG |
25 | #include <pthread.h> |
26 | #include <signal.h> | |
27 | #include <stdio.h> | |
28 | #include <stdlib.h> | |
29 | #include <string.h> | |
331744e3 | 30 | #include <inttypes.h> |
0fdd1e2c | 31 | #include <sys/mman.h> |
b73401da | 32 | #include <sys/mount.h> |
1e307fab | 33 | #include <sys/resource.h> |
fac6795d DG |
34 | #include <sys/socket.h> |
35 | #include <sys/stat.h> | |
36 | #include <sys/types.h> | |
0fdd1e2c | 37 | #include <sys/wait.h> |
5c827ce0 | 38 | #include <urcu/uatomic.h> |
fac6795d | 39 | #include <unistd.h> |
4f0b90ee | 40 | #include <ctype.h> |
fac6795d | 41 | |
990570ed | 42 | #include <common/common.h> |
d27c42b8 | 43 | #include <common/compat/socket.h> |
e8fa9fb0 | 44 | #include <common/compat/getenv.h> |
db758600 DG |
45 | #include <common/defaults.h> |
46 | #include <common/kernel-consumer/kernel-consumer.h> | |
50c8f484 | 47 | #include <common/futex.h> |
00e2e675 | 48 | #include <common/relayd/relayd.h> |
81b86775 | 49 | #include <common/utils.h> |
3ccdf997 | 50 | #include <common/daemonize.h> |
f40ef1d5 | 51 | #include <common/config/session-config.h> |
dcabc190 | 52 | #include <common/dynamic-buffer.h> |
76fcf151 | 53 | #include <lttng/event-internal.h> |
fac6795d | 54 | |
10a8a223 | 55 | #include "lttng-sessiond.h" |
7972aab2 | 56 | #include "buffer-registry.h" |
54d01ffb | 57 | #include "channel.h" |
2f77fc4b | 58 | #include "cmd.h" |
00e2e675 | 59 | #include "consumer.h" |
099e26bd | 60 | #include "context.h" |
54d01ffb | 61 | #include "event.h" |
4771f025 | 62 | #include "kernel.h" |
f1e16794 | 63 | #include "kernel-consumer.h" |
096102bd | 64 | #include "modprobe.h" |
0fdd1e2c | 65 | #include "shm.h" |
1e307fab | 66 | #include "ust-ctl.h" |
00e2e675 | 67 | #include "ust-consumer.h" |
8e68d1c8 | 68 | #include "utils.h" |
4063050c | 69 | #include "fd-limit.h" |
8782cc74 | 70 | #include "health-sessiond.h" |
8ac94142 | 71 | #include "testpoint.h" |
d0b96690 | 72 | #include "ust-thread.h" |
022d91ba | 73 | #include "agent-thread.h" |
fb198a11 | 74 | #include "save.h" |
ef367a93 | 75 | #include "load-session-thread.h" |
b3530820 JG |
76 | #include "notification-thread.h" |
77 | #include "notification-thread-commands.h" | |
db66e574 | 78 | #include "rotation-thread.h" |
0dbc2034 | 79 | #include "lttng-syscall.h" |
7c1d2758 | 80 | #include "agent.h" |
5e97de00 | 81 | #include "ht-cleanup.h" |
e6142f2e | 82 | #include "sessiond-config.h" |
8e319828 | 83 | #include "timer.h" |
a3707772 | 84 | #include "thread.h" |
917a718d | 85 | #include "client.h" |
5d1b0219 | 86 | #include "dispatch.h" |
ebaeda94 | 87 | |
4fc83d94 PP |
88 | static const char *help_msg = |
89 | #ifdef LTTNG_EMBED_HELP | |
90 | #include <lttng-sessiond.8.h> | |
91 | #else | |
92 | NULL | |
93 | #endif | |
94 | ; | |
95 | ||
fac6795d | 96 | const char *progname; |
c9cb3e7d | 97 | static int lockfile_fd = -1; |
3bd1e081 | 98 | |
0bb7724a DG |
99 | /* Set to 1 when a SIGUSR1 signal is received. */ |
100 | static int recv_child_signal; | |
101 | ||
26296c48 JG |
102 | /* Command line options */ |
103 | static const struct option long_options[] = { | |
0f5ea17c JG |
104 | { "client-sock", required_argument, 0, 'c' }, |
105 | { "apps-sock", required_argument, 0, 'a' }, | |
106 | { "kconsumerd-cmd-sock", required_argument, 0, '\0' }, | |
107 | { "kconsumerd-err-sock", required_argument, 0, '\0' }, | |
108 | { "ustconsumerd32-cmd-sock", required_argument, 0, '\0' }, | |
109 | { "ustconsumerd32-err-sock", required_argument, 0, '\0' }, | |
110 | { "ustconsumerd64-cmd-sock", required_argument, 0, '\0' }, | |
111 | { "ustconsumerd64-err-sock", required_argument, 0, '\0' }, | |
112 | { "consumerd32-path", required_argument, 0, '\0' }, | |
113 | { "consumerd32-libdir", required_argument, 0, '\0' }, | |
114 | { "consumerd64-path", required_argument, 0, '\0' }, | |
115 | { "consumerd64-libdir", required_argument, 0, '\0' }, | |
116 | { "daemonize", no_argument, 0, 'd' }, | |
117 | { "background", no_argument, 0, 'b' }, | |
118 | { "sig-parent", no_argument, 0, 'S' }, | |
119 | { "help", no_argument, 0, 'h' }, | |
120 | { "group", required_argument, 0, 'g' }, | |
121 | { "version", no_argument, 0, 'V' }, | |
122 | { "quiet", no_argument, 0, 'q' }, | |
123 | { "verbose", no_argument, 0, 'v' }, | |
124 | { "verbose-consumer", no_argument, 0, '\0' }, | |
125 | { "no-kernel", no_argument, 0, '\0' }, | |
126 | { "pidfile", required_argument, 0, 'p' }, | |
127 | { "agent-tcp-port", required_argument, 0, '\0' }, | |
128 | { "config", required_argument, 0, 'f' }, | |
129 | { "load", required_argument, 0, 'l' }, | |
130 | { "kmod-probes", required_argument, 0, '\0' }, | |
131 | { "extra-kmod-probes", required_argument, 0, '\0' }, | |
26296c48 JG |
132 | { NULL, 0, 0, 0 } |
133 | }; | |
134 | ||
135 | /* Command line options to ignore from configuration file */ | |
136 | static const char *config_ignore_options[] = { "help", "version", "config" }; | |
a4b35e07 | 137 | static int apps_sock = -1; |
1d4b027a | 138 | |
099e26bd DG |
139 | /* |
140 | * This pipe is used to inform the thread managing application communication | |
141 | * that a command is queued and ready to be processed. | |
142 | */ | |
76d7553f | 143 | static int apps_cmd_pipe[2] = { -1, -1 }; |
099e26bd | 144 | |
1d4b027a | 145 | /* Pthread, Mutexes and Semaphores */ |
1d4b027a | 146 | static pthread_t apps_thread; |
d0b96690 | 147 | static pthread_t apps_notify_thread; |
099e26bd | 148 | static pthread_t reg_apps_thread; |
7a485870 | 149 | static pthread_t kernel_thread; |
022d91ba | 150 | static pthread_t agent_reg_thread; |
ef367a93 | 151 | static pthread_t load_session_thread; |
5eb91c98 | 152 | |
099e26bd DG |
153 | /* |
154 | * UST registration command queue. This queue is tied with a futex and uses a N | |
155 | * wakers / 1 waiter implemented and detailed in futex.c/.h | |
156 | * | |
b22c5da8 DG |
157 | * The thread_registration_apps and thread_dispatch_ust_registration uses this |
158 | * queue along with the wait/wake scheme. The thread_manage_apps receives down | |
159 | * the line new application socket and monitors it for any I/O error or clean | |
160 | * close that triggers an unregistration of the application. | |
099e26bd DG |
161 | */ |
162 | static struct ust_cmd_queue ust_cmd_queue; | |
163 | ||
2f77fc4b DG |
164 | static const char *module_proc_lttng = "/proc/lttng"; |
165 | ||
ef367a93 | 166 | /* Load session thread information to operate. */ |
a7333da7 | 167 | static struct load_session_thread_data *load_info; |
7c1d2758 | 168 | |
97bc1426 | 169 | /* |
a7333da7 | 170 | * Section name to look for in the daemon configuration file. |
97bc1426 | 171 | */ |
a7333da7 | 172 | static const char * const config_section_name = "sessiond"; |
5e97de00 | 173 | |
a7333da7 JG |
174 | /* Am I root or not. Set to 1 if the daemon is running as root */ |
175 | static int is_root; | |
5eb91c98 | 176 | |
099e26bd DG |
177 | /* |
178 | * Stop all threads by closing the thread quit pipe. | |
179 | */ | |
cf3af59e MD |
180 | static void stop_threads(void) |
181 | { | |
5eb91c98 DG |
182 | int ret; |
183 | ||
cf3af59e MD |
184 | /* Stopping all threads */ |
185 | DBG("Terminating all threads"); | |
a7333da7 | 186 | ret = sessiond_notify_quit_pipe(); |
5eb91c98 DG |
187 | if (ret < 0) { |
188 | ERR("write error on thread quit pipe"); | |
189 | } | |
cf3af59e MD |
190 | } |
191 | ||
e975f9f8 DG |
192 | /* |
193 | * Close every consumer sockets. | |
194 | */ | |
195 | static void close_consumer_sockets(void) | |
196 | { | |
197 | int ret; | |
198 | ||
199 | if (kconsumer_data.err_sock >= 0) { | |
200 | ret = close(kconsumer_data.err_sock); | |
201 | if (ret < 0) { | |
202 | PERROR("kernel consumer err_sock close"); | |
203 | } | |
204 | } | |
205 | if (ustconsumer32_data.err_sock >= 0) { | |
206 | ret = close(ustconsumer32_data.err_sock); | |
207 | if (ret < 0) { | |
a76cbd9f | 208 | PERROR("UST consumerd32 err_sock close"); |
e975f9f8 DG |
209 | } |
210 | } | |
211 | if (ustconsumer64_data.err_sock >= 0) { | |
212 | ret = close(ustconsumer64_data.err_sock); | |
213 | if (ret < 0) { | |
a76cbd9f | 214 | PERROR("UST consumerd64 err_sock close"); |
e975f9f8 DG |
215 | } |
216 | } | |
217 | if (kconsumer_data.cmd_sock >= 0) { | |
218 | ret = close(kconsumer_data.cmd_sock); | |
219 | if (ret < 0) { | |
220 | PERROR("kernel consumer cmd_sock close"); | |
221 | } | |
222 | } | |
223 | if (ustconsumer32_data.cmd_sock >= 0) { | |
224 | ret = close(ustconsumer32_data.cmd_sock); | |
225 | if (ret < 0) { | |
a76cbd9f | 226 | PERROR("UST consumerd32 cmd_sock close"); |
e975f9f8 DG |
227 | } |
228 | } | |
229 | if (ustconsumer64_data.cmd_sock >= 0) { | |
230 | ret = close(ustconsumer64_data.cmd_sock); | |
231 | if (ret < 0) { | |
a76cbd9f | 232 | PERROR("UST consumerd64 cmd_sock close"); |
e975f9f8 DG |
233 | } |
234 | } | |
b3530820 JG |
235 | if (kconsumer_data.channel_monitor_pipe >= 0) { |
236 | ret = close(kconsumer_data.channel_monitor_pipe); | |
237 | if (ret < 0) { | |
238 | PERROR("kernel consumer channel monitor pipe close"); | |
239 | } | |
240 | } | |
241 | if (ustconsumer32_data.channel_monitor_pipe >= 0) { | |
242 | ret = close(ustconsumer32_data.channel_monitor_pipe); | |
243 | if (ret < 0) { | |
244 | PERROR("UST consumerd32 channel monitor pipe close"); | |
245 | } | |
246 | } | |
247 | if (ustconsumer64_data.channel_monitor_pipe >= 0) { | |
248 | ret = close(ustconsumer64_data.channel_monitor_pipe); | |
249 | if (ret < 0) { | |
250 | PERROR("UST consumerd64 channel monitor pipe close"); | |
251 | } | |
252 | } | |
e975f9f8 DG |
253 | } |
254 | ||
4e4714cb JR |
255 | /* |
256 | * Wait on consumer process termination. | |
257 | * | |
258 | * Need to be called with the consumer data lock held or from a context | |
259 | * ensuring no concurrent access to data (e.g: cleanup). | |
260 | */ | |
261 | static void wait_consumer(struct consumer_data *consumer_data) | |
262 | { | |
263 | pid_t ret; | |
264 | int status; | |
265 | ||
266 | if (consumer_data->pid <= 0) { | |
267 | return; | |
268 | } | |
269 | ||
270 | DBG("Waiting for complete teardown of consumerd (PID: %d)", | |
271 | consumer_data->pid); | |
272 | ret = waitpid(consumer_data->pid, &status, 0); | |
273 | if (ret == -1) { | |
274 | PERROR("consumerd waitpid pid: %d", consumer_data->pid) | |
1640c24c | 275 | } else if (!WIFEXITED(status)) { |
4e4714cb JR |
276 | ERR("consumerd termination with error: %d", |
277 | WEXITSTATUS(ret)); | |
278 | } | |
279 | consumer_data->pid = 0; | |
280 | } | |
281 | ||
fac6795d | 282 | /* |
4a15001e | 283 | * Cleanup the session daemon's data structures. |
fac6795d | 284 | */ |
4a15001e | 285 | static void sessiond_cleanup(void) |
fac6795d | 286 | { |
ef599319 | 287 | int ret; |
e32d7f27 | 288 | struct ltt_session_list *session_list = session_get_list(); |
fac6795d | 289 | |
4a15001e | 290 | DBG("Cleanup sessiond"); |
e07ae692 | 291 | |
4e449f3f MD |
292 | /* |
293 | * Close the thread quit pipe. It has already done its job, | |
294 | * since we are now called. | |
295 | */ | |
a7333da7 | 296 | sessiond_close_quit_pipe(); |
2f77fc4b | 297 | |
c9a2957d JG |
298 | ret = remove(config.pid_file_path.value); |
299 | if (ret < 0) { | |
300 | PERROR("remove pidfile %s", config.pid_file_path.value); | |
35f90c40 DG |
301 | } |
302 | ||
e6142f2e JG |
303 | DBG("Removing sessiond and consumerd content of directory %s", |
304 | config.rundir.value); | |
8c6c56c2 MD |
305 | |
306 | /* sessiond */ | |
e6142f2e JG |
307 | DBG("Removing %s", config.pid_file_path.value); |
308 | (void) unlink(config.pid_file_path.value); | |
8c6c56c2 | 309 | |
e6142f2e JG |
310 | DBG("Removing %s", config.agent_port_file_path.value); |
311 | (void) unlink(config.agent_port_file_path.value); | |
cd9290dd | 312 | |
8c6c56c2 | 313 | /* kconsumerd */ |
e6142f2e JG |
314 | DBG("Removing %s", kconsumer_data.err_unix_sock_path); |
315 | (void) unlink(kconsumer_data.err_unix_sock_path); | |
316 | ||
317 | DBG("Removing directory %s", config.kconsumerd_path.value); | |
318 | (void) rmdir(config.kconsumerd_path.value); | |
8c6c56c2 MD |
319 | |
320 | /* ust consumerd 32 */ | |
e6142f2e JG |
321 | DBG("Removing %s", config.consumerd32_err_unix_sock_path.value); |
322 | (void) unlink(config.consumerd32_err_unix_sock_path.value); | |
323 | ||
324 | DBG("Removing directory %s", config.consumerd32_path.value); | |
325 | (void) rmdir(config.consumerd32_path.value); | |
8c6c56c2 MD |
326 | |
327 | /* ust consumerd 64 */ | |
e6142f2e JG |
328 | DBG("Removing %s", config.consumerd64_err_unix_sock_path.value); |
329 | (void) unlink(config.consumerd64_err_unix_sock_path.value); | |
330 | ||
331 | DBG("Removing directory %s", config.consumerd64_path.value); | |
332 | (void) rmdir(config.consumerd64_path.value); | |
5461b305 | 333 | |
99d688f2 | 334 | pthread_mutex_destroy(&session_list->lock); |
273ea72c | 335 | |
4e4714cb JR |
336 | wait_consumer(&kconsumer_data); |
337 | wait_consumer(&ustconsumer64_data); | |
338 | wait_consumer(&ustconsumer32_data); | |
339 | ||
6a4e4039 JG |
340 | DBG("Cleaning up all agent apps"); |
341 | agent_app_ht_clean(); | |
342 | ||
099e26bd | 343 | DBG("Closing all UST sockets"); |
56fff090 | 344 | ust_app_clean_list(); |
7972aab2 | 345 | buffer_reg_destroy_registries(); |
099e26bd | 346 | |
e6142f2e | 347 | if (is_root && !config.no_kernel) { |
4fba7219 | 348 | DBG2("Closing kernel fd"); |
a4b35e07 | 349 | if (kernel_tracer_fd >= 0) { |
76d7553f MD |
350 | ret = close(kernel_tracer_fd); |
351 | if (ret) { | |
352 | PERROR("close"); | |
353 | } | |
a4b35e07 | 354 | } |
2f50c8a3 | 355 | DBG("Unloading kernel modules"); |
096102bd | 356 | modprobe_remove_lttng_all(); |
834978fd | 357 | free(syscall_table); |
2f50c8a3 | 358 | } |
2f77fc4b | 359 | |
e975f9f8 DG |
360 | close_consumer_sockets(); |
361 | ||
ef367a93 JG |
362 | if (load_info) { |
363 | load_session_destroy_data(load_info); | |
364 | free(load_info); | |
365 | } | |
366 | ||
c9cb3e7d JG |
367 | /* |
368 | * We do NOT rmdir rundir because there are other processes | |
369 | * using it, for instance lttng-relayd, which can start in | |
370 | * parallel with this teardown. | |
371 | */ | |
4a15001e MD |
372 | } |
373 | ||
374 | /* | |
375 | * Cleanup the daemon's option data structures. | |
376 | */ | |
377 | static void sessiond_cleanup_options(void) | |
378 | { | |
379 | DBG("Cleaning up options"); | |
380 | ||
e6142f2e | 381 | sessiond_config_fini(&config); |
c9cb3e7d | 382 | |
7567352f | 383 | run_as_destroy_worker(); |
fac6795d DG |
384 | } |
385 | ||
fac6795d | 386 | /* |
0fdd1e2c | 387 | * Notify UST applications using the shm mmap futex. |
fac6795d | 388 | */ |
0fdd1e2c | 389 | static int notify_ust_apps(int active) |
fac6795d | 390 | { |
0fdd1e2c | 391 | char *wait_shm_mmap; |
fac6795d | 392 | |
0fdd1e2c | 393 | DBG("Notifying applications of session daemon state: %d", active); |
e07ae692 | 394 | |
0fdd1e2c | 395 | /* See shm.c for this call implying mmap, shm and futex calls */ |
e6142f2e | 396 | wait_shm_mmap = shm_ust_get_mmap(config.wait_shm_path.value, is_root); |
0fdd1e2c | 397 | if (wait_shm_mmap == NULL) { |
fac6795d DG |
398 | goto error; |
399 | } | |
400 | ||
0fdd1e2c DG |
401 | /* Wake waiting process */ |
402 | futex_wait_update((int32_t *) wait_shm_mmap, active); | |
403 | ||
404 | /* Apps notified successfully */ | |
405 | return 0; | |
fac6795d DG |
406 | |
407 | error: | |
0fdd1e2c | 408 | return -1; |
fac6795d DG |
409 | } |
410 | ||
7a485870 | 411 | /* |
5eb91c98 | 412 | * Update the kernel poll set of all channel fd available over all tracing |
d063d709 | 413 | * session. Add the wakeup pipe at the end of the set. |
7a485870 | 414 | */ |
5eb91c98 | 415 | static int update_kernel_poll(struct lttng_poll_event *events) |
7a485870 | 416 | { |
5eb91c98 | 417 | int ret; |
7a485870 | 418 | struct ltt_kernel_channel *channel; |
e32d7f27 JG |
419 | struct ltt_session *session; |
420 | const struct ltt_session_list *session_list = session_get_list(); | |
7a485870 | 421 | |
5eb91c98 | 422 | DBG("Updating kernel poll set"); |
7a485870 | 423 | |
54d01ffb | 424 | session_lock_list(); |
e32d7f27 JG |
425 | cds_list_for_each_entry(session, &session_list->head, list) { |
426 | if (!session_get(session)) { | |
427 | continue; | |
428 | } | |
54d01ffb | 429 | session_lock(session); |
7a485870 | 430 | if (session->kernel_session == NULL) { |
54d01ffb | 431 | session_unlock(session); |
e32d7f27 | 432 | session_put(session); |
7a485870 DG |
433 | continue; |
434 | } | |
7a485870 | 435 | |
54d01ffb DG |
436 | cds_list_for_each_entry(channel, |
437 | &session->kernel_session->channel_list.head, list) { | |
5eb91c98 DG |
438 | /* Add channel fd to the kernel poll set */ |
439 | ret = lttng_poll_add(events, channel->fd, LPOLLIN | LPOLLRDNORM); | |
440 | if (ret < 0) { | |
54d01ffb | 441 | session_unlock(session); |
e32d7f27 | 442 | session_put(session); |
5eb91c98 DG |
443 | goto error; |
444 | } | |
445 | DBG("Channel fd %d added to kernel set", channel->fd); | |
7a485870 | 446 | } |
54d01ffb | 447 | session_unlock(session); |
7a485870 | 448 | } |
54d01ffb | 449 | session_unlock_list(); |
7a485870 | 450 | |
5eb91c98 | 451 | return 0; |
7a485870 DG |
452 | |
453 | error: | |
54d01ffb | 454 | session_unlock_list(); |
7a485870 DG |
455 | return -1; |
456 | } | |
457 | ||
458 | /* | |
54d01ffb | 459 | * Find the channel fd from 'fd' over all tracing session. When found, check |
d063d709 | 460 | * for new channel stream and send those stream fds to the kernel consumer. |
7a485870 | 461 | * |
d063d709 | 462 | * Useful for CPU hotplug feature. |
7a485870 | 463 | */ |
7f032058 | 464 | static int update_kernel_stream(int fd) |
7a485870 DG |
465 | { |
466 | int ret = 0; | |
467 | struct ltt_session *session; | |
173af62f | 468 | struct ltt_kernel_session *ksess; |
7a485870 | 469 | struct ltt_kernel_channel *channel; |
e32d7f27 | 470 | const struct ltt_session_list *session_list = session_get_list(); |
7a485870 DG |
471 | |
472 | DBG("Updating kernel streams for channel fd %d", fd); | |
473 | ||
54d01ffb | 474 | session_lock_list(); |
e32d7f27 JG |
475 | cds_list_for_each_entry(session, &session_list->head, list) { |
476 | if (!session_get(session)) { | |
477 | continue; | |
478 | } | |
54d01ffb | 479 | session_lock(session); |
7a485870 | 480 | if (session->kernel_session == NULL) { |
54d01ffb | 481 | session_unlock(session); |
e32d7f27 | 482 | session_put(session); |
7a485870 DG |
483 | continue; |
484 | } | |
173af62f | 485 | ksess = session->kernel_session; |
d9800920 | 486 | |
4a15001e MD |
487 | cds_list_for_each_entry(channel, |
488 | &ksess->channel_list.head, list) { | |
489 | struct lttng_ht_iter iter; | |
490 | struct consumer_socket *socket; | |
d9800920 | 491 | |
4a15001e MD |
492 | if (channel->fd != fd) { |
493 | continue; | |
494 | } | |
495 | DBG("Channel found, updating kernel streams"); | |
496 | ret = kernel_open_channel_stream(channel); | |
497 | if (ret < 0) { | |
498 | goto error; | |
499 | } | |
500 | /* Update the stream global counter */ | |
501 | ksess->stream_count_global += ret; | |
502 | ||
503 | /* | |
504 | * Have we already sent fds to the consumer? If yes, it | |
505 | * means that tracing is started so it is safe to send | |
506 | * our updated stream fds. | |
507 | */ | |
508 | if (ksess->consumer_fds_sent != 1 | |
509 | || ksess->consumer == NULL) { | |
510 | ret = -1; | |
511 | goto error; | |
512 | } | |
513 | ||
514 | rcu_read_lock(); | |
515 | cds_lfht_for_each_entry(ksess->consumer->socks->ht, | |
516 | &iter.iter, socket, node.node) { | |
517 | pthread_mutex_lock(socket->lock); | |
1fc1b7c8 | 518 | ret = kernel_consumer_send_channel_streams(socket, |
4a15001e MD |
519 | channel, ksess, |
520 | session->output_traces ? 1 : 0); | |
521 | pthread_mutex_unlock(socket->lock); | |
522 | if (ret < 0) { | |
e7fe706f | 523 | rcu_read_unlock(); |
4a15001e | 524 | goto error; |
7a485870 | 525 | } |
7a485870 | 526 | } |
4a15001e | 527 | rcu_read_unlock(); |
7a485870 | 528 | } |
54d01ffb | 529 | session_unlock(session); |
e32d7f27 | 530 | session_put(session); |
7a485870 | 531 | } |
54d01ffb | 532 | session_unlock_list(); |
b3c750d2 | 533 | return ret; |
7a485870 | 534 | |
b3c750d2 | 535 | error: |
54d01ffb | 536 | session_unlock(session); |
e32d7f27 | 537 | session_put(session); |
54d01ffb | 538 | session_unlock_list(); |
7a485870 DG |
539 | return ret; |
540 | } | |
541 | ||
542 | /* | |
d063d709 | 543 | * This thread manage event coming from the kernel. |
7a485870 | 544 | * |
d063d709 DG |
545 | * Features supported in this thread: |
546 | * -) CPU Hotplug | |
7a485870 DG |
547 | */ |
548 | static void *thread_manage_kernel(void *data) | |
549 | { | |
139ac872 | 550 | int ret, i, pollfd, update_poll_flag = 1, err = -1; |
5eb91c98 | 551 | uint32_t revents, nb_fd; |
7a485870 | 552 | char tmp; |
5eb91c98 | 553 | struct lttng_poll_event events; |
7a485870 | 554 | |
6993eeb3 | 555 | DBG("[thread] Thread manage kernel started"); |
7a485870 | 556 | |
6c71277b | 557 | health_register(health_sessiond, HEALTH_SESSIOND_TYPE_KERNEL); |
927ca06a | 558 | |
d5d63bf1 DG |
559 | /* |
560 | * This first step of the while is to clean this structure which could free | |
6d737ce4 | 561 | * non NULL pointers so initialize it before the loop. |
d5d63bf1 | 562 | */ |
6d737ce4 | 563 | lttng_poll_init(&events); |
d5d63bf1 | 564 | |
e547b070 | 565 | if (testpoint(sessiond_thread_manage_kernel)) { |
6993eeb3 CB |
566 | goto error_testpoint; |
567 | } | |
8ac94142 | 568 | |
840cb59c | 569 | health_code_update(); |
44a5e5eb | 570 | |
e547b070 | 571 | if (testpoint(sessiond_thread_manage_kernel_before_loop)) { |
d21b0d71 | 572 | goto error_testpoint; |
6993eeb3 CB |
573 | } |
574 | ||
7a485870 | 575 | while (1) { |
840cb59c | 576 | health_code_update(); |
44a5e5eb | 577 | |
7a485870 | 578 | if (update_poll_flag == 1) { |
d21b0d71 DG |
579 | /* Clean events object. We are about to populate it again. */ |
580 | lttng_poll_clean(&events); | |
581 | ||
d0b96690 | 582 | ret = sessiond_set_thread_pollset(&events, 2); |
d21b0d71 DG |
583 | if (ret < 0) { |
584 | goto error_poll_create; | |
585 | } | |
586 | ||
587 | ret = lttng_poll_add(&events, kernel_poll_pipe[0], LPOLLIN); | |
588 | if (ret < 0) { | |
589 | goto error; | |
590 | } | |
5f822d0a | 591 | |
d21b0d71 | 592 | /* This will add the available kernel channel if any. */ |
5eb91c98 DG |
593 | ret = update_kernel_poll(&events); |
594 | if (ret < 0) { | |
7a485870 DG |
595 | goto error; |
596 | } | |
597 | update_poll_flag = 0; | |
598 | } | |
599 | ||
7fa2082e | 600 | DBG("Thread kernel polling"); |
7a485870 DG |
601 | |
602 | /* Poll infinite value of time */ | |
88f2b785 | 603 | restart: |
a78af745 | 604 | health_poll_entry(); |
5eb91c98 | 605 | ret = lttng_poll_wait(&events, -1); |
7fa2082e MD |
606 | DBG("Thread kernel return from poll on %d fds", |
607 | LTTNG_POLL_GETNB(&events)); | |
a78af745 | 608 | health_poll_exit(); |
7a485870 | 609 | if (ret < 0) { |
88f2b785 MD |
610 | /* |
611 | * Restart interrupted system call. | |
612 | */ | |
613 | if (errno == EINTR) { | |
614 | goto restart; | |
615 | } | |
7a485870 DG |
616 | goto error; |
617 | } else if (ret == 0) { | |
618 | /* Should not happen since timeout is infinite */ | |
85611738 DG |
619 | ERR("Return value of poll is 0 with an infinite timeout.\n" |
620 | "This should not have happened! Continuing..."); | |
7a485870 DG |
621 | continue; |
622 | } | |
623 | ||
0d9c5d77 DG |
624 | nb_fd = ret; |
625 | ||
5eb91c98 DG |
626 | for (i = 0; i < nb_fd; i++) { |
627 | /* Fetch once the poll data */ | |
628 | revents = LTTNG_POLL_GETEV(&events, i); | |
629 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
7a485870 | 630 | |
840cb59c | 631 | health_code_update(); |
44a5e5eb | 632 | |
fd20dac9 MD |
633 | if (!revents) { |
634 | /* No activity for this FD (poll implementation). */ | |
635 | continue; | |
636 | } | |
637 | ||
5eb91c98 | 638 | /* Thread quit pipe has been closed. Killing thread. */ |
d0b96690 | 639 | ret = sessiond_check_thread_quit_pipe(pollfd, revents); |
5eb91c98 | 640 | if (ret) { |
139ac872 MD |
641 | err = 0; |
642 | goto exit; | |
5eb91c98 | 643 | } |
7a485870 | 644 | |
5eb91c98 | 645 | /* Check for data on kernel pipe */ |
03e43155 MD |
646 | if (revents & LPOLLIN) { |
647 | if (pollfd == kernel_poll_pipe[0]) { | |
648 | (void) lttng_read(kernel_poll_pipe[0], | |
649 | &tmp, 1); | |
650 | /* | |
651 | * Ret value is useless here, if this pipe gets any actions an | |
652 | * update is required anyway. | |
653 | */ | |
654 | update_poll_flag = 1; | |
655 | continue; | |
656 | } else { | |
657 | /* | |
658 | * New CPU detected by the kernel. Adding kernel stream to | |
659 | * kernel session and updating the kernel consumer | |
660 | */ | |
7f032058 | 661 | ret = update_kernel_stream(pollfd); |
5eb91c98 DG |
662 | if (ret < 0) { |
663 | continue; | |
664 | } | |
665 | break; | |
7a485870 | 666 | } |
03e43155 MD |
667 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
668 | update_poll_flag = 1; | |
669 | continue; | |
670 | } else { | |
671 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); | |
672 | goto error; | |
7a485870 DG |
673 | } |
674 | } | |
675 | } | |
676 | ||
139ac872 | 677 | exit: |
7a485870 | 678 | error: |
5eb91c98 | 679 | lttng_poll_clean(&events); |
76d7553f | 680 | error_poll_create: |
6993eeb3 | 681 | error_testpoint: |
6620da75 DG |
682 | utils_close_pipe(kernel_poll_pipe); |
683 | kernel_poll_pipe[0] = kernel_poll_pipe[1] = -1; | |
139ac872 | 684 | if (err) { |
840cb59c | 685 | health_error(); |
139ac872 | 686 | ERR("Health error occurred in %s", __func__); |
6620da75 DG |
687 | WARN("Kernel thread died unexpectedly. " |
688 | "Kernel tracing can continue but CPU hotplug is disabled."); | |
139ac872 | 689 | } |
8782cc74 | 690 | health_unregister(health_sessiond); |
76d7553f | 691 | DBG("Kernel thread dying"); |
7a485870 DG |
692 | return NULL; |
693 | } | |
694 | ||
a23ec3a7 DG |
695 | /* |
696 | * Signal pthread condition of the consumer data that the thread. | |
697 | */ | |
698 | static void signal_consumer_condition(struct consumer_data *data, int state) | |
699 | { | |
700 | pthread_mutex_lock(&data->cond_mutex); | |
701 | ||
702 | /* | |
703 | * The state is set before signaling. It can be any value, it's the waiter | |
704 | * job to correctly interpret this condition variable associated to the | |
705 | * consumer pthread_cond. | |
706 | * | |
707 | * A value of 0 means that the corresponding thread of the consumer data | |
708 | * was not started. 1 indicates that the thread has started and is ready | |
709 | * for action. A negative value means that there was an error during the | |
710 | * thread bootstrap. | |
711 | */ | |
712 | data->consumer_thread_is_ready = state; | |
713 | (void) pthread_cond_signal(&data->cond); | |
714 | ||
715 | pthread_mutex_unlock(&data->cond_mutex); | |
716 | } | |
717 | ||
1d4b027a | 718 | /* |
3bd1e081 | 719 | * This thread manage the consumer error sent back to the session daemon. |
1d4b027a | 720 | */ |
917a718d | 721 | void *thread_manage_consumer(void *data) |
1d4b027a | 722 | { |
42fc1d0b | 723 | int sock = -1, i, ret, pollfd, err = -1, should_quit = 0; |
5eb91c98 | 724 | uint32_t revents, nb_fd; |
1d4b027a | 725 | enum lttcomm_return_code code; |
5eb91c98 | 726 | struct lttng_poll_event events; |
3bd1e081 | 727 | struct consumer_data *consumer_data = data; |
b3530820 | 728 | struct consumer_socket *cmd_socket_wrapper = NULL; |
1d4b027a | 729 | |
3bd1e081 | 730 | DBG("[thread] Manage consumer started"); |
1d4b027a | 731 | |
34c1e15a MD |
732 | rcu_register_thread(); |
733 | rcu_thread_online(); | |
734 | ||
6c71277b | 735 | health_register(health_sessiond, HEALTH_SESSIOND_TYPE_CONSUMER); |
927ca06a | 736 | |
855060f8 | 737 | health_code_update(); |
9449cc75 | 738 | |
5eb91c98 | 739 | /* |
331744e3 JD |
740 | * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the |
741 | * metadata_sock. Nothing more will be added to this poll set. | |
5eb91c98 | 742 | */ |
331744e3 | 743 | ret = sessiond_set_thread_pollset(&events, 3); |
5eb91c98 | 744 | if (ret < 0) { |
76d7553f | 745 | goto error_poll; |
5eb91c98 | 746 | } |
273ea72c | 747 | |
edb8b045 DG |
748 | /* |
749 | * The error socket here is already in a listening state which was done | |
750 | * just before spawning this thread to avoid a race between the consumer | |
751 | * daemon exec trying to connect and the listen() call. | |
752 | */ | |
3bd1e081 | 753 | ret = lttng_poll_add(&events, consumer_data->err_sock, LPOLLIN | LPOLLRDHUP); |
5eb91c98 DG |
754 | if (ret < 0) { |
755 | goto error; | |
756 | } | |
757 | ||
840cb59c | 758 | health_code_update(); |
44a5e5eb | 759 | |
331744e3 | 760 | /* Infinite blocking call, waiting for transmission */ |
88f2b785 | 761 | restart: |
a78af745 | 762 | health_poll_entry(); |
8ac94142 | 763 | |
e547b070 | 764 | if (testpoint(sessiond_thread_manage_consumer)) { |
6993eeb3 CB |
765 | goto error; |
766 | } | |
8ac94142 | 767 | |
5eb91c98 | 768 | ret = lttng_poll_wait(&events, -1); |
a78af745 | 769 | health_poll_exit(); |
273ea72c | 770 | if (ret < 0) { |
88f2b785 MD |
771 | /* |
772 | * Restart interrupted system call. | |
773 | */ | |
774 | if (errno == EINTR) { | |
775 | goto restart; | |
776 | } | |
273ea72c DG |
777 | goto error; |
778 | } | |
779 | ||
0d9c5d77 DG |
780 | nb_fd = ret; |
781 | ||
5eb91c98 DG |
782 | for (i = 0; i < nb_fd; i++) { |
783 | /* Fetch once the poll data */ | |
784 | revents = LTTNG_POLL_GETEV(&events, i); | |
785 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
786 | ||
840cb59c | 787 | health_code_update(); |
44a5e5eb | 788 | |
fd20dac9 MD |
789 | if (!revents) { |
790 | /* No activity for this FD (poll implementation). */ | |
791 | continue; | |
792 | } | |
793 | ||
5eb91c98 | 794 | /* Thread quit pipe has been closed. Killing thread. */ |
d0b96690 | 795 | ret = sessiond_check_thread_quit_pipe(pollfd, revents); |
5eb91c98 | 796 | if (ret) { |
139ac872 MD |
797 | err = 0; |
798 | goto exit; | |
5eb91c98 DG |
799 | } |
800 | ||
801 | /* Event on the registration socket */ | |
3bd1e081 | 802 | if (pollfd == consumer_data->err_sock) { |
03e43155 MD |
803 | if (revents & LPOLLIN) { |
804 | continue; | |
805 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
3bd1e081 | 806 | ERR("consumer err socket poll error"); |
5eb91c98 | 807 | goto error; |
03e43155 MD |
808 | } else { |
809 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); | |
810 | goto error; | |
5eb91c98 DG |
811 | } |
812 | } | |
273ea72c DG |
813 | } |
814 | ||
3bd1e081 | 815 | sock = lttcomm_accept_unix_sock(consumer_data->err_sock); |
1d4b027a DG |
816 | if (sock < 0) { |
817 | goto error; | |
818 | } | |
819 | ||
b662582b DG |
820 | /* |
821 | * Set the CLOEXEC flag. Return code is useless because either way, the | |
822 | * show must go on. | |
823 | */ | |
824 | (void) utils_set_fd_cloexec(sock); | |
825 | ||
840cb59c | 826 | health_code_update(); |
44a5e5eb | 827 | |
3bd1e081 | 828 | DBG2("Receiving code from consumer err_sock"); |
ee0b0061 | 829 | |
712ea556 | 830 | /* Getting status code from kconsumerd */ |
54d01ffb DG |
831 | ret = lttcomm_recv_unix_sock(sock, &code, |
832 | sizeof(enum lttcomm_return_code)); | |
1d4b027a DG |
833 | if (ret <= 0) { |
834 | goto error; | |
835 | } | |
836 | ||
840cb59c | 837 | health_code_update(); |
b3530820 | 838 | if (code != LTTCOMM_CONSUMERD_COMMAND_SOCK_READY) { |
3bd1e081 | 839 | ERR("consumer error when waiting for SOCK_READY : %s", |
1d4b027a DG |
840 | lttcomm_get_readable_code(-code)); |
841 | goto error; | |
842 | } | |
843 | ||
b3530820 JG |
844 | /* Connect both command and metadata sockets. */ |
845 | consumer_data->cmd_sock = | |
846 | lttcomm_connect_unix_sock( | |
847 | consumer_data->cmd_unix_sock_path); | |
848 | consumer_data->metadata_fd = | |
849 | lttcomm_connect_unix_sock( | |
850 | consumer_data->cmd_unix_sock_path); | |
851 | if (consumer_data->cmd_sock < 0 || consumer_data->metadata_fd < 0) { | |
852 | PERROR("consumer connect cmd socket"); | |
853 | /* On error, signal condition and quit. */ | |
854 | signal_consumer_condition(consumer_data, -1); | |
855 | goto error; | |
856 | } | |
857 | ||
858 | consumer_data->metadata_sock.fd_ptr = &consumer_data->metadata_fd; | |
859 | ||
860 | /* Create metadata socket lock. */ | |
861 | consumer_data->metadata_sock.lock = zmalloc(sizeof(pthread_mutex_t)); | |
862 | if (consumer_data->metadata_sock.lock == NULL) { | |
863 | PERROR("zmalloc pthread mutex"); | |
864 | goto error; | |
865 | } | |
866 | pthread_mutex_init(consumer_data->metadata_sock.lock, NULL); | |
867 | ||
868 | DBG("Consumer command socket ready (fd: %d", consumer_data->cmd_sock); | |
869 | DBG("Consumer metadata socket ready (fd: %d)", | |
870 | consumer_data->metadata_fd); | |
871 | ||
872 | /* | |
873 | * Remove the consumerd error sock since we've established a connection. | |
874 | */ | |
3bd1e081 | 875 | ret = lttng_poll_del(&events, consumer_data->err_sock); |
72079cae | 876 | if (ret < 0) { |
72079cae DG |
877 | goto error; |
878 | } | |
879 | ||
331744e3 | 880 | /* Add new accepted error socket. */ |
5eb91c98 DG |
881 | ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLRDHUP); |
882 | if (ret < 0) { | |
72079cae | 883 | goto error; |
5eb91c98 DG |
884 | } |
885 | ||
331744e3 | 886 | /* Add metadata socket that is successfully connected. */ |
4ce514c4 | 887 | ret = lttng_poll_add(&events, consumer_data->metadata_fd, |
331744e3 JD |
888 | LPOLLIN | LPOLLRDHUP); |
889 | if (ret < 0) { | |
890 | goto error; | |
891 | } | |
892 | ||
840cb59c | 893 | health_code_update(); |
44a5e5eb | 894 | |
b3530820 | 895 | /* |
62c43103 | 896 | * Transfer the write-end of the channel monitoring and rotate pipe |
92816cc3 | 897 | * to the consumer by issuing a SET_CHANNEL_MONITOR_PIPE command. |
b3530820 JG |
898 | */ |
899 | cmd_socket_wrapper = consumer_allocate_socket(&consumer_data->cmd_sock); | |
900 | if (!cmd_socket_wrapper) { | |
901 | goto error; | |
902 | } | |
3e4dc117 | 903 | cmd_socket_wrapper->lock = &consumer_data->lock; |
b3530820 JG |
904 | |
905 | ret = consumer_send_channel_monitor_pipe(cmd_socket_wrapper, | |
906 | consumer_data->channel_monitor_pipe); | |
907 | if (ret) { | |
908 | goto error; | |
909 | } | |
62c43103 | 910 | |
b3530820 JG |
911 | /* Discard the socket wrapper as it is no longer needed. */ |
912 | consumer_destroy_socket(cmd_socket_wrapper); | |
913 | cmd_socket_wrapper = NULL; | |
914 | ||
915 | /* The thread is completely initialized, signal that it is ready. */ | |
916 | signal_consumer_condition(consumer_data, 1); | |
917 | ||
331744e3 | 918 | /* Infinite blocking call, waiting for transmission */ |
88f2b785 | 919 | restart_poll: |
331744e3 | 920 | while (1) { |
42fc1d0b DG |
921 | health_code_update(); |
922 | ||
923 | /* Exit the thread because the thread quit pipe has been triggered. */ | |
924 | if (should_quit) { | |
925 | /* Not a health error. */ | |
926 | err = 0; | |
927 | goto exit; | |
928 | } | |
929 | ||
331744e3 JD |
930 | health_poll_entry(); |
931 | ret = lttng_poll_wait(&events, -1); | |
932 | health_poll_exit(); | |
933 | if (ret < 0) { | |
934 | /* | |
935 | * Restart interrupted system call. | |
936 | */ | |
937 | if (errno == EINTR) { | |
938 | goto restart_poll; | |
939 | } | |
940 | goto error; | |
88f2b785 | 941 | } |
72079cae | 942 | |
331744e3 | 943 | nb_fd = ret; |
0d9c5d77 | 944 | |
331744e3 JD |
945 | for (i = 0; i < nb_fd; i++) { |
946 | /* Fetch once the poll data */ | |
947 | revents = LTTNG_POLL_GETEV(&events, i); | |
948 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
5eb91c98 | 949 | |
331744e3 | 950 | health_code_update(); |
44a5e5eb | 951 | |
fd20dac9 MD |
952 | if (!revents) { |
953 | /* No activity for this FD (poll implementation). */ | |
954 | continue; | |
955 | } | |
956 | ||
42fc1d0b DG |
957 | /* |
958 | * Thread quit pipe has been triggered, flag that we should stop | |
959 | * but continue the current loop to handle potential data from | |
960 | * consumer. | |
961 | */ | |
962 | should_quit = sessiond_check_thread_quit_pipe(pollfd, revents); | |
5eb91c98 | 963 | |
331744e3 JD |
964 | if (pollfd == sock) { |
965 | /* Event on the consumerd socket */ | |
03e43155 MD |
966 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP) |
967 | && !(revents & LPOLLIN)) { | |
331744e3 JD |
968 | ERR("consumer err socket second poll error"); |
969 | goto error; | |
970 | } | |
971 | health_code_update(); | |
972 | /* Wait for any kconsumerd error */ | |
973 | ret = lttcomm_recv_unix_sock(sock, &code, | |
974 | sizeof(enum lttcomm_return_code)); | |
975 | if (ret <= 0) { | |
976 | ERR("consumer closed the command socket"); | |
977 | goto error; | |
978 | } | |
979 | ||
980 | ERR("consumer return code : %s", | |
981 | lttcomm_get_readable_code(-code)); | |
982 | ||
983 | goto exit; | |
4ce514c4 | 984 | } else if (pollfd == consumer_data->metadata_fd) { |
03e43155 MD |
985 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP) |
986 | && !(revents & LPOLLIN)) { | |
987 | ERR("consumer err metadata socket second poll error"); | |
988 | goto error; | |
989 | } | |
331744e3 JD |
990 | /* UST metadata requests */ |
991 | ret = ust_consumer_metadata_request( | |
992 | &consumer_data->metadata_sock); | |
993 | if (ret < 0) { | |
994 | ERR("Handling metadata request"); | |
995 | goto error; | |
996 | } | |
5eb91c98 | 997 | } |
42fc1d0b | 998 | /* No need for an else branch all FDs are tested prior. */ |
5eb91c98 | 999 | } |
331744e3 | 1000 | health_code_update(); |
5eb91c98 DG |
1001 | } |
1002 | ||
139ac872 | 1003 | exit: |
1d4b027a | 1004 | error: |
fdadac08 DG |
1005 | /* |
1006 | * We lock here because we are about to close the sockets and some other | |
92db7cdc DG |
1007 | * thread might be using them so get exclusive access which will abort all |
1008 | * other consumer command by other threads. | |
fdadac08 DG |
1009 | */ |
1010 | pthread_mutex_lock(&consumer_data->lock); | |
1011 | ||
5c827ce0 DG |
1012 | /* Immediately set the consumerd state to stopped */ |
1013 | if (consumer_data->type == LTTNG_CONSUMER_KERNEL) { | |
1014 | uatomic_set(&kernel_consumerd_state, CONSUMER_ERROR); | |
1015 | } else if (consumer_data->type == LTTNG_CONSUMER64_UST || | |
1016 | consumer_data->type == LTTNG_CONSUMER32_UST) { | |
1017 | uatomic_set(&ust_consumerd_state, CONSUMER_ERROR); | |
1018 | } else { | |
1019 | /* Code flow error... */ | |
1020 | assert(0); | |
1021 | } | |
1022 | ||
76d7553f MD |
1023 | if (consumer_data->err_sock >= 0) { |
1024 | ret = close(consumer_data->err_sock); | |
1025 | if (ret) { | |
1026 | PERROR("close"); | |
1027 | } | |
a76cbd9f | 1028 | consumer_data->err_sock = -1; |
76d7553f MD |
1029 | } |
1030 | if (consumer_data->cmd_sock >= 0) { | |
1031 | ret = close(consumer_data->cmd_sock); | |
1032 | if (ret) { | |
1033 | PERROR("close"); | |
1034 | } | |
a76cbd9f | 1035 | consumer_data->cmd_sock = -1; |
76d7553f | 1036 | } |
96544455 SS |
1037 | if (consumer_data->metadata_sock.fd_ptr && |
1038 | *consumer_data->metadata_sock.fd_ptr >= 0) { | |
9363801e | 1039 | ret = close(*consumer_data->metadata_sock.fd_ptr); |
331744e3 JD |
1040 | if (ret) { |
1041 | PERROR("close"); | |
1042 | } | |
1043 | } | |
76d7553f MD |
1044 | if (sock >= 0) { |
1045 | ret = close(sock); | |
1046 | if (ret) { | |
1047 | PERROR("close"); | |
1048 | } | |
1049 | } | |
273ea72c | 1050 | |
3bd1e081 MD |
1051 | unlink(consumer_data->err_unix_sock_path); |
1052 | unlink(consumer_data->cmd_unix_sock_path); | |
fdadac08 | 1053 | pthread_mutex_unlock(&consumer_data->lock); |
92db7cdc | 1054 | |
fdadac08 | 1055 | /* Cleanup metadata socket mutex. */ |
96544455 SS |
1056 | if (consumer_data->metadata_sock.lock) { |
1057 | pthread_mutex_destroy(consumer_data->metadata_sock.lock); | |
1058 | free(consumer_data->metadata_sock.lock); | |
1059 | } | |
5eb91c98 | 1060 | lttng_poll_clean(&events); |
b3530820 JG |
1061 | |
1062 | if (cmd_socket_wrapper) { | |
1063 | consumer_destroy_socket(cmd_socket_wrapper); | |
1064 | } | |
76d7553f | 1065 | error_poll: |
139ac872 | 1066 | if (err) { |
840cb59c | 1067 | health_error(); |
139ac872 MD |
1068 | ERR("Health error occurred in %s", __func__); |
1069 | } | |
8782cc74 | 1070 | health_unregister(health_sessiond); |
76d7553f | 1071 | DBG("consumer thread cleanup completed"); |
0177d773 | 1072 | |
34c1e15a MD |
1073 | rcu_thread_offline(); |
1074 | rcu_unregister_thread(); | |
1075 | ||
5eb91c98 | 1076 | return NULL; |
099e26bd DG |
1077 | } |
1078 | ||
099e26bd | 1079 | /* |
81f04d5f JG |
1080 | * This thread receives application command sockets (FDs) on the |
1081 | * apps_cmd_pipe and waits (polls) on them until they are closed | |
1082 | * or an error occurs. | |
1083 | * | |
1084 | * At that point, it flushes the data (tracing and metadata) associated | |
1085 | * with this application and tears down ust app sessions and other | |
1086 | * associated data structures through ust_app_unregister(). | |
1087 | * | |
1088 | * Note that this thread never sends commands to the applications | |
1089 | * through the command sockets; it merely listens for hang-ups | |
1090 | * and errors on those sockets and cleans-up as they occur. | |
1d4b027a DG |
1091 | */ |
1092 | static void *thread_manage_apps(void *data) | |
099e26bd | 1093 | { |
139ac872 | 1094 | int i, ret, pollfd, err = -1; |
6cd525e8 | 1095 | ssize_t size_ret; |
5eb91c98 | 1096 | uint32_t revents, nb_fd; |
5eb91c98 | 1097 | struct lttng_poll_event events; |
099e26bd DG |
1098 | |
1099 | DBG("[thread] Manage application started"); | |
1100 | ||
f6a9efaa DG |
1101 | rcu_register_thread(); |
1102 | rcu_thread_online(); | |
1103 | ||
6c71277b | 1104 | health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_MANAGE); |
927ca06a | 1105 | |
e547b070 | 1106 | if (testpoint(sessiond_thread_manage_apps)) { |
6993eeb3 CB |
1107 | goto error_testpoint; |
1108 | } | |
1109 | ||
840cb59c | 1110 | health_code_update(); |
44a5e5eb | 1111 | |
d0b96690 | 1112 | ret = sessiond_set_thread_pollset(&events, 2); |
5eb91c98 | 1113 | if (ret < 0) { |
76d7553f | 1114 | goto error_poll_create; |
5eb91c98 | 1115 | } |
099e26bd | 1116 | |
5eb91c98 DG |
1117 | ret = lttng_poll_add(&events, apps_cmd_pipe[0], LPOLLIN | LPOLLRDHUP); |
1118 | if (ret < 0) { | |
1119 | goto error; | |
1120 | } | |
099e26bd | 1121 | |
e547b070 | 1122 | if (testpoint(sessiond_thread_manage_apps_before_loop)) { |
6993eeb3 CB |
1123 | goto error; |
1124 | } | |
8ac94142 | 1125 | |
840cb59c | 1126 | health_code_update(); |
44a5e5eb | 1127 | |
5eb91c98 | 1128 | while (1) { |
7fa2082e | 1129 | DBG("Apps thread polling"); |
099e26bd DG |
1130 | |
1131 | /* Inifinite blocking call, waiting for transmission */ | |
88f2b785 | 1132 | restart: |
a78af745 | 1133 | health_poll_entry(); |
5eb91c98 | 1134 | ret = lttng_poll_wait(&events, -1); |
7fa2082e MD |
1135 | DBG("Apps thread return from poll on %d fds", |
1136 | LTTNG_POLL_GETNB(&events)); | |
a78af745 | 1137 | health_poll_exit(); |
099e26bd | 1138 | if (ret < 0) { |
88f2b785 MD |
1139 | /* |
1140 | * Restart interrupted system call. | |
1141 | */ | |
1142 | if (errno == EINTR) { | |
1143 | goto restart; | |
1144 | } | |
099e26bd DG |
1145 | goto error; |
1146 | } | |
1147 | ||
0d9c5d77 DG |
1148 | nb_fd = ret; |
1149 | ||
5eb91c98 DG |
1150 | for (i = 0; i < nb_fd; i++) { |
1151 | /* Fetch once the poll data */ | |
1152 | revents = LTTNG_POLL_GETEV(&events, i); | |
1153 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
1154 | ||
840cb59c | 1155 | health_code_update(); |
44a5e5eb | 1156 | |
fd20dac9 MD |
1157 | if (!revents) { |
1158 | /* No activity for this FD (poll implementation). */ | |
1159 | continue; | |
1160 | } | |
1161 | ||
5eb91c98 | 1162 | /* Thread quit pipe has been closed. Killing thread. */ |
d0b96690 | 1163 | ret = sessiond_check_thread_quit_pipe(pollfd, revents); |
5eb91c98 | 1164 | if (ret) { |
139ac872 MD |
1165 | err = 0; |
1166 | goto exit; | |
5eb91c98 | 1167 | } |
099e26bd | 1168 | |
5eb91c98 DG |
1169 | /* Inspect the apps cmd pipe */ |
1170 | if (pollfd == apps_cmd_pipe[0]) { | |
03e43155 | 1171 | if (revents & LPOLLIN) { |
d0b96690 DG |
1172 | int sock; |
1173 | ||
5eb91c98 | 1174 | /* Empty pipe */ |
6cd525e8 MD |
1175 | size_ret = lttng_read(apps_cmd_pipe[0], &sock, sizeof(sock)); |
1176 | if (size_ret < sizeof(sock)) { | |
76d7553f | 1177 | PERROR("read apps cmd pipe"); |
5eb91c98 DG |
1178 | goto error; |
1179 | } | |
099e26bd | 1180 | |
840cb59c | 1181 | health_code_update(); |
44a5e5eb | 1182 | |
ffe60014 | 1183 | /* |
03e43155 MD |
1184 | * Since this is a command socket (write then read), |
1185 | * we only monitor the error events of the socket. | |
ffe60014 | 1186 | */ |
d0b96690 DG |
1187 | ret = lttng_poll_add(&events, sock, |
1188 | LPOLLERR | LPOLLHUP | LPOLLRDHUP); | |
1189 | if (ret < 0) { | |
5eb91c98 | 1190 | goto error; |
e0c7ec2b | 1191 | } |
acc7b41b | 1192 | |
d0b96690 | 1193 | DBG("Apps with sock %d added to poll set", sock); |
03e43155 MD |
1194 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
1195 | ERR("Apps command pipe error"); | |
1196 | goto error; | |
1197 | } else { | |
1198 | ERR("Unknown poll events %u for sock %d", revents, pollfd); | |
1199 | goto error; | |
0177d773 | 1200 | } |
5eb91c98 DG |
1201 | } else { |
1202 | /* | |
54d01ffb DG |
1203 | * At this point, we know that a registered application made |
1204 | * the event at poll_wait. | |
5eb91c98 DG |
1205 | */ |
1206 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
1207 | /* Removing from the poll set */ | |
1208 | ret = lttng_poll_del(&events, pollfd); | |
1209 | if (ret < 0) { | |
1210 | goto error; | |
1211 | } | |
099e26bd | 1212 | |
b9d9b220 | 1213 | /* Socket closed on remote end. */ |
56fff090 | 1214 | ust_app_unregister(pollfd); |
03e43155 MD |
1215 | } else { |
1216 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); | |
1217 | goto error; | |
5eb91c98 | 1218 | } |
099e26bd | 1219 | } |
44a5e5eb | 1220 | |
840cb59c | 1221 | health_code_update(); |
099e26bd | 1222 | } |
099e26bd DG |
1223 | } |
1224 | ||
139ac872 | 1225 | exit: |
099e26bd | 1226 | error: |
5eb91c98 | 1227 | lttng_poll_clean(&events); |
76d7553f | 1228 | error_poll_create: |
6993eeb3 | 1229 | error_testpoint: |
6620da75 DG |
1230 | utils_close_pipe(apps_cmd_pipe); |
1231 | apps_cmd_pipe[0] = apps_cmd_pipe[1] = -1; | |
1232 | ||
1233 | /* | |
1234 | * We don't clean the UST app hash table here since already registered | |
1235 | * applications can still be controlled so let them be until the session | |
1236 | * daemon dies or the applications stop. | |
1237 | */ | |
1238 | ||
139ac872 | 1239 | if (err) { |
840cb59c | 1240 | health_error(); |
139ac872 MD |
1241 | ERR("Health error occurred in %s", __func__); |
1242 | } | |
8782cc74 | 1243 | health_unregister(health_sessiond); |
76d7553f | 1244 | DBG("Application communication apps thread cleanup complete"); |
f6a9efaa DG |
1245 | rcu_thread_offline(); |
1246 | rcu_unregister_thread(); | |
099e26bd DG |
1247 | return NULL; |
1248 | } | |
1249 | ||
099e26bd DG |
1250 | /* |
1251 | * This thread manage application registration. | |
1252 | */ | |
1253 | static void *thread_registration_apps(void *data) | |
1d4b027a | 1254 | { |
139ac872 | 1255 | int sock = -1, i, ret, pollfd, err = -1; |
5eb91c98 DG |
1256 | uint32_t revents, nb_fd; |
1257 | struct lttng_poll_event events; | |
099e26bd DG |
1258 | /* |
1259 | * Get allocated in this thread, enqueued to a global queue, dequeued and | |
1260 | * freed in the manage apps thread. | |
1261 | */ | |
1262 | struct ust_command *ust_cmd = NULL; | |
1d4b027a | 1263 | |
099e26bd | 1264 | DBG("[thread] Manage application registration started"); |
1d4b027a | 1265 | |
6c71277b | 1266 | health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_REG); |
927ca06a | 1267 | |
e547b070 | 1268 | if (testpoint(sessiond_thread_registration_apps)) { |
6993eeb3 CB |
1269 | goto error_testpoint; |
1270 | } | |
8ac94142 | 1271 | |
1d4b027a DG |
1272 | ret = lttcomm_listen_unix_sock(apps_sock); |
1273 | if (ret < 0) { | |
76d7553f | 1274 | goto error_listen; |
1d4b027a DG |
1275 | } |
1276 | ||
5eb91c98 DG |
1277 | /* |
1278 | * Pass 2 as size here for the thread quit pipe and apps socket. Nothing | |
1279 | * more will be added to this poll set. | |
1280 | */ | |
d0b96690 | 1281 | ret = sessiond_set_thread_pollset(&events, 2); |
5eb91c98 | 1282 | if (ret < 0) { |
76d7553f | 1283 | goto error_create_poll; |
5eb91c98 | 1284 | } |
273ea72c | 1285 | |
5eb91c98 DG |
1286 | /* Add the application registration socket */ |
1287 | ret = lttng_poll_add(&events, apps_sock, LPOLLIN | LPOLLRDHUP); | |
1288 | if (ret < 0) { | |
76d7553f | 1289 | goto error_poll_add; |
5eb91c98 | 1290 | } |
273ea72c | 1291 | |
1d4b027a | 1292 | /* Notify all applications to register */ |
0fdd1e2c DG |
1293 | ret = notify_ust_apps(1); |
1294 | if (ret < 0) { | |
1295 | ERR("Failed to notify applications or create the wait shared memory.\n" | |
54d01ffb DG |
1296 | "Execution continues but there might be problem for already\n" |
1297 | "running applications that wishes to register."); | |
0fdd1e2c | 1298 | } |
1d4b027a DG |
1299 | |
1300 | while (1) { | |
1301 | DBG("Accepting application registration"); | |
273ea72c DG |
1302 | |
1303 | /* Inifinite blocking call, waiting for transmission */ | |
88f2b785 | 1304 | restart: |
a78af745 | 1305 | health_poll_entry(); |
5eb91c98 | 1306 | ret = lttng_poll_wait(&events, -1); |
a78af745 | 1307 | health_poll_exit(); |
273ea72c | 1308 | if (ret < 0) { |
88f2b785 MD |
1309 | /* |
1310 | * Restart interrupted system call. | |
1311 | */ | |
1312 | if (errno == EINTR) { | |
1313 | goto restart; | |
1314 | } | |
273ea72c DG |
1315 | goto error; |
1316 | } | |
1317 | ||
0d9c5d77 DG |
1318 | nb_fd = ret; |
1319 | ||
5eb91c98 | 1320 | for (i = 0; i < nb_fd; i++) { |
840cb59c | 1321 | health_code_update(); |
139ac872 | 1322 | |
5eb91c98 DG |
1323 | /* Fetch once the poll data */ |
1324 | revents = LTTNG_POLL_GETEV(&events, i); | |
1325 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
273ea72c | 1326 | |
fd20dac9 MD |
1327 | if (!revents) { |
1328 | /* No activity for this FD (poll implementation). */ | |
1329 | continue; | |
1330 | } | |
1331 | ||
5eb91c98 | 1332 | /* Thread quit pipe has been closed. Killing thread. */ |
d0b96690 | 1333 | ret = sessiond_check_thread_quit_pipe(pollfd, revents); |
5eb91c98 | 1334 | if (ret) { |
139ac872 MD |
1335 | err = 0; |
1336 | goto exit; | |
90014c57 | 1337 | } |
1d4b027a | 1338 | |
5eb91c98 DG |
1339 | /* Event on the registration socket */ |
1340 | if (pollfd == apps_sock) { | |
03e43155 | 1341 | if (revents & LPOLLIN) { |
5eb91c98 DG |
1342 | sock = lttcomm_accept_unix_sock(apps_sock); |
1343 | if (sock < 0) { | |
1344 | goto error; | |
1345 | } | |
099e26bd | 1346 | |
16c5c8fa DG |
1347 | /* |
1348 | * Set socket timeout for both receiving and ending. | |
1349 | * app_socket_timeout is in seconds, whereas | |
1350 | * lttcomm_setsockopt_rcv_timeout and | |
1351 | * lttcomm_setsockopt_snd_timeout expect msec as | |
1352 | * parameter. | |
1353 | */ | |
e6142f2e | 1354 | if (config.app_socket_timeout >= 0) { |
28ce0ff2 | 1355 | (void) lttcomm_setsockopt_rcv_timeout(sock, |
e6142f2e | 1356 | config.app_socket_timeout * 1000); |
28ce0ff2 | 1357 | (void) lttcomm_setsockopt_snd_timeout(sock, |
e6142f2e | 1358 | config.app_socket_timeout * 1000); |
28ce0ff2 | 1359 | } |
16c5c8fa | 1360 | |
b662582b DG |
1361 | /* |
1362 | * Set the CLOEXEC flag. Return code is useless because | |
1363 | * either way, the show must go on. | |
1364 | */ | |
1365 | (void) utils_set_fd_cloexec(sock); | |
1366 | ||
5eb91c98 | 1367 | /* Create UST registration command for enqueuing */ |
ba7f0ae5 | 1368 | ust_cmd = zmalloc(sizeof(struct ust_command)); |
5eb91c98 | 1369 | if (ust_cmd == NULL) { |
76d7553f | 1370 | PERROR("ust command zmalloc"); |
41ed8e47 MD |
1371 | ret = close(sock); |
1372 | if (ret) { | |
1373 | PERROR("close"); | |
1374 | } | |
5eb91c98 DG |
1375 | goto error; |
1376 | } | |
1d4b027a | 1377 | |
5eb91c98 DG |
1378 | /* |
1379 | * Using message-based transmissions to ensure we don't | |
1380 | * have to deal with partially received messages. | |
1381 | */ | |
4063050c MD |
1382 | ret = lttng_fd_get(LTTNG_FD_APPS, 1); |
1383 | if (ret < 0) { | |
1384 | ERR("Exhausted file descriptors allowed for applications."); | |
1385 | free(ust_cmd); | |
1386 | ret = close(sock); | |
1387 | if (ret) { | |
1388 | PERROR("close"); | |
1389 | } | |
1390 | sock = -1; | |
1391 | continue; | |
1392 | } | |
d88aee68 | 1393 | |
840cb59c | 1394 | health_code_update(); |
d0b96690 DG |
1395 | ret = ust_app_recv_registration(sock, &ust_cmd->reg_msg); |
1396 | if (ret < 0) { | |
5eb91c98 | 1397 | free(ust_cmd); |
d0b96690 | 1398 | /* Close socket of the application. */ |
76d7553f MD |
1399 | ret = close(sock); |
1400 | if (ret) { | |
1401 | PERROR("close"); | |
1402 | } | |
4063050c | 1403 | lttng_fd_put(LTTNG_FD_APPS, 1); |
76d7553f | 1404 | sock = -1; |
5eb91c98 DG |
1405 | continue; |
1406 | } | |
840cb59c | 1407 | health_code_update(); |
099e26bd | 1408 | |
5eb91c98 | 1409 | ust_cmd->sock = sock; |
34a2494f | 1410 | sock = -1; |
099e26bd | 1411 | |
5eb91c98 DG |
1412 | DBG("UST registration received with pid:%d ppid:%d uid:%d" |
1413 | " gid:%d sock:%d name:%s (version %d.%d)", | |
1414 | ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid, | |
1415 | ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid, | |
1416 | ust_cmd->sock, ust_cmd->reg_msg.name, | |
1417 | ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor); | |
54d01ffb | 1418 | |
5eb91c98 DG |
1419 | /* |
1420 | * Lock free enqueue the registration request. The red pill | |
54d01ffb | 1421 | * has been taken! This apps will be part of the *system*. |
5eb91c98 | 1422 | */ |
8bdee6e2 | 1423 | cds_wfcq_enqueue(&ust_cmd_queue.head, &ust_cmd_queue.tail, &ust_cmd->node); |
5eb91c98 DG |
1424 | |
1425 | /* | |
1426 | * Wake the registration queue futex. Implicit memory | |
8bdee6e2 | 1427 | * barrier with the exchange in cds_wfcq_enqueue. |
5eb91c98 DG |
1428 | */ |
1429 | futex_nto1_wake(&ust_cmd_queue.futex); | |
03e43155 MD |
1430 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
1431 | ERR("Register apps socket poll error"); | |
1432 | goto error; | |
1433 | } else { | |
1434 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); | |
1435 | goto error; | |
5eb91c98 DG |
1436 | } |
1437 | } | |
90014c57 | 1438 | } |
1d4b027a DG |
1439 | } |
1440 | ||
139ac872 | 1441 | exit: |
1d4b027a | 1442 | error: |
0fdd1e2c DG |
1443 | /* Notify that the registration thread is gone */ |
1444 | notify_ust_apps(0); | |
1445 | ||
a4b35e07 | 1446 | if (apps_sock >= 0) { |
76d7553f MD |
1447 | ret = close(apps_sock); |
1448 | if (ret) { | |
1449 | PERROR("close"); | |
1450 | } | |
a4b35e07 | 1451 | } |
46c3f085 | 1452 | if (sock >= 0) { |
76d7553f MD |
1453 | ret = close(sock); |
1454 | if (ret) { | |
1455 | PERROR("close"); | |
1456 | } | |
4063050c | 1457 | lttng_fd_put(LTTNG_FD_APPS, 1); |
a4b35e07 | 1458 | } |
e6142f2e | 1459 | unlink(config.apps_unix_sock_path.value); |
0fdd1e2c | 1460 | |
76d7553f | 1461 | error_poll_add: |
5eb91c98 | 1462 | lttng_poll_clean(&events); |
76d7553f MD |
1463 | error_listen: |
1464 | error_create_poll: | |
6993eeb3 | 1465 | error_testpoint: |
76d7553f | 1466 | DBG("UST Registration thread cleanup complete"); |
9ad42ec1 MD |
1467 | if (err) { |
1468 | health_error(); | |
1469 | ERR("Health error occurred in %s", __func__); | |
1470 | } | |
8782cc74 | 1471 | health_unregister(health_sessiond); |
5eb91c98 | 1472 | |
1d4b027a DG |
1473 | return NULL; |
1474 | } | |
1475 | ||
b73401da | 1476 | /* |
096102bd | 1477 | * Setup necessary data for kernel tracer action. |
b73401da | 1478 | */ |
096102bd | 1479 | static int init_kernel_tracer(void) |
b73401da DG |
1480 | { |
1481 | int ret; | |
b73401da | 1482 | |
096102bd DG |
1483 | /* Modprobe lttng kernel modules */ |
1484 | ret = modprobe_lttng_control(); | |
b73401da | 1485 | if (ret < 0) { |
b73401da DG |
1486 | goto error; |
1487 | } | |
1488 | ||
096102bd DG |
1489 | /* Open debugfs lttng */ |
1490 | kernel_tracer_fd = open(module_proc_lttng, O_RDWR); | |
1491 | if (kernel_tracer_fd < 0) { | |
1492 | DBG("Failed to open %s", module_proc_lttng); | |
2f77fc4b | 1493 | goto error_open; |
54d01ffb DG |
1494 | } |
1495 | ||
2f77fc4b | 1496 | /* Validate kernel version */ |
88076e89 JD |
1497 | ret = kernel_validate_version(kernel_tracer_fd, &kernel_tracer_version, |
1498 | &kernel_tracer_abi_version); | |
2f77fc4b DG |
1499 | if (ret < 0) { |
1500 | goto error_version; | |
b551a063 | 1501 | } |
54d01ffb | 1502 | |
2f77fc4b DG |
1503 | ret = modprobe_lttng_data(); |
1504 | if (ret < 0) { | |
1505 | goto error_modules; | |
54d01ffb DG |
1506 | } |
1507 | ||
6e21424e JR |
1508 | ret = kernel_supports_ring_buffer_snapshot_sample_positions( |
1509 | kernel_tracer_fd); | |
1510 | if (ret < 0) { | |
1511 | goto error_modules; | |
1512 | } | |
1513 | ||
1514 | if (ret < 1) { | |
1515 | WARN("Kernel tracer does not support buffer monitoring. " | |
1516 | "The monitoring timer of channels in the kernel domain " | |
1517 | "will be set to 0 (disabled)."); | |
1518 | } | |
1519 | ||
2f77fc4b DG |
1520 | DBG("Kernel tracer fd %d", kernel_tracer_fd); |
1521 | return 0; | |
1522 | ||
1523 | error_version: | |
1524 | modprobe_remove_lttng_control(); | |
1525 | ret = close(kernel_tracer_fd); | |
1526 | if (ret) { | |
1527 | PERROR("close"); | |
b551a063 | 1528 | } |
2f77fc4b | 1529 | kernel_tracer_fd = -1; |
f73fabfd | 1530 | return LTTNG_ERR_KERN_VERSION; |
b551a063 | 1531 | |
2f77fc4b DG |
1532 | error_modules: |
1533 | ret = close(kernel_tracer_fd); | |
1534 | if (ret) { | |
1535 | PERROR("close"); | |
b551a063 | 1536 | } |
54d01ffb | 1537 | |
2f77fc4b DG |
1538 | error_open: |
1539 | modprobe_remove_lttng_control(); | |
54d01ffb DG |
1540 | |
1541 | error: | |
2f77fc4b DG |
1542 | WARN("No kernel tracer available"); |
1543 | kernel_tracer_fd = -1; | |
1544 | if (!is_root) { | |
f73fabfd | 1545 | return LTTNG_ERR_NEED_ROOT_SESSIOND; |
2f77fc4b | 1546 | } else { |
f73fabfd | 1547 | return LTTNG_ERR_KERN_NA; |
2f77fc4b | 1548 | } |
54d01ffb DG |
1549 | } |
1550 | ||
917a718d | 1551 | static int string_match(const char *str1, const char *str2) |
54d01ffb | 1552 | { |
917a718d | 1553 | return (str1 && str2) && !strcmp(str1, str2); |
54d01ffb DG |
1554 | } |
1555 | ||
00e2e675 | 1556 | /* |
917a718d JG |
1557 | * Take an option from the getopt output and set it in the right variable to be |
1558 | * used later. | |
36b588ed | 1559 | * |
917a718d | 1560 | * Return 0 on success else a negative value. |
00e2e675 | 1561 | */ |
917a718d | 1562 | static int set_option(int opt, const char *arg, const char *optname) |
00e2e675 | 1563 | { |
917a718d | 1564 | int ret = 0; |
fac6795d | 1565 | |
c0232ea5 | 1566 | if (string_match(optname, "client-sock") || opt == 'c') { |
66b2ce8e JG |
1567 | if (!arg || *arg == '\0') { |
1568 | ret = -EINVAL; | |
1569 | goto end; | |
1570 | } | |
e8fa9fb0 MD |
1571 | if (lttng_is_setuid_setgid()) { |
1572 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
1573 | "-c, --client-sock"); | |
1574 | } else { | |
e6142f2e JG |
1575 | config_string_set(&config.client_unix_sock_path, |
1576 | strdup(arg)); | |
1577 | if (!config.client_unix_sock_path.value) { | |
1578 | ret = -ENOMEM; | |
1579 | PERROR("strdup"); | |
1580 | } | |
e8fa9fb0 | 1581 | } |
c0232ea5 | 1582 | } else if (string_match(optname, "apps-sock") || opt == 'a') { |
66b2ce8e JG |
1583 | if (!arg || *arg == '\0') { |
1584 | ret = -EINVAL; | |
1585 | goto end; | |
1586 | } | |
e8fa9fb0 MD |
1587 | if (lttng_is_setuid_setgid()) { |
1588 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
1589 | "-a, --apps-sock"); | |
1590 | } else { | |
e6142f2e JG |
1591 | config_string_set(&config.apps_unix_sock_path, |
1592 | strdup(arg)); | |
1593 | if (!config.apps_unix_sock_path.value) { | |
1594 | ret = -ENOMEM; | |
1595 | PERROR("strdup"); | |
1596 | } | |
e8fa9fb0 | 1597 | } |
c0232ea5 | 1598 | } else if (string_match(optname, "daemonize") || opt == 'd') { |
e6142f2e | 1599 | config.daemonize = true; |
c0232ea5 | 1600 | } else if (string_match(optname, "background") || opt == 'b') { |
e6142f2e | 1601 | config.background = true; |
c0232ea5 | 1602 | } else if (string_match(optname, "group") || opt == 'g') { |
66b2ce8e JG |
1603 | if (!arg || *arg == '\0') { |
1604 | ret = -EINVAL; | |
1605 | goto end; | |
1606 | } | |
e8fa9fb0 MD |
1607 | if (lttng_is_setuid_setgid()) { |
1608 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
1609 | "-g, --group"); | |
1610 | } else { | |
e6142f2e JG |
1611 | config_string_set(&config.tracing_group_name, |
1612 | strdup(arg)); | |
1613 | if (!config.tracing_group_name.value) { | |
e8fa9fb0 | 1614 | ret = -ENOMEM; |
e6142f2e | 1615 | PERROR("strdup"); |
e8fa9fb0 | 1616 | } |
db322c4d | 1617 | } |
c0232ea5 | 1618 | } else if (string_match(optname, "help") || opt == 'h') { |
4fc83d94 | 1619 | ret = utils_show_help(8, "lttng-sessiond", help_msg); |
8190767e | 1620 | if (ret) { |
4fc83d94 | 1621 | ERR("Cannot show --help for `lttng-sessiond`"); |
8190767e PP |
1622 | perror("exec"); |
1623 | } | |
1624 | exit(ret ? EXIT_FAILURE : EXIT_SUCCESS); | |
c0232ea5 | 1625 | } else if (string_match(optname, "version") || opt == 'V') { |
26296c48 JG |
1626 | fprintf(stdout, "%s\n", VERSION); |
1627 | exit(EXIT_SUCCESS); | |
c0232ea5 | 1628 | } else if (string_match(optname, "sig-parent") || opt == 'S') { |
e6142f2e | 1629 | config.sig_parent = true; |
c0232ea5 | 1630 | } else if (string_match(optname, "kconsumerd-err-sock")) { |
66b2ce8e JG |
1631 | if (!arg || *arg == '\0') { |
1632 | ret = -EINVAL; | |
1633 | goto end; | |
1634 | } | |
e8fa9fb0 MD |
1635 | if (lttng_is_setuid_setgid()) { |
1636 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
1637 | "--kconsumerd-err-sock"); | |
1638 | } else { | |
e6142f2e JG |
1639 | config_string_set(&config.kconsumerd_err_unix_sock_path, |
1640 | strdup(arg)); | |
1641 | if (!config.kconsumerd_err_unix_sock_path.value) { | |
1642 | ret = -ENOMEM; | |
1643 | PERROR("strdup"); | |
1644 | } | |
e8fa9fb0 | 1645 | } |
c0232ea5 | 1646 | } else if (string_match(optname, "kconsumerd-cmd-sock")) { |
66b2ce8e JG |
1647 | if (!arg || *arg == '\0') { |
1648 | ret = -EINVAL; | |
1649 | goto end; | |
1650 | } | |
e8fa9fb0 MD |
1651 | if (lttng_is_setuid_setgid()) { |
1652 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
1653 | "--kconsumerd-cmd-sock"); | |
1654 | } else { | |
e6142f2e JG |
1655 | config_string_set(&config.kconsumerd_cmd_unix_sock_path, |
1656 | strdup(arg)); | |
1657 | if (!config.kconsumerd_cmd_unix_sock_path.value) { | |
1658 | ret = -ENOMEM; | |
1659 | PERROR("strdup"); | |
1660 | } | |
e8fa9fb0 | 1661 | } |
c0232ea5 | 1662 | } else if (string_match(optname, "ustconsumerd64-err-sock")) { |
66b2ce8e JG |
1663 | if (!arg || *arg == '\0') { |
1664 | ret = -EINVAL; | |
1665 | goto end; | |
1666 | } | |
e8fa9fb0 MD |
1667 | if (lttng_is_setuid_setgid()) { |
1668 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
1669 | "--ustconsumerd64-err-sock"); | |
1670 | } else { | |
e6142f2e JG |
1671 | config_string_set(&config.consumerd64_err_unix_sock_path, |
1672 | strdup(arg)); | |
1673 | if (!config.consumerd64_err_unix_sock_path.value) { | |
1674 | ret = -ENOMEM; | |
1675 | PERROR("strdup"); | |
1676 | } | |
e8fa9fb0 | 1677 | } |
c0232ea5 | 1678 | } else if (string_match(optname, "ustconsumerd64-cmd-sock")) { |
66b2ce8e JG |
1679 | if (!arg || *arg == '\0') { |
1680 | ret = -EINVAL; | |
1681 | goto end; | |
1682 | } | |
e8fa9fb0 MD |
1683 | if (lttng_is_setuid_setgid()) { |
1684 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
1685 | "--ustconsumerd64-cmd-sock"); | |
1686 | } else { | |
e6142f2e JG |
1687 | config_string_set(&config.consumerd64_cmd_unix_sock_path, |
1688 | strdup(arg)); | |
1689 | if (!config.consumerd64_cmd_unix_sock_path.value) { | |
1690 | ret = -ENOMEM; | |
1691 | PERROR("strdup"); | |
1692 | } | |
e8fa9fb0 | 1693 | } |
c0232ea5 | 1694 | } else if (string_match(optname, "ustconsumerd32-err-sock")) { |
66b2ce8e JG |
1695 | if (!arg || *arg == '\0') { |
1696 | ret = -EINVAL; | |
1697 | goto end; | |
1698 | } | |
e8fa9fb0 MD |
1699 | if (lttng_is_setuid_setgid()) { |
1700 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
1701 | "--ustconsumerd32-err-sock"); | |
1702 | } else { | |
e6142f2e JG |
1703 | config_string_set(&config.consumerd32_err_unix_sock_path, |
1704 | strdup(arg)); | |
1705 | if (!config.consumerd32_err_unix_sock_path.value) { | |
1706 | ret = -ENOMEM; | |
1707 | PERROR("strdup"); | |
1708 | } | |
e8fa9fb0 | 1709 | } |
c0232ea5 | 1710 | } else if (string_match(optname, "ustconsumerd32-cmd-sock")) { |
66b2ce8e JG |
1711 | if (!arg || *arg == '\0') { |
1712 | ret = -EINVAL; | |
1713 | goto end; | |
1714 | } | |
e8fa9fb0 MD |
1715 | if (lttng_is_setuid_setgid()) { |
1716 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
1717 | "--ustconsumerd32-cmd-sock"); | |
1718 | } else { | |
e6142f2e JG |
1719 | config_string_set(&config.consumerd32_cmd_unix_sock_path, |
1720 | strdup(arg)); | |
1721 | if (!config.consumerd32_cmd_unix_sock_path.value) { | |
1722 | ret = -ENOMEM; | |
1723 | PERROR("strdup"); | |
1724 | } | |
e8fa9fb0 | 1725 | } |
c0232ea5 | 1726 | } else if (string_match(optname, "no-kernel")) { |
e6142f2e | 1727 | config.no_kernel = true; |
c0232ea5 | 1728 | } else if (string_match(optname, "quiet") || opt == 'q') { |
6d9a9c65 | 1729 | config.quiet = true; |
c0232ea5 | 1730 | } else if (string_match(optname, "verbose") || opt == 'v') { |
26296c48 JG |
1731 | /* Verbose level can increase using multiple -v */ |
1732 | if (arg) { | |
13755a18 | 1733 | /* Value obtained from config file */ |
e6142f2e | 1734 | config.verbose = config_parse_value(arg); |
26296c48 | 1735 | } else { |
13755a18 | 1736 | /* -v used on command line */ |
e6142f2e | 1737 | config.verbose++; |
26296c48 | 1738 | } |
13755a18 | 1739 | /* Clamp value to [0, 3] */ |
e6142f2e JG |
1740 | config.verbose = config.verbose < 0 ? 0 : |
1741 | (config.verbose <= 3 ? config.verbose : 3); | |
c0232ea5 | 1742 | } else if (string_match(optname, "verbose-consumer")) { |
26296c48 | 1743 | if (arg) { |
e6142f2e | 1744 | config.verbose_consumer = config_parse_value(arg); |
26296c48 | 1745 | } else { |
e6142f2e | 1746 | config.verbose_consumer++; |
26296c48 | 1747 | } |
c0232ea5 | 1748 | } else if (string_match(optname, "consumerd32-path")) { |
66b2ce8e JG |
1749 | if (!arg || *arg == '\0') { |
1750 | ret = -EINVAL; | |
1751 | goto end; | |
1752 | } | |
e8fa9fb0 MD |
1753 | if (lttng_is_setuid_setgid()) { |
1754 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
1755 | "--consumerd32-path"); | |
1756 | } else { | |
e6142f2e JG |
1757 | config_string_set(&config.consumerd32_bin_path, |
1758 | strdup(arg)); | |
1759 | if (!config.consumerd32_bin_path.value) { | |
e8fa9fb0 MD |
1760 | PERROR("strdup"); |
1761 | ret = -ENOMEM; | |
1762 | } | |
db322c4d | 1763 | } |
c0232ea5 | 1764 | } else if (string_match(optname, "consumerd32-libdir")) { |
66b2ce8e JG |
1765 | if (!arg || *arg == '\0') { |
1766 | ret = -EINVAL; | |
1767 | goto end; | |
1768 | } | |
e8fa9fb0 MD |
1769 | if (lttng_is_setuid_setgid()) { |
1770 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
1771 | "--consumerd32-libdir"); | |
1772 | } else { | |
e6142f2e JG |
1773 | config_string_set(&config.consumerd32_lib_dir, |
1774 | strdup(arg)); | |
1775 | if (!config.consumerd32_lib_dir.value) { | |
e8fa9fb0 MD |
1776 | PERROR("strdup"); |
1777 | ret = -ENOMEM; | |
1778 | } | |
db322c4d | 1779 | } |
c0232ea5 | 1780 | } else if (string_match(optname, "consumerd64-path")) { |
66b2ce8e JG |
1781 | if (!arg || *arg == '\0') { |
1782 | ret = -EINVAL; | |
1783 | goto end; | |
1784 | } | |
e8fa9fb0 MD |
1785 | if (lttng_is_setuid_setgid()) { |
1786 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
1787 | "--consumerd64-path"); | |
1788 | } else { | |
e6142f2e JG |
1789 | config_string_set(&config.consumerd64_bin_path, |
1790 | strdup(arg)); | |
1791 | if (!config.consumerd64_bin_path.value) { | |
e8fa9fb0 MD |
1792 | PERROR("strdup"); |
1793 | ret = -ENOMEM; | |
1794 | } | |
db322c4d | 1795 | } |
c0232ea5 | 1796 | } else if (string_match(optname, "consumerd64-libdir")) { |
66b2ce8e JG |
1797 | if (!arg || *arg == '\0') { |
1798 | ret = -EINVAL; | |
1799 | goto end; | |
1800 | } | |
e8fa9fb0 MD |
1801 | if (lttng_is_setuid_setgid()) { |
1802 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
1803 | "--consumerd64-libdir"); | |
1804 | } else { | |
e6142f2e JG |
1805 | config_string_set(&config.consumerd64_lib_dir, |
1806 | strdup(arg)); | |
1807 | if (!config.consumerd64_lib_dir.value) { | |
e8fa9fb0 MD |
1808 | PERROR("strdup"); |
1809 | ret = -ENOMEM; | |
1810 | } | |
db322c4d | 1811 | } |
c0232ea5 | 1812 | } else if (string_match(optname, "pidfile") || opt == 'p') { |
66b2ce8e JG |
1813 | if (!arg || *arg == '\0') { |
1814 | ret = -EINVAL; | |
1815 | goto end; | |
1816 | } | |
e8fa9fb0 MD |
1817 | if (lttng_is_setuid_setgid()) { |
1818 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
1819 | "-p, --pidfile"); | |
1820 | } else { | |
e6142f2e JG |
1821 | config_string_set(&config.pid_file_path, strdup(arg)); |
1822 | if (!config.pid_file_path.value) { | |
e8fa9fb0 MD |
1823 | PERROR("strdup"); |
1824 | ret = -ENOMEM; | |
1825 | } | |
db322c4d | 1826 | } |
c0232ea5 | 1827 | } else if (string_match(optname, "agent-tcp-port")) { |
66b2ce8e JG |
1828 | if (!arg || *arg == '\0') { |
1829 | ret = -EINVAL; | |
1830 | goto end; | |
1831 | } | |
e8fa9fb0 MD |
1832 | if (lttng_is_setuid_setgid()) { |
1833 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
1834 | "--agent-tcp-port"); | |
1835 | } else { | |
1836 | unsigned long v; | |
4d076222 | 1837 | |
e8fa9fb0 MD |
1838 | errno = 0; |
1839 | v = strtoul(arg, NULL, 0); | |
1840 | if (errno != 0 || !isdigit(arg[0])) { | |
1841 | ERR("Wrong value in --agent-tcp-port parameter: %s", arg); | |
1842 | return -1; | |
1843 | } | |
1844 | if (v == 0 || v >= 65535) { | |
1845 | ERR("Port overflow in --agent-tcp-port parameter: %s", arg); | |
1846 | return -1; | |
1847 | } | |
2288467f JG |
1848 | config.agent_tcp_port.begin = config.agent_tcp_port.end = (int) v; |
1849 | DBG3("Agent TCP port set to non default: %i", (int) v); | |
26296c48 | 1850 | } |
c0232ea5 | 1851 | } else if (string_match(optname, "load") || opt == 'l') { |
66b2ce8e JG |
1852 | if (!arg || *arg == '\0') { |
1853 | ret = -EINVAL; | |
1854 | goto end; | |
1855 | } | |
e8fa9fb0 MD |
1856 | if (lttng_is_setuid_setgid()) { |
1857 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
1858 | "-l, --load"); | |
1859 | } else { | |
e6142f2e JG |
1860 | config_string_set(&config.load_session_path, strdup(arg)); |
1861 | if (!config.load_session_path.value) { | |
e8fa9fb0 MD |
1862 | PERROR("strdup"); |
1863 | ret = -ENOMEM; | |
1864 | } | |
ef367a93 | 1865 | } |
c0232ea5 | 1866 | } else if (string_match(optname, "kmod-probes")) { |
66b2ce8e JG |
1867 | if (!arg || *arg == '\0') { |
1868 | ret = -EINVAL; | |
1869 | goto end; | |
1870 | } | |
e8fa9fb0 MD |
1871 | if (lttng_is_setuid_setgid()) { |
1872 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
1873 | "--kmod-probes"); | |
1874 | } else { | |
e6142f2e JG |
1875 | config_string_set(&config.kmod_probes_list, strdup(arg)); |
1876 | if (!config.kmod_probes_list.value) { | |
e8fa9fb0 MD |
1877 | PERROR("strdup"); |
1878 | ret = -ENOMEM; | |
1879 | } | |
c9d42407 | 1880 | } |
c0232ea5 | 1881 | } else if (string_match(optname, "extra-kmod-probes")) { |
66b2ce8e JG |
1882 | if (!arg || *arg == '\0') { |
1883 | ret = -EINVAL; | |
1884 | goto end; | |
1885 | } | |
e8fa9fb0 MD |
1886 | if (lttng_is_setuid_setgid()) { |
1887 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
1888 | "--extra-kmod-probes"); | |
1889 | } else { | |
e6142f2e JG |
1890 | config_string_set(&config.kmod_extra_probes_list, |
1891 | strdup(arg)); | |
1892 | if (!config.kmod_extra_probes_list.value) { | |
e8fa9fb0 MD |
1893 | PERROR("strdup"); |
1894 | ret = -ENOMEM; | |
1895 | } | |
db322c4d | 1896 | } |
c0232ea5 JG |
1897 | } else if (string_match(optname, "config") || opt == 'f') { |
1898 | /* This is handled in set_options() thus silent skip. */ | |
1899 | goto end; | |
1900 | } else { | |
26296c48 JG |
1901 | /* Unknown option or other error. |
1902 | * Error is printed by getopt, just return */ | |
1903 | ret = -1; | |
1904 | } | |
1905 | ||
f3dd7ce7 | 1906 | end: |
c5d350b2 JG |
1907 | if (ret == -EINVAL) { |
1908 | const char *opt_name = "unknown"; | |
1909 | int i; | |
1910 | ||
1911 | for (i = 0; i < sizeof(long_options) / sizeof(struct option); | |
1912 | i++) { | |
1913 | if (opt == long_options[i].val) { | |
1914 | opt_name = long_options[i].name; | |
1915 | break; | |
1916 | } | |
1917 | } | |
1918 | ||
1919 | WARN("Invalid argument provided for option \"%s\", using default value.", | |
1920 | opt_name); | |
1921 | } | |
f3dd7ce7 | 1922 | |
26296c48 JG |
1923 | return ret; |
1924 | } | |
1925 | ||
1926 | /* | |
1927 | * config_entry_handler_cb used to handle options read from a config file. | |
f40ef1d5 | 1928 | * See config_entry_handler_cb comment in common/config/session-config.h for the |
26296c48 JG |
1929 | * return value conventions. |
1930 | */ | |
1931 | static int config_entry_handler(const struct config_entry *entry, void *unused) | |
1932 | { | |
1933 | int ret = 0, i; | |
1934 | ||
1935 | if (!entry || !entry->name || !entry->value) { | |
1936 | ret = -EINVAL; | |
1937 | goto end; | |
1938 | } | |
1939 | ||
1940 | /* Check if the option is to be ignored */ | |
1941 | for (i = 0; i < sizeof(config_ignore_options) / sizeof(char *); i++) { | |
1942 | if (!strcmp(entry->name, config_ignore_options[i])) { | |
1943 | goto end; | |
1944 | } | |
1945 | } | |
1946 | ||
1947 | for (i = 0; i < (sizeof(long_options) / sizeof(struct option)) - 1; | |
1948 | i++) { | |
1949 | ||
1950 | /* Ignore if not fully matched. */ | |
1951 | if (strcmp(entry->name, long_options[i].name)) { | |
1952 | continue; | |
1953 | } | |
1954 | ||
1955 | /* | |
1956 | * If the option takes no argument on the command line, we have to | |
1957 | * check if the value is "true". We support non-zero numeric values, | |
1958 | * true, on and yes. | |
1959 | */ | |
1960 | if (!long_options[i].has_arg) { | |
1961 | ret = config_parse_value(entry->value); | |
1962 | if (ret <= 0) { | |
1963 | if (ret) { | |
1964 | WARN("Invalid configuration value \"%s\" for option %s", | |
1965 | entry->value, entry->name); | |
1966 | } | |
1967 | /* False, skip boolean config option. */ | |
1968 | goto end; | |
4d076222 | 1969 | } |
26296c48 JG |
1970 | } |
1971 | ||
1972 | ret = set_option(long_options[i].val, entry->value, entry->name); | |
1973 | goto end; | |
1974 | } | |
1975 | ||
1976 | WARN("Unrecognized option \"%s\" in daemon configuration file.", entry->name); | |
1977 | ||
1978 | end: | |
1979 | return ret; | |
1980 | } | |
1981 | ||
1982 | /* | |
1983 | * daemon configuration loading and argument parsing | |
1984 | */ | |
1985 | static int set_options(int argc, char **argv) | |
1986 | { | |
1987 | int ret = 0, c = 0, option_index = 0; | |
1988 | int orig_optopt = optopt, orig_optind = optind; | |
1989 | char *optstring; | |
1990 | const char *config_path = NULL; | |
1991 | ||
1992 | optstring = utils_generate_optstring(long_options, | |
1993 | sizeof(long_options) / sizeof(struct option)); | |
1994 | if (!optstring) { | |
1995 | ret = -ENOMEM; | |
1996 | goto end; | |
1997 | } | |
1998 | ||
1999 | /* Check for the --config option */ | |
2000 | while ((c = getopt_long(argc, argv, optstring, long_options, | |
2001 | &option_index)) != -1) { | |
2002 | if (c == '?') { | |
2003 | ret = -EINVAL; | |
2004 | goto end; | |
2005 | } else if (c != 'f') { | |
2006 | /* if not equal to --config option. */ | |
2007 | continue; | |
2008 | } | |
2009 | ||
e8fa9fb0 MD |
2010 | if (lttng_is_setuid_setgid()) { |
2011 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
2012 | "-f, --config"); | |
2013 | } else { | |
2014 | config_path = utils_expand_path(optarg); | |
2015 | if (!config_path) { | |
2016 | ERR("Failed to resolve path: %s", optarg); | |
2017 | } | |
26296c48 JG |
2018 | } |
2019 | } | |
2020 | ||
2021 | ret = config_get_section_entries(config_path, config_section_name, | |
2022 | config_entry_handler, NULL); | |
2023 | if (ret) { | |
2024 | if (ret > 0) { | |
2025 | ERR("Invalid configuration option at line %i", ret); | |
2026 | ret = -1; | |
2027 | } | |
2028 | goto end; | |
2029 | } | |
2030 | ||
2031 | /* Reset getopt's global state */ | |
2032 | optopt = orig_optopt; | |
2033 | optind = orig_optind; | |
2034 | while (1) { | |
c0232ea5 JG |
2035 | option_index = -1; |
2036 | /* | |
2037 | * getopt_long() will not set option_index if it encounters a | |
2038 | * short option. | |
2039 | */ | |
2040 | c = getopt_long(argc, argv, optstring, long_options, | |
2041 | &option_index); | |
26296c48 | 2042 | if (c == -1) { |
4d076222 DG |
2043 | break; |
2044 | } | |
26296c48 | 2045 | |
c0232ea5 JG |
2046 | /* |
2047 | * Pass NULL as the long option name if popt left the index | |
2048 | * unset. | |
2049 | */ | |
2050 | ret = set_option(c, optarg, | |
2051 | option_index < 0 ? NULL : | |
2052 | long_options[option_index].name); | |
26296c48 JG |
2053 | if (ret < 0) { |
2054 | break; | |
fac6795d DG |
2055 | } |
2056 | } | |
2057 | ||
26296c48 JG |
2058 | end: |
2059 | free(optstring); | |
2060 | return ret; | |
fac6795d DG |
2061 | } |
2062 | ||
2063 | /* | |
917a718d | 2064 | * Creates the application socket. |
fac6795d | 2065 | */ |
cf3af59e | 2066 | static int init_daemon_socket(void) |
fac6795d DG |
2067 | { |
2068 | int ret = 0; | |
2069 | mode_t old_umask; | |
2070 | ||
2071 | old_umask = umask(0); | |
2072 | ||
fac6795d | 2073 | /* Create the application unix socket */ |
e6142f2e | 2074 | apps_sock = lttcomm_create_unix_sock(config.apps_unix_sock_path.value); |
d6f42150 | 2075 | if (apps_sock < 0) { |
e6142f2e | 2076 | ERR("Create unix sock failed: %s", config.apps_unix_sock_path.value); |
fac6795d DG |
2077 | ret = -1; |
2078 | goto end; | |
2079 | } | |
2080 | ||
b662582b DG |
2081 | /* Set the cloexec flag */ |
2082 | ret = utils_set_fd_cloexec(apps_sock); | |
2083 | if (ret < 0) { | |
2084 | ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). " | |
2085 | "Continuing but note that the consumer daemon will have a " | |
2086 | "reference to this socket on exec()", apps_sock); | |
2087 | } | |
2088 | ||
d6f42150 | 2089 | /* File permission MUST be 666 */ |
e6142f2e | 2090 | ret = chmod(config.apps_unix_sock_path.value, |
54d01ffb | 2091 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); |
fac6795d | 2092 | if (ret < 0) { |
e6142f2e | 2093 | ERR("Set file permissions failed: %s", config.apps_unix_sock_path.value); |
76d7553f | 2094 | PERROR("chmod"); |
fac6795d DG |
2095 | goto end; |
2096 | } | |
2097 | ||
917a718d JG |
2098 | DBG3("Session daemon application socket %d created", |
2099 | apps_sock); | |
b662582b | 2100 | |
fac6795d DG |
2101 | end: |
2102 | umask(old_umask); | |
2103 | return ret; | |
2104 | } | |
2105 | ||
f472090a JG |
2106 | /* |
2107 | * Create lockfile using the rundir and return its fd. | |
2108 | */ | |
2109 | static int create_lockfile(void) | |
2110 | { | |
2111 | return utils_create_lock_file(config.lock_file_path.value); | |
2112 | } | |
2113 | ||
fac6795d | 2114 | /* |
54d01ffb DG |
2115 | * Check if the global socket is available, and if a daemon is answering at the |
2116 | * other side. If yes, error is returned. | |
f472090a JG |
2117 | * |
2118 | * Also attempts to create and hold the lock file. | |
fac6795d | 2119 | */ |
cf3af59e | 2120 | static int check_existing_daemon(void) |
fac6795d | 2121 | { |
f472090a JG |
2122 | int ret = 0; |
2123 | ||
7d8234d9 | 2124 | /* Is there anybody out there ? */ |
099e26bd | 2125 | if (lttng_session_daemon_alive()) { |
f472090a JG |
2126 | ret = -EEXIST; |
2127 | goto end; | |
099e26bd | 2128 | } |
b09c7c76 | 2129 | |
f472090a JG |
2130 | lockfile_fd = create_lockfile(); |
2131 | if (lockfile_fd < 0) { | |
2132 | ret = -EEXIST; | |
2133 | goto end; | |
2134 | } | |
2135 | end: | |
2136 | return ret; | |
2137 | } | |
2138 | ||
2139 | static void sessiond_cleanup_lock_file(void) | |
2140 | { | |
2141 | int ret; | |
2142 | ||
2143 | /* | |
2144 | * Cleanup lock file by deleting it and finaly closing it which will | |
2145 | * release the file system lock. | |
2146 | */ | |
2147 | if (lockfile_fd >= 0) { | |
2148 | ret = remove(config.lock_file_path.value); | |
2149 | if (ret < 0) { | |
2150 | PERROR("remove lock file"); | |
2151 | } | |
2152 | ret = close(lockfile_fd); | |
2153 | if (ret < 0) { | |
2154 | PERROR("close lock file"); | |
2155 | } | |
2156 | } | |
fac6795d DG |
2157 | } |
2158 | ||
fac6795d | 2159 | /* |
d063d709 | 2160 | * Set the tracing group gid onto the client socket. |
5e16da05 | 2161 | * |
d063d709 | 2162 | * Race window between mkdir and chown is OK because we are going from more |
d1613cf5 | 2163 | * permissive (root.root) to less permissive (root.tracing). |
fac6795d | 2164 | */ |
be040666 | 2165 | static int set_permissions(char *rundir) |
fac6795d DG |
2166 | { |
2167 | int ret; | |
996b65c8 | 2168 | gid_t gid; |
fac6795d | 2169 | |
e6142f2e | 2170 | gid = utils_get_group_id(config.tracing_group_name.value); |
fac6795d | 2171 | |
d6f42150 | 2172 | /* Set lttng run dir */ |
be040666 | 2173 | ret = chown(rundir, 0, gid); |
d6f42150 | 2174 | if (ret < 0) { |
be040666 | 2175 | ERR("Unable to set group on %s", rundir); |
76d7553f | 2176 | PERROR("chown"); |
d6f42150 DG |
2177 | } |
2178 | ||
6c71277b MD |
2179 | /* |
2180 | * Ensure all applications and tracing group can search the run | |
2181 | * dir. Allow everyone to read the directory, since it does not | |
2182 | * buy us anything to hide its content. | |
2183 | */ | |
2184 | ret = chmod(rundir, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); | |
d1613cf5 JN |
2185 | if (ret < 0) { |
2186 | ERR("Unable to set permissions on %s", rundir); | |
76d7553f | 2187 | PERROR("chmod"); |
d1613cf5 JN |
2188 | } |
2189 | ||
d6f42150 | 2190 | /* lttng client socket path */ |
e6142f2e | 2191 | ret = chown(config.client_unix_sock_path.value, 0, gid); |
fac6795d | 2192 | if (ret < 0) { |
e6142f2e | 2193 | ERR("Unable to set group on %s", config.client_unix_sock_path.value); |
76d7553f | 2194 | PERROR("chown"); |
d6f42150 DG |
2195 | } |
2196 | ||
3bd1e081 | 2197 | /* kconsumer error socket path */ |
6c71277b | 2198 | ret = chown(kconsumer_data.err_unix_sock_path, 0, 0); |
d6f42150 | 2199 | if (ret < 0) { |
3bd1e081 | 2200 | ERR("Unable to set group on %s", kconsumer_data.err_unix_sock_path); |
76d7553f | 2201 | PERROR("chown"); |
3bd1e081 MD |
2202 | } |
2203 | ||
7753dea8 | 2204 | /* 64-bit ustconsumer error socket path */ |
6c71277b | 2205 | ret = chown(ustconsumer64_data.err_unix_sock_path, 0, 0); |
7753dea8 MD |
2206 | if (ret < 0) { |
2207 | ERR("Unable to set group on %s", ustconsumer64_data.err_unix_sock_path); | |
76d7553f | 2208 | PERROR("chown"); |
7753dea8 MD |
2209 | } |
2210 | ||
2211 | /* 32-bit ustconsumer compat32 error socket path */ | |
6c71277b | 2212 | ret = chown(ustconsumer32_data.err_unix_sock_path, 0, 0); |
3bd1e081 | 2213 | if (ret < 0) { |
7753dea8 | 2214 | ERR("Unable to set group on %s", ustconsumer32_data.err_unix_sock_path); |
76d7553f | 2215 | PERROR("chown"); |
fac6795d DG |
2216 | } |
2217 | ||
d6f42150 | 2218 | DBG("All permissions are set"); |
e07ae692 | 2219 | |
fac6795d DG |
2220 | return ret; |
2221 | } | |
2222 | ||
d6f42150 | 2223 | /* |
d063d709 | 2224 | * Create the lttng run directory needed for all global sockets and pipe. |
d6f42150 | 2225 | */ |
e6142f2e | 2226 | static int create_lttng_rundir(void) |
d6f42150 DG |
2227 | { |
2228 | int ret; | |
2229 | ||
e6142f2e | 2230 | DBG3("Creating LTTng run directory: %s", config.rundir.value); |
67e40797 | 2231 | |
e6142f2e | 2232 | ret = mkdir(config.rundir.value, S_IRWXU); |
d6f42150 | 2233 | if (ret < 0) { |
b1f11e69 | 2234 | if (errno != EEXIST) { |
e6142f2e | 2235 | ERR("Unable to create %s", config.rundir.value); |
b1f11e69 DG |
2236 | goto error; |
2237 | } else { | |
2238 | ret = 0; | |
2239 | } | |
d6f42150 DG |
2240 | } |
2241 | ||
2242 | error: | |
2243 | return ret; | |
2244 | } | |
2245 | ||
2246 | /* | |
e6142f2e | 2247 | * Setup sockets and directory needed by the consumerds' communication with the |
d063d709 | 2248 | * session daemon. |
d6f42150 | 2249 | */ |
e6142f2e | 2250 | static int set_consumer_sockets(struct consumer_data *consumer_data) |
d6f42150 DG |
2251 | { |
2252 | int ret; | |
e6142f2e | 2253 | char *path = NULL; |
d6f42150 | 2254 | |
6c71277b | 2255 | switch (consumer_data->type) { |
7753dea8 | 2256 | case LTTNG_CONSUMER_KERNEL: |
e6142f2e | 2257 | path = config.kconsumerd_path.value; |
7753dea8 MD |
2258 | break; |
2259 | case LTTNG_CONSUMER64_UST: | |
e6142f2e | 2260 | path = config.consumerd64_path.value; |
7753dea8 MD |
2261 | break; |
2262 | case LTTNG_CONSUMER32_UST: | |
e6142f2e | 2263 | path = config.consumerd32_path.value; |
7753dea8 MD |
2264 | break; |
2265 | default: | |
2266 | ERR("Consumer type unknown"); | |
2267 | ret = -EINVAL; | |
2268 | goto error; | |
d6f42150 | 2269 | } |
e6142f2e | 2270 | assert(path); |
d6f42150 | 2271 | |
67e40797 DG |
2272 | DBG2("Creating consumer directory: %s", path); |
2273 | ||
6c71277b | 2274 | ret = mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP); |
e7e1ef17 JR |
2275 | if (ret < 0 && errno != EEXIST) { |
2276 | PERROR("mkdir"); | |
2277 | ERR("Failed to create %s", path); | |
2278 | goto error; | |
d6f42150 | 2279 | } |
6c71277b | 2280 | if (is_root) { |
e6142f2e | 2281 | ret = chown(path, 0, utils_get_group_id(config.tracing_group_name.value)); |
6c71277b MD |
2282 | if (ret < 0) { |
2283 | ERR("Unable to set group on %s", path); | |
2284 | PERROR("chown"); | |
2285 | goto error; | |
2286 | } | |
2287 | } | |
d6f42150 | 2288 | |
e6142f2e | 2289 | /* Create the consumerd error unix socket */ |
3bd1e081 MD |
2290 | consumer_data->err_sock = |
2291 | lttcomm_create_unix_sock(consumer_data->err_unix_sock_path); | |
2292 | if (consumer_data->err_sock < 0) { | |
2293 | ERR("Create unix sock failed: %s", consumer_data->err_unix_sock_path); | |
d6f42150 DG |
2294 | ret = -1; |
2295 | goto error; | |
2296 | } | |
2297 | ||
a24f05ab MD |
2298 | /* |
2299 | * Set the CLOEXEC flag. Return code is useless because either way, the | |
2300 | * show must go on. | |
2301 | */ | |
2302 | ret = utils_set_fd_cloexec(consumer_data->err_sock); | |
2303 | if (ret < 0) { | |
2304 | PERROR("utils_set_fd_cloexec"); | |
2305 | /* continue anyway */ | |
2306 | } | |
2307 | ||
d6f42150 | 2308 | /* File permission MUST be 660 */ |
3bd1e081 | 2309 | ret = chmod(consumer_data->err_unix_sock_path, |
54d01ffb | 2310 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); |
d6f42150 | 2311 | if (ret < 0) { |
3bd1e081 | 2312 | ERR("Set file permissions failed: %s", consumer_data->err_unix_sock_path); |
67e40797 | 2313 | PERROR("chmod"); |
d6f42150 DG |
2314 | goto error; |
2315 | } | |
2316 | ||
2317 | error: | |
2318 | return ret; | |
2319 | } | |
2320 | ||
fac6795d | 2321 | /* |
d063d709 | 2322 | * Signal handler for the daemon |
cf3af59e | 2323 | * |
54d01ffb DG |
2324 | * Simply stop all worker threads, leaving main() return gracefully after |
2325 | * joining all threads and calling cleanup(). | |
fac6795d DG |
2326 | */ |
2327 | static void sighandler(int sig) | |
2328 | { | |
2329 | switch (sig) { | |
cf3af59e | 2330 | case SIGINT: |
af87c45a | 2331 | DBG("SIGINT caught"); |
cf3af59e MD |
2332 | stop_threads(); |
2333 | break; | |
2334 | case SIGTERM: | |
af87c45a | 2335 | DBG("SIGTERM caught"); |
cf3af59e MD |
2336 | stop_threads(); |
2337 | break; | |
0bb7724a DG |
2338 | case SIGUSR1: |
2339 | CMM_STORE_SHARED(recv_child_signal, 1); | |
2340 | break; | |
cf3af59e MD |
2341 | default: |
2342 | break; | |
fac6795d | 2343 | } |
fac6795d DG |
2344 | } |
2345 | ||
2346 | /* | |
d063d709 | 2347 | * Setup signal handler for : |
1d4b027a | 2348 | * SIGINT, SIGTERM, SIGPIPE |
fac6795d | 2349 | */ |
1d4b027a | 2350 | static int set_signal_handler(void) |
fac6795d | 2351 | { |
1d4b027a DG |
2352 | int ret = 0; |
2353 | struct sigaction sa; | |
2354 | sigset_t sigset; | |
fac6795d | 2355 | |
1d4b027a | 2356 | if ((ret = sigemptyset(&sigset)) < 0) { |
76d7553f | 2357 | PERROR("sigemptyset"); |
1d4b027a DG |
2358 | return ret; |
2359 | } | |
d6f42150 | 2360 | |
1d4b027a DG |
2361 | sa.sa_mask = sigset; |
2362 | sa.sa_flags = 0; | |
0072e5e2 MD |
2363 | |
2364 | sa.sa_handler = sighandler; | |
1d4b027a | 2365 | if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) { |
76d7553f | 2366 | PERROR("sigaction"); |
1d4b027a | 2367 | return ret; |
d6f42150 DG |
2368 | } |
2369 | ||
1d4b027a | 2370 | if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) { |
76d7553f | 2371 | PERROR("sigaction"); |
1d4b027a | 2372 | return ret; |
d6f42150 | 2373 | } |
aaf26714 | 2374 | |
0072e5e2 | 2375 | if ((ret = sigaction(SIGUSR1, &sa, NULL)) < 0) { |
76d7553f | 2376 | PERROR("sigaction"); |
1d4b027a | 2377 | return ret; |
8c0faa1d DG |
2378 | } |
2379 | ||
0072e5e2 MD |
2380 | sa.sa_handler = SIG_IGN; |
2381 | if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) { | |
0bb7724a DG |
2382 | PERROR("sigaction"); |
2383 | return ret; | |
2384 | } | |
2385 | ||
2386 | DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT"); | |
1d4b027a DG |
2387 | |
2388 | return ret; | |
fac6795d DG |
2389 | } |
2390 | ||
f3ed775e | 2391 | /* |
d063d709 | 2392 | * Set open files limit to unlimited. This daemon can open a large number of |
514bb9f0 | 2393 | * file descriptors in order to consume multiple kernel traces. |
f3ed775e DG |
2394 | */ |
2395 | static void set_ulimit(void) | |
2396 | { | |
2397 | int ret; | |
2398 | struct rlimit lim; | |
2399 | ||
514bb9f0 | 2400 | /* The kernel does not allow an infinite limit for open files */ |
f3ed775e DG |
2401 | lim.rlim_cur = 65535; |
2402 | lim.rlim_max = 65535; | |
2403 | ||
2404 | ret = setrlimit(RLIMIT_NOFILE, &lim); | |
2405 | if (ret < 0) { | |
76d7553f | 2406 | PERROR("failed to set open files limit"); |
f3ed775e DG |
2407 | } |
2408 | } | |
2409 | ||
4a15001e | 2410 | static int write_pidfile(void) |
35f90c40 | 2411 | { |
e6142f2e | 2412 | return utils_create_pid_file(getpid(), config.pid_file_path.value); |
35f90c40 DG |
2413 | } |
2414 | ||
e6142f2e JG |
2415 | static int set_clock_plugin_env(void) |
2416 | { | |
2417 | int ret = 0; | |
2418 | char *env_value = NULL; | |
cd9290dd | 2419 | |
e6142f2e JG |
2420 | if (!config.lttng_ust_clock_plugin.value) { |
2421 | goto end; | |
2422 | } | |
2423 | ||
2424 | ret = asprintf(&env_value, "LTTNG_UST_CLOCK_PLUGIN=%s", | |
2425 | config.lttng_ust_clock_plugin.value); | |
cd9290dd | 2426 | if (ret < 0) { |
e6142f2e JG |
2427 | PERROR("asprintf"); |
2428 | goto end; | |
cd9290dd DG |
2429 | } |
2430 | ||
e6142f2e JG |
2431 | ret = putenv(env_value); |
2432 | if (ret) { | |
2433 | free(env_value); | |
2434 | PERROR("putenv of LTTNG_UST_CLOCK_PLUGIN"); | |
2435 | goto end; | |
2436 | } | |
cd9290dd | 2437 | |
e6142f2e JG |
2438 | DBG("Updated LTTNG_UST_CLOCK_PLUGIN environment variable to \"%s\"", |
2439 | config.lttng_ust_clock_plugin.value); | |
2440 | end: | |
4a15001e | 2441 | return ret; |
ef367a93 JG |
2442 | } |
2443 | ||
99d688f2 JG |
2444 | static void destroy_all_sessions_and_wait(void) |
2445 | { | |
2446 | struct ltt_session *session, *tmp; | |
2447 | struct ltt_session_list *session_list; | |
2448 | ||
2449 | session_list = session_get_list(); | |
2450 | DBG("Initiating destruction of all sessions"); | |
2451 | ||
2452 | if (!session_list) { | |
2453 | return; | |
2454 | } | |
2455 | ||
99d688f2 JG |
2456 | session_lock_list(); |
2457 | /* Initiate the destruction of all sessions. */ | |
2458 | cds_list_for_each_entry_safe(session, tmp, | |
2459 | &session_list->head, list) { | |
2460 | if (!session_get(session)) { | |
2461 | continue; | |
2462 | } | |
2463 | ||
2464 | session_lock(session); | |
2465 | if (session->destroyed) { | |
2466 | goto unlock_session; | |
2467 | } | |
2468 | (void) cmd_destroy_session(session, | |
2469 | notification_thread_handle); | |
2470 | unlock_session: | |
2471 | session_unlock(session); | |
2472 | session_put(session); | |
2473 | } | |
2474 | session_unlock_list(); | |
2475 | ||
2476 | /* Wait for the destruction of all sessions to complete. */ | |
2477 | DBG("Waiting for the destruction of all sessions to complete"); | |
2478 | session_list_wait_empty(); | |
2479 | DBG("Destruction of all sessions completed"); | |
2480 | } | |
2481 | ||
fac6795d DG |
2482 | /* |
2483 | * main | |
2484 | */ | |
2485 | int main(int argc, char **argv) | |
2486 | { | |
4a15001e | 2487 | int ret = 0, retval = 0; |
fac6795d | 2488 | void *status; |
e6142f2e | 2489 | const char *env_app_timeout; |
b3530820 JG |
2490 | struct lttng_pipe *ust32_channel_monitor_pipe = NULL, |
2491 | *ust64_channel_monitor_pipe = NULL, | |
2492 | *kernel_channel_monitor_pipe = NULL; | |
a3707772 | 2493 | struct lttng_thread *ht_cleanup_thread = NULL; |
bc26e826 | 2494 | struct timer_thread_parameters timer_thread_parameters; |
64d9b072 JG |
2495 | /* Rotation thread handle. */ |
2496 | struct rotation_thread_handle *rotation_thread_handle = NULL; | |
d086f507 JD |
2497 | /* Queue of rotation jobs populated by the sessiond-timer. */ |
2498 | struct rotation_thread_timer_queue *rotation_timer_queue = NULL; | |
917a718d | 2499 | struct lttng_thread *client_thread = NULL; |
fac6795d | 2500 | |
335a95b7 MD |
2501 | init_kernel_workarounds(); |
2502 | ||
f6a9efaa DG |
2503 | rcu_register_thread(); |
2504 | ||
4a15001e MD |
2505 | if (set_signal_handler()) { |
2506 | retval = -1; | |
2507 | goto exit_set_signal_handler; | |
0bb7724a DG |
2508 | } |
2509 | ||
92816cc3 | 2510 | if (timer_signal_init()) { |
d086f507 JD |
2511 | retval = -1; |
2512 | goto exit_set_signal_handler; | |
2513 | } | |
2514 | ||
12744796 DG |
2515 | page_size = sysconf(_SC_PAGESIZE); |
2516 | if (page_size < 0) { | |
2517 | PERROR("sysconf _SC_PAGESIZE"); | |
2518 | page_size = LONG_MAX; | |
2519 | WARN("Fallback page size to %ld", page_size); | |
2520 | } | |
2521 | ||
e6142f2e JG |
2522 | ret = sessiond_config_init(&config); |
2523 | if (ret) { | |
2524 | retval = -1; | |
2525 | goto exit_set_signal_handler; | |
2526 | } | |
2527 | ||
2788b494 JR |
2528 | /* |
2529 | * Init config from environment variables. | |
2530 | * Command line option override env configuration per-doc. Do env first. | |
2531 | */ | |
2532 | sessiond_config_apply_env_config(&config); | |
2533 | ||
4a15001e MD |
2534 | /* |
2535 | * Parse arguments and load the daemon configuration file. | |
2536 | * | |
2537 | * We have an exit_options exit path to free memory reserved by | |
2538 | * set_options. This is needed because the rest of sessiond_cleanup() | |
2539 | * depends on ht_cleanup_thread, which depends on lttng_daemonize, which | |
2540 | * depends on set_options. | |
2541 | */ | |
fac6795d | 2542 | progname = argv[0]; |
4a15001e MD |
2543 | if (set_options(argc, argv)) { |
2544 | retval = -1; | |
2545 | goto exit_options; | |
fac6795d DG |
2546 | } |
2547 | ||
e6142f2e JG |
2548 | /* |
2549 | * Resolve all paths received as arguments, configuration option, or | |
2550 | * through environment variable as absolute paths. This is necessary | |
2551 | * since daemonizing causes the sessiond's current working directory | |
2552 | * to '/'. | |
2553 | */ | |
2554 | ret = sessiond_config_resolve_paths(&config); | |
2555 | if (ret) { | |
2556 | goto exit_options; | |
2557 | } | |
2558 | ||
2559 | /* Apply config. */ | |
2560 | lttng_opt_verbose = config.verbose; | |
2561 | lttng_opt_quiet = config.quiet; | |
2562 | kconsumer_data.err_unix_sock_path = | |
2563 | config.kconsumerd_err_unix_sock_path.value; | |
2564 | kconsumer_data.cmd_unix_sock_path = | |
2565 | config.kconsumerd_cmd_unix_sock_path.value; | |
2566 | ustconsumer32_data.err_unix_sock_path = | |
2567 | config.consumerd32_err_unix_sock_path.value; | |
2568 | ustconsumer32_data.cmd_unix_sock_path = | |
2569 | config.consumerd32_cmd_unix_sock_path.value; | |
2570 | ustconsumer64_data.err_unix_sock_path = | |
2571 | config.consumerd64_err_unix_sock_path.value; | |
2572 | ustconsumer64_data.cmd_unix_sock_path = | |
2573 | config.consumerd64_cmd_unix_sock_path.value; | |
2574 | set_clock_plugin_env(); | |
2575 | ||
2576 | sessiond_config_log(&config); | |
2577 | ||
f472090a JG |
2578 | if (create_lttng_rundir()) { |
2579 | retval = -1; | |
2580 | goto exit_options; | |
2581 | } | |
2582 | ||
2583 | /* Abort launch if a session daemon is already running. */ | |
2584 | if (check_existing_daemon()) { | |
2585 | ERR("A session daemon is already running."); | |
2586 | retval = -1; | |
2587 | goto exit_options; | |
2588 | } | |
2589 | ||
fac6795d | 2590 | /* Daemonize */ |
e6142f2e | 2591 | if (config.daemonize || config.background) { |
ceed52b5 MD |
2592 | int i; |
2593 | ||
72dd7491 | 2594 | ret = lttng_daemonize(&child_ppid, &recv_child_signal, |
e6142f2e | 2595 | !config.background); |
53094c05 | 2596 | if (ret < 0) { |
4a15001e MD |
2597 | retval = -1; |
2598 | goto exit_options; | |
53094c05 | 2599 | } |
0bb7724a | 2600 | |
ceed52b5 | 2601 | /* |
0bb7724a | 2602 | * We are in the child. Make sure all other file descriptors are |
4a15001e | 2603 | * closed, in case we are called with more opened file |
f472090a | 2604 | * descriptors than the standard ones and the lock file. |
ceed52b5 MD |
2605 | */ |
2606 | for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) { | |
f472090a JG |
2607 | if (i == lockfile_fd) { |
2608 | continue; | |
2609 | } | |
ceed52b5 MD |
2610 | (void) close(i); |
2611 | } | |
2612 | } | |
2613 | ||
7567352f MD |
2614 | if (run_as_create_worker(argv[0]) < 0) { |
2615 | goto exit_create_run_as_worker_cleanup; | |
2616 | } | |
2617 | ||
4a15001e MD |
2618 | /* |
2619 | * Starting from here, we can create threads. This needs to be after | |
2620 | * lttng_daemonize due to RCU. | |
2621 | */ | |
2622 | ||
2623 | /* | |
2624 | * Initialize the health check subsystem. This call should set the | |
2625 | * appropriate time values. | |
2626 | */ | |
2627 | health_sessiond = health_app_create(NR_HEALTH_SESSIOND_TYPES); | |
2628 | if (!health_sessiond) { | |
2629 | PERROR("health_app_create error"); | |
2630 | retval = -1; | |
2631 | goto exit_health_sessiond_cleanup; | |
2632 | } | |
2633 | ||
4a15001e | 2634 | /* Create thread to clean up RCU hash tables */ |
a3707772 JG |
2635 | ht_cleanup_thread = launch_ht_cleanup_thread(); |
2636 | if (!ht_cleanup_thread) { | |
4a15001e MD |
2637 | retval = -1; |
2638 | goto exit_ht_cleanup; | |
2639 | } | |
2640 | ||
ceed52b5 | 2641 | /* Create thread quit pipe */ |
a7333da7 | 2642 | if (sessiond_init_thread_quit_pipe()) { |
4a15001e MD |
2643 | retval = -1; |
2644 | goto exit_init_data; | |
fac6795d DG |
2645 | } |
2646 | ||
2647 | /* Check if daemon is UID = 0 */ | |
2648 | is_root = !getuid(); | |
fac6795d | 2649 | if (is_root) { |
67e40797 | 2650 | /* Create global run dir with root access */ |
67e40797 | 2651 | |
b3530820 JG |
2652 | kernel_channel_monitor_pipe = lttng_pipe_open(0); |
2653 | if (!kernel_channel_monitor_pipe) { | |
2654 | ERR("Failed to create kernel consumer channel monitor pipe"); | |
2655 | retval = -1; | |
2656 | goto exit_init_data; | |
2657 | } | |
2658 | kconsumer_data.channel_monitor_pipe = | |
2659 | lttng_pipe_release_writefd( | |
2660 | kernel_channel_monitor_pipe); | |
2661 | if (kconsumer_data.channel_monitor_pipe < 0) { | |
2662 | retval = -1; | |
2663 | goto exit_init_data; | |
2664 | } | |
fac6795d DG |
2665 | } |
2666 | ||
5c827ce0 DG |
2667 | /* Set consumer initial state */ |
2668 | kernel_consumerd_state = CONSUMER_STOPPED; | |
2669 | ust_consumerd_state = CONSUMER_STOPPED; | |
2670 | ||
b3530820 JG |
2671 | ust32_channel_monitor_pipe = lttng_pipe_open(0); |
2672 | if (!ust32_channel_monitor_pipe) { | |
2673 | ERR("Failed to create 32-bit user space consumer channel monitor pipe"); | |
2674 | retval = -1; | |
2675 | goto exit_init_data; | |
2676 | } | |
2677 | ustconsumer32_data.channel_monitor_pipe = lttng_pipe_release_writefd( | |
2678 | ust32_channel_monitor_pipe); | |
2679 | if (ustconsumer32_data.channel_monitor_pipe < 0) { | |
2680 | retval = -1; | |
2681 | goto exit_init_data; | |
2682 | } | |
62c43103 | 2683 | |
d086f507 | 2684 | /* |
92816cc3 JG |
2685 | * The rotation_thread_timer_queue structure is shared between the |
2686 | * sessiond timer thread and the rotation thread. The main thread keeps | |
2687 | * its ownership and destroys it when both threads have been joined. | |
d086f507 | 2688 | */ |
92816cc3 | 2689 | rotation_timer_queue = rotation_thread_timer_queue_create(); |
d086f507 JD |
2690 | if (!rotation_timer_queue) { |
2691 | retval = -1; | |
2692 | goto exit_init_data; | |
2693 | } | |
bc26e826 JG |
2694 | timer_thread_parameters.rotation_thread_job_queue = |
2695 | rotation_timer_queue; | |
67e40797 | 2696 | |
b3530820 JG |
2697 | ust64_channel_monitor_pipe = lttng_pipe_open(0); |
2698 | if (!ust64_channel_monitor_pipe) { | |
2699 | ERR("Failed to create 64-bit user space consumer channel monitor pipe"); | |
2700 | retval = -1; | |
2701 | goto exit_init_data; | |
2702 | } | |
2703 | ustconsumer64_data.channel_monitor_pipe = lttng_pipe_release_writefd( | |
2704 | ust64_channel_monitor_pipe); | |
2705 | if (ustconsumer64_data.channel_monitor_pipe < 0) { | |
2706 | retval = -1; | |
2707 | goto exit_init_data; | |
2708 | } | |
847177cd | 2709 | |
1427f9b2 DG |
2710 | /* |
2711 | * Init UST app hash table. Alloc hash table before this point since | |
2712 | * cleanup() can get called after that point. | |
2713 | */ | |
4a15001e | 2714 | if (ust_app_ht_alloc()) { |
ddbeb0f6 | 2715 | ERR("Failed to allocate UST app hash table"); |
4a15001e MD |
2716 | retval = -1; |
2717 | goto exit_init_data; | |
2718 | } | |
1427f9b2 | 2719 | |
6a4e4039 JG |
2720 | /* |
2721 | * Initialize agent app hash table. We allocate the hash table here | |
2722 | * since cleanup() can get called after this point. | |
2723 | */ | |
2724 | if (agent_app_ht_alloc()) { | |
2725 | ERR("Failed to allocate Agent app hash table"); | |
4a15001e MD |
2726 | retval = -1; |
2727 | goto exit_init_data; | |
f20baf8e DG |
2728 | } |
2729 | ||
a88df331 DG |
2730 | /* |
2731 | * These actions must be executed as root. We do that *after* setting up | |
2732 | * the sockets path because we MUST make the check for another daemon using | |
2733 | * those paths *before* trying to set the kernel consumer sockets and init | |
2734 | * kernel tracer. | |
2735 | */ | |
2736 | if (is_root) { | |
e6142f2e | 2737 | if (set_consumer_sockets(&kconsumer_data)) { |
4a15001e MD |
2738 | retval = -1; |
2739 | goto exit_init_data; | |
7753dea8 MD |
2740 | } |
2741 | ||
a88df331 | 2742 | /* Setup kernel tracer */ |
e6142f2e | 2743 | if (!config.no_kernel) { |
4fba7219 | 2744 | init_kernel_tracer(); |
89588270 DG |
2745 | if (kernel_tracer_fd >= 0) { |
2746 | ret = syscall_init_table(); | |
2747 | if (ret < 0) { | |
4a15001e MD |
2748 | ERR("Unable to populate syscall table. " |
2749 | "Syscall tracing won't work " | |
2750 | "for this session daemon."); | |
89588270 | 2751 | } |
834978fd | 2752 | } |
4fba7219 | 2753 | } |
a88df331 DG |
2754 | |
2755 | /* Set ulimit for open files */ | |
2756 | set_ulimit(); | |
fac6795d | 2757 | } |
4063050c MD |
2758 | /* init lttng_fd tracking must be done after set_ulimit. */ |
2759 | lttng_fd_init(); | |
fac6795d | 2760 | |
e6142f2e | 2761 | if (set_consumer_sockets(&ustconsumer64_data)) { |
4a15001e MD |
2762 | retval = -1; |
2763 | goto exit_init_data; | |
67e40797 DG |
2764 | } |
2765 | ||
e6142f2e | 2766 | if (set_consumer_sockets(&ustconsumer32_data)) { |
4a15001e MD |
2767 | retval = -1; |
2768 | goto exit_init_data; | |
67e40797 DG |
2769 | } |
2770 | ||
d6f42150 | 2771 | /* Setup the needed unix socket */ |
4a15001e MD |
2772 | if (init_daemon_socket()) { |
2773 | retval = -1; | |
2774 | goto exit_init_data; | |
fac6795d DG |
2775 | } |
2776 | ||
2777 | /* Set credentials to socket */ | |
e6142f2e | 2778 | if (is_root && set_permissions(config.rundir.value)) { |
4a15001e MD |
2779 | retval = -1; |
2780 | goto exit_init_data; | |
fac6795d DG |
2781 | } |
2782 | ||
5b8719f5 | 2783 | /* Get parent pid if -S, --sig-parent is specified. */ |
e6142f2e | 2784 | if (config.sig_parent) { |
5b8719f5 DG |
2785 | ppid = getppid(); |
2786 | } | |
2787 | ||
7a485870 | 2788 | /* Setup the kernel pipe for waking up the kernel thread */ |
e6142f2e | 2789 | if (is_root && !config.no_kernel) { |
4a15001e MD |
2790 | if (utils_create_pipe_cloexec(kernel_poll_pipe)) { |
2791 | retval = -1; | |
2792 | goto exit_init_data; | |
6620da75 | 2793 | } |
7a485870 DG |
2794 | } |
2795 | ||
099e26bd | 2796 | /* Setup the thread apps communication pipe. */ |
4a15001e MD |
2797 | if (utils_create_pipe_cloexec(apps_cmd_pipe)) { |
2798 | retval = -1; | |
2799 | goto exit_init_data; | |
099e26bd DG |
2800 | } |
2801 | ||
d0b96690 | 2802 | /* Setup the thread apps notify communication pipe. */ |
4a15001e MD |
2803 | if (utils_create_pipe_cloexec(apps_cmd_notify_pipe)) { |
2804 | retval = -1; | |
2805 | goto exit_init_data; | |
d0b96690 DG |
2806 | } |
2807 | ||
7972aab2 DG |
2808 | /* Initialize global buffer per UID and PID registry. */ |
2809 | buffer_reg_init_uid_registry(); | |
2810 | buffer_reg_init_pid_registry(); | |
2811 | ||
099e26bd | 2812 | /* Init UST command queue. */ |
8bdee6e2 | 2813 | cds_wfcq_init(&ust_cmd_queue.head, &ust_cmd_queue.tail); |
099e26bd | 2814 | |
2f77fc4b | 2815 | cmd_init(); |
00e2e675 | 2816 | |
ae9e45b3 DG |
2817 | /* Check for the application socket timeout env variable. */ |
2818 | env_app_timeout = getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV); | |
2819 | if (env_app_timeout) { | |
e6142f2e | 2820 | config.app_socket_timeout = atoi(env_app_timeout); |
ae9e45b3 | 2821 | } else { |
e6142f2e | 2822 | config.app_socket_timeout = DEFAULT_APP_SOCKET_RW_TIMEOUT; |
ae9e45b3 DG |
2823 | } |
2824 | ||
4a15001e MD |
2825 | ret = write_pidfile(); |
2826 | if (ret) { | |
2827 | ERR("Error in write_pidfile"); | |
2828 | retval = -1; | |
2829 | goto exit_init_data; | |
2830 | } | |
35f90c40 | 2831 | |
554831e7 MD |
2832 | /* Initialize communication library */ |
2833 | lttcomm_init(); | |
4a15001e | 2834 | /* Initialize TCP timeout values */ |
d831c249 | 2835 | lttcomm_inet_init(); |
554831e7 | 2836 | |
ef367a93 | 2837 | if (load_session_init_data(&load_info) < 0) { |
4a15001e MD |
2838 | retval = -1; |
2839 | goto exit_init_data; | |
ef367a93 | 2840 | } |
e6142f2e | 2841 | load_info->path = config.load_session_path.value; |
ef367a93 | 2842 | |
12b4fb37 | 2843 | /* Create health-check thread. */ |
173fca4f | 2844 | if (!launch_health_management_thread()) { |
4a15001e | 2845 | retval = -1; |
44a5e5eb DG |
2846 | goto exit_health; |
2847 | } | |
2848 | ||
b3530820 JG |
2849 | /* notification_thread_data acquires the pipes' read side. */ |
2850 | notification_thread_handle = notification_thread_handle_create( | |
2851 | ust32_channel_monitor_pipe, | |
2852 | ust64_channel_monitor_pipe, | |
c8a9de5a | 2853 | kernel_channel_monitor_pipe); |
b3530820 JG |
2854 | if (!notification_thread_handle) { |
2855 | retval = -1; | |
2856 | ERR("Failed to create notification thread shared data"); | |
b3530820 JG |
2857 | goto exit_notification; |
2858 | } | |
2859 | ||
2860 | /* Create notification thread. */ | |
c8a9de5a | 2861 | if (!launch_notification_thread(notification_thread_handle)) { |
b3530820 | 2862 | retval = -1; |
b3530820 JG |
2863 | goto exit_notification; |
2864 | } | |
2865 | ||
d086f507 | 2866 | /* Create timer thread. */ |
bc26e826 | 2867 | if (!launch_timer_thread(&timer_thread_parameters)) { |
d086f507 | 2868 | retval = -1; |
d086f507 JD |
2869 | goto exit_notification; |
2870 | } | |
d086f507 | 2871 | |
db66e574 JD |
2872 | /* rotation_thread_data acquires the pipes' read side. */ |
2873 | rotation_thread_handle = rotation_thread_handle_create( | |
90936dcf | 2874 | rotation_timer_queue, |
c8a9de5a | 2875 | notification_thread_handle); |
db66e574 JD |
2876 | if (!rotation_thread_handle) { |
2877 | retval = -1; | |
2878 | ERR("Failed to create rotation thread shared data"); | |
2879 | stop_threads(); | |
2880 | goto exit_rotation; | |
2881 | } | |
db66e574 JD |
2882 | |
2883 | /* Create rotation thread. */ | |
64d9b072 | 2884 | if (!launch_rotation_thread(rotation_thread_handle)) { |
db66e574 | 2885 | retval = -1; |
db66e574 JD |
2886 | goto exit_rotation; |
2887 | } | |
2888 | ||
cf3af59e | 2889 | /* Create thread to manage the client socket */ |
917a718d JG |
2890 | client_thread = launch_client_thread(); |
2891 | if (!client_thread) { | |
4a15001e | 2892 | retval = -1; |
cf3af59e MD |
2893 | goto exit_client; |
2894 | } | |
fac6795d | 2895 | |
5d1b0219 JG |
2896 | if (!launch_ust_dispatch_thread(&ust_cmd_queue, apps_cmd_pipe[1], |
2897 | apps_cmd_notify_pipe[1])) { | |
4a15001e | 2898 | retval = -1; |
099e26bd DG |
2899 | goto exit_dispatch; |
2900 | } | |
2901 | ||
2902 | /* Create thread to manage application registration. */ | |
1a1a34b4 | 2903 | ret = pthread_create(®_apps_thread, default_pthread_attr(), |
099e26bd | 2904 | thread_registration_apps, (void *) NULL); |
4a15001e MD |
2905 | if (ret) { |
2906 | errno = ret; | |
76d7553f | 2907 | PERROR("pthread_create registration"); |
4a15001e | 2908 | retval = -1; |
b3530820 | 2909 | stop_threads(); |
099e26bd DG |
2910 | goto exit_reg_apps; |
2911 | } | |
2912 | ||
cf3af59e | 2913 | /* Create thread to manage application socket */ |
1a1a34b4 | 2914 | ret = pthread_create(&apps_thread, default_pthread_attr(), |
54d01ffb | 2915 | thread_manage_apps, (void *) NULL); |
4a15001e MD |
2916 | if (ret) { |
2917 | errno = ret; | |
d0b96690 | 2918 | PERROR("pthread_create apps"); |
4a15001e | 2919 | retval = -1; |
b3530820 | 2920 | stop_threads(); |
d0b96690 DG |
2921 | goto exit_apps; |
2922 | } | |
2923 | ||
2924 | /* Create thread to manage application notify socket */ | |
1a1a34b4 | 2925 | ret = pthread_create(&apps_notify_thread, default_pthread_attr(), |
d0b96690 | 2926 | ust_thread_manage_notify, (void *) NULL); |
4a15001e MD |
2927 | if (ret) { |
2928 | errno = ret; | |
f5c32ef1 | 2929 | PERROR("pthread_create notify"); |
4a15001e | 2930 | retval = -1; |
b3530820 | 2931 | stop_threads(); |
9563b0ad | 2932 | goto exit_apps_notify; |
cf3af59e | 2933 | } |
fac6795d | 2934 | |
022d91ba | 2935 | /* Create agent registration thread. */ |
1a1a34b4 | 2936 | ret = pthread_create(&agent_reg_thread, default_pthread_attr(), |
022d91ba | 2937 | agent_thread_manage_registration, (void *) NULL); |
4a15001e MD |
2938 | if (ret) { |
2939 | errno = ret; | |
022d91ba | 2940 | PERROR("pthread_create agent"); |
4a15001e | 2941 | retval = -1; |
b3530820 | 2942 | stop_threads(); |
022d91ba | 2943 | goto exit_agent_reg; |
4d076222 DG |
2944 | } |
2945 | ||
6620da75 | 2946 | /* Don't start this thread if kernel tracing is not requested nor root */ |
e6142f2e | 2947 | if (is_root && !config.no_kernel) { |
6620da75 | 2948 | /* Create kernel thread to manage kernel event */ |
1a1a34b4 | 2949 | ret = pthread_create(&kernel_thread, default_pthread_attr(), |
6620da75 | 2950 | thread_manage_kernel, (void *) NULL); |
4a15001e MD |
2951 | if (ret) { |
2952 | errno = ret; | |
6620da75 | 2953 | PERROR("pthread_create kernel"); |
4a15001e | 2954 | retval = -1; |
b3530820 | 2955 | stop_threads(); |
6620da75 DG |
2956 | goto exit_kernel; |
2957 | } | |
ef367a93 | 2958 | } |
7a485870 | 2959 | |
4a15001e | 2960 | /* Create session loading thread. */ |
1a1a34b4 MJ |
2961 | ret = pthread_create(&load_session_thread, default_pthread_attr(), |
2962 | thread_load_session, load_info); | |
4a15001e MD |
2963 | if (ret) { |
2964 | errno = ret; | |
2965 | PERROR("pthread_create load_session_thread"); | |
2966 | retval = -1; | |
b3530820 | 2967 | stop_threads(); |
4a15001e MD |
2968 | goto exit_load_session; |
2969 | } | |
2970 | ||
2971 | /* | |
2972 | * This is where we start awaiting program completion (e.g. through | |
2973 | * signal that asks threads to teardown). | |
2974 | */ | |
2975 | ||
2976 | ret = pthread_join(load_session_thread, &status); | |
2977 | if (ret) { | |
2978 | errno = ret; | |
2979 | PERROR("pthread_join load_session_thread"); | |
2980 | retval = -1; | |
2981 | } | |
99d688f2 JG |
2982 | |
2983 | /* Initiate teardown once activity occurs on the quit pipe. */ | |
2984 | sessiond_wait_for_quit_pipe(-1U); | |
917a718d JG |
2985 | |
2986 | /* | |
2987 | * Ensure that the client thread is no longer accepting new commands, | |
2988 | * which could cause new sessions to be created. | |
2989 | */ | |
2990 | if (!lttng_thread_shutdown(client_thread)) { | |
2991 | ERR("Failed to shutdown the client thread, continuing teardown"); | |
2992 | lttng_thread_put(client_thread); | |
2993 | client_thread = NULL; | |
2994 | } | |
2995 | ||
99d688f2 | 2996 | destroy_all_sessions_and_wait(); |
4a15001e | 2997 | exit_load_session: |
ef367a93 | 2998 | |
e6142f2e | 2999 | if (is_root && !config.no_kernel) { |
6620da75 | 3000 | ret = pthread_join(kernel_thread, &status); |
4a15001e MD |
3001 | if (ret) { |
3002 | errno = ret; | |
6620da75 | 3003 | PERROR("pthread_join"); |
4a15001e | 3004 | retval = -1; |
6620da75 | 3005 | } |
fac6795d | 3006 | } |
cf3af59e | 3007 | exit_kernel: |
4a15001e | 3008 | |
022d91ba | 3009 | ret = pthread_join(agent_reg_thread, &status); |
4a15001e MD |
3010 | if (ret) { |
3011 | errno = ret; | |
022d91ba | 3012 | PERROR("pthread_join agent"); |
4a15001e | 3013 | retval = -1; |
4d076222 | 3014 | } |
022d91ba | 3015 | exit_agent_reg: |
4a15001e | 3016 | |
9563b0ad | 3017 | ret = pthread_join(apps_notify_thread, &status); |
4a15001e MD |
3018 | if (ret) { |
3019 | errno = ret; | |
9563b0ad | 3020 | PERROR("pthread_join apps notify"); |
4a15001e | 3021 | retval = -1; |
9563b0ad | 3022 | } |
9563b0ad | 3023 | exit_apps_notify: |
4a15001e | 3024 | |
cf3af59e | 3025 | ret = pthread_join(apps_thread, &status); |
4a15001e MD |
3026 | if (ret) { |
3027 | errno = ret; | |
9563b0ad | 3028 | PERROR("pthread_join apps"); |
4a15001e | 3029 | retval = -1; |
cf3af59e | 3030 | } |
cf3af59e | 3031 | exit_apps: |
4a15001e | 3032 | |
099e26bd | 3033 | ret = pthread_join(reg_apps_thread, &status); |
4a15001e MD |
3034 | if (ret) { |
3035 | errno = ret; | |
76d7553f | 3036 | PERROR("pthread_join"); |
4a15001e | 3037 | retval = -1; |
099e26bd | 3038 | } |
099e26bd | 3039 | exit_reg_apps: |
099e26bd | 3040 | exit_dispatch: |
b3530820 | 3041 | exit_client: |
db66e574 | 3042 | exit_rotation: |
b3530820 | 3043 | exit_notification: |
a3707772 | 3044 | lttng_thread_list_shutdown_orphans(); |
b3530820 | 3045 | exit_health: |
4a15001e | 3046 | exit_init_data: |
917a718d JG |
3047 | if (client_thread) { |
3048 | lttng_thread_put(client_thread); | |
3049 | } | |
3050 | ||
4d62fbf8 MD |
3051 | /* |
3052 | * Wait for all pending call_rcu work to complete before tearing | |
3053 | * down data structures. call_rcu worker may be trying to | |
3054 | * perform lookups in those structures. | |
3055 | */ | |
3056 | rcu_barrier(); | |
4a15001e MD |
3057 | /* |
3058 | * sessiond_cleanup() is called when no other thread is running, except | |
3059 | * the ht_cleanup thread, which is needed to destroy the hash tables. | |
3060 | */ | |
3061 | rcu_thread_online(); | |
3062 | sessiond_cleanup(); | |
06f525de | 3063 | |
c4d5de9d JG |
3064 | /* |
3065 | * Ensure all prior call_rcu are done. call_rcu callbacks may push | |
3066 | * hash tables to the ht_cleanup thread. Therefore, we ensure that | |
3067 | * the queue is empty before shutting down the clean-up thread. | |
3068 | */ | |
3069 | rcu_barrier(); | |
3070 | ||
a3707772 JG |
3071 | if (ht_cleanup_thread) { |
3072 | lttng_thread_shutdown(ht_cleanup_thread); | |
3073 | lttng_thread_put(ht_cleanup_thread); | |
3074 | } | |
3075 | ||
64d9b072 JG |
3076 | rcu_thread_offline(); |
3077 | rcu_unregister_thread(); | |
3078 | ||
3079 | if (rotation_thread_handle) { | |
3080 | rotation_thread_handle_destroy(rotation_thread_handle); | |
3081 | } | |
3082 | ||
d086f507 JD |
3083 | /* |
3084 | * After the rotation and timer thread have quit, we can safely destroy | |
3085 | * the rotation_timer_queue. | |
3086 | */ | |
92816cc3 | 3087 | rotation_thread_timer_queue_destroy(rotation_timer_queue); |
c8a9de5a JG |
3088 | /* |
3089 | * The teardown of the notification system is performed after the | |
3090 | * session daemon's teardown in order to allow it to be notified | |
3091 | * of the active session and channels at the moment of the teardown. | |
3092 | */ | |
3093 | if (notification_thread_handle) { | |
3094 | notification_thread_handle_destroy(notification_thread_handle); | |
3095 | } | |
b3530820 JG |
3096 | lttng_pipe_destroy(ust32_channel_monitor_pipe); |
3097 | lttng_pipe_destroy(ust64_channel_monitor_pipe); | |
3098 | lttng_pipe_destroy(kernel_channel_monitor_pipe); | |
0b2dc8df | 3099 | exit_ht_cleanup: |
4a15001e | 3100 | |
4a15001e MD |
3101 | health_app_destroy(health_sessiond); |
3102 | exit_health_sessiond_cleanup: | |
7567352f | 3103 | exit_create_run_as_worker_cleanup: |
4a15001e MD |
3104 | |
3105 | exit_options: | |
f472090a | 3106 | sessiond_cleanup_lock_file(); |
4a15001e MD |
3107 | sessiond_cleanup_options(); |
3108 | ||
3109 | exit_set_signal_handler: | |
3110 | if (!retval) { | |
cf3af59e | 3111 | exit(EXIT_SUCCESS); |
4a15001e MD |
3112 | } else { |
3113 | exit(EXIT_FAILURE); | |
67e40797 | 3114 | } |
fac6795d | 3115 | } |