Commit | Line | Data |
---|---|---|
826d496d | 1 | /* |
a4eb26f0 | 2 | * Copyright (C) 2011 EfficiOS Inc. |
ab5be9fa MJ |
3 | * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
4 | * Copyright (C) 2013 Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
fac6795d | 5 | * |
ab5be9fa | 6 | * SPDX-License-Identifier: GPL-2.0-only |
91d76f53 | 7 | * |
fac6795d DG |
8 | */ |
9 | ||
6c1c0768 | 10 | #define _LGPL_SOURCE |
fac6795d DG |
11 | #include <getopt.h> |
12 | #include <grp.h> | |
13 | #include <limits.h> | |
0bb7724a | 14 | #include <paths.h> |
fac6795d DG |
15 | #include <pthread.h> |
16 | #include <signal.h> | |
17 | #include <stdio.h> | |
18 | #include <stdlib.h> | |
19 | #include <string.h> | |
331744e3 | 20 | #include <inttypes.h> |
0fdd1e2c | 21 | #include <sys/mman.h> |
b73401da | 22 | #include <sys/mount.h> |
1e307fab | 23 | #include <sys/resource.h> |
fac6795d DG |
24 | #include <sys/socket.h> |
25 | #include <sys/stat.h> | |
26 | #include <sys/types.h> | |
0fdd1e2c | 27 | #include <sys/wait.h> |
5c827ce0 | 28 | #include <urcu/uatomic.h> |
fac6795d | 29 | #include <unistd.h> |
4f0b90ee | 30 | #include <ctype.h> |
fac6795d | 31 | |
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> |
e40c873f | 40 | #include <common/path.h> |
3ccdf997 | 41 | #include <common/daemonize.h> |
f40ef1d5 | 42 | #include <common/config/session-config.h> |
dcabc190 | 43 | #include <common/dynamic-buffer.h> |
76fcf151 | 44 | #include <lttng/event-internal.h> |
fac6795d | 45 | |
10a8a223 | 46 | #include "lttng-sessiond.h" |
7972aab2 | 47 | #include "buffer-registry.h" |
54d01ffb | 48 | #include "channel.h" |
2f77fc4b | 49 | #include "cmd.h" |
00e2e675 | 50 | #include "consumer.h" |
099e26bd | 51 | #include "context.h" |
54d01ffb | 52 | #include "event.h" |
4771f025 | 53 | #include "kernel.h" |
f1e16794 | 54 | #include "kernel-consumer.h" |
0fdd1e2c | 55 | #include "shm.h" |
75018ab6 | 56 | #include "lttng-ust-ctl.h" |
00e2e675 | 57 | #include "ust-consumer.h" |
8e68d1c8 | 58 | #include "utils.h" |
4063050c | 59 | #include "fd-limit.h" |
8782cc74 | 60 | #include "health-sessiond.h" |
8ac94142 | 61 | #include "testpoint.h" |
f9d2ba6a | 62 | #include "notify-apps.h" |
022d91ba | 63 | #include "agent-thread.h" |
fb198a11 | 64 | #include "save.h" |
b3530820 JG |
65 | #include "notification-thread.h" |
66 | #include "notification-thread-commands.h" | |
db66e574 | 67 | #include "rotation-thread.h" |
7c1d2758 | 68 | #include "agent.h" |
5e97de00 | 69 | #include "ht-cleanup.h" |
e6142f2e | 70 | #include "sessiond-config.h" |
8e319828 | 71 | #include "timer.h" |
a3707772 | 72 | #include "thread.h" |
917a718d | 73 | #include "client.h" |
5d1b0219 | 74 | #include "dispatch.h" |
1785d7f2 | 75 | #include "register.h" |
7649924e | 76 | #include "manage-apps.h" |
5b093681 | 77 | #include "manage-kernel.h" |
ebaeda94 | 78 | |
4fc83d94 PP |
79 | static const char *help_msg = |
80 | #ifdef LTTNG_EMBED_HELP | |
81 | #include <lttng-sessiond.8.h> | |
82 | #else | |
83 | NULL | |
84 | #endif | |
85 | ; | |
86 | ||
fac6795d | 87 | const char *progname; |
c9cb3e7d | 88 | static int lockfile_fd = -1; |
a3bc3918 | 89 | static int opt_print_version; |
3bd1e081 | 90 | |
0bb7724a DG |
91 | /* Set to 1 when a SIGUSR1 signal is received. */ |
92 | static int recv_child_signal; | |
93 | ||
26296c48 JG |
94 | /* Command line options */ |
95 | static const struct option long_options[] = { | |
0f5ea17c JG |
96 | { "client-sock", required_argument, 0, 'c' }, |
97 | { "apps-sock", required_argument, 0, 'a' }, | |
98 | { "kconsumerd-cmd-sock", required_argument, 0, '\0' }, | |
99 | { "kconsumerd-err-sock", required_argument, 0, '\0' }, | |
100 | { "ustconsumerd32-cmd-sock", required_argument, 0, '\0' }, | |
101 | { "ustconsumerd32-err-sock", required_argument, 0, '\0' }, | |
102 | { "ustconsumerd64-cmd-sock", required_argument, 0, '\0' }, | |
103 | { "ustconsumerd64-err-sock", required_argument, 0, '\0' }, | |
104 | { "consumerd32-path", required_argument, 0, '\0' }, | |
105 | { "consumerd32-libdir", required_argument, 0, '\0' }, | |
106 | { "consumerd64-path", required_argument, 0, '\0' }, | |
107 | { "consumerd64-libdir", required_argument, 0, '\0' }, | |
108 | { "daemonize", no_argument, 0, 'd' }, | |
109 | { "background", no_argument, 0, 'b' }, | |
110 | { "sig-parent", no_argument, 0, 'S' }, | |
111 | { "help", no_argument, 0, 'h' }, | |
112 | { "group", required_argument, 0, 'g' }, | |
113 | { "version", no_argument, 0, 'V' }, | |
114 | { "quiet", no_argument, 0, 'q' }, | |
115 | { "verbose", no_argument, 0, 'v' }, | |
116 | { "verbose-consumer", no_argument, 0, '\0' }, | |
117 | { "no-kernel", no_argument, 0, '\0' }, | |
118 | { "pidfile", required_argument, 0, 'p' }, | |
119 | { "agent-tcp-port", required_argument, 0, '\0' }, | |
120 | { "config", required_argument, 0, 'f' }, | |
121 | { "load", required_argument, 0, 'l' }, | |
122 | { "kmod-probes", required_argument, 0, '\0' }, | |
123 | { "extra-kmod-probes", required_argument, 0, '\0' }, | |
26296c48 JG |
124 | { NULL, 0, 0, 0 } |
125 | }; | |
126 | ||
127 | /* Command line options to ignore from configuration file */ | |
128 | static const char *config_ignore_options[] = { "help", "version", "config" }; | |
1d4b027a | 129 | |
099e26bd DG |
130 | /* |
131 | * This pipe is used to inform the thread managing application communication | |
132 | * that a command is queued and ready to be processed. | |
133 | */ | |
76d7553f | 134 | static int apps_cmd_pipe[2] = { -1, -1 }; |
971a61c6 | 135 | static int apps_cmd_notify_pipe[2] = { -1, -1 }; |
099e26bd | 136 | |
099e26bd DG |
137 | /* |
138 | * UST registration command queue. This queue is tied with a futex and uses a N | |
139 | * wakers / 1 waiter implemented and detailed in futex.c/.h | |
140 | * | |
b22c5da8 DG |
141 | * The thread_registration_apps and thread_dispatch_ust_registration uses this |
142 | * queue along with the wait/wake scheme. The thread_manage_apps receives down | |
143 | * the line new application socket and monitors it for any I/O error or clean | |
144 | * close that triggers an unregistration of the application. | |
099e26bd DG |
145 | */ |
146 | static struct ust_cmd_queue ust_cmd_queue; | |
147 | ||
97bc1426 | 148 | /* |
a7333da7 | 149 | * Section name to look for in the daemon configuration file. |
97bc1426 | 150 | */ |
a7333da7 | 151 | static const char * const config_section_name = "sessiond"; |
5e97de00 | 152 | |
a7333da7 JG |
153 | /* Am I root or not. Set to 1 if the daemon is running as root */ |
154 | static int is_root; | |
5eb91c98 | 155 | |
099e26bd DG |
156 | /* |
157 | * Stop all threads by closing the thread quit pipe. | |
158 | */ | |
cf3af59e MD |
159 | static void stop_threads(void) |
160 | { | |
5eb91c98 DG |
161 | int ret; |
162 | ||
cf3af59e MD |
163 | /* Stopping all threads */ |
164 | DBG("Terminating all threads"); | |
a7333da7 | 165 | ret = sessiond_notify_quit_pipe(); |
5eb91c98 DG |
166 | if (ret < 0) { |
167 | ERR("write error on thread quit pipe"); | |
168 | } | |
cf3af59e MD |
169 | } |
170 | ||
e975f9f8 DG |
171 | /* |
172 | * Close every consumer sockets. | |
173 | */ | |
174 | static void close_consumer_sockets(void) | |
175 | { | |
176 | int ret; | |
177 | ||
178 | if (kconsumer_data.err_sock >= 0) { | |
179 | ret = close(kconsumer_data.err_sock); | |
180 | if (ret < 0) { | |
181 | PERROR("kernel consumer err_sock close"); | |
182 | } | |
183 | } | |
184 | if (ustconsumer32_data.err_sock >= 0) { | |
185 | ret = close(ustconsumer32_data.err_sock); | |
186 | if (ret < 0) { | |
a76cbd9f | 187 | PERROR("UST consumerd32 err_sock close"); |
e975f9f8 DG |
188 | } |
189 | } | |
190 | if (ustconsumer64_data.err_sock >= 0) { | |
191 | ret = close(ustconsumer64_data.err_sock); | |
192 | if (ret < 0) { | |
a76cbd9f | 193 | PERROR("UST consumerd64 err_sock close"); |
e975f9f8 DG |
194 | } |
195 | } | |
196 | if (kconsumer_data.cmd_sock >= 0) { | |
197 | ret = close(kconsumer_data.cmd_sock); | |
198 | if (ret < 0) { | |
199 | PERROR("kernel consumer cmd_sock close"); | |
200 | } | |
201 | } | |
202 | if (ustconsumer32_data.cmd_sock >= 0) { | |
203 | ret = close(ustconsumer32_data.cmd_sock); | |
204 | if (ret < 0) { | |
a76cbd9f | 205 | PERROR("UST consumerd32 cmd_sock close"); |
e975f9f8 DG |
206 | } |
207 | } | |
208 | if (ustconsumer64_data.cmd_sock >= 0) { | |
209 | ret = close(ustconsumer64_data.cmd_sock); | |
210 | if (ret < 0) { | |
a76cbd9f | 211 | PERROR("UST consumerd64 cmd_sock close"); |
e975f9f8 DG |
212 | } |
213 | } | |
b3530820 JG |
214 | if (kconsumer_data.channel_monitor_pipe >= 0) { |
215 | ret = close(kconsumer_data.channel_monitor_pipe); | |
216 | if (ret < 0) { | |
217 | PERROR("kernel consumer channel monitor pipe close"); | |
218 | } | |
219 | } | |
220 | if (ustconsumer32_data.channel_monitor_pipe >= 0) { | |
221 | ret = close(ustconsumer32_data.channel_monitor_pipe); | |
222 | if (ret < 0) { | |
223 | PERROR("UST consumerd32 channel monitor pipe close"); | |
224 | } | |
225 | } | |
226 | if (ustconsumer64_data.channel_monitor_pipe >= 0) { | |
227 | ret = close(ustconsumer64_data.channel_monitor_pipe); | |
228 | if (ret < 0) { | |
229 | PERROR("UST consumerd64 channel monitor pipe close"); | |
230 | } | |
231 | } | |
e975f9f8 DG |
232 | } |
233 | ||
4e4714cb JR |
234 | /* |
235 | * Wait on consumer process termination. | |
236 | * | |
237 | * Need to be called with the consumer data lock held or from a context | |
238 | * ensuring no concurrent access to data (e.g: cleanup). | |
239 | */ | |
240 | static void wait_consumer(struct consumer_data *consumer_data) | |
241 | { | |
242 | pid_t ret; | |
243 | int status; | |
244 | ||
245 | if (consumer_data->pid <= 0) { | |
246 | return; | |
247 | } | |
248 | ||
249 | DBG("Waiting for complete teardown of consumerd (PID: %d)", | |
250 | consumer_data->pid); | |
251 | ret = waitpid(consumer_data->pid, &status, 0); | |
252 | if (ret == -1) { | |
253 | PERROR("consumerd waitpid pid: %d", consumer_data->pid) | |
1640c24c | 254 | } else if (!WIFEXITED(status)) { |
4e4714cb JR |
255 | ERR("consumerd termination with error: %d", |
256 | WEXITSTATUS(ret)); | |
257 | } | |
258 | consumer_data->pid = 0; | |
259 | } | |
260 | ||
fac6795d | 261 | /* |
4a15001e | 262 | * Cleanup the session daemon's data structures. |
fac6795d | 263 | */ |
4a15001e | 264 | static void sessiond_cleanup(void) |
fac6795d | 265 | { |
ef599319 | 266 | int ret; |
e32d7f27 | 267 | struct ltt_session_list *session_list = session_get_list(); |
fac6795d | 268 | |
4a15001e | 269 | DBG("Cleanup sessiond"); |
e07ae692 | 270 | |
4e449f3f MD |
271 | /* |
272 | * Close the thread quit pipe. It has already done its job, | |
273 | * since we are now called. | |
274 | */ | |
a7333da7 | 275 | sessiond_close_quit_pipe(); |
971a61c6 JG |
276 | utils_close_pipe(apps_cmd_pipe); |
277 | utils_close_pipe(apps_cmd_notify_pipe); | |
5b093681 | 278 | utils_close_pipe(kernel_poll_pipe); |
2f77fc4b | 279 | |
c9a2957d JG |
280 | ret = remove(config.pid_file_path.value); |
281 | if (ret < 0) { | |
282 | PERROR("remove pidfile %s", config.pid_file_path.value); | |
35f90c40 DG |
283 | } |
284 | ||
e6142f2e JG |
285 | DBG("Removing sessiond and consumerd content of directory %s", |
286 | config.rundir.value); | |
8c6c56c2 MD |
287 | |
288 | /* sessiond */ | |
e6142f2e JG |
289 | DBG("Removing %s", config.pid_file_path.value); |
290 | (void) unlink(config.pid_file_path.value); | |
8c6c56c2 | 291 | |
e6142f2e JG |
292 | DBG("Removing %s", config.agent_port_file_path.value); |
293 | (void) unlink(config.agent_port_file_path.value); | |
cd9290dd | 294 | |
8c6c56c2 | 295 | /* kconsumerd */ |
e6142f2e JG |
296 | DBG("Removing %s", kconsumer_data.err_unix_sock_path); |
297 | (void) unlink(kconsumer_data.err_unix_sock_path); | |
298 | ||
299 | DBG("Removing directory %s", config.kconsumerd_path.value); | |
300 | (void) rmdir(config.kconsumerd_path.value); | |
8c6c56c2 MD |
301 | |
302 | /* ust consumerd 32 */ | |
e6142f2e JG |
303 | DBG("Removing %s", config.consumerd32_err_unix_sock_path.value); |
304 | (void) unlink(config.consumerd32_err_unix_sock_path.value); | |
305 | ||
306 | DBG("Removing directory %s", config.consumerd32_path.value); | |
307 | (void) rmdir(config.consumerd32_path.value); | |
8c6c56c2 MD |
308 | |
309 | /* ust consumerd 64 */ | |
e6142f2e JG |
310 | DBG("Removing %s", config.consumerd64_err_unix_sock_path.value); |
311 | (void) unlink(config.consumerd64_err_unix_sock_path.value); | |
312 | ||
313 | DBG("Removing directory %s", config.consumerd64_path.value); | |
314 | (void) rmdir(config.consumerd64_path.value); | |
5461b305 | 315 | |
99d688f2 | 316 | pthread_mutex_destroy(&session_list->lock); |
273ea72c | 317 | |
6a4e4039 JG |
318 | DBG("Cleaning up all agent apps"); |
319 | agent_app_ht_clean(); | |
099e26bd | 320 | DBG("Closing all UST sockets"); |
56fff090 | 321 | ust_app_clean_list(); |
7972aab2 | 322 | buffer_reg_destroy_registries(); |
099e26bd | 323 | |
0787fc4b MD |
324 | close_consumer_sockets(); |
325 | ||
326 | wait_consumer(&kconsumer_data); | |
327 | wait_consumer(&ustconsumer64_data); | |
328 | wait_consumer(&ustconsumer32_data); | |
329 | ||
e6142f2e | 330 | if (is_root && !config.no_kernel) { |
7d268848 | 331 | cleanup_kernel_tracer(); |
2f50c8a3 | 332 | } |
2f77fc4b | 333 | |
c9cb3e7d JG |
334 | /* |
335 | * We do NOT rmdir rundir because there are other processes | |
336 | * using it, for instance lttng-relayd, which can start in | |
337 | * parallel with this teardown. | |
338 | */ | |
4a15001e MD |
339 | } |
340 | ||
341 | /* | |
342 | * Cleanup the daemon's option data structures. | |
343 | */ | |
344 | static void sessiond_cleanup_options(void) | |
345 | { | |
346 | DBG("Cleaning up options"); | |
347 | ||
e6142f2e | 348 | sessiond_config_fini(&config); |
c9cb3e7d | 349 | |
7567352f | 350 | run_as_destroy_worker(); |
fac6795d DG |
351 | } |
352 | ||
917a718d | 353 | static int string_match(const char *str1, const char *str2) |
54d01ffb | 354 | { |
917a718d | 355 | return (str1 && str2) && !strcmp(str1, str2); |
54d01ffb DG |
356 | } |
357 | ||
00e2e675 | 358 | /* |
917a718d JG |
359 | * Take an option from the getopt output and set it in the right variable to be |
360 | * used later. | |
36b588ed | 361 | * |
917a718d | 362 | * Return 0 on success else a negative value. |
00e2e675 | 363 | */ |
917a718d | 364 | static int set_option(int opt, const char *arg, const char *optname) |
00e2e675 | 365 | { |
917a718d | 366 | int ret = 0; |
fac6795d | 367 | |
c0232ea5 | 368 | if (string_match(optname, "client-sock") || opt == 'c') { |
66b2ce8e JG |
369 | if (!arg || *arg == '\0') { |
370 | ret = -EINVAL; | |
371 | goto end; | |
372 | } | |
e8fa9fb0 MD |
373 | if (lttng_is_setuid_setgid()) { |
374 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
375 | "-c, --client-sock"); | |
376 | } else { | |
e6142f2e JG |
377 | config_string_set(&config.client_unix_sock_path, |
378 | strdup(arg)); | |
379 | if (!config.client_unix_sock_path.value) { | |
380 | ret = -ENOMEM; | |
381 | PERROR("strdup"); | |
382 | } | |
e8fa9fb0 | 383 | } |
c0232ea5 | 384 | } else if (string_match(optname, "apps-sock") || opt == 'a') { |
66b2ce8e JG |
385 | if (!arg || *arg == '\0') { |
386 | ret = -EINVAL; | |
387 | goto end; | |
388 | } | |
e8fa9fb0 MD |
389 | if (lttng_is_setuid_setgid()) { |
390 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
391 | "-a, --apps-sock"); | |
392 | } else { | |
e6142f2e JG |
393 | config_string_set(&config.apps_unix_sock_path, |
394 | strdup(arg)); | |
395 | if (!config.apps_unix_sock_path.value) { | |
396 | ret = -ENOMEM; | |
397 | PERROR("strdup"); | |
398 | } | |
e8fa9fb0 | 399 | } |
c0232ea5 | 400 | } else if (string_match(optname, "daemonize") || opt == 'd') { |
e6142f2e | 401 | config.daemonize = true; |
c0232ea5 | 402 | } else if (string_match(optname, "background") || opt == 'b') { |
e6142f2e | 403 | config.background = true; |
c0232ea5 | 404 | } else if (string_match(optname, "group") || opt == 'g') { |
66b2ce8e JG |
405 | if (!arg || *arg == '\0') { |
406 | ret = -EINVAL; | |
407 | goto end; | |
408 | } | |
e8fa9fb0 MD |
409 | if (lttng_is_setuid_setgid()) { |
410 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
411 | "-g, --group"); | |
412 | } else { | |
e6142f2e JG |
413 | config_string_set(&config.tracing_group_name, |
414 | strdup(arg)); | |
415 | if (!config.tracing_group_name.value) { | |
e8fa9fb0 | 416 | ret = -ENOMEM; |
e6142f2e | 417 | PERROR("strdup"); |
e8fa9fb0 | 418 | } |
db322c4d | 419 | } |
c0232ea5 | 420 | } else if (string_match(optname, "help") || opt == 'h') { |
4fc83d94 | 421 | ret = utils_show_help(8, "lttng-sessiond", help_msg); |
8190767e | 422 | if (ret) { |
4fc83d94 | 423 | ERR("Cannot show --help for `lttng-sessiond`"); |
8190767e PP |
424 | perror("exec"); |
425 | } | |
426 | exit(ret ? EXIT_FAILURE : EXIT_SUCCESS); | |
c0232ea5 | 427 | } else if (string_match(optname, "version") || opt == 'V') { |
a3bc3918 | 428 | opt_print_version = 1; |
c0232ea5 | 429 | } else if (string_match(optname, "sig-parent") || opt == 'S') { |
e6142f2e | 430 | config.sig_parent = true; |
c0232ea5 | 431 | } else if (string_match(optname, "kconsumerd-err-sock")) { |
66b2ce8e JG |
432 | if (!arg || *arg == '\0') { |
433 | ret = -EINVAL; | |
434 | goto end; | |
435 | } | |
e8fa9fb0 MD |
436 | if (lttng_is_setuid_setgid()) { |
437 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
438 | "--kconsumerd-err-sock"); | |
439 | } else { | |
e6142f2e JG |
440 | config_string_set(&config.kconsumerd_err_unix_sock_path, |
441 | strdup(arg)); | |
442 | if (!config.kconsumerd_err_unix_sock_path.value) { | |
443 | ret = -ENOMEM; | |
444 | PERROR("strdup"); | |
445 | } | |
e8fa9fb0 | 446 | } |
c0232ea5 | 447 | } else if (string_match(optname, "kconsumerd-cmd-sock")) { |
66b2ce8e JG |
448 | if (!arg || *arg == '\0') { |
449 | ret = -EINVAL; | |
450 | goto end; | |
451 | } | |
e8fa9fb0 MD |
452 | if (lttng_is_setuid_setgid()) { |
453 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
454 | "--kconsumerd-cmd-sock"); | |
455 | } else { | |
e6142f2e JG |
456 | config_string_set(&config.kconsumerd_cmd_unix_sock_path, |
457 | strdup(arg)); | |
458 | if (!config.kconsumerd_cmd_unix_sock_path.value) { | |
459 | ret = -ENOMEM; | |
460 | PERROR("strdup"); | |
461 | } | |
e8fa9fb0 | 462 | } |
c0232ea5 | 463 | } else if (string_match(optname, "ustconsumerd64-err-sock")) { |
66b2ce8e JG |
464 | if (!arg || *arg == '\0') { |
465 | ret = -EINVAL; | |
466 | goto end; | |
467 | } | |
e8fa9fb0 MD |
468 | if (lttng_is_setuid_setgid()) { |
469 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
470 | "--ustconsumerd64-err-sock"); | |
471 | } else { | |
e6142f2e JG |
472 | config_string_set(&config.consumerd64_err_unix_sock_path, |
473 | strdup(arg)); | |
474 | if (!config.consumerd64_err_unix_sock_path.value) { | |
475 | ret = -ENOMEM; | |
476 | PERROR("strdup"); | |
477 | } | |
e8fa9fb0 | 478 | } |
c0232ea5 | 479 | } else if (string_match(optname, "ustconsumerd64-cmd-sock")) { |
66b2ce8e JG |
480 | if (!arg || *arg == '\0') { |
481 | ret = -EINVAL; | |
482 | goto end; | |
483 | } | |
e8fa9fb0 MD |
484 | if (lttng_is_setuid_setgid()) { |
485 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
486 | "--ustconsumerd64-cmd-sock"); | |
487 | } else { | |
e6142f2e JG |
488 | config_string_set(&config.consumerd64_cmd_unix_sock_path, |
489 | strdup(arg)); | |
490 | if (!config.consumerd64_cmd_unix_sock_path.value) { | |
491 | ret = -ENOMEM; | |
492 | PERROR("strdup"); | |
493 | } | |
e8fa9fb0 | 494 | } |
c0232ea5 | 495 | } else if (string_match(optname, "ustconsumerd32-err-sock")) { |
66b2ce8e JG |
496 | if (!arg || *arg == '\0') { |
497 | ret = -EINVAL; | |
498 | goto end; | |
499 | } | |
e8fa9fb0 MD |
500 | if (lttng_is_setuid_setgid()) { |
501 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
502 | "--ustconsumerd32-err-sock"); | |
503 | } else { | |
e6142f2e JG |
504 | config_string_set(&config.consumerd32_err_unix_sock_path, |
505 | strdup(arg)); | |
506 | if (!config.consumerd32_err_unix_sock_path.value) { | |
507 | ret = -ENOMEM; | |
508 | PERROR("strdup"); | |
509 | } | |
e8fa9fb0 | 510 | } |
c0232ea5 | 511 | } else if (string_match(optname, "ustconsumerd32-cmd-sock")) { |
66b2ce8e JG |
512 | if (!arg || *arg == '\0') { |
513 | ret = -EINVAL; | |
514 | goto end; | |
515 | } | |
e8fa9fb0 MD |
516 | if (lttng_is_setuid_setgid()) { |
517 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
518 | "--ustconsumerd32-cmd-sock"); | |
519 | } else { | |
e6142f2e JG |
520 | config_string_set(&config.consumerd32_cmd_unix_sock_path, |
521 | strdup(arg)); | |
522 | if (!config.consumerd32_cmd_unix_sock_path.value) { | |
523 | ret = -ENOMEM; | |
524 | PERROR("strdup"); | |
525 | } | |
e8fa9fb0 | 526 | } |
c0232ea5 | 527 | } else if (string_match(optname, "no-kernel")) { |
e6142f2e | 528 | config.no_kernel = true; |
c0232ea5 | 529 | } else if (string_match(optname, "quiet") || opt == 'q') { |
6d9a9c65 | 530 | config.quiet = true; |
c0232ea5 | 531 | } else if (string_match(optname, "verbose") || opt == 'v') { |
26296c48 JG |
532 | /* Verbose level can increase using multiple -v */ |
533 | if (arg) { | |
13755a18 | 534 | /* Value obtained from config file */ |
e6142f2e | 535 | config.verbose = config_parse_value(arg); |
26296c48 | 536 | } else { |
13755a18 | 537 | /* -v used on command line */ |
e6142f2e | 538 | config.verbose++; |
26296c48 | 539 | } |
13755a18 | 540 | /* Clamp value to [0, 3] */ |
e6142f2e JG |
541 | config.verbose = config.verbose < 0 ? 0 : |
542 | (config.verbose <= 3 ? config.verbose : 3); | |
c0232ea5 | 543 | } else if (string_match(optname, "verbose-consumer")) { |
26296c48 | 544 | if (arg) { |
e6142f2e | 545 | config.verbose_consumer = config_parse_value(arg); |
26296c48 | 546 | } else { |
e6142f2e | 547 | config.verbose_consumer++; |
26296c48 | 548 | } |
c0232ea5 | 549 | } else if (string_match(optname, "consumerd32-path")) { |
66b2ce8e JG |
550 | if (!arg || *arg == '\0') { |
551 | ret = -EINVAL; | |
552 | goto end; | |
553 | } | |
e8fa9fb0 MD |
554 | if (lttng_is_setuid_setgid()) { |
555 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
556 | "--consumerd32-path"); | |
557 | } else { | |
e6142f2e JG |
558 | config_string_set(&config.consumerd32_bin_path, |
559 | strdup(arg)); | |
560 | if (!config.consumerd32_bin_path.value) { | |
e8fa9fb0 MD |
561 | PERROR("strdup"); |
562 | ret = -ENOMEM; | |
563 | } | |
db322c4d | 564 | } |
c0232ea5 | 565 | } else if (string_match(optname, "consumerd32-libdir")) { |
66b2ce8e JG |
566 | if (!arg || *arg == '\0') { |
567 | ret = -EINVAL; | |
568 | goto end; | |
569 | } | |
e8fa9fb0 MD |
570 | if (lttng_is_setuid_setgid()) { |
571 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
572 | "--consumerd32-libdir"); | |
573 | } else { | |
e6142f2e JG |
574 | config_string_set(&config.consumerd32_lib_dir, |
575 | strdup(arg)); | |
576 | if (!config.consumerd32_lib_dir.value) { | |
e8fa9fb0 MD |
577 | PERROR("strdup"); |
578 | ret = -ENOMEM; | |
579 | } | |
db322c4d | 580 | } |
c0232ea5 | 581 | } else if (string_match(optname, "consumerd64-path")) { |
66b2ce8e JG |
582 | if (!arg || *arg == '\0') { |
583 | ret = -EINVAL; | |
584 | goto end; | |
585 | } | |
e8fa9fb0 MD |
586 | if (lttng_is_setuid_setgid()) { |
587 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
588 | "--consumerd64-path"); | |
589 | } else { | |
e6142f2e JG |
590 | config_string_set(&config.consumerd64_bin_path, |
591 | strdup(arg)); | |
592 | if (!config.consumerd64_bin_path.value) { | |
e8fa9fb0 MD |
593 | PERROR("strdup"); |
594 | ret = -ENOMEM; | |
595 | } | |
db322c4d | 596 | } |
c0232ea5 | 597 | } else if (string_match(optname, "consumerd64-libdir")) { |
66b2ce8e JG |
598 | if (!arg || *arg == '\0') { |
599 | ret = -EINVAL; | |
600 | goto end; | |
601 | } | |
e8fa9fb0 MD |
602 | if (lttng_is_setuid_setgid()) { |
603 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
604 | "--consumerd64-libdir"); | |
605 | } else { | |
e6142f2e JG |
606 | config_string_set(&config.consumerd64_lib_dir, |
607 | strdup(arg)); | |
608 | if (!config.consumerd64_lib_dir.value) { | |
e8fa9fb0 MD |
609 | PERROR("strdup"); |
610 | ret = -ENOMEM; | |
611 | } | |
db322c4d | 612 | } |
c0232ea5 | 613 | } else if (string_match(optname, "pidfile") || opt == 'p') { |
66b2ce8e JG |
614 | if (!arg || *arg == '\0') { |
615 | ret = -EINVAL; | |
616 | goto end; | |
617 | } | |
e8fa9fb0 MD |
618 | if (lttng_is_setuid_setgid()) { |
619 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
620 | "-p, --pidfile"); | |
621 | } else { | |
e6142f2e JG |
622 | config_string_set(&config.pid_file_path, strdup(arg)); |
623 | if (!config.pid_file_path.value) { | |
e8fa9fb0 MD |
624 | PERROR("strdup"); |
625 | ret = -ENOMEM; | |
626 | } | |
db322c4d | 627 | } |
c0232ea5 | 628 | } else if (string_match(optname, "agent-tcp-port")) { |
66b2ce8e JG |
629 | if (!arg || *arg == '\0') { |
630 | ret = -EINVAL; | |
631 | goto end; | |
632 | } | |
e8fa9fb0 MD |
633 | if (lttng_is_setuid_setgid()) { |
634 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
635 | "--agent-tcp-port"); | |
636 | } else { | |
637 | unsigned long v; | |
4d076222 | 638 | |
e8fa9fb0 MD |
639 | errno = 0; |
640 | v = strtoul(arg, NULL, 0); | |
641 | if (errno != 0 || !isdigit(arg[0])) { | |
642 | ERR("Wrong value in --agent-tcp-port parameter: %s", arg); | |
643 | return -1; | |
644 | } | |
645 | if (v == 0 || v >= 65535) { | |
646 | ERR("Port overflow in --agent-tcp-port parameter: %s", arg); | |
647 | return -1; | |
648 | } | |
2288467f JG |
649 | config.agent_tcp_port.begin = config.agent_tcp_port.end = (int) v; |
650 | DBG3("Agent TCP port set to non default: %i", (int) v); | |
26296c48 | 651 | } |
c0232ea5 | 652 | } else if (string_match(optname, "load") || opt == 'l') { |
66b2ce8e JG |
653 | if (!arg || *arg == '\0') { |
654 | ret = -EINVAL; | |
655 | goto end; | |
656 | } | |
e8fa9fb0 MD |
657 | if (lttng_is_setuid_setgid()) { |
658 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
659 | "-l, --load"); | |
660 | } else { | |
e6142f2e JG |
661 | config_string_set(&config.load_session_path, strdup(arg)); |
662 | if (!config.load_session_path.value) { | |
e8fa9fb0 MD |
663 | PERROR("strdup"); |
664 | ret = -ENOMEM; | |
665 | } | |
ef367a93 | 666 | } |
c0232ea5 | 667 | } else if (string_match(optname, "kmod-probes")) { |
66b2ce8e JG |
668 | if (!arg || *arg == '\0') { |
669 | ret = -EINVAL; | |
670 | goto end; | |
671 | } | |
e8fa9fb0 MD |
672 | if (lttng_is_setuid_setgid()) { |
673 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
674 | "--kmod-probes"); | |
675 | } else { | |
e6142f2e JG |
676 | config_string_set(&config.kmod_probes_list, strdup(arg)); |
677 | if (!config.kmod_probes_list.value) { | |
e8fa9fb0 MD |
678 | PERROR("strdup"); |
679 | ret = -ENOMEM; | |
680 | } | |
c9d42407 | 681 | } |
c0232ea5 | 682 | } else if (string_match(optname, "extra-kmod-probes")) { |
66b2ce8e JG |
683 | if (!arg || *arg == '\0') { |
684 | ret = -EINVAL; | |
685 | goto end; | |
686 | } | |
e8fa9fb0 MD |
687 | if (lttng_is_setuid_setgid()) { |
688 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
689 | "--extra-kmod-probes"); | |
690 | } else { | |
e6142f2e JG |
691 | config_string_set(&config.kmod_extra_probes_list, |
692 | strdup(arg)); | |
693 | if (!config.kmod_extra_probes_list.value) { | |
e8fa9fb0 MD |
694 | PERROR("strdup"); |
695 | ret = -ENOMEM; | |
696 | } | |
db322c4d | 697 | } |
c0232ea5 JG |
698 | } else if (string_match(optname, "config") || opt == 'f') { |
699 | /* This is handled in set_options() thus silent skip. */ | |
700 | goto end; | |
701 | } else { | |
26296c48 JG |
702 | /* Unknown option or other error. |
703 | * Error is printed by getopt, just return */ | |
704 | ret = -1; | |
705 | } | |
706 | ||
f3dd7ce7 | 707 | end: |
c5d350b2 JG |
708 | if (ret == -EINVAL) { |
709 | const char *opt_name = "unknown"; | |
710 | int i; | |
711 | ||
712 | for (i = 0; i < sizeof(long_options) / sizeof(struct option); | |
713 | i++) { | |
714 | if (opt == long_options[i].val) { | |
715 | opt_name = long_options[i].name; | |
716 | break; | |
717 | } | |
718 | } | |
719 | ||
720 | WARN("Invalid argument provided for option \"%s\", using default value.", | |
721 | opt_name); | |
722 | } | |
f3dd7ce7 | 723 | |
26296c48 JG |
724 | return ret; |
725 | } | |
726 | ||
727 | /* | |
728 | * config_entry_handler_cb used to handle options read from a config file. | |
f40ef1d5 | 729 | * See config_entry_handler_cb comment in common/config/session-config.h for the |
26296c48 JG |
730 | * return value conventions. |
731 | */ | |
732 | static int config_entry_handler(const struct config_entry *entry, void *unused) | |
733 | { | |
734 | int ret = 0, i; | |
735 | ||
736 | if (!entry || !entry->name || !entry->value) { | |
737 | ret = -EINVAL; | |
738 | goto end; | |
739 | } | |
740 | ||
741 | /* Check if the option is to be ignored */ | |
742 | for (i = 0; i < sizeof(config_ignore_options) / sizeof(char *); i++) { | |
743 | if (!strcmp(entry->name, config_ignore_options[i])) { | |
744 | goto end; | |
745 | } | |
746 | } | |
747 | ||
748 | for (i = 0; i < (sizeof(long_options) / sizeof(struct option)) - 1; | |
749 | i++) { | |
750 | ||
751 | /* Ignore if not fully matched. */ | |
752 | if (strcmp(entry->name, long_options[i].name)) { | |
753 | continue; | |
754 | } | |
755 | ||
756 | /* | |
757 | * If the option takes no argument on the command line, we have to | |
758 | * check if the value is "true". We support non-zero numeric values, | |
759 | * true, on and yes. | |
760 | */ | |
761 | if (!long_options[i].has_arg) { | |
762 | ret = config_parse_value(entry->value); | |
763 | if (ret <= 0) { | |
764 | if (ret) { | |
765 | WARN("Invalid configuration value \"%s\" for option %s", | |
766 | entry->value, entry->name); | |
767 | } | |
768 | /* False, skip boolean config option. */ | |
769 | goto end; | |
4d076222 | 770 | } |
26296c48 JG |
771 | } |
772 | ||
773 | ret = set_option(long_options[i].val, entry->value, entry->name); | |
774 | goto end; | |
775 | } | |
776 | ||
777 | WARN("Unrecognized option \"%s\" in daemon configuration file.", entry->name); | |
778 | ||
779 | end: | |
780 | return ret; | |
781 | } | |
782 | ||
a3bc3918 JR |
783 | static void print_version(void) { |
784 | fprintf(stdout, "%s\n", VERSION); | |
785 | } | |
786 | ||
26296c48 JG |
787 | /* |
788 | * daemon configuration loading and argument parsing | |
789 | */ | |
790 | static int set_options(int argc, char **argv) | |
791 | { | |
792 | int ret = 0, c = 0, option_index = 0; | |
793 | int orig_optopt = optopt, orig_optind = optind; | |
794 | char *optstring; | |
12c5f50a | 795 | char *config_path = NULL; |
26296c48 JG |
796 | |
797 | optstring = utils_generate_optstring(long_options, | |
798 | sizeof(long_options) / sizeof(struct option)); | |
799 | if (!optstring) { | |
800 | ret = -ENOMEM; | |
801 | goto end; | |
802 | } | |
803 | ||
804 | /* Check for the --config option */ | |
805 | while ((c = getopt_long(argc, argv, optstring, long_options, | |
806 | &option_index)) != -1) { | |
807 | if (c == '?') { | |
808 | ret = -EINVAL; | |
809 | goto end; | |
810 | } else if (c != 'f') { | |
811 | /* if not equal to --config option. */ | |
812 | continue; | |
813 | } | |
814 | ||
e8fa9fb0 MD |
815 | if (lttng_is_setuid_setgid()) { |
816 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
817 | "-f, --config"); | |
818 | } else { | |
12c5f50a | 819 | free(config_path); |
e8fa9fb0 MD |
820 | config_path = utils_expand_path(optarg); |
821 | if (!config_path) { | |
822 | ERR("Failed to resolve path: %s", optarg); | |
823 | } | |
26296c48 JG |
824 | } |
825 | } | |
826 | ||
827 | ret = config_get_section_entries(config_path, config_section_name, | |
828 | config_entry_handler, NULL); | |
829 | if (ret) { | |
830 | if (ret > 0) { | |
831 | ERR("Invalid configuration option at line %i", ret); | |
832 | ret = -1; | |
833 | } | |
834 | goto end; | |
835 | } | |
836 | ||
837 | /* Reset getopt's global state */ | |
838 | optopt = orig_optopt; | |
839 | optind = orig_optind; | |
840 | while (1) { | |
c0232ea5 JG |
841 | option_index = -1; |
842 | /* | |
843 | * getopt_long() will not set option_index if it encounters a | |
844 | * short option. | |
845 | */ | |
846 | c = getopt_long(argc, argv, optstring, long_options, | |
847 | &option_index); | |
26296c48 | 848 | if (c == -1) { |
4d076222 DG |
849 | break; |
850 | } | |
26296c48 | 851 | |
c0232ea5 JG |
852 | /* |
853 | * Pass NULL as the long option name if popt left the index | |
854 | * unset. | |
855 | */ | |
856 | ret = set_option(c, optarg, | |
857 | option_index < 0 ? NULL : | |
858 | long_options[option_index].name); | |
26296c48 JG |
859 | if (ret < 0) { |
860 | break; | |
fac6795d DG |
861 | } |
862 | } | |
863 | ||
26296c48 | 864 | end: |
12c5f50a | 865 | free(config_path); |
26296c48 JG |
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 | { | |
875 | return utils_create_lock_file(config.lock_file_path.value); | |
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 | { |
e6142f2e | 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 | ||
1201 | ret = asprintf(&env_value, "LTTNG_UST_CLOCK_PLUGIN=%s", | |
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 | ||
929f71ec JG |
1260 | static int run_as_worker_post_fork_cleanup(void *data) |
1261 | { | |
1262 | struct sessiond_config *sessiond_config = data; | |
1263 | ||
1264 | sessiond_config_fini(sessiond_config); | |
1265 | return 0; | |
1266 | } | |
1267 | ||
1268 | static int launch_run_as_worker(const char *procname) | |
1269 | { | |
1270 | /* | |
1271 | * Clean-up before forking the run-as worker. Any dynamically | |
1272 | * allocated memory of which the worker is not aware will | |
1273 | * be leaked as the process forks a run-as worker (and performs | |
1274 | * no exec*()). The same would apply to any opened fd. | |
1275 | */ | |
1276 | return run_as_create_worker(procname, run_as_worker_post_fork_cleanup, | |
1277 | &config); | |
1278 | } | |
1279 | ||
52a0e931 JG |
1280 | static void sessiond_uuid_log(void) |
1281 | { | |
c70636a7 | 1282 | char uuid_str[LTTNG_UUID_STR_LEN]; |
52a0e931 JG |
1283 | |
1284 | lttng_uuid_to_str(sessiond_uuid, uuid_str); | |
1285 | DBG("Starting lttng-sessiond {%s}", uuid_str); | |
1286 | } | |
1287 | ||
fac6795d DG |
1288 | /* |
1289 | * main | |
1290 | */ | |
1291 | int main(int argc, char **argv) | |
1292 | { | |
4a15001e | 1293 | int ret = 0, retval = 0; |
e6142f2e | 1294 | const char *env_app_timeout; |
b3530820 JG |
1295 | struct lttng_pipe *ust32_channel_monitor_pipe = NULL, |
1296 | *ust64_channel_monitor_pipe = NULL, | |
1297 | *kernel_channel_monitor_pipe = NULL; | |
a3707772 | 1298 | struct lttng_thread *ht_cleanup_thread = NULL; |
bc26e826 | 1299 | struct timer_thread_parameters timer_thread_parameters; |
64d9b072 JG |
1300 | /* Rotation thread handle. */ |
1301 | struct rotation_thread_handle *rotation_thread_handle = NULL; | |
d086f507 JD |
1302 | /* Queue of rotation jobs populated by the sessiond-timer. */ |
1303 | struct rotation_thread_timer_queue *rotation_timer_queue = NULL; | |
917a718d | 1304 | struct lttng_thread *client_thread = NULL; |
4a91420c | 1305 | struct lttng_thread *notification_thread = NULL; |
bd9addf7 | 1306 | struct lttng_thread *register_apps_thread = NULL; |
fac6795d | 1307 | |
335a95b7 MD |
1308 | init_kernel_workarounds(); |
1309 | ||
f6a9efaa DG |
1310 | rcu_register_thread(); |
1311 | ||
4a15001e MD |
1312 | if (set_signal_handler()) { |
1313 | retval = -1; | |
1314 | goto exit_set_signal_handler; | |
0bb7724a DG |
1315 | } |
1316 | ||
92816cc3 | 1317 | if (timer_signal_init()) { |
d086f507 JD |
1318 | retval = -1; |
1319 | goto exit_set_signal_handler; | |
1320 | } | |
1321 | ||
12744796 DG |
1322 | page_size = sysconf(_SC_PAGESIZE); |
1323 | if (page_size < 0) { | |
1324 | PERROR("sysconf _SC_PAGESIZE"); | |
1325 | page_size = LONG_MAX; | |
1326 | WARN("Fallback page size to %ld", page_size); | |
1327 | } | |
1328 | ||
e6142f2e JG |
1329 | ret = sessiond_config_init(&config); |
1330 | if (ret) { | |
1331 | retval = -1; | |
1332 | goto exit_set_signal_handler; | |
1333 | } | |
1334 | ||
2788b494 JR |
1335 | /* |
1336 | * Init config from environment variables. | |
1337 | * Command line option override env configuration per-doc. Do env first. | |
1338 | */ | |
1339 | sessiond_config_apply_env_config(&config); | |
1340 | ||
4a15001e MD |
1341 | /* |
1342 | * Parse arguments and load the daemon configuration file. | |
1343 | * | |
1344 | * We have an exit_options exit path to free memory reserved by | |
1345 | * set_options. This is needed because the rest of sessiond_cleanup() | |
1346 | * depends on ht_cleanup_thread, which depends on lttng_daemonize, which | |
1347 | * depends on set_options. | |
1348 | */ | |
fac6795d | 1349 | progname = argv[0]; |
4a15001e MD |
1350 | if (set_options(argc, argv)) { |
1351 | retval = -1; | |
1352 | goto exit_options; | |
fac6795d DG |
1353 | } |
1354 | ||
e6142f2e JG |
1355 | /* |
1356 | * Resolve all paths received as arguments, configuration option, or | |
1357 | * through environment variable as absolute paths. This is necessary | |
1358 | * since daemonizing causes the sessiond's current working directory | |
1359 | * to '/'. | |
1360 | */ | |
1361 | ret = sessiond_config_resolve_paths(&config); | |
1362 | if (ret) { | |
1363 | goto exit_options; | |
1364 | } | |
1365 | ||
1366 | /* Apply config. */ | |
1367 | lttng_opt_verbose = config.verbose; | |
1368 | lttng_opt_quiet = config.quiet; | |
1369 | kconsumer_data.err_unix_sock_path = | |
1370 | config.kconsumerd_err_unix_sock_path.value; | |
1371 | kconsumer_data.cmd_unix_sock_path = | |
1372 | config.kconsumerd_cmd_unix_sock_path.value; | |
1373 | ustconsumer32_data.err_unix_sock_path = | |
1374 | config.consumerd32_err_unix_sock_path.value; | |
1375 | ustconsumer32_data.cmd_unix_sock_path = | |
1376 | config.consumerd32_cmd_unix_sock_path.value; | |
1377 | ustconsumer64_data.err_unix_sock_path = | |
1378 | config.consumerd64_err_unix_sock_path.value; | |
1379 | ustconsumer64_data.cmd_unix_sock_path = | |
1380 | config.consumerd64_cmd_unix_sock_path.value; | |
1381 | set_clock_plugin_env(); | |
1382 | ||
1383 | sessiond_config_log(&config); | |
52a0e931 | 1384 | sessiond_uuid_log(); |
e6142f2e | 1385 | |
a3bc3918 JR |
1386 | if (opt_print_version) { |
1387 | print_version(); | |
1388 | retval = 0; | |
1389 | goto exit_options; | |
1390 | } | |
1391 | ||
f472090a JG |
1392 | if (create_lttng_rundir()) { |
1393 | retval = -1; | |
1394 | goto exit_options; | |
1395 | } | |
1396 | ||
1397 | /* Abort launch if a session daemon is already running. */ | |
1398 | if (check_existing_daemon()) { | |
1399 | ERR("A session daemon is already running."); | |
1400 | retval = -1; | |
1401 | goto exit_options; | |
1402 | } | |
1403 | ||
fac6795d | 1404 | /* Daemonize */ |
e6142f2e | 1405 | if (config.daemonize || config.background) { |
ceed52b5 MD |
1406 | int i; |
1407 | ||
72dd7491 | 1408 | ret = lttng_daemonize(&child_ppid, &recv_child_signal, |
e6142f2e | 1409 | !config.background); |
53094c05 | 1410 | if (ret < 0) { |
4a15001e MD |
1411 | retval = -1; |
1412 | goto exit_options; | |
53094c05 | 1413 | } |
0bb7724a | 1414 | |
ceed52b5 | 1415 | /* |
0bb7724a | 1416 | * We are in the child. Make sure all other file descriptors are |
4a15001e | 1417 | * closed, in case we are called with more opened file |
f472090a | 1418 | * descriptors than the standard ones and the lock file. |
ceed52b5 MD |
1419 | */ |
1420 | for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) { | |
f472090a JG |
1421 | if (i == lockfile_fd) { |
1422 | continue; | |
1423 | } | |
ceed52b5 MD |
1424 | (void) close(i); |
1425 | } | |
1426 | } | |
1427 | ||
929f71ec | 1428 | if (launch_run_as_worker(argv[0]) < 0) { |
7567352f MD |
1429 | goto exit_create_run_as_worker_cleanup; |
1430 | } | |
1431 | ||
4a15001e MD |
1432 | /* |
1433 | * Starting from here, we can create threads. This needs to be after | |
1434 | * lttng_daemonize due to RCU. | |
1435 | */ | |
1436 | ||
1437 | /* | |
1438 | * Initialize the health check subsystem. This call should set the | |
1439 | * appropriate time values. | |
1440 | */ | |
1441 | health_sessiond = health_app_create(NR_HEALTH_SESSIOND_TYPES); | |
1442 | if (!health_sessiond) { | |
1443 | PERROR("health_app_create error"); | |
1444 | retval = -1; | |
0d64e8fb | 1445 | goto stop_threads; |
4a15001e MD |
1446 | } |
1447 | ||
4a15001e | 1448 | /* Create thread to clean up RCU hash tables */ |
a3707772 JG |
1449 | ht_cleanup_thread = launch_ht_cleanup_thread(); |
1450 | if (!ht_cleanup_thread) { | |
4a15001e | 1451 | retval = -1; |
0d64e8fb | 1452 | goto stop_threads; |
4a15001e MD |
1453 | } |
1454 | ||
ceed52b5 | 1455 | /* Create thread quit pipe */ |
a7333da7 | 1456 | if (sessiond_init_thread_quit_pipe()) { |
4a15001e | 1457 | retval = -1; |
0d64e8fb | 1458 | goto stop_threads; |
fac6795d DG |
1459 | } |
1460 | ||
1461 | /* Check if daemon is UID = 0 */ | |
1462 | is_root = !getuid(); | |
fac6795d | 1463 | if (is_root) { |
67e40797 | 1464 | /* Create global run dir with root access */ |
67e40797 | 1465 | |
b3530820 JG |
1466 | kernel_channel_monitor_pipe = lttng_pipe_open(0); |
1467 | if (!kernel_channel_monitor_pipe) { | |
1468 | ERR("Failed to create kernel consumer channel monitor pipe"); | |
1469 | retval = -1; | |
0d64e8fb | 1470 | goto stop_threads; |
b3530820 JG |
1471 | } |
1472 | kconsumer_data.channel_monitor_pipe = | |
1473 | lttng_pipe_release_writefd( | |
1474 | kernel_channel_monitor_pipe); | |
1475 | if (kconsumer_data.channel_monitor_pipe < 0) { | |
1476 | retval = -1; | |
0d64e8fb | 1477 | goto stop_threads; |
b3530820 | 1478 | } |
fac6795d DG |
1479 | } |
1480 | ||
5c827ce0 DG |
1481 | /* Set consumer initial state */ |
1482 | kernel_consumerd_state = CONSUMER_STOPPED; | |
1483 | ust_consumerd_state = CONSUMER_STOPPED; | |
1484 | ||
b3530820 JG |
1485 | ust32_channel_monitor_pipe = lttng_pipe_open(0); |
1486 | if (!ust32_channel_monitor_pipe) { | |
1487 | ERR("Failed to create 32-bit user space consumer channel monitor pipe"); | |
1488 | retval = -1; | |
0d64e8fb | 1489 | goto stop_threads; |
b3530820 JG |
1490 | } |
1491 | ustconsumer32_data.channel_monitor_pipe = lttng_pipe_release_writefd( | |
1492 | ust32_channel_monitor_pipe); | |
1493 | if (ustconsumer32_data.channel_monitor_pipe < 0) { | |
1494 | retval = -1; | |
0d64e8fb | 1495 | goto stop_threads; |
b3530820 | 1496 | } |
62c43103 | 1497 | |
d086f507 | 1498 | /* |
92816cc3 JG |
1499 | * The rotation_thread_timer_queue structure is shared between the |
1500 | * sessiond timer thread and the rotation thread. The main thread keeps | |
1501 | * its ownership and destroys it when both threads have been joined. | |
d086f507 | 1502 | */ |
92816cc3 | 1503 | rotation_timer_queue = rotation_thread_timer_queue_create(); |
d086f507 JD |
1504 | if (!rotation_timer_queue) { |
1505 | retval = -1; | |
0d64e8fb | 1506 | goto stop_threads; |
d086f507 | 1507 | } |
bc26e826 JG |
1508 | timer_thread_parameters.rotation_thread_job_queue = |
1509 | rotation_timer_queue; | |
67e40797 | 1510 | |
b3530820 JG |
1511 | ust64_channel_monitor_pipe = lttng_pipe_open(0); |
1512 | if (!ust64_channel_monitor_pipe) { | |
1513 | ERR("Failed to create 64-bit user space consumer channel monitor pipe"); | |
1514 | retval = -1; | |
0d64e8fb | 1515 | goto stop_threads; |
b3530820 JG |
1516 | } |
1517 | ustconsumer64_data.channel_monitor_pipe = lttng_pipe_release_writefd( | |
1518 | ust64_channel_monitor_pipe); | |
1519 | if (ustconsumer64_data.channel_monitor_pipe < 0) { | |
1520 | retval = -1; | |
0d64e8fb | 1521 | goto stop_threads; |
b3530820 | 1522 | } |
847177cd | 1523 | |
1427f9b2 DG |
1524 | /* |
1525 | * Init UST app hash table. Alloc hash table before this point since | |
1526 | * cleanup() can get called after that point. | |
1527 | */ | |
4a15001e | 1528 | if (ust_app_ht_alloc()) { |
ddbeb0f6 | 1529 | ERR("Failed to allocate UST app hash table"); |
4a15001e | 1530 | retval = -1; |
0d64e8fb | 1531 | goto stop_threads; |
4a15001e | 1532 | } |
1427f9b2 | 1533 | |
6a4e4039 JG |
1534 | /* |
1535 | * Initialize agent app hash table. We allocate the hash table here | |
1536 | * since cleanup() can get called after this point. | |
1537 | */ | |
1538 | if (agent_app_ht_alloc()) { | |
1539 | ERR("Failed to allocate Agent app hash table"); | |
4a15001e | 1540 | retval = -1; |
0d64e8fb | 1541 | goto stop_threads; |
f20baf8e DG |
1542 | } |
1543 | ||
a88df331 DG |
1544 | /* |
1545 | * These actions must be executed as root. We do that *after* setting up | |
1546 | * the sockets path because we MUST make the check for another daemon using | |
1547 | * those paths *before* trying to set the kernel consumer sockets and init | |
1548 | * kernel tracer. | |
1549 | */ | |
1550 | if (is_root) { | |
e6142f2e | 1551 | if (set_consumer_sockets(&kconsumer_data)) { |
4a15001e | 1552 | retval = -1; |
0d64e8fb | 1553 | goto stop_threads; |
7753dea8 MD |
1554 | } |
1555 | ||
a88df331 | 1556 | /* Setup kernel tracer */ |
e6142f2e | 1557 | if (!config.no_kernel) { |
4fba7219 DG |
1558 | init_kernel_tracer(); |
1559 | } | |
a88df331 DG |
1560 | |
1561 | /* Set ulimit for open files */ | |
1562 | set_ulimit(); | |
fac6795d | 1563 | } |
4063050c MD |
1564 | /* init lttng_fd tracking must be done after set_ulimit. */ |
1565 | lttng_fd_init(); | |
fac6795d | 1566 | |
e6142f2e | 1567 | if (set_consumer_sockets(&ustconsumer64_data)) { |
4a15001e | 1568 | retval = -1; |
0d64e8fb | 1569 | goto stop_threads; |
67e40797 DG |
1570 | } |
1571 | ||
e6142f2e | 1572 | if (set_consumer_sockets(&ustconsumer32_data)) { |
4a15001e | 1573 | retval = -1; |
0d64e8fb | 1574 | goto stop_threads; |
67e40797 DG |
1575 | } |
1576 | ||
5b8719f5 | 1577 | /* Get parent pid if -S, --sig-parent is specified. */ |
e6142f2e | 1578 | if (config.sig_parent) { |
5b8719f5 DG |
1579 | ppid = getppid(); |
1580 | } | |
1581 | ||
7a485870 | 1582 | /* Setup the kernel pipe for waking up the kernel thread */ |
e6142f2e | 1583 | if (is_root && !config.no_kernel) { |
4a15001e MD |
1584 | if (utils_create_pipe_cloexec(kernel_poll_pipe)) { |
1585 | retval = -1; | |
0d64e8fb | 1586 | goto stop_threads; |
6620da75 | 1587 | } |
7a485870 DG |
1588 | } |
1589 | ||
099e26bd | 1590 | /* Setup the thread apps communication pipe. */ |
4a15001e MD |
1591 | if (utils_create_pipe_cloexec(apps_cmd_pipe)) { |
1592 | retval = -1; | |
0d64e8fb | 1593 | goto stop_threads; |
099e26bd DG |
1594 | } |
1595 | ||
d0b96690 | 1596 | /* Setup the thread apps notify communication pipe. */ |
4a15001e MD |
1597 | if (utils_create_pipe_cloexec(apps_cmd_notify_pipe)) { |
1598 | retval = -1; | |
0d64e8fb | 1599 | goto stop_threads; |
d0b96690 DG |
1600 | } |
1601 | ||
7972aab2 DG |
1602 | /* Initialize global buffer per UID and PID registry. */ |
1603 | buffer_reg_init_uid_registry(); | |
1604 | buffer_reg_init_pid_registry(); | |
1605 | ||
099e26bd | 1606 | /* Init UST command queue. */ |
8bdee6e2 | 1607 | cds_wfcq_init(&ust_cmd_queue.head, &ust_cmd_queue.tail); |
099e26bd | 1608 | |
2f77fc4b | 1609 | cmd_init(); |
00e2e675 | 1610 | |
ae9e45b3 DG |
1611 | /* Check for the application socket timeout env variable. */ |
1612 | env_app_timeout = getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV); | |
1613 | if (env_app_timeout) { | |
e6142f2e | 1614 | config.app_socket_timeout = atoi(env_app_timeout); |
ae9e45b3 | 1615 | } else { |
e6142f2e | 1616 | config.app_socket_timeout = DEFAULT_APP_SOCKET_RW_TIMEOUT; |
ae9e45b3 DG |
1617 | } |
1618 | ||
4a15001e MD |
1619 | ret = write_pidfile(); |
1620 | if (ret) { | |
1621 | ERR("Error in write_pidfile"); | |
1622 | retval = -1; | |
0d64e8fb | 1623 | goto stop_threads; |
4a15001e | 1624 | } |
35f90c40 | 1625 | |
554831e7 MD |
1626 | /* Initialize communication library */ |
1627 | lttcomm_init(); | |
4a15001e | 1628 | /* Initialize TCP timeout values */ |
d831c249 | 1629 | lttcomm_inet_init(); |
554831e7 | 1630 | |
12b4fb37 | 1631 | /* Create health-check thread. */ |
173fca4f | 1632 | if (!launch_health_management_thread()) { |
4a15001e | 1633 | retval = -1; |
0d64e8fb | 1634 | goto stop_threads; |
44a5e5eb DG |
1635 | } |
1636 | ||
b3530820 JG |
1637 | /* notification_thread_data acquires the pipes' read side. */ |
1638 | notification_thread_handle = notification_thread_handle_create( | |
1639 | ust32_channel_monitor_pipe, | |
1640 | ust64_channel_monitor_pipe, | |
c8a9de5a | 1641 | kernel_channel_monitor_pipe); |
b3530820 JG |
1642 | if (!notification_thread_handle) { |
1643 | retval = -1; | |
1644 | ERR("Failed to create notification thread shared data"); | |
0d64e8fb | 1645 | goto stop_threads; |
b3530820 JG |
1646 | } |
1647 | ||
1648 | /* Create notification thread. */ | |
4a91420c JG |
1649 | notification_thread = launch_notification_thread( |
1650 | notification_thread_handle); | |
1651 | if (!notification_thread) { | |
b3530820 | 1652 | retval = -1; |
0d64e8fb | 1653 | goto stop_threads; |
b3530820 JG |
1654 | } |
1655 | ||
d086f507 | 1656 | /* Create timer thread. */ |
bc26e826 | 1657 | if (!launch_timer_thread(&timer_thread_parameters)) { |
d086f507 | 1658 | retval = -1; |
0d64e8fb | 1659 | goto stop_threads; |
d086f507 | 1660 | } |
d086f507 | 1661 | |
db66e574 JD |
1662 | /* rotation_thread_data acquires the pipes' read side. */ |
1663 | rotation_thread_handle = rotation_thread_handle_create( | |
90936dcf | 1664 | rotation_timer_queue, |
c8a9de5a | 1665 | notification_thread_handle); |
db66e574 JD |
1666 | if (!rotation_thread_handle) { |
1667 | retval = -1; | |
1668 | ERR("Failed to create rotation thread shared data"); | |
1669 | stop_threads(); | |
0d64e8fb | 1670 | goto stop_threads; |
db66e574 | 1671 | } |
db66e574 JD |
1672 | |
1673 | /* Create rotation thread. */ | |
64d9b072 | 1674 | if (!launch_rotation_thread(rotation_thread_handle)) { |
db66e574 | 1675 | retval = -1; |
0d64e8fb | 1676 | goto stop_threads; |
db66e574 JD |
1677 | } |
1678 | ||
cf3af59e | 1679 | /* Create thread to manage the client socket */ |
917a718d JG |
1680 | client_thread = launch_client_thread(); |
1681 | if (!client_thread) { | |
4a15001e | 1682 | retval = -1; |
0d64e8fb | 1683 | goto stop_threads; |
cf3af59e | 1684 | } |
fac6795d | 1685 | |
308df7bb JG |
1686 | /* Set credentials of the client socket and rundir */ |
1687 | if (is_root && set_permissions(config.rundir.value)) { | |
1688 | retval = -1; | |
1689 | goto stop_threads; | |
1690 | } | |
1691 | ||
5d1b0219 JG |
1692 | if (!launch_ust_dispatch_thread(&ust_cmd_queue, apps_cmd_pipe[1], |
1693 | apps_cmd_notify_pipe[1])) { | |
4a15001e | 1694 | retval = -1; |
0d64e8fb | 1695 | goto stop_threads; |
099e26bd DG |
1696 | } |
1697 | ||
1698 | /* Create thread to manage application registration. */ | |
bd9addf7 JG |
1699 | register_apps_thread = launch_application_registration_thread( |
1700 | &ust_cmd_queue); | |
1701 | if (!register_apps_thread) { | |
4a15001e | 1702 | retval = -1; |
0d64e8fb | 1703 | goto stop_threads; |
099e26bd DG |
1704 | } |
1705 | ||
cf3af59e | 1706 | /* Create thread to manage application socket */ |
7649924e | 1707 | if (!launch_application_management_thread(apps_cmd_pipe[0])) { |
4a15001e | 1708 | retval = -1; |
0d64e8fb | 1709 | goto stop_threads; |
d0b96690 DG |
1710 | } |
1711 | ||
1712 | /* Create thread to manage application notify socket */ | |
971a61c6 | 1713 | if (!launch_application_notification_thread(apps_cmd_notify_pipe[0])) { |
4a15001e | 1714 | retval = -1; |
0d64e8fb | 1715 | goto stop_threads; |
cf3af59e | 1716 | } |
fac6795d | 1717 | |
8a7e4590 JG |
1718 | /* Create agent management thread. */ |
1719 | if (!launch_agent_management_thread()) { | |
4a15001e | 1720 | retval = -1; |
0d64e8fb | 1721 | goto stop_threads; |
4d076222 DG |
1722 | } |
1723 | ||
6620da75 | 1724 | /* Don't start this thread if kernel tracing is not requested nor root */ |
e6142f2e | 1725 | if (is_root && !config.no_kernel) { |
6620da75 | 1726 | /* Create kernel thread to manage kernel event */ |
5b093681 | 1727 | if (!launch_kernel_management_thread(kernel_poll_pipe[0])) { |
4a15001e | 1728 | retval = -1; |
0d64e8fb | 1729 | goto stop_threads; |
6620da75 | 1730 | } |
ef367a93 | 1731 | } |
7a485870 | 1732 | |
3316b9d6 JG |
1733 | /* Load sessions. */ |
1734 | ret = config_load_session(config.load_session_path.value, | |
1735 | NULL, 1, 1, NULL); | |
4a15001e | 1736 | if (ret) { |
3316b9d6 | 1737 | ERR("Session load failed: %s", error_get_str(ret)); |
4a15001e | 1738 | retval = -1; |
0d64e8fb | 1739 | goto stop_threads; |
4a15001e MD |
1740 | } |
1741 | ||
3316b9d6 JG |
1742 | /* Initialization completed. */ |
1743 | sessiond_signal_parents(); | |
1744 | ||
4a15001e MD |
1745 | /* |
1746 | * This is where we start awaiting program completion (e.g. through | |
1747 | * signal that asks threads to teardown). | |
1748 | */ | |
1749 | ||
99d688f2 | 1750 | /* Initiate teardown once activity occurs on the quit pipe. */ |
2d54bfb6 | 1751 | sessiond_wait_for_quit_pipe(-1); |
917a718d | 1752 | |
0d64e8fb | 1753 | stop_threads: |
917a718d JG |
1754 | /* |
1755 | * Ensure that the client thread is no longer accepting new commands, | |
1756 | * which could cause new sessions to be created. | |
1757 | */ | |
0d64e8fb JG |
1758 | if (client_thread) { |
1759 | lttng_thread_shutdown(client_thread); | |
917a718d | 1760 | lttng_thread_put(client_thread); |
917a718d JG |
1761 | } |
1762 | ||
99d688f2 | 1763 | destroy_all_sessions_and_wait(); |
0d64e8fb | 1764 | |
bd9addf7 JG |
1765 | if (register_apps_thread) { |
1766 | lttng_thread_shutdown(register_apps_thread); | |
1767 | lttng_thread_put(register_apps_thread); | |
1768 | } | |
a3707772 | 1769 | lttng_thread_list_shutdown_orphans(); |
917a718d | 1770 | |
4d62fbf8 MD |
1771 | /* |
1772 | * Wait for all pending call_rcu work to complete before tearing | |
1773 | * down data structures. call_rcu worker may be trying to | |
1774 | * perform lookups in those structures. | |
1775 | */ | |
1776 | rcu_barrier(); | |
4a15001e MD |
1777 | /* |
1778 | * sessiond_cleanup() is called when no other thread is running, except | |
1779 | * the ht_cleanup thread, which is needed to destroy the hash tables. | |
1780 | */ | |
1781 | rcu_thread_online(); | |
1782 | sessiond_cleanup(); | |
06f525de | 1783 | |
4a91420c JG |
1784 | if (notification_thread) { |
1785 | lttng_thread_shutdown(notification_thread); | |
1786 | lttng_thread_put(notification_thread); | |
1787 | } | |
1788 | ||
c4d5de9d JG |
1789 | /* |
1790 | * Ensure all prior call_rcu are done. call_rcu callbacks may push | |
1791 | * hash tables to the ht_cleanup thread. Therefore, we ensure that | |
1792 | * the queue is empty before shutting down the clean-up thread. | |
1793 | */ | |
1794 | rcu_barrier(); | |
1795 | ||
a3707772 JG |
1796 | if (ht_cleanup_thread) { |
1797 | lttng_thread_shutdown(ht_cleanup_thread); | |
1798 | lttng_thread_put(ht_cleanup_thread); | |
1799 | } | |
1800 | ||
64d9b072 JG |
1801 | rcu_thread_offline(); |
1802 | rcu_unregister_thread(); | |
1803 | ||
1804 | if (rotation_thread_handle) { | |
1805 | rotation_thread_handle_destroy(rotation_thread_handle); | |
1806 | } | |
1807 | ||
d086f507 JD |
1808 | /* |
1809 | * After the rotation and timer thread have quit, we can safely destroy | |
1810 | * the rotation_timer_queue. | |
1811 | */ | |
92816cc3 | 1812 | rotation_thread_timer_queue_destroy(rotation_timer_queue); |
c8a9de5a JG |
1813 | /* |
1814 | * The teardown of the notification system is performed after the | |
1815 | * session daemon's teardown in order to allow it to be notified | |
1816 | * of the active session and channels at the moment of the teardown. | |
1817 | */ | |
1818 | if (notification_thread_handle) { | |
1819 | notification_thread_handle_destroy(notification_thread_handle); | |
1820 | } | |
b3530820 JG |
1821 | lttng_pipe_destroy(ust32_channel_monitor_pipe); |
1822 | lttng_pipe_destroy(ust64_channel_monitor_pipe); | |
1823 | lttng_pipe_destroy(kernel_channel_monitor_pipe); | |
4a15001e | 1824 | |
9bc2ec5f JR |
1825 | if (health_sessiond) { |
1826 | health_app_destroy(health_sessiond); | |
1827 | } | |
7567352f | 1828 | exit_create_run_as_worker_cleanup: |
4a15001e | 1829 | exit_options: |
f472090a | 1830 | sessiond_cleanup_lock_file(); |
4a15001e MD |
1831 | sessiond_cleanup_options(); |
1832 | ||
1833 | exit_set_signal_handler: | |
1834 | if (!retval) { | |
cf3af59e | 1835 | exit(EXIT_SUCCESS); |
4a15001e MD |
1836 | } else { |
1837 | exit(EXIT_FAILURE); | |
67e40797 | 1838 | } |
fac6795d | 1839 | } |