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