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