Launch the client management thread using lttng_thread
[lttng-tools.git] / src / bin / lttng-sessiond / main.c
CommitLineData
826d496d
MD
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
0fdd1e2c 3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
26296c48 4 * 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
fac6795d 5 *
d14d33bf
AM
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 only,
8 * as published by the Free Software Foundation.
91d76f53 9 *
d14d33bf
AM
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
91d76f53 14 *
d14d33bf
AM
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
fac6795d
DG
18 */
19
6c1c0768 20#define _LGPL_SOURCE
fac6795d
DG
21#include <getopt.h>
22#include <grp.h>
23#include <limits.h>
0bb7724a 24#include <paths.h>
fac6795d
DG
25#include <pthread.h>
26#include <signal.h>
27#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
331744e3 30#include <inttypes.h>
0fdd1e2c 31#include <sys/mman.h>
b73401da 32#include <sys/mount.h>
1e307fab 33#include <sys/resource.h>
fac6795d
DG
34#include <sys/socket.h>
35#include <sys/stat.h>
36#include <sys/types.h>
0fdd1e2c 37#include <sys/wait.h>
5c827ce0 38#include <urcu/uatomic.h>
fac6795d 39#include <unistd.h>
4f0b90ee 40#include <ctype.h>
fac6795d 41
990570ed 42#include <common/common.h>
d27c42b8 43#include <common/compat/socket.h>
e8fa9fb0 44#include <common/compat/getenv.h>
db758600
DG
45#include <common/defaults.h>
46#include <common/kernel-consumer/kernel-consumer.h>
50c8f484 47#include <common/futex.h>
00e2e675 48#include <common/relayd/relayd.h>
81b86775 49#include <common/utils.h>
3ccdf997 50#include <common/daemonize.h>
f40ef1d5 51#include <common/config/session-config.h>
dcabc190 52#include <common/dynamic-buffer.h>
76fcf151 53#include <lttng/event-internal.h>
fac6795d 54
10a8a223 55#include "lttng-sessiond.h"
7972aab2 56#include "buffer-registry.h"
54d01ffb 57#include "channel.h"
2f77fc4b 58#include "cmd.h"
00e2e675 59#include "consumer.h"
099e26bd 60#include "context.h"
54d01ffb 61#include "event.h"
4771f025 62#include "kernel.h"
f1e16794 63#include "kernel-consumer.h"
096102bd 64#include "modprobe.h"
0fdd1e2c 65#include "shm.h"
1e307fab 66#include "ust-ctl.h"
00e2e675 67#include "ust-consumer.h"
8e68d1c8 68#include "utils.h"
4063050c 69#include "fd-limit.h"
8782cc74 70#include "health-sessiond.h"
8ac94142 71#include "testpoint.h"
d0b96690 72#include "ust-thread.h"
022d91ba 73#include "agent-thread.h"
fb198a11 74#include "save.h"
ef367a93 75#include "load-session-thread.h"
b3530820
JG
76#include "notification-thread.h"
77#include "notification-thread-commands.h"
db66e574 78#include "rotation-thread.h"
0dbc2034 79#include "lttng-syscall.h"
7c1d2758 80#include "agent.h"
5e97de00 81#include "ht-cleanup.h"
e6142f2e 82#include "sessiond-config.h"
8e319828 83#include "timer.h"
a3707772 84#include "thread.h"
917a718d 85#include "client.h"
ebaeda94 86
4fc83d94
PP
87static const char *help_msg =
88#ifdef LTTNG_EMBED_HELP
89#include <lttng-sessiond.8.h>
90#else
91NULL
92#endif
93;
94
fac6795d 95const char *progname;
c9cb3e7d 96static int lockfile_fd = -1;
3bd1e081 97
0bb7724a
DG
98/* Set to 1 when a SIGUSR1 signal is received. */
99static int recv_child_signal;
100
26296c48
JG
101/* Command line options */
102static const struct option long_options[] = {
0f5ea17c
JG
103 { "client-sock", required_argument, 0, 'c' },
104 { "apps-sock", required_argument, 0, 'a' },
105 { "kconsumerd-cmd-sock", required_argument, 0, '\0' },
106 { "kconsumerd-err-sock", required_argument, 0, '\0' },
107 { "ustconsumerd32-cmd-sock", required_argument, 0, '\0' },
108 { "ustconsumerd32-err-sock", required_argument, 0, '\0' },
109 { "ustconsumerd64-cmd-sock", required_argument, 0, '\0' },
110 { "ustconsumerd64-err-sock", required_argument, 0, '\0' },
111 { "consumerd32-path", required_argument, 0, '\0' },
112 { "consumerd32-libdir", required_argument, 0, '\0' },
113 { "consumerd64-path", required_argument, 0, '\0' },
114 { "consumerd64-libdir", required_argument, 0, '\0' },
115 { "daemonize", no_argument, 0, 'd' },
116 { "background", no_argument, 0, 'b' },
117 { "sig-parent", no_argument, 0, 'S' },
118 { "help", no_argument, 0, 'h' },
119 { "group", required_argument, 0, 'g' },
120 { "version", no_argument, 0, 'V' },
121 { "quiet", no_argument, 0, 'q' },
122 { "verbose", no_argument, 0, 'v' },
123 { "verbose-consumer", no_argument, 0, '\0' },
124 { "no-kernel", no_argument, 0, '\0' },
125 { "pidfile", required_argument, 0, 'p' },
126 { "agent-tcp-port", required_argument, 0, '\0' },
127 { "config", required_argument, 0, 'f' },
128 { "load", required_argument, 0, 'l' },
129 { "kmod-probes", required_argument, 0, '\0' },
130 { "extra-kmod-probes", required_argument, 0, '\0' },
26296c48
JG
131 { NULL, 0, 0, 0 }
132};
133
134/* Command line options to ignore from configuration file */
135static const char *config_ignore_options[] = { "help", "version", "config" };
136
26c9d55e 137/* Shared between threads */
099e26bd 138static int dispatch_thread_exit;
fac6795d 139
a4b35e07 140static int apps_sock = -1;
1d4b027a 141
099e26bd
DG
142/*
143 * This pipe is used to inform the thread managing application communication
144 * that a command is queued and ready to be processed.
145 */
76d7553f 146static int apps_cmd_pipe[2] = { -1, -1 };
099e26bd 147
1d4b027a 148/* Pthread, Mutexes and Semaphores */
1d4b027a 149static pthread_t apps_thread;
d0b96690 150static pthread_t apps_notify_thread;
099e26bd 151static pthread_t reg_apps_thread;
7a485870 152static pthread_t kernel_thread;
099e26bd 153static pthread_t dispatch_thread;
022d91ba 154static pthread_t agent_reg_thread;
ef367a93 155static pthread_t load_session_thread;
5eb91c98 156
099e26bd
DG
157/*
158 * UST registration command queue. This queue is tied with a futex and uses a N
159 * wakers / 1 waiter implemented and detailed in futex.c/.h
160 *
b22c5da8
DG
161 * The thread_registration_apps and thread_dispatch_ust_registration uses this
162 * queue along with the wait/wake scheme. The thread_manage_apps receives down
163 * the line new application socket and monitors it for any I/O error or clean
164 * close that triggers an unregistration of the application.
099e26bd
DG
165 */
166static struct ust_cmd_queue ust_cmd_queue;
167
2f77fc4b
DG
168static const char *module_proc_lttng = "/proc/lttng";
169
ef367a93 170/* Load session thread information to operate. */
a7333da7 171static struct load_session_thread_data *load_info;
7c1d2758 172
97bc1426 173/*
a7333da7 174 * Section name to look for in the daemon configuration file.
97bc1426 175 */
a7333da7 176static const char * const config_section_name = "sessiond";
5e97de00 177
a7333da7
JG
178/* Am I root or not. Set to 1 if the daemon is running as root */
179static int is_root;
5eb91c98 180
099e26bd
DG
181/*
182 * Stop all threads by closing the thread quit pipe.
183 */
cf3af59e
MD
184static void stop_threads(void)
185{
5eb91c98
DG
186 int ret;
187
cf3af59e
MD
188 /* Stopping all threads */
189 DBG("Terminating all threads");
a7333da7 190 ret = sessiond_notify_quit_pipe();
5eb91c98
DG
191 if (ret < 0) {
192 ERR("write error on thread quit pipe");
193 }
194
099e26bd 195 /* Dispatch thread */
26c9d55e 196 CMM_STORE_SHARED(dispatch_thread_exit, 1);
099e26bd 197 futex_nto1_wake(&ust_cmd_queue.futex);
cf3af59e
MD
198}
199
e975f9f8
DG
200/*
201 * Close every consumer sockets.
202 */
203static void close_consumer_sockets(void)
204{
205 int ret;
206
207 if (kconsumer_data.err_sock >= 0) {
208 ret = close(kconsumer_data.err_sock);
209 if (ret < 0) {
210 PERROR("kernel consumer err_sock close");
211 }
212 }
213 if (ustconsumer32_data.err_sock >= 0) {
214 ret = close(ustconsumer32_data.err_sock);
215 if (ret < 0) {
a76cbd9f 216 PERROR("UST consumerd32 err_sock close");
e975f9f8
DG
217 }
218 }
219 if (ustconsumer64_data.err_sock >= 0) {
220 ret = close(ustconsumer64_data.err_sock);
221 if (ret < 0) {
a76cbd9f 222 PERROR("UST consumerd64 err_sock close");
e975f9f8
DG
223 }
224 }
225 if (kconsumer_data.cmd_sock >= 0) {
226 ret = close(kconsumer_data.cmd_sock);
227 if (ret < 0) {
228 PERROR("kernel consumer cmd_sock close");
229 }
230 }
231 if (ustconsumer32_data.cmd_sock >= 0) {
232 ret = close(ustconsumer32_data.cmd_sock);
233 if (ret < 0) {
a76cbd9f 234 PERROR("UST consumerd32 cmd_sock close");
e975f9f8
DG
235 }
236 }
237 if (ustconsumer64_data.cmd_sock >= 0) {
238 ret = close(ustconsumer64_data.cmd_sock);
239 if (ret < 0) {
a76cbd9f 240 PERROR("UST consumerd64 cmd_sock close");
e975f9f8
DG
241 }
242 }
b3530820
JG
243 if (kconsumer_data.channel_monitor_pipe >= 0) {
244 ret = close(kconsumer_data.channel_monitor_pipe);
245 if (ret < 0) {
246 PERROR("kernel consumer channel monitor pipe close");
247 }
248 }
249 if (ustconsumer32_data.channel_monitor_pipe >= 0) {
250 ret = close(ustconsumer32_data.channel_monitor_pipe);
251 if (ret < 0) {
252 PERROR("UST consumerd32 channel monitor pipe close");
253 }
254 }
255 if (ustconsumer64_data.channel_monitor_pipe >= 0) {
256 ret = close(ustconsumer64_data.channel_monitor_pipe);
257 if (ret < 0) {
258 PERROR("UST consumerd64 channel monitor pipe close");
259 }
260 }
e975f9f8
DG
261}
262
4e4714cb
JR
263/*
264 * Wait on consumer process termination.
265 *
266 * Need to be called with the consumer data lock held or from a context
267 * ensuring no concurrent access to data (e.g: cleanup).
268 */
269static void wait_consumer(struct consumer_data *consumer_data)
270{
271 pid_t ret;
272 int status;
273
274 if (consumer_data->pid <= 0) {
275 return;
276 }
277
278 DBG("Waiting for complete teardown of consumerd (PID: %d)",
279 consumer_data->pid);
280 ret = waitpid(consumer_data->pid, &status, 0);
281 if (ret == -1) {
282 PERROR("consumerd waitpid pid: %d", consumer_data->pid)
1640c24c 283 } else if (!WIFEXITED(status)) {
4e4714cb
JR
284 ERR("consumerd termination with error: %d",
285 WEXITSTATUS(ret));
286 }
287 consumer_data->pid = 0;
288}
289
fac6795d 290/*
4a15001e 291 * Cleanup the session daemon's data structures.
fac6795d 292 */
4a15001e 293static void sessiond_cleanup(void)
fac6795d 294{
ef599319 295 int ret;
e32d7f27 296 struct ltt_session_list *session_list = session_get_list();
fac6795d 297
4a15001e 298 DBG("Cleanup sessiond");
e07ae692 299
4e449f3f
MD
300 /*
301 * Close the thread quit pipe. It has already done its job,
302 * since we are now called.
303 */
a7333da7 304 sessiond_close_quit_pipe();
2f77fc4b 305
c9a2957d
JG
306 ret = remove(config.pid_file_path.value);
307 if (ret < 0) {
308 PERROR("remove pidfile %s", config.pid_file_path.value);
35f90c40
DG
309 }
310
e6142f2e
JG
311 DBG("Removing sessiond and consumerd content of directory %s",
312 config.rundir.value);
8c6c56c2
MD
313
314 /* sessiond */
e6142f2e
JG
315 DBG("Removing %s", config.pid_file_path.value);
316 (void) unlink(config.pid_file_path.value);
8c6c56c2 317
e6142f2e
JG
318 DBG("Removing %s", config.agent_port_file_path.value);
319 (void) unlink(config.agent_port_file_path.value);
cd9290dd 320
8c6c56c2 321 /* kconsumerd */
e6142f2e
JG
322 DBG("Removing %s", kconsumer_data.err_unix_sock_path);
323 (void) unlink(kconsumer_data.err_unix_sock_path);
324
325 DBG("Removing directory %s", config.kconsumerd_path.value);
326 (void) rmdir(config.kconsumerd_path.value);
8c6c56c2
MD
327
328 /* ust consumerd 32 */
e6142f2e
JG
329 DBG("Removing %s", config.consumerd32_err_unix_sock_path.value);
330 (void) unlink(config.consumerd32_err_unix_sock_path.value);
331
332 DBG("Removing directory %s", config.consumerd32_path.value);
333 (void) rmdir(config.consumerd32_path.value);
8c6c56c2
MD
334
335 /* ust consumerd 64 */
e6142f2e
JG
336 DBG("Removing %s", config.consumerd64_err_unix_sock_path.value);
337 (void) unlink(config.consumerd64_err_unix_sock_path.value);
338
339 DBG("Removing directory %s", config.consumerd64_path.value);
340 (void) rmdir(config.consumerd64_path.value);
5461b305 341
99d688f2 342 pthread_mutex_destroy(&session_list->lock);
273ea72c 343
4e4714cb
JR
344 wait_consumer(&kconsumer_data);
345 wait_consumer(&ustconsumer64_data);
346 wait_consumer(&ustconsumer32_data);
347
6a4e4039
JG
348 DBG("Cleaning up all agent apps");
349 agent_app_ht_clean();
350
099e26bd 351 DBG("Closing all UST sockets");
56fff090 352 ust_app_clean_list();
7972aab2 353 buffer_reg_destroy_registries();
099e26bd 354
e6142f2e 355 if (is_root && !config.no_kernel) {
4fba7219 356 DBG2("Closing kernel fd");
a4b35e07 357 if (kernel_tracer_fd >= 0) {
76d7553f
MD
358 ret = close(kernel_tracer_fd);
359 if (ret) {
360 PERROR("close");
361 }
a4b35e07 362 }
2f50c8a3 363 DBG("Unloading kernel modules");
096102bd 364 modprobe_remove_lttng_all();
834978fd 365 free(syscall_table);
2f50c8a3 366 }
2f77fc4b 367
e975f9f8
DG
368 close_consumer_sockets();
369
ef367a93
JG
370 if (load_info) {
371 load_session_destroy_data(load_info);
372 free(load_info);
373 }
374
c9cb3e7d
JG
375 /*
376 * We do NOT rmdir rundir because there are other processes
377 * using it, for instance lttng-relayd, which can start in
378 * parallel with this teardown.
379 */
4a15001e
MD
380}
381
382/*
383 * Cleanup the daemon's option data structures.
384 */
385static void sessiond_cleanup_options(void)
386{
387 DBG("Cleaning up options");
388
e6142f2e 389 sessiond_config_fini(&config);
c9cb3e7d 390
7567352f 391 run_as_destroy_worker();
fac6795d
DG
392}
393
fac6795d 394/*
0fdd1e2c 395 * Notify UST applications using the shm mmap futex.
fac6795d 396 */
0fdd1e2c 397static int notify_ust_apps(int active)
fac6795d 398{
0fdd1e2c 399 char *wait_shm_mmap;
fac6795d 400
0fdd1e2c 401 DBG("Notifying applications of session daemon state: %d", active);
e07ae692 402
0fdd1e2c 403 /* See shm.c for this call implying mmap, shm and futex calls */
e6142f2e 404 wait_shm_mmap = shm_ust_get_mmap(config.wait_shm_path.value, is_root);
0fdd1e2c 405 if (wait_shm_mmap == NULL) {
fac6795d
DG
406 goto error;
407 }
408
0fdd1e2c
DG
409 /* Wake waiting process */
410 futex_wait_update((int32_t *) wait_shm_mmap, active);
411
412 /* Apps notified successfully */
413 return 0;
fac6795d
DG
414
415error:
0fdd1e2c 416 return -1;
fac6795d
DG
417}
418
7a485870 419/*
5eb91c98 420 * Update the kernel poll set of all channel fd available over all tracing
d063d709 421 * session. Add the wakeup pipe at the end of the set.
7a485870 422 */
5eb91c98 423static int update_kernel_poll(struct lttng_poll_event *events)
7a485870 424{
5eb91c98 425 int ret;
7a485870 426 struct ltt_kernel_channel *channel;
e32d7f27
JG
427 struct ltt_session *session;
428 const struct ltt_session_list *session_list = session_get_list();
7a485870 429
5eb91c98 430 DBG("Updating kernel poll set");
7a485870 431
54d01ffb 432 session_lock_list();
e32d7f27
JG
433 cds_list_for_each_entry(session, &session_list->head, list) {
434 if (!session_get(session)) {
435 continue;
436 }
54d01ffb 437 session_lock(session);
7a485870 438 if (session->kernel_session == NULL) {
54d01ffb 439 session_unlock(session);
e32d7f27 440 session_put(session);
7a485870
DG
441 continue;
442 }
7a485870 443
54d01ffb
DG
444 cds_list_for_each_entry(channel,
445 &session->kernel_session->channel_list.head, list) {
5eb91c98
DG
446 /* Add channel fd to the kernel poll set */
447 ret = lttng_poll_add(events, channel->fd, LPOLLIN | LPOLLRDNORM);
448 if (ret < 0) {
54d01ffb 449 session_unlock(session);
e32d7f27 450 session_put(session);
5eb91c98
DG
451 goto error;
452 }
453 DBG("Channel fd %d added to kernel set", channel->fd);
7a485870 454 }
54d01ffb 455 session_unlock(session);
7a485870 456 }
54d01ffb 457 session_unlock_list();
7a485870 458
5eb91c98 459 return 0;
7a485870
DG
460
461error:
54d01ffb 462 session_unlock_list();
7a485870
DG
463 return -1;
464}
465
466/*
54d01ffb 467 * Find the channel fd from 'fd' over all tracing session. When found, check
d063d709 468 * for new channel stream and send those stream fds to the kernel consumer.
7a485870 469 *
d063d709 470 * Useful for CPU hotplug feature.
7a485870 471 */
7f032058 472static int update_kernel_stream(int fd)
7a485870
DG
473{
474 int ret = 0;
475 struct ltt_session *session;
173af62f 476 struct ltt_kernel_session *ksess;
7a485870 477 struct ltt_kernel_channel *channel;
e32d7f27 478 const struct ltt_session_list *session_list = session_get_list();
7a485870
DG
479
480 DBG("Updating kernel streams for channel fd %d", fd);
481
54d01ffb 482 session_lock_list();
e32d7f27
JG
483 cds_list_for_each_entry(session, &session_list->head, list) {
484 if (!session_get(session)) {
485 continue;
486 }
54d01ffb 487 session_lock(session);
7a485870 488 if (session->kernel_session == NULL) {
54d01ffb 489 session_unlock(session);
e32d7f27 490 session_put(session);
7a485870
DG
491 continue;
492 }
173af62f 493 ksess = session->kernel_session;
d9800920 494
4a15001e
MD
495 cds_list_for_each_entry(channel,
496 &ksess->channel_list.head, list) {
497 struct lttng_ht_iter iter;
498 struct consumer_socket *socket;
d9800920 499
4a15001e
MD
500 if (channel->fd != fd) {
501 continue;
502 }
503 DBG("Channel found, updating kernel streams");
504 ret = kernel_open_channel_stream(channel);
505 if (ret < 0) {
506 goto error;
507 }
508 /* Update the stream global counter */
509 ksess->stream_count_global += ret;
510
511 /*
512 * Have we already sent fds to the consumer? If yes, it
513 * means that tracing is started so it is safe to send
514 * our updated stream fds.
515 */
516 if (ksess->consumer_fds_sent != 1
517 || ksess->consumer == NULL) {
518 ret = -1;
519 goto error;
520 }
521
522 rcu_read_lock();
523 cds_lfht_for_each_entry(ksess->consumer->socks->ht,
524 &iter.iter, socket, node.node) {
525 pthread_mutex_lock(socket->lock);
1fc1b7c8 526 ret = kernel_consumer_send_channel_streams(socket,
4a15001e
MD
527 channel, ksess,
528 session->output_traces ? 1 : 0);
529 pthread_mutex_unlock(socket->lock);
530 if (ret < 0) {
e7fe706f 531 rcu_read_unlock();
4a15001e 532 goto error;
7a485870 533 }
7a485870 534 }
4a15001e 535 rcu_read_unlock();
7a485870 536 }
54d01ffb 537 session_unlock(session);
e32d7f27 538 session_put(session);
7a485870 539 }
54d01ffb 540 session_unlock_list();
b3c750d2 541 return ret;
7a485870 542
b3c750d2 543error:
54d01ffb 544 session_unlock(session);
e32d7f27 545 session_put(session);
54d01ffb 546 session_unlock_list();
7a485870
DG
547 return ret;
548}
549
487cf67c 550/*
ffe60014
DG
551 * For each tracing session, update newly registered apps. The session list
552 * lock MUST be acquired before calling this.
487cf67c
DG
553 */
554static void update_ust_app(int app_sock)
555{
556 struct ltt_session *sess, *stmp;
e32d7f27 557 const struct ltt_session_list *session_list = session_get_list();
487cf67c 558
fdadac08
DG
559 /* Consumer is in an ERROR state. Stop any application update. */
560 if (uatomic_read(&ust_consumerd_state) == CONSUMER_ERROR) {
561 /* Stop the update process since the consumer is dead. */
562 return;
563 }
564
487cf67c 565 /* For all tracing session(s) */
e32d7f27 566 cds_list_for_each_entry_safe(sess, stmp, &session_list->head, list) {
a9ad0c8f
MD
567 struct ust_app *app;
568
e32d7f27
JG
569 if (!session_get(sess)) {
570 continue;
571 }
4ee14516 572 session_lock(sess);
a9ad0c8f
MD
573 if (!sess->ust_session) {
574 goto unlock_session;
575 }
576
577 rcu_read_lock();
578 assert(app_sock >= 0);
579 app = ust_app_find_by_sock(app_sock);
580 if (app == NULL) {
581 /*
582 * Application can be unregistered before so
583 * this is possible hence simply stopping the
584 * update.
585 */
586 DBG3("UST app update failed to find app sock %d",
587 app_sock);
588 goto unlock_rcu;
421cb601 589 }
a9ad0c8f
MD
590 ust_app_global_update(sess->ust_session, app);
591 unlock_rcu:
592 rcu_read_unlock();
593 unlock_session:
4ee14516 594 session_unlock(sess);
e32d7f27 595 session_put(sess);
487cf67c
DG
596 }
597}
598
7a485870 599/*
d063d709 600 * This thread manage event coming from the kernel.
7a485870 601 *
d063d709
DG
602 * Features supported in this thread:
603 * -) CPU Hotplug
7a485870
DG
604 */
605static void *thread_manage_kernel(void *data)
606{
139ac872 607 int ret, i, pollfd, update_poll_flag = 1, err = -1;
5eb91c98 608 uint32_t revents, nb_fd;
7a485870 609 char tmp;
5eb91c98 610 struct lttng_poll_event events;
7a485870 611
6993eeb3 612 DBG("[thread] Thread manage kernel started");
7a485870 613
6c71277b 614 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_KERNEL);
927ca06a 615
d5d63bf1
DG
616 /*
617 * This first step of the while is to clean this structure which could free
6d737ce4 618 * non NULL pointers so initialize it before the loop.
d5d63bf1 619 */
6d737ce4 620 lttng_poll_init(&events);
d5d63bf1 621
e547b070 622 if (testpoint(sessiond_thread_manage_kernel)) {
6993eeb3
CB
623 goto error_testpoint;
624 }
8ac94142 625
840cb59c 626 health_code_update();
44a5e5eb 627
e547b070 628 if (testpoint(sessiond_thread_manage_kernel_before_loop)) {
d21b0d71 629 goto error_testpoint;
6993eeb3
CB
630 }
631
7a485870 632 while (1) {
840cb59c 633 health_code_update();
44a5e5eb 634
7a485870 635 if (update_poll_flag == 1) {
d21b0d71
DG
636 /* Clean events object. We are about to populate it again. */
637 lttng_poll_clean(&events);
638
d0b96690 639 ret = sessiond_set_thread_pollset(&events, 2);
d21b0d71
DG
640 if (ret < 0) {
641 goto error_poll_create;
642 }
643
644 ret = lttng_poll_add(&events, kernel_poll_pipe[0], LPOLLIN);
645 if (ret < 0) {
646 goto error;
647 }
5f822d0a 648
d21b0d71 649 /* This will add the available kernel channel if any. */
5eb91c98
DG
650 ret = update_kernel_poll(&events);
651 if (ret < 0) {
7a485870
DG
652 goto error;
653 }
654 update_poll_flag = 0;
655 }
656
7fa2082e 657 DBG("Thread kernel polling");
7a485870
DG
658
659 /* Poll infinite value of time */
88f2b785 660 restart:
a78af745 661 health_poll_entry();
5eb91c98 662 ret = lttng_poll_wait(&events, -1);
7fa2082e
MD
663 DBG("Thread kernel return from poll on %d fds",
664 LTTNG_POLL_GETNB(&events));
a78af745 665 health_poll_exit();
7a485870 666 if (ret < 0) {
88f2b785
MD
667 /*
668 * Restart interrupted system call.
669 */
670 if (errno == EINTR) {
671 goto restart;
672 }
7a485870
DG
673 goto error;
674 } else if (ret == 0) {
675 /* Should not happen since timeout is infinite */
85611738
DG
676 ERR("Return value of poll is 0 with an infinite timeout.\n"
677 "This should not have happened! Continuing...");
7a485870
DG
678 continue;
679 }
680
0d9c5d77
DG
681 nb_fd = ret;
682
5eb91c98
DG
683 for (i = 0; i < nb_fd; i++) {
684 /* Fetch once the poll data */
685 revents = LTTNG_POLL_GETEV(&events, i);
686 pollfd = LTTNG_POLL_GETFD(&events, i);
7a485870 687
840cb59c 688 health_code_update();
44a5e5eb 689
fd20dac9
MD
690 if (!revents) {
691 /* No activity for this FD (poll implementation). */
692 continue;
693 }
694
5eb91c98 695 /* Thread quit pipe has been closed. Killing thread. */
d0b96690 696 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
5eb91c98 697 if (ret) {
139ac872
MD
698 err = 0;
699 goto exit;
5eb91c98 700 }
7a485870 701
5eb91c98 702 /* Check for data on kernel pipe */
03e43155
MD
703 if (revents & LPOLLIN) {
704 if (pollfd == kernel_poll_pipe[0]) {
705 (void) lttng_read(kernel_poll_pipe[0],
706 &tmp, 1);
707 /*
708 * Ret value is useless here, if this pipe gets any actions an
709 * update is required anyway.
710 */
711 update_poll_flag = 1;
712 continue;
713 } else {
714 /*
715 * New CPU detected by the kernel. Adding kernel stream to
716 * kernel session and updating the kernel consumer
717 */
7f032058 718 ret = update_kernel_stream(pollfd);
5eb91c98
DG
719 if (ret < 0) {
720 continue;
721 }
722 break;
7a485870 723 }
03e43155
MD
724 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
725 update_poll_flag = 1;
726 continue;
727 } else {
728 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
729 goto error;
7a485870
DG
730 }
731 }
732 }
733
139ac872 734exit:
7a485870 735error:
5eb91c98 736 lttng_poll_clean(&events);
76d7553f 737error_poll_create:
6993eeb3 738error_testpoint:
6620da75
DG
739 utils_close_pipe(kernel_poll_pipe);
740 kernel_poll_pipe[0] = kernel_poll_pipe[1] = -1;
139ac872 741 if (err) {
840cb59c 742 health_error();
139ac872 743 ERR("Health error occurred in %s", __func__);
6620da75
DG
744 WARN("Kernel thread died unexpectedly. "
745 "Kernel tracing can continue but CPU hotplug is disabled.");
139ac872 746 }
8782cc74 747 health_unregister(health_sessiond);
76d7553f 748 DBG("Kernel thread dying");
7a485870
DG
749 return NULL;
750}
751
a23ec3a7
DG
752/*
753 * Signal pthread condition of the consumer data that the thread.
754 */
755static void signal_consumer_condition(struct consumer_data *data, int state)
756{
757 pthread_mutex_lock(&data->cond_mutex);
758
759 /*
760 * The state is set before signaling. It can be any value, it's the waiter
761 * job to correctly interpret this condition variable associated to the
762 * consumer pthread_cond.
763 *
764 * A value of 0 means that the corresponding thread of the consumer data
765 * was not started. 1 indicates that the thread has started and is ready
766 * for action. A negative value means that there was an error during the
767 * thread bootstrap.
768 */
769 data->consumer_thread_is_ready = state;
770 (void) pthread_cond_signal(&data->cond);
771
772 pthread_mutex_unlock(&data->cond_mutex);
773}
774
1d4b027a 775/*
3bd1e081 776 * This thread manage the consumer error sent back to the session daemon.
1d4b027a 777 */
917a718d 778void *thread_manage_consumer(void *data)
1d4b027a 779{
42fc1d0b 780 int sock = -1, i, ret, pollfd, err = -1, should_quit = 0;
5eb91c98 781 uint32_t revents, nb_fd;
1d4b027a 782 enum lttcomm_return_code code;
5eb91c98 783 struct lttng_poll_event events;
3bd1e081 784 struct consumer_data *consumer_data = data;
b3530820 785 struct consumer_socket *cmd_socket_wrapper = NULL;
1d4b027a 786
3bd1e081 787 DBG("[thread] Manage consumer started");
1d4b027a 788
34c1e15a
MD
789 rcu_register_thread();
790 rcu_thread_online();
791
6c71277b 792 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_CONSUMER);
927ca06a 793
855060f8 794 health_code_update();
9449cc75 795
5eb91c98 796 /*
331744e3
JD
797 * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the
798 * metadata_sock. Nothing more will be added to this poll set.
5eb91c98 799 */
331744e3 800 ret = sessiond_set_thread_pollset(&events, 3);
5eb91c98 801 if (ret < 0) {
76d7553f 802 goto error_poll;
5eb91c98 803 }
273ea72c 804
edb8b045
DG
805 /*
806 * The error socket here is already in a listening state which was done
807 * just before spawning this thread to avoid a race between the consumer
808 * daemon exec trying to connect and the listen() call.
809 */
3bd1e081 810 ret = lttng_poll_add(&events, consumer_data->err_sock, LPOLLIN | LPOLLRDHUP);
5eb91c98
DG
811 if (ret < 0) {
812 goto error;
813 }
814
840cb59c 815 health_code_update();
44a5e5eb 816
331744e3 817 /* Infinite blocking call, waiting for transmission */
88f2b785 818restart:
a78af745 819 health_poll_entry();
8ac94142 820
e547b070 821 if (testpoint(sessiond_thread_manage_consumer)) {
6993eeb3
CB
822 goto error;
823 }
8ac94142 824
5eb91c98 825 ret = lttng_poll_wait(&events, -1);
a78af745 826 health_poll_exit();
273ea72c 827 if (ret < 0) {
88f2b785
MD
828 /*
829 * Restart interrupted system call.
830 */
831 if (errno == EINTR) {
832 goto restart;
833 }
273ea72c
DG
834 goto error;
835 }
836
0d9c5d77
DG
837 nb_fd = ret;
838
5eb91c98
DG
839 for (i = 0; i < nb_fd; i++) {
840 /* Fetch once the poll data */
841 revents = LTTNG_POLL_GETEV(&events, i);
842 pollfd = LTTNG_POLL_GETFD(&events, i);
843
840cb59c 844 health_code_update();
44a5e5eb 845
fd20dac9
MD
846 if (!revents) {
847 /* No activity for this FD (poll implementation). */
848 continue;
849 }
850
5eb91c98 851 /* Thread quit pipe has been closed. Killing thread. */
d0b96690 852 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
5eb91c98 853 if (ret) {
139ac872
MD
854 err = 0;
855 goto exit;
5eb91c98
DG
856 }
857
858 /* Event on the registration socket */
3bd1e081 859 if (pollfd == consumer_data->err_sock) {
03e43155
MD
860 if (revents & LPOLLIN) {
861 continue;
862 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
3bd1e081 863 ERR("consumer err socket poll error");
5eb91c98 864 goto error;
03e43155
MD
865 } else {
866 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
867 goto error;
5eb91c98
DG
868 }
869 }
273ea72c
DG
870 }
871
3bd1e081 872 sock = lttcomm_accept_unix_sock(consumer_data->err_sock);
1d4b027a
DG
873 if (sock < 0) {
874 goto error;
875 }
876
b662582b
DG
877 /*
878 * Set the CLOEXEC flag. Return code is useless because either way, the
879 * show must go on.
880 */
881 (void) utils_set_fd_cloexec(sock);
882
840cb59c 883 health_code_update();
44a5e5eb 884
3bd1e081 885 DBG2("Receiving code from consumer err_sock");
ee0b0061 886
712ea556 887 /* Getting status code from kconsumerd */
54d01ffb
DG
888 ret = lttcomm_recv_unix_sock(sock, &code,
889 sizeof(enum lttcomm_return_code));
1d4b027a
DG
890 if (ret <= 0) {
891 goto error;
892 }
893
840cb59c 894 health_code_update();
b3530820 895 if (code != LTTCOMM_CONSUMERD_COMMAND_SOCK_READY) {
3bd1e081 896 ERR("consumer error when waiting for SOCK_READY : %s",
1d4b027a
DG
897 lttcomm_get_readable_code(-code));
898 goto error;
899 }
900
b3530820
JG
901 /* Connect both command and metadata sockets. */
902 consumer_data->cmd_sock =
903 lttcomm_connect_unix_sock(
904 consumer_data->cmd_unix_sock_path);
905 consumer_data->metadata_fd =
906 lttcomm_connect_unix_sock(
907 consumer_data->cmd_unix_sock_path);
908 if (consumer_data->cmd_sock < 0 || consumer_data->metadata_fd < 0) {
909 PERROR("consumer connect cmd socket");
910 /* On error, signal condition and quit. */
911 signal_consumer_condition(consumer_data, -1);
912 goto error;
913 }
914
915 consumer_data->metadata_sock.fd_ptr = &consumer_data->metadata_fd;
916
917 /* Create metadata socket lock. */
918 consumer_data->metadata_sock.lock = zmalloc(sizeof(pthread_mutex_t));
919 if (consumer_data->metadata_sock.lock == NULL) {
920 PERROR("zmalloc pthread mutex");
921 goto error;
922 }
923 pthread_mutex_init(consumer_data->metadata_sock.lock, NULL);
924
925 DBG("Consumer command socket ready (fd: %d", consumer_data->cmd_sock);
926 DBG("Consumer metadata socket ready (fd: %d)",
927 consumer_data->metadata_fd);
928
929 /*
930 * Remove the consumerd error sock since we've established a connection.
931 */
3bd1e081 932 ret = lttng_poll_del(&events, consumer_data->err_sock);
72079cae 933 if (ret < 0) {
72079cae
DG
934 goto error;
935 }
936
331744e3 937 /* Add new accepted error socket. */
5eb91c98
DG
938 ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLRDHUP);
939 if (ret < 0) {
72079cae 940 goto error;
5eb91c98
DG
941 }
942
331744e3 943 /* Add metadata socket that is successfully connected. */
4ce514c4 944 ret = lttng_poll_add(&events, consumer_data->metadata_fd,
331744e3
JD
945 LPOLLIN | LPOLLRDHUP);
946 if (ret < 0) {
947 goto error;
948 }
949
840cb59c 950 health_code_update();
44a5e5eb 951
b3530820 952 /*
62c43103 953 * Transfer the write-end of the channel monitoring and rotate pipe
92816cc3 954 * to the consumer by issuing a SET_CHANNEL_MONITOR_PIPE command.
b3530820
JG
955 */
956 cmd_socket_wrapper = consumer_allocate_socket(&consumer_data->cmd_sock);
957 if (!cmd_socket_wrapper) {
958 goto error;
959 }
3e4dc117 960 cmd_socket_wrapper->lock = &consumer_data->lock;
b3530820
JG
961
962 ret = consumer_send_channel_monitor_pipe(cmd_socket_wrapper,
963 consumer_data->channel_monitor_pipe);
964 if (ret) {
965 goto error;
966 }
62c43103 967
b3530820
JG
968 /* Discard the socket wrapper as it is no longer needed. */
969 consumer_destroy_socket(cmd_socket_wrapper);
970 cmd_socket_wrapper = NULL;
971
972 /* The thread is completely initialized, signal that it is ready. */
973 signal_consumer_condition(consumer_data, 1);
974
331744e3 975 /* Infinite blocking call, waiting for transmission */
88f2b785 976restart_poll:
331744e3 977 while (1) {
42fc1d0b
DG
978 health_code_update();
979
980 /* Exit the thread because the thread quit pipe has been triggered. */
981 if (should_quit) {
982 /* Not a health error. */
983 err = 0;
984 goto exit;
985 }
986
331744e3
JD
987 health_poll_entry();
988 ret = lttng_poll_wait(&events, -1);
989 health_poll_exit();
990 if (ret < 0) {
991 /*
992 * Restart interrupted system call.
993 */
994 if (errno == EINTR) {
995 goto restart_poll;
996 }
997 goto error;
88f2b785 998 }
72079cae 999
331744e3 1000 nb_fd = ret;
0d9c5d77 1001
331744e3
JD
1002 for (i = 0; i < nb_fd; i++) {
1003 /* Fetch once the poll data */
1004 revents = LTTNG_POLL_GETEV(&events, i);
1005 pollfd = LTTNG_POLL_GETFD(&events, i);
5eb91c98 1006
331744e3 1007 health_code_update();
44a5e5eb 1008
fd20dac9
MD
1009 if (!revents) {
1010 /* No activity for this FD (poll implementation). */
1011 continue;
1012 }
1013
42fc1d0b
DG
1014 /*
1015 * Thread quit pipe has been triggered, flag that we should stop
1016 * but continue the current loop to handle potential data from
1017 * consumer.
1018 */
1019 should_quit = sessiond_check_thread_quit_pipe(pollfd, revents);
5eb91c98 1020
331744e3
JD
1021 if (pollfd == sock) {
1022 /* Event on the consumerd socket */
03e43155
MD
1023 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)
1024 && !(revents & LPOLLIN)) {
331744e3
JD
1025 ERR("consumer err socket second poll error");
1026 goto error;
1027 }
1028 health_code_update();
1029 /* Wait for any kconsumerd error */
1030 ret = lttcomm_recv_unix_sock(sock, &code,
1031 sizeof(enum lttcomm_return_code));
1032 if (ret <= 0) {
1033 ERR("consumer closed the command socket");
1034 goto error;
1035 }
1036
1037 ERR("consumer return code : %s",
1038 lttcomm_get_readable_code(-code));
1039
1040 goto exit;
4ce514c4 1041 } else if (pollfd == consumer_data->metadata_fd) {
03e43155
MD
1042 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)
1043 && !(revents & LPOLLIN)) {
1044 ERR("consumer err metadata socket second poll error");
1045 goto error;
1046 }
331744e3
JD
1047 /* UST metadata requests */
1048 ret = ust_consumer_metadata_request(
1049 &consumer_data->metadata_sock);
1050 if (ret < 0) {
1051 ERR("Handling metadata request");
1052 goto error;
1053 }
5eb91c98 1054 }
42fc1d0b 1055 /* No need for an else branch all FDs are tested prior. */
5eb91c98 1056 }
331744e3 1057 health_code_update();
5eb91c98
DG
1058 }
1059
139ac872 1060exit:
1d4b027a 1061error:
fdadac08
DG
1062 /*
1063 * We lock here because we are about to close the sockets and some other
92db7cdc
DG
1064 * thread might be using them so get exclusive access which will abort all
1065 * other consumer command by other threads.
fdadac08
DG
1066 */
1067 pthread_mutex_lock(&consumer_data->lock);
1068
5c827ce0
DG
1069 /* Immediately set the consumerd state to stopped */
1070 if (consumer_data->type == LTTNG_CONSUMER_KERNEL) {
1071 uatomic_set(&kernel_consumerd_state, CONSUMER_ERROR);
1072 } else if (consumer_data->type == LTTNG_CONSUMER64_UST ||
1073 consumer_data->type == LTTNG_CONSUMER32_UST) {
1074 uatomic_set(&ust_consumerd_state, CONSUMER_ERROR);
1075 } else {
1076 /* Code flow error... */
1077 assert(0);
1078 }
1079
76d7553f
MD
1080 if (consumer_data->err_sock >= 0) {
1081 ret = close(consumer_data->err_sock);
1082 if (ret) {
1083 PERROR("close");
1084 }
a76cbd9f 1085 consumer_data->err_sock = -1;
76d7553f
MD
1086 }
1087 if (consumer_data->cmd_sock >= 0) {
1088 ret = close(consumer_data->cmd_sock);
1089 if (ret) {
1090 PERROR("close");
1091 }
a76cbd9f 1092 consumer_data->cmd_sock = -1;
76d7553f 1093 }
96544455
SS
1094 if (consumer_data->metadata_sock.fd_ptr &&
1095 *consumer_data->metadata_sock.fd_ptr >= 0) {
9363801e 1096 ret = close(*consumer_data->metadata_sock.fd_ptr);
331744e3
JD
1097 if (ret) {
1098 PERROR("close");
1099 }
1100 }
76d7553f
MD
1101 if (sock >= 0) {
1102 ret = close(sock);
1103 if (ret) {
1104 PERROR("close");
1105 }
1106 }
273ea72c 1107
3bd1e081
MD
1108 unlink(consumer_data->err_unix_sock_path);
1109 unlink(consumer_data->cmd_unix_sock_path);
fdadac08 1110 pthread_mutex_unlock(&consumer_data->lock);
92db7cdc 1111
fdadac08 1112 /* Cleanup metadata socket mutex. */
96544455
SS
1113 if (consumer_data->metadata_sock.lock) {
1114 pthread_mutex_destroy(consumer_data->metadata_sock.lock);
1115 free(consumer_data->metadata_sock.lock);
1116 }
5eb91c98 1117 lttng_poll_clean(&events);
b3530820
JG
1118
1119 if (cmd_socket_wrapper) {
1120 consumer_destroy_socket(cmd_socket_wrapper);
1121 }
76d7553f 1122error_poll:
139ac872 1123 if (err) {
840cb59c 1124 health_error();
139ac872
MD
1125 ERR("Health error occurred in %s", __func__);
1126 }
8782cc74 1127 health_unregister(health_sessiond);
76d7553f 1128 DBG("consumer thread cleanup completed");
0177d773 1129
34c1e15a
MD
1130 rcu_thread_offline();
1131 rcu_unregister_thread();
1132
5eb91c98 1133 return NULL;
099e26bd
DG
1134}
1135
099e26bd 1136/*
81f04d5f
JG
1137 * This thread receives application command sockets (FDs) on the
1138 * apps_cmd_pipe and waits (polls) on them until they are closed
1139 * or an error occurs.
1140 *
1141 * At that point, it flushes the data (tracing and metadata) associated
1142 * with this application and tears down ust app sessions and other
1143 * associated data structures through ust_app_unregister().
1144 *
1145 * Note that this thread never sends commands to the applications
1146 * through the command sockets; it merely listens for hang-ups
1147 * and errors on those sockets and cleans-up as they occur.
1d4b027a
DG
1148 */
1149static void *thread_manage_apps(void *data)
099e26bd 1150{
139ac872 1151 int i, ret, pollfd, err = -1;
6cd525e8 1152 ssize_t size_ret;
5eb91c98 1153 uint32_t revents, nb_fd;
5eb91c98 1154 struct lttng_poll_event events;
099e26bd
DG
1155
1156 DBG("[thread] Manage application started");
1157
f6a9efaa
DG
1158 rcu_register_thread();
1159 rcu_thread_online();
1160
6c71277b 1161 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_MANAGE);
927ca06a 1162
e547b070 1163 if (testpoint(sessiond_thread_manage_apps)) {
6993eeb3
CB
1164 goto error_testpoint;
1165 }
1166
840cb59c 1167 health_code_update();
44a5e5eb 1168
d0b96690 1169 ret = sessiond_set_thread_pollset(&events, 2);
5eb91c98 1170 if (ret < 0) {
76d7553f 1171 goto error_poll_create;
5eb91c98 1172 }
099e26bd 1173
5eb91c98
DG
1174 ret = lttng_poll_add(&events, apps_cmd_pipe[0], LPOLLIN | LPOLLRDHUP);
1175 if (ret < 0) {
1176 goto error;
1177 }
099e26bd 1178
e547b070 1179 if (testpoint(sessiond_thread_manage_apps_before_loop)) {
6993eeb3
CB
1180 goto error;
1181 }
8ac94142 1182
840cb59c 1183 health_code_update();
44a5e5eb 1184
5eb91c98 1185 while (1) {
7fa2082e 1186 DBG("Apps thread polling");
099e26bd
DG
1187
1188 /* Inifinite blocking call, waiting for transmission */
88f2b785 1189 restart:
a78af745 1190 health_poll_entry();
5eb91c98 1191 ret = lttng_poll_wait(&events, -1);
7fa2082e
MD
1192 DBG("Apps thread return from poll on %d fds",
1193 LTTNG_POLL_GETNB(&events));
a78af745 1194 health_poll_exit();
099e26bd 1195 if (ret < 0) {
88f2b785
MD
1196 /*
1197 * Restart interrupted system call.
1198 */
1199 if (errno == EINTR) {
1200 goto restart;
1201 }
099e26bd
DG
1202 goto error;
1203 }
1204
0d9c5d77
DG
1205 nb_fd = ret;
1206
5eb91c98
DG
1207 for (i = 0; i < nb_fd; i++) {
1208 /* Fetch once the poll data */
1209 revents = LTTNG_POLL_GETEV(&events, i);
1210 pollfd = LTTNG_POLL_GETFD(&events, i);
1211
840cb59c 1212 health_code_update();
44a5e5eb 1213
fd20dac9
MD
1214 if (!revents) {
1215 /* No activity for this FD (poll implementation). */
1216 continue;
1217 }
1218
5eb91c98 1219 /* Thread quit pipe has been closed. Killing thread. */
d0b96690 1220 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
5eb91c98 1221 if (ret) {
139ac872
MD
1222 err = 0;
1223 goto exit;
5eb91c98 1224 }
099e26bd 1225
5eb91c98
DG
1226 /* Inspect the apps cmd pipe */
1227 if (pollfd == apps_cmd_pipe[0]) {
03e43155 1228 if (revents & LPOLLIN) {
d0b96690
DG
1229 int sock;
1230
5eb91c98 1231 /* Empty pipe */
6cd525e8
MD
1232 size_ret = lttng_read(apps_cmd_pipe[0], &sock, sizeof(sock));
1233 if (size_ret < sizeof(sock)) {
76d7553f 1234 PERROR("read apps cmd pipe");
5eb91c98
DG
1235 goto error;
1236 }
099e26bd 1237
840cb59c 1238 health_code_update();
44a5e5eb 1239
ffe60014 1240 /*
03e43155
MD
1241 * Since this is a command socket (write then read),
1242 * we only monitor the error events of the socket.
ffe60014 1243 */
d0b96690
DG
1244 ret = lttng_poll_add(&events, sock,
1245 LPOLLERR | LPOLLHUP | LPOLLRDHUP);
1246 if (ret < 0) {
5eb91c98 1247 goto error;
e0c7ec2b 1248 }
acc7b41b 1249
d0b96690 1250 DBG("Apps with sock %d added to poll set", sock);
03e43155
MD
1251 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1252 ERR("Apps command pipe error");
1253 goto error;
1254 } else {
1255 ERR("Unknown poll events %u for sock %d", revents, pollfd);
1256 goto error;
0177d773 1257 }
5eb91c98
DG
1258 } else {
1259 /*
54d01ffb
DG
1260 * At this point, we know that a registered application made
1261 * the event at poll_wait.
5eb91c98
DG
1262 */
1263 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1264 /* Removing from the poll set */
1265 ret = lttng_poll_del(&events, pollfd);
1266 if (ret < 0) {
1267 goto error;
1268 }
099e26bd 1269
b9d9b220 1270 /* Socket closed on remote end. */
56fff090 1271 ust_app_unregister(pollfd);
03e43155
MD
1272 } else {
1273 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
1274 goto error;
5eb91c98 1275 }
099e26bd 1276 }
44a5e5eb 1277
840cb59c 1278 health_code_update();
099e26bd 1279 }
099e26bd
DG
1280 }
1281
139ac872 1282exit:
099e26bd 1283error:
5eb91c98 1284 lttng_poll_clean(&events);
76d7553f 1285error_poll_create:
6993eeb3 1286error_testpoint:
6620da75
DG
1287 utils_close_pipe(apps_cmd_pipe);
1288 apps_cmd_pipe[0] = apps_cmd_pipe[1] = -1;
1289
1290 /*
1291 * We don't clean the UST app hash table here since already registered
1292 * applications can still be controlled so let them be until the session
1293 * daemon dies or the applications stop.
1294 */
1295
139ac872 1296 if (err) {
840cb59c 1297 health_error();
139ac872
MD
1298 ERR("Health error occurred in %s", __func__);
1299 }
8782cc74 1300 health_unregister(health_sessiond);
76d7553f 1301 DBG("Application communication apps thread cleanup complete");
f6a9efaa
DG
1302 rcu_thread_offline();
1303 rcu_unregister_thread();
099e26bd
DG
1304 return NULL;
1305}
1306
d0b96690 1307/*
d88aee68
DG
1308 * Send a socket to a thread This is called from the dispatch UST registration
1309 * thread once all sockets are set for the application.
d0b96690 1310 *
b85dc84c
DG
1311 * The sock value can be invalid, we don't really care, the thread will handle
1312 * it and make the necessary cleanup if so.
1313 *
d0b96690
DG
1314 * On success, return 0 else a negative value being the errno message of the
1315 * write().
1316 */
d88aee68 1317static int send_socket_to_thread(int fd, int sock)
d0b96690 1318{
6cd525e8 1319 ssize_t ret;
d0b96690 1320
b85dc84c
DG
1321 /*
1322 * It's possible that the FD is set as invalid with -1 concurrently just
1323 * before calling this function being a shutdown state of the thread.
1324 */
1325 if (fd < 0) {
1326 ret = -EBADF;
1327 goto error;
1328 }
d0b96690 1329
6cd525e8
MD
1330 ret = lttng_write(fd, &sock, sizeof(sock));
1331 if (ret < sizeof(sock)) {
d88aee68 1332 PERROR("write apps pipe %d", fd);
d0b96690
DG
1333 if (ret < 0) {
1334 ret = -errno;
1335 }
1336 goto error;
1337 }
1338
1339 /* All good. Don't send back the write positive ret value. */
1340 ret = 0;
1341error:
6cd525e8 1342 return (int) ret;
d0b96690
DG
1343}
1344
f45e313d
DG
1345/*
1346 * Sanitize the wait queue of the dispatch registration thread meaning removing
1347 * invalid nodes from it. This is to avoid memory leaks for the case the UST
1348 * notify socket is never received.
1349 */
1350static void sanitize_wait_queue(struct ust_reg_wait_queue *wait_queue)
1351{
1352 int ret, nb_fd = 0, i;
1353 unsigned int fd_added = 0;
1354 struct lttng_poll_event events;
1355 struct ust_reg_wait_node *wait_node = NULL, *tmp_wait_node;
1356
1357 assert(wait_queue);
1358
1359 lttng_poll_init(&events);
1360
1361 /* Just skip everything for an empty queue. */
1362 if (!wait_queue->count) {
1363 goto end;
1364 }
1365
1366 ret = lttng_poll_create(&events, wait_queue->count, LTTNG_CLOEXEC);
1367 if (ret < 0) {
1368 goto error_create;
1369 }
1370
1371 cds_list_for_each_entry_safe(wait_node, tmp_wait_node,
1372 &wait_queue->head, head) {
1373 assert(wait_node->app);
1374 ret = lttng_poll_add(&events, wait_node->app->sock,
1375 LPOLLHUP | LPOLLERR);
1376 if (ret < 0) {
1377 goto error;
1378 }
1379
1380 fd_added = 1;
1381 }
1382
1383 if (!fd_added) {
1384 goto end;
1385 }
1386
1387 /*
1388 * Poll but don't block so we can quickly identify the faulty events and
1389 * clean them afterwards from the wait queue.
1390 */
1391 ret = lttng_poll_wait(&events, 0);
1392 if (ret < 0) {
1393 goto error;
1394 }
1395 nb_fd = ret;
1396
1397 for (i = 0; i < nb_fd; i++) {
1398 /* Get faulty FD. */
1399 uint32_t revents = LTTNG_POLL_GETEV(&events, i);
1400 int pollfd = LTTNG_POLL_GETFD(&events, i);
1401
fd20dac9
MD
1402 if (!revents) {
1403 /* No activity for this FD (poll implementation). */
1404 continue;
1405 }
1406
f45e313d
DG
1407 cds_list_for_each_entry_safe(wait_node, tmp_wait_node,
1408 &wait_queue->head, head) {
1409 if (pollfd == wait_node->app->sock &&
1410 (revents & (LPOLLHUP | LPOLLERR))) {
1411 cds_list_del(&wait_node->head);
1412 wait_queue->count--;
1413 ust_app_destroy(wait_node->app);
1414 free(wait_node);
48b40bcf
JG
1415 /*
1416 * Silence warning of use-after-free in
1417 * cds_list_for_each_entry_safe which uses
1418 * __typeof__(*wait_node).
1419 */
1420 wait_node = NULL;
f45e313d 1421 break;
03e43155
MD
1422 } else {
1423 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
1424 goto error;
f45e313d
DG
1425 }
1426 }
1427 }
1428
1429 if (nb_fd > 0) {
1430 DBG("Wait queue sanitized, %d node were cleaned up", nb_fd);
1431 }
1432
1433end:
1434 lttng_poll_clean(&events);
1435 return;
1436
1437error:
1438 lttng_poll_clean(&events);
1439error_create:
1440 ERR("Unable to sanitize wait queue");
1441 return;
1442}
1443
099e26bd
DG
1444/*
1445 * Dispatch request from the registration threads to the application
1446 * communication thread.
1447 */
1448static void *thread_dispatch_ust_registration(void *data)
1449{
12e2b881 1450 int ret, err = -1;
8bdee6e2 1451 struct cds_wfcq_node *node;
099e26bd 1452 struct ust_command *ust_cmd = NULL;
f45e313d
DG
1453 struct ust_reg_wait_node *wait_node = NULL, *tmp_wait_node;
1454 struct ust_reg_wait_queue wait_queue = {
1455 .count = 0,
1456 };
d0b96690 1457
967e3668
MD
1458 rcu_register_thread();
1459
6c71277b 1460 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_REG_DISPATCH);
12e2b881 1461
9ad42ec1
MD
1462 if (testpoint(sessiond_thread_app_reg_dispatch)) {
1463 goto error_testpoint;
1464 }
1465
12e2b881
MD
1466 health_code_update();
1467
f45e313d 1468 CDS_INIT_LIST_HEAD(&wait_queue.head);
099e26bd
DG
1469
1470 DBG("[thread] Dispatch UST command started");
1471
0ed3b1a8 1472 for (;;) {
12e2b881
MD
1473 health_code_update();
1474
099e26bd
DG
1475 /* Atomically prepare the queue futex */
1476 futex_nto1_prepare(&ust_cmd_queue.futex);
1477
0ed3b1a8
MD
1478 if (CMM_LOAD_SHARED(dispatch_thread_exit)) {
1479 break;
1480 }
1481
099e26bd 1482 do {
d0b96690 1483 struct ust_app *app = NULL;
7972aab2 1484 ust_cmd = NULL;
d0b96690 1485
f45e313d
DG
1486 /*
1487 * Make sure we don't have node(s) that have hung up before receiving
1488 * the notify socket. This is to clean the list in order to avoid
1489 * memory leaks from notify socket that are never seen.
1490 */
1491 sanitize_wait_queue(&wait_queue);
1492
12e2b881 1493 health_code_update();
099e26bd 1494 /* Dequeue command for registration */
8bdee6e2 1495 node = cds_wfcq_dequeue_blocking(&ust_cmd_queue.head, &ust_cmd_queue.tail);
099e26bd 1496 if (node == NULL) {
00a17c97 1497 DBG("Woken up but nothing in the UST command queue");
099e26bd
DG
1498 /* Continue thread execution */
1499 break;
1500 }
1501
1502 ust_cmd = caa_container_of(node, struct ust_command, node);
1503
2f50c8a3
DG
1504 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1505 " gid:%d sock:%d name:%s (version %d.%d)",
1506 ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid,
1507 ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid,
1508 ust_cmd->sock, ust_cmd->reg_msg.name,
1509 ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor);
d0b96690
DG
1510
1511 if (ust_cmd->reg_msg.type == USTCTL_SOCKET_CMD) {
1512 wait_node = zmalloc(sizeof(*wait_node));
1513 if (!wait_node) {
1514 PERROR("zmalloc wait_node dispatch");
020d7f60
DG
1515 ret = close(ust_cmd->sock);
1516 if (ret < 0) {
1517 PERROR("close ust sock dispatch %d", ust_cmd->sock);
1518 }
51dec90d 1519 lttng_fd_put(LTTNG_FD_APPS, 1);
7972aab2 1520 free(ust_cmd);
d0b96690
DG
1521 goto error;
1522 }
1523 CDS_INIT_LIST_HEAD(&wait_node->head);
1524
1525 /* Create application object if socket is CMD. */
1526 wait_node->app = ust_app_create(&ust_cmd->reg_msg,
1527 ust_cmd->sock);
1528 if (!wait_node->app) {
1529 ret = close(ust_cmd->sock);
1530 if (ret < 0) {
1531 PERROR("close ust sock dispatch %d", ust_cmd->sock);
6620da75 1532 }
51dec90d 1533 lttng_fd_put(LTTNG_FD_APPS, 1);
d88aee68 1534 free(wait_node);
7972aab2 1535 free(ust_cmd);
d0b96690
DG
1536 continue;
1537 }
1538 /*
1539 * Add application to the wait queue so we can set the notify
1540 * socket before putting this object in the global ht.
1541 */
f45e313d
DG
1542 cds_list_add(&wait_node->head, &wait_queue.head);
1543 wait_queue.count++;
d0b96690 1544
7972aab2 1545 free(ust_cmd);
d0b96690
DG
1546 /*
1547 * We have to continue here since we don't have the notify
1548 * socket and the application MUST be added to the hash table
1549 * only at that moment.
1550 */
1551 continue;
1552 } else {
1553 /*
1554 * Look for the application in the local wait queue and set the
1555 * notify socket if found.
1556 */
d88aee68 1557 cds_list_for_each_entry_safe(wait_node, tmp_wait_node,
f45e313d 1558 &wait_queue.head, head) {
12e2b881 1559 health_code_update();
d0b96690
DG
1560 if (wait_node->app->pid == ust_cmd->reg_msg.pid) {
1561 wait_node->app->notify_sock = ust_cmd->sock;
1562 cds_list_del(&wait_node->head);
f45e313d 1563 wait_queue.count--;
d0b96690
DG
1564 app = wait_node->app;
1565 free(wait_node);
1566 DBG3("UST app notify socket %d is set", ust_cmd->sock);
1567 break;
1568 }
1569 }
020d7f60
DG
1570
1571 /*
1572 * With no application at this stage the received socket is
1573 * basically useless so close it before we free the cmd data
1574 * structure for good.
1575 */
1576 if (!app) {
1577 ret = close(ust_cmd->sock);
1578 if (ret < 0) {
1579 PERROR("close ust sock dispatch %d", ust_cmd->sock);
1580 }
51dec90d 1581 lttng_fd_put(LTTNG_FD_APPS, 1);
020d7f60 1582 }
7972aab2 1583 free(ust_cmd);
d0b96690
DG
1584 }
1585
1586 if (app) {
d0b96690
DG
1587 /*
1588 * @session_lock_list
1589 *
1590 * Lock the global session list so from the register up to the
1591 * registration done message, no thread can see the application
1592 * and change its state.
1593 */
1594 session_lock_list();
1595 rcu_read_lock();
d88aee68 1596
d0b96690
DG
1597 /*
1598 * Add application to the global hash table. This needs to be
1599 * done before the update to the UST registry can locate the
1600 * application.
1601 */
1602 ust_app_add(app);
d88aee68
DG
1603
1604 /* Set app version. This call will print an error if needed. */
1605 (void) ust_app_version(app);
1606
1607 /* Send notify socket through the notify pipe. */
1608 ret = send_socket_to_thread(apps_cmd_notify_pipe[1],
1609 app->notify_sock);
1610 if (ret < 0) {
1611 rcu_read_unlock();
1612 session_unlock_list();
b85dc84c
DG
1613 /*
1614 * No notify thread, stop the UST tracing. However, this is
1615 * not an internal error of the this thread thus setting
1616 * the health error code to a normal exit.
1617 */
1618 err = 0;
d88aee68 1619 goto error;
6620da75 1620 }
d88aee68 1621
d0b96690
DG
1622 /*
1623 * Update newly registered application with the tracing
1624 * registry info already enabled information.
1625 */
1626 update_ust_app(app->sock);
d88aee68
DG
1627
1628 /*
1629 * Don't care about return value. Let the manage apps threads
1630 * handle app unregistration upon socket close.
1631 */
fb45065e 1632 (void) ust_app_register_done(app);
d88aee68
DG
1633
1634 /*
1635 * Even if the application socket has been closed, send the app
1636 * to the thread and unregistration will take place at that
1637 * place.
1638 */
1639 ret = send_socket_to_thread(apps_cmd_pipe[1], app->sock);
d0b96690 1640 if (ret < 0) {
d88aee68
DG
1641 rcu_read_unlock();
1642 session_unlock_list();
b85dc84c
DG
1643 /*
1644 * No apps. thread, stop the UST tracing. However, this is
1645 * not an internal error of the this thread thus setting
1646 * the health error code to a normal exit.
1647 */
1648 err = 0;
d88aee68 1649 goto error;
d0b96690 1650 }
d88aee68 1651
d0b96690
DG
1652 rcu_read_unlock();
1653 session_unlock_list();
099e26bd 1654 }
099e26bd
DG
1655 } while (node != NULL);
1656
12e2b881 1657 health_poll_entry();
099e26bd
DG
1658 /* Futex wait on queue. Blocking call on futex() */
1659 futex_nto1_wait(&ust_cmd_queue.futex);
12e2b881 1660 health_poll_exit();
099e26bd 1661 }
12e2b881
MD
1662 /* Normal exit, no error */
1663 err = 0;
099e26bd
DG
1664
1665error:
d88aee68
DG
1666 /* Clean up wait queue. */
1667 cds_list_for_each_entry_safe(wait_node, tmp_wait_node,
f45e313d 1668 &wait_queue.head, head) {
d88aee68 1669 cds_list_del(&wait_node->head);
f45e313d 1670 wait_queue.count--;
d88aee68
DG
1671 free(wait_node);
1672 }
1673
772b8f4d
MD
1674 /* Empty command queue. */
1675 for (;;) {
1676 /* Dequeue command for registration */
1677 node = cds_wfcq_dequeue_blocking(&ust_cmd_queue.head, &ust_cmd_queue.tail);
1678 if (node == NULL) {
1679 break;
1680 }
1681 ust_cmd = caa_container_of(node, struct ust_command, node);
1682 ret = close(ust_cmd->sock);
1683 if (ret < 0) {
1684 PERROR("close ust sock exit dispatch %d", ust_cmd->sock);
1685 }
1686 lttng_fd_put(LTTNG_FD_APPS, 1);
1687 free(ust_cmd);
1688 }
1689
9ad42ec1 1690error_testpoint:
099e26bd 1691 DBG("Dispatch thread dying");
12e2b881
MD
1692 if (err) {
1693 health_error();
1694 ERR("Health error occurred in %s", __func__);
1695 }
8782cc74 1696 health_unregister(health_sessiond);
967e3668 1697 rcu_unregister_thread();
099e26bd
DG
1698 return NULL;
1699}
1700
1701/*
1702 * This thread manage application registration.
1703 */
1704static void *thread_registration_apps(void *data)
1d4b027a 1705{
139ac872 1706 int sock = -1, i, ret, pollfd, err = -1;
5eb91c98
DG
1707 uint32_t revents, nb_fd;
1708 struct lttng_poll_event events;
099e26bd
DG
1709 /*
1710 * Get allocated in this thread, enqueued to a global queue, dequeued and
1711 * freed in the manage apps thread.
1712 */
1713 struct ust_command *ust_cmd = NULL;
1d4b027a 1714
099e26bd 1715 DBG("[thread] Manage application registration started");
1d4b027a 1716
6c71277b 1717 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_REG);
927ca06a 1718
e547b070 1719 if (testpoint(sessiond_thread_registration_apps)) {
6993eeb3
CB
1720 goto error_testpoint;
1721 }
8ac94142 1722
1d4b027a
DG
1723 ret = lttcomm_listen_unix_sock(apps_sock);
1724 if (ret < 0) {
76d7553f 1725 goto error_listen;
1d4b027a
DG
1726 }
1727
5eb91c98
DG
1728 /*
1729 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
1730 * more will be added to this poll set.
1731 */
d0b96690 1732 ret = sessiond_set_thread_pollset(&events, 2);
5eb91c98 1733 if (ret < 0) {
76d7553f 1734 goto error_create_poll;
5eb91c98 1735 }
273ea72c 1736
5eb91c98
DG
1737 /* Add the application registration socket */
1738 ret = lttng_poll_add(&events, apps_sock, LPOLLIN | LPOLLRDHUP);
1739 if (ret < 0) {
76d7553f 1740 goto error_poll_add;
5eb91c98 1741 }
273ea72c 1742
1d4b027a 1743 /* Notify all applications to register */
0fdd1e2c
DG
1744 ret = notify_ust_apps(1);
1745 if (ret < 0) {
1746 ERR("Failed to notify applications or create the wait shared memory.\n"
54d01ffb
DG
1747 "Execution continues but there might be problem for already\n"
1748 "running applications that wishes to register.");
0fdd1e2c 1749 }
1d4b027a
DG
1750
1751 while (1) {
1752 DBG("Accepting application registration");
273ea72c
DG
1753
1754 /* Inifinite blocking call, waiting for transmission */
88f2b785 1755 restart:
a78af745 1756 health_poll_entry();
5eb91c98 1757 ret = lttng_poll_wait(&events, -1);
a78af745 1758 health_poll_exit();
273ea72c 1759 if (ret < 0) {
88f2b785
MD
1760 /*
1761 * Restart interrupted system call.
1762 */
1763 if (errno == EINTR) {
1764 goto restart;
1765 }
273ea72c
DG
1766 goto error;
1767 }
1768
0d9c5d77
DG
1769 nb_fd = ret;
1770
5eb91c98 1771 for (i = 0; i < nb_fd; i++) {
840cb59c 1772 health_code_update();
139ac872 1773
5eb91c98
DG
1774 /* Fetch once the poll data */
1775 revents = LTTNG_POLL_GETEV(&events, i);
1776 pollfd = LTTNG_POLL_GETFD(&events, i);
273ea72c 1777
fd20dac9
MD
1778 if (!revents) {
1779 /* No activity for this FD (poll implementation). */
1780 continue;
1781 }
1782
5eb91c98 1783 /* Thread quit pipe has been closed. Killing thread. */
d0b96690 1784 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
5eb91c98 1785 if (ret) {
139ac872
MD
1786 err = 0;
1787 goto exit;
90014c57 1788 }
1d4b027a 1789
5eb91c98
DG
1790 /* Event on the registration socket */
1791 if (pollfd == apps_sock) {
03e43155 1792 if (revents & LPOLLIN) {
5eb91c98
DG
1793 sock = lttcomm_accept_unix_sock(apps_sock);
1794 if (sock < 0) {
1795 goto error;
1796 }
099e26bd 1797
16c5c8fa
DG
1798 /*
1799 * Set socket timeout for both receiving and ending.
1800 * app_socket_timeout is in seconds, whereas
1801 * lttcomm_setsockopt_rcv_timeout and
1802 * lttcomm_setsockopt_snd_timeout expect msec as
1803 * parameter.
1804 */
e6142f2e 1805 if (config.app_socket_timeout >= 0) {
28ce0ff2 1806 (void) lttcomm_setsockopt_rcv_timeout(sock,
e6142f2e 1807 config.app_socket_timeout * 1000);
28ce0ff2 1808 (void) lttcomm_setsockopt_snd_timeout(sock,
e6142f2e 1809 config.app_socket_timeout * 1000);
28ce0ff2 1810 }
16c5c8fa 1811
b662582b
DG
1812 /*
1813 * Set the CLOEXEC flag. Return code is useless because
1814 * either way, the show must go on.
1815 */
1816 (void) utils_set_fd_cloexec(sock);
1817
5eb91c98 1818 /* Create UST registration command for enqueuing */
ba7f0ae5 1819 ust_cmd = zmalloc(sizeof(struct ust_command));
5eb91c98 1820 if (ust_cmd == NULL) {
76d7553f 1821 PERROR("ust command zmalloc");
41ed8e47
MD
1822 ret = close(sock);
1823 if (ret) {
1824 PERROR("close");
1825 }
5eb91c98
DG
1826 goto error;
1827 }
1d4b027a 1828
5eb91c98
DG
1829 /*
1830 * Using message-based transmissions to ensure we don't
1831 * have to deal with partially received messages.
1832 */
4063050c
MD
1833 ret = lttng_fd_get(LTTNG_FD_APPS, 1);
1834 if (ret < 0) {
1835 ERR("Exhausted file descriptors allowed for applications.");
1836 free(ust_cmd);
1837 ret = close(sock);
1838 if (ret) {
1839 PERROR("close");
1840 }
1841 sock = -1;
1842 continue;
1843 }
d88aee68 1844
840cb59c 1845 health_code_update();
d0b96690
DG
1846 ret = ust_app_recv_registration(sock, &ust_cmd->reg_msg);
1847 if (ret < 0) {
5eb91c98 1848 free(ust_cmd);
d0b96690 1849 /* Close socket of the application. */
76d7553f
MD
1850 ret = close(sock);
1851 if (ret) {
1852 PERROR("close");
1853 }
4063050c 1854 lttng_fd_put(LTTNG_FD_APPS, 1);
76d7553f 1855 sock = -1;
5eb91c98
DG
1856 continue;
1857 }
840cb59c 1858 health_code_update();
099e26bd 1859
5eb91c98 1860 ust_cmd->sock = sock;
34a2494f 1861 sock = -1;
099e26bd 1862
5eb91c98
DG
1863 DBG("UST registration received with pid:%d ppid:%d uid:%d"
1864 " gid:%d sock:%d name:%s (version %d.%d)",
1865 ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid,
1866 ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid,
1867 ust_cmd->sock, ust_cmd->reg_msg.name,
1868 ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor);
54d01ffb 1869
5eb91c98
DG
1870 /*
1871 * Lock free enqueue the registration request. The red pill
54d01ffb 1872 * has been taken! This apps will be part of the *system*.
5eb91c98 1873 */
8bdee6e2 1874 cds_wfcq_enqueue(&ust_cmd_queue.head, &ust_cmd_queue.tail, &ust_cmd->node);
5eb91c98
DG
1875
1876 /*
1877 * Wake the registration queue futex. Implicit memory
8bdee6e2 1878 * barrier with the exchange in cds_wfcq_enqueue.
5eb91c98
DG
1879 */
1880 futex_nto1_wake(&ust_cmd_queue.futex);
03e43155
MD
1881 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1882 ERR("Register apps socket poll error");
1883 goto error;
1884 } else {
1885 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
1886 goto error;
5eb91c98
DG
1887 }
1888 }
90014c57 1889 }
1d4b027a
DG
1890 }
1891
139ac872 1892exit:
1d4b027a 1893error:
0fdd1e2c
DG
1894 /* Notify that the registration thread is gone */
1895 notify_ust_apps(0);
1896
a4b35e07 1897 if (apps_sock >= 0) {
76d7553f
MD
1898 ret = close(apps_sock);
1899 if (ret) {
1900 PERROR("close");
1901 }
a4b35e07 1902 }
46c3f085 1903 if (sock >= 0) {
76d7553f
MD
1904 ret = close(sock);
1905 if (ret) {
1906 PERROR("close");
1907 }
4063050c 1908 lttng_fd_put(LTTNG_FD_APPS, 1);
a4b35e07 1909 }
e6142f2e 1910 unlink(config.apps_unix_sock_path.value);
0fdd1e2c 1911
76d7553f 1912error_poll_add:
5eb91c98 1913 lttng_poll_clean(&events);
76d7553f
MD
1914error_listen:
1915error_create_poll:
6993eeb3 1916error_testpoint:
76d7553f 1917 DBG("UST Registration thread cleanup complete");
9ad42ec1
MD
1918 if (err) {
1919 health_error();
1920 ERR("Health error occurred in %s", __func__);
1921 }
8782cc74 1922 health_unregister(health_sessiond);
5eb91c98 1923
1d4b027a
DG
1924 return NULL;
1925}
1926
b73401da 1927/*
096102bd 1928 * Setup necessary data for kernel tracer action.
b73401da 1929 */
096102bd 1930static int init_kernel_tracer(void)
b73401da
DG
1931{
1932 int ret;
b73401da 1933
096102bd
DG
1934 /* Modprobe lttng kernel modules */
1935 ret = modprobe_lttng_control();
b73401da 1936 if (ret < 0) {
b73401da
DG
1937 goto error;
1938 }
1939
096102bd
DG
1940 /* Open debugfs lttng */
1941 kernel_tracer_fd = open(module_proc_lttng, O_RDWR);
1942 if (kernel_tracer_fd < 0) {
1943 DBG("Failed to open %s", module_proc_lttng);
2f77fc4b 1944 goto error_open;
54d01ffb
DG
1945 }
1946
2f77fc4b 1947 /* Validate kernel version */
88076e89
JD
1948 ret = kernel_validate_version(kernel_tracer_fd, &kernel_tracer_version,
1949 &kernel_tracer_abi_version);
2f77fc4b
DG
1950 if (ret < 0) {
1951 goto error_version;
b551a063 1952 }
54d01ffb 1953
2f77fc4b
DG
1954 ret = modprobe_lttng_data();
1955 if (ret < 0) {
1956 goto error_modules;
54d01ffb
DG
1957 }
1958
6e21424e
JR
1959 ret = kernel_supports_ring_buffer_snapshot_sample_positions(
1960 kernel_tracer_fd);
1961 if (ret < 0) {
1962 goto error_modules;
1963 }
1964
1965 if (ret < 1) {
1966 WARN("Kernel tracer does not support buffer monitoring. "
1967 "The monitoring timer of channels in the kernel domain "
1968 "will be set to 0 (disabled).");
1969 }
1970
2f77fc4b
DG
1971 DBG("Kernel tracer fd %d", kernel_tracer_fd);
1972 return 0;
1973
1974error_version:
1975 modprobe_remove_lttng_control();
1976 ret = close(kernel_tracer_fd);
1977 if (ret) {
1978 PERROR("close");
b551a063 1979 }
2f77fc4b 1980 kernel_tracer_fd = -1;
f73fabfd 1981 return LTTNG_ERR_KERN_VERSION;
b551a063 1982
2f77fc4b
DG
1983error_modules:
1984 ret = close(kernel_tracer_fd);
1985 if (ret) {
1986 PERROR("close");
b551a063 1987 }
54d01ffb 1988
2f77fc4b
DG
1989error_open:
1990 modprobe_remove_lttng_control();
54d01ffb
DG
1991
1992error:
2f77fc4b
DG
1993 WARN("No kernel tracer available");
1994 kernel_tracer_fd = -1;
1995 if (!is_root) {
f73fabfd 1996 return LTTNG_ERR_NEED_ROOT_SESSIOND;
2f77fc4b 1997 } else {
f73fabfd 1998 return LTTNG_ERR_KERN_NA;
2f77fc4b 1999 }
54d01ffb
DG
2000}
2001
917a718d 2002static int string_match(const char *str1, const char *str2)
54d01ffb 2003{
917a718d 2004 return (str1 && str2) && !strcmp(str1, str2);
54d01ffb
DG
2005}
2006
00e2e675 2007/*
917a718d
JG
2008 * Take an option from the getopt output and set it in the right variable to be
2009 * used later.
36b588ed 2010 *
917a718d 2011 * Return 0 on success else a negative value.
00e2e675 2012 */
917a718d 2013static int set_option(int opt, const char *arg, const char *optname)
00e2e675 2014{
917a718d 2015 int ret = 0;
fac6795d 2016
c0232ea5 2017 if (string_match(optname, "client-sock") || opt == 'c') {
66b2ce8e
JG
2018 if (!arg || *arg == '\0') {
2019 ret = -EINVAL;
2020 goto end;
2021 }
e8fa9fb0
MD
2022 if (lttng_is_setuid_setgid()) {
2023 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
2024 "-c, --client-sock");
2025 } else {
e6142f2e
JG
2026 config_string_set(&config.client_unix_sock_path,
2027 strdup(arg));
2028 if (!config.client_unix_sock_path.value) {
2029 ret = -ENOMEM;
2030 PERROR("strdup");
2031 }
e8fa9fb0 2032 }
c0232ea5 2033 } else if (string_match(optname, "apps-sock") || opt == 'a') {
66b2ce8e
JG
2034 if (!arg || *arg == '\0') {
2035 ret = -EINVAL;
2036 goto end;
2037 }
e8fa9fb0
MD
2038 if (lttng_is_setuid_setgid()) {
2039 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
2040 "-a, --apps-sock");
2041 } else {
e6142f2e
JG
2042 config_string_set(&config.apps_unix_sock_path,
2043 strdup(arg));
2044 if (!config.apps_unix_sock_path.value) {
2045 ret = -ENOMEM;
2046 PERROR("strdup");
2047 }
e8fa9fb0 2048 }
c0232ea5 2049 } else if (string_match(optname, "daemonize") || opt == 'd') {
e6142f2e 2050 config.daemonize = true;
c0232ea5 2051 } else if (string_match(optname, "background") || opt == 'b') {
e6142f2e 2052 config.background = true;
c0232ea5 2053 } else if (string_match(optname, "group") || opt == 'g') {
66b2ce8e
JG
2054 if (!arg || *arg == '\0') {
2055 ret = -EINVAL;
2056 goto end;
2057 }
e8fa9fb0
MD
2058 if (lttng_is_setuid_setgid()) {
2059 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
2060 "-g, --group");
2061 } else {
e6142f2e
JG
2062 config_string_set(&config.tracing_group_name,
2063 strdup(arg));
2064 if (!config.tracing_group_name.value) {
e8fa9fb0 2065 ret = -ENOMEM;
e6142f2e 2066 PERROR("strdup");
e8fa9fb0 2067 }
db322c4d 2068 }
c0232ea5 2069 } else if (string_match(optname, "help") || opt == 'h') {
4fc83d94 2070 ret = utils_show_help(8, "lttng-sessiond", help_msg);
8190767e 2071 if (ret) {
4fc83d94 2072 ERR("Cannot show --help for `lttng-sessiond`");
8190767e
PP
2073 perror("exec");
2074 }
2075 exit(ret ? EXIT_FAILURE : EXIT_SUCCESS);
c0232ea5 2076 } else if (string_match(optname, "version") || opt == 'V') {
26296c48
JG
2077 fprintf(stdout, "%s\n", VERSION);
2078 exit(EXIT_SUCCESS);
c0232ea5 2079 } else if (string_match(optname, "sig-parent") || opt == 'S') {
e6142f2e 2080 config.sig_parent = true;
c0232ea5 2081 } else if (string_match(optname, "kconsumerd-err-sock")) {
66b2ce8e
JG
2082 if (!arg || *arg == '\0') {
2083 ret = -EINVAL;
2084 goto end;
2085 }
e8fa9fb0
MD
2086 if (lttng_is_setuid_setgid()) {
2087 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
2088 "--kconsumerd-err-sock");
2089 } else {
e6142f2e
JG
2090 config_string_set(&config.kconsumerd_err_unix_sock_path,
2091 strdup(arg));
2092 if (!config.kconsumerd_err_unix_sock_path.value) {
2093 ret = -ENOMEM;
2094 PERROR("strdup");
2095 }
e8fa9fb0 2096 }
c0232ea5 2097 } else if (string_match(optname, "kconsumerd-cmd-sock")) {
66b2ce8e
JG
2098 if (!arg || *arg == '\0') {
2099 ret = -EINVAL;
2100 goto end;
2101 }
e8fa9fb0
MD
2102 if (lttng_is_setuid_setgid()) {
2103 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
2104 "--kconsumerd-cmd-sock");
2105 } else {
e6142f2e
JG
2106 config_string_set(&config.kconsumerd_cmd_unix_sock_path,
2107 strdup(arg));
2108 if (!config.kconsumerd_cmd_unix_sock_path.value) {
2109 ret = -ENOMEM;
2110 PERROR("strdup");
2111 }
e8fa9fb0 2112 }
c0232ea5 2113 } else if (string_match(optname, "ustconsumerd64-err-sock")) {
66b2ce8e
JG
2114 if (!arg || *arg == '\0') {
2115 ret = -EINVAL;
2116 goto end;
2117 }
e8fa9fb0
MD
2118 if (lttng_is_setuid_setgid()) {
2119 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
2120 "--ustconsumerd64-err-sock");
2121 } else {
e6142f2e
JG
2122 config_string_set(&config.consumerd64_err_unix_sock_path,
2123 strdup(arg));
2124 if (!config.consumerd64_err_unix_sock_path.value) {
2125 ret = -ENOMEM;
2126 PERROR("strdup");
2127 }
e8fa9fb0 2128 }
c0232ea5 2129 } else if (string_match(optname, "ustconsumerd64-cmd-sock")) {
66b2ce8e
JG
2130 if (!arg || *arg == '\0') {
2131 ret = -EINVAL;
2132 goto end;
2133 }
e8fa9fb0
MD
2134 if (lttng_is_setuid_setgid()) {
2135 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
2136 "--ustconsumerd64-cmd-sock");
2137 } else {
e6142f2e
JG
2138 config_string_set(&config.consumerd64_cmd_unix_sock_path,
2139 strdup(arg));
2140 if (!config.consumerd64_cmd_unix_sock_path.value) {
2141 ret = -ENOMEM;
2142 PERROR("strdup");
2143 }
e8fa9fb0 2144 }
c0232ea5 2145 } else if (string_match(optname, "ustconsumerd32-err-sock")) {
66b2ce8e
JG
2146 if (!arg || *arg == '\0') {
2147 ret = -EINVAL;
2148 goto end;
2149 }
e8fa9fb0
MD
2150 if (lttng_is_setuid_setgid()) {
2151 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
2152 "--ustconsumerd32-err-sock");
2153 } else {
e6142f2e
JG
2154 config_string_set(&config.consumerd32_err_unix_sock_path,
2155 strdup(arg));
2156 if (!config.consumerd32_err_unix_sock_path.value) {
2157 ret = -ENOMEM;
2158 PERROR("strdup");
2159 }
e8fa9fb0 2160 }
c0232ea5 2161 } else if (string_match(optname, "ustconsumerd32-cmd-sock")) {
66b2ce8e
JG
2162 if (!arg || *arg == '\0') {
2163 ret = -EINVAL;
2164 goto end;
2165 }
e8fa9fb0
MD
2166 if (lttng_is_setuid_setgid()) {
2167 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
2168 "--ustconsumerd32-cmd-sock");
2169 } else {
e6142f2e
JG
2170 config_string_set(&config.consumerd32_cmd_unix_sock_path,
2171 strdup(arg));
2172 if (!config.consumerd32_cmd_unix_sock_path.value) {
2173 ret = -ENOMEM;
2174 PERROR("strdup");
2175 }
e8fa9fb0 2176 }
c0232ea5 2177 } else if (string_match(optname, "no-kernel")) {
e6142f2e 2178 config.no_kernel = true;
c0232ea5 2179 } else if (string_match(optname, "quiet") || opt == 'q') {
6d9a9c65 2180 config.quiet = true;
c0232ea5 2181 } else if (string_match(optname, "verbose") || opt == 'v') {
26296c48
JG
2182 /* Verbose level can increase using multiple -v */
2183 if (arg) {
13755a18 2184 /* Value obtained from config file */
e6142f2e 2185 config.verbose = config_parse_value(arg);
26296c48 2186 } else {
13755a18 2187 /* -v used on command line */
e6142f2e 2188 config.verbose++;
26296c48 2189 }
13755a18 2190 /* Clamp value to [0, 3] */
e6142f2e
JG
2191 config.verbose = config.verbose < 0 ? 0 :
2192 (config.verbose <= 3 ? config.verbose : 3);
c0232ea5 2193 } else if (string_match(optname, "verbose-consumer")) {
26296c48 2194 if (arg) {
e6142f2e 2195 config.verbose_consumer = config_parse_value(arg);
26296c48 2196 } else {
e6142f2e 2197 config.verbose_consumer++;
26296c48 2198 }
c0232ea5 2199 } else if (string_match(optname, "consumerd32-path")) {
66b2ce8e
JG
2200 if (!arg || *arg == '\0') {
2201 ret = -EINVAL;
2202 goto end;
2203 }
e8fa9fb0
MD
2204 if (lttng_is_setuid_setgid()) {
2205 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
2206 "--consumerd32-path");
2207 } else {
e6142f2e
JG
2208 config_string_set(&config.consumerd32_bin_path,
2209 strdup(arg));
2210 if (!config.consumerd32_bin_path.value) {
e8fa9fb0
MD
2211 PERROR("strdup");
2212 ret = -ENOMEM;
2213 }
db322c4d 2214 }
c0232ea5 2215 } else if (string_match(optname, "consumerd32-libdir")) {
66b2ce8e
JG
2216 if (!arg || *arg == '\0') {
2217 ret = -EINVAL;
2218 goto end;
2219 }
e8fa9fb0
MD
2220 if (lttng_is_setuid_setgid()) {
2221 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
2222 "--consumerd32-libdir");
2223 } else {
e6142f2e
JG
2224 config_string_set(&config.consumerd32_lib_dir,
2225 strdup(arg));
2226 if (!config.consumerd32_lib_dir.value) {
e8fa9fb0
MD
2227 PERROR("strdup");
2228 ret = -ENOMEM;
2229 }
db322c4d 2230 }
c0232ea5 2231 } else if (string_match(optname, "consumerd64-path")) {
66b2ce8e
JG
2232 if (!arg || *arg == '\0') {
2233 ret = -EINVAL;
2234 goto end;
2235 }
e8fa9fb0
MD
2236 if (lttng_is_setuid_setgid()) {
2237 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
2238 "--consumerd64-path");
2239 } else {
e6142f2e
JG
2240 config_string_set(&config.consumerd64_bin_path,
2241 strdup(arg));
2242 if (!config.consumerd64_bin_path.value) {
e8fa9fb0
MD
2243 PERROR("strdup");
2244 ret = -ENOMEM;
2245 }
db322c4d 2246 }
c0232ea5 2247 } else if (string_match(optname, "consumerd64-libdir")) {
66b2ce8e
JG
2248 if (!arg || *arg == '\0') {
2249 ret = -EINVAL;
2250 goto end;
2251 }
e8fa9fb0
MD
2252 if (lttng_is_setuid_setgid()) {
2253 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
2254 "--consumerd64-libdir");
2255 } else {
e6142f2e
JG
2256 config_string_set(&config.consumerd64_lib_dir,
2257 strdup(arg));
2258 if (!config.consumerd64_lib_dir.value) {
e8fa9fb0
MD
2259 PERROR("strdup");
2260 ret = -ENOMEM;
2261 }
db322c4d 2262 }
c0232ea5 2263 } else if (string_match(optname, "pidfile") || opt == 'p') {
66b2ce8e
JG
2264 if (!arg || *arg == '\0') {
2265 ret = -EINVAL;
2266 goto end;
2267 }
e8fa9fb0
MD
2268 if (lttng_is_setuid_setgid()) {
2269 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
2270 "-p, --pidfile");
2271 } else {
e6142f2e
JG
2272 config_string_set(&config.pid_file_path, strdup(arg));
2273 if (!config.pid_file_path.value) {
e8fa9fb0
MD
2274 PERROR("strdup");
2275 ret = -ENOMEM;
2276 }
db322c4d 2277 }
c0232ea5 2278 } else if (string_match(optname, "agent-tcp-port")) {
66b2ce8e
JG
2279 if (!arg || *arg == '\0') {
2280 ret = -EINVAL;
2281 goto end;
2282 }
e8fa9fb0
MD
2283 if (lttng_is_setuid_setgid()) {
2284 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
2285 "--agent-tcp-port");
2286 } else {
2287 unsigned long v;
4d076222 2288
e8fa9fb0
MD
2289 errno = 0;
2290 v = strtoul(arg, NULL, 0);
2291 if (errno != 0 || !isdigit(arg[0])) {
2292 ERR("Wrong value in --agent-tcp-port parameter: %s", arg);
2293 return -1;
2294 }
2295 if (v == 0 || v >= 65535) {
2296 ERR("Port overflow in --agent-tcp-port parameter: %s", arg);
2297 return -1;
2298 }
2288467f
JG
2299 config.agent_tcp_port.begin = config.agent_tcp_port.end = (int) v;
2300 DBG3("Agent TCP port set to non default: %i", (int) v);
26296c48 2301 }
c0232ea5 2302 } else if (string_match(optname, "load") || opt == 'l') {
66b2ce8e
JG
2303 if (!arg || *arg == '\0') {
2304 ret = -EINVAL;
2305 goto end;
2306 }
e8fa9fb0
MD
2307 if (lttng_is_setuid_setgid()) {
2308 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
2309 "-l, --load");
2310 } else {
e6142f2e
JG
2311 config_string_set(&config.load_session_path, strdup(arg));
2312 if (!config.load_session_path.value) {
e8fa9fb0
MD
2313 PERROR("strdup");
2314 ret = -ENOMEM;
2315 }
ef367a93 2316 }
c0232ea5 2317 } else if (string_match(optname, "kmod-probes")) {
66b2ce8e
JG
2318 if (!arg || *arg == '\0') {
2319 ret = -EINVAL;
2320 goto end;
2321 }
e8fa9fb0
MD
2322 if (lttng_is_setuid_setgid()) {
2323 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
2324 "--kmod-probes");
2325 } else {
e6142f2e
JG
2326 config_string_set(&config.kmod_probes_list, strdup(arg));
2327 if (!config.kmod_probes_list.value) {
e8fa9fb0
MD
2328 PERROR("strdup");
2329 ret = -ENOMEM;
2330 }
c9d42407 2331 }
c0232ea5 2332 } else if (string_match(optname, "extra-kmod-probes")) {
66b2ce8e
JG
2333 if (!arg || *arg == '\0') {
2334 ret = -EINVAL;
2335 goto end;
2336 }
e8fa9fb0
MD
2337 if (lttng_is_setuid_setgid()) {
2338 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
2339 "--extra-kmod-probes");
2340 } else {
e6142f2e
JG
2341 config_string_set(&config.kmod_extra_probes_list,
2342 strdup(arg));
2343 if (!config.kmod_extra_probes_list.value) {
e8fa9fb0
MD
2344 PERROR("strdup");
2345 ret = -ENOMEM;
2346 }
db322c4d 2347 }
c0232ea5
JG
2348 } else if (string_match(optname, "config") || opt == 'f') {
2349 /* This is handled in set_options() thus silent skip. */
2350 goto end;
2351 } else {
26296c48
JG
2352 /* Unknown option or other error.
2353 * Error is printed by getopt, just return */
2354 ret = -1;
2355 }
2356
f3dd7ce7 2357end:
c5d350b2
JG
2358 if (ret == -EINVAL) {
2359 const char *opt_name = "unknown";
2360 int i;
2361
2362 for (i = 0; i < sizeof(long_options) / sizeof(struct option);
2363 i++) {
2364 if (opt == long_options[i].val) {
2365 opt_name = long_options[i].name;
2366 break;
2367 }
2368 }
2369
2370 WARN("Invalid argument provided for option \"%s\", using default value.",
2371 opt_name);
2372 }
f3dd7ce7 2373
26296c48
JG
2374 return ret;
2375}
2376
2377/*
2378 * config_entry_handler_cb used to handle options read from a config file.
f40ef1d5 2379 * See config_entry_handler_cb comment in common/config/session-config.h for the
26296c48
JG
2380 * return value conventions.
2381 */
2382static int config_entry_handler(const struct config_entry *entry, void *unused)
2383{
2384 int ret = 0, i;
2385
2386 if (!entry || !entry->name || !entry->value) {
2387 ret = -EINVAL;
2388 goto end;
2389 }
2390
2391 /* Check if the option is to be ignored */
2392 for (i = 0; i < sizeof(config_ignore_options) / sizeof(char *); i++) {
2393 if (!strcmp(entry->name, config_ignore_options[i])) {
2394 goto end;
2395 }
2396 }
2397
2398 for (i = 0; i < (sizeof(long_options) / sizeof(struct option)) - 1;
2399 i++) {
2400
2401 /* Ignore if not fully matched. */
2402 if (strcmp(entry->name, long_options[i].name)) {
2403 continue;
2404 }
2405
2406 /*
2407 * If the option takes no argument on the command line, we have to
2408 * check if the value is "true". We support non-zero numeric values,
2409 * true, on and yes.
2410 */
2411 if (!long_options[i].has_arg) {
2412 ret = config_parse_value(entry->value);
2413 if (ret <= 0) {
2414 if (ret) {
2415 WARN("Invalid configuration value \"%s\" for option %s",
2416 entry->value, entry->name);
2417 }
2418 /* False, skip boolean config option. */
2419 goto end;
4d076222 2420 }
26296c48
JG
2421 }
2422
2423 ret = set_option(long_options[i].val, entry->value, entry->name);
2424 goto end;
2425 }
2426
2427 WARN("Unrecognized option \"%s\" in daemon configuration file.", entry->name);
2428
2429end:
2430 return ret;
2431}
2432
2433/*
2434 * daemon configuration loading and argument parsing
2435 */
2436static int set_options(int argc, char **argv)
2437{
2438 int ret = 0, c = 0, option_index = 0;
2439 int orig_optopt = optopt, orig_optind = optind;
2440 char *optstring;
2441 const char *config_path = NULL;
2442
2443 optstring = utils_generate_optstring(long_options,
2444 sizeof(long_options) / sizeof(struct option));
2445 if (!optstring) {
2446 ret = -ENOMEM;
2447 goto end;
2448 }
2449
2450 /* Check for the --config option */
2451 while ((c = getopt_long(argc, argv, optstring, long_options,
2452 &option_index)) != -1) {
2453 if (c == '?') {
2454 ret = -EINVAL;
2455 goto end;
2456 } else if (c != 'f') {
2457 /* if not equal to --config option. */
2458 continue;
2459 }
2460
e8fa9fb0
MD
2461 if (lttng_is_setuid_setgid()) {
2462 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
2463 "-f, --config");
2464 } else {
2465 config_path = utils_expand_path(optarg);
2466 if (!config_path) {
2467 ERR("Failed to resolve path: %s", optarg);
2468 }
26296c48
JG
2469 }
2470 }
2471
2472 ret = config_get_section_entries(config_path, config_section_name,
2473 config_entry_handler, NULL);
2474 if (ret) {
2475 if (ret > 0) {
2476 ERR("Invalid configuration option at line %i", ret);
2477 ret = -1;
2478 }
2479 goto end;
2480 }
2481
2482 /* Reset getopt's global state */
2483 optopt = orig_optopt;
2484 optind = orig_optind;
2485 while (1) {
c0232ea5
JG
2486 option_index = -1;
2487 /*
2488 * getopt_long() will not set option_index if it encounters a
2489 * short option.
2490 */
2491 c = getopt_long(argc, argv, optstring, long_options,
2492 &option_index);
26296c48 2493 if (c == -1) {
4d076222
DG
2494 break;
2495 }
26296c48 2496
c0232ea5
JG
2497 /*
2498 * Pass NULL as the long option name if popt left the index
2499 * unset.
2500 */
2501 ret = set_option(c, optarg,
2502 option_index < 0 ? NULL :
2503 long_options[option_index].name);
26296c48
JG
2504 if (ret < 0) {
2505 break;
fac6795d
DG
2506 }
2507 }
2508
26296c48
JG
2509end:
2510 free(optstring);
2511 return ret;
fac6795d
DG
2512}
2513
2514/*
917a718d 2515 * Creates the application socket.
fac6795d 2516 */
cf3af59e 2517static int init_daemon_socket(void)
fac6795d
DG
2518{
2519 int ret = 0;
2520 mode_t old_umask;
2521
2522 old_umask = umask(0);
2523
fac6795d 2524 /* Create the application unix socket */
e6142f2e 2525 apps_sock = lttcomm_create_unix_sock(config.apps_unix_sock_path.value);
d6f42150 2526 if (apps_sock < 0) {
e6142f2e 2527 ERR("Create unix sock failed: %s", config.apps_unix_sock_path.value);
fac6795d
DG
2528 ret = -1;
2529 goto end;
2530 }
2531
b662582b
DG
2532 /* Set the cloexec flag */
2533 ret = utils_set_fd_cloexec(apps_sock);
2534 if (ret < 0) {
2535 ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). "
2536 "Continuing but note that the consumer daemon will have a "
2537 "reference to this socket on exec()", apps_sock);
2538 }
2539
d6f42150 2540 /* File permission MUST be 666 */
e6142f2e 2541 ret = chmod(config.apps_unix_sock_path.value,
54d01ffb 2542 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
fac6795d 2543 if (ret < 0) {
e6142f2e 2544 ERR("Set file permissions failed: %s", config.apps_unix_sock_path.value);
76d7553f 2545 PERROR("chmod");
fac6795d
DG
2546 goto end;
2547 }
2548
917a718d
JG
2549 DBG3("Session daemon application socket %d created",
2550 apps_sock);
b662582b 2551
fac6795d
DG
2552end:
2553 umask(old_umask);
2554 return ret;
2555}
2556
f472090a
JG
2557/*
2558 * Create lockfile using the rundir and return its fd.
2559 */
2560static int create_lockfile(void)
2561{
2562 return utils_create_lock_file(config.lock_file_path.value);
2563}
2564
fac6795d 2565/*
54d01ffb
DG
2566 * Check if the global socket is available, and if a daemon is answering at the
2567 * other side. If yes, error is returned.
f472090a
JG
2568 *
2569 * Also attempts to create and hold the lock file.
fac6795d 2570 */
cf3af59e 2571static int check_existing_daemon(void)
fac6795d 2572{
f472090a
JG
2573 int ret = 0;
2574
7d8234d9 2575 /* Is there anybody out there ? */
099e26bd 2576 if (lttng_session_daemon_alive()) {
f472090a
JG
2577 ret = -EEXIST;
2578 goto end;
099e26bd 2579 }
b09c7c76 2580
f472090a
JG
2581 lockfile_fd = create_lockfile();
2582 if (lockfile_fd < 0) {
2583 ret = -EEXIST;
2584 goto end;
2585 }
2586end:
2587 return ret;
2588}
2589
2590static void sessiond_cleanup_lock_file(void)
2591{
2592 int ret;
2593
2594 /*
2595 * Cleanup lock file by deleting it and finaly closing it which will
2596 * release the file system lock.
2597 */
2598 if (lockfile_fd >= 0) {
2599 ret = remove(config.lock_file_path.value);
2600 if (ret < 0) {
2601 PERROR("remove lock file");
2602 }
2603 ret = close(lockfile_fd);
2604 if (ret < 0) {
2605 PERROR("close lock file");
2606 }
2607 }
fac6795d
DG
2608}
2609
fac6795d 2610/*
d063d709 2611 * Set the tracing group gid onto the client socket.
5e16da05 2612 *
d063d709 2613 * Race window between mkdir and chown is OK because we are going from more
d1613cf5 2614 * permissive (root.root) to less permissive (root.tracing).
fac6795d 2615 */
be040666 2616static int set_permissions(char *rundir)
fac6795d
DG
2617{
2618 int ret;
996b65c8 2619 gid_t gid;
fac6795d 2620
e6142f2e 2621 gid = utils_get_group_id(config.tracing_group_name.value);
fac6795d 2622
d6f42150 2623 /* Set lttng run dir */
be040666 2624 ret = chown(rundir, 0, gid);
d6f42150 2625 if (ret < 0) {
be040666 2626 ERR("Unable to set group on %s", rundir);
76d7553f 2627 PERROR("chown");
d6f42150
DG
2628 }
2629
6c71277b
MD
2630 /*
2631 * Ensure all applications and tracing group can search the run
2632 * dir. Allow everyone to read the directory, since it does not
2633 * buy us anything to hide its content.
2634 */
2635 ret = chmod(rundir, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
d1613cf5
JN
2636 if (ret < 0) {
2637 ERR("Unable to set permissions on %s", rundir);
76d7553f 2638 PERROR("chmod");
d1613cf5
JN
2639 }
2640
d6f42150 2641 /* lttng client socket path */
e6142f2e 2642 ret = chown(config.client_unix_sock_path.value, 0, gid);
fac6795d 2643 if (ret < 0) {
e6142f2e 2644 ERR("Unable to set group on %s", config.client_unix_sock_path.value);
76d7553f 2645 PERROR("chown");
d6f42150
DG
2646 }
2647
3bd1e081 2648 /* kconsumer error socket path */
6c71277b 2649 ret = chown(kconsumer_data.err_unix_sock_path, 0, 0);
d6f42150 2650 if (ret < 0) {
3bd1e081 2651 ERR("Unable to set group on %s", kconsumer_data.err_unix_sock_path);
76d7553f 2652 PERROR("chown");
3bd1e081
MD
2653 }
2654
7753dea8 2655 /* 64-bit ustconsumer error socket path */
6c71277b 2656 ret = chown(ustconsumer64_data.err_unix_sock_path, 0, 0);
7753dea8
MD
2657 if (ret < 0) {
2658 ERR("Unable to set group on %s", ustconsumer64_data.err_unix_sock_path);
76d7553f 2659 PERROR("chown");
7753dea8
MD
2660 }
2661
2662 /* 32-bit ustconsumer compat32 error socket path */
6c71277b 2663 ret = chown(ustconsumer32_data.err_unix_sock_path, 0, 0);
3bd1e081 2664 if (ret < 0) {
7753dea8 2665 ERR("Unable to set group on %s", ustconsumer32_data.err_unix_sock_path);
76d7553f 2666 PERROR("chown");
fac6795d
DG
2667 }
2668
d6f42150 2669 DBG("All permissions are set");
e07ae692 2670
fac6795d
DG
2671 return ret;
2672}
2673
d6f42150 2674/*
d063d709 2675 * Create the lttng run directory needed for all global sockets and pipe.
d6f42150 2676 */
e6142f2e 2677static int create_lttng_rundir(void)
d6f42150
DG
2678{
2679 int ret;
2680
e6142f2e 2681 DBG3("Creating LTTng run directory: %s", config.rundir.value);
67e40797 2682
e6142f2e 2683 ret = mkdir(config.rundir.value, S_IRWXU);
d6f42150 2684 if (ret < 0) {
b1f11e69 2685 if (errno != EEXIST) {
e6142f2e 2686 ERR("Unable to create %s", config.rundir.value);
b1f11e69
DG
2687 goto error;
2688 } else {
2689 ret = 0;
2690 }
d6f42150
DG
2691 }
2692
2693error:
2694 return ret;
2695}
2696
2697/*
e6142f2e 2698 * Setup sockets and directory needed by the consumerds' communication with the
d063d709 2699 * session daemon.
d6f42150 2700 */
e6142f2e 2701static int set_consumer_sockets(struct consumer_data *consumer_data)
d6f42150
DG
2702{
2703 int ret;
e6142f2e 2704 char *path = NULL;
d6f42150 2705
6c71277b 2706 switch (consumer_data->type) {
7753dea8 2707 case LTTNG_CONSUMER_KERNEL:
e6142f2e 2708 path = config.kconsumerd_path.value;
7753dea8
MD
2709 break;
2710 case LTTNG_CONSUMER64_UST:
e6142f2e 2711 path = config.consumerd64_path.value;
7753dea8
MD
2712 break;
2713 case LTTNG_CONSUMER32_UST:
e6142f2e 2714 path = config.consumerd32_path.value;
7753dea8
MD
2715 break;
2716 default:
2717 ERR("Consumer type unknown");
2718 ret = -EINVAL;
2719 goto error;
d6f42150 2720 }
e6142f2e 2721 assert(path);
d6f42150 2722
67e40797
DG
2723 DBG2("Creating consumer directory: %s", path);
2724
6c71277b 2725 ret = mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP);
e7e1ef17
JR
2726 if (ret < 0 && errno != EEXIST) {
2727 PERROR("mkdir");
2728 ERR("Failed to create %s", path);
2729 goto error;
d6f42150 2730 }
6c71277b 2731 if (is_root) {
e6142f2e 2732 ret = chown(path, 0, utils_get_group_id(config.tracing_group_name.value));
6c71277b
MD
2733 if (ret < 0) {
2734 ERR("Unable to set group on %s", path);
2735 PERROR("chown");
2736 goto error;
2737 }
2738 }
d6f42150 2739
e6142f2e 2740 /* Create the consumerd error unix socket */
3bd1e081
MD
2741 consumer_data->err_sock =
2742 lttcomm_create_unix_sock(consumer_data->err_unix_sock_path);
2743 if (consumer_data->err_sock < 0) {
2744 ERR("Create unix sock failed: %s", consumer_data->err_unix_sock_path);
d6f42150
DG
2745 ret = -1;
2746 goto error;
2747 }
2748
a24f05ab
MD
2749 /*
2750 * Set the CLOEXEC flag. Return code is useless because either way, the
2751 * show must go on.
2752 */
2753 ret = utils_set_fd_cloexec(consumer_data->err_sock);
2754 if (ret < 0) {
2755 PERROR("utils_set_fd_cloexec");
2756 /* continue anyway */
2757 }
2758
d6f42150 2759 /* File permission MUST be 660 */
3bd1e081 2760 ret = chmod(consumer_data->err_unix_sock_path,
54d01ffb 2761 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
d6f42150 2762 if (ret < 0) {
3bd1e081 2763 ERR("Set file permissions failed: %s", consumer_data->err_unix_sock_path);
67e40797 2764 PERROR("chmod");
d6f42150
DG
2765 goto error;
2766 }
2767
2768error:
2769 return ret;
2770}
2771
fac6795d 2772/*
d063d709 2773 * Signal handler for the daemon
cf3af59e 2774 *
54d01ffb
DG
2775 * Simply stop all worker threads, leaving main() return gracefully after
2776 * joining all threads and calling cleanup().
fac6795d
DG
2777 */
2778static void sighandler(int sig)
2779{
2780 switch (sig) {
cf3af59e 2781 case SIGINT:
af87c45a 2782 DBG("SIGINT caught");
cf3af59e
MD
2783 stop_threads();
2784 break;
2785 case SIGTERM:
af87c45a 2786 DBG("SIGTERM caught");
cf3af59e
MD
2787 stop_threads();
2788 break;
0bb7724a
DG
2789 case SIGUSR1:
2790 CMM_STORE_SHARED(recv_child_signal, 1);
2791 break;
cf3af59e
MD
2792 default:
2793 break;
fac6795d 2794 }
fac6795d
DG
2795}
2796
2797/*
d063d709 2798 * Setup signal handler for :
1d4b027a 2799 * SIGINT, SIGTERM, SIGPIPE
fac6795d 2800 */
1d4b027a 2801static int set_signal_handler(void)
fac6795d 2802{
1d4b027a
DG
2803 int ret = 0;
2804 struct sigaction sa;
2805 sigset_t sigset;
fac6795d 2806
1d4b027a 2807 if ((ret = sigemptyset(&sigset)) < 0) {
76d7553f 2808 PERROR("sigemptyset");
1d4b027a
DG
2809 return ret;
2810 }
d6f42150 2811
1d4b027a
DG
2812 sa.sa_mask = sigset;
2813 sa.sa_flags = 0;
0072e5e2
MD
2814
2815 sa.sa_handler = sighandler;
1d4b027a 2816 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
76d7553f 2817 PERROR("sigaction");
1d4b027a 2818 return ret;
d6f42150
DG
2819 }
2820
1d4b027a 2821 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
76d7553f 2822 PERROR("sigaction");
1d4b027a 2823 return ret;
d6f42150 2824 }
aaf26714 2825
0072e5e2 2826 if ((ret = sigaction(SIGUSR1, &sa, NULL)) < 0) {
76d7553f 2827 PERROR("sigaction");
1d4b027a 2828 return ret;
8c0faa1d
DG
2829 }
2830
0072e5e2
MD
2831 sa.sa_handler = SIG_IGN;
2832 if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) {
0bb7724a
DG
2833 PERROR("sigaction");
2834 return ret;
2835 }
2836
2837 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
1d4b027a
DG
2838
2839 return ret;
fac6795d
DG
2840}
2841
f3ed775e 2842/*
d063d709 2843 * Set open files limit to unlimited. This daemon can open a large number of
514bb9f0 2844 * file descriptors in order to consume multiple kernel traces.
f3ed775e
DG
2845 */
2846static void set_ulimit(void)
2847{
2848 int ret;
2849 struct rlimit lim;
2850
514bb9f0 2851 /* The kernel does not allow an infinite limit for open files */
f3ed775e
DG
2852 lim.rlim_cur = 65535;
2853 lim.rlim_max = 65535;
2854
2855 ret = setrlimit(RLIMIT_NOFILE, &lim);
2856 if (ret < 0) {
76d7553f 2857 PERROR("failed to set open files limit");
f3ed775e
DG
2858 }
2859}
2860
4a15001e 2861static int write_pidfile(void)
35f90c40 2862{
e6142f2e 2863 return utils_create_pid_file(getpid(), config.pid_file_path.value);
35f90c40
DG
2864}
2865
e6142f2e
JG
2866static int set_clock_plugin_env(void)
2867{
2868 int ret = 0;
2869 char *env_value = NULL;
cd9290dd 2870
e6142f2e
JG
2871 if (!config.lttng_ust_clock_plugin.value) {
2872 goto end;
2873 }
2874
2875 ret = asprintf(&env_value, "LTTNG_UST_CLOCK_PLUGIN=%s",
2876 config.lttng_ust_clock_plugin.value);
cd9290dd 2877 if (ret < 0) {
e6142f2e
JG
2878 PERROR("asprintf");
2879 goto end;
cd9290dd
DG
2880 }
2881
e6142f2e
JG
2882 ret = putenv(env_value);
2883 if (ret) {
2884 free(env_value);
2885 PERROR("putenv of LTTNG_UST_CLOCK_PLUGIN");
2886 goto end;
2887 }
cd9290dd 2888
e6142f2e
JG
2889 DBG("Updated LTTNG_UST_CLOCK_PLUGIN environment variable to \"%s\"",
2890 config.lttng_ust_clock_plugin.value);
2891end:
4a15001e 2892 return ret;
ef367a93
JG
2893}
2894
99d688f2
JG
2895static void destroy_all_sessions_and_wait(void)
2896{
2897 struct ltt_session *session, *tmp;
2898 struct ltt_session_list *session_list;
2899
2900 session_list = session_get_list();
2901 DBG("Initiating destruction of all sessions");
2902
2903 if (!session_list) {
2904 return;
2905 }
2906
99d688f2
JG
2907 session_lock_list();
2908 /* Initiate the destruction of all sessions. */
2909 cds_list_for_each_entry_safe(session, tmp,
2910 &session_list->head, list) {
2911 if (!session_get(session)) {
2912 continue;
2913 }
2914
2915 session_lock(session);
2916 if (session->destroyed) {
2917 goto unlock_session;
2918 }
2919 (void) cmd_destroy_session(session,
2920 notification_thread_handle);
2921 unlock_session:
2922 session_unlock(session);
2923 session_put(session);
2924 }
2925 session_unlock_list();
2926
2927 /* Wait for the destruction of all sessions to complete. */
2928 DBG("Waiting for the destruction of all sessions to complete");
2929 session_list_wait_empty();
2930 DBG("Destruction of all sessions completed");
2931}
2932
fac6795d
DG
2933/*
2934 * main
2935 */
2936int main(int argc, char **argv)
2937{
4a15001e 2938 int ret = 0, retval = 0;
fac6795d 2939 void *status;
e6142f2e 2940 const char *env_app_timeout;
b3530820
JG
2941 struct lttng_pipe *ust32_channel_monitor_pipe = NULL,
2942 *ust64_channel_monitor_pipe = NULL,
2943 *kernel_channel_monitor_pipe = NULL;
a3707772 2944 struct lttng_thread *ht_cleanup_thread = NULL;
bc26e826 2945 struct timer_thread_parameters timer_thread_parameters;
64d9b072
JG
2946 /* Rotation thread handle. */
2947 struct rotation_thread_handle *rotation_thread_handle = NULL;
d086f507
JD
2948 /* Queue of rotation jobs populated by the sessiond-timer. */
2949 struct rotation_thread_timer_queue *rotation_timer_queue = NULL;
917a718d 2950 struct lttng_thread *client_thread = NULL;
fac6795d 2951
335a95b7
MD
2952 init_kernel_workarounds();
2953
f6a9efaa
DG
2954 rcu_register_thread();
2955
4a15001e
MD
2956 if (set_signal_handler()) {
2957 retval = -1;
2958 goto exit_set_signal_handler;
0bb7724a
DG
2959 }
2960
92816cc3 2961 if (timer_signal_init()) {
d086f507
JD
2962 retval = -1;
2963 goto exit_set_signal_handler;
2964 }
2965
12744796
DG
2966 page_size = sysconf(_SC_PAGESIZE);
2967 if (page_size < 0) {
2968 PERROR("sysconf _SC_PAGESIZE");
2969 page_size = LONG_MAX;
2970 WARN("Fallback page size to %ld", page_size);
2971 }
2972
e6142f2e
JG
2973 ret = sessiond_config_init(&config);
2974 if (ret) {
2975 retval = -1;
2976 goto exit_set_signal_handler;
2977 }
2978
2788b494
JR
2979 /*
2980 * Init config from environment variables.
2981 * Command line option override env configuration per-doc. Do env first.
2982 */
2983 sessiond_config_apply_env_config(&config);
2984
4a15001e
MD
2985 /*
2986 * Parse arguments and load the daemon configuration file.
2987 *
2988 * We have an exit_options exit path to free memory reserved by
2989 * set_options. This is needed because the rest of sessiond_cleanup()
2990 * depends on ht_cleanup_thread, which depends on lttng_daemonize, which
2991 * depends on set_options.
2992 */
fac6795d 2993 progname = argv[0];
4a15001e
MD
2994 if (set_options(argc, argv)) {
2995 retval = -1;
2996 goto exit_options;
fac6795d
DG
2997 }
2998
e6142f2e
JG
2999 /*
3000 * Resolve all paths received as arguments, configuration option, or
3001 * through environment variable as absolute paths. This is necessary
3002 * since daemonizing causes the sessiond's current working directory
3003 * to '/'.
3004 */
3005 ret = sessiond_config_resolve_paths(&config);
3006 if (ret) {
3007 goto exit_options;
3008 }
3009
3010 /* Apply config. */
3011 lttng_opt_verbose = config.verbose;
3012 lttng_opt_quiet = config.quiet;
3013 kconsumer_data.err_unix_sock_path =
3014 config.kconsumerd_err_unix_sock_path.value;
3015 kconsumer_data.cmd_unix_sock_path =
3016 config.kconsumerd_cmd_unix_sock_path.value;
3017 ustconsumer32_data.err_unix_sock_path =
3018 config.consumerd32_err_unix_sock_path.value;
3019 ustconsumer32_data.cmd_unix_sock_path =
3020 config.consumerd32_cmd_unix_sock_path.value;
3021 ustconsumer64_data.err_unix_sock_path =
3022 config.consumerd64_err_unix_sock_path.value;
3023 ustconsumer64_data.cmd_unix_sock_path =
3024 config.consumerd64_cmd_unix_sock_path.value;
3025 set_clock_plugin_env();
3026
3027 sessiond_config_log(&config);
3028
f472090a
JG
3029 if (create_lttng_rundir()) {
3030 retval = -1;
3031 goto exit_options;
3032 }
3033
3034 /* Abort launch if a session daemon is already running. */
3035 if (check_existing_daemon()) {
3036 ERR("A session daemon is already running.");
3037 retval = -1;
3038 goto exit_options;
3039 }
3040
fac6795d 3041 /* Daemonize */
e6142f2e 3042 if (config.daemonize || config.background) {
ceed52b5
MD
3043 int i;
3044
72dd7491 3045 ret = lttng_daemonize(&child_ppid, &recv_child_signal,
e6142f2e 3046 !config.background);
53094c05 3047 if (ret < 0) {
4a15001e
MD
3048 retval = -1;
3049 goto exit_options;
53094c05 3050 }
0bb7724a 3051
ceed52b5 3052 /*
0bb7724a 3053 * We are in the child. Make sure all other file descriptors are
4a15001e 3054 * closed, in case we are called with more opened file
f472090a 3055 * descriptors than the standard ones and the lock file.
ceed52b5
MD
3056 */
3057 for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) {
f472090a
JG
3058 if (i == lockfile_fd) {
3059 continue;
3060 }
ceed52b5
MD
3061 (void) close(i);
3062 }
3063 }
3064
7567352f
MD
3065 if (run_as_create_worker(argv[0]) < 0) {
3066 goto exit_create_run_as_worker_cleanup;
3067 }
3068
4a15001e
MD
3069 /*
3070 * Starting from here, we can create threads. This needs to be after
3071 * lttng_daemonize due to RCU.
3072 */
3073
3074 /*
3075 * Initialize the health check subsystem. This call should set the
3076 * appropriate time values.
3077 */
3078 health_sessiond = health_app_create(NR_HEALTH_SESSIOND_TYPES);
3079 if (!health_sessiond) {
3080 PERROR("health_app_create error");
3081 retval = -1;
3082 goto exit_health_sessiond_cleanup;
3083 }
3084
4a15001e 3085 /* Create thread to clean up RCU hash tables */
a3707772
JG
3086 ht_cleanup_thread = launch_ht_cleanup_thread();
3087 if (!ht_cleanup_thread) {
4a15001e
MD
3088 retval = -1;
3089 goto exit_ht_cleanup;
3090 }
3091
ceed52b5 3092 /* Create thread quit pipe */
a7333da7 3093 if (sessiond_init_thread_quit_pipe()) {
4a15001e
MD
3094 retval = -1;
3095 goto exit_init_data;
fac6795d
DG
3096 }
3097
3098 /* Check if daemon is UID = 0 */
3099 is_root = !getuid();
fac6795d 3100 if (is_root) {
67e40797 3101 /* Create global run dir with root access */
67e40797 3102
b3530820
JG
3103 kernel_channel_monitor_pipe = lttng_pipe_open(0);
3104 if (!kernel_channel_monitor_pipe) {
3105 ERR("Failed to create kernel consumer channel monitor pipe");
3106 retval = -1;
3107 goto exit_init_data;
3108 }
3109 kconsumer_data.channel_monitor_pipe =
3110 lttng_pipe_release_writefd(
3111 kernel_channel_monitor_pipe);
3112 if (kconsumer_data.channel_monitor_pipe < 0) {
3113 retval = -1;
3114 goto exit_init_data;
3115 }
fac6795d
DG
3116 }
3117
5c827ce0
DG
3118 /* Set consumer initial state */
3119 kernel_consumerd_state = CONSUMER_STOPPED;
3120 ust_consumerd_state = CONSUMER_STOPPED;
3121
b3530820
JG
3122 ust32_channel_monitor_pipe = lttng_pipe_open(0);
3123 if (!ust32_channel_monitor_pipe) {
3124 ERR("Failed to create 32-bit user space consumer channel monitor pipe");
3125 retval = -1;
3126 goto exit_init_data;
3127 }
3128 ustconsumer32_data.channel_monitor_pipe = lttng_pipe_release_writefd(
3129 ust32_channel_monitor_pipe);
3130 if (ustconsumer32_data.channel_monitor_pipe < 0) {
3131 retval = -1;
3132 goto exit_init_data;
3133 }
62c43103 3134
d086f507 3135 /*
92816cc3
JG
3136 * The rotation_thread_timer_queue structure is shared between the
3137 * sessiond timer thread and the rotation thread. The main thread keeps
3138 * its ownership and destroys it when both threads have been joined.
d086f507 3139 */
92816cc3 3140 rotation_timer_queue = rotation_thread_timer_queue_create();
d086f507
JD
3141 if (!rotation_timer_queue) {
3142 retval = -1;
3143 goto exit_init_data;
3144 }
bc26e826
JG
3145 timer_thread_parameters.rotation_thread_job_queue =
3146 rotation_timer_queue;
67e40797 3147
b3530820
JG
3148 ust64_channel_monitor_pipe = lttng_pipe_open(0);
3149 if (!ust64_channel_monitor_pipe) {
3150 ERR("Failed to create 64-bit user space consumer channel monitor pipe");
3151 retval = -1;
3152 goto exit_init_data;
3153 }
3154 ustconsumer64_data.channel_monitor_pipe = lttng_pipe_release_writefd(
3155 ust64_channel_monitor_pipe);
3156 if (ustconsumer64_data.channel_monitor_pipe < 0) {
3157 retval = -1;
3158 goto exit_init_data;
3159 }
847177cd 3160
1427f9b2
DG
3161 /*
3162 * Init UST app hash table. Alloc hash table before this point since
3163 * cleanup() can get called after that point.
3164 */
4a15001e 3165 if (ust_app_ht_alloc()) {
ddbeb0f6 3166 ERR("Failed to allocate UST app hash table");
4a15001e
MD
3167 retval = -1;
3168 goto exit_init_data;
3169 }
1427f9b2 3170
6a4e4039
JG
3171 /*
3172 * Initialize agent app hash table. We allocate the hash table here
3173 * since cleanup() can get called after this point.
3174 */
3175 if (agent_app_ht_alloc()) {
3176 ERR("Failed to allocate Agent app hash table");
4a15001e
MD
3177 retval = -1;
3178 goto exit_init_data;
f20baf8e
DG
3179 }
3180
a88df331
DG
3181 /*
3182 * These actions must be executed as root. We do that *after* setting up
3183 * the sockets path because we MUST make the check for another daemon using
3184 * those paths *before* trying to set the kernel consumer sockets and init
3185 * kernel tracer.
3186 */
3187 if (is_root) {
e6142f2e 3188 if (set_consumer_sockets(&kconsumer_data)) {
4a15001e
MD
3189 retval = -1;
3190 goto exit_init_data;
7753dea8
MD
3191 }
3192
a88df331 3193 /* Setup kernel tracer */
e6142f2e 3194 if (!config.no_kernel) {
4fba7219 3195 init_kernel_tracer();
89588270
DG
3196 if (kernel_tracer_fd >= 0) {
3197 ret = syscall_init_table();
3198 if (ret < 0) {
4a15001e
MD
3199 ERR("Unable to populate syscall table. "
3200 "Syscall tracing won't work "
3201 "for this session daemon.");
89588270 3202 }
834978fd 3203 }
4fba7219 3204 }
a88df331
DG
3205
3206 /* Set ulimit for open files */
3207 set_ulimit();
fac6795d 3208 }
4063050c
MD
3209 /* init lttng_fd tracking must be done after set_ulimit. */
3210 lttng_fd_init();
fac6795d 3211
e6142f2e 3212 if (set_consumer_sockets(&ustconsumer64_data)) {
4a15001e
MD
3213 retval = -1;
3214 goto exit_init_data;
67e40797
DG
3215 }
3216
e6142f2e 3217 if (set_consumer_sockets(&ustconsumer32_data)) {
4a15001e
MD
3218 retval = -1;
3219 goto exit_init_data;
67e40797
DG
3220 }
3221
d6f42150 3222 /* Setup the needed unix socket */
4a15001e
MD
3223 if (init_daemon_socket()) {
3224 retval = -1;
3225 goto exit_init_data;
fac6795d
DG
3226 }
3227
3228 /* Set credentials to socket */
e6142f2e 3229 if (is_root && set_permissions(config.rundir.value)) {
4a15001e
MD
3230 retval = -1;
3231 goto exit_init_data;
fac6795d
DG
3232 }
3233
5b8719f5 3234 /* Get parent pid if -S, --sig-parent is specified. */
e6142f2e 3235 if (config.sig_parent) {
5b8719f5
DG
3236 ppid = getppid();
3237 }
3238
7a485870 3239 /* Setup the kernel pipe for waking up the kernel thread */
e6142f2e 3240 if (is_root && !config.no_kernel) {
4a15001e
MD
3241 if (utils_create_pipe_cloexec(kernel_poll_pipe)) {
3242 retval = -1;
3243 goto exit_init_data;
6620da75 3244 }
7a485870
DG
3245 }
3246
099e26bd 3247 /* Setup the thread apps communication pipe. */
4a15001e
MD
3248 if (utils_create_pipe_cloexec(apps_cmd_pipe)) {
3249 retval = -1;
3250 goto exit_init_data;
099e26bd
DG
3251 }
3252
d0b96690 3253 /* Setup the thread apps notify communication pipe. */
4a15001e
MD
3254 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe)) {
3255 retval = -1;
3256 goto exit_init_data;
d0b96690
DG
3257 }
3258
7972aab2
DG
3259 /* Initialize global buffer per UID and PID registry. */
3260 buffer_reg_init_uid_registry();
3261 buffer_reg_init_pid_registry();
3262
099e26bd 3263 /* Init UST command queue. */
8bdee6e2 3264 cds_wfcq_init(&ust_cmd_queue.head, &ust_cmd_queue.tail);
099e26bd 3265
2f77fc4b 3266 cmd_init();
00e2e675 3267
ae9e45b3
DG
3268 /* Check for the application socket timeout env variable. */
3269 env_app_timeout = getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV);
3270 if (env_app_timeout) {
e6142f2e 3271 config.app_socket_timeout = atoi(env_app_timeout);
ae9e45b3 3272 } else {
e6142f2e 3273 config.app_socket_timeout = DEFAULT_APP_SOCKET_RW_TIMEOUT;
ae9e45b3
DG
3274 }
3275
4a15001e
MD
3276 ret = write_pidfile();
3277 if (ret) {
3278 ERR("Error in write_pidfile");
3279 retval = -1;
3280 goto exit_init_data;
3281 }
35f90c40 3282
554831e7
MD
3283 /* Initialize communication library */
3284 lttcomm_init();
4a15001e 3285 /* Initialize TCP timeout values */
d831c249 3286 lttcomm_inet_init();
554831e7 3287
ef367a93 3288 if (load_session_init_data(&load_info) < 0) {
4a15001e
MD
3289 retval = -1;
3290 goto exit_init_data;
ef367a93 3291 }
e6142f2e 3292 load_info->path = config.load_session_path.value;
ef367a93 3293
12b4fb37 3294 /* Create health-check thread. */
173fca4f 3295 if (!launch_health_management_thread()) {
4a15001e 3296 retval = -1;
44a5e5eb
DG
3297 goto exit_health;
3298 }
3299
b3530820
JG
3300 /* notification_thread_data acquires the pipes' read side. */
3301 notification_thread_handle = notification_thread_handle_create(
3302 ust32_channel_monitor_pipe,
3303 ust64_channel_monitor_pipe,
c8a9de5a 3304 kernel_channel_monitor_pipe);
b3530820
JG
3305 if (!notification_thread_handle) {
3306 retval = -1;
3307 ERR("Failed to create notification thread shared data");
b3530820
JG
3308 goto exit_notification;
3309 }
3310
3311 /* Create notification thread. */
c8a9de5a 3312 if (!launch_notification_thread(notification_thread_handle)) {
b3530820 3313 retval = -1;
b3530820
JG
3314 goto exit_notification;
3315 }
3316
d086f507 3317 /* Create timer thread. */
bc26e826 3318 if (!launch_timer_thread(&timer_thread_parameters)) {
d086f507 3319 retval = -1;
d086f507
JD
3320 goto exit_notification;
3321 }
d086f507 3322
db66e574
JD
3323 /* rotation_thread_data acquires the pipes' read side. */
3324 rotation_thread_handle = rotation_thread_handle_create(
90936dcf 3325 rotation_timer_queue,
c8a9de5a 3326 notification_thread_handle);
db66e574
JD
3327 if (!rotation_thread_handle) {
3328 retval = -1;
3329 ERR("Failed to create rotation thread shared data");
3330 stop_threads();
3331 goto exit_rotation;
3332 }
db66e574
JD
3333
3334 /* Create rotation thread. */
64d9b072 3335 if (!launch_rotation_thread(rotation_thread_handle)) {
db66e574 3336 retval = -1;
db66e574
JD
3337 goto exit_rotation;
3338 }
3339
cf3af59e 3340 /* Create thread to manage the client socket */
917a718d
JG
3341 client_thread = launch_client_thread();
3342 if (!client_thread) {
4a15001e 3343 retval = -1;
cf3af59e
MD
3344 goto exit_client;
3345 }
fac6795d 3346
099e26bd 3347 /* Create thread to dispatch registration */
1a1a34b4 3348 ret = pthread_create(&dispatch_thread, default_pthread_attr(),
099e26bd 3349 thread_dispatch_ust_registration, (void *) NULL);
4a15001e
MD
3350 if (ret) {
3351 errno = ret;
76d7553f 3352 PERROR("pthread_create dispatch");
4a15001e 3353 retval = -1;
b3530820 3354 stop_threads();
099e26bd
DG
3355 goto exit_dispatch;
3356 }
3357
3358 /* Create thread to manage application registration. */
1a1a34b4 3359 ret = pthread_create(&reg_apps_thread, default_pthread_attr(),
099e26bd 3360 thread_registration_apps, (void *) NULL);
4a15001e
MD
3361 if (ret) {
3362 errno = ret;
76d7553f 3363 PERROR("pthread_create registration");
4a15001e 3364 retval = -1;
b3530820 3365 stop_threads();
099e26bd
DG
3366 goto exit_reg_apps;
3367 }
3368
cf3af59e 3369 /* Create thread to manage application socket */
1a1a34b4 3370 ret = pthread_create(&apps_thread, default_pthread_attr(),
54d01ffb 3371 thread_manage_apps, (void *) NULL);
4a15001e
MD
3372 if (ret) {
3373 errno = ret;
d0b96690 3374 PERROR("pthread_create apps");
4a15001e 3375 retval = -1;
b3530820 3376 stop_threads();
d0b96690
DG
3377 goto exit_apps;
3378 }
3379
3380 /* Create thread to manage application notify socket */
1a1a34b4 3381 ret = pthread_create(&apps_notify_thread, default_pthread_attr(),
d0b96690 3382 ust_thread_manage_notify, (void *) NULL);
4a15001e
MD
3383 if (ret) {
3384 errno = ret;
f5c32ef1 3385 PERROR("pthread_create notify");
4a15001e 3386 retval = -1;
b3530820 3387 stop_threads();
9563b0ad 3388 goto exit_apps_notify;
cf3af59e 3389 }
fac6795d 3390
022d91ba 3391 /* Create agent registration thread. */
1a1a34b4 3392 ret = pthread_create(&agent_reg_thread, default_pthread_attr(),
022d91ba 3393 agent_thread_manage_registration, (void *) NULL);
4a15001e
MD
3394 if (ret) {
3395 errno = ret;
022d91ba 3396 PERROR("pthread_create agent");
4a15001e 3397 retval = -1;
b3530820 3398 stop_threads();
022d91ba 3399 goto exit_agent_reg;
4d076222
DG
3400 }
3401
6620da75 3402 /* Don't start this thread if kernel tracing is not requested nor root */
e6142f2e 3403 if (is_root && !config.no_kernel) {
6620da75 3404 /* Create kernel thread to manage kernel event */
1a1a34b4 3405 ret = pthread_create(&kernel_thread, default_pthread_attr(),
6620da75 3406 thread_manage_kernel, (void *) NULL);
4a15001e
MD
3407 if (ret) {
3408 errno = ret;
6620da75 3409 PERROR("pthread_create kernel");
4a15001e 3410 retval = -1;
b3530820 3411 stop_threads();
6620da75
DG
3412 goto exit_kernel;
3413 }
ef367a93 3414 }
7a485870 3415
4a15001e 3416 /* Create session loading thread. */
1a1a34b4
MJ
3417 ret = pthread_create(&load_session_thread, default_pthread_attr(),
3418 thread_load_session, load_info);
4a15001e
MD
3419 if (ret) {
3420 errno = ret;
3421 PERROR("pthread_create load_session_thread");
3422 retval = -1;
b3530820 3423 stop_threads();
4a15001e
MD
3424 goto exit_load_session;
3425 }
3426
3427 /*
3428 * This is where we start awaiting program completion (e.g. through
3429 * signal that asks threads to teardown).
3430 */
3431
3432 ret = pthread_join(load_session_thread, &status);
3433 if (ret) {
3434 errno = ret;
3435 PERROR("pthread_join load_session_thread");
3436 retval = -1;
3437 }
99d688f2
JG
3438
3439 /* Initiate teardown once activity occurs on the quit pipe. */
3440 sessiond_wait_for_quit_pipe(-1U);
917a718d
JG
3441
3442 /*
3443 * Ensure that the client thread is no longer accepting new commands,
3444 * which could cause new sessions to be created.
3445 */
3446 if (!lttng_thread_shutdown(client_thread)) {
3447 ERR("Failed to shutdown the client thread, continuing teardown");
3448 lttng_thread_put(client_thread);
3449 client_thread = NULL;
3450 }
3451
99d688f2 3452 destroy_all_sessions_and_wait();
4a15001e 3453exit_load_session:
ef367a93 3454
e6142f2e 3455 if (is_root && !config.no_kernel) {
6620da75 3456 ret = pthread_join(kernel_thread, &status);
4a15001e
MD
3457 if (ret) {
3458 errno = ret;
6620da75 3459 PERROR("pthread_join");
4a15001e 3460 retval = -1;
6620da75 3461 }
fac6795d 3462 }
cf3af59e 3463exit_kernel:
4a15001e 3464
022d91ba 3465 ret = pthread_join(agent_reg_thread, &status);
4a15001e
MD
3466 if (ret) {
3467 errno = ret;
022d91ba 3468 PERROR("pthread_join agent");
4a15001e 3469 retval = -1;
4d076222 3470 }
022d91ba 3471exit_agent_reg:
4a15001e 3472
9563b0ad 3473 ret = pthread_join(apps_notify_thread, &status);
4a15001e
MD
3474 if (ret) {
3475 errno = ret;
9563b0ad 3476 PERROR("pthread_join apps notify");
4a15001e 3477 retval = -1;
9563b0ad 3478 }
9563b0ad 3479exit_apps_notify:
4a15001e 3480
cf3af59e 3481 ret = pthread_join(apps_thread, &status);
4a15001e
MD
3482 if (ret) {
3483 errno = ret;
9563b0ad 3484 PERROR("pthread_join apps");
4a15001e 3485 retval = -1;
cf3af59e 3486 }
cf3af59e 3487exit_apps:
4a15001e 3488
099e26bd 3489 ret = pthread_join(reg_apps_thread, &status);
4a15001e
MD
3490 if (ret) {
3491 errno = ret;
76d7553f 3492 PERROR("pthread_join");
4a15001e 3493 retval = -1;
099e26bd 3494 }
099e26bd 3495exit_reg_apps:
4a15001e 3496
772b8f4d
MD
3497 /*
3498 * Join dispatch thread after joining reg_apps_thread to ensure
3499 * we don't leak applications in the queue.
3500 */
099e26bd 3501 ret = pthread_join(dispatch_thread, &status);
4a15001e
MD
3502 if (ret) {
3503 errno = ret;
76d7553f 3504 PERROR("pthread_join");
4a15001e 3505 retval = -1;
099e26bd 3506 }
099e26bd 3507exit_dispatch:
b3530820 3508exit_client:
db66e574 3509exit_rotation:
b3530820 3510exit_notification:
a3707772 3511 lttng_thread_list_shutdown_orphans();
b3530820 3512exit_health:
4a15001e 3513exit_init_data:
917a718d
JG
3514 if (client_thread) {
3515 lttng_thread_put(client_thread);
3516 }
3517
4d62fbf8
MD
3518 /*
3519 * Wait for all pending call_rcu work to complete before tearing
3520 * down data structures. call_rcu worker may be trying to
3521 * perform lookups in those structures.
3522 */
3523 rcu_barrier();
4a15001e
MD
3524 /*
3525 * sessiond_cleanup() is called when no other thread is running, except
3526 * the ht_cleanup thread, which is needed to destroy the hash tables.
3527 */
3528 rcu_thread_online();
3529 sessiond_cleanup();
06f525de 3530
c4d5de9d
JG
3531 /*
3532 * Ensure all prior call_rcu are done. call_rcu callbacks may push
3533 * hash tables to the ht_cleanup thread. Therefore, we ensure that
3534 * the queue is empty before shutting down the clean-up thread.
3535 */
3536 rcu_barrier();
3537
a3707772
JG
3538 if (ht_cleanup_thread) {
3539 lttng_thread_shutdown(ht_cleanup_thread);
3540 lttng_thread_put(ht_cleanup_thread);
3541 }
3542
64d9b072
JG
3543 rcu_thread_offline();
3544 rcu_unregister_thread();
3545
3546 if (rotation_thread_handle) {
3547 rotation_thread_handle_destroy(rotation_thread_handle);
3548 }
3549
d086f507
JD
3550 /*
3551 * After the rotation and timer thread have quit, we can safely destroy
3552 * the rotation_timer_queue.
3553 */
92816cc3 3554 rotation_thread_timer_queue_destroy(rotation_timer_queue);
c8a9de5a
JG
3555 /*
3556 * The teardown of the notification system is performed after the
3557 * session daemon's teardown in order to allow it to be notified
3558 * of the active session and channels at the moment of the teardown.
3559 */
3560 if (notification_thread_handle) {
3561 notification_thread_handle_destroy(notification_thread_handle);
3562 }
b3530820
JG
3563 lttng_pipe_destroy(ust32_channel_monitor_pipe);
3564 lttng_pipe_destroy(ust64_channel_monitor_pipe);
3565 lttng_pipe_destroy(kernel_channel_monitor_pipe);
0b2dc8df 3566exit_ht_cleanup:
4a15001e 3567
4a15001e
MD
3568 health_app_destroy(health_sessiond);
3569exit_health_sessiond_cleanup:
7567352f 3570exit_create_run_as_worker_cleanup:
4a15001e
MD
3571
3572exit_options:
f472090a 3573 sessiond_cleanup_lock_file();
4a15001e
MD
3574 sessiond_cleanup_options();
3575
3576exit_set_signal_handler:
3577 if (!retval) {
cf3af59e 3578 exit(EXIT_SUCCESS);
4a15001e
MD
3579 } else {
3580 exit(EXIT_FAILURE);
67e40797 3581 }
fac6795d 3582}
This page took 0.458194 seconds and 5 git commands to generate.