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