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