Launch agent management thread using lttng_thread
[lttng-tools.git] / src / bin / lttng-sessiond / main.c
CommitLineData
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"
096102bd 64#include "modprobe.h"
0fdd1e2c 65#include "shm.h"
1e307fab 66#include "ust-ctl.h"
00e2e675 67#include "ust-consumer.h"
8e68d1c8 68#include "utils.h"
4063050c 69#include "fd-limit.h"
8782cc74 70#include "health-sessiond.h"
8ac94142 71#include "testpoint.h"
d0b96690 72#include "ust-thread.h"
022d91ba 73#include "agent-thread.h"
fb198a11 74#include "save.h"
ef367a93 75#include "load-session-thread.h"
b3530820
JG
76#include "notification-thread.h"
77#include "notification-thread-commands.h"
db66e574 78#include "rotation-thread.h"
0dbc2034 79#include "lttng-syscall.h"
7c1d2758 80#include "agent.h"
5e97de00 81#include "ht-cleanup.h"
e6142f2e 82#include "sessiond-config.h"
8e319828 83#include "timer.h"
a3707772 84#include "thread.h"
917a718d 85#include "client.h"
5d1b0219 86#include "dispatch.h"
1785d7f2 87#include "register.h"
7649924e 88#include "manage-apps.h"
ebaeda94 89
4fc83d94
PP
90static const char *help_msg =
91#ifdef LTTNG_EMBED_HELP
92#include <lttng-sessiond.8.h>
93#else
94NULL
95#endif
96;
97
fac6795d 98const char *progname;
c9cb3e7d 99static int lockfile_fd = -1;
3bd1e081 100
0bb7724a
DG
101/* Set to 1 when a SIGUSR1 signal is received. */
102static int recv_child_signal;
103
26296c48
JG
104/* Command line options */
105static const struct option long_options[] = {
0f5ea17c
JG
106 { "client-sock", required_argument, 0, 'c' },
107 { "apps-sock", required_argument, 0, 'a' },
108 { "kconsumerd-cmd-sock", required_argument, 0, '\0' },
109 { "kconsumerd-err-sock", required_argument, 0, '\0' },
110 { "ustconsumerd32-cmd-sock", required_argument, 0, '\0' },
111 { "ustconsumerd32-err-sock", required_argument, 0, '\0' },
112 { "ustconsumerd64-cmd-sock", required_argument, 0, '\0' },
113 { "ustconsumerd64-err-sock", required_argument, 0, '\0' },
114 { "consumerd32-path", required_argument, 0, '\0' },
115 { "consumerd32-libdir", required_argument, 0, '\0' },
116 { "consumerd64-path", required_argument, 0, '\0' },
117 { "consumerd64-libdir", required_argument, 0, '\0' },
118 { "daemonize", no_argument, 0, 'd' },
119 { "background", no_argument, 0, 'b' },
120 { "sig-parent", no_argument, 0, 'S' },
121 { "help", no_argument, 0, 'h' },
122 { "group", required_argument, 0, 'g' },
123 { "version", no_argument, 0, 'V' },
124 { "quiet", no_argument, 0, 'q' },
125 { "verbose", no_argument, 0, 'v' },
126 { "verbose-consumer", no_argument, 0, '\0' },
127 { "no-kernel", no_argument, 0, '\0' },
128 { "pidfile", required_argument, 0, 'p' },
129 { "agent-tcp-port", required_argument, 0, '\0' },
130 { "config", required_argument, 0, 'f' },
131 { "load", required_argument, 0, 'l' },
132 { "kmod-probes", required_argument, 0, '\0' },
133 { "extra-kmod-probes", required_argument, 0, '\0' },
26296c48
JG
134 { NULL, 0, 0, 0 }
135};
136
137/* Command line options to ignore from configuration file */
138static const char *config_ignore_options[] = { "help", "version", "config" };
1d4b027a 139
099e26bd
DG
140/*
141 * This pipe is used to inform the thread managing application communication
142 * that a command is queued and ready to be processed.
143 */
76d7553f 144static int apps_cmd_pipe[2] = { -1, -1 };
971a61c6 145static int apps_cmd_notify_pipe[2] = { -1, -1 };
099e26bd 146
1d4b027a 147/* Pthread, Mutexes and Semaphores */
7a485870 148static pthread_t kernel_thread;
ef367a93 149static pthread_t load_session_thread;
5eb91c98 150
099e26bd
DG
151/*
152 * UST registration command queue. This queue is tied with a futex and uses a N
153 * wakers / 1 waiter implemented and detailed in futex.c/.h
154 *
b22c5da8
DG
155 * The thread_registration_apps and thread_dispatch_ust_registration uses this
156 * queue along with the wait/wake scheme. The thread_manage_apps receives down
157 * the line new application socket and monitors it for any I/O error or clean
158 * close that triggers an unregistration of the application.
099e26bd
DG
159 */
160static struct ust_cmd_queue ust_cmd_queue;
161
2f77fc4b
DG
162static const char *module_proc_lttng = "/proc/lttng";
163
ef367a93 164/* Load session thread information to operate. */
a7333da7 165static struct load_session_thread_data *load_info;
7c1d2758 166
97bc1426 167/*
a7333da7 168 * Section name to look for in the daemon configuration file.
97bc1426 169 */
a7333da7 170static const char * const config_section_name = "sessiond";
5e97de00 171
a7333da7
JG
172/* Am I root or not. Set to 1 if the daemon is running as root */
173static int is_root;
5eb91c98 174
099e26bd
DG
175/*
176 * Stop all threads by closing the thread quit pipe.
177 */
cf3af59e
MD
178static void stop_threads(void)
179{
5eb91c98
DG
180 int ret;
181
cf3af59e
MD
182 /* Stopping all threads */
183 DBG("Terminating all threads");
a7333da7 184 ret = sessiond_notify_quit_pipe();
5eb91c98
DG
185 if (ret < 0) {
186 ERR("write error on thread quit pipe");
187 }
cf3af59e
MD
188}
189
e975f9f8
DG
190/*
191 * Close every consumer sockets.
192 */
193static void close_consumer_sockets(void)
194{
195 int ret;
196
197 if (kconsumer_data.err_sock >= 0) {
198 ret = close(kconsumer_data.err_sock);
199 if (ret < 0) {
200 PERROR("kernel consumer err_sock close");
201 }
202 }
203 if (ustconsumer32_data.err_sock >= 0) {
204 ret = close(ustconsumer32_data.err_sock);
205 if (ret < 0) {
a76cbd9f 206 PERROR("UST consumerd32 err_sock close");
e975f9f8
DG
207 }
208 }
209 if (ustconsumer64_data.err_sock >= 0) {
210 ret = close(ustconsumer64_data.err_sock);
211 if (ret < 0) {
a76cbd9f 212 PERROR("UST consumerd64 err_sock close");
e975f9f8
DG
213 }
214 }
215 if (kconsumer_data.cmd_sock >= 0) {
216 ret = close(kconsumer_data.cmd_sock);
217 if (ret < 0) {
218 PERROR("kernel consumer cmd_sock close");
219 }
220 }
221 if (ustconsumer32_data.cmd_sock >= 0) {
222 ret = close(ustconsumer32_data.cmd_sock);
223 if (ret < 0) {
a76cbd9f 224 PERROR("UST consumerd32 cmd_sock close");
e975f9f8
DG
225 }
226 }
227 if (ustconsumer64_data.cmd_sock >= 0) {
228 ret = close(ustconsumer64_data.cmd_sock);
229 if (ret < 0) {
a76cbd9f 230 PERROR("UST consumerd64 cmd_sock close");
e975f9f8
DG
231 }
232 }
b3530820
JG
233 if (kconsumer_data.channel_monitor_pipe >= 0) {
234 ret = close(kconsumer_data.channel_monitor_pipe);
235 if (ret < 0) {
236 PERROR("kernel consumer channel monitor pipe close");
237 }
238 }
239 if (ustconsumer32_data.channel_monitor_pipe >= 0) {
240 ret = close(ustconsumer32_data.channel_monitor_pipe);
241 if (ret < 0) {
242 PERROR("UST consumerd32 channel monitor pipe close");
243 }
244 }
245 if (ustconsumer64_data.channel_monitor_pipe >= 0) {
246 ret = close(ustconsumer64_data.channel_monitor_pipe);
247 if (ret < 0) {
248 PERROR("UST consumerd64 channel monitor pipe close");
249 }
250 }
e975f9f8
DG
251}
252
4e4714cb
JR
253/*
254 * Wait on consumer process termination.
255 *
256 * Need to be called with the consumer data lock held or from a context
257 * ensuring no concurrent access to data (e.g: cleanup).
258 */
259static void wait_consumer(struct consumer_data *consumer_data)
260{
261 pid_t ret;
262 int status;
263
264 if (consumer_data->pid <= 0) {
265 return;
266 }
267
268 DBG("Waiting for complete teardown of consumerd (PID: %d)",
269 consumer_data->pid);
270 ret = waitpid(consumer_data->pid, &status, 0);
271 if (ret == -1) {
272 PERROR("consumerd waitpid pid: %d", consumer_data->pid)
1640c24c 273 } else if (!WIFEXITED(status)) {
4e4714cb
JR
274 ERR("consumerd termination with error: %d",
275 WEXITSTATUS(ret));
276 }
277 consumer_data->pid = 0;
278}
279
fac6795d 280/*
4a15001e 281 * Cleanup the session daemon's data structures.
fac6795d 282 */
4a15001e 283static void sessiond_cleanup(void)
fac6795d 284{
ef599319 285 int ret;
e32d7f27 286 struct ltt_session_list *session_list = session_get_list();
fac6795d 287
4a15001e 288 DBG("Cleanup sessiond");
e07ae692 289
4e449f3f
MD
290 /*
291 * Close the thread quit pipe. It has already done its job,
292 * since we are now called.
293 */
a7333da7 294 sessiond_close_quit_pipe();
971a61c6
JG
295 utils_close_pipe(apps_cmd_pipe);
296 utils_close_pipe(apps_cmd_notify_pipe);
2f77fc4b 297
c9a2957d
JG
298 ret = remove(config.pid_file_path.value);
299 if (ret < 0) {
300 PERROR("remove pidfile %s", config.pid_file_path.value);
35f90c40
DG
301 }
302
e6142f2e
JG
303 DBG("Removing sessiond and consumerd content of directory %s",
304 config.rundir.value);
8c6c56c2
MD
305
306 /* sessiond */
e6142f2e
JG
307 DBG("Removing %s", config.pid_file_path.value);
308 (void) unlink(config.pid_file_path.value);
8c6c56c2 309
e6142f2e
JG
310 DBG("Removing %s", config.agent_port_file_path.value);
311 (void) unlink(config.agent_port_file_path.value);
cd9290dd 312
8c6c56c2 313 /* kconsumerd */
e6142f2e
JG
314 DBG("Removing %s", kconsumer_data.err_unix_sock_path);
315 (void) unlink(kconsumer_data.err_unix_sock_path);
316
317 DBG("Removing directory %s", config.kconsumerd_path.value);
318 (void) rmdir(config.kconsumerd_path.value);
8c6c56c2
MD
319
320 /* ust consumerd 32 */
e6142f2e
JG
321 DBG("Removing %s", config.consumerd32_err_unix_sock_path.value);
322 (void) unlink(config.consumerd32_err_unix_sock_path.value);
323
324 DBG("Removing directory %s", config.consumerd32_path.value);
325 (void) rmdir(config.consumerd32_path.value);
8c6c56c2
MD
326
327 /* ust consumerd 64 */
e6142f2e
JG
328 DBG("Removing %s", config.consumerd64_err_unix_sock_path.value);
329 (void) unlink(config.consumerd64_err_unix_sock_path.value);
330
331 DBG("Removing directory %s", config.consumerd64_path.value);
332 (void) rmdir(config.consumerd64_path.value);
5461b305 333
99d688f2 334 pthread_mutex_destroy(&session_list->lock);
273ea72c 335
4e4714cb
JR
336 wait_consumer(&kconsumer_data);
337 wait_consumer(&ustconsumer64_data);
338 wait_consumer(&ustconsumer32_data);
339
6a4e4039
JG
340 DBG("Cleaning up all agent apps");
341 agent_app_ht_clean();
342
099e26bd 343 DBG("Closing all UST sockets");
56fff090 344 ust_app_clean_list();
7972aab2 345 buffer_reg_destroy_registries();
099e26bd 346
e6142f2e 347 if (is_root && !config.no_kernel) {
4fba7219 348 DBG2("Closing kernel fd");
a4b35e07 349 if (kernel_tracer_fd >= 0) {
76d7553f
MD
350 ret = close(kernel_tracer_fd);
351 if (ret) {
352 PERROR("close");
353 }
a4b35e07 354 }
2f50c8a3 355 DBG("Unloading kernel modules");
096102bd 356 modprobe_remove_lttng_all();
834978fd 357 free(syscall_table);
2f50c8a3 358 }
2f77fc4b 359
e975f9f8
DG
360 close_consumer_sockets();
361
ef367a93
JG
362 if (load_info) {
363 load_session_destroy_data(load_info);
364 free(load_info);
365 }
366
c9cb3e7d
JG
367 /*
368 * We do NOT rmdir rundir because there are other processes
369 * using it, for instance lttng-relayd, which can start in
370 * parallel with this teardown.
371 */
4a15001e
MD
372}
373
374/*
375 * Cleanup the daemon's option data structures.
376 */
377static void sessiond_cleanup_options(void)
378{
379 DBG("Cleaning up options");
380
e6142f2e 381 sessiond_config_fini(&config);
c9cb3e7d 382
7567352f 383 run_as_destroy_worker();
fac6795d
DG
384}
385
7a485870 386/*
5eb91c98 387 * Update the kernel poll set of all channel fd available over all tracing
d063d709 388 * session. Add the wakeup pipe at the end of the set.
7a485870 389 */
5eb91c98 390static int update_kernel_poll(struct lttng_poll_event *events)
7a485870 391{
5eb91c98 392 int ret;
7a485870 393 struct ltt_kernel_channel *channel;
e32d7f27
JG
394 struct ltt_session *session;
395 const struct ltt_session_list *session_list = session_get_list();
7a485870 396
5eb91c98 397 DBG("Updating kernel poll set");
7a485870 398
54d01ffb 399 session_lock_list();
e32d7f27
JG
400 cds_list_for_each_entry(session, &session_list->head, list) {
401 if (!session_get(session)) {
402 continue;
403 }
54d01ffb 404 session_lock(session);
7a485870 405 if (session->kernel_session == NULL) {
54d01ffb 406 session_unlock(session);
e32d7f27 407 session_put(session);
7a485870
DG
408 continue;
409 }
7a485870 410
54d01ffb
DG
411 cds_list_for_each_entry(channel,
412 &session->kernel_session->channel_list.head, list) {
5eb91c98
DG
413 /* Add channel fd to the kernel poll set */
414 ret = lttng_poll_add(events, channel->fd, LPOLLIN | LPOLLRDNORM);
415 if (ret < 0) {
54d01ffb 416 session_unlock(session);
e32d7f27 417 session_put(session);
5eb91c98
DG
418 goto error;
419 }
420 DBG("Channel fd %d added to kernel set", channel->fd);
7a485870 421 }
54d01ffb 422 session_unlock(session);
7a485870 423 }
54d01ffb 424 session_unlock_list();
7a485870 425
5eb91c98 426 return 0;
7a485870
DG
427
428error:
54d01ffb 429 session_unlock_list();
7a485870
DG
430 return -1;
431}
432
433/*
54d01ffb 434 * Find the channel fd from 'fd' over all tracing session. When found, check
d063d709 435 * for new channel stream and send those stream fds to the kernel consumer.
7a485870 436 *
d063d709 437 * Useful for CPU hotplug feature.
7a485870 438 */
7f032058 439static int update_kernel_stream(int fd)
7a485870
DG
440{
441 int ret = 0;
442 struct ltt_session *session;
173af62f 443 struct ltt_kernel_session *ksess;
7a485870 444 struct ltt_kernel_channel *channel;
e32d7f27 445 const struct ltt_session_list *session_list = session_get_list();
7a485870
DG
446
447 DBG("Updating kernel streams for channel fd %d", fd);
448
54d01ffb 449 session_lock_list();
e32d7f27
JG
450 cds_list_for_each_entry(session, &session_list->head, list) {
451 if (!session_get(session)) {
452 continue;
453 }
54d01ffb 454 session_lock(session);
7a485870 455 if (session->kernel_session == NULL) {
54d01ffb 456 session_unlock(session);
e32d7f27 457 session_put(session);
7a485870
DG
458 continue;
459 }
173af62f 460 ksess = session->kernel_session;
d9800920 461
4a15001e
MD
462 cds_list_for_each_entry(channel,
463 &ksess->channel_list.head, list) {
464 struct lttng_ht_iter iter;
465 struct consumer_socket *socket;
d9800920 466
4a15001e
MD
467 if (channel->fd != fd) {
468 continue;
469 }
470 DBG("Channel found, updating kernel streams");
471 ret = kernel_open_channel_stream(channel);
472 if (ret < 0) {
473 goto error;
474 }
475 /* Update the stream global counter */
476 ksess->stream_count_global += ret;
477
478 /*
479 * Have we already sent fds to the consumer? If yes, it
480 * means that tracing is started so it is safe to send
481 * our updated stream fds.
482 */
483 if (ksess->consumer_fds_sent != 1
484 || ksess->consumer == NULL) {
485 ret = -1;
486 goto error;
487 }
488
489 rcu_read_lock();
490 cds_lfht_for_each_entry(ksess->consumer->socks->ht,
491 &iter.iter, socket, node.node) {
492 pthread_mutex_lock(socket->lock);
1fc1b7c8 493 ret = kernel_consumer_send_channel_streams(socket,
4a15001e
MD
494 channel, ksess,
495 session->output_traces ? 1 : 0);
496 pthread_mutex_unlock(socket->lock);
497 if (ret < 0) {
e7fe706f 498 rcu_read_unlock();
4a15001e 499 goto error;
7a485870 500 }
7a485870 501 }
4a15001e 502 rcu_read_unlock();
7a485870 503 }
54d01ffb 504 session_unlock(session);
e32d7f27 505 session_put(session);
7a485870 506 }
54d01ffb 507 session_unlock_list();
b3c750d2 508 return ret;
7a485870 509
b3c750d2 510error:
54d01ffb 511 session_unlock(session);
e32d7f27 512 session_put(session);
54d01ffb 513 session_unlock_list();
7a485870
DG
514 return ret;
515}
516
517/*
d063d709 518 * This thread manage event coming from the kernel.
7a485870 519 *
d063d709
DG
520 * Features supported in this thread:
521 * -) CPU Hotplug
7a485870
DG
522 */
523static void *thread_manage_kernel(void *data)
524{
139ac872 525 int ret, i, pollfd, update_poll_flag = 1, err = -1;
5eb91c98 526 uint32_t revents, nb_fd;
7a485870 527 char tmp;
5eb91c98 528 struct lttng_poll_event events;
7a485870 529
6993eeb3 530 DBG("[thread] Thread manage kernel started");
7a485870 531
6c71277b 532 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_KERNEL);
927ca06a 533
d5d63bf1
DG
534 /*
535 * This first step of the while is to clean this structure which could free
6d737ce4 536 * non NULL pointers so initialize it before the loop.
d5d63bf1 537 */
6d737ce4 538 lttng_poll_init(&events);
d5d63bf1 539
e547b070 540 if (testpoint(sessiond_thread_manage_kernel)) {
6993eeb3
CB
541 goto error_testpoint;
542 }
8ac94142 543
840cb59c 544 health_code_update();
44a5e5eb 545
e547b070 546 if (testpoint(sessiond_thread_manage_kernel_before_loop)) {
d21b0d71 547 goto error_testpoint;
6993eeb3
CB
548 }
549
7a485870 550 while (1) {
840cb59c 551 health_code_update();
44a5e5eb 552
7a485870 553 if (update_poll_flag == 1) {
d21b0d71
DG
554 /* Clean events object. We are about to populate it again. */
555 lttng_poll_clean(&events);
556
d0b96690 557 ret = sessiond_set_thread_pollset(&events, 2);
d21b0d71
DG
558 if (ret < 0) {
559 goto error_poll_create;
560 }
561
562 ret = lttng_poll_add(&events, kernel_poll_pipe[0], LPOLLIN);
563 if (ret < 0) {
564 goto error;
565 }
5f822d0a 566
d21b0d71 567 /* This will add the available kernel channel if any. */
5eb91c98
DG
568 ret = update_kernel_poll(&events);
569 if (ret < 0) {
7a485870
DG
570 goto error;
571 }
572 update_poll_flag = 0;
573 }
574
7fa2082e 575 DBG("Thread kernel polling");
7a485870
DG
576
577 /* Poll infinite value of time */
88f2b785 578 restart:
a78af745 579 health_poll_entry();
5eb91c98 580 ret = lttng_poll_wait(&events, -1);
7fa2082e
MD
581 DBG("Thread kernel return from poll on %d fds",
582 LTTNG_POLL_GETNB(&events));
a78af745 583 health_poll_exit();
7a485870 584 if (ret < 0) {
88f2b785
MD
585 /*
586 * Restart interrupted system call.
587 */
588 if (errno == EINTR) {
589 goto restart;
590 }
7a485870
DG
591 goto error;
592 } else if (ret == 0) {
593 /* Should not happen since timeout is infinite */
85611738
DG
594 ERR("Return value of poll is 0 with an infinite timeout.\n"
595 "This should not have happened! Continuing...");
7a485870
DG
596 continue;
597 }
598
0d9c5d77
DG
599 nb_fd = ret;
600
5eb91c98
DG
601 for (i = 0; i < nb_fd; i++) {
602 /* Fetch once the poll data */
603 revents = LTTNG_POLL_GETEV(&events, i);
604 pollfd = LTTNG_POLL_GETFD(&events, i);
7a485870 605
840cb59c 606 health_code_update();
44a5e5eb 607
fd20dac9
MD
608 if (!revents) {
609 /* No activity for this FD (poll implementation). */
610 continue;
611 }
612
5eb91c98 613 /* Thread quit pipe has been closed. Killing thread. */
d0b96690 614 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
5eb91c98 615 if (ret) {
139ac872
MD
616 err = 0;
617 goto exit;
5eb91c98 618 }
7a485870 619
5eb91c98 620 /* Check for data on kernel pipe */
03e43155
MD
621 if (revents & LPOLLIN) {
622 if (pollfd == kernel_poll_pipe[0]) {
623 (void) lttng_read(kernel_poll_pipe[0],
624 &tmp, 1);
625 /*
626 * Ret value is useless here, if this pipe gets any actions an
627 * update is required anyway.
628 */
629 update_poll_flag = 1;
630 continue;
631 } else {
632 /*
633 * New CPU detected by the kernel. Adding kernel stream to
634 * kernel session and updating the kernel consumer
635 */
7f032058 636 ret = update_kernel_stream(pollfd);
5eb91c98
DG
637 if (ret < 0) {
638 continue;
639 }
640 break;
7a485870 641 }
03e43155
MD
642 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
643 update_poll_flag = 1;
644 continue;
645 } else {
646 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
647 goto error;
7a485870
DG
648 }
649 }
650 }
651
139ac872 652exit:
7a485870 653error:
5eb91c98 654 lttng_poll_clean(&events);
76d7553f 655error_poll_create:
6993eeb3 656error_testpoint:
6620da75
DG
657 utils_close_pipe(kernel_poll_pipe);
658 kernel_poll_pipe[0] = kernel_poll_pipe[1] = -1;
139ac872 659 if (err) {
840cb59c 660 health_error();
139ac872 661 ERR("Health error occurred in %s", __func__);
6620da75
DG
662 WARN("Kernel thread died unexpectedly. "
663 "Kernel tracing can continue but CPU hotplug is disabled.");
139ac872 664 }
8782cc74 665 health_unregister(health_sessiond);
76d7553f 666 DBG("Kernel thread dying");
7a485870
DG
667 return NULL;
668}
669
a23ec3a7
DG
670/*
671 * Signal pthread condition of the consumer data that the thread.
672 */
673static void signal_consumer_condition(struct consumer_data *data, int state)
674{
675 pthread_mutex_lock(&data->cond_mutex);
676
677 /*
678 * The state is set before signaling. It can be any value, it's the waiter
679 * job to correctly interpret this condition variable associated to the
680 * consumer pthread_cond.
681 *
682 * A value of 0 means that the corresponding thread of the consumer data
683 * was not started. 1 indicates that the thread has started and is ready
684 * for action. A negative value means that there was an error during the
685 * thread bootstrap.
686 */
687 data->consumer_thread_is_ready = state;
688 (void) pthread_cond_signal(&data->cond);
689
690 pthread_mutex_unlock(&data->cond_mutex);
691}
692
1d4b027a 693/*
3bd1e081 694 * This thread manage the consumer error sent back to the session daemon.
1d4b027a 695 */
917a718d 696void *thread_manage_consumer(void *data)
1d4b027a 697{
42fc1d0b 698 int sock = -1, i, ret, pollfd, err = -1, should_quit = 0;
5eb91c98 699 uint32_t revents, nb_fd;
1d4b027a 700 enum lttcomm_return_code code;
5eb91c98 701 struct lttng_poll_event events;
3bd1e081 702 struct consumer_data *consumer_data = data;
b3530820 703 struct consumer_socket *cmd_socket_wrapper = NULL;
1d4b027a 704
3bd1e081 705 DBG("[thread] Manage consumer started");
1d4b027a 706
34c1e15a
MD
707 rcu_register_thread();
708 rcu_thread_online();
709
6c71277b 710 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_CONSUMER);
927ca06a 711
855060f8 712 health_code_update();
9449cc75 713
5eb91c98 714 /*
331744e3
JD
715 * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the
716 * metadata_sock. Nothing more will be added to this poll set.
5eb91c98 717 */
331744e3 718 ret = sessiond_set_thread_pollset(&events, 3);
5eb91c98 719 if (ret < 0) {
76d7553f 720 goto error_poll;
5eb91c98 721 }
273ea72c 722
edb8b045
DG
723 /*
724 * The error socket here is already in a listening state which was done
725 * just before spawning this thread to avoid a race between the consumer
726 * daemon exec trying to connect and the listen() call.
727 */
3bd1e081 728 ret = lttng_poll_add(&events, consumer_data->err_sock, LPOLLIN | LPOLLRDHUP);
5eb91c98
DG
729 if (ret < 0) {
730 goto error;
731 }
732
840cb59c 733 health_code_update();
44a5e5eb 734
331744e3 735 /* Infinite blocking call, waiting for transmission */
88f2b785 736restart:
a78af745 737 health_poll_entry();
8ac94142 738
e547b070 739 if (testpoint(sessiond_thread_manage_consumer)) {
6993eeb3
CB
740 goto error;
741 }
8ac94142 742
5eb91c98 743 ret = lttng_poll_wait(&events, -1);
a78af745 744 health_poll_exit();
273ea72c 745 if (ret < 0) {
88f2b785
MD
746 /*
747 * Restart interrupted system call.
748 */
749 if (errno == EINTR) {
750 goto restart;
751 }
273ea72c
DG
752 goto error;
753 }
754
0d9c5d77
DG
755 nb_fd = ret;
756
5eb91c98
DG
757 for (i = 0; i < nb_fd; i++) {
758 /* Fetch once the poll data */
759 revents = LTTNG_POLL_GETEV(&events, i);
760 pollfd = LTTNG_POLL_GETFD(&events, i);
761
840cb59c 762 health_code_update();
44a5e5eb 763
fd20dac9
MD
764 if (!revents) {
765 /* No activity for this FD (poll implementation). */
766 continue;
767 }
768
5eb91c98 769 /* Thread quit pipe has been closed. Killing thread. */
d0b96690 770 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
5eb91c98 771 if (ret) {
139ac872
MD
772 err = 0;
773 goto exit;
5eb91c98
DG
774 }
775
776 /* Event on the registration socket */
3bd1e081 777 if (pollfd == consumer_data->err_sock) {
03e43155
MD
778 if (revents & LPOLLIN) {
779 continue;
780 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
3bd1e081 781 ERR("consumer err socket poll error");
5eb91c98 782 goto error;
03e43155
MD
783 } else {
784 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
785 goto error;
5eb91c98
DG
786 }
787 }
273ea72c
DG
788 }
789
3bd1e081 790 sock = lttcomm_accept_unix_sock(consumer_data->err_sock);
1d4b027a
DG
791 if (sock < 0) {
792 goto error;
793 }
794
b662582b
DG
795 /*
796 * Set the CLOEXEC flag. Return code is useless because either way, the
797 * show must go on.
798 */
799 (void) utils_set_fd_cloexec(sock);
800
840cb59c 801 health_code_update();
44a5e5eb 802
3bd1e081 803 DBG2("Receiving code from consumer err_sock");
ee0b0061 804
712ea556 805 /* Getting status code from kconsumerd */
54d01ffb
DG
806 ret = lttcomm_recv_unix_sock(sock, &code,
807 sizeof(enum lttcomm_return_code));
1d4b027a
DG
808 if (ret <= 0) {
809 goto error;
810 }
811
840cb59c 812 health_code_update();
b3530820 813 if (code != LTTCOMM_CONSUMERD_COMMAND_SOCK_READY) {
3bd1e081 814 ERR("consumer error when waiting for SOCK_READY : %s",
1d4b027a
DG
815 lttcomm_get_readable_code(-code));
816 goto error;
817 }
818
b3530820
JG
819 /* Connect both command and metadata sockets. */
820 consumer_data->cmd_sock =
821 lttcomm_connect_unix_sock(
822 consumer_data->cmd_unix_sock_path);
823 consumer_data->metadata_fd =
824 lttcomm_connect_unix_sock(
825 consumer_data->cmd_unix_sock_path);
826 if (consumer_data->cmd_sock < 0 || consumer_data->metadata_fd < 0) {
827 PERROR("consumer connect cmd socket");
828 /* On error, signal condition and quit. */
829 signal_consumer_condition(consumer_data, -1);
830 goto error;
831 }
832
833 consumer_data->metadata_sock.fd_ptr = &consumer_data->metadata_fd;
834
835 /* Create metadata socket lock. */
836 consumer_data->metadata_sock.lock = zmalloc(sizeof(pthread_mutex_t));
837 if (consumer_data->metadata_sock.lock == NULL) {
838 PERROR("zmalloc pthread mutex");
839 goto error;
840 }
841 pthread_mutex_init(consumer_data->metadata_sock.lock, NULL);
842
843 DBG("Consumer command socket ready (fd: %d", consumer_data->cmd_sock);
844 DBG("Consumer metadata socket ready (fd: %d)",
845 consumer_data->metadata_fd);
846
847 /*
848 * Remove the consumerd error sock since we've established a connection.
849 */
3bd1e081 850 ret = lttng_poll_del(&events, consumer_data->err_sock);
72079cae 851 if (ret < 0) {
72079cae
DG
852 goto error;
853 }
854
331744e3 855 /* Add new accepted error socket. */
5eb91c98
DG
856 ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLRDHUP);
857 if (ret < 0) {
72079cae 858 goto error;
5eb91c98
DG
859 }
860
331744e3 861 /* Add metadata socket that is successfully connected. */
4ce514c4 862 ret = lttng_poll_add(&events, consumer_data->metadata_fd,
331744e3
JD
863 LPOLLIN | LPOLLRDHUP);
864 if (ret < 0) {
865 goto error;
866 }
867
840cb59c 868 health_code_update();
44a5e5eb 869
b3530820 870 /*
62c43103 871 * Transfer the write-end of the channel monitoring and rotate pipe
92816cc3 872 * to the consumer by issuing a SET_CHANNEL_MONITOR_PIPE command.
b3530820
JG
873 */
874 cmd_socket_wrapper = consumer_allocate_socket(&consumer_data->cmd_sock);
875 if (!cmd_socket_wrapper) {
876 goto error;
877 }
3e4dc117 878 cmd_socket_wrapper->lock = &consumer_data->lock;
b3530820
JG
879
880 ret = consumer_send_channel_monitor_pipe(cmd_socket_wrapper,
881 consumer_data->channel_monitor_pipe);
882 if (ret) {
883 goto error;
884 }
62c43103 885
b3530820
JG
886 /* Discard the socket wrapper as it is no longer needed. */
887 consumer_destroy_socket(cmd_socket_wrapper);
888 cmd_socket_wrapper = NULL;
889
890 /* The thread is completely initialized, signal that it is ready. */
891 signal_consumer_condition(consumer_data, 1);
892
331744e3 893 /* Infinite blocking call, waiting for transmission */
88f2b785 894restart_poll:
331744e3 895 while (1) {
42fc1d0b
DG
896 health_code_update();
897
898 /* Exit the thread because the thread quit pipe has been triggered. */
899 if (should_quit) {
900 /* Not a health error. */
901 err = 0;
902 goto exit;
903 }
904
331744e3
JD
905 health_poll_entry();
906 ret = lttng_poll_wait(&events, -1);
907 health_poll_exit();
908 if (ret < 0) {
909 /*
910 * Restart interrupted system call.
911 */
912 if (errno == EINTR) {
913 goto restart_poll;
914 }
915 goto error;
88f2b785 916 }
72079cae 917
331744e3 918 nb_fd = ret;
0d9c5d77 919
331744e3
JD
920 for (i = 0; i < nb_fd; i++) {
921 /* Fetch once the poll data */
922 revents = LTTNG_POLL_GETEV(&events, i);
923 pollfd = LTTNG_POLL_GETFD(&events, i);
5eb91c98 924
331744e3 925 health_code_update();
44a5e5eb 926
fd20dac9
MD
927 if (!revents) {
928 /* No activity for this FD (poll implementation). */
929 continue;
930 }
931
42fc1d0b
DG
932 /*
933 * Thread quit pipe has been triggered, flag that we should stop
934 * but continue the current loop to handle potential data from
935 * consumer.
936 */
937 should_quit = sessiond_check_thread_quit_pipe(pollfd, revents);
5eb91c98 938
331744e3
JD
939 if (pollfd == sock) {
940 /* Event on the consumerd socket */
03e43155
MD
941 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)
942 && !(revents & LPOLLIN)) {
331744e3
JD
943 ERR("consumer err socket second poll error");
944 goto error;
945 }
946 health_code_update();
947 /* Wait for any kconsumerd error */
948 ret = lttcomm_recv_unix_sock(sock, &code,
949 sizeof(enum lttcomm_return_code));
950 if (ret <= 0) {
951 ERR("consumer closed the command socket");
952 goto error;
953 }
954
955 ERR("consumer return code : %s",
956 lttcomm_get_readable_code(-code));
957
958 goto exit;
4ce514c4 959 } else if (pollfd == consumer_data->metadata_fd) {
03e43155
MD
960 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)
961 && !(revents & LPOLLIN)) {
962 ERR("consumer err metadata socket second poll error");
963 goto error;
964 }
331744e3
JD
965 /* UST metadata requests */
966 ret = ust_consumer_metadata_request(
967 &consumer_data->metadata_sock);
968 if (ret < 0) {
969 ERR("Handling metadata request");
970 goto error;
971 }
5eb91c98 972 }
42fc1d0b 973 /* No need for an else branch all FDs are tested prior. */
5eb91c98 974 }
331744e3 975 health_code_update();
5eb91c98
DG
976 }
977
139ac872 978exit:
1d4b027a 979error:
fdadac08
DG
980 /*
981 * We lock here because we are about to close the sockets and some other
92db7cdc
DG
982 * thread might be using them so get exclusive access which will abort all
983 * other consumer command by other threads.
fdadac08
DG
984 */
985 pthread_mutex_lock(&consumer_data->lock);
986
5c827ce0
DG
987 /* Immediately set the consumerd state to stopped */
988 if (consumer_data->type == LTTNG_CONSUMER_KERNEL) {
989 uatomic_set(&kernel_consumerd_state, CONSUMER_ERROR);
990 } else if (consumer_data->type == LTTNG_CONSUMER64_UST ||
991 consumer_data->type == LTTNG_CONSUMER32_UST) {
992 uatomic_set(&ust_consumerd_state, CONSUMER_ERROR);
993 } else {
994 /* Code flow error... */
995 assert(0);
996 }
997
76d7553f
MD
998 if (consumer_data->err_sock >= 0) {
999 ret = close(consumer_data->err_sock);
1000 if (ret) {
1001 PERROR("close");
1002 }
a76cbd9f 1003 consumer_data->err_sock = -1;
76d7553f
MD
1004 }
1005 if (consumer_data->cmd_sock >= 0) {
1006 ret = close(consumer_data->cmd_sock);
1007 if (ret) {
1008 PERROR("close");
1009 }
a76cbd9f 1010 consumer_data->cmd_sock = -1;
76d7553f 1011 }
96544455
SS
1012 if (consumer_data->metadata_sock.fd_ptr &&
1013 *consumer_data->metadata_sock.fd_ptr >= 0) {
9363801e 1014 ret = close(*consumer_data->metadata_sock.fd_ptr);
331744e3
JD
1015 if (ret) {
1016 PERROR("close");
1017 }
1018 }
76d7553f
MD
1019 if (sock >= 0) {
1020 ret = close(sock);
1021 if (ret) {
1022 PERROR("close");
1023 }
1024 }
273ea72c 1025
3bd1e081
MD
1026 unlink(consumer_data->err_unix_sock_path);
1027 unlink(consumer_data->cmd_unix_sock_path);
fdadac08 1028 pthread_mutex_unlock(&consumer_data->lock);
92db7cdc 1029
fdadac08 1030 /* Cleanup metadata socket mutex. */
96544455
SS
1031 if (consumer_data->metadata_sock.lock) {
1032 pthread_mutex_destroy(consumer_data->metadata_sock.lock);
1033 free(consumer_data->metadata_sock.lock);
1034 }
5eb91c98 1035 lttng_poll_clean(&events);
b3530820
JG
1036
1037 if (cmd_socket_wrapper) {
1038 consumer_destroy_socket(cmd_socket_wrapper);
1039 }
76d7553f 1040error_poll:
139ac872 1041 if (err) {
840cb59c 1042 health_error();
139ac872
MD
1043 ERR("Health error occurred in %s", __func__);
1044 }
8782cc74 1045 health_unregister(health_sessiond);
76d7553f 1046 DBG("consumer thread cleanup completed");
0177d773 1047
34c1e15a
MD
1048 rcu_thread_offline();
1049 rcu_unregister_thread();
1050
5eb91c98 1051 return NULL;
099e26bd
DG
1052}
1053
b73401da 1054/*
096102bd 1055 * Setup necessary data for kernel tracer action.
b73401da 1056 */
096102bd 1057static int init_kernel_tracer(void)
b73401da
DG
1058{
1059 int ret;
b73401da 1060
096102bd
DG
1061 /* Modprobe lttng kernel modules */
1062 ret = modprobe_lttng_control();
b73401da 1063 if (ret < 0) {
b73401da
DG
1064 goto error;
1065 }
1066
096102bd
DG
1067 /* Open debugfs lttng */
1068 kernel_tracer_fd = open(module_proc_lttng, O_RDWR);
1069 if (kernel_tracer_fd < 0) {
1070 DBG("Failed to open %s", module_proc_lttng);
2f77fc4b 1071 goto error_open;
54d01ffb
DG
1072 }
1073
2f77fc4b 1074 /* Validate kernel version */
88076e89
JD
1075 ret = kernel_validate_version(kernel_tracer_fd, &kernel_tracer_version,
1076 &kernel_tracer_abi_version);
2f77fc4b
DG
1077 if (ret < 0) {
1078 goto error_version;
b551a063 1079 }
54d01ffb 1080
2f77fc4b
DG
1081 ret = modprobe_lttng_data();
1082 if (ret < 0) {
1083 goto error_modules;
54d01ffb
DG
1084 }
1085
6e21424e
JR
1086 ret = kernel_supports_ring_buffer_snapshot_sample_positions(
1087 kernel_tracer_fd);
1088 if (ret < 0) {
1089 goto error_modules;
1090 }
1091
1092 if (ret < 1) {
1093 WARN("Kernel tracer does not support buffer monitoring. "
1094 "The monitoring timer of channels in the kernel domain "
1095 "will be set to 0 (disabled).");
1096 }
1097
2f77fc4b
DG
1098 DBG("Kernel tracer fd %d", kernel_tracer_fd);
1099 return 0;
1100
1101error_version:
1102 modprobe_remove_lttng_control();
1103 ret = close(kernel_tracer_fd);
1104 if (ret) {
1105 PERROR("close");
b551a063 1106 }
2f77fc4b 1107 kernel_tracer_fd = -1;
f73fabfd 1108 return LTTNG_ERR_KERN_VERSION;
b551a063 1109
2f77fc4b
DG
1110error_modules:
1111 ret = close(kernel_tracer_fd);
1112 if (ret) {
1113 PERROR("close");
b551a063 1114 }
54d01ffb 1115
2f77fc4b
DG
1116error_open:
1117 modprobe_remove_lttng_control();
54d01ffb
DG
1118
1119error:
2f77fc4b
DG
1120 WARN("No kernel tracer available");
1121 kernel_tracer_fd = -1;
1122 if (!is_root) {
f73fabfd 1123 return LTTNG_ERR_NEED_ROOT_SESSIOND;
2f77fc4b 1124 } else {
f73fabfd 1125 return LTTNG_ERR_KERN_NA;
2f77fc4b 1126 }
54d01ffb
DG
1127}
1128
917a718d 1129static int string_match(const char *str1, const char *str2)
54d01ffb 1130{
917a718d 1131 return (str1 && str2) && !strcmp(str1, str2);
54d01ffb
DG
1132}
1133
00e2e675 1134/*
917a718d
JG
1135 * Take an option from the getopt output and set it in the right variable to be
1136 * used later.
36b588ed 1137 *
917a718d 1138 * Return 0 on success else a negative value.
00e2e675 1139 */
917a718d 1140static int set_option(int opt, const char *arg, const char *optname)
00e2e675 1141{
917a718d 1142 int ret = 0;
fac6795d 1143
c0232ea5 1144 if (string_match(optname, "client-sock") || opt == 'c') {
66b2ce8e
JG
1145 if (!arg || *arg == '\0') {
1146 ret = -EINVAL;
1147 goto end;
1148 }
e8fa9fb0
MD
1149 if (lttng_is_setuid_setgid()) {
1150 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1151 "-c, --client-sock");
1152 } else {
e6142f2e
JG
1153 config_string_set(&config.client_unix_sock_path,
1154 strdup(arg));
1155 if (!config.client_unix_sock_path.value) {
1156 ret = -ENOMEM;
1157 PERROR("strdup");
1158 }
e8fa9fb0 1159 }
c0232ea5 1160 } else if (string_match(optname, "apps-sock") || opt == 'a') {
66b2ce8e
JG
1161 if (!arg || *arg == '\0') {
1162 ret = -EINVAL;
1163 goto end;
1164 }
e8fa9fb0
MD
1165 if (lttng_is_setuid_setgid()) {
1166 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1167 "-a, --apps-sock");
1168 } else {
e6142f2e
JG
1169 config_string_set(&config.apps_unix_sock_path,
1170 strdup(arg));
1171 if (!config.apps_unix_sock_path.value) {
1172 ret = -ENOMEM;
1173 PERROR("strdup");
1174 }
e8fa9fb0 1175 }
c0232ea5 1176 } else if (string_match(optname, "daemonize") || opt == 'd') {
e6142f2e 1177 config.daemonize = true;
c0232ea5 1178 } else if (string_match(optname, "background") || opt == 'b') {
e6142f2e 1179 config.background = true;
c0232ea5 1180 } else if (string_match(optname, "group") || opt == 'g') {
66b2ce8e
JG
1181 if (!arg || *arg == '\0') {
1182 ret = -EINVAL;
1183 goto end;
1184 }
e8fa9fb0
MD
1185 if (lttng_is_setuid_setgid()) {
1186 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1187 "-g, --group");
1188 } else {
e6142f2e
JG
1189 config_string_set(&config.tracing_group_name,
1190 strdup(arg));
1191 if (!config.tracing_group_name.value) {
e8fa9fb0 1192 ret = -ENOMEM;
e6142f2e 1193 PERROR("strdup");
e8fa9fb0 1194 }
db322c4d 1195 }
c0232ea5 1196 } else if (string_match(optname, "help") || opt == 'h') {
4fc83d94 1197 ret = utils_show_help(8, "lttng-sessiond", help_msg);
8190767e 1198 if (ret) {
4fc83d94 1199 ERR("Cannot show --help for `lttng-sessiond`");
8190767e
PP
1200 perror("exec");
1201 }
1202 exit(ret ? EXIT_FAILURE : EXIT_SUCCESS);
c0232ea5 1203 } else if (string_match(optname, "version") || opt == 'V') {
26296c48
JG
1204 fprintf(stdout, "%s\n", VERSION);
1205 exit(EXIT_SUCCESS);
c0232ea5 1206 } else if (string_match(optname, "sig-parent") || opt == 'S') {
e6142f2e 1207 config.sig_parent = true;
c0232ea5 1208 } else if (string_match(optname, "kconsumerd-err-sock")) {
66b2ce8e
JG
1209 if (!arg || *arg == '\0') {
1210 ret = -EINVAL;
1211 goto end;
1212 }
e8fa9fb0
MD
1213 if (lttng_is_setuid_setgid()) {
1214 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1215 "--kconsumerd-err-sock");
1216 } else {
e6142f2e
JG
1217 config_string_set(&config.kconsumerd_err_unix_sock_path,
1218 strdup(arg));
1219 if (!config.kconsumerd_err_unix_sock_path.value) {
1220 ret = -ENOMEM;
1221 PERROR("strdup");
1222 }
e8fa9fb0 1223 }
c0232ea5 1224 } else if (string_match(optname, "kconsumerd-cmd-sock")) {
66b2ce8e
JG
1225 if (!arg || *arg == '\0') {
1226 ret = -EINVAL;
1227 goto end;
1228 }
e8fa9fb0
MD
1229 if (lttng_is_setuid_setgid()) {
1230 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1231 "--kconsumerd-cmd-sock");
1232 } else {
e6142f2e
JG
1233 config_string_set(&config.kconsumerd_cmd_unix_sock_path,
1234 strdup(arg));
1235 if (!config.kconsumerd_cmd_unix_sock_path.value) {
1236 ret = -ENOMEM;
1237 PERROR("strdup");
1238 }
e8fa9fb0 1239 }
c0232ea5 1240 } else if (string_match(optname, "ustconsumerd64-err-sock")) {
66b2ce8e
JG
1241 if (!arg || *arg == '\0') {
1242 ret = -EINVAL;
1243 goto end;
1244 }
e8fa9fb0
MD
1245 if (lttng_is_setuid_setgid()) {
1246 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1247 "--ustconsumerd64-err-sock");
1248 } else {
e6142f2e
JG
1249 config_string_set(&config.consumerd64_err_unix_sock_path,
1250 strdup(arg));
1251 if (!config.consumerd64_err_unix_sock_path.value) {
1252 ret = -ENOMEM;
1253 PERROR("strdup");
1254 }
e8fa9fb0 1255 }
c0232ea5 1256 } else if (string_match(optname, "ustconsumerd64-cmd-sock")) {
66b2ce8e
JG
1257 if (!arg || *arg == '\0') {
1258 ret = -EINVAL;
1259 goto end;
1260 }
e8fa9fb0
MD
1261 if (lttng_is_setuid_setgid()) {
1262 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1263 "--ustconsumerd64-cmd-sock");
1264 } else {
e6142f2e
JG
1265 config_string_set(&config.consumerd64_cmd_unix_sock_path,
1266 strdup(arg));
1267 if (!config.consumerd64_cmd_unix_sock_path.value) {
1268 ret = -ENOMEM;
1269 PERROR("strdup");
1270 }
e8fa9fb0 1271 }
c0232ea5 1272 } else if (string_match(optname, "ustconsumerd32-err-sock")) {
66b2ce8e
JG
1273 if (!arg || *arg == '\0') {
1274 ret = -EINVAL;
1275 goto end;
1276 }
e8fa9fb0
MD
1277 if (lttng_is_setuid_setgid()) {
1278 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1279 "--ustconsumerd32-err-sock");
1280 } else {
e6142f2e
JG
1281 config_string_set(&config.consumerd32_err_unix_sock_path,
1282 strdup(arg));
1283 if (!config.consumerd32_err_unix_sock_path.value) {
1284 ret = -ENOMEM;
1285 PERROR("strdup");
1286 }
e8fa9fb0 1287 }
c0232ea5 1288 } else if (string_match(optname, "ustconsumerd32-cmd-sock")) {
66b2ce8e
JG
1289 if (!arg || *arg == '\0') {
1290 ret = -EINVAL;
1291 goto end;
1292 }
e8fa9fb0
MD
1293 if (lttng_is_setuid_setgid()) {
1294 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1295 "--ustconsumerd32-cmd-sock");
1296 } else {
e6142f2e
JG
1297 config_string_set(&config.consumerd32_cmd_unix_sock_path,
1298 strdup(arg));
1299 if (!config.consumerd32_cmd_unix_sock_path.value) {
1300 ret = -ENOMEM;
1301 PERROR("strdup");
1302 }
e8fa9fb0 1303 }
c0232ea5 1304 } else if (string_match(optname, "no-kernel")) {
e6142f2e 1305 config.no_kernel = true;
c0232ea5 1306 } else if (string_match(optname, "quiet") || opt == 'q') {
6d9a9c65 1307 config.quiet = true;
c0232ea5 1308 } else if (string_match(optname, "verbose") || opt == 'v') {
26296c48
JG
1309 /* Verbose level can increase using multiple -v */
1310 if (arg) {
13755a18 1311 /* Value obtained from config file */
e6142f2e 1312 config.verbose = config_parse_value(arg);
26296c48 1313 } else {
13755a18 1314 /* -v used on command line */
e6142f2e 1315 config.verbose++;
26296c48 1316 }
13755a18 1317 /* Clamp value to [0, 3] */
e6142f2e
JG
1318 config.verbose = config.verbose < 0 ? 0 :
1319 (config.verbose <= 3 ? config.verbose : 3);
c0232ea5 1320 } else if (string_match(optname, "verbose-consumer")) {
26296c48 1321 if (arg) {
e6142f2e 1322 config.verbose_consumer = config_parse_value(arg);
26296c48 1323 } else {
e6142f2e 1324 config.verbose_consumer++;
26296c48 1325 }
c0232ea5 1326 } else if (string_match(optname, "consumerd32-path")) {
66b2ce8e
JG
1327 if (!arg || *arg == '\0') {
1328 ret = -EINVAL;
1329 goto end;
1330 }
e8fa9fb0
MD
1331 if (lttng_is_setuid_setgid()) {
1332 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1333 "--consumerd32-path");
1334 } else {
e6142f2e
JG
1335 config_string_set(&config.consumerd32_bin_path,
1336 strdup(arg));
1337 if (!config.consumerd32_bin_path.value) {
e8fa9fb0
MD
1338 PERROR("strdup");
1339 ret = -ENOMEM;
1340 }
db322c4d 1341 }
c0232ea5 1342 } else if (string_match(optname, "consumerd32-libdir")) {
66b2ce8e
JG
1343 if (!arg || *arg == '\0') {
1344 ret = -EINVAL;
1345 goto end;
1346 }
e8fa9fb0
MD
1347 if (lttng_is_setuid_setgid()) {
1348 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1349 "--consumerd32-libdir");
1350 } else {
e6142f2e
JG
1351 config_string_set(&config.consumerd32_lib_dir,
1352 strdup(arg));
1353 if (!config.consumerd32_lib_dir.value) {
e8fa9fb0
MD
1354 PERROR("strdup");
1355 ret = -ENOMEM;
1356 }
db322c4d 1357 }
c0232ea5 1358 } else if (string_match(optname, "consumerd64-path")) {
66b2ce8e
JG
1359 if (!arg || *arg == '\0') {
1360 ret = -EINVAL;
1361 goto end;
1362 }
e8fa9fb0
MD
1363 if (lttng_is_setuid_setgid()) {
1364 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1365 "--consumerd64-path");
1366 } else {
e6142f2e
JG
1367 config_string_set(&config.consumerd64_bin_path,
1368 strdup(arg));
1369 if (!config.consumerd64_bin_path.value) {
e8fa9fb0
MD
1370 PERROR("strdup");
1371 ret = -ENOMEM;
1372 }
db322c4d 1373 }
c0232ea5 1374 } else if (string_match(optname, "consumerd64-libdir")) {
66b2ce8e
JG
1375 if (!arg || *arg == '\0') {
1376 ret = -EINVAL;
1377 goto end;
1378 }
e8fa9fb0
MD
1379 if (lttng_is_setuid_setgid()) {
1380 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1381 "--consumerd64-libdir");
1382 } else {
e6142f2e
JG
1383 config_string_set(&config.consumerd64_lib_dir,
1384 strdup(arg));
1385 if (!config.consumerd64_lib_dir.value) {
e8fa9fb0
MD
1386 PERROR("strdup");
1387 ret = -ENOMEM;
1388 }
db322c4d 1389 }
c0232ea5 1390 } else if (string_match(optname, "pidfile") || opt == 'p') {
66b2ce8e
JG
1391 if (!arg || *arg == '\0') {
1392 ret = -EINVAL;
1393 goto end;
1394 }
e8fa9fb0
MD
1395 if (lttng_is_setuid_setgid()) {
1396 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1397 "-p, --pidfile");
1398 } else {
e6142f2e
JG
1399 config_string_set(&config.pid_file_path, strdup(arg));
1400 if (!config.pid_file_path.value) {
e8fa9fb0
MD
1401 PERROR("strdup");
1402 ret = -ENOMEM;
1403 }
db322c4d 1404 }
c0232ea5 1405 } else if (string_match(optname, "agent-tcp-port")) {
66b2ce8e
JG
1406 if (!arg || *arg == '\0') {
1407 ret = -EINVAL;
1408 goto end;
1409 }
e8fa9fb0
MD
1410 if (lttng_is_setuid_setgid()) {
1411 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1412 "--agent-tcp-port");
1413 } else {
1414 unsigned long v;
4d076222 1415
e8fa9fb0
MD
1416 errno = 0;
1417 v = strtoul(arg, NULL, 0);
1418 if (errno != 0 || !isdigit(arg[0])) {
1419 ERR("Wrong value in --agent-tcp-port parameter: %s", arg);
1420 return -1;
1421 }
1422 if (v == 0 || v >= 65535) {
1423 ERR("Port overflow in --agent-tcp-port parameter: %s", arg);
1424 return -1;
1425 }
2288467f
JG
1426 config.agent_tcp_port.begin = config.agent_tcp_port.end = (int) v;
1427 DBG3("Agent TCP port set to non default: %i", (int) v);
26296c48 1428 }
c0232ea5 1429 } else if (string_match(optname, "load") || opt == 'l') {
66b2ce8e
JG
1430 if (!arg || *arg == '\0') {
1431 ret = -EINVAL;
1432 goto end;
1433 }
e8fa9fb0
MD
1434 if (lttng_is_setuid_setgid()) {
1435 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1436 "-l, --load");
1437 } else {
e6142f2e
JG
1438 config_string_set(&config.load_session_path, strdup(arg));
1439 if (!config.load_session_path.value) {
e8fa9fb0
MD
1440 PERROR("strdup");
1441 ret = -ENOMEM;
1442 }
ef367a93 1443 }
c0232ea5 1444 } else if (string_match(optname, "kmod-probes")) {
66b2ce8e
JG
1445 if (!arg || *arg == '\0') {
1446 ret = -EINVAL;
1447 goto end;
1448 }
e8fa9fb0
MD
1449 if (lttng_is_setuid_setgid()) {
1450 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1451 "--kmod-probes");
1452 } else {
e6142f2e
JG
1453 config_string_set(&config.kmod_probes_list, strdup(arg));
1454 if (!config.kmod_probes_list.value) {
e8fa9fb0
MD
1455 PERROR("strdup");
1456 ret = -ENOMEM;
1457 }
c9d42407 1458 }
c0232ea5 1459 } else if (string_match(optname, "extra-kmod-probes")) {
66b2ce8e
JG
1460 if (!arg || *arg == '\0') {
1461 ret = -EINVAL;
1462 goto end;
1463 }
e8fa9fb0
MD
1464 if (lttng_is_setuid_setgid()) {
1465 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1466 "--extra-kmod-probes");
1467 } else {
e6142f2e
JG
1468 config_string_set(&config.kmod_extra_probes_list,
1469 strdup(arg));
1470 if (!config.kmod_extra_probes_list.value) {
e8fa9fb0
MD
1471 PERROR("strdup");
1472 ret = -ENOMEM;
1473 }
db322c4d 1474 }
c0232ea5
JG
1475 } else if (string_match(optname, "config") || opt == 'f') {
1476 /* This is handled in set_options() thus silent skip. */
1477 goto end;
1478 } else {
26296c48
JG
1479 /* Unknown option or other error.
1480 * Error is printed by getopt, just return */
1481 ret = -1;
1482 }
1483
f3dd7ce7 1484end:
c5d350b2
JG
1485 if (ret == -EINVAL) {
1486 const char *opt_name = "unknown";
1487 int i;
1488
1489 for (i = 0; i < sizeof(long_options) / sizeof(struct option);
1490 i++) {
1491 if (opt == long_options[i].val) {
1492 opt_name = long_options[i].name;
1493 break;
1494 }
1495 }
1496
1497 WARN("Invalid argument provided for option \"%s\", using default value.",
1498 opt_name);
1499 }
f3dd7ce7 1500
26296c48
JG
1501 return ret;
1502}
1503
1504/*
1505 * config_entry_handler_cb used to handle options read from a config file.
f40ef1d5 1506 * See config_entry_handler_cb comment in common/config/session-config.h for the
26296c48
JG
1507 * return value conventions.
1508 */
1509static int config_entry_handler(const struct config_entry *entry, void *unused)
1510{
1511 int ret = 0, i;
1512
1513 if (!entry || !entry->name || !entry->value) {
1514 ret = -EINVAL;
1515 goto end;
1516 }
1517
1518 /* Check if the option is to be ignored */
1519 for (i = 0; i < sizeof(config_ignore_options) / sizeof(char *); i++) {
1520 if (!strcmp(entry->name, config_ignore_options[i])) {
1521 goto end;
1522 }
1523 }
1524
1525 for (i = 0; i < (sizeof(long_options) / sizeof(struct option)) - 1;
1526 i++) {
1527
1528 /* Ignore if not fully matched. */
1529 if (strcmp(entry->name, long_options[i].name)) {
1530 continue;
1531 }
1532
1533 /*
1534 * If the option takes no argument on the command line, we have to
1535 * check if the value is "true". We support non-zero numeric values,
1536 * true, on and yes.
1537 */
1538 if (!long_options[i].has_arg) {
1539 ret = config_parse_value(entry->value);
1540 if (ret <= 0) {
1541 if (ret) {
1542 WARN("Invalid configuration value \"%s\" for option %s",
1543 entry->value, entry->name);
1544 }
1545 /* False, skip boolean config option. */
1546 goto end;
4d076222 1547 }
26296c48
JG
1548 }
1549
1550 ret = set_option(long_options[i].val, entry->value, entry->name);
1551 goto end;
1552 }
1553
1554 WARN("Unrecognized option \"%s\" in daemon configuration file.", entry->name);
1555
1556end:
1557 return ret;
1558}
1559
1560/*
1561 * daemon configuration loading and argument parsing
1562 */
1563static int set_options(int argc, char **argv)
1564{
1565 int ret = 0, c = 0, option_index = 0;
1566 int orig_optopt = optopt, orig_optind = optind;
1567 char *optstring;
1568 const char *config_path = NULL;
1569
1570 optstring = utils_generate_optstring(long_options,
1571 sizeof(long_options) / sizeof(struct option));
1572 if (!optstring) {
1573 ret = -ENOMEM;
1574 goto end;
1575 }
1576
1577 /* Check for the --config option */
1578 while ((c = getopt_long(argc, argv, optstring, long_options,
1579 &option_index)) != -1) {
1580 if (c == '?') {
1581 ret = -EINVAL;
1582 goto end;
1583 } else if (c != 'f') {
1584 /* if not equal to --config option. */
1585 continue;
1586 }
1587
e8fa9fb0
MD
1588 if (lttng_is_setuid_setgid()) {
1589 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1590 "-f, --config");
1591 } else {
1592 config_path = utils_expand_path(optarg);
1593 if (!config_path) {
1594 ERR("Failed to resolve path: %s", optarg);
1595 }
26296c48
JG
1596 }
1597 }
1598
1599 ret = config_get_section_entries(config_path, config_section_name,
1600 config_entry_handler, NULL);
1601 if (ret) {
1602 if (ret > 0) {
1603 ERR("Invalid configuration option at line %i", ret);
1604 ret = -1;
1605 }
1606 goto end;
1607 }
1608
1609 /* Reset getopt's global state */
1610 optopt = orig_optopt;
1611 optind = orig_optind;
1612 while (1) {
c0232ea5
JG
1613 option_index = -1;
1614 /*
1615 * getopt_long() will not set option_index if it encounters a
1616 * short option.
1617 */
1618 c = getopt_long(argc, argv, optstring, long_options,
1619 &option_index);
26296c48 1620 if (c == -1) {
4d076222
DG
1621 break;
1622 }
26296c48 1623
c0232ea5
JG
1624 /*
1625 * Pass NULL as the long option name if popt left the index
1626 * unset.
1627 */
1628 ret = set_option(c, optarg,
1629 option_index < 0 ? NULL :
1630 long_options[option_index].name);
26296c48
JG
1631 if (ret < 0) {
1632 break;
fac6795d
DG
1633 }
1634 }
1635
26296c48
JG
1636end:
1637 free(optstring);
1638 return ret;
fac6795d
DG
1639}
1640
f472090a
JG
1641/*
1642 * Create lockfile using the rundir and return its fd.
1643 */
1644static int create_lockfile(void)
1645{
1646 return utils_create_lock_file(config.lock_file_path.value);
1647}
1648
fac6795d 1649/*
54d01ffb
DG
1650 * Check if the global socket is available, and if a daemon is answering at the
1651 * other side. If yes, error is returned.
f472090a
JG
1652 *
1653 * Also attempts to create and hold the lock file.
fac6795d 1654 */
cf3af59e 1655static int check_existing_daemon(void)
fac6795d 1656{
f472090a
JG
1657 int ret = 0;
1658
7d8234d9 1659 /* Is there anybody out there ? */
099e26bd 1660 if (lttng_session_daemon_alive()) {
f472090a
JG
1661 ret = -EEXIST;
1662 goto end;
099e26bd 1663 }
b09c7c76 1664
f472090a
JG
1665 lockfile_fd = create_lockfile();
1666 if (lockfile_fd < 0) {
1667 ret = -EEXIST;
1668 goto end;
1669 }
1670end:
1671 return ret;
1672}
1673
1674static void sessiond_cleanup_lock_file(void)
1675{
1676 int ret;
1677
1678 /*
1679 * Cleanup lock file by deleting it and finaly closing it which will
1680 * release the file system lock.
1681 */
1682 if (lockfile_fd >= 0) {
1683 ret = remove(config.lock_file_path.value);
1684 if (ret < 0) {
1685 PERROR("remove lock file");
1686 }
1687 ret = close(lockfile_fd);
1688 if (ret < 0) {
1689 PERROR("close lock file");
1690 }
1691 }
fac6795d
DG
1692}
1693
fac6795d 1694/*
d063d709 1695 * Set the tracing group gid onto the client socket.
5e16da05 1696 *
d063d709 1697 * Race window between mkdir and chown is OK because we are going from more
d1613cf5 1698 * permissive (root.root) to less permissive (root.tracing).
fac6795d 1699 */
be040666 1700static int set_permissions(char *rundir)
fac6795d
DG
1701{
1702 int ret;
996b65c8 1703 gid_t gid;
fac6795d 1704
e6142f2e 1705 gid = utils_get_group_id(config.tracing_group_name.value);
fac6795d 1706
d6f42150 1707 /* Set lttng run dir */
be040666 1708 ret = chown(rundir, 0, gid);
d6f42150 1709 if (ret < 0) {
be040666 1710 ERR("Unable to set group on %s", rundir);
76d7553f 1711 PERROR("chown");
d6f42150
DG
1712 }
1713
6c71277b
MD
1714 /*
1715 * Ensure all applications and tracing group can search the run
1716 * dir. Allow everyone to read the directory, since it does not
1717 * buy us anything to hide its content.
1718 */
1719 ret = chmod(rundir, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
d1613cf5
JN
1720 if (ret < 0) {
1721 ERR("Unable to set permissions on %s", rundir);
76d7553f 1722 PERROR("chmod");
d1613cf5
JN
1723 }
1724
d6f42150 1725 /* lttng client socket path */
e6142f2e 1726 ret = chown(config.client_unix_sock_path.value, 0, gid);
fac6795d 1727 if (ret < 0) {
e6142f2e 1728 ERR("Unable to set group on %s", config.client_unix_sock_path.value);
76d7553f 1729 PERROR("chown");
d6f42150
DG
1730 }
1731
3bd1e081 1732 /* kconsumer error socket path */
6c71277b 1733 ret = chown(kconsumer_data.err_unix_sock_path, 0, 0);
d6f42150 1734 if (ret < 0) {
3bd1e081 1735 ERR("Unable to set group on %s", kconsumer_data.err_unix_sock_path);
76d7553f 1736 PERROR("chown");
3bd1e081
MD
1737 }
1738
7753dea8 1739 /* 64-bit ustconsumer error socket path */
6c71277b 1740 ret = chown(ustconsumer64_data.err_unix_sock_path, 0, 0);
7753dea8
MD
1741 if (ret < 0) {
1742 ERR("Unable to set group on %s", ustconsumer64_data.err_unix_sock_path);
76d7553f 1743 PERROR("chown");
7753dea8
MD
1744 }
1745
1746 /* 32-bit ustconsumer compat32 error socket path */
6c71277b 1747 ret = chown(ustconsumer32_data.err_unix_sock_path, 0, 0);
3bd1e081 1748 if (ret < 0) {
7753dea8 1749 ERR("Unable to set group on %s", ustconsumer32_data.err_unix_sock_path);
76d7553f 1750 PERROR("chown");
fac6795d
DG
1751 }
1752
d6f42150 1753 DBG("All permissions are set");
e07ae692 1754
fac6795d
DG
1755 return ret;
1756}
1757
d6f42150 1758/*
d063d709 1759 * Create the lttng run directory needed for all global sockets and pipe.
d6f42150 1760 */
e6142f2e 1761static int create_lttng_rundir(void)
d6f42150
DG
1762{
1763 int ret;
1764
e6142f2e 1765 DBG3("Creating LTTng run directory: %s", config.rundir.value);
67e40797 1766
e6142f2e 1767 ret = mkdir(config.rundir.value, S_IRWXU);
d6f42150 1768 if (ret < 0) {
b1f11e69 1769 if (errno != EEXIST) {
e6142f2e 1770 ERR("Unable to create %s", config.rundir.value);
b1f11e69
DG
1771 goto error;
1772 } else {
1773 ret = 0;
1774 }
d6f42150
DG
1775 }
1776
1777error:
1778 return ret;
1779}
1780
1781/*
e6142f2e 1782 * Setup sockets and directory needed by the consumerds' communication with the
d063d709 1783 * session daemon.
d6f42150 1784 */
e6142f2e 1785static int set_consumer_sockets(struct consumer_data *consumer_data)
d6f42150
DG
1786{
1787 int ret;
e6142f2e 1788 char *path = NULL;
d6f42150 1789
6c71277b 1790 switch (consumer_data->type) {
7753dea8 1791 case LTTNG_CONSUMER_KERNEL:
e6142f2e 1792 path = config.kconsumerd_path.value;
7753dea8
MD
1793 break;
1794 case LTTNG_CONSUMER64_UST:
e6142f2e 1795 path = config.consumerd64_path.value;
7753dea8
MD
1796 break;
1797 case LTTNG_CONSUMER32_UST:
e6142f2e 1798 path = config.consumerd32_path.value;
7753dea8
MD
1799 break;
1800 default:
1801 ERR("Consumer type unknown");
1802 ret = -EINVAL;
1803 goto error;
d6f42150 1804 }
e6142f2e 1805 assert(path);
d6f42150 1806
67e40797
DG
1807 DBG2("Creating consumer directory: %s", path);
1808
6c71277b 1809 ret = mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP);
e7e1ef17
JR
1810 if (ret < 0 && errno != EEXIST) {
1811 PERROR("mkdir");
1812 ERR("Failed to create %s", path);
1813 goto error;
d6f42150 1814 }
6c71277b 1815 if (is_root) {
e6142f2e 1816 ret = chown(path, 0, utils_get_group_id(config.tracing_group_name.value));
6c71277b
MD
1817 if (ret < 0) {
1818 ERR("Unable to set group on %s", path);
1819 PERROR("chown");
1820 goto error;
1821 }
1822 }
d6f42150 1823
e6142f2e 1824 /* Create the consumerd error unix socket */
3bd1e081
MD
1825 consumer_data->err_sock =
1826 lttcomm_create_unix_sock(consumer_data->err_unix_sock_path);
1827 if (consumer_data->err_sock < 0) {
1828 ERR("Create unix sock failed: %s", consumer_data->err_unix_sock_path);
d6f42150
DG
1829 ret = -1;
1830 goto error;
1831 }
1832
a24f05ab
MD
1833 /*
1834 * Set the CLOEXEC flag. Return code is useless because either way, the
1835 * show must go on.
1836 */
1837 ret = utils_set_fd_cloexec(consumer_data->err_sock);
1838 if (ret < 0) {
1839 PERROR("utils_set_fd_cloexec");
1840 /* continue anyway */
1841 }
1842
d6f42150 1843 /* File permission MUST be 660 */
3bd1e081 1844 ret = chmod(consumer_data->err_unix_sock_path,
54d01ffb 1845 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
d6f42150 1846 if (ret < 0) {
3bd1e081 1847 ERR("Set file permissions failed: %s", consumer_data->err_unix_sock_path);
67e40797 1848 PERROR("chmod");
d6f42150
DG
1849 goto error;
1850 }
1851
1852error:
1853 return ret;
1854}
1855
fac6795d 1856/*
d063d709 1857 * Signal handler for the daemon
cf3af59e 1858 *
54d01ffb
DG
1859 * Simply stop all worker threads, leaving main() return gracefully after
1860 * joining all threads and calling cleanup().
fac6795d
DG
1861 */
1862static void sighandler(int sig)
1863{
1864 switch (sig) {
cf3af59e 1865 case SIGINT:
af87c45a 1866 DBG("SIGINT caught");
cf3af59e
MD
1867 stop_threads();
1868 break;
1869 case SIGTERM:
af87c45a 1870 DBG("SIGTERM caught");
cf3af59e
MD
1871 stop_threads();
1872 break;
0bb7724a
DG
1873 case SIGUSR1:
1874 CMM_STORE_SHARED(recv_child_signal, 1);
1875 break;
cf3af59e
MD
1876 default:
1877 break;
fac6795d 1878 }
fac6795d
DG
1879}
1880
1881/*
d063d709 1882 * Setup signal handler for :
1d4b027a 1883 * SIGINT, SIGTERM, SIGPIPE
fac6795d 1884 */
1d4b027a 1885static int set_signal_handler(void)
fac6795d 1886{
1d4b027a
DG
1887 int ret = 0;
1888 struct sigaction sa;
1889 sigset_t sigset;
fac6795d 1890
1d4b027a 1891 if ((ret = sigemptyset(&sigset)) < 0) {
76d7553f 1892 PERROR("sigemptyset");
1d4b027a
DG
1893 return ret;
1894 }
d6f42150 1895
1d4b027a
DG
1896 sa.sa_mask = sigset;
1897 sa.sa_flags = 0;
0072e5e2
MD
1898
1899 sa.sa_handler = sighandler;
1d4b027a 1900 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
76d7553f 1901 PERROR("sigaction");
1d4b027a 1902 return ret;
d6f42150
DG
1903 }
1904
1d4b027a 1905 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
76d7553f 1906 PERROR("sigaction");
1d4b027a 1907 return ret;
d6f42150 1908 }
aaf26714 1909
0072e5e2 1910 if ((ret = sigaction(SIGUSR1, &sa, NULL)) < 0) {
76d7553f 1911 PERROR("sigaction");
1d4b027a 1912 return ret;
8c0faa1d
DG
1913 }
1914
0072e5e2
MD
1915 sa.sa_handler = SIG_IGN;
1916 if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) {
0bb7724a
DG
1917 PERROR("sigaction");
1918 return ret;
1919 }
1920
1921 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
1d4b027a
DG
1922
1923 return ret;
fac6795d
DG
1924}
1925
f3ed775e 1926/*
d063d709 1927 * Set open files limit to unlimited. This daemon can open a large number of
514bb9f0 1928 * file descriptors in order to consume multiple kernel traces.
f3ed775e
DG
1929 */
1930static void set_ulimit(void)
1931{
1932 int ret;
1933 struct rlimit lim;
1934
514bb9f0 1935 /* The kernel does not allow an infinite limit for open files */
f3ed775e
DG
1936 lim.rlim_cur = 65535;
1937 lim.rlim_max = 65535;
1938
1939 ret = setrlimit(RLIMIT_NOFILE, &lim);
1940 if (ret < 0) {
76d7553f 1941 PERROR("failed to set open files limit");
f3ed775e
DG
1942 }
1943}
1944
4a15001e 1945static int write_pidfile(void)
35f90c40 1946{
e6142f2e 1947 return utils_create_pid_file(getpid(), config.pid_file_path.value);
35f90c40
DG
1948}
1949
e6142f2e
JG
1950static int set_clock_plugin_env(void)
1951{
1952 int ret = 0;
1953 char *env_value = NULL;
cd9290dd 1954
e6142f2e
JG
1955 if (!config.lttng_ust_clock_plugin.value) {
1956 goto end;
1957 }
1958
1959 ret = asprintf(&env_value, "LTTNG_UST_CLOCK_PLUGIN=%s",
1960 config.lttng_ust_clock_plugin.value);
cd9290dd 1961 if (ret < 0) {
e6142f2e
JG
1962 PERROR("asprintf");
1963 goto end;
cd9290dd
DG
1964 }
1965
e6142f2e
JG
1966 ret = putenv(env_value);
1967 if (ret) {
1968 free(env_value);
1969 PERROR("putenv of LTTNG_UST_CLOCK_PLUGIN");
1970 goto end;
1971 }
cd9290dd 1972
e6142f2e
JG
1973 DBG("Updated LTTNG_UST_CLOCK_PLUGIN environment variable to \"%s\"",
1974 config.lttng_ust_clock_plugin.value);
1975end:
4a15001e 1976 return ret;
ef367a93
JG
1977}
1978
99d688f2
JG
1979static void destroy_all_sessions_and_wait(void)
1980{
1981 struct ltt_session *session, *tmp;
1982 struct ltt_session_list *session_list;
1983
1984 session_list = session_get_list();
1985 DBG("Initiating destruction of all sessions");
1986
1987 if (!session_list) {
1988 return;
1989 }
1990
99d688f2
JG
1991 session_lock_list();
1992 /* Initiate the destruction of all sessions. */
1993 cds_list_for_each_entry_safe(session, tmp,
1994 &session_list->head, list) {
1995 if (!session_get(session)) {
1996 continue;
1997 }
1998
1999 session_lock(session);
2000 if (session->destroyed) {
2001 goto unlock_session;
2002 }
2003 (void) cmd_destroy_session(session,
2004 notification_thread_handle);
2005 unlock_session:
2006 session_unlock(session);
2007 session_put(session);
2008 }
2009 session_unlock_list();
2010
2011 /* Wait for the destruction of all sessions to complete. */
2012 DBG("Waiting for the destruction of all sessions to complete");
2013 session_list_wait_empty();
2014 DBG("Destruction of all sessions completed");
2015}
2016
fac6795d
DG
2017/*
2018 * main
2019 */
2020int main(int argc, char **argv)
2021{
4a15001e 2022 int ret = 0, retval = 0;
fac6795d 2023 void *status;
e6142f2e 2024 const char *env_app_timeout;
b3530820
JG
2025 struct lttng_pipe *ust32_channel_monitor_pipe = NULL,
2026 *ust64_channel_monitor_pipe = NULL,
2027 *kernel_channel_monitor_pipe = NULL;
a3707772 2028 struct lttng_thread *ht_cleanup_thread = NULL;
bc26e826 2029 struct timer_thread_parameters timer_thread_parameters;
64d9b072
JG
2030 /* Rotation thread handle. */
2031 struct rotation_thread_handle *rotation_thread_handle = NULL;
d086f507
JD
2032 /* Queue of rotation jobs populated by the sessiond-timer. */
2033 struct rotation_thread_timer_queue *rotation_timer_queue = NULL;
917a718d 2034 struct lttng_thread *client_thread = NULL;
fac6795d 2035
335a95b7
MD
2036 init_kernel_workarounds();
2037
f6a9efaa
DG
2038 rcu_register_thread();
2039
4a15001e
MD
2040 if (set_signal_handler()) {
2041 retval = -1;
2042 goto exit_set_signal_handler;
0bb7724a
DG
2043 }
2044
92816cc3 2045 if (timer_signal_init()) {
d086f507
JD
2046 retval = -1;
2047 goto exit_set_signal_handler;
2048 }
2049
12744796
DG
2050 page_size = sysconf(_SC_PAGESIZE);
2051 if (page_size < 0) {
2052 PERROR("sysconf _SC_PAGESIZE");
2053 page_size = LONG_MAX;
2054 WARN("Fallback page size to %ld", page_size);
2055 }
2056
e6142f2e
JG
2057 ret = sessiond_config_init(&config);
2058 if (ret) {
2059 retval = -1;
2060 goto exit_set_signal_handler;
2061 }
2062
2788b494
JR
2063 /*
2064 * Init config from environment variables.
2065 * Command line option override env configuration per-doc. Do env first.
2066 */
2067 sessiond_config_apply_env_config(&config);
2068
4a15001e
MD
2069 /*
2070 * Parse arguments and load the daemon configuration file.
2071 *
2072 * We have an exit_options exit path to free memory reserved by
2073 * set_options. This is needed because the rest of sessiond_cleanup()
2074 * depends on ht_cleanup_thread, which depends on lttng_daemonize, which
2075 * depends on set_options.
2076 */
fac6795d 2077 progname = argv[0];
4a15001e
MD
2078 if (set_options(argc, argv)) {
2079 retval = -1;
2080 goto exit_options;
fac6795d
DG
2081 }
2082
e6142f2e
JG
2083 /*
2084 * Resolve all paths received as arguments, configuration option, or
2085 * through environment variable as absolute paths. This is necessary
2086 * since daemonizing causes the sessiond's current working directory
2087 * to '/'.
2088 */
2089 ret = sessiond_config_resolve_paths(&config);
2090 if (ret) {
2091 goto exit_options;
2092 }
2093
2094 /* Apply config. */
2095 lttng_opt_verbose = config.verbose;
2096 lttng_opt_quiet = config.quiet;
2097 kconsumer_data.err_unix_sock_path =
2098 config.kconsumerd_err_unix_sock_path.value;
2099 kconsumer_data.cmd_unix_sock_path =
2100 config.kconsumerd_cmd_unix_sock_path.value;
2101 ustconsumer32_data.err_unix_sock_path =
2102 config.consumerd32_err_unix_sock_path.value;
2103 ustconsumer32_data.cmd_unix_sock_path =
2104 config.consumerd32_cmd_unix_sock_path.value;
2105 ustconsumer64_data.err_unix_sock_path =
2106 config.consumerd64_err_unix_sock_path.value;
2107 ustconsumer64_data.cmd_unix_sock_path =
2108 config.consumerd64_cmd_unix_sock_path.value;
2109 set_clock_plugin_env();
2110
2111 sessiond_config_log(&config);
2112
f472090a
JG
2113 if (create_lttng_rundir()) {
2114 retval = -1;
2115 goto exit_options;
2116 }
2117
2118 /* Abort launch if a session daemon is already running. */
2119 if (check_existing_daemon()) {
2120 ERR("A session daemon is already running.");
2121 retval = -1;
2122 goto exit_options;
2123 }
2124
fac6795d 2125 /* Daemonize */
e6142f2e 2126 if (config.daemonize || config.background) {
ceed52b5
MD
2127 int i;
2128
72dd7491 2129 ret = lttng_daemonize(&child_ppid, &recv_child_signal,
e6142f2e 2130 !config.background);
53094c05 2131 if (ret < 0) {
4a15001e
MD
2132 retval = -1;
2133 goto exit_options;
53094c05 2134 }
0bb7724a 2135
ceed52b5 2136 /*
0bb7724a 2137 * We are in the child. Make sure all other file descriptors are
4a15001e 2138 * closed, in case we are called with more opened file
f472090a 2139 * descriptors than the standard ones and the lock file.
ceed52b5
MD
2140 */
2141 for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) {
f472090a
JG
2142 if (i == lockfile_fd) {
2143 continue;
2144 }
ceed52b5
MD
2145 (void) close(i);
2146 }
2147 }
2148
7567352f
MD
2149 if (run_as_create_worker(argv[0]) < 0) {
2150 goto exit_create_run_as_worker_cleanup;
2151 }
2152
4a15001e
MD
2153 /*
2154 * Starting from here, we can create threads. This needs to be after
2155 * lttng_daemonize due to RCU.
2156 */
2157
2158 /*
2159 * Initialize the health check subsystem. This call should set the
2160 * appropriate time values.
2161 */
2162 health_sessiond = health_app_create(NR_HEALTH_SESSIOND_TYPES);
2163 if (!health_sessiond) {
2164 PERROR("health_app_create error");
2165 retval = -1;
2166 goto exit_health_sessiond_cleanup;
2167 }
2168
4a15001e 2169 /* Create thread to clean up RCU hash tables */
a3707772
JG
2170 ht_cleanup_thread = launch_ht_cleanup_thread();
2171 if (!ht_cleanup_thread) {
4a15001e
MD
2172 retval = -1;
2173 goto exit_ht_cleanup;
2174 }
2175
ceed52b5 2176 /* Create thread quit pipe */
a7333da7 2177 if (sessiond_init_thread_quit_pipe()) {
4a15001e
MD
2178 retval = -1;
2179 goto exit_init_data;
fac6795d
DG
2180 }
2181
2182 /* Check if daemon is UID = 0 */
2183 is_root = !getuid();
fac6795d 2184 if (is_root) {
67e40797 2185 /* Create global run dir with root access */
67e40797 2186
b3530820
JG
2187 kernel_channel_monitor_pipe = lttng_pipe_open(0);
2188 if (!kernel_channel_monitor_pipe) {
2189 ERR("Failed to create kernel consumer channel monitor pipe");
2190 retval = -1;
2191 goto exit_init_data;
2192 }
2193 kconsumer_data.channel_monitor_pipe =
2194 lttng_pipe_release_writefd(
2195 kernel_channel_monitor_pipe);
2196 if (kconsumer_data.channel_monitor_pipe < 0) {
2197 retval = -1;
2198 goto exit_init_data;
2199 }
fac6795d
DG
2200 }
2201
5c827ce0
DG
2202 /* Set consumer initial state */
2203 kernel_consumerd_state = CONSUMER_STOPPED;
2204 ust_consumerd_state = CONSUMER_STOPPED;
2205
b3530820
JG
2206 ust32_channel_monitor_pipe = lttng_pipe_open(0);
2207 if (!ust32_channel_monitor_pipe) {
2208 ERR("Failed to create 32-bit user space consumer channel monitor pipe");
2209 retval = -1;
2210 goto exit_init_data;
2211 }
2212 ustconsumer32_data.channel_monitor_pipe = lttng_pipe_release_writefd(
2213 ust32_channel_monitor_pipe);
2214 if (ustconsumer32_data.channel_monitor_pipe < 0) {
2215 retval = -1;
2216 goto exit_init_data;
2217 }
62c43103 2218
d086f507 2219 /*
92816cc3
JG
2220 * The rotation_thread_timer_queue structure is shared between the
2221 * sessiond timer thread and the rotation thread. The main thread keeps
2222 * its ownership and destroys it when both threads have been joined.
d086f507 2223 */
92816cc3 2224 rotation_timer_queue = rotation_thread_timer_queue_create();
d086f507
JD
2225 if (!rotation_timer_queue) {
2226 retval = -1;
2227 goto exit_init_data;
2228 }
bc26e826
JG
2229 timer_thread_parameters.rotation_thread_job_queue =
2230 rotation_timer_queue;
67e40797 2231
b3530820
JG
2232 ust64_channel_monitor_pipe = lttng_pipe_open(0);
2233 if (!ust64_channel_monitor_pipe) {
2234 ERR("Failed to create 64-bit user space consumer channel monitor pipe");
2235 retval = -1;
2236 goto exit_init_data;
2237 }
2238 ustconsumer64_data.channel_monitor_pipe = lttng_pipe_release_writefd(
2239 ust64_channel_monitor_pipe);
2240 if (ustconsumer64_data.channel_monitor_pipe < 0) {
2241 retval = -1;
2242 goto exit_init_data;
2243 }
847177cd 2244
1427f9b2
DG
2245 /*
2246 * Init UST app hash table. Alloc hash table before this point since
2247 * cleanup() can get called after that point.
2248 */
4a15001e 2249 if (ust_app_ht_alloc()) {
ddbeb0f6 2250 ERR("Failed to allocate UST app hash table");
4a15001e
MD
2251 retval = -1;
2252 goto exit_init_data;
2253 }
1427f9b2 2254
6a4e4039
JG
2255 /*
2256 * Initialize agent app hash table. We allocate the hash table here
2257 * since cleanup() can get called after this point.
2258 */
2259 if (agent_app_ht_alloc()) {
2260 ERR("Failed to allocate Agent app hash table");
4a15001e
MD
2261 retval = -1;
2262 goto exit_init_data;
f20baf8e
DG
2263 }
2264
a88df331
DG
2265 /*
2266 * These actions must be executed as root. We do that *after* setting up
2267 * the sockets path because we MUST make the check for another daemon using
2268 * those paths *before* trying to set the kernel consumer sockets and init
2269 * kernel tracer.
2270 */
2271 if (is_root) {
e6142f2e 2272 if (set_consumer_sockets(&kconsumer_data)) {
4a15001e
MD
2273 retval = -1;
2274 goto exit_init_data;
7753dea8
MD
2275 }
2276
a88df331 2277 /* Setup kernel tracer */
e6142f2e 2278 if (!config.no_kernel) {
4fba7219 2279 init_kernel_tracer();
89588270
DG
2280 if (kernel_tracer_fd >= 0) {
2281 ret = syscall_init_table();
2282 if (ret < 0) {
4a15001e
MD
2283 ERR("Unable to populate syscall table. "
2284 "Syscall tracing won't work "
2285 "for this session daemon.");
89588270 2286 }
834978fd 2287 }
4fba7219 2288 }
a88df331
DG
2289
2290 /* Set ulimit for open files */
2291 set_ulimit();
fac6795d 2292 }
4063050c
MD
2293 /* init lttng_fd tracking must be done after set_ulimit. */
2294 lttng_fd_init();
fac6795d 2295
e6142f2e 2296 if (set_consumer_sockets(&ustconsumer64_data)) {
4a15001e
MD
2297 retval = -1;
2298 goto exit_init_data;
67e40797
DG
2299 }
2300
e6142f2e 2301 if (set_consumer_sockets(&ustconsumer32_data)) {
4a15001e
MD
2302 retval = -1;
2303 goto exit_init_data;
67e40797
DG
2304 }
2305
fac6795d 2306 /* Set credentials to socket */
e6142f2e 2307 if (is_root && set_permissions(config.rundir.value)) {
4a15001e
MD
2308 retval = -1;
2309 goto exit_init_data;
fac6795d
DG
2310 }
2311
5b8719f5 2312 /* Get parent pid if -S, --sig-parent is specified. */
e6142f2e 2313 if (config.sig_parent) {
5b8719f5
DG
2314 ppid = getppid();
2315 }
2316
7a485870 2317 /* Setup the kernel pipe for waking up the kernel thread */
e6142f2e 2318 if (is_root && !config.no_kernel) {
4a15001e
MD
2319 if (utils_create_pipe_cloexec(kernel_poll_pipe)) {
2320 retval = -1;
2321 goto exit_init_data;
6620da75 2322 }
7a485870
DG
2323 }
2324
099e26bd 2325 /* Setup the thread apps communication pipe. */
4a15001e
MD
2326 if (utils_create_pipe_cloexec(apps_cmd_pipe)) {
2327 retval = -1;
2328 goto exit_init_data;
099e26bd
DG
2329 }
2330
d0b96690 2331 /* Setup the thread apps notify communication pipe. */
4a15001e
MD
2332 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe)) {
2333 retval = -1;
2334 goto exit_init_data;
d0b96690
DG
2335 }
2336
7972aab2
DG
2337 /* Initialize global buffer per UID and PID registry. */
2338 buffer_reg_init_uid_registry();
2339 buffer_reg_init_pid_registry();
2340
099e26bd 2341 /* Init UST command queue. */
8bdee6e2 2342 cds_wfcq_init(&ust_cmd_queue.head, &ust_cmd_queue.tail);
099e26bd 2343
2f77fc4b 2344 cmd_init();
00e2e675 2345
ae9e45b3
DG
2346 /* Check for the application socket timeout env variable. */
2347 env_app_timeout = getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV);
2348 if (env_app_timeout) {
e6142f2e 2349 config.app_socket_timeout = atoi(env_app_timeout);
ae9e45b3 2350 } else {
e6142f2e 2351 config.app_socket_timeout = DEFAULT_APP_SOCKET_RW_TIMEOUT;
ae9e45b3
DG
2352 }
2353
4a15001e
MD
2354 ret = write_pidfile();
2355 if (ret) {
2356 ERR("Error in write_pidfile");
2357 retval = -1;
2358 goto exit_init_data;
2359 }
35f90c40 2360
554831e7
MD
2361 /* Initialize communication library */
2362 lttcomm_init();
4a15001e 2363 /* Initialize TCP timeout values */
d831c249 2364 lttcomm_inet_init();
554831e7 2365
ef367a93 2366 if (load_session_init_data(&load_info) < 0) {
4a15001e
MD
2367 retval = -1;
2368 goto exit_init_data;
ef367a93 2369 }
e6142f2e 2370 load_info->path = config.load_session_path.value;
ef367a93 2371
12b4fb37 2372 /* Create health-check thread. */
173fca4f 2373 if (!launch_health_management_thread()) {
4a15001e 2374 retval = -1;
44a5e5eb
DG
2375 goto exit_health;
2376 }
2377
b3530820
JG
2378 /* notification_thread_data acquires the pipes' read side. */
2379 notification_thread_handle = notification_thread_handle_create(
2380 ust32_channel_monitor_pipe,
2381 ust64_channel_monitor_pipe,
c8a9de5a 2382 kernel_channel_monitor_pipe);
b3530820
JG
2383 if (!notification_thread_handle) {
2384 retval = -1;
2385 ERR("Failed to create notification thread shared data");
b3530820
JG
2386 goto exit_notification;
2387 }
2388
2389 /* Create notification thread. */
c8a9de5a 2390 if (!launch_notification_thread(notification_thread_handle)) {
b3530820 2391 retval = -1;
b3530820
JG
2392 goto exit_notification;
2393 }
2394
d086f507 2395 /* Create timer thread. */
bc26e826 2396 if (!launch_timer_thread(&timer_thread_parameters)) {
d086f507 2397 retval = -1;
d086f507
JD
2398 goto exit_notification;
2399 }
d086f507 2400
db66e574
JD
2401 /* rotation_thread_data acquires the pipes' read side. */
2402 rotation_thread_handle = rotation_thread_handle_create(
90936dcf 2403 rotation_timer_queue,
c8a9de5a 2404 notification_thread_handle);
db66e574
JD
2405 if (!rotation_thread_handle) {
2406 retval = -1;
2407 ERR("Failed to create rotation thread shared data");
2408 stop_threads();
2409 goto exit_rotation;
2410 }
db66e574
JD
2411
2412 /* Create rotation thread. */
64d9b072 2413 if (!launch_rotation_thread(rotation_thread_handle)) {
db66e574 2414 retval = -1;
db66e574
JD
2415 goto exit_rotation;
2416 }
2417
cf3af59e 2418 /* Create thread to manage the client socket */
917a718d
JG
2419 client_thread = launch_client_thread();
2420 if (!client_thread) {
4a15001e 2421 retval = -1;
cf3af59e
MD
2422 goto exit_client;
2423 }
fac6795d 2424
5d1b0219
JG
2425 if (!launch_ust_dispatch_thread(&ust_cmd_queue, apps_cmd_pipe[1],
2426 apps_cmd_notify_pipe[1])) {
4a15001e 2427 retval = -1;
099e26bd
DG
2428 goto exit_dispatch;
2429 }
2430
2431 /* Create thread to manage application registration. */
1785d7f2 2432 if (!launch_application_registration_thread(&ust_cmd_queue)) {
4a15001e 2433 retval = -1;
099e26bd
DG
2434 goto exit_reg_apps;
2435 }
2436
cf3af59e 2437 /* Create thread to manage application socket */
7649924e 2438 if (!launch_application_management_thread(apps_cmd_pipe[0])) {
4a15001e 2439 retval = -1;
d0b96690
DG
2440 goto exit_apps;
2441 }
2442
2443 /* Create thread to manage application notify socket */
971a61c6 2444 if (!launch_application_notification_thread(apps_cmd_notify_pipe[0])) {
4a15001e 2445 retval = -1;
9563b0ad 2446 goto exit_apps_notify;
cf3af59e 2447 }
fac6795d 2448
8a7e4590
JG
2449 /* Create agent management thread. */
2450 if (!launch_agent_management_thread()) {
4a15001e 2451 retval = -1;
022d91ba 2452 goto exit_agent_reg;
4d076222
DG
2453 }
2454
6620da75 2455 /* Don't start this thread if kernel tracing is not requested nor root */
e6142f2e 2456 if (is_root && !config.no_kernel) {
6620da75 2457 /* Create kernel thread to manage kernel event */
1a1a34b4 2458 ret = pthread_create(&kernel_thread, default_pthread_attr(),
6620da75 2459 thread_manage_kernel, (void *) NULL);
4a15001e
MD
2460 if (ret) {
2461 errno = ret;
6620da75 2462 PERROR("pthread_create kernel");
4a15001e 2463 retval = -1;
b3530820 2464 stop_threads();
6620da75
DG
2465 goto exit_kernel;
2466 }
ef367a93 2467 }
7a485870 2468
4a15001e 2469 /* Create session loading thread. */
1a1a34b4
MJ
2470 ret = pthread_create(&load_session_thread, default_pthread_attr(),
2471 thread_load_session, load_info);
4a15001e
MD
2472 if (ret) {
2473 errno = ret;
2474 PERROR("pthread_create load_session_thread");
2475 retval = -1;
b3530820 2476 stop_threads();
4a15001e
MD
2477 goto exit_load_session;
2478 }
2479
2480 /*
2481 * This is where we start awaiting program completion (e.g. through
2482 * signal that asks threads to teardown).
2483 */
2484
2485 ret = pthread_join(load_session_thread, &status);
2486 if (ret) {
2487 errno = ret;
2488 PERROR("pthread_join load_session_thread");
2489 retval = -1;
2490 }
99d688f2
JG
2491
2492 /* Initiate teardown once activity occurs on the quit pipe. */
2493 sessiond_wait_for_quit_pipe(-1U);
917a718d
JG
2494
2495 /*
2496 * Ensure that the client thread is no longer accepting new commands,
2497 * which could cause new sessions to be created.
2498 */
2499 if (!lttng_thread_shutdown(client_thread)) {
2500 ERR("Failed to shutdown the client thread, continuing teardown");
2501 lttng_thread_put(client_thread);
2502 client_thread = NULL;
2503 }
2504
99d688f2 2505 destroy_all_sessions_and_wait();
4a15001e 2506exit_load_session:
ef367a93 2507
e6142f2e 2508 if (is_root && !config.no_kernel) {
6620da75 2509 ret = pthread_join(kernel_thread, &status);
4a15001e
MD
2510 if (ret) {
2511 errno = ret;
6620da75 2512 PERROR("pthread_join");
4a15001e 2513 retval = -1;
6620da75 2514 }
fac6795d 2515 }
cf3af59e 2516exit_kernel:
022d91ba 2517exit_agent_reg:
9563b0ad 2518exit_apps_notify:
cf3af59e 2519exit_apps:
099e26bd 2520exit_reg_apps:
099e26bd 2521exit_dispatch:
b3530820 2522exit_client:
db66e574 2523exit_rotation:
b3530820 2524exit_notification:
a3707772 2525 lttng_thread_list_shutdown_orphans();
b3530820 2526exit_health:
4a15001e 2527exit_init_data:
917a718d
JG
2528 if (client_thread) {
2529 lttng_thread_put(client_thread);
2530 }
2531
4d62fbf8
MD
2532 /*
2533 * Wait for all pending call_rcu work to complete before tearing
2534 * down data structures. call_rcu worker may be trying to
2535 * perform lookups in those structures.
2536 */
2537 rcu_barrier();
4a15001e
MD
2538 /*
2539 * sessiond_cleanup() is called when no other thread is running, except
2540 * the ht_cleanup thread, which is needed to destroy the hash tables.
2541 */
2542 rcu_thread_online();
2543 sessiond_cleanup();
06f525de 2544
c4d5de9d
JG
2545 /*
2546 * Ensure all prior call_rcu are done. call_rcu callbacks may push
2547 * hash tables to the ht_cleanup thread. Therefore, we ensure that
2548 * the queue is empty before shutting down the clean-up thread.
2549 */
2550 rcu_barrier();
2551
a3707772
JG
2552 if (ht_cleanup_thread) {
2553 lttng_thread_shutdown(ht_cleanup_thread);
2554 lttng_thread_put(ht_cleanup_thread);
2555 }
2556
64d9b072
JG
2557 rcu_thread_offline();
2558 rcu_unregister_thread();
2559
2560 if (rotation_thread_handle) {
2561 rotation_thread_handle_destroy(rotation_thread_handle);
2562 }
2563
d086f507
JD
2564 /*
2565 * After the rotation and timer thread have quit, we can safely destroy
2566 * the rotation_timer_queue.
2567 */
92816cc3 2568 rotation_thread_timer_queue_destroy(rotation_timer_queue);
c8a9de5a
JG
2569 /*
2570 * The teardown of the notification system is performed after the
2571 * session daemon's teardown in order to allow it to be notified
2572 * of the active session and channels at the moment of the teardown.
2573 */
2574 if (notification_thread_handle) {
2575 notification_thread_handle_destroy(notification_thread_handle);
2576 }
b3530820
JG
2577 lttng_pipe_destroy(ust32_channel_monitor_pipe);
2578 lttng_pipe_destroy(ust64_channel_monitor_pipe);
2579 lttng_pipe_destroy(kernel_channel_monitor_pipe);
0b2dc8df 2580exit_ht_cleanup:
4a15001e 2581
4a15001e
MD
2582 health_app_destroy(health_sessiond);
2583exit_health_sessiond_cleanup:
7567352f 2584exit_create_run_as_worker_cleanup:
4a15001e
MD
2585
2586exit_options:
f472090a 2587 sessiond_cleanup_lock_file();
4a15001e
MD
2588 sessiond_cleanup_options();
2589
2590exit_set_signal_handler:
2591 if (!retval) {
cf3af59e 2592 exit(EXIT_SUCCESS);
4a15001e
MD
2593 } else {
2594 exit(EXIT_FAILURE);
67e40797 2595 }
fac6795d 2596}
This page took 0.385303 seconds and 4 git commands to generate.