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" |
1e307fab | 65 | #include "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" |
4ac33400 | 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" |
432b38d7 | 80 | #include "timer.h" |
548468b9 | 81 | #include "thread.h" |
050e613c | 82 | #include "client.h" |
dd5875e7 | 83 | #include "dispatch.h" |
22b80785 | 84 | #include "register.h" |
cdffb210 | 85 | #include "manage-apps.h" |
45f6525f | 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; |
3bd1e081 | 98 | |
0bb7724a DG |
99 | /* Set to 1 when a SIGUSR1 signal is received. */ |
100 | static int recv_child_signal; | |
101 | ||
26296c48 JG |
102 | /* Command line options */ |
103 | static const struct option long_options[] = { | |
0f5ea17c JG |
104 | { "client-sock", required_argument, 0, 'c' }, |
105 | { "apps-sock", required_argument, 0, 'a' }, | |
106 | { "kconsumerd-cmd-sock", required_argument, 0, '\0' }, | |
107 | { "kconsumerd-err-sock", required_argument, 0, '\0' }, | |
108 | { "ustconsumerd32-cmd-sock", required_argument, 0, '\0' }, | |
109 | { "ustconsumerd32-err-sock", required_argument, 0, '\0' }, | |
110 | { "ustconsumerd64-cmd-sock", required_argument, 0, '\0' }, | |
111 | { "ustconsumerd64-err-sock", required_argument, 0, '\0' }, | |
112 | { "consumerd32-path", required_argument, 0, '\0' }, | |
113 | { "consumerd32-libdir", required_argument, 0, '\0' }, | |
114 | { "consumerd64-path", required_argument, 0, '\0' }, | |
115 | { "consumerd64-libdir", required_argument, 0, '\0' }, | |
116 | { "daemonize", no_argument, 0, 'd' }, | |
117 | { "background", no_argument, 0, 'b' }, | |
118 | { "sig-parent", no_argument, 0, 'S' }, | |
119 | { "help", no_argument, 0, 'h' }, | |
120 | { "group", required_argument, 0, 'g' }, | |
121 | { "version", no_argument, 0, 'V' }, | |
122 | { "quiet", no_argument, 0, 'q' }, | |
123 | { "verbose", no_argument, 0, 'v' }, | |
124 | { "verbose-consumer", no_argument, 0, '\0' }, | |
125 | { "no-kernel", no_argument, 0, '\0' }, | |
126 | { "pidfile", required_argument, 0, 'p' }, | |
127 | { "agent-tcp-port", required_argument, 0, '\0' }, | |
128 | { "config", required_argument, 0, 'f' }, | |
129 | { "load", required_argument, 0, 'l' }, | |
130 | { "kmod-probes", required_argument, 0, '\0' }, | |
131 | { "extra-kmod-probes", required_argument, 0, '\0' }, | |
26296c48 JG |
132 | { NULL, 0, 0, 0 } |
133 | }; | |
134 | ||
135 | /* Command line options to ignore from configuration file */ | |
136 | static const char *config_ignore_options[] = { "help", "version", "config" }; | |
1d4b027a | 137 | |
099e26bd DG |
138 | /* |
139 | * This pipe is used to inform the thread managing application communication | |
140 | * that a command is queued and ready to be processed. | |
141 | */ | |
76d7553f | 142 | static int apps_cmd_pipe[2] = { -1, -1 }; |
53c51aaa | 143 | static int apps_cmd_notify_pipe[2] = { -1, -1 }; |
099e26bd | 144 | |
099e26bd DG |
145 | /* |
146 | * UST registration command queue. This queue is tied with a futex and uses a N | |
147 | * wakers / 1 waiter implemented and detailed in futex.c/.h | |
148 | * | |
b22c5da8 DG |
149 | * The thread_registration_apps and thread_dispatch_ust_registration uses this |
150 | * queue along with the wait/wake scheme. The thread_manage_apps receives down | |
151 | * the line new application socket and monitors it for any I/O error or clean | |
152 | * close that triggers an unregistration of the application. | |
099e26bd DG |
153 | */ |
154 | static struct ust_cmd_queue ust_cmd_queue; | |
155 | ||
97bc1426 | 156 | /* |
51bb4dbb | 157 | * Section name to look for in the daemon configuration file. |
97bc1426 | 158 | */ |
51bb4dbb | 159 | static const char * const config_section_name = "sessiond"; |
5e97de00 | 160 | |
51bb4dbb JG |
161 | /* Am I root or not. Set to 1 if the daemon is running as root */ |
162 | static int is_root; | |
5eb91c98 | 163 | |
099e26bd DG |
164 | /* |
165 | * Stop all threads by closing the thread quit pipe. | |
166 | */ | |
cf3af59e MD |
167 | static void stop_threads(void) |
168 | { | |
5eb91c98 DG |
169 | int ret; |
170 | ||
cf3af59e MD |
171 | /* Stopping all threads */ |
172 | DBG("Terminating all threads"); | |
51bb4dbb | 173 | ret = sessiond_notify_quit_pipe(); |
5eb91c98 DG |
174 | if (ret < 0) { |
175 | ERR("write error on thread quit pipe"); | |
176 | } | |
cf3af59e MD |
177 | } |
178 | ||
e975f9f8 DG |
179 | /* |
180 | * Close every consumer sockets. | |
181 | */ | |
182 | static void close_consumer_sockets(void) | |
183 | { | |
184 | int ret; | |
185 | ||
186 | if (kconsumer_data.err_sock >= 0) { | |
187 | ret = close(kconsumer_data.err_sock); | |
188 | if (ret < 0) { | |
189 | PERROR("kernel consumer err_sock close"); | |
190 | } | |
191 | } | |
192 | if (ustconsumer32_data.err_sock >= 0) { | |
193 | ret = close(ustconsumer32_data.err_sock); | |
194 | if (ret < 0) { | |
a76cbd9f | 195 | PERROR("UST consumerd32 err_sock close"); |
e975f9f8 DG |
196 | } |
197 | } | |
198 | if (ustconsumer64_data.err_sock >= 0) { | |
199 | ret = close(ustconsumer64_data.err_sock); | |
200 | if (ret < 0) { | |
a76cbd9f | 201 | PERROR("UST consumerd64 err_sock close"); |
e975f9f8 DG |
202 | } |
203 | } | |
204 | if (kconsumer_data.cmd_sock >= 0) { | |
205 | ret = close(kconsumer_data.cmd_sock); | |
206 | if (ret < 0) { | |
207 | PERROR("kernel consumer cmd_sock close"); | |
208 | } | |
209 | } | |
210 | if (ustconsumer32_data.cmd_sock >= 0) { | |
211 | ret = close(ustconsumer32_data.cmd_sock); | |
212 | if (ret < 0) { | |
a76cbd9f | 213 | PERROR("UST consumerd32 cmd_sock close"); |
e975f9f8 DG |
214 | } |
215 | } | |
216 | if (ustconsumer64_data.cmd_sock >= 0) { | |
217 | ret = close(ustconsumer64_data.cmd_sock); | |
218 | if (ret < 0) { | |
a76cbd9f | 219 | PERROR("UST consumerd64 cmd_sock close"); |
e975f9f8 DG |
220 | } |
221 | } | |
b3530820 JG |
222 | if (kconsumer_data.channel_monitor_pipe >= 0) { |
223 | ret = close(kconsumer_data.channel_monitor_pipe); | |
224 | if (ret < 0) { | |
225 | PERROR("kernel consumer channel monitor pipe close"); | |
226 | } | |
227 | } | |
228 | if (ustconsumer32_data.channel_monitor_pipe >= 0) { | |
229 | ret = close(ustconsumer32_data.channel_monitor_pipe); | |
230 | if (ret < 0) { | |
231 | PERROR("UST consumerd32 channel monitor pipe close"); | |
232 | } | |
233 | } | |
234 | if (ustconsumer64_data.channel_monitor_pipe >= 0) { | |
235 | ret = close(ustconsumer64_data.channel_monitor_pipe); | |
236 | if (ret < 0) { | |
237 | PERROR("UST consumerd64 channel monitor pipe close"); | |
238 | } | |
239 | } | |
e975f9f8 DG |
240 | } |
241 | ||
4e4714cb JR |
242 | /* |
243 | * Wait on consumer process termination. | |
244 | * | |
245 | * Need to be called with the consumer data lock held or from a context | |
246 | * ensuring no concurrent access to data (e.g: cleanup). | |
247 | */ | |
248 | static void wait_consumer(struct consumer_data *consumer_data) | |
249 | { | |
250 | pid_t ret; | |
251 | int status; | |
252 | ||
253 | if (consumer_data->pid <= 0) { | |
254 | return; | |
255 | } | |
256 | ||
257 | DBG("Waiting for complete teardown of consumerd (PID: %d)", | |
258 | consumer_data->pid); | |
259 | ret = waitpid(consumer_data->pid, &status, 0); | |
260 | if (ret == -1) { | |
261 | PERROR("consumerd waitpid pid: %d", consumer_data->pid) | |
1640c24c | 262 | } else if (!WIFEXITED(status)) { |
4e4714cb JR |
263 | ERR("consumerd termination with error: %d", |
264 | WEXITSTATUS(ret)); | |
265 | } | |
266 | consumer_data->pid = 0; | |
267 | } | |
268 | ||
fac6795d | 269 | /* |
4a15001e | 270 | * Cleanup the session daemon's data structures. |
fac6795d | 271 | */ |
4a15001e | 272 | static void sessiond_cleanup(void) |
fac6795d | 273 | { |
ef599319 | 274 | int ret; |
48a86f68 | 275 | struct ltt_session_list *session_list = session_get_list(); |
fac6795d | 276 | |
4a15001e | 277 | DBG("Cleanup sessiond"); |
e07ae692 | 278 | |
4e449f3f MD |
279 | /* |
280 | * Close the thread quit pipe. It has already done its job, | |
281 | * since we are now called. | |
282 | */ | |
51bb4dbb | 283 | sessiond_close_quit_pipe(); |
53c51aaa JG |
284 | utils_close_pipe(apps_cmd_pipe); |
285 | utils_close_pipe(apps_cmd_notify_pipe); | |
45f6525f | 286 | utils_close_pipe(kernel_poll_pipe); |
2f77fc4b | 287 | |
c9a2957d JG |
288 | ret = remove(config.pid_file_path.value); |
289 | if (ret < 0) { | |
290 | PERROR("remove pidfile %s", config.pid_file_path.value); | |
35f90c40 DG |
291 | } |
292 | ||
e6142f2e JG |
293 | DBG("Removing sessiond and consumerd content of directory %s", |
294 | config.rundir.value); | |
8c6c56c2 MD |
295 | |
296 | /* sessiond */ | |
e6142f2e JG |
297 | DBG("Removing %s", config.pid_file_path.value); |
298 | (void) unlink(config.pid_file_path.value); | |
8c6c56c2 | 299 | |
e6142f2e JG |
300 | DBG("Removing %s", config.agent_port_file_path.value); |
301 | (void) unlink(config.agent_port_file_path.value); | |
cd9290dd | 302 | |
8c6c56c2 | 303 | /* kconsumerd */ |
e6142f2e JG |
304 | DBG("Removing %s", kconsumer_data.err_unix_sock_path); |
305 | (void) unlink(kconsumer_data.err_unix_sock_path); | |
306 | ||
307 | DBG("Removing directory %s", config.kconsumerd_path.value); | |
308 | (void) rmdir(config.kconsumerd_path.value); | |
8c6c56c2 MD |
309 | |
310 | /* ust consumerd 32 */ | |
e6142f2e JG |
311 | DBG("Removing %s", config.consumerd32_err_unix_sock_path.value); |
312 | (void) unlink(config.consumerd32_err_unix_sock_path.value); | |
313 | ||
314 | DBG("Removing directory %s", config.consumerd32_path.value); | |
315 | (void) rmdir(config.consumerd32_path.value); | |
8c6c56c2 MD |
316 | |
317 | /* ust consumerd 64 */ | |
e6142f2e JG |
318 | DBG("Removing %s", config.consumerd64_err_unix_sock_path.value); |
319 | (void) unlink(config.consumerd64_err_unix_sock_path.value); | |
320 | ||
321 | DBG("Removing directory %s", config.consumerd64_path.value); | |
322 | (void) rmdir(config.consumerd64_path.value); | |
5461b305 | 323 | |
6af972a2 | 324 | pthread_mutex_destroy(&session_list->lock); |
273ea72c | 325 | |
6a4e4039 JG |
326 | DBG("Cleaning up all agent apps"); |
327 | agent_app_ht_clean(); | |
099e26bd | 328 | DBG("Closing all UST sockets"); |
56fff090 | 329 | ust_app_clean_list(); |
7972aab2 | 330 | buffer_reg_destroy_registries(); |
099e26bd | 331 | |
caf7f341 MD |
332 | close_consumer_sockets(); |
333 | ||
334 | wait_consumer(&kconsumer_data); | |
335 | wait_consumer(&ustconsumer64_data); | |
336 | wait_consumer(&ustconsumer32_data); | |
337 | ||
e6142f2e | 338 | if (is_root && !config.no_kernel) { |
32e10baa | 339 | cleanup_kernel_tracer(); |
2f50c8a3 | 340 | } |
2f77fc4b | 341 | |
c9cb3e7d JG |
342 | /* |
343 | * We do NOT rmdir rundir because there are other processes | |
344 | * using it, for instance lttng-relayd, which can start in | |
345 | * parallel with this teardown. | |
346 | */ | |
4a15001e MD |
347 | } |
348 | ||
349 | /* | |
350 | * Cleanup the daemon's option data structures. | |
351 | */ | |
352 | static void sessiond_cleanup_options(void) | |
353 | { | |
354 | DBG("Cleaning up options"); | |
355 | ||
e6142f2e | 356 | sessiond_config_fini(&config); |
c9cb3e7d | 357 | |
7567352f | 358 | run_as_destroy_worker(); |
fac6795d DG |
359 | } |
360 | ||
050e613c | 361 | static int string_match(const char *str1, const char *str2) |
54d01ffb | 362 | { |
050e613c | 363 | return (str1 && str2) && !strcmp(str1, str2); |
54d01ffb DG |
364 | } |
365 | ||
00e2e675 | 366 | /* |
050e613c JG |
367 | * Take an option from the getopt output and set it in the right variable to be |
368 | * used later. | |
36b588ed | 369 | * |
050e613c | 370 | * Return 0 on success else a negative value. |
00e2e675 | 371 | */ |
050e613c | 372 | static int set_option(int opt, const char *arg, const char *optname) |
00e2e675 | 373 | { |
050e613c | 374 | int ret = 0; |
fac6795d | 375 | |
c0232ea5 | 376 | if (string_match(optname, "client-sock") || opt == 'c') { |
66b2ce8e JG |
377 | if (!arg || *arg == '\0') { |
378 | ret = -EINVAL; | |
379 | goto end; | |
380 | } | |
e8fa9fb0 MD |
381 | if (lttng_is_setuid_setgid()) { |
382 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
383 | "-c, --client-sock"); | |
384 | } else { | |
e6142f2e JG |
385 | config_string_set(&config.client_unix_sock_path, |
386 | strdup(arg)); | |
387 | if (!config.client_unix_sock_path.value) { | |
388 | ret = -ENOMEM; | |
389 | PERROR("strdup"); | |
390 | } | |
e8fa9fb0 | 391 | } |
c0232ea5 | 392 | } else if (string_match(optname, "apps-sock") || opt == 'a') { |
66b2ce8e JG |
393 | if (!arg || *arg == '\0') { |
394 | ret = -EINVAL; | |
395 | goto end; | |
396 | } | |
e8fa9fb0 MD |
397 | if (lttng_is_setuid_setgid()) { |
398 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
399 | "-a, --apps-sock"); | |
400 | } else { | |
e6142f2e JG |
401 | config_string_set(&config.apps_unix_sock_path, |
402 | strdup(arg)); | |
403 | if (!config.apps_unix_sock_path.value) { | |
404 | ret = -ENOMEM; | |
405 | PERROR("strdup"); | |
406 | } | |
e8fa9fb0 | 407 | } |
c0232ea5 | 408 | } else if (string_match(optname, "daemonize") || opt == 'd') { |
e6142f2e | 409 | config.daemonize = true; |
c0232ea5 | 410 | } else if (string_match(optname, "background") || opt == 'b') { |
e6142f2e | 411 | config.background = true; |
c0232ea5 | 412 | } else if (string_match(optname, "group") || opt == 'g') { |
66b2ce8e JG |
413 | if (!arg || *arg == '\0') { |
414 | ret = -EINVAL; | |
415 | goto end; | |
416 | } | |
e8fa9fb0 MD |
417 | if (lttng_is_setuid_setgid()) { |
418 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
419 | "-g, --group"); | |
420 | } else { | |
e6142f2e JG |
421 | config_string_set(&config.tracing_group_name, |
422 | strdup(arg)); | |
423 | if (!config.tracing_group_name.value) { | |
e8fa9fb0 | 424 | ret = -ENOMEM; |
e6142f2e | 425 | PERROR("strdup"); |
e8fa9fb0 | 426 | } |
db322c4d | 427 | } |
c0232ea5 | 428 | } else if (string_match(optname, "help") || opt == 'h') { |
4fc83d94 | 429 | ret = utils_show_help(8, "lttng-sessiond", help_msg); |
8190767e | 430 | if (ret) { |
4fc83d94 | 431 | ERR("Cannot show --help for `lttng-sessiond`"); |
8190767e PP |
432 | perror("exec"); |
433 | } | |
434 | exit(ret ? EXIT_FAILURE : EXIT_SUCCESS); | |
c0232ea5 | 435 | } else if (string_match(optname, "version") || opt == 'V') { |
26296c48 JG |
436 | fprintf(stdout, "%s\n", VERSION); |
437 | exit(EXIT_SUCCESS); | |
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 | ||
792 | /* | |
793 | * daemon configuration loading and argument parsing | |
794 | */ | |
795 | static int set_options(int argc, char **argv) | |
796 | { | |
797 | int ret = 0, c = 0, option_index = 0; | |
798 | int orig_optopt = optopt, orig_optind = optind; | |
799 | char *optstring; | |
800 | const char *config_path = NULL; | |
801 | ||
802 | optstring = utils_generate_optstring(long_options, | |
803 | sizeof(long_options) / sizeof(struct option)); | |
804 | if (!optstring) { | |
805 | ret = -ENOMEM; | |
806 | goto end; | |
807 | } | |
808 | ||
809 | /* Check for the --config option */ | |
810 | while ((c = getopt_long(argc, argv, optstring, long_options, | |
811 | &option_index)) != -1) { | |
812 | if (c == '?') { | |
813 | ret = -EINVAL; | |
814 | goto end; | |
815 | } else if (c != 'f') { | |
816 | /* if not equal to --config option. */ | |
817 | continue; | |
818 | } | |
819 | ||
e8fa9fb0 MD |
820 | if (lttng_is_setuid_setgid()) { |
821 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
822 | "-f, --config"); | |
823 | } else { | |
824 | config_path = utils_expand_path(optarg); | |
825 | if (!config_path) { | |
826 | ERR("Failed to resolve path: %s", optarg); | |
827 | } | |
26296c48 JG |
828 | } |
829 | } | |
830 | ||
831 | ret = config_get_section_entries(config_path, config_section_name, | |
832 | config_entry_handler, NULL); | |
833 | if (ret) { | |
834 | if (ret > 0) { | |
835 | ERR("Invalid configuration option at line %i", ret); | |
836 | ret = -1; | |
837 | } | |
838 | goto end; | |
839 | } | |
840 | ||
841 | /* Reset getopt's global state */ | |
842 | optopt = orig_optopt; | |
843 | optind = orig_optind; | |
844 | while (1) { | |
c0232ea5 JG |
845 | option_index = -1; |
846 | /* | |
847 | * getopt_long() will not set option_index if it encounters a | |
848 | * short option. | |
849 | */ | |
850 | c = getopt_long(argc, argv, optstring, long_options, | |
851 | &option_index); | |
26296c48 | 852 | if (c == -1) { |
4d076222 DG |
853 | break; |
854 | } | |
26296c48 | 855 | |
c0232ea5 JG |
856 | /* |
857 | * Pass NULL as the long option name if popt left the index | |
858 | * unset. | |
859 | */ | |
860 | ret = set_option(c, optarg, | |
861 | option_index < 0 ? NULL : | |
862 | long_options[option_index].name); | |
26296c48 JG |
863 | if (ret < 0) { |
864 | break; | |
fac6795d DG |
865 | } |
866 | } | |
867 | ||
26296c48 JG |
868 | end: |
869 | free(optstring); | |
870 | return ret; | |
fac6795d DG |
871 | } |
872 | ||
f472090a JG |
873 | /* |
874 | * Create lockfile using the rundir and return its fd. | |
875 | */ | |
876 | static int create_lockfile(void) | |
877 | { | |
878 | return utils_create_lock_file(config.lock_file_path.value); | |
879 | } | |
880 | ||
fac6795d | 881 | /* |
54d01ffb DG |
882 | * Check if the global socket is available, and if a daemon is answering at the |
883 | * other side. If yes, error is returned. | |
f472090a JG |
884 | * |
885 | * Also attempts to create and hold the lock file. | |
fac6795d | 886 | */ |
cf3af59e | 887 | static int check_existing_daemon(void) |
fac6795d | 888 | { |
f472090a JG |
889 | int ret = 0; |
890 | ||
7d8234d9 | 891 | /* Is there anybody out there ? */ |
099e26bd | 892 | if (lttng_session_daemon_alive()) { |
f472090a JG |
893 | ret = -EEXIST; |
894 | goto end; | |
099e26bd | 895 | } |
b09c7c76 | 896 | |
f472090a JG |
897 | lockfile_fd = create_lockfile(); |
898 | if (lockfile_fd < 0) { | |
899 | ret = -EEXIST; | |
900 | goto end; | |
901 | } | |
902 | end: | |
903 | return ret; | |
904 | } | |
905 | ||
906 | static void sessiond_cleanup_lock_file(void) | |
907 | { | |
908 | int ret; | |
909 | ||
910 | /* | |
911 | * Cleanup lock file by deleting it and finaly closing it which will | |
912 | * release the file system lock. | |
913 | */ | |
914 | if (lockfile_fd >= 0) { | |
915 | ret = remove(config.lock_file_path.value); | |
916 | if (ret < 0) { | |
917 | PERROR("remove lock file"); | |
918 | } | |
919 | ret = close(lockfile_fd); | |
920 | if (ret < 0) { | |
921 | PERROR("close lock file"); | |
922 | } | |
923 | } | |
fac6795d DG |
924 | } |
925 | ||
fac6795d | 926 | /* |
d063d709 | 927 | * Set the tracing group gid onto the client socket. |
5e16da05 | 928 | * |
d063d709 | 929 | * Race window between mkdir and chown is OK because we are going from more |
d1613cf5 | 930 | * permissive (root.root) to less permissive (root.tracing). |
fac6795d | 931 | */ |
be040666 | 932 | static int set_permissions(char *rundir) |
fac6795d DG |
933 | { |
934 | int ret; | |
996b65c8 | 935 | gid_t gid; |
fac6795d | 936 | |
6710e5aa JR |
937 | ret = utils_get_group_id(config.tracing_group_name.value, true, &gid); |
938 | if (ret) { | |
939 | /* Default to root group. */ | |
940 | gid = 0; | |
941 | } | |
fac6795d | 942 | |
d6f42150 | 943 | /* Set lttng run dir */ |
be040666 | 944 | ret = chown(rundir, 0, gid); |
d6f42150 | 945 | if (ret < 0) { |
be040666 | 946 | ERR("Unable to set group on %s", rundir); |
76d7553f | 947 | PERROR("chown"); |
d6f42150 DG |
948 | } |
949 | ||
6c71277b MD |
950 | /* |
951 | * Ensure all applications and tracing group can search the run | |
952 | * dir. Allow everyone to read the directory, since it does not | |
953 | * buy us anything to hide its content. | |
954 | */ | |
955 | ret = chmod(rundir, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); | |
d1613cf5 JN |
956 | if (ret < 0) { |
957 | ERR("Unable to set permissions on %s", rundir); | |
76d7553f | 958 | PERROR("chmod"); |
d1613cf5 JN |
959 | } |
960 | ||
d6f42150 | 961 | /* lttng client socket path */ |
e6142f2e | 962 | ret = chown(config.client_unix_sock_path.value, 0, gid); |
fac6795d | 963 | if (ret < 0) { |
e6142f2e | 964 | ERR("Unable to set group on %s", config.client_unix_sock_path.value); |
76d7553f | 965 | PERROR("chown"); |
d6f42150 DG |
966 | } |
967 | ||
3bd1e081 | 968 | /* kconsumer error socket path */ |
6c71277b | 969 | ret = chown(kconsumer_data.err_unix_sock_path, 0, 0); |
d6f42150 | 970 | if (ret < 0) { |
3bd1e081 | 971 | ERR("Unable to set group on %s", kconsumer_data.err_unix_sock_path); |
76d7553f | 972 | PERROR("chown"); |
3bd1e081 MD |
973 | } |
974 | ||
7753dea8 | 975 | /* 64-bit ustconsumer error socket path */ |
6c71277b | 976 | ret = chown(ustconsumer64_data.err_unix_sock_path, 0, 0); |
7753dea8 MD |
977 | if (ret < 0) { |
978 | ERR("Unable to set group on %s", ustconsumer64_data.err_unix_sock_path); | |
76d7553f | 979 | PERROR("chown"); |
7753dea8 MD |
980 | } |
981 | ||
982 | /* 32-bit ustconsumer compat32 error socket path */ | |
6c71277b | 983 | ret = chown(ustconsumer32_data.err_unix_sock_path, 0, 0); |
3bd1e081 | 984 | if (ret < 0) { |
7753dea8 | 985 | ERR("Unable to set group on %s", ustconsumer32_data.err_unix_sock_path); |
76d7553f | 986 | PERROR("chown"); |
fac6795d DG |
987 | } |
988 | ||
d6f42150 | 989 | DBG("All permissions are set"); |
e07ae692 | 990 | |
fac6795d DG |
991 | return ret; |
992 | } | |
993 | ||
d6f42150 | 994 | /* |
d063d709 | 995 | * Create the lttng run directory needed for all global sockets and pipe. |
d6f42150 | 996 | */ |
e6142f2e | 997 | static int create_lttng_rundir(void) |
d6f42150 DG |
998 | { |
999 | int ret; | |
1000 | ||
e6142f2e | 1001 | DBG3("Creating LTTng run directory: %s", config.rundir.value); |
67e40797 | 1002 | |
e6142f2e | 1003 | ret = mkdir(config.rundir.value, S_IRWXU); |
d6f42150 | 1004 | if (ret < 0) { |
b1f11e69 | 1005 | if (errno != EEXIST) { |
e6142f2e | 1006 | ERR("Unable to create %s", config.rundir.value); |
b1f11e69 DG |
1007 | goto error; |
1008 | } else { | |
1009 | ret = 0; | |
1010 | } | |
d6f42150 DG |
1011 | } |
1012 | ||
1013 | error: | |
1014 | return ret; | |
1015 | } | |
1016 | ||
1017 | /* | |
e6142f2e | 1018 | * Setup sockets and directory needed by the consumerds' communication with the |
d063d709 | 1019 | * session daemon. |
d6f42150 | 1020 | */ |
e6142f2e | 1021 | static int set_consumer_sockets(struct consumer_data *consumer_data) |
d6f42150 DG |
1022 | { |
1023 | int ret; | |
e6142f2e | 1024 | char *path = NULL; |
d6f42150 | 1025 | |
6c71277b | 1026 | switch (consumer_data->type) { |
7753dea8 | 1027 | case LTTNG_CONSUMER_KERNEL: |
e6142f2e | 1028 | path = config.kconsumerd_path.value; |
7753dea8 MD |
1029 | break; |
1030 | case LTTNG_CONSUMER64_UST: | |
e6142f2e | 1031 | path = config.consumerd64_path.value; |
7753dea8 MD |
1032 | break; |
1033 | case LTTNG_CONSUMER32_UST: | |
e6142f2e | 1034 | path = config.consumerd32_path.value; |
7753dea8 MD |
1035 | break; |
1036 | default: | |
1037 | ERR("Consumer type unknown"); | |
1038 | ret = -EINVAL; | |
1039 | goto error; | |
d6f42150 | 1040 | } |
e6142f2e | 1041 | assert(path); |
d6f42150 | 1042 | |
67e40797 DG |
1043 | DBG2("Creating consumer directory: %s", path); |
1044 | ||
6c71277b | 1045 | ret = mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP); |
e7e1ef17 JR |
1046 | if (ret < 0 && errno != EEXIST) { |
1047 | PERROR("mkdir"); | |
1048 | ERR("Failed to create %s", path); | |
1049 | goto error; | |
d6f42150 | 1050 | } |
6c71277b | 1051 | if (is_root) { |
6710e5aa JR |
1052 | gid_t gid; |
1053 | ||
1054 | ret = utils_get_group_id(config.tracing_group_name.value, true, | |
1055 | &gid); | |
1056 | if (ret) { | |
1057 | /* Default to root group. */ | |
1058 | gid = 0; | |
1059 | } | |
1060 | ||
1061 | ret = chown(path, 0, gid); | |
6c71277b MD |
1062 | if (ret < 0) { |
1063 | ERR("Unable to set group on %s", path); | |
1064 | PERROR("chown"); | |
1065 | goto error; | |
1066 | } | |
1067 | } | |
d6f42150 | 1068 | |
e6142f2e | 1069 | /* Create the consumerd error unix socket */ |
3bd1e081 MD |
1070 | consumer_data->err_sock = |
1071 | lttcomm_create_unix_sock(consumer_data->err_unix_sock_path); | |
1072 | if (consumer_data->err_sock < 0) { | |
1073 | ERR("Create unix sock failed: %s", consumer_data->err_unix_sock_path); | |
d6f42150 DG |
1074 | ret = -1; |
1075 | goto error; | |
1076 | } | |
1077 | ||
a24f05ab MD |
1078 | /* |
1079 | * Set the CLOEXEC flag. Return code is useless because either way, the | |
1080 | * show must go on. | |
1081 | */ | |
1082 | ret = utils_set_fd_cloexec(consumer_data->err_sock); | |
1083 | if (ret < 0) { | |
1084 | PERROR("utils_set_fd_cloexec"); | |
1085 | /* continue anyway */ | |
1086 | } | |
1087 | ||
d6f42150 | 1088 | /* File permission MUST be 660 */ |
3bd1e081 | 1089 | ret = chmod(consumer_data->err_unix_sock_path, |
54d01ffb | 1090 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); |
d6f42150 | 1091 | if (ret < 0) { |
3bd1e081 | 1092 | ERR("Set file permissions failed: %s", consumer_data->err_unix_sock_path); |
67e40797 | 1093 | PERROR("chmod"); |
d6f42150 DG |
1094 | goto error; |
1095 | } | |
1096 | ||
1097 | error: | |
1098 | return ret; | |
1099 | } | |
1100 | ||
fac6795d | 1101 | /* |
d063d709 | 1102 | * Signal handler for the daemon |
cf3af59e | 1103 | * |
54d01ffb DG |
1104 | * Simply stop all worker threads, leaving main() return gracefully after |
1105 | * joining all threads and calling cleanup(). | |
fac6795d DG |
1106 | */ |
1107 | static void sighandler(int sig) | |
1108 | { | |
1109 | switch (sig) { | |
cf3af59e | 1110 | case SIGINT: |
af87c45a | 1111 | DBG("SIGINT caught"); |
cf3af59e MD |
1112 | stop_threads(); |
1113 | break; | |
1114 | case SIGTERM: | |
af87c45a | 1115 | DBG("SIGTERM caught"); |
cf3af59e MD |
1116 | stop_threads(); |
1117 | break; | |
0bb7724a DG |
1118 | case SIGUSR1: |
1119 | CMM_STORE_SHARED(recv_child_signal, 1); | |
1120 | break; | |
cf3af59e MD |
1121 | default: |
1122 | break; | |
fac6795d | 1123 | } |
fac6795d DG |
1124 | } |
1125 | ||
1126 | /* | |
d063d709 | 1127 | * Setup signal handler for : |
1d4b027a | 1128 | * SIGINT, SIGTERM, SIGPIPE |
fac6795d | 1129 | */ |
1d4b027a | 1130 | static int set_signal_handler(void) |
fac6795d | 1131 | { |
1d4b027a DG |
1132 | int ret = 0; |
1133 | struct sigaction sa; | |
1134 | sigset_t sigset; | |
fac6795d | 1135 | |
1d4b027a | 1136 | if ((ret = sigemptyset(&sigset)) < 0) { |
76d7553f | 1137 | PERROR("sigemptyset"); |
1d4b027a DG |
1138 | return ret; |
1139 | } | |
d6f42150 | 1140 | |
1d4b027a DG |
1141 | sa.sa_mask = sigset; |
1142 | sa.sa_flags = 0; | |
0072e5e2 MD |
1143 | |
1144 | sa.sa_handler = sighandler; | |
1d4b027a | 1145 | if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) { |
76d7553f | 1146 | PERROR("sigaction"); |
1d4b027a | 1147 | return ret; |
d6f42150 DG |
1148 | } |
1149 | ||
1d4b027a | 1150 | if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) { |
76d7553f | 1151 | PERROR("sigaction"); |
1d4b027a | 1152 | return ret; |
d6f42150 | 1153 | } |
aaf26714 | 1154 | |
0072e5e2 | 1155 | if ((ret = sigaction(SIGUSR1, &sa, NULL)) < 0) { |
76d7553f | 1156 | PERROR("sigaction"); |
1d4b027a | 1157 | return ret; |
8c0faa1d DG |
1158 | } |
1159 | ||
0072e5e2 MD |
1160 | sa.sa_handler = SIG_IGN; |
1161 | if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) { | |
0bb7724a DG |
1162 | PERROR("sigaction"); |
1163 | return ret; | |
1164 | } | |
1165 | ||
1166 | DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT"); | |
1d4b027a DG |
1167 | |
1168 | return ret; | |
fac6795d DG |
1169 | } |
1170 | ||
f3ed775e | 1171 | /* |
d063d709 | 1172 | * Set open files limit to unlimited. This daemon can open a large number of |
514bb9f0 | 1173 | * file descriptors in order to consume multiple kernel traces. |
f3ed775e DG |
1174 | */ |
1175 | static void set_ulimit(void) | |
1176 | { | |
1177 | int ret; | |
1178 | struct rlimit lim; | |
1179 | ||
514bb9f0 | 1180 | /* The kernel does not allow an infinite limit for open files */ |
f3ed775e DG |
1181 | lim.rlim_cur = 65535; |
1182 | lim.rlim_max = 65535; | |
1183 | ||
1184 | ret = setrlimit(RLIMIT_NOFILE, &lim); | |
1185 | if (ret < 0) { | |
76d7553f | 1186 | PERROR("failed to set open files limit"); |
f3ed775e DG |
1187 | } |
1188 | } | |
1189 | ||
4a15001e | 1190 | static int write_pidfile(void) |
35f90c40 | 1191 | { |
e6142f2e | 1192 | return utils_create_pid_file(getpid(), config.pid_file_path.value); |
35f90c40 DG |
1193 | } |
1194 | ||
e6142f2e JG |
1195 | static int set_clock_plugin_env(void) |
1196 | { | |
1197 | int ret = 0; | |
1198 | char *env_value = NULL; | |
cd9290dd | 1199 | |
e6142f2e JG |
1200 | if (!config.lttng_ust_clock_plugin.value) { |
1201 | goto end; | |
1202 | } | |
1203 | ||
1204 | ret = asprintf(&env_value, "LTTNG_UST_CLOCK_PLUGIN=%s", | |
1205 | config.lttng_ust_clock_plugin.value); | |
cd9290dd | 1206 | if (ret < 0) { |
e6142f2e JG |
1207 | PERROR("asprintf"); |
1208 | goto end; | |
cd9290dd DG |
1209 | } |
1210 | ||
e6142f2e JG |
1211 | ret = putenv(env_value); |
1212 | if (ret) { | |
1213 | free(env_value); | |
1214 | PERROR("putenv of LTTNG_UST_CLOCK_PLUGIN"); | |
1215 | goto end; | |
1216 | } | |
cd9290dd | 1217 | |
e6142f2e JG |
1218 | DBG("Updated LTTNG_UST_CLOCK_PLUGIN environment variable to \"%s\"", |
1219 | config.lttng_ust_clock_plugin.value); | |
1220 | end: | |
4a15001e | 1221 | return ret; |
ef367a93 JG |
1222 | } |
1223 | ||
6af972a2 JG |
1224 | static void destroy_all_sessions_and_wait(void) |
1225 | { | |
1226 | struct ltt_session *session, *tmp; | |
1227 | struct ltt_session_list *session_list; | |
1228 | ||
1229 | session_list = session_get_list(); | |
1230 | DBG("Initiating destruction of all sessions"); | |
1231 | ||
1232 | if (!session_list) { | |
1233 | return; | |
1234 | } | |
1235 | ||
6af972a2 JG |
1236 | session_lock_list(); |
1237 | /* Initiate the destruction of all sessions. */ | |
1238 | cds_list_for_each_entry_safe(session, tmp, | |
1239 | &session_list->head, list) { | |
1240 | if (!session_get(session)) { | |
1241 | continue; | |
1242 | } | |
1243 | ||
1244 | session_lock(session); | |
1245 | if (session->destroyed) { | |
1246 | goto unlock_session; | |
1247 | } | |
293d47db | 1248 | (void) cmd_stop_trace(session); |
823a1989 JG |
1249 | (void) cmd_destroy_session(session, notification_thread_handle, |
1250 | NULL); | |
6af972a2 JG |
1251 | unlock_session: |
1252 | session_unlock(session); | |
1253 | session_put(session); | |
1254 | } | |
1255 | session_unlock_list(); | |
1256 | ||
1257 | /* Wait for the destruction of all sessions to complete. */ | |
1258 | DBG("Waiting for the destruction of all sessions to complete"); | |
1259 | session_list_wait_empty(); | |
1260 | DBG("Destruction of all sessions completed"); | |
1261 | } | |
1262 | ||
5bedd986 JG |
1263 | static int run_as_worker_post_fork_cleanup(void *data) |
1264 | { | |
1265 | struct sessiond_config *sessiond_config = data; | |
1266 | ||
1267 | sessiond_config_fini(sessiond_config); | |
1268 | return 0; | |
1269 | } | |
1270 | ||
1271 | static int launch_run_as_worker(const char *procname) | |
1272 | { | |
1273 | /* | |
1274 | * Clean-up before forking the run-as worker. Any dynamically | |
1275 | * allocated memory of which the worker is not aware will | |
1276 | * be leaked as the process forks a run-as worker (and performs | |
1277 | * no exec*()). The same would apply to any opened fd. | |
1278 | */ | |
1279 | return run_as_create_worker(procname, run_as_worker_post_fork_cleanup, | |
1280 | &config); | |
1281 | } | |
1282 | ||
3438f6f5 JG |
1283 | static void sessiond_uuid_log(void) |
1284 | { | |
1285 | char uuid_str[UUID_STR_LEN]; | |
1286 | ||
1287 | lttng_uuid_to_str(sessiond_uuid, uuid_str); | |
1288 | DBG("Starting lttng-sessiond {%s}", uuid_str); | |
1289 | } | |
1290 | ||
fac6795d DG |
1291 | /* |
1292 | * main | |
1293 | */ | |
1294 | int main(int argc, char **argv) | |
1295 | { | |
4a15001e | 1296 | int ret = 0, retval = 0; |
e6142f2e | 1297 | const char *env_app_timeout; |
b3530820 JG |
1298 | struct lttng_pipe *ust32_channel_monitor_pipe = NULL, |
1299 | *ust64_channel_monitor_pipe = NULL, | |
1300 | *kernel_channel_monitor_pipe = NULL; | |
548468b9 | 1301 | struct lttng_thread *ht_cleanup_thread = NULL; |
a5ab9e05 | 1302 | struct timer_thread_parameters timer_thread_parameters; |
a430759b JG |
1303 | /* Rotation thread handle. */ |
1304 | struct rotation_thread_handle *rotation_thread_handle = NULL; | |
d086f507 JD |
1305 | /* Queue of rotation jobs populated by the sessiond-timer. */ |
1306 | struct rotation_thread_timer_queue *rotation_timer_queue = NULL; | |
050e613c | 1307 | struct lttng_thread *client_thread = NULL; |
4de27698 | 1308 | struct lttng_thread *notification_thread = NULL; |
143745a6 | 1309 | struct lttng_thread *register_apps_thread = NULL; |
fac6795d | 1310 | |
335a95b7 MD |
1311 | init_kernel_workarounds(); |
1312 | ||
f6a9efaa DG |
1313 | rcu_register_thread(); |
1314 | ||
4a15001e MD |
1315 | if (set_signal_handler()) { |
1316 | retval = -1; | |
1317 | goto exit_set_signal_handler; | |
0bb7724a DG |
1318 | } |
1319 | ||
82528808 | 1320 | if (timer_signal_init()) { |
d086f507 JD |
1321 | retval = -1; |
1322 | goto exit_set_signal_handler; | |
1323 | } | |
1324 | ||
12744796 DG |
1325 | page_size = sysconf(_SC_PAGESIZE); |
1326 | if (page_size < 0) { | |
1327 | PERROR("sysconf _SC_PAGESIZE"); | |
1328 | page_size = LONG_MAX; | |
1329 | WARN("Fallback page size to %ld", page_size); | |
1330 | } | |
1331 | ||
e6142f2e JG |
1332 | ret = sessiond_config_init(&config); |
1333 | if (ret) { | |
1334 | retval = -1; | |
1335 | goto exit_set_signal_handler; | |
1336 | } | |
1337 | ||
2788b494 JR |
1338 | /* |
1339 | * Init config from environment variables. | |
1340 | * Command line option override env configuration per-doc. Do env first. | |
1341 | */ | |
1342 | sessiond_config_apply_env_config(&config); | |
1343 | ||
4a15001e MD |
1344 | /* |
1345 | * Parse arguments and load the daemon configuration file. | |
1346 | * | |
1347 | * We have an exit_options exit path to free memory reserved by | |
1348 | * set_options. This is needed because the rest of sessiond_cleanup() | |
1349 | * depends on ht_cleanup_thread, which depends on lttng_daemonize, which | |
1350 | * depends on set_options. | |
1351 | */ | |
fac6795d | 1352 | progname = argv[0]; |
4a15001e MD |
1353 | if (set_options(argc, argv)) { |
1354 | retval = -1; | |
1355 | goto exit_options; | |
fac6795d DG |
1356 | } |
1357 | ||
e6142f2e JG |
1358 | /* |
1359 | * Resolve all paths received as arguments, configuration option, or | |
1360 | * through environment variable as absolute paths. This is necessary | |
1361 | * since daemonizing causes the sessiond's current working directory | |
1362 | * to '/'. | |
1363 | */ | |
1364 | ret = sessiond_config_resolve_paths(&config); | |
1365 | if (ret) { | |
1366 | goto exit_options; | |
1367 | } | |
1368 | ||
1369 | /* Apply config. */ | |
1370 | lttng_opt_verbose = config.verbose; | |
1371 | lttng_opt_quiet = config.quiet; | |
1372 | kconsumer_data.err_unix_sock_path = | |
1373 | config.kconsumerd_err_unix_sock_path.value; | |
1374 | kconsumer_data.cmd_unix_sock_path = | |
1375 | config.kconsumerd_cmd_unix_sock_path.value; | |
1376 | ustconsumer32_data.err_unix_sock_path = | |
1377 | config.consumerd32_err_unix_sock_path.value; | |
1378 | ustconsumer32_data.cmd_unix_sock_path = | |
1379 | config.consumerd32_cmd_unix_sock_path.value; | |
1380 | ustconsumer64_data.err_unix_sock_path = | |
1381 | config.consumerd64_err_unix_sock_path.value; | |
1382 | ustconsumer64_data.cmd_unix_sock_path = | |
1383 | config.consumerd64_cmd_unix_sock_path.value; | |
1384 | set_clock_plugin_env(); | |
1385 | ||
1386 | sessiond_config_log(&config); | |
3438f6f5 | 1387 | sessiond_uuid_log(); |
e6142f2e | 1388 | |
f472090a JG |
1389 | if (create_lttng_rundir()) { |
1390 | retval = -1; | |
1391 | goto exit_options; | |
1392 | } | |
1393 | ||
1394 | /* Abort launch if a session daemon is already running. */ | |
1395 | if (check_existing_daemon()) { | |
1396 | ERR("A session daemon is already running."); | |
1397 | retval = -1; | |
1398 | goto exit_options; | |
1399 | } | |
1400 | ||
fac6795d | 1401 | /* Daemonize */ |
e6142f2e | 1402 | if (config.daemonize || config.background) { |
ceed52b5 MD |
1403 | int i; |
1404 | ||
72dd7491 | 1405 | ret = lttng_daemonize(&child_ppid, &recv_child_signal, |
e6142f2e | 1406 | !config.background); |
53094c05 | 1407 | if (ret < 0) { |
4a15001e MD |
1408 | retval = -1; |
1409 | goto exit_options; | |
53094c05 | 1410 | } |
0bb7724a | 1411 | |
ceed52b5 | 1412 | /* |
0bb7724a | 1413 | * We are in the child. Make sure all other file descriptors are |
4a15001e | 1414 | * closed, in case we are called with more opened file |
f472090a | 1415 | * descriptors than the standard ones and the lock file. |
ceed52b5 MD |
1416 | */ |
1417 | for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) { | |
f472090a JG |
1418 | if (i == lockfile_fd) { |
1419 | continue; | |
1420 | } | |
ceed52b5 MD |
1421 | (void) close(i); |
1422 | } | |
1423 | } | |
1424 | ||
5bedd986 | 1425 | if (launch_run_as_worker(argv[0]) < 0) { |
7567352f MD |
1426 | goto exit_create_run_as_worker_cleanup; |
1427 | } | |
1428 | ||
4a15001e MD |
1429 | /* |
1430 | * Starting from here, we can create threads. This needs to be after | |
1431 | * lttng_daemonize due to RCU. | |
1432 | */ | |
1433 | ||
1434 | /* | |
1435 | * Initialize the health check subsystem. This call should set the | |
1436 | * appropriate time values. | |
1437 | */ | |
1438 | health_sessiond = health_app_create(NR_HEALTH_SESSIOND_TYPES); | |
1439 | if (!health_sessiond) { | |
1440 | PERROR("health_app_create error"); | |
1441 | retval = -1; | |
813ef6cb | 1442 | goto stop_threads; |
4a15001e MD |
1443 | } |
1444 | ||
4a15001e | 1445 | /* Create thread to clean up RCU hash tables */ |
548468b9 JG |
1446 | ht_cleanup_thread = launch_ht_cleanup_thread(); |
1447 | if (!ht_cleanup_thread) { | |
4a15001e | 1448 | retval = -1; |
813ef6cb | 1449 | goto stop_threads; |
4a15001e MD |
1450 | } |
1451 | ||
ceed52b5 | 1452 | /* Create thread quit pipe */ |
51bb4dbb | 1453 | if (sessiond_init_thread_quit_pipe()) { |
4a15001e | 1454 | retval = -1; |
813ef6cb | 1455 | goto stop_threads; |
fac6795d DG |
1456 | } |
1457 | ||
1458 | /* Check if daemon is UID = 0 */ | |
1459 | is_root = !getuid(); | |
fac6795d | 1460 | if (is_root) { |
67e40797 | 1461 | /* Create global run dir with root access */ |
67e40797 | 1462 | |
b3530820 JG |
1463 | kernel_channel_monitor_pipe = lttng_pipe_open(0); |
1464 | if (!kernel_channel_monitor_pipe) { | |
1465 | ERR("Failed to create kernel consumer channel monitor pipe"); | |
1466 | retval = -1; | |
813ef6cb | 1467 | goto stop_threads; |
b3530820 JG |
1468 | } |
1469 | kconsumer_data.channel_monitor_pipe = | |
1470 | lttng_pipe_release_writefd( | |
1471 | kernel_channel_monitor_pipe); | |
1472 | if (kconsumer_data.channel_monitor_pipe < 0) { | |
1473 | retval = -1; | |
813ef6cb | 1474 | goto stop_threads; |
b3530820 | 1475 | } |
fac6795d DG |
1476 | } |
1477 | ||
5c827ce0 DG |
1478 | /* Set consumer initial state */ |
1479 | kernel_consumerd_state = CONSUMER_STOPPED; | |
1480 | ust_consumerd_state = CONSUMER_STOPPED; | |
1481 | ||
b3530820 JG |
1482 | ust32_channel_monitor_pipe = lttng_pipe_open(0); |
1483 | if (!ust32_channel_monitor_pipe) { | |
1484 | ERR("Failed to create 32-bit user space consumer channel monitor pipe"); | |
1485 | retval = -1; | |
813ef6cb | 1486 | goto stop_threads; |
b3530820 JG |
1487 | } |
1488 | ustconsumer32_data.channel_monitor_pipe = lttng_pipe_release_writefd( | |
1489 | ust32_channel_monitor_pipe); | |
1490 | if (ustconsumer32_data.channel_monitor_pipe < 0) { | |
1491 | retval = -1; | |
813ef6cb | 1492 | goto stop_threads; |
b3530820 | 1493 | } |
62c43103 | 1494 | |
d086f507 | 1495 | /* |
82528808 JG |
1496 | * The rotation_thread_timer_queue structure is shared between the |
1497 | * sessiond timer thread and the rotation thread. The main thread keeps | |
1498 | * its ownership and destroys it when both threads have been joined. | |
d086f507 | 1499 | */ |
82528808 | 1500 | rotation_timer_queue = rotation_thread_timer_queue_create(); |
d086f507 JD |
1501 | if (!rotation_timer_queue) { |
1502 | retval = -1; | |
813ef6cb | 1503 | goto stop_threads; |
d086f507 | 1504 | } |
a5ab9e05 JG |
1505 | timer_thread_parameters.rotation_thread_job_queue = |
1506 | rotation_timer_queue; | |
67e40797 | 1507 | |
b3530820 JG |
1508 | ust64_channel_monitor_pipe = lttng_pipe_open(0); |
1509 | if (!ust64_channel_monitor_pipe) { | |
1510 | ERR("Failed to create 64-bit user space consumer channel monitor pipe"); | |
1511 | retval = -1; | |
813ef6cb | 1512 | goto stop_threads; |
b3530820 JG |
1513 | } |
1514 | ustconsumer64_data.channel_monitor_pipe = lttng_pipe_release_writefd( | |
1515 | ust64_channel_monitor_pipe); | |
1516 | if (ustconsumer64_data.channel_monitor_pipe < 0) { | |
1517 | retval = -1; | |
813ef6cb | 1518 | goto stop_threads; |
b3530820 | 1519 | } |
847177cd | 1520 | |
1427f9b2 DG |
1521 | /* |
1522 | * Init UST app hash table. Alloc hash table before this point since | |
1523 | * cleanup() can get called after that point. | |
1524 | */ | |
4a15001e | 1525 | if (ust_app_ht_alloc()) { |
ddbeb0f6 | 1526 | ERR("Failed to allocate UST app hash table"); |
4a15001e | 1527 | retval = -1; |
813ef6cb | 1528 | goto stop_threads; |
4a15001e | 1529 | } |
1427f9b2 | 1530 | |
6a4e4039 JG |
1531 | /* |
1532 | * Initialize agent app hash table. We allocate the hash table here | |
1533 | * since cleanup() can get called after this point. | |
1534 | */ | |
1535 | if (agent_app_ht_alloc()) { | |
1536 | ERR("Failed to allocate Agent app hash table"); | |
4a15001e | 1537 | retval = -1; |
813ef6cb | 1538 | goto stop_threads; |
f20baf8e DG |
1539 | } |
1540 | ||
a88df331 DG |
1541 | /* |
1542 | * These actions must be executed as root. We do that *after* setting up | |
1543 | * the sockets path because we MUST make the check for another daemon using | |
1544 | * those paths *before* trying to set the kernel consumer sockets and init | |
1545 | * kernel tracer. | |
1546 | */ | |
1547 | if (is_root) { | |
e6142f2e | 1548 | if (set_consumer_sockets(&kconsumer_data)) { |
4a15001e | 1549 | retval = -1; |
813ef6cb | 1550 | goto stop_threads; |
7753dea8 MD |
1551 | } |
1552 | ||
a88df331 | 1553 | /* Setup kernel tracer */ |
e6142f2e | 1554 | if (!config.no_kernel) { |
4fba7219 DG |
1555 | init_kernel_tracer(); |
1556 | } | |
a88df331 DG |
1557 | |
1558 | /* Set ulimit for open files */ | |
1559 | set_ulimit(); | |
fac6795d | 1560 | } |
4063050c MD |
1561 | /* init lttng_fd tracking must be done after set_ulimit. */ |
1562 | lttng_fd_init(); | |
fac6795d | 1563 | |
e6142f2e | 1564 | if (set_consumer_sockets(&ustconsumer64_data)) { |
4a15001e | 1565 | retval = -1; |
813ef6cb | 1566 | goto stop_threads; |
67e40797 DG |
1567 | } |
1568 | ||
e6142f2e | 1569 | if (set_consumer_sockets(&ustconsumer32_data)) { |
4a15001e | 1570 | retval = -1; |
813ef6cb | 1571 | goto stop_threads; |
67e40797 DG |
1572 | } |
1573 | ||
5b8719f5 | 1574 | /* Get parent pid if -S, --sig-parent is specified. */ |
e6142f2e | 1575 | if (config.sig_parent) { |
5b8719f5 DG |
1576 | ppid = getppid(); |
1577 | } | |
1578 | ||
7a485870 | 1579 | /* Setup the kernel pipe for waking up the kernel thread */ |
e6142f2e | 1580 | if (is_root && !config.no_kernel) { |
4a15001e MD |
1581 | if (utils_create_pipe_cloexec(kernel_poll_pipe)) { |
1582 | retval = -1; | |
813ef6cb | 1583 | goto stop_threads; |
6620da75 | 1584 | } |
7a485870 DG |
1585 | } |
1586 | ||
099e26bd | 1587 | /* Setup the thread apps communication pipe. */ |
4a15001e MD |
1588 | if (utils_create_pipe_cloexec(apps_cmd_pipe)) { |
1589 | retval = -1; | |
813ef6cb | 1590 | goto stop_threads; |
099e26bd DG |
1591 | } |
1592 | ||
d0b96690 | 1593 | /* Setup the thread apps notify communication pipe. */ |
4a15001e MD |
1594 | if (utils_create_pipe_cloexec(apps_cmd_notify_pipe)) { |
1595 | retval = -1; | |
813ef6cb | 1596 | goto stop_threads; |
d0b96690 DG |
1597 | } |
1598 | ||
7972aab2 DG |
1599 | /* Initialize global buffer per UID and PID registry. */ |
1600 | buffer_reg_init_uid_registry(); | |
1601 | buffer_reg_init_pid_registry(); | |
1602 | ||
099e26bd | 1603 | /* Init UST command queue. */ |
8bdee6e2 | 1604 | cds_wfcq_init(&ust_cmd_queue.head, &ust_cmd_queue.tail); |
099e26bd | 1605 | |
2f77fc4b | 1606 | cmd_init(); |
00e2e675 | 1607 | |
ae9e45b3 DG |
1608 | /* Check for the application socket timeout env variable. */ |
1609 | env_app_timeout = getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV); | |
1610 | if (env_app_timeout) { | |
e6142f2e | 1611 | config.app_socket_timeout = atoi(env_app_timeout); |
ae9e45b3 | 1612 | } else { |
e6142f2e | 1613 | config.app_socket_timeout = DEFAULT_APP_SOCKET_RW_TIMEOUT; |
ae9e45b3 DG |
1614 | } |
1615 | ||
4a15001e MD |
1616 | ret = write_pidfile(); |
1617 | if (ret) { | |
1618 | ERR("Error in write_pidfile"); | |
1619 | retval = -1; | |
813ef6cb | 1620 | goto stop_threads; |
4a15001e | 1621 | } |
35f90c40 | 1622 | |
554831e7 MD |
1623 | /* Initialize communication library */ |
1624 | lttcomm_init(); | |
4a15001e | 1625 | /* Initialize TCP timeout values */ |
d831c249 | 1626 | lttcomm_inet_init(); |
554831e7 | 1627 | |
12b4fb37 | 1628 | /* Create health-check thread. */ |
225221a8 | 1629 | if (!launch_health_management_thread()) { |
4a15001e | 1630 | retval = -1; |
813ef6cb | 1631 | goto stop_threads; |
44a5e5eb DG |
1632 | } |
1633 | ||
b3530820 JG |
1634 | /* notification_thread_data acquires the pipes' read side. */ |
1635 | notification_thread_handle = notification_thread_handle_create( | |
1636 | ust32_channel_monitor_pipe, | |
1637 | ust64_channel_monitor_pipe, | |
8898b0da | 1638 | kernel_channel_monitor_pipe); |
b3530820 JG |
1639 | if (!notification_thread_handle) { |
1640 | retval = -1; | |
1641 | ERR("Failed to create notification thread shared data"); | |
813ef6cb | 1642 | goto stop_threads; |
b3530820 JG |
1643 | } |
1644 | ||
1645 | /* Create notification thread. */ | |
4de27698 JG |
1646 | notification_thread = launch_notification_thread( |
1647 | notification_thread_handle); | |
1648 | if (!notification_thread) { | |
b3530820 | 1649 | retval = -1; |
813ef6cb | 1650 | goto stop_threads; |
b3530820 JG |
1651 | } |
1652 | ||
d086f507 | 1653 | /* Create timer thread. */ |
a5ab9e05 | 1654 | if (!launch_timer_thread(&timer_thread_parameters)) { |
d086f507 | 1655 | retval = -1; |
813ef6cb | 1656 | goto stop_threads; |
d086f507 | 1657 | } |
d086f507 | 1658 | |
db66e574 JD |
1659 | /* rotation_thread_data acquires the pipes' read side. */ |
1660 | rotation_thread_handle = rotation_thread_handle_create( | |
90936dcf | 1661 | rotation_timer_queue, |
8898b0da | 1662 | notification_thread_handle); |
db66e574 JD |
1663 | if (!rotation_thread_handle) { |
1664 | retval = -1; | |
1665 | ERR("Failed to create rotation thread shared data"); | |
1666 | stop_threads(); | |
813ef6cb | 1667 | goto stop_threads; |
db66e574 | 1668 | } |
db66e574 JD |
1669 | |
1670 | /* Create rotation thread. */ | |
a430759b | 1671 | if (!launch_rotation_thread(rotation_thread_handle)) { |
db66e574 | 1672 | retval = -1; |
813ef6cb | 1673 | goto stop_threads; |
db66e574 JD |
1674 | } |
1675 | ||
cf3af59e | 1676 | /* Create thread to manage the client socket */ |
050e613c JG |
1677 | client_thread = launch_client_thread(); |
1678 | if (!client_thread) { | |
4a15001e | 1679 | retval = -1; |
813ef6cb | 1680 | goto stop_threads; |
cf3af59e | 1681 | } |
fac6795d | 1682 | |
eaacb6d7 JG |
1683 | /* Set credentials of the client socket and rundir */ |
1684 | if (is_root && set_permissions(config.rundir.value)) { | |
1685 | retval = -1; | |
1686 | goto stop_threads; | |
1687 | } | |
1688 | ||
dd5875e7 JG |
1689 | if (!launch_ust_dispatch_thread(&ust_cmd_queue, apps_cmd_pipe[1], |
1690 | apps_cmd_notify_pipe[1])) { | |
4a15001e | 1691 | retval = -1; |
813ef6cb | 1692 | goto stop_threads; |
099e26bd DG |
1693 | } |
1694 | ||
1695 | /* Create thread to manage application registration. */ | |
143745a6 JG |
1696 | register_apps_thread = launch_application_registration_thread( |
1697 | &ust_cmd_queue); | |
1698 | if (!register_apps_thread) { | |
4a15001e | 1699 | retval = -1; |
813ef6cb | 1700 | goto stop_threads; |
099e26bd DG |
1701 | } |
1702 | ||
cf3af59e | 1703 | /* Create thread to manage application socket */ |
cdffb210 | 1704 | if (!launch_application_management_thread(apps_cmd_pipe[0])) { |
4a15001e | 1705 | retval = -1; |
813ef6cb | 1706 | goto stop_threads; |
d0b96690 DG |
1707 | } |
1708 | ||
1709 | /* Create thread to manage application notify socket */ | |
53c51aaa | 1710 | if (!launch_application_notification_thread(apps_cmd_notify_pipe[0])) { |
4a15001e | 1711 | retval = -1; |
813ef6cb | 1712 | goto stop_threads; |
cf3af59e | 1713 | } |
fac6795d | 1714 | |
7baf7871 JG |
1715 | /* Create agent management thread. */ |
1716 | if (!launch_agent_management_thread()) { | |
4a15001e | 1717 | retval = -1; |
813ef6cb | 1718 | goto stop_threads; |
4d076222 DG |
1719 | } |
1720 | ||
6620da75 | 1721 | /* Don't start this thread if kernel tracing is not requested nor root */ |
e6142f2e | 1722 | if (is_root && !config.no_kernel) { |
6620da75 | 1723 | /* Create kernel thread to manage kernel event */ |
45f6525f | 1724 | if (!launch_kernel_management_thread(kernel_poll_pipe[0])) { |
4a15001e | 1725 | retval = -1; |
813ef6cb | 1726 | goto stop_threads; |
6620da75 | 1727 | } |
ef367a93 | 1728 | } |
7a485870 | 1729 | |
51e3e495 JG |
1730 | /* Load sessions. */ |
1731 | ret = config_load_session(config.load_session_path.value, | |
1732 | NULL, 1, 1, NULL); | |
4a15001e | 1733 | if (ret) { |
51e3e495 | 1734 | ERR("Session load failed: %s", error_get_str(ret)); |
4a15001e | 1735 | retval = -1; |
813ef6cb | 1736 | goto stop_threads; |
4a15001e MD |
1737 | } |
1738 | ||
51e3e495 JG |
1739 | /* Initialization completed. */ |
1740 | sessiond_signal_parents(); | |
1741 | ||
4a15001e MD |
1742 | /* |
1743 | * This is where we start awaiting program completion (e.g. through | |
1744 | * signal that asks threads to teardown). | |
1745 | */ | |
1746 | ||
6af972a2 | 1747 | /* Initiate teardown once activity occurs on the quit pipe. */ |
06d7785f | 1748 | sessiond_wait_for_quit_pipe(-1); |
050e613c | 1749 | |
813ef6cb | 1750 | stop_threads: |
050e613c JG |
1751 | /* |
1752 | * Ensure that the client thread is no longer accepting new commands, | |
1753 | * which could cause new sessions to be created. | |
1754 | */ | |
813ef6cb JG |
1755 | if (client_thread) { |
1756 | lttng_thread_shutdown(client_thread); | |
050e613c | 1757 | lttng_thread_put(client_thread); |
050e613c JG |
1758 | } |
1759 | ||
6af972a2 | 1760 | destroy_all_sessions_and_wait(); |
813ef6cb | 1761 | |
143745a6 JG |
1762 | if (register_apps_thread) { |
1763 | lttng_thread_shutdown(register_apps_thread); | |
1764 | lttng_thread_put(register_apps_thread); | |
1765 | } | |
548468b9 | 1766 | lttng_thread_list_shutdown_orphans(); |
050e613c | 1767 | |
4d62fbf8 MD |
1768 | /* |
1769 | * Wait for all pending call_rcu work to complete before tearing | |
1770 | * down data structures. call_rcu worker may be trying to | |
1771 | * perform lookups in those structures. | |
1772 | */ | |
1773 | rcu_barrier(); | |
4a15001e MD |
1774 | /* |
1775 | * sessiond_cleanup() is called when no other thread is running, except | |
1776 | * the ht_cleanup thread, which is needed to destroy the hash tables. | |
1777 | */ | |
1778 | rcu_thread_online(); | |
1779 | sessiond_cleanup(); | |
06f525de | 1780 | |
4de27698 JG |
1781 | if (notification_thread) { |
1782 | lttng_thread_shutdown(notification_thread); | |
1783 | lttng_thread_put(notification_thread); | |
1784 | } | |
1785 | ||
c4d5de9d JG |
1786 | /* |
1787 | * Ensure all prior call_rcu are done. call_rcu callbacks may push | |
1788 | * hash tables to the ht_cleanup thread. Therefore, we ensure that | |
1789 | * the queue is empty before shutting down the clean-up thread. | |
1790 | */ | |
1791 | rcu_barrier(); | |
1792 | ||
548468b9 JG |
1793 | if (ht_cleanup_thread) { |
1794 | lttng_thread_shutdown(ht_cleanup_thread); | |
1795 | lttng_thread_put(ht_cleanup_thread); | |
1796 | } | |
1797 | ||
a430759b JG |
1798 | rcu_thread_offline(); |
1799 | rcu_unregister_thread(); | |
1800 | ||
1801 | if (rotation_thread_handle) { | |
1802 | rotation_thread_handle_destroy(rotation_thread_handle); | |
1803 | } | |
1804 | ||
d086f507 JD |
1805 | /* |
1806 | * After the rotation and timer thread have quit, we can safely destroy | |
1807 | * the rotation_timer_queue. | |
1808 | */ | |
82528808 | 1809 | rotation_thread_timer_queue_destroy(rotation_timer_queue); |
8898b0da JG |
1810 | /* |
1811 | * The teardown of the notification system is performed after the | |
1812 | * session daemon's teardown in order to allow it to be notified | |
1813 | * of the active session and channels at the moment of the teardown. | |
1814 | */ | |
1815 | if (notification_thread_handle) { | |
1816 | notification_thread_handle_destroy(notification_thread_handle); | |
1817 | } | |
b3530820 JG |
1818 | lttng_pipe_destroy(ust32_channel_monitor_pipe); |
1819 | lttng_pipe_destroy(ust64_channel_monitor_pipe); | |
1820 | lttng_pipe_destroy(kernel_channel_monitor_pipe); | |
4a15001e | 1821 | |
4a15001e | 1822 | health_app_destroy(health_sessiond); |
7567352f | 1823 | exit_create_run_as_worker_cleanup: |
4a15001e | 1824 | exit_options: |
f472090a | 1825 | sessiond_cleanup_lock_file(); |
4a15001e MD |
1826 | sessiond_cleanup_options(); |
1827 | ||
1828 | exit_set_signal_handler: | |
1829 | if (!retval) { | |
cf3af59e | 1830 | exit(EXIT_SUCCESS); |
4a15001e MD |
1831 | } else { |
1832 | exit(EXIT_FAILURE); | |
67e40797 | 1833 | } |
fac6795d | 1834 | } |