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