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