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