Commit | Line | Data |
---|---|---|
826d496d | 1 | /* |
21cf9b6b | 2 | * Copyright (C) 2011 EfficiOS Inc. |
ab5be9fa MJ |
3 | * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
4 | * Copyright (C) 2013 Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
fac6795d | 5 | * |
ab5be9fa | 6 | * SPDX-License-Identifier: GPL-2.0-only |
91d76f53 | 7 | * |
fac6795d DG |
8 | */ |
9 | ||
6c1c0768 | 10 | #define _LGPL_SOURCE |
fac6795d DG |
11 | #include <getopt.h> |
12 | #include <grp.h> | |
13 | #include <limits.h> | |
0bb7724a | 14 | #include <paths.h> |
fac6795d DG |
15 | #include <pthread.h> |
16 | #include <signal.h> | |
17 | #include <stdio.h> | |
18 | #include <stdlib.h> | |
19 | #include <string.h> | |
331744e3 | 20 | #include <inttypes.h> |
0fdd1e2c | 21 | #include <sys/mman.h> |
b73401da | 22 | #include <sys/mount.h> |
1e307fab | 23 | #include <sys/resource.h> |
fac6795d DG |
24 | #include <sys/socket.h> |
25 | #include <sys/stat.h> | |
26 | #include <sys/types.h> | |
0fdd1e2c | 27 | #include <sys/wait.h> |
5c827ce0 | 28 | #include <urcu/uatomic.h> |
fac6795d | 29 | #include <unistd.h> |
4f0b90ee | 30 | #include <ctype.h> |
fac6795d | 31 | |
c9e313bc SM |
32 | #include <common/common.hpp> |
33 | #include <common/compat/socket.hpp> | |
34 | #include <common/compat/getenv.hpp> | |
35 | #include <common/defaults.hpp> | |
36 | #include <common/kernel-consumer/kernel-consumer.hpp> | |
37 | #include <common/futex.hpp> | |
38 | #include <common/relayd/relayd.hpp> | |
39 | #include <common/utils.hpp> | |
40 | #include <common/path.hpp> | |
41 | #include <common/daemonize.hpp> | |
42 | #include <common/config/session-config.hpp> | |
43 | #include <common/ini-config/ini-config.hpp> | |
44 | #include <common/dynamic-buffer.hpp> | |
e35e95ea JG |
45 | #include <common/logging-utils.hpp> |
46 | ||
c9e313bc SM |
47 | #include <lttng/event-internal.hpp> |
48 | #include "lttng-sessiond.hpp" | |
49 | #include "buffer-registry.hpp" | |
50 | #include "channel.hpp" | |
51 | #include "cmd.hpp" | |
52 | #include "consumer.hpp" | |
53 | #include "context.hpp" | |
54 | #include "event.hpp" | |
55 | #include "event-notifier-error-accounting.hpp" | |
56 | #include "kernel.hpp" | |
57 | #include "kernel-consumer.hpp" | |
58 | #include "lttng-ust-ctl.hpp" | |
59 | #include "ust-consumer.hpp" | |
60 | #include "utils.hpp" | |
61 | #include "fd-limit.hpp" | |
62 | #include "health-sessiond.hpp" | |
63 | #include "testpoint.hpp" | |
64 | #include "notify-apps.hpp" | |
65 | #include "agent-thread.hpp" | |
66 | #include "save.hpp" | |
67 | #include "notification-thread.hpp" | |
68 | #include "notification-thread-commands.hpp" | |
69 | #include "rotation-thread.hpp" | |
70 | #include "agent.hpp" | |
71 | #include "sessiond-config.hpp" | |
72 | #include "timer.hpp" | |
73 | #include "thread.hpp" | |
74 | #include "client.hpp" | |
75 | #include "dispatch.hpp" | |
76 | #include "register.hpp" | |
77 | #include "manage-apps.hpp" | |
78 | #include "manage-kernel.hpp" | |
79 | #include "modprobe.hpp" | |
80 | #include "ust-sigbus.hpp" | |
ebaeda94 | 81 | |
4fc83d94 PP |
82 | static const char *help_msg = |
83 | #ifdef LTTNG_EMBED_HELP | |
84 | #include <lttng-sessiond.8.h> | |
85 | #else | |
86 | NULL | |
87 | #endif | |
88 | ; | |
89 | ||
90aa04a1 | 90 | #define EVENT_NOTIFIER_ERROR_COUNTER_NUMBER_OF_BUCKET_MAX 65535 |
761ffce2 FD |
91 | #define EVENT_NOTIFIER_ERROR_BUFFER_SIZE_BASE_OPTION_STR \ |
92 | "event-notifier-error-buffer-size" | |
93 | #define EVENT_NOTIFIER_ERROR_BUFFER_SIZE_KERNEL_OPTION_STR \ | |
94 | EVENT_NOTIFIER_ERROR_BUFFER_SIZE_BASE_OPTION_STR "-kernel" | |
95 | #define EVENT_NOTIFIER_ERROR_BUFFER_SIZE_USERSPACE_OPTION_STR \ | |
96 | EVENT_NOTIFIER_ERROR_BUFFER_SIZE_BASE_OPTION_STR "-userspace" | |
97 | ||
90aa04a1 | 98 | |
fac6795d | 99 | const char *progname; |
c9cb3e7d | 100 | static int lockfile_fd = -1; |
a3bc3918 | 101 | static int opt_print_version; |
3bd1e081 | 102 | |
0bb7724a DG |
103 | /* Set to 1 when a SIGUSR1 signal is received. */ |
104 | static int recv_child_signal; | |
105 | ||
26296c48 JG |
106 | /* Command line options */ |
107 | static const struct option long_options[] = { | |
0f5ea17c JG |
108 | { "client-sock", required_argument, 0, 'c' }, |
109 | { "apps-sock", required_argument, 0, 'a' }, | |
110 | { "kconsumerd-cmd-sock", required_argument, 0, '\0' }, | |
111 | { "kconsumerd-err-sock", required_argument, 0, '\0' }, | |
112 | { "ustconsumerd32-cmd-sock", required_argument, 0, '\0' }, | |
113 | { "ustconsumerd32-err-sock", required_argument, 0, '\0' }, | |
114 | { "ustconsumerd64-cmd-sock", required_argument, 0, '\0' }, | |
115 | { "ustconsumerd64-err-sock", required_argument, 0, '\0' }, | |
116 | { "consumerd32-path", required_argument, 0, '\0' }, | |
117 | { "consumerd32-libdir", required_argument, 0, '\0' }, | |
118 | { "consumerd64-path", required_argument, 0, '\0' }, | |
119 | { "consumerd64-libdir", required_argument, 0, '\0' }, | |
120 | { "daemonize", no_argument, 0, 'd' }, | |
121 | { "background", no_argument, 0, 'b' }, | |
122 | { "sig-parent", no_argument, 0, 'S' }, | |
123 | { "help", no_argument, 0, 'h' }, | |
124 | { "group", required_argument, 0, 'g' }, | |
125 | { "version", no_argument, 0, 'V' }, | |
126 | { "quiet", no_argument, 0, 'q' }, | |
127 | { "verbose", no_argument, 0, 'v' }, | |
128 | { "verbose-consumer", no_argument, 0, '\0' }, | |
129 | { "no-kernel", no_argument, 0, '\0' }, | |
130 | { "pidfile", required_argument, 0, 'p' }, | |
131 | { "agent-tcp-port", required_argument, 0, '\0' }, | |
132 | { "config", required_argument, 0, 'f' }, | |
133 | { "load", required_argument, 0, 'l' }, | |
134 | { "kmod-probes", required_argument, 0, '\0' }, | |
135 | { "extra-kmod-probes", required_argument, 0, '\0' }, | |
761ffce2 FD |
136 | { EVENT_NOTIFIER_ERROR_BUFFER_SIZE_KERNEL_OPTION_STR, required_argument, 0, '\0' }, |
137 | { EVENT_NOTIFIER_ERROR_BUFFER_SIZE_USERSPACE_OPTION_STR, required_argument, 0, '\0' }, | |
26296c48 JG |
138 | { NULL, 0, 0, 0 } |
139 | }; | |
140 | ||
141 | /* Command line options to ignore from configuration file */ | |
142 | static const char *config_ignore_options[] = { "help", "version", "config" }; | |
1d4b027a | 143 | |
099e26bd DG |
144 | /* |
145 | * This pipe is used to inform the thread managing application communication | |
146 | * that a command is queued and ready to be processed. | |
147 | */ | |
76d7553f | 148 | static int apps_cmd_pipe[2] = { -1, -1 }; |
971a61c6 | 149 | static int apps_cmd_notify_pipe[2] = { -1, -1 }; |
099e26bd | 150 | |
099e26bd DG |
151 | /* |
152 | * UST registration command queue. This queue is tied with a futex and uses a N | |
153 | * wakers / 1 waiter implemented and detailed in futex.c/.h | |
154 | * | |
b22c5da8 DG |
155 | * The thread_registration_apps and thread_dispatch_ust_registration uses this |
156 | * queue along with the wait/wake scheme. The thread_manage_apps receives down | |
157 | * the line new application socket and monitors it for any I/O error or clean | |
158 | * close that triggers an unregistration of the application. | |
099e26bd DG |
159 | */ |
160 | static struct ust_cmd_queue ust_cmd_queue; | |
161 | ||
97bc1426 | 162 | /* |
a7333da7 | 163 | * Section name to look for in the daemon configuration file. |
97bc1426 | 164 | */ |
a7333da7 | 165 | static const char * const config_section_name = "sessiond"; |
5e97de00 | 166 | |
a7333da7 JG |
167 | /* Am I root or not. Set to 1 if the daemon is running as root */ |
168 | static int is_root; | |
5eb91c98 | 169 | |
099e26bd | 170 | /* |
f90a04ed MJ |
171 | * Notify the main thread to initiate the teardown of the worker threads by |
172 | * writing to the main quit pipe. | |
099e26bd | 173 | */ |
f90a04ed | 174 | static void notify_main_quit_pipe(void) |
cf3af59e | 175 | { |
5eb91c98 DG |
176 | int ret; |
177 | ||
cf3af59e | 178 | /* Stopping all threads */ |
f90a04ed MJ |
179 | DBG("Notify the main thread to terminate all worker threads"); |
180 | ret = sessiond_notify_main_quit_pipe(); | |
5eb91c98 | 181 | if (ret < 0) { |
f90a04ed | 182 | ERR("write error on main quit pipe"); |
5eb91c98 | 183 | } |
cf3af59e MD |
184 | } |
185 | ||
e975f9f8 DG |
186 | /* |
187 | * Close every consumer sockets. | |
188 | */ | |
189 | static void close_consumer_sockets(void) | |
190 | { | |
191 | int ret; | |
192 | ||
412d7227 SM |
193 | if (the_kconsumer_data.err_sock >= 0) { |
194 | ret = close(the_kconsumer_data.err_sock); | |
e975f9f8 DG |
195 | if (ret < 0) { |
196 | PERROR("kernel consumer err_sock close"); | |
197 | } | |
198 | } | |
412d7227 SM |
199 | if (the_ustconsumer32_data.err_sock >= 0) { |
200 | ret = close(the_ustconsumer32_data.err_sock); | |
e975f9f8 | 201 | if (ret < 0) { |
a76cbd9f | 202 | PERROR("UST consumerd32 err_sock close"); |
e975f9f8 DG |
203 | } |
204 | } | |
412d7227 SM |
205 | if (the_ustconsumer64_data.err_sock >= 0) { |
206 | ret = close(the_ustconsumer64_data.err_sock); | |
e975f9f8 | 207 | if (ret < 0) { |
a76cbd9f | 208 | PERROR("UST consumerd64 err_sock close"); |
e975f9f8 DG |
209 | } |
210 | } | |
412d7227 SM |
211 | if (the_kconsumer_data.cmd_sock >= 0) { |
212 | ret = close(the_kconsumer_data.cmd_sock); | |
e975f9f8 DG |
213 | if (ret < 0) { |
214 | PERROR("kernel consumer cmd_sock close"); | |
215 | } | |
216 | } | |
412d7227 SM |
217 | if (the_ustconsumer32_data.cmd_sock >= 0) { |
218 | ret = close(the_ustconsumer32_data.cmd_sock); | |
e975f9f8 | 219 | if (ret < 0) { |
a76cbd9f | 220 | PERROR("UST consumerd32 cmd_sock close"); |
e975f9f8 DG |
221 | } |
222 | } | |
412d7227 SM |
223 | if (the_ustconsumer64_data.cmd_sock >= 0) { |
224 | ret = close(the_ustconsumer64_data.cmd_sock); | |
e975f9f8 | 225 | if (ret < 0) { |
a76cbd9f | 226 | PERROR("UST consumerd64 cmd_sock close"); |
e975f9f8 DG |
227 | } |
228 | } | |
412d7227 SM |
229 | if (the_kconsumer_data.channel_monitor_pipe >= 0) { |
230 | ret = close(the_kconsumer_data.channel_monitor_pipe); | |
b3530820 JG |
231 | if (ret < 0) { |
232 | PERROR("kernel consumer channel monitor pipe close"); | |
233 | } | |
234 | } | |
412d7227 SM |
235 | if (the_ustconsumer32_data.channel_monitor_pipe >= 0) { |
236 | ret = close(the_ustconsumer32_data.channel_monitor_pipe); | |
b3530820 JG |
237 | if (ret < 0) { |
238 | PERROR("UST consumerd32 channel monitor pipe close"); | |
239 | } | |
240 | } | |
412d7227 SM |
241 | if (the_ustconsumer64_data.channel_monitor_pipe >= 0) { |
242 | ret = close(the_ustconsumer64_data.channel_monitor_pipe); | |
b3530820 JG |
243 | if (ret < 0) { |
244 | PERROR("UST consumerd64 channel monitor pipe close"); | |
245 | } | |
246 | } | |
e975f9f8 DG |
247 | } |
248 | ||
4e4714cb JR |
249 | /* |
250 | * Wait on consumer process termination. | |
251 | * | |
252 | * Need to be called with the consumer data lock held or from a context | |
253 | * ensuring no concurrent access to data (e.g: cleanup). | |
254 | */ | |
255 | static void wait_consumer(struct consumer_data *consumer_data) | |
256 | { | |
257 | pid_t ret; | |
258 | int status; | |
259 | ||
260 | if (consumer_data->pid <= 0) { | |
261 | return; | |
262 | } | |
263 | ||
264 | DBG("Waiting for complete teardown of consumerd (PID: %d)", | |
265 | consumer_data->pid); | |
266 | ret = waitpid(consumer_data->pid, &status, 0); | |
267 | if (ret == -1) { | |
268 | PERROR("consumerd waitpid pid: %d", consumer_data->pid) | |
1640c24c | 269 | } else if (!WIFEXITED(status)) { |
4e4714cb JR |
270 | ERR("consumerd termination with error: %d", |
271 | WEXITSTATUS(ret)); | |
272 | } | |
273 | consumer_data->pid = 0; | |
274 | } | |
275 | ||
fac6795d | 276 | /* |
4a15001e | 277 | * Cleanup the session daemon's data structures. |
fac6795d | 278 | */ |
4a15001e | 279 | static void sessiond_cleanup(void) |
fac6795d | 280 | { |
ef599319 | 281 | int ret; |
e32d7f27 | 282 | struct ltt_session_list *session_list = session_get_list(); |
fac6795d | 283 | |
4a15001e | 284 | DBG("Cleanup sessiond"); |
e07ae692 | 285 | |
4e449f3f | 286 | /* |
f90a04ed MJ |
287 | * Close the main quit pipe. It has already done its job, since we are |
288 | * now cleaning up. | |
4e449f3f | 289 | */ |
f90a04ed MJ |
290 | sessiond_close_main_quit_pipe(); |
291 | ||
292 | /* Close all other pipes. */ | |
971a61c6 JG |
293 | utils_close_pipe(apps_cmd_pipe); |
294 | utils_close_pipe(apps_cmd_notify_pipe); | |
412d7227 | 295 | utils_close_pipe(the_kernel_poll_pipe); |
2f77fc4b | 296 | |
412d7227 | 297 | ret = remove(the_config.pid_file_path.value); |
c9a2957d | 298 | if (ret < 0) { |
412d7227 | 299 | PERROR("remove pidfile %s", the_config.pid_file_path.value); |
35f90c40 DG |
300 | } |
301 | ||
e6142f2e | 302 | DBG("Removing sessiond and consumerd content of directory %s", |
412d7227 | 303 | the_config.rundir.value); |
8c6c56c2 MD |
304 | |
305 | /* sessiond */ | |
412d7227 SM |
306 | DBG("Removing %s", the_config.pid_file_path.value); |
307 | (void) unlink(the_config.pid_file_path.value); | |
8c6c56c2 | 308 | |
412d7227 SM |
309 | DBG("Removing %s", the_config.agent_port_file_path.value); |
310 | (void) unlink(the_config.agent_port_file_path.value); | |
cd9290dd | 311 | |
8c6c56c2 | 312 | /* kconsumerd */ |
412d7227 SM |
313 | DBG("Removing %s", the_kconsumer_data.err_unix_sock_path); |
314 | (void) unlink(the_kconsumer_data.err_unix_sock_path); | |
e6142f2e | 315 | |
412d7227 SM |
316 | DBG("Removing directory %s", the_config.kconsumerd_path.value); |
317 | (void) rmdir(the_config.kconsumerd_path.value); | |
8c6c56c2 MD |
318 | |
319 | /* ust consumerd 32 */ | |
412d7227 SM |
320 | DBG("Removing %s", the_config.consumerd32_err_unix_sock_path.value); |
321 | (void) unlink(the_config.consumerd32_err_unix_sock_path.value); | |
e6142f2e | 322 | |
412d7227 SM |
323 | DBG("Removing directory %s", the_config.consumerd32_path.value); |
324 | (void) rmdir(the_config.consumerd32_path.value); | |
8c6c56c2 MD |
325 | |
326 | /* ust consumerd 64 */ | |
412d7227 SM |
327 | DBG("Removing %s", the_config.consumerd64_err_unix_sock_path.value); |
328 | (void) unlink(the_config.consumerd64_err_unix_sock_path.value); | |
e6142f2e | 329 | |
412d7227 SM |
330 | DBG("Removing directory %s", the_config.consumerd64_path.value); |
331 | (void) rmdir(the_config.consumerd64_path.value); | |
5461b305 | 332 | |
99d688f2 | 333 | pthread_mutex_destroy(&session_list->lock); |
273ea72c | 334 | |
44760c20 JR |
335 | DBG("Cleaning up all per-event notifier domain agents"); |
336 | agent_by_event_notifier_domain_ht_destroy(); | |
337 | ||
6a4e4039 JG |
338 | DBG("Cleaning up all agent apps"); |
339 | agent_app_ht_clean(); | |
099e26bd | 340 | DBG("Closing all UST sockets"); |
56fff090 | 341 | ust_app_clean_list(); |
7972aab2 | 342 | buffer_reg_destroy_registries(); |
099e26bd | 343 | |
0787fc4b MD |
344 | close_consumer_sockets(); |
345 | ||
412d7227 SM |
346 | wait_consumer(&the_kconsumer_data); |
347 | wait_consumer(&the_ustconsumer64_data); | |
348 | wait_consumer(&the_ustconsumer32_data); | |
0787fc4b | 349 | |
412d7227 | 350 | if (is_root && !the_config.no_kernel) { |
7d268848 | 351 | cleanup_kernel_tracer(); |
2f50c8a3 | 352 | } |
2f77fc4b | 353 | |
c9cb3e7d JG |
354 | /* |
355 | * We do NOT rmdir rundir because there are other processes | |
356 | * using it, for instance lttng-relayd, which can start in | |
357 | * parallel with this teardown. | |
358 | */ | |
4a15001e MD |
359 | } |
360 | ||
361 | /* | |
362 | * Cleanup the daemon's option data structures. | |
363 | */ | |
364 | static void sessiond_cleanup_options(void) | |
365 | { | |
366 | DBG("Cleaning up options"); | |
367 | ||
412d7227 | 368 | sessiond_config_fini(&the_config); |
c9cb3e7d | 369 | |
7567352f | 370 | run_as_destroy_worker(); |
fac6795d DG |
371 | } |
372 | ||
917a718d | 373 | static int string_match(const char *str1, const char *str2) |
54d01ffb | 374 | { |
917a718d | 375 | return (str1 && str2) && !strcmp(str1, str2); |
54d01ffb DG |
376 | } |
377 | ||
00e2e675 | 378 | /* |
917a718d JG |
379 | * Take an option from the getopt output and set it in the right variable to be |
380 | * used later. | |
36b588ed | 381 | * |
917a718d | 382 | * Return 0 on success else a negative value. |
00e2e675 | 383 | */ |
917a718d | 384 | static int set_option(int opt, const char *arg, const char *optname) |
00e2e675 | 385 | { |
917a718d | 386 | int ret = 0; |
fac6795d | 387 | |
c0232ea5 | 388 | if (string_match(optname, "client-sock") || opt == 'c') { |
66b2ce8e JG |
389 | if (!arg || *arg == '\0') { |
390 | ret = -EINVAL; | |
391 | goto end; | |
392 | } | |
e8fa9fb0 MD |
393 | if (lttng_is_setuid_setgid()) { |
394 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
395 | "-c, --client-sock"); | |
396 | } else { | |
412d7227 | 397 | config_string_set(&the_config.client_unix_sock_path, |
e6142f2e | 398 | strdup(arg)); |
412d7227 | 399 | if (!the_config.client_unix_sock_path.value) { |
e6142f2e JG |
400 | ret = -ENOMEM; |
401 | PERROR("strdup"); | |
402 | } | |
e8fa9fb0 | 403 | } |
c0232ea5 | 404 | } else if (string_match(optname, "apps-sock") || opt == 'a') { |
66b2ce8e JG |
405 | if (!arg || *arg == '\0') { |
406 | ret = -EINVAL; | |
407 | goto end; | |
408 | } | |
e8fa9fb0 MD |
409 | if (lttng_is_setuid_setgid()) { |
410 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
411 | "-a, --apps-sock"); | |
412 | } else { | |
412d7227 | 413 | config_string_set(&the_config.apps_unix_sock_path, |
e6142f2e | 414 | strdup(arg)); |
412d7227 | 415 | if (!the_config.apps_unix_sock_path.value) { |
e6142f2e JG |
416 | ret = -ENOMEM; |
417 | PERROR("strdup"); | |
418 | } | |
e8fa9fb0 | 419 | } |
c0232ea5 | 420 | } else if (string_match(optname, "daemonize") || opt == 'd') { |
412d7227 | 421 | the_config.daemonize = true; |
c0232ea5 | 422 | } else if (string_match(optname, "background") || opt == 'b') { |
412d7227 | 423 | the_config.background = true; |
c0232ea5 | 424 | } else if (string_match(optname, "group") || opt == 'g') { |
66b2ce8e JG |
425 | if (!arg || *arg == '\0') { |
426 | ret = -EINVAL; | |
427 | goto end; | |
428 | } | |
e8fa9fb0 MD |
429 | if (lttng_is_setuid_setgid()) { |
430 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
431 | "-g, --group"); | |
432 | } else { | |
412d7227 | 433 | config_string_set(&the_config.tracing_group_name, |
e6142f2e | 434 | strdup(arg)); |
412d7227 | 435 | if (!the_config.tracing_group_name.value) { |
e8fa9fb0 | 436 | ret = -ENOMEM; |
e6142f2e | 437 | PERROR("strdup"); |
e8fa9fb0 | 438 | } |
db322c4d | 439 | } |
c0232ea5 | 440 | } else if (string_match(optname, "help") || opt == 'h') { |
4fc83d94 | 441 | ret = utils_show_help(8, "lttng-sessiond", help_msg); |
8190767e | 442 | if (ret) { |
4fc83d94 | 443 | ERR("Cannot show --help for `lttng-sessiond`"); |
8190767e PP |
444 | perror("exec"); |
445 | } | |
446 | exit(ret ? EXIT_FAILURE : EXIT_SUCCESS); | |
c0232ea5 | 447 | } else if (string_match(optname, "version") || opt == 'V') { |
a3bc3918 | 448 | opt_print_version = 1; |
c0232ea5 | 449 | } else if (string_match(optname, "sig-parent") || opt == 'S') { |
412d7227 | 450 | the_config.sig_parent = true; |
c0232ea5 | 451 | } else if (string_match(optname, "kconsumerd-err-sock")) { |
66b2ce8e JG |
452 | if (!arg || *arg == '\0') { |
453 | ret = -EINVAL; | |
454 | goto end; | |
455 | } | |
e8fa9fb0 MD |
456 | if (lttng_is_setuid_setgid()) { |
457 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
458 | "--kconsumerd-err-sock"); | |
459 | } else { | |
412d7227 SM |
460 | config_string_set( |
461 | &the_config.kconsumerd_err_unix_sock_path, | |
e6142f2e | 462 | strdup(arg)); |
412d7227 | 463 | if (!the_config.kconsumerd_err_unix_sock_path.value) { |
e6142f2e JG |
464 | ret = -ENOMEM; |
465 | PERROR("strdup"); | |
466 | } | |
e8fa9fb0 | 467 | } |
c0232ea5 | 468 | } else if (string_match(optname, "kconsumerd-cmd-sock")) { |
66b2ce8e JG |
469 | if (!arg || *arg == '\0') { |
470 | ret = -EINVAL; | |
471 | goto end; | |
472 | } | |
e8fa9fb0 MD |
473 | if (lttng_is_setuid_setgid()) { |
474 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
475 | "--kconsumerd-cmd-sock"); | |
476 | } else { | |
412d7227 SM |
477 | config_string_set( |
478 | &the_config.kconsumerd_cmd_unix_sock_path, | |
e6142f2e | 479 | strdup(arg)); |
412d7227 | 480 | if (!the_config.kconsumerd_cmd_unix_sock_path.value) { |
e6142f2e JG |
481 | ret = -ENOMEM; |
482 | PERROR("strdup"); | |
483 | } | |
e8fa9fb0 | 484 | } |
c0232ea5 | 485 | } else if (string_match(optname, "ustconsumerd64-err-sock")) { |
66b2ce8e JG |
486 | if (!arg || *arg == '\0') { |
487 | ret = -EINVAL; | |
488 | goto end; | |
489 | } | |
e8fa9fb0 MD |
490 | if (lttng_is_setuid_setgid()) { |
491 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
492 | "--ustconsumerd64-err-sock"); | |
493 | } else { | |
412d7227 SM |
494 | config_string_set( |
495 | &the_config.consumerd64_err_unix_sock_path, | |
e6142f2e | 496 | strdup(arg)); |
412d7227 | 497 | if (!the_config.consumerd64_err_unix_sock_path.value) { |
e6142f2e JG |
498 | ret = -ENOMEM; |
499 | PERROR("strdup"); | |
500 | } | |
e8fa9fb0 | 501 | } |
c0232ea5 | 502 | } else if (string_match(optname, "ustconsumerd64-cmd-sock")) { |
66b2ce8e JG |
503 | if (!arg || *arg == '\0') { |
504 | ret = -EINVAL; | |
505 | goto end; | |
506 | } | |
e8fa9fb0 MD |
507 | if (lttng_is_setuid_setgid()) { |
508 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
509 | "--ustconsumerd64-cmd-sock"); | |
510 | } else { | |
412d7227 SM |
511 | config_string_set( |
512 | &the_config.consumerd64_cmd_unix_sock_path, | |
e6142f2e | 513 | strdup(arg)); |
412d7227 | 514 | if (!the_config.consumerd64_cmd_unix_sock_path.value) { |
e6142f2e JG |
515 | ret = -ENOMEM; |
516 | PERROR("strdup"); | |
517 | } | |
e8fa9fb0 | 518 | } |
c0232ea5 | 519 | } else if (string_match(optname, "ustconsumerd32-err-sock")) { |
66b2ce8e JG |
520 | if (!arg || *arg == '\0') { |
521 | ret = -EINVAL; | |
522 | goto end; | |
523 | } | |
e8fa9fb0 MD |
524 | if (lttng_is_setuid_setgid()) { |
525 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
526 | "--ustconsumerd32-err-sock"); | |
527 | } else { | |
412d7227 SM |
528 | config_string_set( |
529 | &the_config.consumerd32_err_unix_sock_path, | |
e6142f2e | 530 | strdup(arg)); |
412d7227 | 531 | if (!the_config.consumerd32_err_unix_sock_path.value) { |
e6142f2e JG |
532 | ret = -ENOMEM; |
533 | PERROR("strdup"); | |
534 | } | |
e8fa9fb0 | 535 | } |
c0232ea5 | 536 | } else if (string_match(optname, "ustconsumerd32-cmd-sock")) { |
66b2ce8e JG |
537 | if (!arg || *arg == '\0') { |
538 | ret = -EINVAL; | |
539 | goto end; | |
540 | } | |
e8fa9fb0 MD |
541 | if (lttng_is_setuid_setgid()) { |
542 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
543 | "--ustconsumerd32-cmd-sock"); | |
544 | } else { | |
412d7227 SM |
545 | config_string_set( |
546 | &the_config.consumerd32_cmd_unix_sock_path, | |
e6142f2e | 547 | strdup(arg)); |
412d7227 | 548 | if (!the_config.consumerd32_cmd_unix_sock_path.value) { |
e6142f2e JG |
549 | ret = -ENOMEM; |
550 | PERROR("strdup"); | |
551 | } | |
e8fa9fb0 | 552 | } |
c0232ea5 | 553 | } else if (string_match(optname, "no-kernel")) { |
412d7227 | 554 | the_config.no_kernel = true; |
c0232ea5 | 555 | } else if (string_match(optname, "quiet") || opt == 'q') { |
412d7227 | 556 | the_config.quiet = true; |
c0232ea5 | 557 | } else if (string_match(optname, "verbose") || opt == 'v') { |
26296c48 JG |
558 | /* Verbose level can increase using multiple -v */ |
559 | if (arg) { | |
13755a18 | 560 | /* Value obtained from config file */ |
412d7227 | 561 | the_config.verbose = config_parse_value(arg); |
26296c48 | 562 | } else { |
13755a18 | 563 | /* -v used on command line */ |
412d7227 | 564 | the_config.verbose++; |
26296c48 | 565 | } |
13755a18 | 566 | /* Clamp value to [0, 3] */ |
412d7227 SM |
567 | the_config.verbose = the_config.verbose < 0 ? |
568 | 0 : | |
569 | (the_config.verbose <= 3 ? the_config.verbose : | |
570 | 3); | |
c0232ea5 | 571 | } else if (string_match(optname, "verbose-consumer")) { |
26296c48 | 572 | if (arg) { |
412d7227 | 573 | the_config.verbose_consumer = config_parse_value(arg); |
26296c48 | 574 | } else { |
412d7227 | 575 | the_config.verbose_consumer++; |
26296c48 | 576 | } |
c0232ea5 | 577 | } else if (string_match(optname, "consumerd32-path")) { |
66b2ce8e JG |
578 | if (!arg || *arg == '\0') { |
579 | ret = -EINVAL; | |
580 | goto end; | |
581 | } | |
e8fa9fb0 MD |
582 | if (lttng_is_setuid_setgid()) { |
583 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
584 | "--consumerd32-path"); | |
585 | } else { | |
412d7227 | 586 | config_string_set(&the_config.consumerd32_bin_path, |
e6142f2e | 587 | strdup(arg)); |
412d7227 | 588 | if (!the_config.consumerd32_bin_path.value) { |
e8fa9fb0 MD |
589 | PERROR("strdup"); |
590 | ret = -ENOMEM; | |
591 | } | |
db322c4d | 592 | } |
c0232ea5 | 593 | } else if (string_match(optname, "consumerd32-libdir")) { |
66b2ce8e JG |
594 | if (!arg || *arg == '\0') { |
595 | ret = -EINVAL; | |
596 | goto end; | |
597 | } | |
e8fa9fb0 MD |
598 | if (lttng_is_setuid_setgid()) { |
599 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
600 | "--consumerd32-libdir"); | |
601 | } else { | |
412d7227 | 602 | config_string_set(&the_config.consumerd32_lib_dir, |
e6142f2e | 603 | strdup(arg)); |
412d7227 | 604 | if (!the_config.consumerd32_lib_dir.value) { |
e8fa9fb0 MD |
605 | PERROR("strdup"); |
606 | ret = -ENOMEM; | |
607 | } | |
db322c4d | 608 | } |
c0232ea5 | 609 | } else if (string_match(optname, "consumerd64-path")) { |
66b2ce8e JG |
610 | if (!arg || *arg == '\0') { |
611 | ret = -EINVAL; | |
612 | goto end; | |
613 | } | |
e8fa9fb0 MD |
614 | if (lttng_is_setuid_setgid()) { |
615 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
616 | "--consumerd64-path"); | |
617 | } else { | |
412d7227 | 618 | config_string_set(&the_config.consumerd64_bin_path, |
e6142f2e | 619 | strdup(arg)); |
412d7227 | 620 | if (!the_config.consumerd64_bin_path.value) { |
e8fa9fb0 MD |
621 | PERROR("strdup"); |
622 | ret = -ENOMEM; | |
623 | } | |
db322c4d | 624 | } |
c0232ea5 | 625 | } else if (string_match(optname, "consumerd64-libdir")) { |
66b2ce8e JG |
626 | if (!arg || *arg == '\0') { |
627 | ret = -EINVAL; | |
628 | goto end; | |
629 | } | |
e8fa9fb0 MD |
630 | if (lttng_is_setuid_setgid()) { |
631 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
632 | "--consumerd64-libdir"); | |
633 | } else { | |
412d7227 | 634 | config_string_set(&the_config.consumerd64_lib_dir, |
e6142f2e | 635 | strdup(arg)); |
412d7227 | 636 | if (!the_config.consumerd64_lib_dir.value) { |
e8fa9fb0 MD |
637 | PERROR("strdup"); |
638 | ret = -ENOMEM; | |
639 | } | |
db322c4d | 640 | } |
c0232ea5 | 641 | } else if (string_match(optname, "pidfile") || opt == 'p') { |
66b2ce8e JG |
642 | if (!arg || *arg == '\0') { |
643 | ret = -EINVAL; | |
644 | goto end; | |
645 | } | |
e8fa9fb0 MD |
646 | if (lttng_is_setuid_setgid()) { |
647 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
648 | "-p, --pidfile"); | |
649 | } else { | |
412d7227 SM |
650 | config_string_set( |
651 | &the_config.pid_file_path, strdup(arg)); | |
652 | if (!the_config.pid_file_path.value) { | |
e8fa9fb0 MD |
653 | PERROR("strdup"); |
654 | ret = -ENOMEM; | |
655 | } | |
db322c4d | 656 | } |
c0232ea5 | 657 | } else if (string_match(optname, "agent-tcp-port")) { |
66b2ce8e JG |
658 | if (!arg || *arg == '\0') { |
659 | ret = -EINVAL; | |
660 | goto end; | |
661 | } | |
e8fa9fb0 MD |
662 | if (lttng_is_setuid_setgid()) { |
663 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
664 | "--agent-tcp-port"); | |
665 | } else { | |
666 | unsigned long v; | |
4d076222 | 667 | |
e8fa9fb0 MD |
668 | errno = 0; |
669 | v = strtoul(arg, NULL, 0); | |
670 | if (errno != 0 || !isdigit(arg[0])) { | |
671 | ERR("Wrong value in --agent-tcp-port parameter: %s", arg); | |
672 | return -1; | |
673 | } | |
674 | if (v == 0 || v >= 65535) { | |
675 | ERR("Port overflow in --agent-tcp-port parameter: %s", arg); | |
676 | return -1; | |
677 | } | |
412d7227 SM |
678 | the_config.agent_tcp_port.begin = |
679 | the_config.agent_tcp_port.end = (int) v; | |
2288467f | 680 | DBG3("Agent TCP port set to non default: %i", (int) v); |
26296c48 | 681 | } |
c0232ea5 | 682 | } else if (string_match(optname, "load") || opt == 'l') { |
66b2ce8e JG |
683 | if (!arg || *arg == '\0') { |
684 | ret = -EINVAL; | |
685 | goto end; | |
686 | } | |
e8fa9fb0 MD |
687 | if (lttng_is_setuid_setgid()) { |
688 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
689 | "-l, --load"); | |
690 | } else { | |
412d7227 SM |
691 | config_string_set(&the_config.load_session_path, |
692 | strdup(arg)); | |
693 | if (!the_config.load_session_path.value) { | |
e8fa9fb0 MD |
694 | PERROR("strdup"); |
695 | ret = -ENOMEM; | |
696 | } | |
ef367a93 | 697 | } |
c0232ea5 | 698 | } else if (string_match(optname, "kmod-probes")) { |
66b2ce8e JG |
699 | if (!arg || *arg == '\0') { |
700 | ret = -EINVAL; | |
701 | goto end; | |
702 | } | |
e8fa9fb0 MD |
703 | if (lttng_is_setuid_setgid()) { |
704 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
705 | "--kmod-probes"); | |
706 | } else { | |
412d7227 SM |
707 | config_string_set(&the_config.kmod_probes_list, |
708 | strdup(arg)); | |
709 | if (!the_config.kmod_probes_list.value) { | |
e8fa9fb0 MD |
710 | PERROR("strdup"); |
711 | ret = -ENOMEM; | |
712 | } | |
c9d42407 | 713 | } |
c0232ea5 | 714 | } else if (string_match(optname, "extra-kmod-probes")) { |
66b2ce8e JG |
715 | if (!arg || *arg == '\0') { |
716 | ret = -EINVAL; | |
717 | goto end; | |
718 | } | |
e8fa9fb0 MD |
719 | if (lttng_is_setuid_setgid()) { |
720 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
721 | "--extra-kmod-probes"); | |
722 | } else { | |
412d7227 | 723 | config_string_set(&the_config.kmod_extra_probes_list, |
e6142f2e | 724 | strdup(arg)); |
412d7227 | 725 | if (!the_config.kmod_extra_probes_list.value) { |
e8fa9fb0 MD |
726 | PERROR("strdup"); |
727 | ret = -ENOMEM; | |
728 | } | |
db322c4d | 729 | } |
761ffce2 | 730 | } else if (string_match(optname, EVENT_NOTIFIER_ERROR_BUFFER_SIZE_KERNEL_OPTION_STR)) { |
90aa04a1 FD |
731 | unsigned long v; |
732 | ||
733 | errno = 0; | |
734 | v = strtoul(arg, NULL, 0); | |
735 | if (errno != 0 || !isdigit(arg[0])) { | |
761ffce2 FD |
736 | ERR("Wrong value in --%s parameter: %s", |
737 | EVENT_NOTIFIER_ERROR_BUFFER_SIZE_KERNEL_OPTION_STR, arg); | |
90aa04a1 FD |
738 | return -1; |
739 | } | |
740 | if (v == 0 || v >= EVENT_NOTIFIER_ERROR_COUNTER_NUMBER_OF_BUCKET_MAX) { | |
761ffce2 FD |
741 | ERR("Value out of range for --%s parameter: %s", |
742 | EVENT_NOTIFIER_ERROR_BUFFER_SIZE_KERNEL_OPTION_STR, arg); | |
90aa04a1 FD |
743 | return -1; |
744 | } | |
761ffce2 FD |
745 | the_config.event_notifier_buffer_size_kernel = (int) v; |
746 | DBG3("Number of event notifier error buffer kernel size to non default: %i", | |
747 | the_config.event_notifier_buffer_size_kernel); | |
748 | goto end; | |
749 | } else if (string_match(optname, EVENT_NOTIFIER_ERROR_BUFFER_SIZE_USERSPACE_OPTION_STR)) { | |
750 | unsigned long v; | |
751 | ||
752 | errno = 0; | |
753 | v = strtoul(arg, NULL, 0); | |
754 | if (errno != 0 || !isdigit(arg[0])) { | |
755 | ERR("Wrong value in --%s parameter: %s", | |
756 | EVENT_NOTIFIER_ERROR_BUFFER_SIZE_USERSPACE_OPTION_STR, arg); | |
757 | return -1; | |
758 | } | |
759 | if (v == 0 || v >= EVENT_NOTIFIER_ERROR_COUNTER_NUMBER_OF_BUCKET_MAX) { | |
760 | ERR("Value out of range for --%s parameter: %s", | |
761 | EVENT_NOTIFIER_ERROR_BUFFER_SIZE_USERSPACE_OPTION_STR, arg); | |
762 | return -1; | |
763 | } | |
764 | the_config.event_notifier_buffer_size_userspace = (int) v; | |
765 | DBG3("Number of event notifier error buffer userspace size to non default: %i", | |
766 | the_config.event_notifier_buffer_size_userspace); | |
90aa04a1 | 767 | goto end; |
c0232ea5 JG |
768 | } else if (string_match(optname, "config") || opt == 'f') { |
769 | /* This is handled in set_options() thus silent skip. */ | |
770 | goto end; | |
771 | } else { | |
26296c48 JG |
772 | /* Unknown option or other error. |
773 | * Error is printed by getopt, just return */ | |
774 | ret = -1; | |
775 | } | |
776 | ||
f3dd7ce7 | 777 | end: |
c5d350b2 JG |
778 | if (ret == -EINVAL) { |
779 | const char *opt_name = "unknown"; | |
780 | int i; | |
781 | ||
782 | for (i = 0; i < sizeof(long_options) / sizeof(struct option); | |
783 | i++) { | |
784 | if (opt == long_options[i].val) { | |
785 | opt_name = long_options[i].name; | |
786 | break; | |
787 | } | |
788 | } | |
789 | ||
790 | WARN("Invalid argument provided for option \"%s\", using default value.", | |
791 | opt_name); | |
792 | } | |
f3dd7ce7 | 793 | |
26296c48 JG |
794 | return ret; |
795 | } | |
796 | ||
797 | /* | |
798 | * config_entry_handler_cb used to handle options read from a config file. | |
f40ef1d5 | 799 | * See config_entry_handler_cb comment in common/config/session-config.h for the |
26296c48 JG |
800 | * return value conventions. |
801 | */ | |
f46376a1 MJ |
802 | static int config_entry_handler(const struct config_entry *entry, |
803 | void *unused __attribute__((unused))) | |
26296c48 JG |
804 | { |
805 | int ret = 0, i; | |
806 | ||
807 | if (!entry || !entry->name || !entry->value) { | |
808 | ret = -EINVAL; | |
809 | goto end; | |
810 | } | |
811 | ||
812 | /* Check if the option is to be ignored */ | |
813 | for (i = 0; i < sizeof(config_ignore_options) / sizeof(char *); i++) { | |
814 | if (!strcmp(entry->name, config_ignore_options[i])) { | |
815 | goto end; | |
816 | } | |
817 | } | |
818 | ||
819 | for (i = 0; i < (sizeof(long_options) / sizeof(struct option)) - 1; | |
820 | i++) { | |
821 | ||
822 | /* Ignore if not fully matched. */ | |
823 | if (strcmp(entry->name, long_options[i].name)) { | |
824 | continue; | |
825 | } | |
826 | ||
827 | /* | |
828 | * If the option takes no argument on the command line, we have to | |
829 | * check if the value is "true". We support non-zero numeric values, | |
830 | * true, on and yes. | |
831 | */ | |
832 | if (!long_options[i].has_arg) { | |
833 | ret = config_parse_value(entry->value); | |
834 | if (ret <= 0) { | |
835 | if (ret) { | |
836 | WARN("Invalid configuration value \"%s\" for option %s", | |
837 | entry->value, entry->name); | |
838 | } | |
839 | /* False, skip boolean config option. */ | |
840 | goto end; | |
4d076222 | 841 | } |
26296c48 JG |
842 | } |
843 | ||
844 | ret = set_option(long_options[i].val, entry->value, entry->name); | |
845 | goto end; | |
846 | } | |
847 | ||
848 | WARN("Unrecognized option \"%s\" in daemon configuration file.", entry->name); | |
849 | ||
850 | end: | |
851 | return ret; | |
852 | } | |
853 | ||
a3bc3918 JR |
854 | static void print_version(void) { |
855 | fprintf(stdout, "%s\n", VERSION); | |
856 | } | |
857 | ||
26296c48 JG |
858 | /* |
859 | * daemon configuration loading and argument parsing | |
860 | */ | |
861 | static int set_options(int argc, char **argv) | |
862 | { | |
863 | int ret = 0, c = 0, option_index = 0; | |
864 | int orig_optopt = optopt, orig_optind = optind; | |
865 | char *optstring; | |
fcf27bee | 866 | char *config_path = NULL; |
26296c48 JG |
867 | |
868 | optstring = utils_generate_optstring(long_options, | |
869 | sizeof(long_options) / sizeof(struct option)); | |
870 | if (!optstring) { | |
871 | ret = -ENOMEM; | |
872 | goto end; | |
873 | } | |
874 | ||
875 | /* Check for the --config option */ | |
876 | while ((c = getopt_long(argc, argv, optstring, long_options, | |
877 | &option_index)) != -1) { | |
878 | if (c == '?') { | |
879 | ret = -EINVAL; | |
880 | goto end; | |
881 | } else if (c != 'f') { | |
882 | /* if not equal to --config option. */ | |
883 | continue; | |
884 | } | |
885 | ||
e8fa9fb0 MD |
886 | if (lttng_is_setuid_setgid()) { |
887 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
888 | "-f, --config"); | |
889 | } else { | |
fcf27bee | 890 | free(config_path); |
e8fa9fb0 MD |
891 | config_path = utils_expand_path(optarg); |
892 | if (!config_path) { | |
893 | ERR("Failed to resolve path: %s", optarg); | |
894 | } | |
26296c48 JG |
895 | } |
896 | } | |
897 | ||
898 | ret = config_get_section_entries(config_path, config_section_name, | |
899 | config_entry_handler, NULL); | |
900 | if (ret) { | |
901 | if (ret > 0) { | |
902 | ERR("Invalid configuration option at line %i", ret); | |
903 | ret = -1; | |
904 | } | |
905 | goto end; | |
906 | } | |
907 | ||
908 | /* Reset getopt's global state */ | |
909 | optopt = orig_optopt; | |
910 | optind = orig_optind; | |
911 | while (1) { | |
c0232ea5 JG |
912 | option_index = -1; |
913 | /* | |
914 | * getopt_long() will not set option_index if it encounters a | |
915 | * short option. | |
916 | */ | |
917 | c = getopt_long(argc, argv, optstring, long_options, | |
918 | &option_index); | |
26296c48 | 919 | if (c == -1) { |
4d076222 DG |
920 | break; |
921 | } | |
26296c48 | 922 | |
c0232ea5 JG |
923 | /* |
924 | * Pass NULL as the long option name if popt left the index | |
925 | * unset. | |
926 | */ | |
927 | ret = set_option(c, optarg, | |
928 | option_index < 0 ? NULL : | |
929 | long_options[option_index].name); | |
26296c48 JG |
930 | if (ret < 0) { |
931 | break; | |
fac6795d DG |
932 | } |
933 | } | |
934 | ||
26296c48 | 935 | end: |
fcf27bee | 936 | free(config_path); |
26296c48 JG |
937 | free(optstring); |
938 | return ret; | |
fac6795d DG |
939 | } |
940 | ||
f472090a JG |
941 | /* |
942 | * Create lockfile using the rundir and return its fd. | |
943 | */ | |
944 | static int create_lockfile(void) | |
945 | { | |
412d7227 | 946 | return utils_create_lock_file(the_config.lock_file_path.value); |
f472090a JG |
947 | } |
948 | ||
fac6795d | 949 | /* |
54d01ffb DG |
950 | * Check if the global socket is available, and if a daemon is answering at the |
951 | * other side. If yes, error is returned. | |
f472090a JG |
952 | * |
953 | * Also attempts to create and hold the lock file. | |
fac6795d | 954 | */ |
cf3af59e | 955 | static int check_existing_daemon(void) |
fac6795d | 956 | { |
f472090a JG |
957 | int ret = 0; |
958 | ||
7d8234d9 | 959 | /* Is there anybody out there ? */ |
099e26bd | 960 | if (lttng_session_daemon_alive()) { |
f472090a JG |
961 | ret = -EEXIST; |
962 | goto end; | |
099e26bd | 963 | } |
b09c7c76 | 964 | |
f472090a JG |
965 | lockfile_fd = create_lockfile(); |
966 | if (lockfile_fd < 0) { | |
967 | ret = -EEXIST; | |
968 | goto end; | |
969 | } | |
970 | end: | |
971 | return ret; | |
972 | } | |
973 | ||
974 | static void sessiond_cleanup_lock_file(void) | |
975 | { | |
976 | int ret; | |
977 | ||
978 | /* | |
979 | * Cleanup lock file by deleting it and finaly closing it which will | |
980 | * release the file system lock. | |
981 | */ | |
982 | if (lockfile_fd >= 0) { | |
412d7227 | 983 | ret = remove(the_config.lock_file_path.value); |
f472090a JG |
984 | if (ret < 0) { |
985 | PERROR("remove lock file"); | |
986 | } | |
987 | ret = close(lockfile_fd); | |
988 | if (ret < 0) { | |
989 | PERROR("close lock file"); | |
990 | } | |
991 | } | |
fac6795d DG |
992 | } |
993 | ||
fac6795d | 994 | /* |
d063d709 | 995 | * Set the tracing group gid onto the client socket. |
5e16da05 | 996 | * |
d063d709 | 997 | * Race window between mkdir and chown is OK because we are going from more |
d1613cf5 | 998 | * permissive (root.root) to less permissive (root.tracing). |
fac6795d | 999 | */ |
be040666 | 1000 | static int set_permissions(char *rundir) |
fac6795d DG |
1001 | { |
1002 | int ret; | |
996b65c8 | 1003 | gid_t gid; |
fac6795d | 1004 | |
412d7227 SM |
1005 | ret = utils_get_group_id( |
1006 | the_config.tracing_group_name.value, true, &gid); | |
28ab59d0 JR |
1007 | if (ret) { |
1008 | /* Default to root group. */ | |
1009 | gid = 0; | |
1010 | } | |
fac6795d | 1011 | |
d6f42150 | 1012 | /* Set lttng run dir */ |
be040666 | 1013 | ret = chown(rundir, 0, gid); |
d6f42150 | 1014 | if (ret < 0) { |
be040666 | 1015 | ERR("Unable to set group on %s", rundir); |
76d7553f | 1016 | PERROR("chown"); |
d6f42150 DG |
1017 | } |
1018 | ||
6c71277b MD |
1019 | /* |
1020 | * Ensure all applications and tracing group can search the run | |
1021 | * dir. Allow everyone to read the directory, since it does not | |
1022 | * buy us anything to hide its content. | |
1023 | */ | |
1024 | ret = chmod(rundir, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); | |
d1613cf5 JN |
1025 | if (ret < 0) { |
1026 | ERR("Unable to set permissions on %s", rundir); | |
76d7553f | 1027 | PERROR("chmod"); |
d1613cf5 JN |
1028 | } |
1029 | ||
d6f42150 | 1030 | /* lttng client socket path */ |
412d7227 | 1031 | ret = chown(the_config.client_unix_sock_path.value, 0, gid); |
fac6795d | 1032 | if (ret < 0) { |
412d7227 SM |
1033 | ERR("Unable to set group on %s", |
1034 | the_config.client_unix_sock_path.value); | |
76d7553f | 1035 | PERROR("chown"); |
d6f42150 DG |
1036 | } |
1037 | ||
3bd1e081 | 1038 | /* kconsumer error socket path */ |
412d7227 | 1039 | ret = chown(the_kconsumer_data.err_unix_sock_path, 0, 0); |
d6f42150 | 1040 | if (ret < 0) { |
412d7227 SM |
1041 | ERR("Unable to set group on %s", |
1042 | the_kconsumer_data.err_unix_sock_path); | |
76d7553f | 1043 | PERROR("chown"); |
3bd1e081 MD |
1044 | } |
1045 | ||
7753dea8 | 1046 | /* 64-bit ustconsumer error socket path */ |
412d7227 | 1047 | ret = chown(the_ustconsumer64_data.err_unix_sock_path, 0, 0); |
7753dea8 | 1048 | if (ret < 0) { |
412d7227 SM |
1049 | ERR("Unable to set group on %s", |
1050 | the_ustconsumer64_data.err_unix_sock_path); | |
76d7553f | 1051 | PERROR("chown"); |
7753dea8 MD |
1052 | } |
1053 | ||
1054 | /* 32-bit ustconsumer compat32 error socket path */ | |
412d7227 | 1055 | ret = chown(the_ustconsumer32_data.err_unix_sock_path, 0, 0); |
3bd1e081 | 1056 | if (ret < 0) { |
412d7227 SM |
1057 | ERR("Unable to set group on %s", |
1058 | the_ustconsumer32_data.err_unix_sock_path); | |
76d7553f | 1059 | PERROR("chown"); |
fac6795d DG |
1060 | } |
1061 | ||
d6f42150 | 1062 | DBG("All permissions are set"); |
e07ae692 | 1063 | |
fac6795d DG |
1064 | return ret; |
1065 | } | |
1066 | ||
d6f42150 | 1067 | /* |
d063d709 | 1068 | * Create the lttng run directory needed for all global sockets and pipe. |
d6f42150 | 1069 | */ |
e6142f2e | 1070 | static int create_lttng_rundir(void) |
d6f42150 DG |
1071 | { |
1072 | int ret; | |
1073 | ||
412d7227 | 1074 | DBG3("Creating LTTng run directory: %s", the_config.rundir.value); |
67e40797 | 1075 | |
412d7227 | 1076 | ret = mkdir(the_config.rundir.value, S_IRWXU); |
d6f42150 | 1077 | if (ret < 0) { |
b1f11e69 | 1078 | if (errno != EEXIST) { |
412d7227 | 1079 | ERR("Unable to create %s", the_config.rundir.value); |
b1f11e69 DG |
1080 | goto error; |
1081 | } else { | |
1082 | ret = 0; | |
1083 | } | |
d6f42150 DG |
1084 | } |
1085 | ||
1086 | error: | |
1087 | return ret; | |
1088 | } | |
1089 | ||
1090 | /* | |
e6142f2e | 1091 | * Setup sockets and directory needed by the consumerds' communication with the |
d063d709 | 1092 | * session daemon. |
d6f42150 | 1093 | */ |
e6142f2e | 1094 | static int set_consumer_sockets(struct consumer_data *consumer_data) |
d6f42150 DG |
1095 | { |
1096 | int ret; | |
e6142f2e | 1097 | char *path = NULL; |
d6f42150 | 1098 | |
6c71277b | 1099 | switch (consumer_data->type) { |
7753dea8 | 1100 | case LTTNG_CONSUMER_KERNEL: |
412d7227 | 1101 | path = the_config.kconsumerd_path.value; |
7753dea8 MD |
1102 | break; |
1103 | case LTTNG_CONSUMER64_UST: | |
412d7227 | 1104 | path = the_config.consumerd64_path.value; |
7753dea8 MD |
1105 | break; |
1106 | case LTTNG_CONSUMER32_UST: | |
412d7227 | 1107 | path = the_config.consumerd32_path.value; |
7753dea8 MD |
1108 | break; |
1109 | default: | |
1110 | ERR("Consumer type unknown"); | |
1111 | ret = -EINVAL; | |
1112 | goto error; | |
d6f42150 | 1113 | } |
a0377dfe | 1114 | LTTNG_ASSERT(path); |
d6f42150 | 1115 | |
67e40797 DG |
1116 | DBG2("Creating consumer directory: %s", path); |
1117 | ||
6c71277b | 1118 | ret = mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP); |
e7e1ef17 JR |
1119 | if (ret < 0 && errno != EEXIST) { |
1120 | PERROR("mkdir"); | |
1121 | ERR("Failed to create %s", path); | |
1122 | goto error; | |
d6f42150 | 1123 | } |
6c71277b | 1124 | if (is_root) { |
28ab59d0 JR |
1125 | gid_t gid; |
1126 | ||
412d7227 SM |
1127 | ret = utils_get_group_id(the_config.tracing_group_name.value, |
1128 | true, &gid); | |
28ab59d0 JR |
1129 | if (ret) { |
1130 | /* Default to root group. */ | |
1131 | gid = 0; | |
1132 | } | |
1133 | ||
1134 | ret = chown(path, 0, gid); | |
6c71277b MD |
1135 | if (ret < 0) { |
1136 | ERR("Unable to set group on %s", path); | |
1137 | PERROR("chown"); | |
1138 | goto error; | |
1139 | } | |
1140 | } | |
d6f42150 | 1141 | |
e6142f2e | 1142 | /* Create the consumerd error unix socket */ |
3bd1e081 MD |
1143 | consumer_data->err_sock = |
1144 | lttcomm_create_unix_sock(consumer_data->err_unix_sock_path); | |
1145 | if (consumer_data->err_sock < 0) { | |
1146 | ERR("Create unix sock failed: %s", consumer_data->err_unix_sock_path); | |
d6f42150 DG |
1147 | ret = -1; |
1148 | goto error; | |
1149 | } | |
1150 | ||
a24f05ab MD |
1151 | /* |
1152 | * Set the CLOEXEC flag. Return code is useless because either way, the | |
1153 | * show must go on. | |
1154 | */ | |
1155 | ret = utils_set_fd_cloexec(consumer_data->err_sock); | |
1156 | if (ret < 0) { | |
1157 | PERROR("utils_set_fd_cloexec"); | |
1158 | /* continue anyway */ | |
1159 | } | |
1160 | ||
d6f42150 | 1161 | /* File permission MUST be 660 */ |
3bd1e081 | 1162 | ret = chmod(consumer_data->err_unix_sock_path, |
54d01ffb | 1163 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); |
d6f42150 | 1164 | if (ret < 0) { |
3bd1e081 | 1165 | ERR("Set file permissions failed: %s", consumer_data->err_unix_sock_path); |
67e40797 | 1166 | PERROR("chmod"); |
d6f42150 DG |
1167 | goto error; |
1168 | } | |
1169 | ||
1170 | error: | |
1171 | return ret; | |
1172 | } | |
1173 | ||
fac6795d | 1174 | /* |
d063d709 | 1175 | * Signal handler for the daemon |
cf3af59e | 1176 | * |
54d01ffb DG |
1177 | * Simply stop all worker threads, leaving main() return gracefully after |
1178 | * joining all threads and calling cleanup(). | |
fac6795d | 1179 | */ |
f46376a1 MJ |
1180 | static void sighandler(int sig, siginfo_t *siginfo, |
1181 | void *arg __attribute__((unused))) | |
fac6795d DG |
1182 | { |
1183 | switch (sig) { | |
cf3af59e | 1184 | case SIGINT: |
af87c45a | 1185 | DBG("SIGINT caught"); |
f90a04ed | 1186 | notify_main_quit_pipe(); |
cf3af59e MD |
1187 | break; |
1188 | case SIGTERM: | |
af87c45a | 1189 | DBG("SIGTERM caught"); |
f90a04ed | 1190 | notify_main_quit_pipe(); |
cf3af59e | 1191 | break; |
0bb7724a DG |
1192 | case SIGUSR1: |
1193 | CMM_STORE_SHARED(recv_child_signal, 1); | |
1194 | break; | |
881fc67f MD |
1195 | case SIGBUS: |
1196 | { | |
1197 | int write_ret; | |
1198 | const char msg[] = "Received SIGBUS, aborting program.\n"; | |
1199 | ||
1200 | lttng_ust_handle_sigbus(siginfo->si_addr); | |
1201 | /* | |
1202 | * If ustctl did not catch this signal (triggering a | |
1203 | * siglongjmp), abort the program. Otherwise, the execution | |
1204 | * will resume from the ust-ctl call which caused this error. | |
1205 | * | |
1206 | * The return value is ignored since the program aborts anyhow. | |
1207 | */ | |
1208 | write_ret = write(STDERR_FILENO, msg, sizeof(msg)); | |
1209 | (void) write_ret; | |
1210 | abort(); | |
1211 | } | |
cf3af59e MD |
1212 | default: |
1213 | break; | |
fac6795d | 1214 | } |
fac6795d DG |
1215 | } |
1216 | ||
1217 | /* | |
d063d709 | 1218 | * Setup signal handler for : |
1d4b027a | 1219 | * SIGINT, SIGTERM, SIGPIPE |
fac6795d | 1220 | */ |
1d4b027a | 1221 | static int set_signal_handler(void) |
fac6795d | 1222 | { |
1d4b027a DG |
1223 | int ret = 0; |
1224 | struct sigaction sa; | |
1225 | sigset_t sigset; | |
fac6795d | 1226 | |
1d4b027a | 1227 | if ((ret = sigemptyset(&sigset)) < 0) { |
76d7553f | 1228 | PERROR("sigemptyset"); |
1d4b027a DG |
1229 | return ret; |
1230 | } | |
d6f42150 | 1231 | |
1d4b027a | 1232 | sa.sa_mask = sigset; |
881fc67f | 1233 | sa.sa_flags = SA_SIGINFO; |
0072e5e2 | 1234 | |
881fc67f | 1235 | sa.sa_sigaction = sighandler; |
1d4b027a | 1236 | if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) { |
76d7553f | 1237 | PERROR("sigaction"); |
1d4b027a | 1238 | return ret; |
d6f42150 DG |
1239 | } |
1240 | ||
1d4b027a | 1241 | if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) { |
76d7553f | 1242 | PERROR("sigaction"); |
1d4b027a | 1243 | return ret; |
d6f42150 | 1244 | } |
aaf26714 | 1245 | |
0072e5e2 | 1246 | if ((ret = sigaction(SIGUSR1, &sa, NULL)) < 0) { |
76d7553f | 1247 | PERROR("sigaction"); |
1d4b027a | 1248 | return ret; |
8c0faa1d DG |
1249 | } |
1250 | ||
881fc67f MD |
1251 | if ((ret = sigaction(SIGBUS, &sa, NULL)) < 0) { |
1252 | PERROR("sigaction"); | |
1253 | return ret; | |
1254 | } | |
1255 | ||
1256 | sa.sa_flags = 0; | |
0072e5e2 MD |
1257 | sa.sa_handler = SIG_IGN; |
1258 | if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) { | |
0bb7724a DG |
1259 | PERROR("sigaction"); |
1260 | return ret; | |
1261 | } | |
1262 | ||
881fc67f | 1263 | DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE, SIGINT, and SIGBUS"); |
1d4b027a DG |
1264 | |
1265 | return ret; | |
fac6795d DG |
1266 | } |
1267 | ||
f3ed775e | 1268 | /* |
d063d709 | 1269 | * Set open files limit to unlimited. This daemon can open a large number of |
514bb9f0 | 1270 | * file descriptors in order to consume multiple kernel traces. |
f3ed775e DG |
1271 | */ |
1272 | static void set_ulimit(void) | |
1273 | { | |
1274 | int ret; | |
1275 | struct rlimit lim; | |
1276 | ||
514bb9f0 | 1277 | /* The kernel does not allow an infinite limit for open files */ |
f3ed775e DG |
1278 | lim.rlim_cur = 65535; |
1279 | lim.rlim_max = 65535; | |
1280 | ||
1281 | ret = setrlimit(RLIMIT_NOFILE, &lim); | |
1282 | if (ret < 0) { | |
76d7553f | 1283 | PERROR("failed to set open files limit"); |
f3ed775e DG |
1284 | } |
1285 | } | |
1286 | ||
4a15001e | 1287 | static int write_pidfile(void) |
35f90c40 | 1288 | { |
412d7227 | 1289 | return utils_create_pid_file(getpid(), the_config.pid_file_path.value); |
35f90c40 DG |
1290 | } |
1291 | ||
e6142f2e JG |
1292 | static int set_clock_plugin_env(void) |
1293 | { | |
1294 | int ret = 0; | |
1295 | char *env_value = NULL; | |
cd9290dd | 1296 | |
412d7227 | 1297 | if (!the_config.lttng_ust_clock_plugin.value) { |
e6142f2e JG |
1298 | goto end; |
1299 | } | |
1300 | ||
3cfcd698 | 1301 | ret = asprintf(&env_value, "LTTNG_UST_CLOCK_PLUGIN=%s", |
412d7227 | 1302 | the_config.lttng_ust_clock_plugin.value); |
cd9290dd | 1303 | if (ret < 0) { |
e6142f2e JG |
1304 | PERROR("asprintf"); |
1305 | goto end; | |
cd9290dd DG |
1306 | } |
1307 | ||
e6142f2e JG |
1308 | ret = putenv(env_value); |
1309 | if (ret) { | |
1310 | free(env_value); | |
1311 | PERROR("putenv of LTTNG_UST_CLOCK_PLUGIN"); | |
1312 | goto end; | |
1313 | } | |
cd9290dd | 1314 | |
e6142f2e | 1315 | DBG("Updated LTTNG_UST_CLOCK_PLUGIN environment variable to \"%s\"", |
412d7227 | 1316 | the_config.lttng_ust_clock_plugin.value); |
e6142f2e | 1317 | end: |
4a15001e | 1318 | return ret; |
ef367a93 JG |
1319 | } |
1320 | ||
99d688f2 JG |
1321 | static void destroy_all_sessions_and_wait(void) |
1322 | { | |
1323 | struct ltt_session *session, *tmp; | |
1324 | struct ltt_session_list *session_list; | |
1325 | ||
1326 | session_list = session_get_list(); | |
1327 | DBG("Initiating destruction of all sessions"); | |
1328 | ||
1329 | if (!session_list) { | |
1330 | return; | |
1331 | } | |
1332 | ||
99d688f2 JG |
1333 | session_lock_list(); |
1334 | /* Initiate the destruction of all sessions. */ | |
1335 | cds_list_for_each_entry_safe(session, tmp, | |
1336 | &session_list->head, list) { | |
1337 | if (!session_get(session)) { | |
1338 | continue; | |
1339 | } | |
1340 | ||
1341 | session_lock(session); | |
1342 | if (session->destroyed) { | |
1343 | goto unlock_session; | |
1344 | } | |
63763958 | 1345 | (void) cmd_stop_trace(session); |
412d7227 SM |
1346 | (void) cmd_destroy_session( |
1347 | session, the_notification_thread_handle, NULL); | |
99d688f2 JG |
1348 | unlock_session: |
1349 | session_unlock(session); | |
1350 | session_put(session); | |
1351 | } | |
1352 | session_unlock_list(); | |
1353 | ||
1354 | /* Wait for the destruction of all sessions to complete. */ | |
1355 | DBG("Waiting for the destruction of all sessions to complete"); | |
1356 | session_list_wait_empty(); | |
1357 | DBG("Destruction of all sessions completed"); | |
1358 | } | |
1359 | ||
b69a1b40 JG |
1360 | static void unregister_all_triggers(void) |
1361 | { | |
1362 | enum lttng_error_code ret_code; | |
1363 | enum lttng_trigger_status trigger_status; | |
1364 | struct lttng_triggers *triggers = NULL; | |
1365 | unsigned int trigger_count, i; | |
1366 | const struct lttng_credentials creds = { | |
e534f206 | 1367 | .uid = LTTNG_OPTIONAL_INIT_VALUE(0), |
1c9a0b0e | 1368 | .gid = LTTNG_OPTIONAL_INIT_UNSET, |
b69a1b40 JG |
1369 | }; |
1370 | ||
1371 | DBG("Unregistering all triggers"); | |
1372 | ||
1373 | /* | |
1374 | * List all triggers as "root" since we wish to unregister all triggers. | |
1375 | */ | |
1376 | ret_code = notification_thread_command_list_triggers( | |
412d7227 SM |
1377 | the_notification_thread_handle, creds.uid.value, |
1378 | &triggers); | |
b69a1b40 JG |
1379 | if (ret_code != LTTNG_OK) { |
1380 | ERR("Failed to list triggers while unregistering all triggers"); | |
1381 | goto end; | |
1382 | } | |
1383 | ||
1384 | trigger_status = lttng_triggers_get_count(triggers, &trigger_count); | |
a0377dfe | 1385 | LTTNG_ASSERT(trigger_status == LTTNG_TRIGGER_STATUS_OK); |
b69a1b40 JG |
1386 | |
1387 | for (i = 0; i < trigger_count; i++) { | |
b69a1b40 JG |
1388 | uid_t trigger_owner; |
1389 | const char *trigger_name; | |
1390 | const struct lttng_trigger *trigger = | |
1391 | lttng_triggers_get_at_index(triggers, i); | |
1392 | ||
a0377dfe | 1393 | LTTNG_ASSERT(trigger); |
b69a1b40 JG |
1394 | |
1395 | trigger_status = lttng_trigger_get_owner_uid( | |
1396 | trigger, &trigger_owner); | |
a0377dfe | 1397 | LTTNG_ASSERT(trigger_status == LTTNG_TRIGGER_STATUS_OK); |
b69a1b40 JG |
1398 | |
1399 | trigger_status = lttng_trigger_get_name(trigger, &trigger_name); | |
0efb2ad7 JG |
1400 | trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ? |
1401 | trigger_name : "(anonymous)"; | |
b69a1b40 JG |
1402 | |
1403 | DBG("Unregistering trigger: trigger owner uid = %d, trigger name = '%s'", | |
1404 | (int) trigger_owner, trigger_name); | |
1405 | ||
412d7227 SM |
1406 | ret_code = cmd_unregister_trigger(&creds, trigger, |
1407 | the_notification_thread_handle); | |
b69a1b40 JG |
1408 | if (ret_code != LTTNG_OK) { |
1409 | ERR("Failed to unregister trigger: trigger owner uid = %d, trigger name = '%s', error: '%s'", | |
1410 | (int) trigger_owner, trigger_name, | |
1411 | lttng_strerror(-ret_code)); | |
1412 | /* Continue to unregister the remaining triggers. */ | |
1413 | } | |
1414 | } | |
1415 | end: | |
1416 | lttng_triggers_destroy(triggers); | |
1417 | } | |
1418 | ||
929f71ec JG |
1419 | static int run_as_worker_post_fork_cleanup(void *data) |
1420 | { | |
7966af57 | 1421 | struct sessiond_config *sessiond_config = (struct sessiond_config *) data; |
929f71ec JG |
1422 | |
1423 | sessiond_config_fini(sessiond_config); | |
1424 | return 0; | |
1425 | } | |
1426 | ||
1427 | static int launch_run_as_worker(const char *procname) | |
1428 | { | |
1429 | /* | |
1430 | * Clean-up before forking the run-as worker. Any dynamically | |
1431 | * allocated memory of which the worker is not aware will | |
1432 | * be leaked as the process forks a run-as worker (and performs | |
1433 | * no exec*()). The same would apply to any opened fd. | |
1434 | */ | |
412d7227 SM |
1435 | return run_as_create_worker( |
1436 | procname, run_as_worker_post_fork_cleanup, &the_config); | |
929f71ec JG |
1437 | } |
1438 | ||
52a0e931 JG |
1439 | static void sessiond_uuid_log(void) |
1440 | { | |
c70636a7 | 1441 | char uuid_str[LTTNG_UUID_STR_LEN]; |
52a0e931 | 1442 | |
412d7227 | 1443 | lttng_uuid_to_str(the_sessiond_uuid, uuid_str); |
52a0e931 JG |
1444 | DBG("Starting lttng-sessiond {%s}", uuid_str); |
1445 | } | |
1446 | ||
fac6795d DG |
1447 | /* |
1448 | * main | |
1449 | */ | |
1450 | int main(int argc, char **argv) | |
1451 | { | |
4a15001e | 1452 | int ret = 0, retval = 0; |
e6142f2e | 1453 | const char *env_app_timeout; |
b3530820 JG |
1454 | struct lttng_pipe *ust32_channel_monitor_pipe = NULL, |
1455 | *ust64_channel_monitor_pipe = NULL, | |
1456 | *kernel_channel_monitor_pipe = NULL; | |
bc26e826 | 1457 | struct timer_thread_parameters timer_thread_parameters; |
64d9b072 JG |
1458 | /* Rotation thread handle. */ |
1459 | struct rotation_thread_handle *rotation_thread_handle = NULL; | |
d086f507 JD |
1460 | /* Queue of rotation jobs populated by the sessiond-timer. */ |
1461 | struct rotation_thread_timer_queue *rotation_timer_queue = NULL; | |
917a718d | 1462 | struct lttng_thread *client_thread = NULL; |
4a91420c | 1463 | struct lttng_thread *notification_thread = NULL; |
bd9addf7 | 1464 | struct lttng_thread *register_apps_thread = NULL; |
761ffce2 | 1465 | enum event_notifier_error_accounting_status event_notifier_error_accounting_status; |
fac6795d | 1466 | |
f5fb86c1 | 1467 | logger_set_thread_name("Main", false); |
335a95b7 MD |
1468 | init_kernel_workarounds(); |
1469 | ||
f6a9efaa DG |
1470 | rcu_register_thread(); |
1471 | ||
4a15001e MD |
1472 | if (set_signal_handler()) { |
1473 | retval = -1; | |
1474 | goto exit_set_signal_handler; | |
0bb7724a DG |
1475 | } |
1476 | ||
92816cc3 | 1477 | if (timer_signal_init()) { |
d086f507 JD |
1478 | retval = -1; |
1479 | goto exit_set_signal_handler; | |
1480 | } | |
1481 | ||
81663f07 | 1482 | the_page_size = sysconf(_SC_PAGE_SIZE); |
412d7227 | 1483 | if (the_page_size < 0) { |
81663f07 | 1484 | PERROR("sysconf _SC_PAGE_SIZE"); |
412d7227 SM |
1485 | the_page_size = LONG_MAX; |
1486 | WARN("Fallback page size to %ld", the_page_size); | |
12744796 DG |
1487 | } |
1488 | ||
412d7227 | 1489 | ret = sessiond_config_init(&the_config); |
e6142f2e JG |
1490 | if (ret) { |
1491 | retval = -1; | |
1492 | goto exit_set_signal_handler; | |
1493 | } | |
1494 | ||
2788b494 JR |
1495 | /* |
1496 | * Init config from environment variables. | |
1497 | * Command line option override env configuration per-doc. Do env first. | |
1498 | */ | |
412d7227 | 1499 | sessiond_config_apply_env_config(&the_config); |
2788b494 | 1500 | |
4a15001e MD |
1501 | /* |
1502 | * Parse arguments and load the daemon configuration file. | |
1503 | * | |
1504 | * We have an exit_options exit path to free memory reserved by | |
3c339053 | 1505 | * set_options. |
4a15001e | 1506 | */ |
fac6795d | 1507 | progname = argv[0]; |
4a15001e MD |
1508 | if (set_options(argc, argv)) { |
1509 | retval = -1; | |
1510 | goto exit_options; | |
fac6795d DG |
1511 | } |
1512 | ||
e6142f2e JG |
1513 | /* |
1514 | * Resolve all paths received as arguments, configuration option, or | |
1515 | * through environment variable as absolute paths. This is necessary | |
1516 | * since daemonizing causes the sessiond's current working directory | |
1517 | * to '/'. | |
1518 | */ | |
412d7227 | 1519 | ret = sessiond_config_resolve_paths(&the_config); |
e6142f2e JG |
1520 | if (ret) { |
1521 | goto exit_options; | |
1522 | } | |
1523 | ||
1524 | /* Apply config. */ | |
412d7227 SM |
1525 | lttng_opt_verbose = the_config.verbose; |
1526 | lttng_opt_quiet = the_config.quiet; | |
1527 | the_kconsumer_data.err_unix_sock_path = | |
1528 | the_config.kconsumerd_err_unix_sock_path.value; | |
1529 | the_kconsumer_data.cmd_unix_sock_path = | |
1530 | the_config.kconsumerd_cmd_unix_sock_path.value; | |
1531 | the_ustconsumer32_data.err_unix_sock_path = | |
1532 | the_config.consumerd32_err_unix_sock_path.value; | |
1533 | the_ustconsumer32_data.cmd_unix_sock_path = | |
1534 | the_config.consumerd32_cmd_unix_sock_path.value; | |
1535 | the_ustconsumer64_data.err_unix_sock_path = | |
1536 | the_config.consumerd64_err_unix_sock_path.value; | |
1537 | the_ustconsumer64_data.cmd_unix_sock_path = | |
1538 | the_config.consumerd64_cmd_unix_sock_path.value; | |
e6142f2e JG |
1539 | set_clock_plugin_env(); |
1540 | ||
412d7227 | 1541 | sessiond_config_log(&the_config); |
52a0e931 | 1542 | sessiond_uuid_log(); |
e35e95ea | 1543 | lttng::logging::log_system_information(PRINT_DBG); |
e6142f2e | 1544 | |
a3bc3918 JR |
1545 | if (opt_print_version) { |
1546 | print_version(); | |
1547 | retval = 0; | |
1548 | goto exit_options; | |
1549 | } | |
1550 | ||
f472090a JG |
1551 | if (create_lttng_rundir()) { |
1552 | retval = -1; | |
1553 | goto exit_options; | |
1554 | } | |
1555 | ||
1556 | /* Abort launch if a session daemon is already running. */ | |
1557 | if (check_existing_daemon()) { | |
1558 | ERR("A session daemon is already running."); | |
1559 | retval = -1; | |
1560 | goto exit_options; | |
1561 | } | |
1562 | ||
fac6795d | 1563 | /* Daemonize */ |
412d7227 | 1564 | if (the_config.daemonize || the_config.background) { |
ceed52b5 MD |
1565 | int i; |
1566 | ||
412d7227 SM |
1567 | ret = lttng_daemonize(&the_child_ppid, &recv_child_signal, |
1568 | !the_config.background); | |
53094c05 | 1569 | if (ret < 0) { |
4a15001e MD |
1570 | retval = -1; |
1571 | goto exit_options; | |
53094c05 | 1572 | } |
0bb7724a | 1573 | |
ceed52b5 | 1574 | /* |
0bb7724a | 1575 | * We are in the child. Make sure all other file descriptors are |
4a15001e | 1576 | * closed, in case we are called with more opened file |
f472090a | 1577 | * descriptors than the standard ones and the lock file. |
ceed52b5 MD |
1578 | */ |
1579 | for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) { | |
f472090a JG |
1580 | if (i == lockfile_fd) { |
1581 | continue; | |
1582 | } | |
ceed52b5 MD |
1583 | (void) close(i); |
1584 | } | |
1585 | } | |
1586 | ||
929f71ec | 1587 | if (launch_run_as_worker(argv[0]) < 0) { |
7567352f MD |
1588 | goto exit_create_run_as_worker_cleanup; |
1589 | } | |
1590 | ||
4a15001e MD |
1591 | /* |
1592 | * Starting from here, we can create threads. This needs to be after | |
1593 | * lttng_daemonize due to RCU. | |
1594 | */ | |
1595 | ||
1596 | /* | |
1597 | * Initialize the health check subsystem. This call should set the | |
1598 | * appropriate time values. | |
1599 | */ | |
412d7227 SM |
1600 | the_health_sessiond = health_app_create(NR_HEALTH_SESSIOND_TYPES); |
1601 | if (!the_health_sessiond) { | |
4a15001e MD |
1602 | PERROR("health_app_create error"); |
1603 | retval = -1; | |
0d64e8fb | 1604 | goto stop_threads; |
4a15001e MD |
1605 | } |
1606 | ||
f90a04ed MJ |
1607 | /* Create main quit pipe */ |
1608 | if (sessiond_init_main_quit_pipe()) { | |
4a15001e | 1609 | retval = -1; |
0d64e8fb | 1610 | goto stop_threads; |
fac6795d DG |
1611 | } |
1612 | ||
1613 | /* Check if daemon is UID = 0 */ | |
1614 | is_root = !getuid(); | |
fac6795d | 1615 | if (is_root) { |
67e40797 | 1616 | /* Create global run dir with root access */ |
67e40797 | 1617 | |
b3530820 JG |
1618 | kernel_channel_monitor_pipe = lttng_pipe_open(0); |
1619 | if (!kernel_channel_monitor_pipe) { | |
1620 | ERR("Failed to create kernel consumer channel monitor pipe"); | |
1621 | retval = -1; | |
0d64e8fb | 1622 | goto stop_threads; |
b3530820 | 1623 | } |
412d7227 | 1624 | the_kconsumer_data.channel_monitor_pipe = |
b3530820 | 1625 | lttng_pipe_release_writefd( |
412d7227 SM |
1626 | kernel_channel_monitor_pipe); |
1627 | if (the_kconsumer_data.channel_monitor_pipe < 0) { | |
b3530820 | 1628 | retval = -1; |
0d64e8fb | 1629 | goto stop_threads; |
b3530820 | 1630 | } |
fac6795d DG |
1631 | } |
1632 | ||
5c827ce0 | 1633 | /* Set consumer initial state */ |
412d7227 SM |
1634 | the_kernel_consumerd_state = CONSUMER_STOPPED; |
1635 | the_ust_consumerd_state = CONSUMER_STOPPED; | |
5c827ce0 | 1636 | |
b3530820 JG |
1637 | ust32_channel_monitor_pipe = lttng_pipe_open(0); |
1638 | if (!ust32_channel_monitor_pipe) { | |
1639 | ERR("Failed to create 32-bit user space consumer channel monitor pipe"); | |
1640 | retval = -1; | |
0d64e8fb | 1641 | goto stop_threads; |
b3530820 | 1642 | } |
412d7227 SM |
1643 | the_ustconsumer32_data.channel_monitor_pipe = |
1644 | lttng_pipe_release_writefd(ust32_channel_monitor_pipe); | |
1645 | if (the_ustconsumer32_data.channel_monitor_pipe < 0) { | |
b3530820 | 1646 | retval = -1; |
0d64e8fb | 1647 | goto stop_threads; |
b3530820 | 1648 | } |
62c43103 | 1649 | |
d086f507 | 1650 | /* |
92816cc3 JG |
1651 | * The rotation_thread_timer_queue structure is shared between the |
1652 | * sessiond timer thread and the rotation thread. The main thread keeps | |
1653 | * its ownership and destroys it when both threads have been joined. | |
d086f507 | 1654 | */ |
92816cc3 | 1655 | rotation_timer_queue = rotation_thread_timer_queue_create(); |
d086f507 JD |
1656 | if (!rotation_timer_queue) { |
1657 | retval = -1; | |
0d64e8fb | 1658 | goto stop_threads; |
d086f507 | 1659 | } |
bc26e826 JG |
1660 | timer_thread_parameters.rotation_thread_job_queue = |
1661 | rotation_timer_queue; | |
67e40797 | 1662 | |
b3530820 JG |
1663 | ust64_channel_monitor_pipe = lttng_pipe_open(0); |
1664 | if (!ust64_channel_monitor_pipe) { | |
1665 | ERR("Failed to create 64-bit user space consumer channel monitor pipe"); | |
1666 | retval = -1; | |
0d64e8fb | 1667 | goto stop_threads; |
b3530820 | 1668 | } |
412d7227 SM |
1669 | the_ustconsumer64_data.channel_monitor_pipe = |
1670 | lttng_pipe_release_writefd(ust64_channel_monitor_pipe); | |
1671 | if (the_ustconsumer64_data.channel_monitor_pipe < 0) { | |
b3530820 | 1672 | retval = -1; |
0d64e8fb | 1673 | goto stop_threads; |
b3530820 | 1674 | } |
847177cd | 1675 | |
1427f9b2 DG |
1676 | /* |
1677 | * Init UST app hash table. Alloc hash table before this point since | |
1678 | * cleanup() can get called after that point. | |
1679 | */ | |
4a15001e | 1680 | if (ust_app_ht_alloc()) { |
ddbeb0f6 | 1681 | ERR("Failed to allocate UST app hash table"); |
4a15001e | 1682 | retval = -1; |
0d64e8fb | 1683 | goto stop_threads; |
4a15001e | 1684 | } |
1427f9b2 | 1685 | |
761ffce2 FD |
1686 | event_notifier_error_accounting_status = event_notifier_error_accounting_init( |
1687 | the_config.event_notifier_buffer_size_kernel, | |
1688 | the_config.event_notifier_buffer_size_userspace); | |
1689 | if (event_notifier_error_accounting_status != EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK) { | |
1690 | ERR("Failed to initialize event notifier error accounting system"); | |
1691 | retval = -1; | |
1692 | goto stop_threads; | |
1693 | } | |
90aa04a1 | 1694 | |
6a4e4039 JG |
1695 | /* |
1696 | * Initialize agent app hash table. We allocate the hash table here | |
1697 | * since cleanup() can get called after this point. | |
1698 | */ | |
1699 | if (agent_app_ht_alloc()) { | |
1700 | ERR("Failed to allocate Agent app hash table"); | |
4a15001e | 1701 | retval = -1; |
0d64e8fb | 1702 | goto stop_threads; |
f20baf8e DG |
1703 | } |
1704 | ||
44760c20 JR |
1705 | if (agent_by_event_notifier_domain_ht_create()) { |
1706 | ERR("Failed to allocate per-event notifier domain agent hash table"); | |
1707 | retval = -1; | |
1708 | goto stop_threads; | |
1709 | } | |
a88df331 DG |
1710 | /* |
1711 | * These actions must be executed as root. We do that *after* setting up | |
1712 | * the sockets path because we MUST make the check for another daemon using | |
1713 | * those paths *before* trying to set the kernel consumer sockets and init | |
1714 | * kernel tracer. | |
1715 | */ | |
1716 | if (is_root) { | |
412d7227 | 1717 | if (set_consumer_sockets(&the_kconsumer_data)) { |
4a15001e | 1718 | retval = -1; |
0d64e8fb | 1719 | goto stop_threads; |
7753dea8 MD |
1720 | } |
1721 | ||
a88df331 | 1722 | /* Setup kernel tracer */ |
412d7227 | 1723 | if (!the_config.no_kernel) { |
4fba7219 DG |
1724 | init_kernel_tracer(); |
1725 | } | |
a88df331 DG |
1726 | |
1727 | /* Set ulimit for open files */ | |
1728 | set_ulimit(); | |
fac6795d | 1729 | } |
4063050c MD |
1730 | /* init lttng_fd tracking must be done after set_ulimit. */ |
1731 | lttng_fd_init(); | |
fac6795d | 1732 | |
412d7227 | 1733 | if (set_consumer_sockets(&the_ustconsumer64_data)) { |
4a15001e | 1734 | retval = -1; |
0d64e8fb | 1735 | goto stop_threads; |
67e40797 DG |
1736 | } |
1737 | ||
412d7227 | 1738 | if (set_consumer_sockets(&the_ustconsumer32_data)) { |
4a15001e | 1739 | retval = -1; |
0d64e8fb | 1740 | goto stop_threads; |
67e40797 DG |
1741 | } |
1742 | ||
5b8719f5 | 1743 | /* Get parent pid if -S, --sig-parent is specified. */ |
412d7227 SM |
1744 | if (the_config.sig_parent) { |
1745 | the_ppid = getppid(); | |
5b8719f5 DG |
1746 | } |
1747 | ||
7a485870 | 1748 | /* Setup the kernel pipe for waking up the kernel thread */ |
412d7227 SM |
1749 | if (is_root && !the_config.no_kernel) { |
1750 | if (utils_create_pipe_cloexec(the_kernel_poll_pipe)) { | |
4a15001e | 1751 | retval = -1; |
0d64e8fb | 1752 | goto stop_threads; |
6620da75 | 1753 | } |
7a485870 DG |
1754 | } |
1755 | ||
099e26bd | 1756 | /* Setup the thread apps communication pipe. */ |
4a15001e MD |
1757 | if (utils_create_pipe_cloexec(apps_cmd_pipe)) { |
1758 | retval = -1; | |
0d64e8fb | 1759 | goto stop_threads; |
099e26bd DG |
1760 | } |
1761 | ||
d0b96690 | 1762 | /* Setup the thread apps notify communication pipe. */ |
4a15001e MD |
1763 | if (utils_create_pipe_cloexec(apps_cmd_notify_pipe)) { |
1764 | retval = -1; | |
0d64e8fb | 1765 | goto stop_threads; |
d0b96690 DG |
1766 | } |
1767 | ||
7972aab2 DG |
1768 | /* Initialize global buffer per UID and PID registry. */ |
1769 | buffer_reg_init_uid_registry(); | |
1770 | buffer_reg_init_pid_registry(); | |
1771 | ||
099e26bd | 1772 | /* Init UST command queue. */ |
8bdee6e2 | 1773 | cds_wfcq_init(&ust_cmd_queue.head, &ust_cmd_queue.tail); |
099e26bd | 1774 | |
2f77fc4b | 1775 | cmd_init(); |
00e2e675 | 1776 | |
ae9e45b3 DG |
1777 | /* Check for the application socket timeout env variable. */ |
1778 | env_app_timeout = getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV); | |
1779 | if (env_app_timeout) { | |
412d7227 | 1780 | the_config.app_socket_timeout = atoi(env_app_timeout); |
ae9e45b3 | 1781 | } else { |
412d7227 | 1782 | the_config.app_socket_timeout = DEFAULT_APP_SOCKET_RW_TIMEOUT; |
ae9e45b3 DG |
1783 | } |
1784 | ||
4a15001e MD |
1785 | ret = write_pidfile(); |
1786 | if (ret) { | |
1787 | ERR("Error in write_pidfile"); | |
1788 | retval = -1; | |
0d64e8fb | 1789 | goto stop_threads; |
4a15001e | 1790 | } |
35f90c40 | 1791 | |
554831e7 MD |
1792 | /* Initialize communication library */ |
1793 | lttcomm_init(); | |
4a15001e | 1794 | /* Initialize TCP timeout values */ |
d831c249 | 1795 | lttcomm_inet_init(); |
554831e7 | 1796 | |
12b4fb37 | 1797 | /* Create health-check thread. */ |
173fca4f | 1798 | if (!launch_health_management_thread()) { |
4a15001e | 1799 | retval = -1; |
0d64e8fb | 1800 | goto stop_threads; |
44a5e5eb DG |
1801 | } |
1802 | ||
b3530820 | 1803 | /* notification_thread_data acquires the pipes' read side. */ |
412d7227 SM |
1804 | the_notification_thread_handle = notification_thread_handle_create( |
1805 | ust32_channel_monitor_pipe, ust64_channel_monitor_pipe, | |
c8a9de5a | 1806 | kernel_channel_monitor_pipe); |
412d7227 | 1807 | if (!the_notification_thread_handle) { |
b3530820 JG |
1808 | retval = -1; |
1809 | ERR("Failed to create notification thread shared data"); | |
0d64e8fb | 1810 | goto stop_threads; |
b3530820 JG |
1811 | } |
1812 | ||
1813 | /* Create notification thread. */ | |
4a91420c | 1814 | notification_thread = launch_notification_thread( |
412d7227 | 1815 | the_notification_thread_handle); |
4a91420c | 1816 | if (!notification_thread) { |
b3530820 | 1817 | retval = -1; |
0d64e8fb | 1818 | goto stop_threads; |
b3530820 JG |
1819 | } |
1820 | ||
d086f507 | 1821 | /* Create timer thread. */ |
bc26e826 | 1822 | if (!launch_timer_thread(&timer_thread_parameters)) { |
d086f507 | 1823 | retval = -1; |
0d64e8fb | 1824 | goto stop_threads; |
d086f507 | 1825 | } |
d086f507 | 1826 | |
db66e574 JD |
1827 | /* rotation_thread_data acquires the pipes' read side. */ |
1828 | rotation_thread_handle = rotation_thread_handle_create( | |
412d7227 | 1829 | rotation_timer_queue, the_notification_thread_handle); |
db66e574 JD |
1830 | if (!rotation_thread_handle) { |
1831 | retval = -1; | |
1832 | ERR("Failed to create rotation thread shared data"); | |
0d64e8fb | 1833 | goto stop_threads; |
db66e574 | 1834 | } |
db66e574 JD |
1835 | |
1836 | /* Create rotation thread. */ | |
64d9b072 | 1837 | if (!launch_rotation_thread(rotation_thread_handle)) { |
db66e574 | 1838 | retval = -1; |
0d64e8fb | 1839 | goto stop_threads; |
db66e574 JD |
1840 | } |
1841 | ||
cf3af59e | 1842 | /* Create thread to manage the client socket */ |
917a718d JG |
1843 | client_thread = launch_client_thread(); |
1844 | if (!client_thread) { | |
4a15001e | 1845 | retval = -1; |
0d64e8fb | 1846 | goto stop_threads; |
cf3af59e | 1847 | } |
fac6795d | 1848 | |
308df7bb | 1849 | /* Set credentials of the client socket and rundir */ |
412d7227 | 1850 | if (is_root && set_permissions(the_config.rundir.value)) { |
308df7bb JG |
1851 | retval = -1; |
1852 | goto stop_threads; | |
1853 | } | |
1854 | ||
5d1b0219 JG |
1855 | if (!launch_ust_dispatch_thread(&ust_cmd_queue, apps_cmd_pipe[1], |
1856 | apps_cmd_notify_pipe[1])) { | |
4a15001e | 1857 | retval = -1; |
0d64e8fb | 1858 | goto stop_threads; |
099e26bd DG |
1859 | } |
1860 | ||
1861 | /* Create thread to manage application registration. */ | |
bd9addf7 JG |
1862 | register_apps_thread = launch_application_registration_thread( |
1863 | &ust_cmd_queue); | |
1864 | if (!register_apps_thread) { | |
4a15001e | 1865 | retval = -1; |
0d64e8fb | 1866 | goto stop_threads; |
099e26bd DG |
1867 | } |
1868 | ||
cf3af59e | 1869 | /* Create thread to manage application socket */ |
7649924e | 1870 | if (!launch_application_management_thread(apps_cmd_pipe[0])) { |
4a15001e | 1871 | retval = -1; |
0d64e8fb | 1872 | goto stop_threads; |
d0b96690 DG |
1873 | } |
1874 | ||
1875 | /* Create thread to manage application notify socket */ | |
971a61c6 | 1876 | if (!launch_application_notification_thread(apps_cmd_notify_pipe[0])) { |
4a15001e | 1877 | retval = -1; |
0d64e8fb | 1878 | goto stop_threads; |
cf3af59e | 1879 | } |
fac6795d | 1880 | |
8a7e4590 JG |
1881 | /* Create agent management thread. */ |
1882 | if (!launch_agent_management_thread()) { | |
4a15001e | 1883 | retval = -1; |
0d64e8fb | 1884 | goto stop_threads; |
4d076222 DG |
1885 | } |
1886 | ||
6620da75 | 1887 | /* Don't start this thread if kernel tracing is not requested nor root */ |
412d7227 | 1888 | if (is_root && !the_config.no_kernel) { |
6620da75 | 1889 | /* Create kernel thread to manage kernel event */ |
412d7227 | 1890 | if (!launch_kernel_management_thread(the_kernel_poll_pipe[0])) { |
4a15001e | 1891 | retval = -1; |
0d64e8fb | 1892 | goto stop_threads; |
6620da75 | 1893 | } |
352b58f5 JR |
1894 | |
1895 | if (kernel_get_notification_fd() >= 0) { | |
1896 | ret = notification_thread_command_add_tracer_event_source( | |
412d7227 | 1897 | the_notification_thread_handle, |
352b58f5 JR |
1898 | kernel_get_notification_fd(), |
1899 | LTTNG_DOMAIN_KERNEL); | |
1900 | if (ret != LTTNG_OK) { | |
1901 | ERR("Failed to add kernel trigger event source to notification thread"); | |
1902 | retval = -1; | |
1903 | goto stop_threads; | |
1904 | } | |
1905 | } | |
ef367a93 | 1906 | } |
7a485870 | 1907 | |
3316b9d6 | 1908 | /* Load sessions. */ |
412d7227 SM |
1909 | ret = config_load_session( |
1910 | the_config.load_session_path.value, NULL, 1, 1, NULL); | |
4a15001e | 1911 | if (ret) { |
3316b9d6 | 1912 | ERR("Session load failed: %s", error_get_str(ret)); |
4a15001e | 1913 | retval = -1; |
0d64e8fb | 1914 | goto stop_threads; |
4a15001e MD |
1915 | } |
1916 | ||
3316b9d6 JG |
1917 | /* Initialization completed. */ |
1918 | sessiond_signal_parents(); | |
1919 | ||
4a15001e MD |
1920 | /* |
1921 | * This is where we start awaiting program completion (e.g. through | |
1922 | * signal that asks threads to teardown). | |
1923 | */ | |
1924 | ||
f90a04ed MJ |
1925 | /* Initiate teardown once activity occurs on the main quit pipe. */ |
1926 | sessiond_wait_for_main_quit_pipe(-1); | |
917a718d | 1927 | |
0d64e8fb | 1928 | stop_threads: |
90aa04a1 | 1929 | |
f90a04ed MJ |
1930 | DBG("Terminating all threads"); |
1931 | ||
917a718d JG |
1932 | /* |
1933 | * Ensure that the client thread is no longer accepting new commands, | |
1934 | * which could cause new sessions to be created. | |
1935 | */ | |
0d64e8fb JG |
1936 | if (client_thread) { |
1937 | lttng_thread_shutdown(client_thread); | |
917a718d | 1938 | lttng_thread_put(client_thread); |
917a718d JG |
1939 | } |
1940 | ||
99d688f2 | 1941 | destroy_all_sessions_and_wait(); |
0d64e8fb | 1942 | |
b69a1b40 JG |
1943 | /* |
1944 | * At this point no new trigger can be registered (no sessions are | |
1945 | * running/rotating) and clients can't connect to the session daemon | |
1946 | * anymore. Unregister all triggers. | |
1947 | */ | |
1948 | unregister_all_triggers(); | |
1949 | ||
bd9addf7 JG |
1950 | if (register_apps_thread) { |
1951 | lttng_thread_shutdown(register_apps_thread); | |
1952 | lttng_thread_put(register_apps_thread); | |
1953 | } | |
a3707772 | 1954 | lttng_thread_list_shutdown_orphans(); |
917a718d | 1955 | |
4d62fbf8 MD |
1956 | /* |
1957 | * Wait for all pending call_rcu work to complete before tearing | |
1958 | * down data structures. call_rcu worker may be trying to | |
1959 | * perform lookups in those structures. | |
1960 | */ | |
1961 | rcu_barrier(); | |
3c339053 | 1962 | |
4a15001e MD |
1963 | rcu_thread_online(); |
1964 | sessiond_cleanup(); | |
06f525de | 1965 | |
3b8a092f | 1966 | /* |
1d25334c | 1967 | * Wait for all pending call_rcu work to complete before shutting down |
3b8a092f FD |
1968 | * the notification thread. This call_rcu work includes shutting down |
1969 | * UST apps and event notifier pipes. | |
1970 | */ | |
1971 | rcu_barrier(); | |
1972 | ||
4a91420c JG |
1973 | if (notification_thread) { |
1974 | lttng_thread_shutdown(notification_thread); | |
1975 | lttng_thread_put(notification_thread); | |
1976 | } | |
1977 | ||
90aa04a1 | 1978 | /* |
1d25334c FD |
1979 | * Error accounting teardown has to be done after the teardown of all |
1980 | * event notifier pipes to ensure that no tracer may try to use the | |
1981 | * error accounting facilities. | |
90aa04a1 FD |
1982 | */ |
1983 | event_notifier_error_accounting_fini(); | |
1984 | ||
1d25334c FD |
1985 | /* |
1986 | * Unloading the kernel modules needs to be done after all kernel | |
1987 | * ressources have been released. In our case, this includes the | |
1988 | * notification fd, the event notifier group fd, error accounting fd, | |
1989 | * all event and event notifier fds, etc. | |
1990 | * | |
1991 | * In short, at this point, we need to have called close() on all fds | |
1992 | * received from the kernel tracer. | |
1993 | */ | |
412d7227 | 1994 | if (is_root && !the_config.no_kernel) { |
1d25334c FD |
1995 | DBG("Unloading kernel modules"); |
1996 | modprobe_remove_lttng_all(); | |
1997 | } | |
1998 | ||
64d9b072 JG |
1999 | rcu_thread_offline(); |
2000 | rcu_unregister_thread(); | |
2001 | ||
2002 | if (rotation_thread_handle) { | |
2003 | rotation_thread_handle_destroy(rotation_thread_handle); | |
2004 | } | |
2005 | ||
d086f507 JD |
2006 | /* |
2007 | * After the rotation and timer thread have quit, we can safely destroy | |
2008 | * the rotation_timer_queue. | |
2009 | */ | |
92816cc3 | 2010 | rotation_thread_timer_queue_destroy(rotation_timer_queue); |
c8a9de5a JG |
2011 | /* |
2012 | * The teardown of the notification system is performed after the | |
2013 | * session daemon's teardown in order to allow it to be notified | |
2014 | * of the active session and channels at the moment of the teardown. | |
2015 | */ | |
412d7227 SM |
2016 | if (the_notification_thread_handle) { |
2017 | notification_thread_handle_destroy( | |
2018 | the_notification_thread_handle); | |
c8a9de5a | 2019 | } |
b3530820 JG |
2020 | lttng_pipe_destroy(ust32_channel_monitor_pipe); |
2021 | lttng_pipe_destroy(ust64_channel_monitor_pipe); | |
2022 | lttng_pipe_destroy(kernel_channel_monitor_pipe); | |
4a15001e | 2023 | |
412d7227 SM |
2024 | if (the_health_sessiond) { |
2025 | health_app_destroy(the_health_sessiond); | |
9bc2ec5f | 2026 | } |
7567352f | 2027 | exit_create_run_as_worker_cleanup: |
4a15001e | 2028 | exit_options: |
f472090a | 2029 | sessiond_cleanup_lock_file(); |
4a15001e MD |
2030 | sessiond_cleanup_options(); |
2031 | ||
2032 | exit_set_signal_handler: | |
2033 | if (!retval) { | |
cf3af59e | 2034 | exit(EXIT_SUCCESS); |
4a15001e MD |
2035 | } else { |
2036 | exit(EXIT_FAILURE); | |
67e40797 | 2037 | } |
fac6795d | 2038 | } |