Backport: Introduce EXTRA_VERSION_PATCHES
[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>
fac6795d 52
10a8a223 53#include "lttng-sessiond.h"
7972aab2 54#include "buffer-registry.h"
54d01ffb 55#include "channel.h"
2f77fc4b 56#include "cmd.h"
00e2e675 57#include "consumer.h"
099e26bd 58#include "context.h"
54d01ffb 59#include "event.h"
4771f025 60#include "kernel.h"
f1e16794 61#include "kernel-consumer.h"
096102bd 62#include "modprobe.h"
0fdd1e2c 63#include "shm.h"
1e307fab 64#include "ust-ctl.h"
00e2e675 65#include "ust-consumer.h"
8e68d1c8 66#include "utils.h"
4063050c 67#include "fd-limit.h"
8782cc74 68#include "health-sessiond.h"
8ac94142 69#include "testpoint.h"
d0b96690 70#include "ust-thread.h"
022d91ba 71#include "agent-thread.h"
fb198a11 72#include "save.h"
ef367a93 73#include "load-session-thread.h"
834978fd 74#include "syscall.h"
7c1d2758 75#include "agent.h"
5e97de00 76#include "ht-cleanup.h"
fac6795d 77
ebaeda94
MD
78#define CONSUMERD_FILE "lttng-consumerd"
79
fac6795d 80const char *progname;
6c71277b 81static const char *tracing_group_name = DEFAULT_TRACING_GROUP;
26296c48
JG
82static int tracing_group_name_override;
83static char *opt_pidfile;
5b8719f5 84static int opt_sig_parent;
97e19046 85static int opt_verbose_consumer;
72dd7491 86static int opt_daemon, opt_background;
4fba7219 87static int opt_no_kernel;
ef367a93 88static char *opt_load_session_path;
1d4b027a 89static pid_t ppid; /* Parent PID for --sig-parent option */
0bb7724a 90static pid_t child_ppid; /* Internal parent PID use with daemonize. */
67e40797 91static char *rundir;
c9cb3e7d 92static int lockfile_fd = -1;
8ff7624a 93static int opt_print_version;
3bd1e081 94
0bb7724a
DG
95/* Set to 1 when a SIGUSR1 signal is received. */
96static int recv_child_signal;
97
a23ec3a7
DG
98/*
99 * Consumer daemon specific control data. Every value not initialized here is
100 * set to 0 by the static definition.
101 */
3bd1e081
MD
102static struct consumer_data kconsumer_data = {
103 .type = LTTNG_CONSUMER_KERNEL,
60922cb0
DG
104 .err_unix_sock_path = DEFAULT_KCONSUMERD_ERR_SOCK_PATH,
105 .cmd_unix_sock_path = DEFAULT_KCONSUMERD_CMD_SOCK_PATH,
03550b58
MD
106 .err_sock = -1,
107 .cmd_sock = -1,
173af62f
DG
108 .pid_mutex = PTHREAD_MUTEX_INITIALIZER,
109 .lock = PTHREAD_MUTEX_INITIALIZER,
a23ec3a7
DG
110 .cond = PTHREAD_COND_INITIALIZER,
111 .cond_mutex = PTHREAD_MUTEX_INITIALIZER,
3bd1e081 112};
7753dea8
MD
113static struct consumer_data ustconsumer64_data = {
114 .type = LTTNG_CONSUMER64_UST,
60922cb0
DG
115 .err_unix_sock_path = DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH,
116 .cmd_unix_sock_path = DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH,
03550b58
MD
117 .err_sock = -1,
118 .cmd_sock = -1,
173af62f
DG
119 .pid_mutex = PTHREAD_MUTEX_INITIALIZER,
120 .lock = PTHREAD_MUTEX_INITIALIZER,
a23ec3a7
DG
121 .cond = PTHREAD_COND_INITIALIZER,
122 .cond_mutex = PTHREAD_MUTEX_INITIALIZER,
7753dea8
MD
123};
124static struct consumer_data ustconsumer32_data = {
125 .type = LTTNG_CONSUMER32_UST,
60922cb0
DG
126 .err_unix_sock_path = DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH,
127 .cmd_unix_sock_path = DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH,
03550b58
MD
128 .err_sock = -1,
129 .cmd_sock = -1,
173af62f
DG
130 .pid_mutex = PTHREAD_MUTEX_INITIALIZER,
131 .lock = PTHREAD_MUTEX_INITIALIZER,
a23ec3a7
DG
132 .cond = PTHREAD_COND_INITIALIZER,
133 .cond_mutex = PTHREAD_MUTEX_INITIALIZER,
3bd1e081
MD
134};
135
26296c48
JG
136/* Command line options */
137static const struct option long_options[] = {
0f5ea17c
JG
138 { "client-sock", required_argument, 0, 'c' },
139 { "apps-sock", required_argument, 0, 'a' },
140 { "kconsumerd-cmd-sock", required_argument, 0, '\0' },
141 { "kconsumerd-err-sock", required_argument, 0, '\0' },
142 { "ustconsumerd32-cmd-sock", required_argument, 0, '\0' },
143 { "ustconsumerd32-err-sock", required_argument, 0, '\0' },
144 { "ustconsumerd64-cmd-sock", required_argument, 0, '\0' },
145 { "ustconsumerd64-err-sock", required_argument, 0, '\0' },
146 { "consumerd32-path", required_argument, 0, '\0' },
147 { "consumerd32-libdir", required_argument, 0, '\0' },
148 { "consumerd64-path", required_argument, 0, '\0' },
149 { "consumerd64-libdir", required_argument, 0, '\0' },
150 { "daemonize", no_argument, 0, 'd' },
151 { "background", no_argument, 0, 'b' },
152 { "sig-parent", no_argument, 0, 'S' },
153 { "help", no_argument, 0, 'h' },
154 { "group", required_argument, 0, 'g' },
155 { "version", no_argument, 0, 'V' },
156 { "quiet", no_argument, 0, 'q' },
157 { "verbose", no_argument, 0, 'v' },
158 { "verbose-consumer", no_argument, 0, '\0' },
159 { "no-kernel", no_argument, 0, '\0' },
160 { "pidfile", required_argument, 0, 'p' },
161 { "agent-tcp-port", required_argument, 0, '\0' },
162 { "config", required_argument, 0, 'f' },
163 { "load", required_argument, 0, 'l' },
164 { "kmod-probes", required_argument, 0, '\0' },
165 { "extra-kmod-probes", required_argument, 0, '\0' },
26296c48
JG
166 { NULL, 0, 0, 0 }
167};
168
169/* Command line options to ignore from configuration file */
170static const char *config_ignore_options[] = { "help", "version", "config" };
171
26c9d55e 172/* Shared between threads */
099e26bd 173static int dispatch_thread_exit;
fac6795d 174
54d01ffb
DG
175/* Global application Unix socket path */
176static char apps_unix_sock_path[PATH_MAX];
177/* Global client Unix socket path */
178static char client_unix_sock_path[PATH_MAX];
54d01ffb
DG
179/* global wait shm path for UST */
180static char wait_shm_path[PATH_MAX];
44a5e5eb
DG
181/* Global health check unix path */
182static char health_unix_sock_path[PATH_MAX];
fac6795d 183
1d4b027a 184/* Sockets and FDs */
a4b35e07
MD
185static int client_sock = -1;
186static int apps_sock = -1;
2f77fc4b 187int kernel_tracer_fd = -1;
76d7553f 188static int kernel_poll_pipe[2] = { -1, -1 };
1d4b027a 189
273ea72c
DG
190/*
191 * Quit pipe for all threads. This permits a single cancellation point
192 * for all threads when receiving an event on the pipe.
193 */
76d7553f 194static int thread_quit_pipe[2] = { -1, -1 };
273ea72c 195
099e26bd
DG
196/*
197 * This pipe is used to inform the thread managing application communication
198 * that a command is queued and ready to be processed.
199 */
76d7553f 200static int apps_cmd_pipe[2] = { -1, -1 };
099e26bd 201
d0b96690
DG
202int apps_cmd_notify_pipe[2] = { -1, -1 };
203
1d4b027a 204/* Pthread, Mutexes and Semaphores */
1d4b027a 205static pthread_t apps_thread;
d0b96690 206static pthread_t apps_notify_thread;
099e26bd 207static pthread_t reg_apps_thread;
1d4b027a 208static pthread_t client_thread;
7a485870 209static pthread_t kernel_thread;
099e26bd 210static pthread_t dispatch_thread;
44a5e5eb 211static pthread_t health_thread;
0b2dc8df 212static pthread_t ht_cleanup_thread;
022d91ba 213static pthread_t agent_reg_thread;
ef367a93 214static pthread_t load_session_thread;
5eb91c98 215
099e26bd
DG
216/*
217 * UST registration command queue. This queue is tied with a futex and uses a N
218 * wakers / 1 waiter implemented and detailed in futex.c/.h
219 *
b22c5da8
DG
220 * The thread_registration_apps and thread_dispatch_ust_registration uses this
221 * queue along with the wait/wake scheme. The thread_manage_apps receives down
222 * the line new application socket and monitors it for any I/O error or clean
223 * close that triggers an unregistration of the application.
099e26bd
DG
224 */
225static struct ust_cmd_queue ust_cmd_queue;
226
b5541356
DG
227/*
228 * Pointer initialized before thread creation.
229 *
230 * This points to the tracing session list containing the session count and a
231 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
232 * MUST NOT be taken if you call a public function in session.c.
04ea676f 233 *
d063d709 234 * The lock is nested inside the structure: session_list_ptr->lock. Please use
54d01ffb 235 * session_lock_list and session_unlock_list for lock acquisition.
b5541356
DG
236 */
237static struct ltt_session_list *session_list_ptr;
238
7753dea8
MD
239int ust_consumerd64_fd = -1;
240int ust_consumerd32_fd = -1;
241
fb6f1fa2
YB
242static const char *consumerd32_bin = CONFIG_CONSUMERD32_BIN;
243static const char *consumerd64_bin = CONFIG_CONSUMERD64_BIN;
244static const char *consumerd32_libdir = CONFIG_CONSUMERD32_LIBDIR;
245static const char *consumerd64_libdir = CONFIG_CONSUMERD64_LIBDIR;
26296c48
JG
246static int consumerd32_bin_override;
247static int consumerd64_bin_override;
248static int consumerd32_libdir_override;
249static int consumerd64_libdir_override;
fb09408a 250
2f77fc4b
DG
251static const char *module_proc_lttng = "/proc/lttng";
252
5c827ce0
DG
253/*
254 * Consumer daemon state which is changed when spawning it, killing it or in
255 * case of a fatal error.
256 */
257enum consumerd_state {
258 CONSUMER_STARTED = 1,
259 CONSUMER_STOPPED = 2,
260 CONSUMER_ERROR = 3,
261};
262
263/*
264 * This consumer daemon state is used to validate if a client command will be
265 * able to reach the consumer. If not, the client is informed. For instance,
266 * doing a "lttng start" when the consumer state is set to ERROR will return an
267 * error to the client.
268 *
269 * The following example shows a possible race condition of this scheme:
270 *
271 * consumer thread error happens
272 * client cmd arrives
273 * client cmd checks state -> still OK
274 * consumer thread exit, sets error
275 * client cmd try to talk to consumer
276 * ...
277 *
278 * However, since the consumer is a different daemon, we have no way of making
279 * sure the command will reach it safely even with this state flag. This is why
280 * we consider that up to the state validation during command processing, the
281 * command is safe. After that, we can not guarantee the correctness of the
282 * client request vis-a-vis the consumer.
283 */
284static enum consumerd_state ust_consumerd_state;
285static enum consumerd_state kernel_consumerd_state;
286
ae9e45b3
DG
287/*
288 * Socket timeout for receiving and sending in seconds.
289 */
290static int app_socket_timeout;
291
12744796
DG
292/* Set in main() with the current page size. */
293long page_size;
294
8782cc74
MD
295/* Application health monitoring */
296struct health_app *health_sessiond;
297
022d91ba
DG
298/* Agent TCP port for registration. Used by the agent thread. */
299unsigned int agent_tcp_port = DEFAULT_AGENT_TCP_PORT;
4d076222 300
f43f95a9
DG
301/* Am I root or not. */
302int is_root; /* Set to 1 if the daemon is running as root */
303
26296c48
JG
304const char * const config_section_name = "sessiond";
305
ef367a93
JG
306/* Load session thread information to operate. */
307struct load_session_thread_data *load_info;
308
7c1d2758
JG
309/* Global hash tables */
310struct lttng_ht *agent_apps_ht_by_sock = NULL;
311
97bc1426 312/*
f15331c0
JG
313 * The initialization of the session daemon is done in multiple phases.
314 *
315 * While all threads are launched near-simultaneously, only some of them
316 * are needed to ensure the session daemon can start to respond to client
317 * requests.
318 *
319 * There are two important guarantees that we wish to offer with respect
320 * to the initialisation of the session daemon:
321 * - When the daemonize/background launcher process exits, the sessiond
322 * is fully able to respond to client requests,
323 * - Auto-loaded sessions are visible to clients.
324 *
325 * In order to achieve this, a number of support threads have to be launched
326 * to allow the "client" thread to function properly. Moreover, since the
327 * "load session" thread needs the client thread, we must provide a way
328 * for the "load session" thread to know that the "client" thread is up
329 * and running.
330 *
331 * Hence, the support threads decrement the lttng_sessiond_ready counter
332 * while the "client" threads waits for it to reach 0. Once the "client" thread
333 * unblocks, it posts the message_thread_ready semaphore which allows the
334 * "load session" thread to progress.
335 *
336 * This implies that the "load session" thread is the last to be initialized
337 * and will explicitly call sessiond_signal_parents(), which signals the parents
338 * that the session daemon is fully initialized.
339 *
340 * The two (2) support threads are:
341 * - agent_thread
342 * - health_thread
97bc1426 343 */
f15331c0 344int lttng_sessiond_ready = 2;
97bc1426 345
5e97de00
JG
346int sessiond_check_thread_quit_pipe(int fd, uint32_t events)
347{
348 return (fd == thread_quit_pipe[0] && (events & LPOLLIN)) ? 1 : 0;
349}
350
97bc1426 351/* Notify parents that we are ready for cmd and health check */
ef367a93 352LTTNG_HIDDEN
f15331c0 353void sessiond_signal_parents(void)
97bc1426 354{
f15331c0
JG
355 /*
356 * Notify parent pid that we are ready to accept command
357 * for client side. This ppid is the one from the
358 * external process that spawned us.
359 */
360 if (opt_sig_parent) {
361 kill(ppid, SIGUSR1);
362 }
97bc1426 363
f15331c0
JG
364 /*
365 * Notify the parent of the fork() process that we are
366 * ready.
367 */
368 if (opt_daemon || opt_background) {
369 kill(child_ppid, SIGUSR1);
97bc1426
MD
370 }
371}
372
f15331c0
JG
373LTTNG_HIDDEN
374void sessiond_notify_ready(void)
375{
376 /*
377 * The _return variant is used since the implied memory barriers are
378 * required.
379 */
380 (void) uatomic_sub_return(&lttng_sessiond_ready, 1);
381}
382
fb09408a 383static
7753dea8 384void setup_consumerd_path(void)
fb09408a 385{
fc7a59ce 386 const char *bin, *libdir;
fb09408a 387
7753dea8
MD
388 /*
389 * Allow INSTALL_BIN_PATH to be used as a target path for the
ebaeda94
MD
390 * native architecture size consumer if CONFIG_CONSUMER*_PATH
391 * has not been defined.
7753dea8 392 */
ebaeda94 393#if (CAA_BITS_PER_LONG == 32)
fc7a59ce
AM
394 if (!consumerd32_bin[0]) {
395 consumerd32_bin = INSTALL_BIN_PATH "/" CONSUMERD_FILE;
ebaeda94
MD
396 }
397 if (!consumerd32_libdir[0]) {
398 consumerd32_libdir = INSTALL_LIB_PATH;
399 }
400#elif (CAA_BITS_PER_LONG == 64)
fc7a59ce
AM
401 if (!consumerd64_bin[0]) {
402 consumerd64_bin = INSTALL_BIN_PATH "/" CONSUMERD_FILE;
7753dea8 403 }
ebaeda94
MD
404 if (!consumerd64_libdir[0]) {
405 consumerd64_libdir = INSTALL_LIB_PATH;
7753dea8
MD
406 }
407#else
408#error "Unknown bitness"
409#endif
410
fb09408a
MD
411 /*
412 * runtime env. var. overrides the build default.
413 */
e8fa9fb0 414 bin = lttng_secure_getenv("LTTNG_CONSUMERD32_BIN");
fc7a59ce
AM
415 if (bin) {
416 consumerd32_bin = bin;
7753dea8 417 }
e8fa9fb0 418 bin = lttng_secure_getenv("LTTNG_CONSUMERD64_BIN");
fc7a59ce
AM
419 if (bin) {
420 consumerd64_bin = bin;
ebaeda94 421 }
e8fa9fb0 422 libdir = lttng_secure_getenv("LTTNG_CONSUMERD32_LIBDIR");
ebaeda94
MD
423 if (libdir) {
424 consumerd32_libdir = libdir;
425 }
e8fa9fb0 426 libdir = lttng_secure_getenv("LTTNG_CONSUMERD64_LIBDIR");
ebaeda94
MD
427 if (libdir) {
428 consumerd64_libdir = libdir;
fb09408a
MD
429 }
430}
431
4a15001e
MD
432static
433int __sessiond_set_thread_pollset(struct lttng_poll_event *events, size_t size,
434 int *a_pipe)
5eb91c98
DG
435{
436 int ret;
437
d0b96690 438 assert(events);
5eb91c98
DG
439
440 ret = lttng_poll_create(events, size, LTTNG_CLOEXEC);
441 if (ret < 0) {
442 goto error;
443 }
444
445 /* Add quit pipe */
4a15001e 446 ret = lttng_poll_add(events, a_pipe[0], LPOLLIN | LPOLLERR);
5eb91c98
DG
447 if (ret < 0) {
448 goto error;
449 }
450
451 return 0;
452
453error:
454 return ret;
455}
456
4a15001e
MD
457/*
458 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
459 */
460int sessiond_set_thread_pollset(struct lttng_poll_event *events, size_t size)
461{
462 return __sessiond_set_thread_pollset(events, size, thread_quit_pipe);
463}
464
273ea72c 465/*
5eb91c98 466 * Init thread quit pipe.
273ea72c
DG
467 *
468 * Return -1 on error or 0 if all pipes are created.
469 */
4a15001e 470static int __init_thread_quit_pipe(int *a_pipe)
273ea72c 471{
730389d9 472 int ret, i;
273ea72c 473
4a15001e 474 ret = pipe(a_pipe);
273ea72c 475 if (ret < 0) {
730389d9 476 PERROR("thread quit pipe");
273ea72c
DG
477 goto error;
478 }
479
730389d9 480 for (i = 0; i < 2; i++) {
4a15001e 481 ret = fcntl(a_pipe[i], F_SETFD, FD_CLOEXEC);
730389d9
DG
482 if (ret < 0) {
483 PERROR("fcntl");
484 goto error;
485 }
486 }
487
273ea72c
DG
488error:
489 return ret;
490}
491
4a15001e
MD
492static int init_thread_quit_pipe(void)
493{
494 return __init_thread_quit_pipe(thread_quit_pipe);
495}
496
099e26bd
DG
497/*
498 * Stop all threads by closing the thread quit pipe.
499 */
cf3af59e
MD
500static void stop_threads(void)
501{
5eb91c98
DG
502 int ret;
503
cf3af59e
MD
504 /* Stopping all threads */
505 DBG("Terminating all threads");
54d01ffb 506 ret = notify_thread_pipe(thread_quit_pipe[1]);
5eb91c98
DG
507 if (ret < 0) {
508 ERR("write error on thread quit pipe");
509 }
510
099e26bd 511 /* Dispatch thread */
26c9d55e 512 CMM_STORE_SHARED(dispatch_thread_exit, 1);
099e26bd 513 futex_nto1_wake(&ust_cmd_queue.futex);
cf3af59e
MD
514}
515
e975f9f8
DG
516/*
517 * Close every consumer sockets.
518 */
519static void close_consumer_sockets(void)
520{
521 int ret;
522
523 if (kconsumer_data.err_sock >= 0) {
524 ret = close(kconsumer_data.err_sock);
525 if (ret < 0) {
526 PERROR("kernel consumer err_sock close");
527 }
528 }
529 if (ustconsumer32_data.err_sock >= 0) {
530 ret = close(ustconsumer32_data.err_sock);
531 if (ret < 0) {
a76cbd9f 532 PERROR("UST consumerd32 err_sock close");
e975f9f8
DG
533 }
534 }
535 if (ustconsumer64_data.err_sock >= 0) {
536 ret = close(ustconsumer64_data.err_sock);
537 if (ret < 0) {
a76cbd9f 538 PERROR("UST consumerd64 err_sock close");
e975f9f8
DG
539 }
540 }
541 if (kconsumer_data.cmd_sock >= 0) {
542 ret = close(kconsumer_data.cmd_sock);
543 if (ret < 0) {
544 PERROR("kernel consumer cmd_sock close");
545 }
546 }
547 if (ustconsumer32_data.cmd_sock >= 0) {
548 ret = close(ustconsumer32_data.cmd_sock);
549 if (ret < 0) {
a76cbd9f 550 PERROR("UST consumerd32 cmd_sock close");
e975f9f8
DG
551 }
552 }
553 if (ustconsumer64_data.cmd_sock >= 0) {
554 ret = close(ustconsumer64_data.cmd_sock);
555 if (ret < 0) {
a76cbd9f 556 PERROR("UST consumerd64 cmd_sock close");
e975f9f8
DG
557 }
558 }
559}
560
c9cb3e7d
JG
561/*
562 * Generate the full lock file path using the rundir.
563 *
564 * Return the snprintf() return value thus a negative value is an error.
565 */
566static int generate_lock_file_path(char *path, size_t len)
567{
568 int ret;
569
570 assert(path);
571 assert(rundir);
572
573 /* Build lockfile path from rundir. */
574 ret = snprintf(path, len, "%s/" DEFAULT_LTTNG_SESSIOND_LOCKFILE, rundir);
575 if (ret < 0) {
576 PERROR("snprintf lockfile path");
577 }
578
579 return ret;
580}
581
4e4714cb
JR
582/*
583 * Wait on consumer process termination.
584 *
585 * Need to be called with the consumer data lock held or from a context
586 * ensuring no concurrent access to data (e.g: cleanup).
587 */
588static void wait_consumer(struct consumer_data *consumer_data)
589{
590 pid_t ret;
591 int status;
592
593 if (consumer_data->pid <= 0) {
594 return;
595 }
596
597 DBG("Waiting for complete teardown of consumerd (PID: %d)",
598 consumer_data->pid);
599 ret = waitpid(consumer_data->pid, &status, 0);
600 if (ret == -1) {
601 PERROR("consumerd waitpid pid: %d", consumer_data->pid)
78f1416a 602 } else if (!WIFEXITED(status)) {
4e4714cb
JR
603 ERR("consumerd termination with error: %d",
604 WEXITSTATUS(ret));
605 }
606 consumer_data->pid = 0;
607}
608
fac6795d 609/*
4a15001e 610 * Cleanup the session daemon's data structures.
fac6795d 611 */
4a15001e 612static void sessiond_cleanup(void)
fac6795d 613{
ef599319 614 int ret;
af9737e9 615 struct ltt_session *sess, *stmp;
8c6c56c2 616 char path[PATH_MAX];
fac6795d 617
4a15001e 618 DBG("Cleanup sessiond");
e07ae692 619
4e449f3f
MD
620 /*
621 * Close the thread quit pipe. It has already done its job,
622 * since we are now called.
623 */
2f77fc4b
DG
624 utils_close_pipe(thread_quit_pipe);
625
35f90c40
DG
626 /*
627 * If opt_pidfile is undefined, the default file will be wiped when
628 * removing the rundir.
629 */
630 if (opt_pidfile) {
631 ret = remove(opt_pidfile);
632 if (ret < 0) {
633 PERROR("remove pidfile %s", opt_pidfile);
634 }
635 }
636
8c6c56c2
MD
637 DBG("Removing sessiond and consumerd content of directory %s", rundir);
638
639 /* sessiond */
640 snprintf(path, PATH_MAX,
641 "%s/%s",
642 rundir, DEFAULT_LTTNG_SESSIOND_PIDFILE);
643 DBG("Removing %s", path);
644 (void) unlink(path);
645
cd9290dd 646 snprintf(path, PATH_MAX, "%s/%s", rundir,
022d91ba 647 DEFAULT_LTTNG_SESSIOND_AGENTPORT_FILE);
cd9290dd
DG
648 DBG("Removing %s", path);
649 (void) unlink(path);
650
8c6c56c2
MD
651 /* kconsumerd */
652 snprintf(path, PATH_MAX,
653 DEFAULT_KCONSUMERD_ERR_SOCK_PATH,
654 rundir);
655 DBG("Removing %s", path);
656 (void) unlink(path);
657
658 snprintf(path, PATH_MAX,
659 DEFAULT_KCONSUMERD_PATH,
660 rundir);
661 DBG("Removing directory %s", path);
662 (void) rmdir(path);
663
664 /* ust consumerd 32 */
665 snprintf(path, PATH_MAX,
666 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH,
667 rundir);
668 DBG("Removing %s", path);
669 (void) unlink(path);
670
671 snprintf(path, PATH_MAX,
672 DEFAULT_USTCONSUMERD32_PATH,
673 rundir);
674 DBG("Removing directory %s", path);
675 (void) rmdir(path);
676
677 /* ust consumerd 64 */
678 snprintf(path, PATH_MAX,
679 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH,
680 rundir);
681 DBG("Removing %s", path);
682 (void) unlink(path);
683
684 snprintf(path, PATH_MAX,
685 DEFAULT_USTCONSUMERD64_PATH,
686 rundir);
687 DBG("Removing directory %s", path);
688 (void) rmdir(path);
5461b305 689
99bab54f 690 DBG("Cleaning up all sessions");
fac6795d 691
b5541356 692 /* Destroy session list mutex */
273ea72c
DG
693 if (session_list_ptr != NULL) {
694 pthread_mutex_destroy(&session_list_ptr->lock);
695
696 /* Cleanup ALL session */
54d01ffb
DG
697 cds_list_for_each_entry_safe(sess, stmp,
698 &session_list_ptr->head, list) {
2f77fc4b 699 cmd_destroy_session(sess, kernel_poll_pipe[1]);
273ea72c
DG
700 }
701 }
702
4e4714cb
JR
703 wait_consumer(&kconsumer_data);
704 wait_consumer(&ustconsumer64_data);
705 wait_consumer(&ustconsumer32_data);
706
6a4e4039
JG
707 DBG("Cleaning up all agent apps");
708 agent_app_ht_clean();
709
099e26bd 710 DBG("Closing all UST sockets");
56fff090 711 ust_app_clean_list();
7972aab2 712 buffer_reg_destroy_registries();
099e26bd 713
4fba7219
DG
714 if (is_root && !opt_no_kernel) {
715 DBG2("Closing kernel fd");
a4b35e07 716 if (kernel_tracer_fd >= 0) {
76d7553f
MD
717 ret = close(kernel_tracer_fd);
718 if (ret) {
719 PERROR("close");
720 }
a4b35e07 721 }
2f50c8a3 722 DBG("Unloading kernel modules");
096102bd 723 modprobe_remove_lttng_all();
834978fd 724 free(syscall_table);
2f50c8a3 725 }
2f77fc4b 726
e975f9f8
DG
727 close_consumer_sockets();
728
ef367a93
JG
729 if (load_info) {
730 load_session_destroy_data(load_info);
731 free(load_info);
732 }
733
c9cb3e7d
JG
734 /*
735 * Cleanup lock file by deleting it and finaly closing it which will
736 * release the file system lock.
737 */
738 if (lockfile_fd >= 0) {
739 char lockfile_path[PATH_MAX];
740
4a15001e
MD
741 ret = generate_lock_file_path(lockfile_path,
742 sizeof(lockfile_path));
c9cb3e7d
JG
743 if (ret > 0) {
744 ret = remove(lockfile_path);
745 if (ret < 0) {
746 PERROR("remove lock file");
747 }
748 ret = close(lockfile_fd);
749 if (ret < 0) {
750 PERROR("close lock file");
751 }
752 }
753 }
754
755 /*
756 * We do NOT rmdir rundir because there are other processes
757 * using it, for instance lttng-relayd, which can start in
758 * parallel with this teardown.
759 */
760
761 free(rundir);
4a15001e
MD
762}
763
764/*
765 * Cleanup the daemon's option data structures.
766 */
767static void sessiond_cleanup_options(void)
768{
769 DBG("Cleaning up options");
770
771 /*
772 * If the override option is set, the pointer points to a *non* const
773 * thus freeing it even though the variable type is set to const.
774 */
775 if (tracing_group_name_override) {
776 free((void *) tracing_group_name);
777 }
778 if (consumerd32_bin_override) {
779 free((void *) consumerd32_bin);
780 }
781 if (consumerd64_bin_override) {
782 free((void *) consumerd64_bin);
783 }
784 if (consumerd32_libdir_override) {
785 free((void *) consumerd32_libdir);
786 }
787 if (consumerd64_libdir_override) {
788 free((void *) consumerd64_libdir);
789 }
790
791 free(opt_pidfile);
792 free(opt_load_session_path);
793 free(kmod_probes_list);
794 free(kmod_extra_probes_list);
c9cb3e7d 795
7567352f 796 run_as_destroy_worker();
fac6795d
DG
797}
798
e065084a 799/*
d063d709 800 * Send data on a unix socket using the liblttsessiondcomm API.
e065084a 801 *
d063d709 802 * Return lttcomm error code.
e065084a
DG
803 */
804static int send_unix_sock(int sock, void *buf, size_t len)
805{
806 /* Check valid length */
c617c0c6 807 if (len == 0) {
e065084a
DG
808 return -1;
809 }
810
811 return lttcomm_send_unix_sock(sock, buf, len);
812}
813
5461b305 814/*
d063d709 815 * Free memory of a command context structure.
5461b305 816 */
a2fb29a5 817static void clean_command_ctx(struct command_ctx **cmd_ctx)
5461b305 818{
a2fb29a5
DG
819 DBG("Clean command context structure");
820 if (*cmd_ctx) {
821 if ((*cmd_ctx)->llm) {
822 free((*cmd_ctx)->llm);
5461b305 823 }
a2fb29a5
DG
824 if ((*cmd_ctx)->lsm) {
825 free((*cmd_ctx)->lsm);
5461b305 826 }
a2fb29a5
DG
827 free(*cmd_ctx);
828 *cmd_ctx = NULL;
5461b305
DG
829 }
830}
831
fac6795d 832/*
0fdd1e2c 833 * Notify UST applications using the shm mmap futex.
fac6795d 834 */
0fdd1e2c 835static int notify_ust_apps(int active)
fac6795d 836{
0fdd1e2c 837 char *wait_shm_mmap;
fac6795d 838
0fdd1e2c 839 DBG("Notifying applications of session daemon state: %d", active);
e07ae692 840
0fdd1e2c
DG
841 /* See shm.c for this call implying mmap, shm and futex calls */
842 wait_shm_mmap = shm_ust_get_mmap(wait_shm_path, is_root);
843 if (wait_shm_mmap == NULL) {
fac6795d
DG
844 goto error;
845 }
846
0fdd1e2c
DG
847 /* Wake waiting process */
848 futex_wait_update((int32_t *) wait_shm_mmap, active);
849
850 /* Apps notified successfully */
851 return 0;
fac6795d
DG
852
853error:
0fdd1e2c 854 return -1;
fac6795d
DG
855}
856
e065084a 857/*
d063d709
DG
858 * Setup the outgoing data buffer for the response (llm) by allocating the
859 * right amount of memory and copying the original information from the lsm
860 * structure.
ca95a216 861 *
6e10c9b9 862 * Return 0 on success, negative value on error.
ca95a216 863 */
6e10c9b9
PP
864static int setup_lttng_msg(struct command_ctx *cmd_ctx,
865 const void *payload_buf, size_t payload_len,
866 const void *cmd_header_buf, size_t cmd_header_len)
ca95a216 867{
6e10c9b9
PP
868 int ret = 0;
869 const size_t header_len = sizeof(struct lttcomm_lttng_msg);
870 const size_t cmd_header_offset = header_len;
871 const size_t payload_offset = cmd_header_offset + cmd_header_len;
872 const size_t total_msg_size = header_len + cmd_header_len + payload_len;
ca95a216 873
6e10c9b9 874 cmd_ctx->llm = zmalloc(total_msg_size);
5461b305 875
5461b305 876 if (cmd_ctx->llm == NULL) {
76d7553f 877 PERROR("zmalloc");
5461b305 878 ret = -ENOMEM;
6e10c9b9 879 goto end;
ca95a216
DG
880 }
881
5461b305
DG
882 /* Copy common data */
883 cmd_ctx->llm->cmd_type = cmd_ctx->lsm->cmd_type;
9f19cc17 884 cmd_ctx->llm->pid = cmd_ctx->lsm->domain.attr.pid;
6e10c9b9
PP
885 cmd_ctx->llm->cmd_header_size = cmd_header_len;
886 cmd_ctx->llm->data_size = payload_len;
887 cmd_ctx->lttng_msg_size = total_msg_size;
5461b305 888
6e10c9b9 889 /* Copy command header */
b4e3ceb9
PP
890 if (cmd_header_len) {
891 memcpy(((uint8_t *) cmd_ctx->llm) + cmd_header_offset, cmd_header_buf,
892 cmd_header_len);
893 }
5461b305 894
6e10c9b9 895 /* Copy payload */
b4e3ceb9
PP
896 if (payload_len) {
897 memcpy(((uint8_t *) cmd_ctx->llm) + payload_offset, payload_buf,
898 payload_len);
899 }
ca95a216 900
6e10c9b9 901end:
ca95a216
DG
902 return ret;
903}
904
6e10c9b9
PP
905/*
906 * Version of setup_lttng_msg() without command header.
907 */
908static int setup_lttng_msg_no_cmd_header(struct command_ctx *cmd_ctx,
909 void *payload_buf, size_t payload_len)
910{
911 return setup_lttng_msg(cmd_ctx, payload_buf, payload_len, NULL, 0);
912}
7a485870 913/*
5eb91c98 914 * Update the kernel poll set of all channel fd available over all tracing
d063d709 915 * session. Add the wakeup pipe at the end of the set.
7a485870 916 */
5eb91c98 917static int update_kernel_poll(struct lttng_poll_event *events)
7a485870 918{
5eb91c98 919 int ret;
7a485870
DG
920 struct ltt_session *session;
921 struct ltt_kernel_channel *channel;
922
5eb91c98 923 DBG("Updating kernel poll set");
7a485870 924
54d01ffb 925 session_lock_list();
b5541356 926 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
54d01ffb 927 session_lock(session);
7a485870 928 if (session->kernel_session == NULL) {
54d01ffb 929 session_unlock(session);
7a485870
DG
930 continue;
931 }
7a485870 932
54d01ffb
DG
933 cds_list_for_each_entry(channel,
934 &session->kernel_session->channel_list.head, list) {
5eb91c98
DG
935 /* Add channel fd to the kernel poll set */
936 ret = lttng_poll_add(events, channel->fd, LPOLLIN | LPOLLRDNORM);
937 if (ret < 0) {
54d01ffb 938 session_unlock(session);
5eb91c98
DG
939 goto error;
940 }
941 DBG("Channel fd %d added to kernel set", channel->fd);
7a485870 942 }
54d01ffb 943 session_unlock(session);
7a485870 944 }
54d01ffb 945 session_unlock_list();
7a485870 946
5eb91c98 947 return 0;
7a485870
DG
948
949error:
54d01ffb 950 session_unlock_list();
7a485870
DG
951 return -1;
952}
953
954/*
54d01ffb 955 * Find the channel fd from 'fd' over all tracing session. When found, check
d063d709 956 * for new channel stream and send those stream fds to the kernel consumer.
7a485870 957 *
d063d709 958 * Useful for CPU hotplug feature.
7a485870 959 */
2bdd86d4 960static int update_kernel_stream(struct consumer_data *consumer_data, int fd)
7a485870
DG
961{
962 int ret = 0;
963 struct ltt_session *session;
173af62f 964 struct ltt_kernel_session *ksess;
7a485870
DG
965 struct ltt_kernel_channel *channel;
966
967 DBG("Updating kernel streams for channel fd %d", fd);
968
54d01ffb 969 session_lock_list();
b5541356 970 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
54d01ffb 971 session_lock(session);
7a485870 972 if (session->kernel_session == NULL) {
54d01ffb 973 session_unlock(session);
7a485870
DG
974 continue;
975 }
173af62f 976 ksess = session->kernel_session;
d9800920 977
4a15001e
MD
978 cds_list_for_each_entry(channel,
979 &ksess->channel_list.head, list) {
980 struct lttng_ht_iter iter;
981 struct consumer_socket *socket;
d9800920 982
4a15001e
MD
983 if (channel->fd != fd) {
984 continue;
985 }
986 DBG("Channel found, updating kernel streams");
987 ret = kernel_open_channel_stream(channel);
988 if (ret < 0) {
989 goto error;
990 }
991 /* Update the stream global counter */
992 ksess->stream_count_global += ret;
993
994 /*
995 * Have we already sent fds to the consumer? If yes, it
996 * means that tracing is started so it is safe to send
997 * our updated stream fds.
998 */
999 if (ksess->consumer_fds_sent != 1
1000 || ksess->consumer == NULL) {
1001 ret = -1;
1002 goto error;
1003 }
1004
1005 rcu_read_lock();
1006 cds_lfht_for_each_entry(ksess->consumer->socks->ht,
1007 &iter.iter, socket, node.node) {
1008 pthread_mutex_lock(socket->lock);
1009 ret = kernel_consumer_send_channel_stream(socket,
1010 channel, ksess,
1011 session->output_traces ? 1 : 0);
1012 pthread_mutex_unlock(socket->lock);
1013 if (ret < 0) {
e7fe706f 1014 rcu_read_unlock();
4a15001e 1015 goto error;
7a485870 1016 }
7a485870 1017 }
4a15001e 1018 rcu_read_unlock();
7a485870 1019 }
54d01ffb 1020 session_unlock(session);
7a485870 1021 }
54d01ffb 1022 session_unlock_list();
b3c750d2 1023 return ret;
7a485870 1024
b3c750d2 1025error:
54d01ffb
DG
1026 session_unlock(session);
1027 session_unlock_list();
7a485870
DG
1028 return ret;
1029}
1030
487cf67c 1031/*
ffe60014
DG
1032 * For each tracing session, update newly registered apps. The session list
1033 * lock MUST be acquired before calling this.
487cf67c
DG
1034 */
1035static void update_ust_app(int app_sock)
1036{
1037 struct ltt_session *sess, *stmp;
1038
fdadac08
DG
1039 /* Consumer is in an ERROR state. Stop any application update. */
1040 if (uatomic_read(&ust_consumerd_state) == CONSUMER_ERROR) {
1041 /* Stop the update process since the consumer is dead. */
1042 return;
1043 }
1044
487cf67c
DG
1045 /* For all tracing session(s) */
1046 cds_list_for_each_entry_safe(sess, stmp, &session_list_ptr->head, list) {
a9ad0c8f
MD
1047 struct ust_app *app;
1048
4ee14516 1049 session_lock(sess);
a9ad0c8f
MD
1050 if (!sess->ust_session) {
1051 goto unlock_session;
1052 }
1053
1054 rcu_read_lock();
1055 assert(app_sock >= 0);
1056 app = ust_app_find_by_sock(app_sock);
1057 if (app == NULL) {
1058 /*
1059 * Application can be unregistered before so
1060 * this is possible hence simply stopping the
1061 * update.
1062 */
1063 DBG3("UST app update failed to find app sock %d",
1064 app_sock);
1065 goto unlock_rcu;
421cb601 1066 }
a9ad0c8f
MD
1067 ust_app_global_update(sess->ust_session, app);
1068 unlock_rcu:
1069 rcu_read_unlock();
1070 unlock_session:
4ee14516 1071 session_unlock(sess);
487cf67c
DG
1072 }
1073}
1074
7a485870 1075/*
d063d709 1076 * This thread manage event coming from the kernel.
7a485870 1077 *
d063d709
DG
1078 * Features supported in this thread:
1079 * -) CPU Hotplug
7a485870
DG
1080 */
1081static void *thread_manage_kernel(void *data)
1082{
139ac872 1083 int ret, i, pollfd, update_poll_flag = 1, err = -1;
5eb91c98 1084 uint32_t revents, nb_fd;
7a485870 1085 char tmp;
5eb91c98 1086 struct lttng_poll_event events;
7a485870 1087
6993eeb3 1088 DBG("[thread] Thread manage kernel started");
7a485870 1089
6c71277b 1090 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_KERNEL);
927ca06a 1091
d5d63bf1
DG
1092 /*
1093 * This first step of the while is to clean this structure which could free
6d737ce4 1094 * non NULL pointers so initialize it before the loop.
d5d63bf1 1095 */
6d737ce4 1096 lttng_poll_init(&events);
d5d63bf1 1097
e547b070 1098 if (testpoint(sessiond_thread_manage_kernel)) {
6993eeb3
CB
1099 goto error_testpoint;
1100 }
8ac94142 1101
840cb59c 1102 health_code_update();
44a5e5eb 1103
e547b070 1104 if (testpoint(sessiond_thread_manage_kernel_before_loop)) {
d21b0d71 1105 goto error_testpoint;
6993eeb3
CB
1106 }
1107
7a485870 1108 while (1) {
840cb59c 1109 health_code_update();
44a5e5eb 1110
7a485870 1111 if (update_poll_flag == 1) {
d21b0d71
DG
1112 /* Clean events object. We are about to populate it again. */
1113 lttng_poll_clean(&events);
1114
d0b96690 1115 ret = sessiond_set_thread_pollset(&events, 2);
d21b0d71
DG
1116 if (ret < 0) {
1117 goto error_poll_create;
1118 }
1119
1120 ret = lttng_poll_add(&events, kernel_poll_pipe[0], LPOLLIN);
1121 if (ret < 0) {
1122 goto error;
1123 }
5f822d0a 1124
d21b0d71 1125 /* This will add the available kernel channel if any. */
5eb91c98
DG
1126 ret = update_kernel_poll(&events);
1127 if (ret < 0) {
7a485870
DG
1128 goto error;
1129 }
1130 update_poll_flag = 0;
1131 }
1132
7fa2082e 1133 DBG("Thread kernel polling");
7a485870
DG
1134
1135 /* Poll infinite value of time */
88f2b785 1136 restart:
a78af745 1137 health_poll_entry();
5eb91c98 1138 ret = lttng_poll_wait(&events, -1);
7fa2082e
MD
1139 DBG("Thread kernel return from poll on %d fds",
1140 LTTNG_POLL_GETNB(&events));
a78af745 1141 health_poll_exit();
7a485870 1142 if (ret < 0) {
88f2b785
MD
1143 /*
1144 * Restart interrupted system call.
1145 */
1146 if (errno == EINTR) {
1147 goto restart;
1148 }
7a485870
DG
1149 goto error;
1150 } else if (ret == 0) {
1151 /* Should not happen since timeout is infinite */
85611738
DG
1152 ERR("Return value of poll is 0 with an infinite timeout.\n"
1153 "This should not have happened! Continuing...");
7a485870
DG
1154 continue;
1155 }
1156
0d9c5d77
DG
1157 nb_fd = ret;
1158
5eb91c98
DG
1159 for (i = 0; i < nb_fd; i++) {
1160 /* Fetch once the poll data */
1161 revents = LTTNG_POLL_GETEV(&events, i);
1162 pollfd = LTTNG_POLL_GETFD(&events, i);
7a485870 1163
840cb59c 1164 health_code_update();
44a5e5eb 1165
fd20dac9
MD
1166 if (!revents) {
1167 /* No activity for this FD (poll implementation). */
1168 continue;
1169 }
1170
5eb91c98 1171 /* Thread quit pipe has been closed. Killing thread. */
d0b96690 1172 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
5eb91c98 1173 if (ret) {
139ac872
MD
1174 err = 0;
1175 goto exit;
5eb91c98 1176 }
7a485870 1177
5eb91c98 1178 /* Check for data on kernel pipe */
03e43155
MD
1179 if (revents & LPOLLIN) {
1180 if (pollfd == kernel_poll_pipe[0]) {
1181 (void) lttng_read(kernel_poll_pipe[0],
1182 &tmp, 1);
1183 /*
1184 * Ret value is useless here, if this pipe gets any actions an
1185 * update is required anyway.
1186 */
1187 update_poll_flag = 1;
1188 continue;
1189 } else {
1190 /*
1191 * New CPU detected by the kernel. Adding kernel stream to
1192 * kernel session and updating the kernel consumer
1193 */
2bdd86d4 1194 ret = update_kernel_stream(&kconsumer_data, pollfd);
5eb91c98
DG
1195 if (ret < 0) {
1196 continue;
1197 }
1198 break;
7a485870 1199 }
03e43155
MD
1200 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1201 update_poll_flag = 1;
1202 continue;
1203 } else {
1204 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
1205 goto error;
7a485870
DG
1206 }
1207 }
1208 }
1209
139ac872 1210exit:
7a485870 1211error:
5eb91c98 1212 lttng_poll_clean(&events);
76d7553f 1213error_poll_create:
6993eeb3 1214error_testpoint:
6620da75
DG
1215 utils_close_pipe(kernel_poll_pipe);
1216 kernel_poll_pipe[0] = kernel_poll_pipe[1] = -1;
139ac872 1217 if (err) {
840cb59c 1218 health_error();
139ac872 1219 ERR("Health error occurred in %s", __func__);
6620da75
DG
1220 WARN("Kernel thread died unexpectedly. "
1221 "Kernel tracing can continue but CPU hotplug is disabled.");
139ac872 1222 }
8782cc74 1223 health_unregister(health_sessiond);
76d7553f 1224 DBG("Kernel thread dying");
7a485870
DG
1225 return NULL;
1226}
1227
a23ec3a7
DG
1228/*
1229 * Signal pthread condition of the consumer data that the thread.
1230 */
1231static void signal_consumer_condition(struct consumer_data *data, int state)
1232{
1233 pthread_mutex_lock(&data->cond_mutex);
1234
1235 /*
1236 * The state is set before signaling. It can be any value, it's the waiter
1237 * job to correctly interpret this condition variable associated to the
1238 * consumer pthread_cond.
1239 *
1240 * A value of 0 means that the corresponding thread of the consumer data
1241 * was not started. 1 indicates that the thread has started and is ready
1242 * for action. A negative value means that there was an error during the
1243 * thread bootstrap.
1244 */
1245 data->consumer_thread_is_ready = state;
1246 (void) pthread_cond_signal(&data->cond);
1247
1248 pthread_mutex_unlock(&data->cond_mutex);
1249}
1250
1d4b027a 1251/*
3bd1e081 1252 * This thread manage the consumer error sent back to the session daemon.
1d4b027a 1253 */
3bd1e081 1254static void *thread_manage_consumer(void *data)
1d4b027a 1255{
42fc1d0b 1256 int sock = -1, i, ret, pollfd, err = -1, should_quit = 0;
5eb91c98 1257 uint32_t revents, nb_fd;
1d4b027a 1258 enum lttcomm_return_code code;
5eb91c98 1259 struct lttng_poll_event events;
3bd1e081 1260 struct consumer_data *consumer_data = data;
1d4b027a 1261
3bd1e081 1262 DBG("[thread] Manage consumer started");
1d4b027a 1263
34c1e15a
MD
1264 rcu_register_thread();
1265 rcu_thread_online();
1266
6c71277b 1267 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_CONSUMER);
927ca06a 1268
855060f8 1269 health_code_update();
9449cc75 1270
5eb91c98 1271 /*
331744e3
JD
1272 * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the
1273 * metadata_sock. Nothing more will be added to this poll set.
5eb91c98 1274 */
331744e3 1275 ret = sessiond_set_thread_pollset(&events, 3);
5eb91c98 1276 if (ret < 0) {
76d7553f 1277 goto error_poll;
5eb91c98 1278 }
273ea72c 1279
edb8b045
DG
1280 /*
1281 * The error socket here is already in a listening state which was done
1282 * just before spawning this thread to avoid a race between the consumer
1283 * daemon exec trying to connect and the listen() call.
1284 */
3bd1e081 1285 ret = lttng_poll_add(&events, consumer_data->err_sock, LPOLLIN | LPOLLRDHUP);
5eb91c98
DG
1286 if (ret < 0) {
1287 goto error;
1288 }
1289
840cb59c 1290 health_code_update();
44a5e5eb 1291
331744e3 1292 /* Infinite blocking call, waiting for transmission */
88f2b785 1293restart:
a78af745 1294 health_poll_entry();
8ac94142 1295
e547b070 1296 if (testpoint(sessiond_thread_manage_consumer)) {
6993eeb3
CB
1297 goto error;
1298 }
8ac94142 1299
5eb91c98 1300 ret = lttng_poll_wait(&events, -1);
a78af745 1301 health_poll_exit();
273ea72c 1302 if (ret < 0) {
88f2b785
MD
1303 /*
1304 * Restart interrupted system call.
1305 */
1306 if (errno == EINTR) {
1307 goto restart;
1308 }
273ea72c
DG
1309 goto error;
1310 }
1311
0d9c5d77
DG
1312 nb_fd = ret;
1313
5eb91c98
DG
1314 for (i = 0; i < nb_fd; i++) {
1315 /* Fetch once the poll data */
1316 revents = LTTNG_POLL_GETEV(&events, i);
1317 pollfd = LTTNG_POLL_GETFD(&events, i);
1318
840cb59c 1319 health_code_update();
44a5e5eb 1320
fd20dac9
MD
1321 if (!revents) {
1322 /* No activity for this FD (poll implementation). */
1323 continue;
1324 }
1325
5eb91c98 1326 /* Thread quit pipe has been closed. Killing thread. */
d0b96690 1327 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
5eb91c98 1328 if (ret) {
139ac872
MD
1329 err = 0;
1330 goto exit;
5eb91c98
DG
1331 }
1332
1333 /* Event on the registration socket */
3bd1e081 1334 if (pollfd == consumer_data->err_sock) {
03e43155
MD
1335 if (revents & LPOLLIN) {
1336 continue;
1337 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
3bd1e081 1338 ERR("consumer err socket poll error");
5eb91c98 1339 goto error;
03e43155
MD
1340 } else {
1341 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
1342 goto error;
5eb91c98
DG
1343 }
1344 }
273ea72c
DG
1345 }
1346
3bd1e081 1347 sock = lttcomm_accept_unix_sock(consumer_data->err_sock);
1d4b027a
DG
1348 if (sock < 0) {
1349 goto error;
1350 }
1351
b662582b
DG
1352 /*
1353 * Set the CLOEXEC flag. Return code is useless because either way, the
1354 * show must go on.
1355 */
1356 (void) utils_set_fd_cloexec(sock);
1357
840cb59c 1358 health_code_update();
44a5e5eb 1359
3bd1e081 1360 DBG2("Receiving code from consumer err_sock");
ee0b0061 1361
712ea556 1362 /* Getting status code from kconsumerd */
54d01ffb
DG
1363 ret = lttcomm_recv_unix_sock(sock, &code,
1364 sizeof(enum lttcomm_return_code));
1d4b027a
DG
1365 if (ret <= 0) {
1366 goto error;
1367 }
1368
840cb59c 1369 health_code_update();
f73fabfd 1370 if (code == LTTCOMM_CONSUMERD_COMMAND_SOCK_READY) {
331744e3 1371 /* Connect both socket, command and metadata. */
3bd1e081
MD
1372 consumer_data->cmd_sock =
1373 lttcomm_connect_unix_sock(consumer_data->cmd_unix_sock_path);
4ce514c4 1374 consumer_data->metadata_fd =
331744e3 1375 lttcomm_connect_unix_sock(consumer_data->cmd_unix_sock_path);
92db7cdc
DG
1376 if (consumer_data->cmd_sock < 0
1377 || consumer_data->metadata_fd < 0) {
331744e3 1378 PERROR("consumer connect cmd socket");
a23ec3a7
DG
1379 /* On error, signal condition and quit. */
1380 signal_consumer_condition(consumer_data, -1);
1d4b027a
DG
1381 goto error;
1382 }
9363801e 1383 consumer_data->metadata_sock.fd_ptr = &consumer_data->metadata_fd;
331744e3
JD
1384 /* Create metadata socket lock. */
1385 consumer_data->metadata_sock.lock = zmalloc(sizeof(pthread_mutex_t));
1386 if (consumer_data->metadata_sock.lock == NULL) {
1387 PERROR("zmalloc pthread mutex");
331744e3
JD
1388 goto error;
1389 }
1390 pthread_mutex_init(consumer_data->metadata_sock.lock, NULL);
1391
a23ec3a7 1392 signal_consumer_condition(consumer_data, 1);
331744e3
JD
1393 DBG("Consumer command socket ready (fd: %d", consumer_data->cmd_sock);
1394 DBG("Consumer metadata socket ready (fd: %d)",
4ce514c4 1395 consumer_data->metadata_fd);
1d4b027a 1396 } else {
3bd1e081 1397 ERR("consumer error when waiting for SOCK_READY : %s",
1d4b027a
DG
1398 lttcomm_get_readable_code(-code));
1399 goto error;
1400 }
1401
331744e3 1402 /* Remove the consumerd error sock since we've established a connexion */
3bd1e081 1403 ret = lttng_poll_del(&events, consumer_data->err_sock);
72079cae 1404 if (ret < 0) {
72079cae
DG
1405 goto error;
1406 }
1407
331744e3 1408 /* Add new accepted error socket. */
5eb91c98
DG
1409 ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLRDHUP);
1410 if (ret < 0) {
72079cae 1411 goto error;
5eb91c98
DG
1412 }
1413
331744e3 1414 /* Add metadata socket that is successfully connected. */
4ce514c4 1415 ret = lttng_poll_add(&events, consumer_data->metadata_fd,
331744e3
JD
1416 LPOLLIN | LPOLLRDHUP);
1417 if (ret < 0) {
1418 goto error;
1419 }
1420
840cb59c 1421 health_code_update();
44a5e5eb 1422
331744e3 1423 /* Infinite blocking call, waiting for transmission */
88f2b785 1424restart_poll:
331744e3 1425 while (1) {
42fc1d0b
DG
1426 health_code_update();
1427
1428 /* Exit the thread because the thread quit pipe has been triggered. */
1429 if (should_quit) {
1430 /* Not a health error. */
1431 err = 0;
1432 goto exit;
1433 }
1434
331744e3
JD
1435 health_poll_entry();
1436 ret = lttng_poll_wait(&events, -1);
1437 health_poll_exit();
1438 if (ret < 0) {
1439 /*
1440 * Restart interrupted system call.
1441 */
1442 if (errno == EINTR) {
1443 goto restart_poll;
1444 }
1445 goto error;
88f2b785 1446 }
72079cae 1447
331744e3 1448 nb_fd = ret;
0d9c5d77 1449
331744e3
JD
1450 for (i = 0; i < nb_fd; i++) {
1451 /* Fetch once the poll data */
1452 revents = LTTNG_POLL_GETEV(&events, i);
1453 pollfd = LTTNG_POLL_GETFD(&events, i);
5eb91c98 1454
331744e3 1455 health_code_update();
44a5e5eb 1456
fd20dac9
MD
1457 if (!revents) {
1458 /* No activity for this FD (poll implementation). */
1459 continue;
1460 }
1461
42fc1d0b
DG
1462 /*
1463 * Thread quit pipe has been triggered, flag that we should stop
1464 * but continue the current loop to handle potential data from
1465 * consumer.
1466 */
1467 should_quit = sessiond_check_thread_quit_pipe(pollfd, revents);
5eb91c98 1468
331744e3
JD
1469 if (pollfd == sock) {
1470 /* Event on the consumerd socket */
03e43155
MD
1471 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)
1472 && !(revents & LPOLLIN)) {
331744e3
JD
1473 ERR("consumer err socket second poll error");
1474 goto error;
1475 }
1476 health_code_update();
1477 /* Wait for any kconsumerd error */
1478 ret = lttcomm_recv_unix_sock(sock, &code,
1479 sizeof(enum lttcomm_return_code));
1480 if (ret <= 0) {
1481 ERR("consumer closed the command socket");
1482 goto error;
1483 }
1484
1485 ERR("consumer return code : %s",
1486 lttcomm_get_readable_code(-code));
1487
1488 goto exit;
4ce514c4 1489 } else if (pollfd == consumer_data->metadata_fd) {
03e43155
MD
1490 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)
1491 && !(revents & LPOLLIN)) {
1492 ERR("consumer err metadata socket second poll error");
1493 goto error;
1494 }
331744e3
JD
1495 /* UST metadata requests */
1496 ret = ust_consumer_metadata_request(
1497 &consumer_data->metadata_sock);
1498 if (ret < 0) {
1499 ERR("Handling metadata request");
1500 goto error;
1501 }
5eb91c98 1502 }
42fc1d0b 1503 /* No need for an else branch all FDs are tested prior. */
5eb91c98 1504 }
331744e3 1505 health_code_update();
5eb91c98
DG
1506 }
1507
139ac872 1508exit:
1d4b027a 1509error:
fdadac08
DG
1510 /*
1511 * We lock here because we are about to close the sockets and some other
92db7cdc
DG
1512 * thread might be using them so get exclusive access which will abort all
1513 * other consumer command by other threads.
fdadac08
DG
1514 */
1515 pthread_mutex_lock(&consumer_data->lock);
1516
5c827ce0
DG
1517 /* Immediately set the consumerd state to stopped */
1518 if (consumer_data->type == LTTNG_CONSUMER_KERNEL) {
1519 uatomic_set(&kernel_consumerd_state, CONSUMER_ERROR);
1520 } else if (consumer_data->type == LTTNG_CONSUMER64_UST ||
1521 consumer_data->type == LTTNG_CONSUMER32_UST) {
1522 uatomic_set(&ust_consumerd_state, CONSUMER_ERROR);
1523 } else {
1524 /* Code flow error... */
1525 assert(0);
1526 }
1527
76d7553f
MD
1528 if (consumer_data->err_sock >= 0) {
1529 ret = close(consumer_data->err_sock);
1530 if (ret) {
1531 PERROR("close");
1532 }
a76cbd9f 1533 consumer_data->err_sock = -1;
76d7553f
MD
1534 }
1535 if (consumer_data->cmd_sock >= 0) {
1536 ret = close(consumer_data->cmd_sock);
1537 if (ret) {
1538 PERROR("close");
1539 }
a76cbd9f 1540 consumer_data->cmd_sock = -1;
76d7553f 1541 }
96544455
SS
1542 if (consumer_data->metadata_sock.fd_ptr &&
1543 *consumer_data->metadata_sock.fd_ptr >= 0) {
9363801e 1544 ret = close(*consumer_data->metadata_sock.fd_ptr);
331744e3
JD
1545 if (ret) {
1546 PERROR("close");
1547 }
1548 }
76d7553f
MD
1549 if (sock >= 0) {
1550 ret = close(sock);
1551 if (ret) {
1552 PERROR("close");
1553 }
1554 }
273ea72c 1555
3bd1e081
MD
1556 unlink(consumer_data->err_unix_sock_path);
1557 unlink(consumer_data->cmd_unix_sock_path);
fdadac08 1558 pthread_mutex_unlock(&consumer_data->lock);
92db7cdc 1559
fdadac08 1560 /* Cleanup metadata socket mutex. */
96544455
SS
1561 if (consumer_data->metadata_sock.lock) {
1562 pthread_mutex_destroy(consumer_data->metadata_sock.lock);
1563 free(consumer_data->metadata_sock.lock);
1564 }
5eb91c98 1565 lttng_poll_clean(&events);
76d7553f 1566error_poll:
139ac872 1567 if (err) {
840cb59c 1568 health_error();
139ac872
MD
1569 ERR("Health error occurred in %s", __func__);
1570 }
8782cc74 1571 health_unregister(health_sessiond);
76d7553f 1572 DBG("consumer thread cleanup completed");
0177d773 1573
34c1e15a
MD
1574 rcu_thread_offline();
1575 rcu_unregister_thread();
1576
5eb91c98 1577 return NULL;
099e26bd
DG
1578}
1579
099e26bd
DG
1580/*
1581 * This thread manage application communication.
1d4b027a
DG
1582 */
1583static void *thread_manage_apps(void *data)
099e26bd 1584{
139ac872 1585 int i, ret, pollfd, err = -1;
6cd525e8 1586 ssize_t size_ret;
5eb91c98 1587 uint32_t revents, nb_fd;
5eb91c98 1588 struct lttng_poll_event events;
099e26bd
DG
1589
1590 DBG("[thread] Manage application started");
1591
f6a9efaa
DG
1592 rcu_register_thread();
1593 rcu_thread_online();
1594
6c71277b 1595 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_MANAGE);
927ca06a 1596
e547b070 1597 if (testpoint(sessiond_thread_manage_apps)) {
6993eeb3
CB
1598 goto error_testpoint;
1599 }
1600
840cb59c 1601 health_code_update();
44a5e5eb 1602
d0b96690 1603 ret = sessiond_set_thread_pollset(&events, 2);
5eb91c98 1604 if (ret < 0) {
76d7553f 1605 goto error_poll_create;
5eb91c98 1606 }
099e26bd 1607
5eb91c98
DG
1608 ret = lttng_poll_add(&events, apps_cmd_pipe[0], LPOLLIN | LPOLLRDHUP);
1609 if (ret < 0) {
1610 goto error;
1611 }
099e26bd 1612
e547b070 1613 if (testpoint(sessiond_thread_manage_apps_before_loop)) {
6993eeb3
CB
1614 goto error;
1615 }
8ac94142 1616
840cb59c 1617 health_code_update();
44a5e5eb 1618
5eb91c98 1619 while (1) {
7fa2082e 1620 DBG("Apps thread polling");
099e26bd
DG
1621
1622 /* Inifinite blocking call, waiting for transmission */
88f2b785 1623 restart:
a78af745 1624 health_poll_entry();
5eb91c98 1625 ret = lttng_poll_wait(&events, -1);
7fa2082e
MD
1626 DBG("Apps thread return from poll on %d fds",
1627 LTTNG_POLL_GETNB(&events));
a78af745 1628 health_poll_exit();
099e26bd 1629 if (ret < 0) {
88f2b785
MD
1630 /*
1631 * Restart interrupted system call.
1632 */
1633 if (errno == EINTR) {
1634 goto restart;
1635 }
099e26bd
DG
1636 goto error;
1637 }
1638
0d9c5d77
DG
1639 nb_fd = ret;
1640
5eb91c98
DG
1641 for (i = 0; i < nb_fd; i++) {
1642 /* Fetch once the poll data */
1643 revents = LTTNG_POLL_GETEV(&events, i);
1644 pollfd = LTTNG_POLL_GETFD(&events, i);
1645
840cb59c 1646 health_code_update();
44a5e5eb 1647
fd20dac9
MD
1648 if (!revents) {
1649 /* No activity for this FD (poll implementation). */
1650 continue;
1651 }
1652
5eb91c98 1653 /* Thread quit pipe has been closed. Killing thread. */
d0b96690 1654 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
5eb91c98 1655 if (ret) {
139ac872
MD
1656 err = 0;
1657 goto exit;
5eb91c98 1658 }
099e26bd 1659
5eb91c98
DG
1660 /* Inspect the apps cmd pipe */
1661 if (pollfd == apps_cmd_pipe[0]) {
03e43155 1662 if (revents & LPOLLIN) {
d0b96690
DG
1663 int sock;
1664
5eb91c98 1665 /* Empty pipe */
6cd525e8
MD
1666 size_ret = lttng_read(apps_cmd_pipe[0], &sock, sizeof(sock));
1667 if (size_ret < sizeof(sock)) {
76d7553f 1668 PERROR("read apps cmd pipe");
5eb91c98
DG
1669 goto error;
1670 }
099e26bd 1671
840cb59c 1672 health_code_update();
44a5e5eb 1673
ffe60014 1674 /*
03e43155
MD
1675 * Since this is a command socket (write then read),
1676 * we only monitor the error events of the socket.
ffe60014 1677 */
d0b96690
DG
1678 ret = lttng_poll_add(&events, sock,
1679 LPOLLERR | LPOLLHUP | LPOLLRDHUP);
1680 if (ret < 0) {
5eb91c98 1681 goto error;
e0c7ec2b 1682 }
acc7b41b 1683
d0b96690 1684 DBG("Apps with sock %d added to poll set", sock);
03e43155
MD
1685 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1686 ERR("Apps command pipe error");
1687 goto error;
1688 } else {
1689 ERR("Unknown poll events %u for sock %d", revents, pollfd);
1690 goto error;
0177d773 1691 }
5eb91c98
DG
1692 } else {
1693 /*
54d01ffb
DG
1694 * At this point, we know that a registered application made
1695 * the event at poll_wait.
5eb91c98
DG
1696 */
1697 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1698 /* Removing from the poll set */
1699 ret = lttng_poll_del(&events, pollfd);
1700 if (ret < 0) {
1701 goto error;
1702 }
099e26bd 1703
b9d9b220 1704 /* Socket closed on remote end. */
56fff090 1705 ust_app_unregister(pollfd);
03e43155
MD
1706 } else {
1707 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
1708 goto error;
5eb91c98 1709 }
099e26bd 1710 }
44a5e5eb 1711
840cb59c 1712 health_code_update();
099e26bd 1713 }
099e26bd
DG
1714 }
1715
139ac872 1716exit:
099e26bd 1717error:
5eb91c98 1718 lttng_poll_clean(&events);
76d7553f 1719error_poll_create:
6993eeb3 1720error_testpoint:
6620da75
DG
1721 utils_close_pipe(apps_cmd_pipe);
1722 apps_cmd_pipe[0] = apps_cmd_pipe[1] = -1;
1723
1724 /*
1725 * We don't clean the UST app hash table here since already registered
1726 * applications can still be controlled so let them be until the session
1727 * daemon dies or the applications stop.
1728 */
1729
139ac872 1730 if (err) {
840cb59c 1731 health_error();
139ac872
MD
1732 ERR("Health error occurred in %s", __func__);
1733 }
8782cc74 1734 health_unregister(health_sessiond);
76d7553f 1735 DBG("Application communication apps thread cleanup complete");
f6a9efaa
DG
1736 rcu_thread_offline();
1737 rcu_unregister_thread();
099e26bd
DG
1738 return NULL;
1739}
1740
d0b96690 1741/*
d88aee68
DG
1742 * Send a socket to a thread This is called from the dispatch UST registration
1743 * thread once all sockets are set for the application.
d0b96690 1744 *
b85dc84c
DG
1745 * The sock value can be invalid, we don't really care, the thread will handle
1746 * it and make the necessary cleanup if so.
1747 *
d0b96690
DG
1748 * On success, return 0 else a negative value being the errno message of the
1749 * write().
1750 */
d88aee68 1751static int send_socket_to_thread(int fd, int sock)
d0b96690 1752{
6cd525e8 1753 ssize_t ret;
d0b96690 1754
b85dc84c
DG
1755 /*
1756 * It's possible that the FD is set as invalid with -1 concurrently just
1757 * before calling this function being a shutdown state of the thread.
1758 */
1759 if (fd < 0) {
1760 ret = -EBADF;
1761 goto error;
1762 }
d0b96690 1763
6cd525e8
MD
1764 ret = lttng_write(fd, &sock, sizeof(sock));
1765 if (ret < sizeof(sock)) {
d88aee68 1766 PERROR("write apps pipe %d", fd);
d0b96690
DG
1767 if (ret < 0) {
1768 ret = -errno;
1769 }
1770 goto error;
1771 }
1772
1773 /* All good. Don't send back the write positive ret value. */
1774 ret = 0;
1775error:
6cd525e8 1776 return (int) ret;
d0b96690
DG
1777}
1778
f45e313d
DG
1779/*
1780 * Sanitize the wait queue of the dispatch registration thread meaning removing
1781 * invalid nodes from it. This is to avoid memory leaks for the case the UST
1782 * notify socket is never received.
1783 */
1784static void sanitize_wait_queue(struct ust_reg_wait_queue *wait_queue)
1785{
1786 int ret, nb_fd = 0, i;
1787 unsigned int fd_added = 0;
1788 struct lttng_poll_event events;
1789 struct ust_reg_wait_node *wait_node = NULL, *tmp_wait_node;
1790
1791 assert(wait_queue);
1792
1793 lttng_poll_init(&events);
1794
1795 /* Just skip everything for an empty queue. */
1796 if (!wait_queue->count) {
1797 goto end;
1798 }
1799
1800 ret = lttng_poll_create(&events, wait_queue->count, LTTNG_CLOEXEC);
1801 if (ret < 0) {
1802 goto error_create;
1803 }
1804
1805 cds_list_for_each_entry_safe(wait_node, tmp_wait_node,
1806 &wait_queue->head, head) {
1807 assert(wait_node->app);
1808 ret = lttng_poll_add(&events, wait_node->app->sock,
1809 LPOLLHUP | LPOLLERR);
1810 if (ret < 0) {
1811 goto error;
1812 }
1813
1814 fd_added = 1;
1815 }
1816
1817 if (!fd_added) {
1818 goto end;
1819 }
1820
1821 /*
1822 * Poll but don't block so we can quickly identify the faulty events and
1823 * clean them afterwards from the wait queue.
1824 */
1825 ret = lttng_poll_wait(&events, 0);
1826 if (ret < 0) {
1827 goto error;
1828 }
1829 nb_fd = ret;
1830
1831 for (i = 0; i < nb_fd; i++) {
1832 /* Get faulty FD. */
1833 uint32_t revents = LTTNG_POLL_GETEV(&events, i);
1834 int pollfd = LTTNG_POLL_GETFD(&events, i);
1835
fd20dac9
MD
1836 if (!revents) {
1837 /* No activity for this FD (poll implementation). */
1838 continue;
1839 }
1840
f45e313d
DG
1841 cds_list_for_each_entry_safe(wait_node, tmp_wait_node,
1842 &wait_queue->head, head) {
1843 if (pollfd == wait_node->app->sock &&
1844 (revents & (LPOLLHUP | LPOLLERR))) {
1845 cds_list_del(&wait_node->head);
1846 wait_queue->count--;
1847 ust_app_destroy(wait_node->app);
1848 free(wait_node);
48b40bcf
JG
1849 /*
1850 * Silence warning of use-after-free in
1851 * cds_list_for_each_entry_safe which uses
1852 * __typeof__(*wait_node).
1853 */
1854 wait_node = NULL;
f45e313d 1855 break;
03e43155
MD
1856 } else {
1857 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
1858 goto error;
f45e313d
DG
1859 }
1860 }
1861 }
1862
1863 if (nb_fd > 0) {
1864 DBG("Wait queue sanitized, %d node were cleaned up", nb_fd);
1865 }
1866
1867end:
1868 lttng_poll_clean(&events);
1869 return;
1870
1871error:
1872 lttng_poll_clean(&events);
1873error_create:
1874 ERR("Unable to sanitize wait queue");
1875 return;
1876}
1877
099e26bd
DG
1878/*
1879 * Dispatch request from the registration threads to the application
1880 * communication thread.
1881 */
1882static void *thread_dispatch_ust_registration(void *data)
1883{
12e2b881 1884 int ret, err = -1;
8bdee6e2 1885 struct cds_wfcq_node *node;
099e26bd 1886 struct ust_command *ust_cmd = NULL;
f45e313d
DG
1887 struct ust_reg_wait_node *wait_node = NULL, *tmp_wait_node;
1888 struct ust_reg_wait_queue wait_queue = {
1889 .count = 0,
1890 };
d0b96690 1891
78d8cf23
MD
1892 rcu_register_thread();
1893
6c71277b 1894 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_REG_DISPATCH);
12e2b881 1895
9ad42ec1
MD
1896 if (testpoint(sessiond_thread_app_reg_dispatch)) {
1897 goto error_testpoint;
1898 }
1899
12e2b881
MD
1900 health_code_update();
1901
f45e313d 1902 CDS_INIT_LIST_HEAD(&wait_queue.head);
099e26bd
DG
1903
1904 DBG("[thread] Dispatch UST command started");
1905
68924dc1 1906 for (;;) {
12e2b881
MD
1907 health_code_update();
1908
099e26bd
DG
1909 /* Atomically prepare the queue futex */
1910 futex_nto1_prepare(&ust_cmd_queue.futex);
1911
68924dc1
MD
1912 if (CMM_LOAD_SHARED(dispatch_thread_exit)) {
1913 break;
1914 }
1915
099e26bd 1916 do {
d0b96690 1917 struct ust_app *app = NULL;
7972aab2 1918 ust_cmd = NULL;
d0b96690 1919
f45e313d
DG
1920 /*
1921 * Make sure we don't have node(s) that have hung up before receiving
1922 * the notify socket. This is to clean the list in order to avoid
1923 * memory leaks from notify socket that are never seen.
1924 */
1925 sanitize_wait_queue(&wait_queue);
1926
12e2b881 1927 health_code_update();
099e26bd 1928 /* Dequeue command for registration */
8bdee6e2 1929 node = cds_wfcq_dequeue_blocking(&ust_cmd_queue.head, &ust_cmd_queue.tail);
099e26bd 1930 if (node == NULL) {
00a17c97 1931 DBG("Woken up but nothing in the UST command queue");
099e26bd
DG
1932 /* Continue thread execution */
1933 break;
1934 }
1935
1936 ust_cmd = caa_container_of(node, struct ust_command, node);
1937
2f50c8a3
DG
1938 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1939 " gid:%d sock:%d name:%s (version %d.%d)",
1940 ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid,
1941 ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid,
1942 ust_cmd->sock, ust_cmd->reg_msg.name,
1943 ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor);
d0b96690
DG
1944
1945 if (ust_cmd->reg_msg.type == USTCTL_SOCKET_CMD) {
1946 wait_node = zmalloc(sizeof(*wait_node));
1947 if (!wait_node) {
1948 PERROR("zmalloc wait_node dispatch");
020d7f60
DG
1949 ret = close(ust_cmd->sock);
1950 if (ret < 0) {
1951 PERROR("close ust sock dispatch %d", ust_cmd->sock);
1952 }
51dec90d 1953 lttng_fd_put(LTTNG_FD_APPS, 1);
7972aab2 1954 free(ust_cmd);
d0b96690
DG
1955 goto error;
1956 }
1957 CDS_INIT_LIST_HEAD(&wait_node->head);
1958
1959 /* Create application object if socket is CMD. */
1960 wait_node->app = ust_app_create(&ust_cmd->reg_msg,
1961 ust_cmd->sock);
1962 if (!wait_node->app) {
1963 ret = close(ust_cmd->sock);
1964 if (ret < 0) {
1965 PERROR("close ust sock dispatch %d", ust_cmd->sock);
6620da75 1966 }
51dec90d 1967 lttng_fd_put(LTTNG_FD_APPS, 1);
d88aee68 1968 free(wait_node);
7972aab2 1969 free(ust_cmd);
d0b96690
DG
1970 continue;
1971 }
1972 /*
1973 * Add application to the wait queue so we can set the notify
1974 * socket before putting this object in the global ht.
1975 */
f45e313d
DG
1976 cds_list_add(&wait_node->head, &wait_queue.head);
1977 wait_queue.count++;
d0b96690 1978
7972aab2 1979 free(ust_cmd);
d0b96690
DG
1980 /*
1981 * We have to continue here since we don't have the notify
1982 * socket and the application MUST be added to the hash table
1983 * only at that moment.
1984 */
1985 continue;
1986 } else {
1987 /*
1988 * Look for the application in the local wait queue and set the
1989 * notify socket if found.
1990 */
d88aee68 1991 cds_list_for_each_entry_safe(wait_node, tmp_wait_node,
f45e313d 1992 &wait_queue.head, head) {
12e2b881 1993 health_code_update();
d0b96690
DG
1994 if (wait_node->app->pid == ust_cmd->reg_msg.pid) {
1995 wait_node->app->notify_sock = ust_cmd->sock;
1996 cds_list_del(&wait_node->head);
f45e313d 1997 wait_queue.count--;
d0b96690
DG
1998 app = wait_node->app;
1999 free(wait_node);
2000 DBG3("UST app notify socket %d is set", ust_cmd->sock);
2001 break;
2002 }
2003 }
020d7f60
DG
2004
2005 /*
2006 * With no application at this stage the received socket is
2007 * basically useless so close it before we free the cmd data
2008 * structure for good.
2009 */
2010 if (!app) {
2011 ret = close(ust_cmd->sock);
2012 if (ret < 0) {
2013 PERROR("close ust sock dispatch %d", ust_cmd->sock);
2014 }
51dec90d 2015 lttng_fd_put(LTTNG_FD_APPS, 1);
020d7f60 2016 }
7972aab2 2017 free(ust_cmd);
d0b96690
DG
2018 }
2019
2020 if (app) {
d0b96690
DG
2021 /*
2022 * @session_lock_list
2023 *
2024 * Lock the global session list so from the register up to the
2025 * registration done message, no thread can see the application
2026 * and change its state.
2027 */
2028 session_lock_list();
2029 rcu_read_lock();
d88aee68 2030
d0b96690
DG
2031 /*
2032 * Add application to the global hash table. This needs to be
2033 * done before the update to the UST registry can locate the
2034 * application.
2035 */
2036 ust_app_add(app);
d88aee68
DG
2037
2038 /* Set app version. This call will print an error if needed. */
2039 (void) ust_app_version(app);
2040
2041 /* Send notify socket through the notify pipe. */
2042 ret = send_socket_to_thread(apps_cmd_notify_pipe[1],
2043 app->notify_sock);
2044 if (ret < 0) {
2045 rcu_read_unlock();
2046 session_unlock_list();
b85dc84c
DG
2047 /*
2048 * No notify thread, stop the UST tracing. However, this is
2049 * not an internal error of the this thread thus setting
2050 * the health error code to a normal exit.
2051 */
2052 err = 0;
d88aee68 2053 goto error;
6620da75 2054 }
d88aee68 2055
d0b96690
DG
2056 /*
2057 * Update newly registered application with the tracing
2058 * registry info already enabled information.
2059 */
2060 update_ust_app(app->sock);
d88aee68
DG
2061
2062 /*
2063 * Don't care about return value. Let the manage apps threads
2064 * handle app unregistration upon socket close.
2065 */
fb45065e 2066 (void) ust_app_register_done(app);
d88aee68
DG
2067
2068 /*
2069 * Even if the application socket has been closed, send the app
2070 * to the thread and unregistration will take place at that
2071 * place.
2072 */
2073 ret = send_socket_to_thread(apps_cmd_pipe[1], app->sock);
d0b96690 2074 if (ret < 0) {
d88aee68
DG
2075 rcu_read_unlock();
2076 session_unlock_list();
b85dc84c
DG
2077 /*
2078 * No apps. thread, stop the UST tracing. However, this is
2079 * not an internal error of the this thread thus setting
2080 * the health error code to a normal exit.
2081 */
2082 err = 0;
d88aee68 2083 goto error;
d0b96690 2084 }
d88aee68 2085
d0b96690
DG
2086 rcu_read_unlock();
2087 session_unlock_list();
099e26bd 2088 }
099e26bd
DG
2089 } while (node != NULL);
2090
12e2b881 2091 health_poll_entry();
099e26bd
DG
2092 /* Futex wait on queue. Blocking call on futex() */
2093 futex_nto1_wait(&ust_cmd_queue.futex);
12e2b881 2094 health_poll_exit();
099e26bd 2095 }
12e2b881
MD
2096 /* Normal exit, no error */
2097 err = 0;
099e26bd
DG
2098
2099error:
d88aee68
DG
2100 /* Clean up wait queue. */
2101 cds_list_for_each_entry_safe(wait_node, tmp_wait_node,
f45e313d 2102 &wait_queue.head, head) {
d88aee68 2103 cds_list_del(&wait_node->head);
f45e313d 2104 wait_queue.count--;
d88aee68
DG
2105 free(wait_node);
2106 }
2107
772b8f4d
MD
2108 /* Empty command queue. */
2109 for (;;) {
2110 /* Dequeue command for registration */
2111 node = cds_wfcq_dequeue_blocking(&ust_cmd_queue.head, &ust_cmd_queue.tail);
2112 if (node == NULL) {
2113 break;
2114 }
2115 ust_cmd = caa_container_of(node, struct ust_command, node);
2116 ret = close(ust_cmd->sock);
2117 if (ret < 0) {
2118 PERROR("close ust sock exit dispatch %d", ust_cmd->sock);
2119 }
2120 lttng_fd_put(LTTNG_FD_APPS, 1);
2121 free(ust_cmd);
2122 }
2123
9ad42ec1 2124error_testpoint:
099e26bd 2125 DBG("Dispatch thread dying");
12e2b881
MD
2126 if (err) {
2127 health_error();
2128 ERR("Health error occurred in %s", __func__);
2129 }
8782cc74 2130 health_unregister(health_sessiond);
78d8cf23 2131 rcu_unregister_thread();
099e26bd
DG
2132 return NULL;
2133}
2134
2135/*
2136 * This thread manage application registration.
2137 */
2138static void *thread_registration_apps(void *data)
1d4b027a 2139{
139ac872 2140 int sock = -1, i, ret, pollfd, err = -1;
5eb91c98
DG
2141 uint32_t revents, nb_fd;
2142 struct lttng_poll_event events;
099e26bd
DG
2143 /*
2144 * Get allocated in this thread, enqueued to a global queue, dequeued and
2145 * freed in the manage apps thread.
2146 */
2147 struct ust_command *ust_cmd = NULL;
1d4b027a 2148
099e26bd 2149 DBG("[thread] Manage application registration started");
1d4b027a 2150
6c71277b 2151 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_REG);
927ca06a 2152
e547b070 2153 if (testpoint(sessiond_thread_registration_apps)) {
6993eeb3
CB
2154 goto error_testpoint;
2155 }
8ac94142 2156
1d4b027a
DG
2157 ret = lttcomm_listen_unix_sock(apps_sock);
2158 if (ret < 0) {
76d7553f 2159 goto error_listen;
1d4b027a
DG
2160 }
2161
5eb91c98
DG
2162 /*
2163 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
2164 * more will be added to this poll set.
2165 */
d0b96690 2166 ret = sessiond_set_thread_pollset(&events, 2);
5eb91c98 2167 if (ret < 0) {
76d7553f 2168 goto error_create_poll;
5eb91c98 2169 }
273ea72c 2170
5eb91c98
DG
2171 /* Add the application registration socket */
2172 ret = lttng_poll_add(&events, apps_sock, LPOLLIN | LPOLLRDHUP);
2173 if (ret < 0) {
76d7553f 2174 goto error_poll_add;
5eb91c98 2175 }
273ea72c 2176
1d4b027a 2177 /* Notify all applications to register */
0fdd1e2c
DG
2178 ret = notify_ust_apps(1);
2179 if (ret < 0) {
2180 ERR("Failed to notify applications or create the wait shared memory.\n"
54d01ffb
DG
2181 "Execution continues but there might be problem for already\n"
2182 "running applications that wishes to register.");
0fdd1e2c 2183 }
1d4b027a
DG
2184
2185 while (1) {
2186 DBG("Accepting application registration");
273ea72c
DG
2187
2188 /* Inifinite blocking call, waiting for transmission */
88f2b785 2189 restart:
a78af745 2190 health_poll_entry();
5eb91c98 2191 ret = lttng_poll_wait(&events, -1);
a78af745 2192 health_poll_exit();
273ea72c 2193 if (ret < 0) {
88f2b785
MD
2194 /*
2195 * Restart interrupted system call.
2196 */
2197 if (errno == EINTR) {
2198 goto restart;
2199 }
273ea72c
DG
2200 goto error;
2201 }
2202
0d9c5d77
DG
2203 nb_fd = ret;
2204
5eb91c98 2205 for (i = 0; i < nb_fd; i++) {
840cb59c 2206 health_code_update();
139ac872 2207
5eb91c98
DG
2208 /* Fetch once the poll data */
2209 revents = LTTNG_POLL_GETEV(&events, i);
2210 pollfd = LTTNG_POLL_GETFD(&events, i);
273ea72c 2211
fd20dac9
MD
2212 if (!revents) {
2213 /* No activity for this FD (poll implementation). */
2214 continue;
2215 }
2216
5eb91c98 2217 /* Thread quit pipe has been closed. Killing thread. */
d0b96690 2218 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
5eb91c98 2219 if (ret) {
139ac872
MD
2220 err = 0;
2221 goto exit;
90014c57 2222 }
1d4b027a 2223
5eb91c98
DG
2224 /* Event on the registration socket */
2225 if (pollfd == apps_sock) {
03e43155 2226 if (revents & LPOLLIN) {
5eb91c98
DG
2227 sock = lttcomm_accept_unix_sock(apps_sock);
2228 if (sock < 0) {
2229 goto error;
2230 }
099e26bd 2231
16c5c8fa
DG
2232 /*
2233 * Set socket timeout for both receiving and ending.
2234 * app_socket_timeout is in seconds, whereas
2235 * lttcomm_setsockopt_rcv_timeout and
2236 * lttcomm_setsockopt_snd_timeout expect msec as
2237 * parameter.
2238 */
28ce0ff2
JG
2239 if (app_socket_timeout >= 0) {
2240 (void) lttcomm_setsockopt_rcv_timeout(sock,
2241 app_socket_timeout * 1000);
2242 (void) lttcomm_setsockopt_snd_timeout(sock,
2243 app_socket_timeout * 1000);
2244 }
16c5c8fa 2245
b662582b
DG
2246 /*
2247 * Set the CLOEXEC flag. Return code is useless because
2248 * either way, the show must go on.
2249 */
2250 (void) utils_set_fd_cloexec(sock);
2251
5eb91c98 2252 /* Create UST registration command for enqueuing */
ba7f0ae5 2253 ust_cmd = zmalloc(sizeof(struct ust_command));
5eb91c98 2254 if (ust_cmd == NULL) {
76d7553f 2255 PERROR("ust command zmalloc");
41ed8e47
MD
2256 ret = close(sock);
2257 if (ret) {
2258 PERROR("close");
2259 }
5eb91c98
DG
2260 goto error;
2261 }
1d4b027a 2262
5eb91c98
DG
2263 /*
2264 * Using message-based transmissions to ensure we don't
2265 * have to deal with partially received messages.
2266 */
4063050c
MD
2267 ret = lttng_fd_get(LTTNG_FD_APPS, 1);
2268 if (ret < 0) {
2269 ERR("Exhausted file descriptors allowed for applications.");
2270 free(ust_cmd);
2271 ret = close(sock);
2272 if (ret) {
2273 PERROR("close");
2274 }
2275 sock = -1;
2276 continue;
2277 }
d88aee68 2278
840cb59c 2279 health_code_update();
d0b96690
DG
2280 ret = ust_app_recv_registration(sock, &ust_cmd->reg_msg);
2281 if (ret < 0) {
5eb91c98 2282 free(ust_cmd);
d0b96690 2283 /* Close socket of the application. */
76d7553f
MD
2284 ret = close(sock);
2285 if (ret) {
2286 PERROR("close");
2287 }
4063050c 2288 lttng_fd_put(LTTNG_FD_APPS, 1);
76d7553f 2289 sock = -1;
5eb91c98
DG
2290 continue;
2291 }
840cb59c 2292 health_code_update();
099e26bd 2293
5eb91c98 2294 ust_cmd->sock = sock;
34a2494f 2295 sock = -1;
099e26bd 2296
5eb91c98
DG
2297 DBG("UST registration received with pid:%d ppid:%d uid:%d"
2298 " gid:%d sock:%d name:%s (version %d.%d)",
2299 ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid,
2300 ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid,
2301 ust_cmd->sock, ust_cmd->reg_msg.name,
2302 ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor);
54d01ffb 2303
5eb91c98
DG
2304 /*
2305 * Lock free enqueue the registration request. The red pill
54d01ffb 2306 * has been taken! This apps will be part of the *system*.
5eb91c98 2307 */
8bdee6e2 2308 cds_wfcq_enqueue(&ust_cmd_queue.head, &ust_cmd_queue.tail, &ust_cmd->node);
5eb91c98
DG
2309
2310 /*
2311 * Wake the registration queue futex. Implicit memory
8bdee6e2 2312 * barrier with the exchange in cds_wfcq_enqueue.
5eb91c98
DG
2313 */
2314 futex_nto1_wake(&ust_cmd_queue.futex);
03e43155
MD
2315 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
2316 ERR("Register apps socket poll error");
2317 goto error;
2318 } else {
2319 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
2320 goto error;
5eb91c98
DG
2321 }
2322 }
90014c57 2323 }
1d4b027a
DG
2324 }
2325
139ac872 2326exit:
1d4b027a 2327error:
0fdd1e2c
DG
2328 /* Notify that the registration thread is gone */
2329 notify_ust_apps(0);
2330
a4b35e07 2331 if (apps_sock >= 0) {
76d7553f
MD
2332 ret = close(apps_sock);
2333 if (ret) {
2334 PERROR("close");
2335 }
a4b35e07 2336 }
46c3f085 2337 if (sock >= 0) {
76d7553f
MD
2338 ret = close(sock);
2339 if (ret) {
2340 PERROR("close");
2341 }
4063050c 2342 lttng_fd_put(LTTNG_FD_APPS, 1);
a4b35e07 2343 }
273ea72c 2344 unlink(apps_unix_sock_path);
0fdd1e2c 2345
76d7553f 2346error_poll_add:
5eb91c98 2347 lttng_poll_clean(&events);
76d7553f
MD
2348error_listen:
2349error_create_poll:
6993eeb3 2350error_testpoint:
76d7553f 2351 DBG("UST Registration thread cleanup complete");
9ad42ec1
MD
2352 if (err) {
2353 health_error();
2354 ERR("Health error occurred in %s", __func__);
2355 }
8782cc74 2356 health_unregister(health_sessiond);
5eb91c98 2357
1d4b027a
DG
2358 return NULL;
2359}
2360
8c0faa1d 2361/*
3bd1e081 2362 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
d063d709 2363 * exec or it will fails.
8c0faa1d 2364 */
3bd1e081 2365static int spawn_consumer_thread(struct consumer_data *consumer_data)
8c0faa1d 2366{
a23ec3a7 2367 int ret, clock_ret;
ee0b0061
DG
2368 struct timespec timeout;
2369
13a7bce3
JG
2370 /*
2371 * Make sure we set the readiness flag to 0 because we are NOT ready.
2372 * This access to consumer_thread_is_ready does not need to be
2373 * protected by consumer_data.cond_mutex (yet) since the consumer
2374 * management thread has not been started at this point.
2375 */
a23ec3a7 2376 consumer_data->consumer_thread_is_ready = 0;
8c0faa1d 2377
a23ec3a7
DG
2378 /* Setup pthread condition */
2379 ret = pthread_condattr_init(&consumer_data->condattr);
4a15001e 2380 if (ret) {
a23ec3a7
DG
2381 errno = ret;
2382 PERROR("pthread_condattr_init consumer data");
2383 goto error;
2384 }
2385
2386 /*
2387 * Set the monotonic clock in order to make sure we DO NOT jump in time
2388 * between the clock_gettime() call and the timedwait call. See bug #324
2389 * for a more details and how we noticed it.
2390 */
2391 ret = pthread_condattr_setclock(&consumer_data->condattr, CLOCK_MONOTONIC);
4a15001e 2392 if (ret) {
a23ec3a7
DG
2393 errno = ret;
2394 PERROR("pthread_condattr_setclock consumer data");
ee0b0061
DG
2395 goto error;
2396 }
8c0faa1d 2397
a23ec3a7 2398 ret = pthread_cond_init(&consumer_data->cond, &consumer_data->condattr);
4a15001e 2399 if (ret) {
a23ec3a7
DG
2400 errno = ret;
2401 PERROR("pthread_cond_init consumer data");
2402 goto error;
2403 }
2404
1a1a34b4
MJ
2405 ret = pthread_create(&consumer_data->thread, default_pthread_attr(),
2406 thread_manage_consumer, consumer_data);
4a15001e
MD
2407 if (ret) {
2408 errno = ret;
3bd1e081 2409 PERROR("pthread_create consumer");
ee0b0061 2410 ret = -1;
8c0faa1d
DG
2411 goto error;
2412 }
2413
a23ec3a7
DG
2414 /* We are about to wait on a pthread condition */
2415 pthread_mutex_lock(&consumer_data->cond_mutex);
2416
ee0b0061 2417 /* Get time for sem_timedwait absolute timeout */
389fbf04 2418 clock_ret = lttng_clock_gettime(CLOCK_MONOTONIC, &timeout);
a23ec3a7
DG
2419 /*
2420 * Set the timeout for the condition timed wait even if the clock gettime
2421 * call fails since we might loop on that call and we want to avoid to
2422 * increment the timeout too many times.
2423 */
2424 timeout.tv_sec += DEFAULT_SEM_WAIT_TIMEOUT;
2425
2426 /*
2427 * The following loop COULD be skipped in some conditions so this is why we
2428 * set ret to 0 in order to make sure at least one round of the loop is
2429 * done.
2430 */
2431 ret = 0;
2432
2433 /*
2434 * Loop until the condition is reached or when a timeout is reached. Note
2435 * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT
2436 * be returned but the pthread_cond(3), from the glibc-doc, says that it is
2437 * possible. This loop does not take any chances and works with both of
2438 * them.
2439 */
2440 while (!consumer_data->consumer_thread_is_ready && ret != ETIMEDOUT) {
2441 if (clock_ret < 0) {
2442 PERROR("clock_gettime spawn consumer");
2443 /* Infinite wait for the consumerd thread to be ready */
2444 ret = pthread_cond_wait(&consumer_data->cond,
2445 &consumer_data->cond_mutex);
2446 } else {
2447 ret = pthread_cond_timedwait(&consumer_data->cond,
2448 &consumer_data->cond_mutex, &timeout);
2449 }
ee0b0061 2450 }
8c0faa1d 2451
a23ec3a7
DG
2452 /* Release the pthread condition */
2453 pthread_mutex_unlock(&consumer_data->cond_mutex);
2454
2455 if (ret != 0) {
2456 errno = ret;
2457 if (ret == ETIMEDOUT) {
4282f9a3
DG
2458 int pth_ret;
2459
ee0b0061
DG
2460 /*
2461 * Call has timed out so we kill the kconsumerd_thread and return
2462 * an error.
2463 */
a23ec3a7
DG
2464 ERR("Condition timed out. The consumer thread was never ready."
2465 " Killing it");
4282f9a3
DG
2466 pth_ret = pthread_cancel(consumer_data->thread);
2467 if (pth_ret < 0) {
3bd1e081 2468 PERROR("pthread_cancel consumer thread");
ee0b0061
DG
2469 }
2470 } else {
a23ec3a7 2471 PERROR("pthread_cond_wait failed consumer thread");
ee0b0061 2472 }
4282f9a3
DG
2473 /* Caller is expecting a negative value on failure. */
2474 ret = -1;
ee0b0061
DG
2475 goto error;
2476 }
2477
3bd1e081
MD
2478 pthread_mutex_lock(&consumer_data->pid_mutex);
2479 if (consumer_data->pid == 0) {
a23ec3a7 2480 ERR("Consumerd did not start");
3bd1e081 2481 pthread_mutex_unlock(&consumer_data->pid_mutex);
712ea556
DG
2482 goto error;
2483 }
3bd1e081 2484 pthread_mutex_unlock(&consumer_data->pid_mutex);
712ea556 2485
8c0faa1d
DG
2486 return 0;
2487
2488error:
2489 return ret;
2490}
2491
d9800920 2492/*
3bd1e081 2493 * Join consumer thread
d9800920 2494 */
3bd1e081 2495static int join_consumer_thread(struct consumer_data *consumer_data)
cf3af59e
MD
2496{
2497 void *status;
cf3af59e 2498
e8209f6b
DG
2499 /* Consumer pid must be a real one. */
2500 if (consumer_data->pid > 0) {
c617c0c6 2501 int ret;
3bd1e081 2502 ret = kill(consumer_data->pid, SIGTERM);
cf3af59e 2503 if (ret) {
4a15001e 2504 PERROR("Error killing consumer daemon");
cf3af59e
MD
2505 return ret;
2506 }
3bd1e081 2507 return pthread_join(consumer_data->thread, &status);
cf3af59e
MD
2508 } else {
2509 return 0;
2510 }
2511}
2512
8c0faa1d 2513/*
3bd1e081 2514 * Fork and exec a consumer daemon (consumerd).
8c0faa1d 2515 *
d063d709 2516 * Return pid if successful else -1.
8c0faa1d 2517 */
3bd1e081 2518static pid_t spawn_consumerd(struct consumer_data *consumer_data)
8c0faa1d
DG
2519{
2520 int ret;
2521 pid_t pid;
94c55f17 2522 const char *consumer_to_use;
53086306 2523 const char *verbosity;
94c55f17 2524 struct stat st;
8c0faa1d 2525
3bd1e081 2526 DBG("Spawning consumerd");
c49dc785 2527
8c0faa1d
DG
2528 pid = fork();
2529 if (pid == 0) {
2530 /*
3bd1e081 2531 * Exec consumerd.
8c0faa1d 2532 */
daee5345 2533 if (opt_verbose_consumer) {
53086306 2534 verbosity = "--verbose";
4421f712 2535 } else if (lttng_opt_quiet) {
53086306 2536 verbosity = "--quiet";
4421f712
DG
2537 } else {
2538 verbosity = "";
53086306 2539 }
4421f712 2540
3bd1e081
MD
2541 switch (consumer_data->type) {
2542 case LTTNG_CONSUMER_KERNEL:
94c55f17 2543 /*
c7704d57
DG
2544 * Find out which consumerd to execute. We will first try the
2545 * 64-bit path, then the sessiond's installation directory, and
2546 * fallback on the 32-bit one,
94c55f17 2547 */
63a799e8
AM
2548 DBG3("Looking for a kernel consumer at these locations:");
2549 DBG3(" 1) %s", consumerd64_bin);
2550 DBG3(" 2) %s/%s", INSTALL_BIN_PATH, CONSUMERD_FILE);
2551 DBG3(" 3) %s", consumerd32_bin);
94c55f17 2552 if (stat(consumerd64_bin, &st) == 0) {
63a799e8 2553 DBG3("Found location #1");
94c55f17 2554 consumer_to_use = consumerd64_bin;
94c55f17 2555 } else if (stat(INSTALL_BIN_PATH "/" CONSUMERD_FILE, &st) == 0) {
63a799e8 2556 DBG3("Found location #2");
94c55f17 2557 consumer_to_use = INSTALL_BIN_PATH "/" CONSUMERD_FILE;
eb1e0bd4 2558 } else if (stat(consumerd32_bin, &st) == 0) {
63a799e8 2559 DBG3("Found location #3");
eb1e0bd4 2560 consumer_to_use = consumerd32_bin;
94c55f17 2561 } else {
63a799e8 2562 DBG("Could not find any valid consumerd executable");
4282f9a3 2563 ret = -EINVAL;
5b49a21f 2564 goto error;
94c55f17
AM
2565 }
2566 DBG("Using kernel consumer at: %s", consumer_to_use);
4282f9a3 2567 ret = execl(consumer_to_use,
94c55f17
AM
2568 "lttng-consumerd", verbosity, "-k",
2569 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
2570 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
6c71277b 2571 "--group", tracing_group_name,
94c55f17 2572 NULL);
3bd1e081 2573 break;
7753dea8
MD
2574 case LTTNG_CONSUMER64_UST:
2575 {
b1e0b6b6 2576 char *tmpnew = NULL;
8f4905da
MD
2577
2578 if (consumerd64_libdir[0] != '\0') {
2579 char *tmp;
2580 size_t tmplen;
2581
e8fa9fb0 2582 tmp = lttng_secure_getenv("LD_LIBRARY_PATH");
8f4905da
MD
2583 if (!tmp) {
2584 tmp = "";
2585 }
2586 tmplen = strlen("LD_LIBRARY_PATH=")
2587 + strlen(consumerd64_libdir) + 1 /* : */ + strlen(tmp);
2588 tmpnew = zmalloc(tmplen + 1 /* \0 */);
2589 if (!tmpnew) {
2590 ret = -ENOMEM;
2591 goto error;
2592 }
2593 strcpy(tmpnew, "LD_LIBRARY_PATH=");
2594 strcat(tmpnew, consumerd64_libdir);
2595 if (tmp[0] != '\0') {
2596 strcat(tmpnew, ":");
2597 strcat(tmpnew, tmp);
2598 }
2599 ret = putenv(tmpnew);
2600 if (ret) {
2601 ret = -errno;
c6f76da9 2602 free(tmpnew);
8f4905da
MD
2603 goto error;
2604 }
2605 }
94c55f17 2606 DBG("Using 64-bit UST consumer at: %s", consumerd64_bin);
a5a6aff3 2607 ret = execl(consumerd64_bin, "lttng-consumerd", verbosity, "-u",
7753dea8
MD
2608 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
2609 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
6c71277b 2610 "--group", tracing_group_name,
7753dea8 2611 NULL);
8f4905da
MD
2612 if (consumerd64_libdir[0] != '\0') {
2613 free(tmpnew);
2614 }
3bd1e081 2615 break;
7753dea8
MD
2616 }
2617 case LTTNG_CONSUMER32_UST:
2618 {
937dde8e 2619 char *tmpnew = NULL;
8f4905da
MD
2620
2621 if (consumerd32_libdir[0] != '\0') {
2622 char *tmp;
2623 size_t tmplen;
2624
e8fa9fb0 2625 tmp = lttng_secure_getenv("LD_LIBRARY_PATH");
8f4905da
MD
2626 if (!tmp) {
2627 tmp = "";
2628 }
2629 tmplen = strlen("LD_LIBRARY_PATH=")
2630 + strlen(consumerd32_libdir) + 1 /* : */ + strlen(tmp);
2631 tmpnew = zmalloc(tmplen + 1 /* \0 */);
2632 if (!tmpnew) {
2633 ret = -ENOMEM;
2634 goto error;
2635 }
2636 strcpy(tmpnew, "LD_LIBRARY_PATH=");
2637 strcat(tmpnew, consumerd32_libdir);
2638 if (tmp[0] != '\0') {
2639 strcat(tmpnew, ":");
2640 strcat(tmpnew, tmp);
2641 }
2642 ret = putenv(tmpnew);
2643 if (ret) {
2644 ret = -errno;
c6f76da9 2645 free(tmpnew);
8f4905da
MD
2646 goto error;
2647 }
2648 }
94c55f17 2649 DBG("Using 32-bit UST consumer at: %s", consumerd32_bin);
a5a6aff3 2650 ret = execl(consumerd32_bin, "lttng-consumerd", verbosity, "-u",
7753dea8
MD
2651 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
2652 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
6c71277b 2653 "--group", tracing_group_name,
7753dea8 2654 NULL);
8f4905da
MD
2655 if (consumerd32_libdir[0] != '\0') {
2656 free(tmpnew);
2657 }
7753dea8
MD
2658 break;
2659 }
3bd1e081 2660 default:
db58870b 2661 ERR("unknown consumer type");
3bd1e081
MD
2662 exit(EXIT_FAILURE);
2663 }
8c0faa1d 2664 if (errno != 0) {
4282f9a3 2665 PERROR("Consumer execl()");
8c0faa1d 2666 }
4282f9a3 2667 /* Reaching this point, we got a failure on our execl(). */
8c0faa1d
DG
2668 exit(EXIT_FAILURE);
2669 } else if (pid > 0) {
2670 ret = pid;
8c0faa1d 2671 } else {
76d7553f 2672 PERROR("start consumer fork");
8c0faa1d 2673 ret = -errno;
8c0faa1d 2674 }
8f4905da 2675error:
8c0faa1d
DG
2676 return ret;
2677}
2678
693bd40b 2679/*
3bd1e081 2680 * Spawn the consumerd daemon and session daemon thread.
693bd40b 2681 */
3bd1e081 2682static int start_consumerd(struct consumer_data *consumer_data)
693bd40b 2683{
c617c0c6 2684 int ret;
edb8b045
DG
2685
2686 /*
2687 * Set the listen() state on the socket since there is a possible race
2688 * between the exec() of the consumer daemon and this call if place in the
2689 * consumer thread. See bug #366 for more details.
2690 */
2691 ret = lttcomm_listen_unix_sock(consumer_data->err_sock);
2692 if (ret < 0) {
2693 goto error;
2694 }
693bd40b 2695
3bd1e081
MD
2696 pthread_mutex_lock(&consumer_data->pid_mutex);
2697 if (consumer_data->pid != 0) {
2698 pthread_mutex_unlock(&consumer_data->pid_mutex);
c49dc785
DG
2699 goto end;
2700 }
693bd40b 2701
3bd1e081 2702 ret = spawn_consumerd(consumer_data);
c49dc785 2703 if (ret < 0) {
3bd1e081
MD
2704 ERR("Spawning consumerd failed");
2705 pthread_mutex_unlock(&consumer_data->pid_mutex);
c49dc785 2706 goto error;
693bd40b 2707 }
c49dc785 2708
3bd1e081
MD
2709 /* Setting up the consumer_data pid */
2710 consumer_data->pid = ret;
48842b30 2711 DBG2("Consumer pid %d", consumer_data->pid);
3bd1e081 2712 pthread_mutex_unlock(&consumer_data->pid_mutex);
693bd40b 2713
3bd1e081
MD
2714 DBG2("Spawning consumer control thread");
2715 ret = spawn_consumer_thread(consumer_data);
693bd40b 2716 if (ret < 0) {
3bd1e081 2717 ERR("Fatal error spawning consumer control thread");
693bd40b
DG
2718 goto error;
2719 }
2720
c49dc785 2721end:
693bd40b
DG
2722 return 0;
2723
2724error:
331744e3 2725 /* Cleanup already created sockets on error. */
edb8b045 2726 if (consumer_data->err_sock >= 0) {
c617c0c6
MD
2727 int err;
2728
edb8b045
DG
2729 err = close(consumer_data->err_sock);
2730 if (err < 0) {
2731 PERROR("close consumer data error socket");
2732 }
2733 }
693bd40b
DG
2734 return ret;
2735}
2736
b73401da 2737/*
096102bd 2738 * Setup necessary data for kernel tracer action.
b73401da 2739 */
096102bd 2740static int init_kernel_tracer(void)
b73401da
DG
2741{
2742 int ret;
b73401da 2743
096102bd
DG
2744 /* Modprobe lttng kernel modules */
2745 ret = modprobe_lttng_control();
b73401da 2746 if (ret < 0) {
b73401da
DG
2747 goto error;
2748 }
2749
096102bd
DG
2750 /* Open debugfs lttng */
2751 kernel_tracer_fd = open(module_proc_lttng, O_RDWR);
2752 if (kernel_tracer_fd < 0) {
2753 DBG("Failed to open %s", module_proc_lttng);
2f77fc4b 2754 goto error_open;
54d01ffb
DG
2755 }
2756
2f77fc4b
DG
2757 /* Validate kernel version */
2758 ret = kernel_validate_version(kernel_tracer_fd);
2759 if (ret < 0) {
2760 goto error_version;
b551a063 2761 }
54d01ffb 2762
2f77fc4b
DG
2763 ret = modprobe_lttng_data();
2764 if (ret < 0) {
2765 goto error_modules;
54d01ffb
DG
2766 }
2767
2f77fc4b
DG
2768 DBG("Kernel tracer fd %d", kernel_tracer_fd);
2769 return 0;
2770
2771error_version:
2772 modprobe_remove_lttng_control();
2773 ret = close(kernel_tracer_fd);
2774 if (ret) {
2775 PERROR("close");
b551a063 2776 }
2f77fc4b 2777 kernel_tracer_fd = -1;
f73fabfd 2778 return LTTNG_ERR_KERN_VERSION;
b551a063 2779
2f77fc4b
DG
2780error_modules:
2781 ret = close(kernel_tracer_fd);
2782 if (ret) {
2783 PERROR("close");
b551a063 2784 }
54d01ffb 2785
2f77fc4b
DG
2786error_open:
2787 modprobe_remove_lttng_control();
54d01ffb
DG
2788
2789error:
2f77fc4b
DG
2790 WARN("No kernel tracer available");
2791 kernel_tracer_fd = -1;
2792 if (!is_root) {
f73fabfd 2793 return LTTNG_ERR_NEED_ROOT_SESSIOND;
2f77fc4b 2794 } else {
f73fabfd 2795 return LTTNG_ERR_KERN_NA;
2f77fc4b 2796 }
54d01ffb
DG
2797}
2798
2f77fc4b 2799
54d01ffb 2800/*
2f77fc4b
DG
2801 * Copy consumer output from the tracing session to the domain session. The
2802 * function also applies the right modification on a per domain basis for the
2803 * trace files destination directory.
36b588ed
MD
2804 *
2805 * Should *NOT* be called with RCU read-side lock held.
54d01ffb 2806 */
2f77fc4b 2807static int copy_session_consumer(int domain, struct ltt_session *session)
54d01ffb
DG
2808{
2809 int ret;
2f77fc4b
DG
2810 const char *dir_name;
2811 struct consumer_output *consumer;
2812
2813 assert(session);
2814 assert(session->consumer);
54d01ffb 2815
b551a063
DG
2816 switch (domain) {
2817 case LTTNG_DOMAIN_KERNEL:
2f77fc4b 2818 DBG3("Copying tracing session consumer output in kernel session");
09a90bcd
DG
2819 /*
2820 * XXX: We should audit the session creation and what this function
2821 * does "extra" in order to avoid a destroy since this function is used
2822 * in the domain session creation (kernel and ust) only. Same for UST
2823 * domain.
2824 */
2825 if (session->kernel_session->consumer) {
6addfa37 2826 consumer_output_put(session->kernel_session->consumer);
09a90bcd 2827 }
2f77fc4b
DG
2828 session->kernel_session->consumer =
2829 consumer_copy_output(session->consumer);
2830 /* Ease our life a bit for the next part */
2831 consumer = session->kernel_session->consumer;
2832 dir_name = DEFAULT_KERNEL_TRACE_DIR;
b551a063 2833 break;
f20baf8e 2834 case LTTNG_DOMAIN_JUL:
5cdb6027 2835 case LTTNG_DOMAIN_LOG4J:
0e115563 2836 case LTTNG_DOMAIN_PYTHON:
b551a063 2837 case LTTNG_DOMAIN_UST:
2f77fc4b 2838 DBG3("Copying tracing session consumer output in UST session");
09a90bcd 2839 if (session->ust_session->consumer) {
6addfa37 2840 consumer_output_put(session->ust_session->consumer);
09a90bcd 2841 }
2f77fc4b
DG
2842 session->ust_session->consumer =
2843 consumer_copy_output(session->consumer);
2844 /* Ease our life a bit for the next part */
2845 consumer = session->ust_session->consumer;
2846 dir_name = DEFAULT_UST_TRACE_DIR;
b551a063
DG
2847 break;
2848 default:
f73fabfd 2849 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
54d01ffb
DG
2850 goto error;
2851 }
2852
2f77fc4b 2853 /* Append correct directory to subdir */
c30ce0b3
CB
2854 strncat(consumer->subdir, dir_name,
2855 sizeof(consumer->subdir) - strlen(consumer->subdir) - 1);
2f77fc4b
DG
2856 DBG3("Copy session consumer subdir %s", consumer->subdir);
2857
f73fabfd 2858 ret = LTTNG_OK;
54d01ffb
DG
2859
2860error:
2861 return ret;
2862}
2863
00e2e675 2864/*
2f77fc4b 2865 * Create an UST session and add it to the session ust list.
36b588ed
MD
2866 *
2867 * Should *NOT* be called with RCU read-side lock held.
00e2e675 2868 */
2f77fc4b
DG
2869static int create_ust_session(struct ltt_session *session,
2870 struct lttng_domain *domain)
00e2e675
DG
2871{
2872 int ret;
2f77fc4b 2873 struct ltt_ust_session *lus = NULL;
00e2e675 2874
a4b92340 2875 assert(session);
2f77fc4b
DG
2876 assert(domain);
2877 assert(session->consumer);
a4b92340 2878
2f77fc4b 2879 switch (domain->type) {
f20baf8e 2880 case LTTNG_DOMAIN_JUL:
5cdb6027 2881 case LTTNG_DOMAIN_LOG4J:
0e115563 2882 case LTTNG_DOMAIN_PYTHON:
2f77fc4b
DG
2883 case LTTNG_DOMAIN_UST:
2884 break;
2885 default:
2886 ERR("Unknown UST domain on create session %d", domain->type);
f73fabfd 2887 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
00e2e675
DG
2888 goto error;
2889 }
2890
2f77fc4b
DG
2891 DBG("Creating UST session");
2892
dec56f6c 2893 lus = trace_ust_create_session(session->id);
2f77fc4b 2894 if (lus == NULL) {
f73fabfd 2895 ret = LTTNG_ERR_UST_SESS_FAIL;
a4b92340
DG
2896 goto error;
2897 }
2898
2f77fc4b
DG
2899 lus->uid = session->uid;
2900 lus->gid = session->gid;
2bba9e53 2901 lus->output_traces = session->output_traces;
27babd3a 2902 lus->snapshot_mode = session->snapshot_mode;
ecc48a90 2903 lus->live_timer_interval = session->live_timer;
2f77fc4b 2904 session->ust_session = lus;
d7ba1388 2905 if (session->shm_path[0]) {
3d071855
MD
2906 strncpy(lus->root_shm_path, session->shm_path,
2907 sizeof(lus->root_shm_path));
2908 lus->root_shm_path[sizeof(lus->root_shm_path) - 1] = '\0';
d7ba1388
MD
2909 strncpy(lus->shm_path, session->shm_path,
2910 sizeof(lus->shm_path));
2911 lus->shm_path[sizeof(lus->shm_path) - 1] = '\0';
2912 strncat(lus->shm_path, "/ust",
2913 sizeof(lus->shm_path) - strlen(lus->shm_path) - 1);
2914 }
2f77fc4b
DG
2915 /* Copy session output to the newly created UST session */
2916 ret = copy_session_consumer(domain->type, session);
f73fabfd 2917 if (ret != LTTNG_OK) {
00e2e675
DG
2918 goto error;
2919 }
2920
f73fabfd 2921 return LTTNG_OK;
00e2e675
DG
2922
2923error:
2f77fc4b
DG
2924 free(lus);
2925 session->ust_session = NULL;
00e2e675
DG
2926 return ret;
2927}
2928
2929/*
2f77fc4b 2930 * Create a kernel tracer session then create the default channel.
00e2e675 2931 */
2f77fc4b 2932static int create_kernel_session(struct ltt_session *session)
00e2e675
DG
2933{
2934 int ret;
a4b92340 2935
2f77fc4b 2936 DBG("Creating kernel session");
00e2e675 2937
2f77fc4b
DG
2938 ret = kernel_create_session(session, kernel_tracer_fd);
2939 if (ret < 0) {
f73fabfd 2940 ret = LTTNG_ERR_KERN_SESS_FAIL;
00e2e675
DG
2941 goto error;
2942 }
2943
2f77fc4b
DG
2944 /* Code flow safety */
2945 assert(session->kernel_session);
2946
2947 /* Copy session output to the newly created Kernel session */
2948 ret = copy_session_consumer(LTTNG_DOMAIN_KERNEL, session);
f73fabfd 2949 if (ret != LTTNG_OK) {
a4b92340
DG
2950 goto error;
2951 }
2952
2f77fc4b
DG
2953 /* Create directory(ies) on local filesystem. */
2954 if (session->kernel_session->consumer->type == CONSUMER_DST_LOCAL &&
2955 strlen(session->kernel_session->consumer->dst.trace_path) > 0) {
2956 ret = run_as_mkdir_recursive(
2957 session->kernel_session->consumer->dst.trace_path,
2958 S_IRWXU | S_IRWXG, session->uid, session->gid);
2959 if (ret < 0) {
df5b86c8 2960 if (errno != EEXIST) {
2f77fc4b 2961 ERR("Trace directory creation error");
00e2e675
DG
2962 goto error;
2963 }
00e2e675 2964 }
2f77fc4b 2965 }
00e2e675 2966
2f77fc4b
DG
2967 session->kernel_session->uid = session->uid;
2968 session->kernel_session->gid = session->gid;
2bba9e53 2969 session->kernel_session->output_traces = session->output_traces;
27babd3a 2970 session->kernel_session->snapshot_mode = session->snapshot_mode;
00e2e675 2971
f73fabfd 2972 return LTTNG_OK;
00e2e675 2973
2f77fc4b
DG
2974error:
2975 trace_kernel_destroy_session(session->kernel_session);
2976 session->kernel_session = NULL;
2977 return ret;
2978}
00e2e675 2979
2f77fc4b
DG
2980/*
2981 * Count number of session permitted by uid/gid.
2982 */
2983static unsigned int lttng_sessions_count(uid_t uid, gid_t gid)
2984{
2985 unsigned int i = 0;
2986 struct ltt_session *session;
07424f16 2987
2f77fc4b
DG
2988 DBG("Counting number of available session for UID %d GID %d",
2989 uid, gid);
2990 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
00e2e675 2991 /*
2f77fc4b 2992 * Only list the sessions the user can control.
00e2e675 2993 */
2f77fc4b
DG
2994 if (!session_access_ok(session, uid, gid)) {
2995 continue;
2996 }
2997 i++;
a4b92340 2998 }
2f77fc4b 2999 return i;
00e2e675
DG
3000}
3001
54d01ffb
DG
3002/*
3003 * Process the command requested by the lttng client within the command
3004 * context structure. This function make sure that the return structure (llm)
3005 * is set and ready for transmission before returning.
3006 *
3007 * Return any error encountered or 0 for success.
53a80697
MD
3008 *
3009 * "sock" is only used for special-case var. len data.
36b588ed
MD
3010 *
3011 * Should *NOT* be called with RCU read-side lock held.
54d01ffb 3012 */
53a80697
MD
3013static int process_client_msg(struct command_ctx *cmd_ctx, int sock,
3014 int *sock_error)
54d01ffb 3015{
f73fabfd 3016 int ret = LTTNG_OK;
44d3bd01 3017 int need_tracing_session = 1;
2e09ba09 3018 int need_domain;
54d01ffb
DG
3019
3020 DBG("Processing client command %d", cmd_ctx->lsm->cmd_type);
3021
3745d315
MD
3022 assert(!rcu_read_ongoing());
3023
53a80697
MD
3024 *sock_error = 0;
3025
2e09ba09
MD
3026 switch (cmd_ctx->lsm->cmd_type) {
3027 case LTTNG_CREATE_SESSION:
27babd3a 3028 case LTTNG_CREATE_SESSION_SNAPSHOT:
ecc48a90 3029 case LTTNG_CREATE_SESSION_LIVE:
2e09ba09
MD
3030 case LTTNG_DESTROY_SESSION:
3031 case LTTNG_LIST_SESSIONS:
3032 case LTTNG_LIST_DOMAINS:
3033 case LTTNG_START_TRACE:
3034 case LTTNG_STOP_TRACE:
6d805429 3035 case LTTNG_DATA_PENDING:
da3c9ec1
DG
3036 case LTTNG_SNAPSHOT_ADD_OUTPUT:
3037 case LTTNG_SNAPSHOT_DEL_OUTPUT:
3038 case LTTNG_SNAPSHOT_LIST_OUTPUT:
3039 case LTTNG_SNAPSHOT_RECORD:
fb198a11 3040 case LTTNG_SAVE_SESSION:
d7ba1388 3041 case LTTNG_SET_SESSION_SHM_PATH:
eded6438 3042 case LTTNG_REGENERATE_METADATA:
c2561365 3043 case LTTNG_REGENERATE_STATEDUMP:
2e09ba09 3044 need_domain = 0;
3aace903 3045 break;
2e09ba09
MD
3046 default:
3047 need_domain = 1;
3048 }
3049
3050 if (opt_no_kernel && need_domain
3051 && cmd_ctx->lsm->domain.type == LTTNG_DOMAIN_KERNEL) {
531d29f9 3052 if (!is_root) {
f73fabfd 3053 ret = LTTNG_ERR_NEED_ROOT_SESSIOND;
531d29f9 3054 } else {
f73fabfd 3055 ret = LTTNG_ERR_KERN_NA;
531d29f9 3056 }
4fba7219
DG
3057 goto error;
3058 }
3059
8d3113b2
DG
3060 /* Deny register consumer if we already have a spawned consumer. */
3061 if (cmd_ctx->lsm->cmd_type == LTTNG_REGISTER_CONSUMER) {
3062 pthread_mutex_lock(&kconsumer_data.pid_mutex);
3063 if (kconsumer_data.pid > 0) {
f73fabfd 3064 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
fa317f24 3065 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
8d3113b2
DG
3066 goto error;
3067 }
3068 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
3069 }
3070
54d01ffb
DG
3071 /*
3072 * Check for command that don't needs to allocate a returned payload. We do
44d3bd01 3073 * this here so we don't have to make the call for no payload at each
54d01ffb
DG
3074 * command.
3075 */
3076 switch(cmd_ctx->lsm->cmd_type) {
3077 case LTTNG_LIST_SESSIONS:
3078 case LTTNG_LIST_TRACEPOINTS:
f37d259d 3079 case LTTNG_LIST_TRACEPOINT_FIELDS:
54d01ffb
DG
3080 case LTTNG_LIST_DOMAINS:
3081 case LTTNG_LIST_CHANNELS:
3082 case LTTNG_LIST_EVENTS:
834978fd 3083 case LTTNG_LIST_SYSCALLS:
a5dfbb9d 3084 case LTTNG_LIST_TRACKER_PIDS:
5cd0780d 3085 case LTTNG_DATA_PENDING:
54d01ffb
DG
3086 break;
3087 default:
3088 /* Setup lttng message with no payload */
6e10c9b9 3089 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, NULL, 0);
54d01ffb
DG
3090 if (ret < 0) {
3091 /* This label does not try to unlock the session */
3092 goto init_setup_error;
3093 }
3094 }
3095
3096 /* Commands that DO NOT need a session. */
3097 switch (cmd_ctx->lsm->cmd_type) {
54d01ffb 3098 case LTTNG_CREATE_SESSION:
27babd3a 3099 case LTTNG_CREATE_SESSION_SNAPSHOT:
ecc48a90 3100 case LTTNG_CREATE_SESSION_LIVE:
54d01ffb
DG
3101 case LTTNG_LIST_SESSIONS:
3102 case LTTNG_LIST_TRACEPOINTS:
834978fd 3103 case LTTNG_LIST_SYSCALLS:
f37d259d 3104 case LTTNG_LIST_TRACEPOINT_FIELDS:
fb198a11 3105 case LTTNG_SAVE_SESSION:
44d3bd01 3106 need_tracing_session = 0;
54d01ffb
DG
3107 break;
3108 default:
3109 DBG("Getting session %s by name", cmd_ctx->lsm->session.name);
256a5576
MD
3110 /*
3111 * We keep the session list lock across _all_ commands
3112 * for now, because the per-session lock does not
3113 * handle teardown properly.
3114 */
74babd95 3115 session_lock_list();
54d01ffb
DG
3116 cmd_ctx->session = session_find_by_name(cmd_ctx->lsm->session.name);
3117 if (cmd_ctx->session == NULL) {
bba2d65f 3118 ret = LTTNG_ERR_SESS_NOT_FOUND;
54d01ffb
DG
3119 goto error;
3120 } else {
3121 /* Acquire lock for the session */
3122 session_lock(cmd_ctx->session);
3123 }
3124 break;
3125 }
b389abbe 3126
5f3ecf22
DG
3127 /*
3128 * Commands that need a valid session but should NOT create one if none
3129 * exists. Instead of creating one and destroying it when the command is
3130 * handled, process that right before so we save some round trip in useless
3131 * code path.
3132 */
3133 switch (cmd_ctx->lsm->cmd_type) {
3134 case LTTNG_DISABLE_CHANNEL:
3135 case LTTNG_DISABLE_EVENT:
5f3ecf22
DG
3136 switch (cmd_ctx->lsm->domain.type) {
3137 case LTTNG_DOMAIN_KERNEL:
3138 if (!cmd_ctx->session->kernel_session) {
3139 ret = LTTNG_ERR_NO_CHANNEL;
3140 goto error;
3141 }
3142 break;
3143 case LTTNG_DOMAIN_JUL:
5cdb6027 3144 case LTTNG_DOMAIN_LOG4J:
0e115563 3145 case LTTNG_DOMAIN_PYTHON:
5f3ecf22
DG
3146 case LTTNG_DOMAIN_UST:
3147 if (!cmd_ctx->session->ust_session) {
3148 ret = LTTNG_ERR_NO_CHANNEL;
3149 goto error;
3150 }
3151 break;
3152 default:
3153 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
3154 goto error;
3155 }
3156 default:
3157 break;
3158 }
3159
2e09ba09
MD
3160 if (!need_domain) {
3161 goto skip_domain;
3162 }
a4b92340 3163
54d01ffb
DG
3164 /*
3165 * Check domain type for specific "pre-action".
3166 */
3167 switch (cmd_ctx->lsm->domain.type) {
3168 case LTTNG_DOMAIN_KERNEL:
d1f1c568 3169 if (!is_root) {
f73fabfd 3170 ret = LTTNG_ERR_NEED_ROOT_SESSIOND;
d1f1c568
DG
3171 goto error;
3172 }
3173
54d01ffb 3174 /* Kernel tracer check */
a4b35e07 3175 if (kernel_tracer_fd == -1) {
54d01ffb 3176 /* Basically, load kernel tracer modules */
096102bd
DG
3177 ret = init_kernel_tracer();
3178 if (ret != 0) {
54d01ffb
DG
3179 goto error;
3180 }
3181 }
5eb91c98 3182
5c827ce0
DG
3183 /* Consumer is in an ERROR state. Report back to client */
3184 if (uatomic_read(&kernel_consumerd_state) == CONSUMER_ERROR) {
f73fabfd 3185 ret = LTTNG_ERR_NO_KERNCONSUMERD;
5c827ce0
DG
3186 goto error;
3187 }
3188
54d01ffb 3189 /* Need a session for kernel command */
44d3bd01 3190 if (need_tracing_session) {
54d01ffb 3191 if (cmd_ctx->session->kernel_session == NULL) {
6df2e2c9 3192 ret = create_kernel_session(cmd_ctx->session);
5eb91c98 3193 if (ret < 0) {
f73fabfd 3194 ret = LTTNG_ERR_KERN_SESS_FAIL;
5eb91c98
DG
3195 goto error;
3196 }
b389abbe 3197 }
7d29a247 3198
54d01ffb 3199 /* Start the kernel consumer daemon */
3bd1e081
MD
3200 pthread_mutex_lock(&kconsumer_data.pid_mutex);
3201 if (kconsumer_data.pid == 0 &&
785d2d0d 3202 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
3bd1e081
MD
3203 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
3204 ret = start_consumerd(&kconsumer_data);
7d29a247 3205 if (ret < 0) {
f73fabfd 3206 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
54d01ffb 3207 goto error;
950131af 3208 }
5c827ce0 3209 uatomic_set(&kernel_consumerd_state, CONSUMER_STARTED);
3ff2ecac
MD
3210 } else {
3211 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
33a2b854 3212 }
173af62f 3213
a4b92340
DG
3214 /*
3215 * The consumer was just spawned so we need to add the socket to
3216 * the consumer output of the session if exist.
3217 */
3218 ret = consumer_create_socket(&kconsumer_data,
3219 cmd_ctx->session->kernel_session->consumer);
3220 if (ret < 0) {
3221 goto error;
173af62f 3222 }
0d0c377a 3223 }
5c827ce0 3224
54d01ffb 3225 break;
b9dfb167 3226 case LTTNG_DOMAIN_JUL:
5cdb6027 3227 case LTTNG_DOMAIN_LOG4J:
0e115563 3228 case LTTNG_DOMAIN_PYTHON:
2bdd86d4 3229 case LTTNG_DOMAIN_UST:
44d3bd01 3230 {
b51ec5b4
MD
3231 if (!ust_app_supported()) {
3232 ret = LTTNG_ERR_NO_UST;
3233 goto error;
3234 }
5c827ce0
DG
3235 /* Consumer is in an ERROR state. Report back to client */
3236 if (uatomic_read(&ust_consumerd_state) == CONSUMER_ERROR) {
f73fabfd 3237 ret = LTTNG_ERR_NO_USTCONSUMERD;
5c827ce0
DG
3238 goto error;
3239 }
3240
44d3bd01 3241 if (need_tracing_session) {
a4b92340 3242 /* Create UST session if none exist. */
f6a9efaa 3243 if (cmd_ctx->session->ust_session == NULL) {
44d3bd01 3244 ret = create_ust_session(cmd_ctx->session,
6df2e2c9 3245 &cmd_ctx->lsm->domain);
f73fabfd 3246 if (ret != LTTNG_OK) {
44d3bd01
DG
3247 goto error;
3248 }
3249 }
00e2e675 3250
7753dea8
MD
3251 /* Start the UST consumer daemons */
3252 /* 64-bit */
3253 pthread_mutex_lock(&ustconsumer64_data.pid_mutex);
fc7a59ce 3254 if (consumerd64_bin[0] != '\0' &&
7753dea8 3255 ustconsumer64_data.pid == 0 &&
785d2d0d 3256 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
7753dea8
MD
3257 pthread_mutex_unlock(&ustconsumer64_data.pid_mutex);
3258 ret = start_consumerd(&ustconsumer64_data);
2bdd86d4 3259 if (ret < 0) {
f73fabfd 3260 ret = LTTNG_ERR_UST_CONSUMER64_FAIL;
173af62f 3261 uatomic_set(&ust_consumerd64_fd, -EINVAL);
2bdd86d4
MD
3262 goto error;
3263 }
48842b30 3264
173af62f 3265 uatomic_set(&ust_consumerd64_fd, ustconsumer64_data.cmd_sock);
5c827ce0 3266 uatomic_set(&ust_consumerd_state, CONSUMER_STARTED);
3ff2ecac 3267 } else {
7753dea8
MD
3268 pthread_mutex_unlock(&ustconsumer64_data.pid_mutex);
3269 }
173af62f
DG
3270
3271 /*
3272 * Setup socket for consumer 64 bit. No need for atomic access
3273 * since it was set above and can ONLY be set in this thread.
3274 */
a4b92340
DG
3275 ret = consumer_create_socket(&ustconsumer64_data,
3276 cmd_ctx->session->ust_session->consumer);
3277 if (ret < 0) {
3278 goto error;
173af62f
DG
3279 }
3280
7753dea8 3281 /* 32-bit */
385b881b 3282 pthread_mutex_lock(&ustconsumer32_data.pid_mutex);
fc7a59ce 3283 if (consumerd32_bin[0] != '\0' &&
7753dea8 3284 ustconsumer32_data.pid == 0 &&
785d2d0d 3285 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
7753dea8
MD
3286 pthread_mutex_unlock(&ustconsumer32_data.pid_mutex);
3287 ret = start_consumerd(&ustconsumer32_data);
3288 if (ret < 0) {
f73fabfd 3289 ret = LTTNG_ERR_UST_CONSUMER32_FAIL;
173af62f 3290 uatomic_set(&ust_consumerd32_fd, -EINVAL);
7753dea8
MD
3291 goto error;
3292 }
5c827ce0 3293
173af62f 3294 uatomic_set(&ust_consumerd32_fd, ustconsumer32_data.cmd_sock);
5c827ce0 3295 uatomic_set(&ust_consumerd_state, CONSUMER_STARTED);
7753dea8
MD
3296 } else {
3297 pthread_mutex_unlock(&ustconsumer32_data.pid_mutex);
2bdd86d4 3298 }
173af62f
DG
3299
3300 /*
3301 * Setup socket for consumer 64 bit. No need for atomic access
3302 * since it was set above and can ONLY be set in this thread.
3303 */
a4b92340
DG
3304 ret = consumer_create_socket(&ustconsumer32_data,
3305 cmd_ctx->session->ust_session->consumer);
3306 if (ret < 0) {
3307 goto error;
173af62f 3308 }
44d3bd01
DG
3309 }
3310 break;
48842b30 3311 }
54d01ffb 3312 default:
54d01ffb
DG
3313 break;
3314 }
2e09ba09 3315skip_domain:
33a2b854 3316
5c827ce0
DG
3317 /* Validate consumer daemon state when start/stop trace command */
3318 if (cmd_ctx->lsm->cmd_type == LTTNG_START_TRACE ||
3319 cmd_ctx->lsm->cmd_type == LTTNG_STOP_TRACE) {
3320 switch (cmd_ctx->lsm->domain.type) {
e7619875
JG
3321 case LTTNG_DOMAIN_NONE:
3322 break;
b9dfb167 3323 case LTTNG_DOMAIN_JUL:
5cdb6027 3324 case LTTNG_DOMAIN_LOG4J:
0e115563 3325 case LTTNG_DOMAIN_PYTHON:
5c827ce0
DG
3326 case LTTNG_DOMAIN_UST:
3327 if (uatomic_read(&ust_consumerd_state) != CONSUMER_STARTED) {
f73fabfd 3328 ret = LTTNG_ERR_NO_USTCONSUMERD;
5c827ce0
DG
3329 goto error;
3330 }
3331 break;
3332 case LTTNG_DOMAIN_KERNEL:
3333 if (uatomic_read(&kernel_consumerd_state) != CONSUMER_STARTED) {
f73fabfd 3334 ret = LTTNG_ERR_NO_KERNCONSUMERD;
5c827ce0
DG
3335 goto error;
3336 }
3337 break;
825535cc
JG
3338 default:
3339 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
3340 goto error;
5c827ce0
DG
3341 }
3342 }
3343
8e0af1b4
MD
3344 /*
3345 * Check that the UID or GID match that of the tracing session.
3346 * The root user can interact with all sessions.
3347 */
3348 if (need_tracing_session) {
3349 if (!session_access_ok(cmd_ctx->session,
730389d9
DG
3350 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
3351 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds))) {
f73fabfd 3352 ret = LTTNG_ERR_EPERM;
8e0af1b4
MD
3353 goto error;
3354 }
3355 }
3356
ffe60014
DG
3357 /*
3358 * Send relayd information to consumer as soon as we have a domain and a
3359 * session defined.
3360 */
3361 if (cmd_ctx->session && need_domain) {
3362 /*
3363 * Setup relayd if not done yet. If the relayd information was already
3364 * sent to the consumer, this call will gracefully return.
3365 */
3366 ret = cmd_setup_relayd(cmd_ctx->session);
3367 if (ret != LTTNG_OK) {
3368 goto error;
3369 }
3370 }
3371
54d01ffb
DG
3372 /* Process by command type */
3373 switch (cmd_ctx->lsm->cmd_type) {
3374 case LTTNG_ADD_CONTEXT:
3375 {
2001793c
JG
3376 /*
3377 * An LTTNG_ADD_CONTEXT command might have a supplementary
3378 * payload if the context being added is an application context.
3379 */
3380 if (cmd_ctx->lsm->u.context.ctx.ctx ==
3381 LTTNG_EVENT_CONTEXT_APP_CONTEXT) {
3382 char *provider_name = NULL, *context_name = NULL;
3383 size_t provider_name_len =
3384 cmd_ctx->lsm->u.context.provider_name_len;
3385 size_t context_name_len =
3386 cmd_ctx->lsm->u.context.context_name_len;
3387
3388 if (provider_name_len == 0 || context_name_len == 0) {
3389 /*
3390 * Application provider and context names MUST
3391 * be provided.
3392 */
3393 ret = -LTTNG_ERR_INVALID;
3394 goto error;
3395 }
3396
3397 provider_name = zmalloc(provider_name_len + 1);
3398 if (!provider_name) {
3399 ret = -LTTNG_ERR_NOMEM;
3400 goto error;
3401 }
e0f4d107
JG
3402 cmd_ctx->lsm->u.context.ctx.u.app_ctx.provider_name =
3403 provider_name;
2001793c
JG
3404
3405 context_name = zmalloc(context_name_len + 1);
3406 if (!context_name) {
3407 ret = -LTTNG_ERR_NOMEM;
3408 goto error_add_context;
3409 }
e0f4d107
JG
3410 cmd_ctx->lsm->u.context.ctx.u.app_ctx.ctx_name =
3411 context_name;
2001793c
JG
3412
3413 ret = lttcomm_recv_unix_sock(sock, provider_name,
3414 provider_name_len);
3415 if (ret < 0) {
3416 goto error_add_context;
3417 }
3418
3419 ret = lttcomm_recv_unix_sock(sock, context_name,
3420 context_name_len);
3421 if (ret < 0) {
3422 goto error_add_context;
3423 }
2001793c
JG
3424 }
3425
3426 /*
3427 * cmd_add_context assumes ownership of the provider and context
3428 * names.
3429 */
3430 ret = cmd_add_context(cmd_ctx->session,
3431 cmd_ctx->lsm->domain.type,
54d01ffb 3432 cmd_ctx->lsm->u.context.channel_name,
2001793c
JG
3433 &cmd_ctx->lsm->u.context.ctx,
3434 kernel_poll_pipe[1]);
3435
3436 cmd_ctx->lsm->u.context.ctx.u.app_ctx.provider_name = NULL;
3437 cmd_ctx->lsm->u.context.ctx.u.app_ctx.ctx_name = NULL;
3438error_add_context:
3439 free(cmd_ctx->lsm->u.context.ctx.u.app_ctx.provider_name);
3440 free(cmd_ctx->lsm->u.context.ctx.u.app_ctx.ctx_name);
3441 if (ret < 0) {
3442 goto error;
3443 }
54d01ffb
DG
3444 break;
3445 }
3446 case LTTNG_DISABLE_CHANNEL:
3447 {
3448 ret = cmd_disable_channel(cmd_ctx->session, cmd_ctx->lsm->domain.type,
3449 cmd_ctx->lsm->u.disable.channel_name);
3450 break;
3451 }
3452 case LTTNG_DISABLE_EVENT:
3453 {
47b7e2f9
JG
3454
3455 /*
3456 * FIXME: handle filter; for now we just receive the filter's
3457 * bytecode along with the filter expression which are sent by
3458 * liblttng-ctl and discard them.
3459 *
3460 * This fixes an issue where the client may block while sending
3461 * the filter payload and encounter an error because the session
3462 * daemon closes the socket without ever handling this data.
3463 */
3464 size_t count = cmd_ctx->lsm->u.disable.expression_len +
3465 cmd_ctx->lsm->u.disable.bytecode_len;
3466
3467 if (count) {
3468 char data[LTTNG_FILTER_MAX_LEN];
3469
3470 DBG("Discarding disable event command payload of size %zu", count);
3471 while (count) {
3472 ret = lttcomm_recv_unix_sock(sock, data,
3473 count > sizeof(data) ? sizeof(data) : count);
3474 if (ret < 0) {
3475 goto error;
3476 }
3477
3478 count -= (size_t) ret;
3479 }
3480 }
6e911cad 3481 /* FIXME: passing packed structure to non-packed pointer */
54d01ffb
DG
3482 ret = cmd_disable_event(cmd_ctx->session, cmd_ctx->lsm->domain.type,
3483 cmd_ctx->lsm->u.disable.channel_name,
6e911cad 3484 &cmd_ctx->lsm->u.disable.event);
33a2b854
DG
3485 break;
3486 }
54d01ffb
DG
3487 case LTTNG_ENABLE_CHANNEL:
3488 {
7972aab2 3489 ret = cmd_enable_channel(cmd_ctx->session, &cmd_ctx->lsm->domain,
2f77fc4b 3490 &cmd_ctx->lsm->u.channel.chan, kernel_poll_pipe[1]);
54d01ffb
DG
3491 break;
3492 }
ccf10263
MD
3493 case LTTNG_TRACK_PID:
3494 {
3495 ret = cmd_track_pid(cmd_ctx->session,
3496 cmd_ctx->lsm->domain.type,
3497 cmd_ctx->lsm->u.pid_tracker.pid);
3498 break;
3499 }
3500 case LTTNG_UNTRACK_PID:
3501 {
3502 ret = cmd_untrack_pid(cmd_ctx->session,
3503 cmd_ctx->lsm->domain.type,
3504 cmd_ctx->lsm->u.pid_tracker.pid);
3505 break;
3506 }
54d01ffb
DG
3507 case LTTNG_ENABLE_EVENT:
3508 {
882ef835
JI
3509 struct lttng_event_exclusion *exclusion = NULL;
3510 struct lttng_filter_bytecode *bytecode = NULL;
6b453b5e 3511 char *filter_expression = NULL;
882ef835 3512
67676bd8
DG
3513 /* Handle exclusion events and receive it from the client. */
3514 if (cmd_ctx->lsm->u.enable.exclusion_count > 0) {
3515 size_t count = cmd_ctx->lsm->u.enable.exclusion_count;
3516
3517 exclusion = zmalloc(sizeof(struct lttng_event_exclusion) +
3518 (count * LTTNG_SYMBOL_NAME_LEN));
3519 if (!exclusion) {
3520 ret = LTTNG_ERR_EXCLUSION_NOMEM;
3521 goto error;
882ef835 3522 }
882ef835 3523
67676bd8
DG
3524 DBG("Receiving var len exclusion event list from client ...");
3525 exclusion->count = count;
3526 ret = lttcomm_recv_unix_sock(sock, exclusion->names,
3527 count * LTTNG_SYMBOL_NAME_LEN);
3528 if (ret <= 0) {
3529 DBG("Nothing recv() from client var len data... continuing");
3530 *sock_error = 1;
3531 free(exclusion);
3532 ret = LTTNG_ERR_EXCLUSION_INVAL;
3533 goto error;
3534 }
3535 }
3536
6b453b5e
JG
3537 /* Get filter expression from client. */
3538 if (cmd_ctx->lsm->u.enable.expression_len > 0) {
3539 size_t expression_len =
3540 cmd_ctx->lsm->u.enable.expression_len;
3541
3542 if (expression_len > LTTNG_FILTER_MAX_LEN) {
3543 ret = LTTNG_ERR_FILTER_INVAL;
3544 free(exclusion);
3545 goto error;
3546 }
3547
3548 filter_expression = zmalloc(expression_len);
3549 if (!filter_expression) {
3550 free(exclusion);
3551 ret = LTTNG_ERR_FILTER_NOMEM;
3552 goto error;
3553 }
3554
3555 /* Receive var. len. data */
3556 DBG("Receiving var len filter's expression from client ...");
3557 ret = lttcomm_recv_unix_sock(sock, filter_expression,
3558 expression_len);
3559 if (ret <= 0) {
3560 DBG("Nothing recv() from client car len data... continuing");
3561 *sock_error = 1;
3562 free(filter_expression);
3563 free(exclusion);
3564 ret = LTTNG_ERR_FILTER_INVAL;
3565 goto error;
3566 }
3567 }
3568
67676bd8
DG
3569 /* Handle filter and get bytecode from client. */
3570 if (cmd_ctx->lsm->u.enable.bytecode_len > 0) {
3571 size_t bytecode_len = cmd_ctx->lsm->u.enable.bytecode_len;
3572
3573 if (bytecode_len > LTTNG_FILTER_MAX_LEN) {
3574 ret = LTTNG_ERR_FILTER_INVAL;
49d21f93 3575 free(filter_expression);
67676bd8
DG
3576 free(exclusion);
3577 goto error;
3578 }
3579
3580 bytecode = zmalloc(bytecode_len);
3581 if (!bytecode) {
49d21f93 3582 free(filter_expression);
67676bd8
DG
3583 free(exclusion);
3584 ret = LTTNG_ERR_FILTER_NOMEM;
3585 goto error;
882ef835
JI
3586 }
3587
67676bd8
DG
3588 /* Receive var. len. data */
3589 DBG("Receiving var len filter's bytecode from client ...");
3590 ret = lttcomm_recv_unix_sock(sock, bytecode, bytecode_len);
3591 if (ret <= 0) {
3592 DBG("Nothing recv() from client car len data... continuing");
3593 *sock_error = 1;
49d21f93 3594 free(filter_expression);
67676bd8
DG
3595 free(bytecode);
3596 free(exclusion);
3597 ret = LTTNG_ERR_FILTER_INVAL;
3598 goto error;
3599 }
3600
3601 if ((bytecode->len + sizeof(*bytecode)) != bytecode_len) {
49d21f93 3602 free(filter_expression);
67676bd8
DG
3603 free(bytecode);
3604 free(exclusion);
3605 ret = LTTNG_ERR_FILTER_INVAL;
3606 goto error;
3607 }
882ef835 3608 }
67676bd8
DG
3609
3610 ret = cmd_enable_event(cmd_ctx->session, &cmd_ctx->lsm->domain,
3611 cmd_ctx->lsm->u.enable.channel_name,
6b453b5e
JG
3612 &cmd_ctx->lsm->u.enable.event,
3613 filter_expression, bytecode, exclusion,
67676bd8 3614 kernel_poll_pipe[1]);
54d01ffb
DG
3615 break;
3616 }
052da939 3617 case LTTNG_LIST_TRACEPOINTS:
2ef84c95 3618 {
9f19cc17 3619 struct lttng_event *events;
54d01ffb 3620 ssize_t nb_events;
052da939 3621
0845bbfa 3622 session_lock_list();
54d01ffb 3623 nb_events = cmd_list_tracepoints(cmd_ctx->lsm->domain.type, &events);
0845bbfa 3624 session_unlock_list();
54d01ffb 3625 if (nb_events < 0) {
f73fabfd 3626 /* Return value is a negative lttng_error_code. */
54d01ffb
DG
3627 ret = -nb_events;
3628 goto error;
2ef84c95
DG
3629 }
3630
3631 /*
3632 * Setup lttng message with payload size set to the event list size in
3633 * bytes and then copy list into the llm payload.
3634 */
6e10c9b9
PP
3635 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, events,
3636 sizeof(struct lttng_event) * nb_events);
3637 free(events);
3638
2ef84c95
DG
3639 if (ret < 0) {
3640 goto setup_error;
3641 }
3642
f73fabfd 3643 ret = LTTNG_OK;
2ef84c95
DG
3644 break;
3645 }
f37d259d
MD
3646 case LTTNG_LIST_TRACEPOINT_FIELDS:
3647 {
3648 struct lttng_event_field *fields;
3649 ssize_t nb_fields;
3650
0845bbfa 3651 session_lock_list();
a4b92340
DG
3652 nb_fields = cmd_list_tracepoint_fields(cmd_ctx->lsm->domain.type,
3653 &fields);
0845bbfa 3654 session_unlock_list();
f37d259d 3655 if (nb_fields < 0) {
f73fabfd 3656 /* Return value is a negative lttng_error_code. */
f37d259d
MD
3657 ret = -nb_fields;
3658 goto error;
3659 }
3660
3661 /*
3662 * Setup lttng message with payload size set to the event list size in
3663 * bytes and then copy list into the llm payload.
3664 */
6e10c9b9 3665 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, fields,
a4b92340 3666 sizeof(struct lttng_event_field) * nb_fields);
6e10c9b9
PP
3667 free(fields);
3668
f37d259d 3669 if (ret < 0) {
f37d259d
MD
3670 goto setup_error;
3671 }
3672
f73fabfd 3673 ret = LTTNG_OK;
f37d259d
MD
3674 break;
3675 }
834978fd
DG
3676 case LTTNG_LIST_SYSCALLS:
3677 {
3678 struct lttng_event *events;
3679 ssize_t nb_events;
3680
3681 nb_events = cmd_list_syscalls(&events);
3682 if (nb_events < 0) {
3683 /* Return value is a negative lttng_error_code. */
3684 ret = -nb_events;
3685 goto error;
3686 }
3687
3688 /*
3689 * Setup lttng message with payload size set to the event list size in
3690 * bytes and then copy list into the llm payload.
3691 */
6e10c9b9
PP
3692 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, events,
3693 sizeof(struct lttng_event) * nb_events);
3694 free(events);
3695
834978fd 3696 if (ret < 0) {
834978fd
DG
3697 goto setup_error;
3698 }
3699
834978fd
DG
3700 ret = LTTNG_OK;
3701 break;
3702 }
a5dfbb9d
MD
3703 case LTTNG_LIST_TRACKER_PIDS:
3704 {
3705 int32_t *pids = NULL;
3706 ssize_t nr_pids;
3707
3708 nr_pids = cmd_list_tracker_pids(cmd_ctx->session,
3709 cmd_ctx->lsm->domain.type, &pids);
3710 if (nr_pids < 0) {
3711 /* Return value is a negative lttng_error_code. */
3712 ret = -nr_pids;
3713 goto error;
3714 }
3715
3716 /*
3717 * Setup lttng message with payload size set to the event list size in
3718 * bytes and then copy list into the llm payload.
3719 */
6e10c9b9
PP
3720 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, pids,
3721 sizeof(int32_t) * nr_pids);
3722 free(pids);
3723
a5dfbb9d 3724 if (ret < 0) {
a5dfbb9d
MD
3725 goto setup_error;
3726 }
3727
a5dfbb9d
MD
3728 ret = LTTNG_OK;
3729 break;
3730 }
00e2e675
DG
3731 case LTTNG_SET_CONSUMER_URI:
3732 {
a4b92340
DG
3733 size_t nb_uri, len;
3734 struct lttng_uri *uris;
3735
3736 nb_uri = cmd_ctx->lsm->u.uri.size;
3737 len = nb_uri * sizeof(struct lttng_uri);
3738
3739 if (nb_uri == 0) {
f73fabfd 3740 ret = LTTNG_ERR_INVALID;
a4b92340
DG
3741 goto error;
3742 }
3743
3744 uris = zmalloc(len);
3745 if (uris == NULL) {
f73fabfd 3746 ret = LTTNG_ERR_FATAL;
a4b92340
DG
3747 goto error;
3748 }
3749
3750 /* Receive variable len data */
77c7c900 3751 DBG("Receiving %zu URI(s) from client ...", nb_uri);
a4b92340
DG
3752 ret = lttcomm_recv_unix_sock(sock, uris, len);
3753 if (ret <= 0) {
3754 DBG("No URIs received from client... continuing");
3755 *sock_error = 1;
f73fabfd 3756 ret = LTTNG_ERR_SESSION_FAIL;
b1d41407 3757 free(uris);
a4b92340
DG
3758 goto error;
3759 }
3760
bda32d56
JG
3761 ret = cmd_set_consumer_uri(cmd_ctx->session, nb_uri, uris);
3762 free(uris);
f73fabfd 3763 if (ret != LTTNG_OK) {
a4b92340
DG
3764 goto error;
3765 }
3766
b1d41407 3767
00e2e675
DG
3768 break;
3769 }
f3ed775e 3770 case LTTNG_START_TRACE:
8c0faa1d 3771 {
54d01ffb 3772 ret = cmd_start_trace(cmd_ctx->session);
8c0faa1d
DG
3773 break;
3774 }
f3ed775e 3775 case LTTNG_STOP_TRACE:
8c0faa1d 3776 {
54d01ffb 3777 ret = cmd_stop_trace(cmd_ctx->session);
8c0faa1d
DG
3778 break;
3779 }
5e16da05
MD
3780 case LTTNG_CREATE_SESSION:
3781 {
a4b92340
DG
3782 size_t nb_uri, len;
3783 struct lttng_uri *uris = NULL;
3784
3785 nb_uri = cmd_ctx->lsm->u.uri.size;
3786 len = nb_uri * sizeof(struct lttng_uri);
3787
3788 if (nb_uri > 0) {
3789 uris = zmalloc(len);
3790 if (uris == NULL) {
f73fabfd 3791 ret = LTTNG_ERR_FATAL;
a4b92340
DG
3792 goto error;
3793 }
3794
3795 /* Receive variable len data */
77c7c900 3796 DBG("Waiting for %zu URIs from client ...", nb_uri);
a4b92340
DG
3797 ret = lttcomm_recv_unix_sock(sock, uris, len);
3798 if (ret <= 0) {
3799 DBG("No URIs received from client... continuing");
3800 *sock_error = 1;
f73fabfd 3801 ret = LTTNG_ERR_SESSION_FAIL;
b1d41407 3802 free(uris);
a4b92340
DG
3803 goto error;
3804 }
3805
3806 if (nb_uri == 1 && uris[0].dtype != LTTNG_DST_PATH) {
3807 DBG("Creating session with ONE network URI is a bad call");
f73fabfd 3808 ret = LTTNG_ERR_SESSION_FAIL;
b1d41407 3809 free(uris);
a4b92340
DG
3810 goto error;
3811 }
3812 }
3813
3814 ret = cmd_create_session_uri(cmd_ctx->lsm->session.name, uris, nb_uri,
ecc48a90 3815 &cmd_ctx->creds, 0);
a4b92340 3816
b1d41407
DG
3817 free(uris);
3818
00e2e675
DG
3819 break;
3820 }
5e16da05
MD
3821 case LTTNG_DESTROY_SESSION:
3822 {
2f77fc4b 3823 ret = cmd_destroy_session(cmd_ctx->session, kernel_poll_pipe[1]);
a4b92340
DG
3824
3825 /* Set session to NULL so we do not unlock it after free. */
256a5576 3826 cmd_ctx->session = NULL;
5461b305 3827 break;
5e16da05 3828 }
9f19cc17 3829 case LTTNG_LIST_DOMAINS:
5e16da05 3830 {
54d01ffb 3831 ssize_t nb_dom;
fa64dfb4 3832 struct lttng_domain *domains = NULL;
5461b305 3833
54d01ffb
DG
3834 nb_dom = cmd_list_domains(cmd_ctx->session, &domains);
3835 if (nb_dom < 0) {
f73fabfd 3836 /* Return value is a negative lttng_error_code. */
54d01ffb
DG
3837 ret = -nb_dom;
3838 goto error;
ce3d728c 3839 }
520ff687 3840
6e10c9b9
PP
3841 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, domains,
3842 nb_dom * sizeof(struct lttng_domain));
3843 free(domains);
3844
5461b305
DG
3845 if (ret < 0) {
3846 goto setup_error;
520ff687 3847 }
57167058 3848
f73fabfd 3849 ret = LTTNG_OK;
5e16da05
MD
3850 break;
3851 }
9f19cc17 3852 case LTTNG_LIST_CHANNELS:
5e16da05 3853 {
53e367f9 3854 ssize_t payload_size;
ade7ce52 3855 struct lttng_channel *channels = NULL;
54d01ffb 3856
53e367f9 3857 payload_size = cmd_list_channels(cmd_ctx->lsm->domain.type,
b551a063 3858 cmd_ctx->session, &channels);
53e367f9 3859 if (payload_size < 0) {
f73fabfd 3860 /* Return value is a negative lttng_error_code. */
53e367f9 3861 ret = -payload_size;
54d01ffb 3862 goto error;
5461b305 3863 }
ca95a216 3864
6e10c9b9 3865 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, channels,
53e367f9 3866 payload_size);
6e10c9b9
PP
3867 free(channels);
3868
5461b305
DG
3869 if (ret < 0) {
3870 goto setup_error;
3871 }
9f19cc17 3872
f73fabfd 3873 ret = LTTNG_OK;
5461b305 3874 break;
5e16da05 3875 }
9f19cc17 3876 case LTTNG_LIST_EVENTS:
5e16da05 3877 {
b551a063 3878 ssize_t nb_event;
684d34d2 3879 struct lttng_event *events = NULL;
b4e3ceb9
PP
3880 struct lttcomm_event_command_header cmd_header;
3881 size_t total_size;
3882
b2d66287 3883 memset(&cmd_header, 0, sizeof(cmd_header));
b4e3ceb9
PP
3884 /* Extended infos are included at the end of events */
3885 nb_event = cmd_list_events(cmd_ctx->lsm->domain.type,
3886 cmd_ctx->session, cmd_ctx->lsm->u.list.channel_name,
3887 &events, &total_size);
9f19cc17 3888
54d01ffb 3889 if (nb_event < 0) {
f73fabfd 3890 /* Return value is a negative lttng_error_code. */
54d01ffb
DG
3891 ret = -nb_event;
3892 goto error;
5461b305 3893 }
ca95a216 3894
b4e3ceb9
PP
3895 cmd_header.nb_events = nb_event;
3896 ret = setup_lttng_msg(cmd_ctx, events, total_size,
3897 &cmd_header, sizeof(cmd_header));
6e10c9b9
PP
3898 free(events);
3899
5461b305
DG
3900 if (ret < 0) {
3901 goto setup_error;
3902 }
9f19cc17 3903
f73fabfd 3904 ret = LTTNG_OK;
5461b305 3905 break;
5e16da05
MD
3906 }
3907 case LTTNG_LIST_SESSIONS:
3908 {
8e0af1b4 3909 unsigned int nr_sessions;
6e10c9b9
PP
3910 void *sessions_payload;
3911 size_t payload_len;
5461b305 3912
8e0af1b4 3913 session_lock_list();
730389d9
DG
3914 nr_sessions = lttng_sessions_count(
3915 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
3916 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds));
6e10c9b9
PP
3917 payload_len = sizeof(struct lttng_session) * nr_sessions;
3918 sessions_payload = zmalloc(payload_len);
d32fb093 3919
6e10c9b9 3920 if (!sessions_payload) {
54d01ffb 3921 session_unlock_list();
6e10c9b9 3922 ret = -ENOMEM;
5461b305 3923 goto setup_error;
e065084a 3924 }
5e16da05 3925
6e10c9b9 3926 cmd_list_lttng_sessions(sessions_payload,
730389d9
DG
3927 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
3928 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds));
54d01ffb 3929 session_unlock_list();
5e16da05 3930
6e10c9b9
PP
3931 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, sessions_payload,
3932 payload_len);
3933 free(sessions_payload);
3934
3935 if (ret < 0) {
3936 goto setup_error;
3937 }
3938
f73fabfd 3939 ret = LTTNG_OK;
5e16da05
MD
3940 break;
3941 }
d9800920
DG
3942 case LTTNG_REGISTER_CONSUMER:
3943 {
2f77fc4b
DG
3944 struct consumer_data *cdata;
3945
3946 switch (cmd_ctx->lsm->domain.type) {
3947 case LTTNG_DOMAIN_KERNEL:
3948 cdata = &kconsumer_data;
3949 break;
3950 default:
f73fabfd 3951 ret = LTTNG_ERR_UND;
2f77fc4b
DG
3952 goto error;
3953 }
3954
54d01ffb 3955 ret = cmd_register_consumer(cmd_ctx->session, cmd_ctx->lsm->domain.type,
2f77fc4b 3956 cmd_ctx->lsm->u.reg.path, cdata);
d9800920
DG
3957 break;
3958 }
6d805429 3959 case LTTNG_DATA_PENDING:
806e2684 3960 {
f6151c55 3961 int pending_ret;
6e10c9b9 3962 uint8_t pending_ret_byte;
f6151c55
JG
3963
3964 pending_ret = cmd_data_pending(cmd_ctx->session);
6e10c9b9 3965
f6151c55
JG
3966 /*
3967 * FIXME
3968 *
3969 * This function may returns 0 or 1 to indicate whether or not
3970 * there is data pending. In case of error, it should return an
3971 * LTTNG_ERR code. However, some code paths may still return
3972 * a nondescript error code, which we handle by returning an
3973 * "unknown" error.
3974 */
3975 if (pending_ret == 0 || pending_ret == 1) {
6e10c9b9
PP
3976 /*
3977 * ret will be set to LTTNG_OK at the end of
3978 * this function.
3979 */
f6151c55
JG
3980 } else if (pending_ret < 0) {
3981 ret = LTTNG_ERR_UNK;
3982 goto setup_error;
3983 } else {
3984 ret = pending_ret;
3985 goto setup_error;
3986 }
3987
6e10c9b9
PP
3988 pending_ret_byte = (uint8_t) pending_ret;
3989
3990 /* 1 byte to return whether or not data is pending */
3991 ret = setup_lttng_msg_no_cmd_header(cmd_ctx,
3992 &pending_ret_byte, 1);
3993
3994 if (ret < 0) {
3995 goto setup_error;
3996 }
3997
3998 ret = LTTNG_OK;
806e2684
DG
3999 break;
4000 }
da3c9ec1
DG
4001 case LTTNG_SNAPSHOT_ADD_OUTPUT:
4002 {
6dc3064a
DG
4003 struct lttcomm_lttng_output_id reply;
4004
4005 ret = cmd_snapshot_add_output(cmd_ctx->session,
4006 &cmd_ctx->lsm->u.snapshot_output.output, &reply.id);
4007 if (ret != LTTNG_OK) {
4008 goto error;
4009 }
4010
6e10c9b9
PP
4011 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, &reply,
4012 sizeof(reply));
6dc3064a
DG
4013 if (ret < 0) {
4014 goto setup_error;
4015 }
4016
4017 /* Copy output list into message payload */
6dc3064a 4018 ret = LTTNG_OK;
da3c9ec1
DG
4019 break;
4020 }
4021 case LTTNG_SNAPSHOT_DEL_OUTPUT:
4022 {
6dc3064a
DG
4023 ret = cmd_snapshot_del_output(cmd_ctx->session,
4024 &cmd_ctx->lsm->u.snapshot_output.output);
da3c9ec1
DG
4025 break;
4026 }
4027 case LTTNG_SNAPSHOT_LIST_OUTPUT:
4028 {
6dc3064a
DG
4029 ssize_t nb_output;
4030 struct lttng_snapshot_output *outputs = NULL;
4031
4032 nb_output = cmd_snapshot_list_outputs(cmd_ctx->session, &outputs);
4033 if (nb_output < 0) {
4034 ret = -nb_output;
4035 goto error;
4036 }
4037
6e10c9b9
PP
4038 assert((nb_output > 0 && outputs) || nb_output == 0);
4039 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, outputs,
6dc3064a 4040 nb_output * sizeof(struct lttng_snapshot_output));
6e10c9b9
PP
4041 free(outputs);
4042
6dc3064a 4043 if (ret < 0) {
6dc3064a
DG
4044 goto setup_error;
4045 }
4046
6dc3064a 4047 ret = LTTNG_OK;
da3c9ec1
DG
4048 break;
4049 }
4050 case LTTNG_SNAPSHOT_RECORD:
4051 {
6dc3064a
DG
4052 ret = cmd_snapshot_record(cmd_ctx->session,
4053 &cmd_ctx->lsm->u.snapshot_record.output,
4054 cmd_ctx->lsm->u.snapshot_record.wait);
da3c9ec1
DG
4055 break;
4056 }
27babd3a
DG
4057 case LTTNG_CREATE_SESSION_SNAPSHOT:
4058 {
4059 size_t nb_uri, len;
4060 struct lttng_uri *uris = NULL;
4061
4062 nb_uri = cmd_ctx->lsm->u.uri.size;
4063 len = nb_uri * sizeof(struct lttng_uri);
4064
4065 if (nb_uri > 0) {
4066 uris = zmalloc(len);
4067 if (uris == NULL) {
4068 ret = LTTNG_ERR_FATAL;
4069 goto error;
4070 }
4071
4072 /* Receive variable len data */
4073 DBG("Waiting for %zu URIs from client ...", nb_uri);
4074 ret = lttcomm_recv_unix_sock(sock, uris, len);
4075 if (ret <= 0) {
4076 DBG("No URIs received from client... continuing");
4077 *sock_error = 1;
4078 ret = LTTNG_ERR_SESSION_FAIL;
4079 free(uris);
4080 goto error;
4081 }
4082
4083 if (nb_uri == 1 && uris[0].dtype != LTTNG_DST_PATH) {
4084 DBG("Creating session with ONE network URI is a bad call");
4085 ret = LTTNG_ERR_SESSION_FAIL;
4086 free(uris);
4087 goto error;
4088 }
4089 }
4090
4091 ret = cmd_create_session_snapshot(cmd_ctx->lsm->session.name, uris,
4092 nb_uri, &cmd_ctx->creds);
4093 free(uris);
4094 break;
4095 }
ecc48a90
JD
4096 case LTTNG_CREATE_SESSION_LIVE:
4097 {
4098 size_t nb_uri, len;
4099 struct lttng_uri *uris = NULL;
4100
4101 nb_uri = cmd_ctx->lsm->u.uri.size;
4102 len = nb_uri * sizeof(struct lttng_uri);
4103
4104 if (nb_uri > 0) {
4105 uris = zmalloc(len);
4106 if (uris == NULL) {
4107 ret = LTTNG_ERR_FATAL;
4108 goto error;
4109 }
4110
4111 /* Receive variable len data */
4112 DBG("Waiting for %zu URIs from client ...", nb_uri);
4113 ret = lttcomm_recv_unix_sock(sock, uris, len);
4114 if (ret <= 0) {
4115 DBG("No URIs received from client... continuing");
4116 *sock_error = 1;
4117 ret = LTTNG_ERR_SESSION_FAIL;
4118 free(uris);
4119 goto error;
4120 }
4121
4122 if (nb_uri == 1 && uris[0].dtype != LTTNG_DST_PATH) {
4123 DBG("Creating session with ONE network URI is a bad call");
4124 ret = LTTNG_ERR_SESSION_FAIL;
4125 free(uris);
4126 goto error;
4127 }
4128 }
4129
4130 ret = cmd_create_session_uri(cmd_ctx->lsm->session.name, uris,
4131 nb_uri, &cmd_ctx->creds, cmd_ctx->lsm->u.session_live.timer_interval);
4132 free(uris);
4133 break;
4134 }
fb198a11
JG
4135 case LTTNG_SAVE_SESSION:
4136 {
4137 ret = cmd_save_sessions(&cmd_ctx->lsm->u.save_session.attr,
4138 &cmd_ctx->creds);
4139 break;
4140 }
d7ba1388
MD
4141 case LTTNG_SET_SESSION_SHM_PATH:
4142 {
4143 ret = cmd_set_session_shm_path(cmd_ctx->session,
4144 cmd_ctx->lsm->u.set_shm_path.shm_path);
4145 break;
4146 }
eded6438 4147 case LTTNG_REGENERATE_METADATA:
93ec662e 4148 {
eded6438 4149 ret = cmd_regenerate_metadata(cmd_ctx->session);
93ec662e
JD
4150 break;
4151 }
c2561365
JD
4152 case LTTNG_REGENERATE_STATEDUMP:
4153 {
4154 ret = cmd_regenerate_statedump(cmd_ctx->session);
4155 break;
4156 }
5e16da05 4157 default:
f73fabfd 4158 ret = LTTNG_ERR_UND;
5461b305 4159 break;
fac6795d
DG
4160 }
4161
5461b305 4162error:
5461b305
DG
4163 if (cmd_ctx->llm == NULL) {
4164 DBG("Missing llm structure. Allocating one.");
6e10c9b9 4165 if (setup_lttng_msg_no_cmd_header(cmd_ctx, NULL, 0) < 0) {
5461b305
DG
4166 goto setup_error;
4167 }
4168 }
54d01ffb 4169 /* Set return code */
5461b305 4170 cmd_ctx->llm->ret_code = ret;
5461b305 4171setup_error:
b5541356 4172 if (cmd_ctx->session) {
54d01ffb 4173 session_unlock(cmd_ctx->session);
b5541356 4174 }
256a5576
MD
4175 if (need_tracing_session) {
4176 session_unlock_list();
4177 }
54d01ffb 4178init_setup_error:
3745d315 4179 assert(!rcu_read_ongoing());
8028d920 4180 return ret;
fac6795d
DG
4181}
4182
44a5e5eb
DG
4183/*
4184 * Thread managing health check socket.
4185 */
4186static void *thread_manage_health(void *data)
4187{
eb4a2943 4188 int sock = -1, new_sock = -1, ret, i, pollfd, err = -1;
44a5e5eb
DG
4189 uint32_t revents, nb_fd;
4190 struct lttng_poll_event events;
0c89d795
MD
4191 struct health_comm_msg msg;
4192 struct health_comm_reply reply;
44a5e5eb
DG
4193
4194 DBG("[thread] Manage health check started");
4195
4196 rcu_register_thread();
4197
6d737ce4
DG
4198 /* We might hit an error path before this is created. */
4199 lttng_poll_init(&events);
3cc04881 4200
44a5e5eb
DG
4201 /* Create unix socket */
4202 sock = lttcomm_create_unix_sock(health_unix_sock_path);
4203 if (sock < 0) {
4204 ERR("Unable to create health check Unix socket");
44a5e5eb
DG
4205 goto error;
4206 }
4207
6c71277b
MD
4208 if (is_root) {
4209 /* lttng health client socket path permissions */
4210 ret = chown(health_unix_sock_path, 0,
4211 utils_get_group_id(tracing_group_name));
4212 if (ret < 0) {
4213 ERR("Unable to set group on %s", health_unix_sock_path);
4214 PERROR("chown");
6c71277b
MD
4215 goto error;
4216 }
4217
4218 ret = chmod(health_unix_sock_path,
4219 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
4220 if (ret < 0) {
4221 ERR("Unable to set permissions on %s", health_unix_sock_path);
4222 PERROR("chmod");
6c71277b
MD
4223 goto error;
4224 }
4225 }
4226
b662582b
DG
4227 /*
4228 * Set the CLOEXEC flag. Return code is useless because either way, the
4229 * show must go on.
4230 */
4231 (void) utils_set_fd_cloexec(sock);
4232
44a5e5eb
DG
4233 ret = lttcomm_listen_unix_sock(sock);
4234 if (ret < 0) {
4235 goto error;
4236 }
4237
4238 /*
4239 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4240 * more will be added to this poll set.
4241 */
d0b96690 4242 ret = sessiond_set_thread_pollset(&events, 2);
44a5e5eb
DG
4243 if (ret < 0) {
4244 goto error;
4245 }
4246
4247 /* Add the application registration socket */
4248 ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLPRI);
4249 if (ret < 0) {
4250 goto error;
4251 }
4252
ef367a93 4253 sessiond_notify_ready();
97bc1426 4254
44a5e5eb
DG
4255 while (1) {
4256 DBG("Health check ready");
4257
44a5e5eb
DG
4258 /* Inifinite blocking call, waiting for transmission */
4259restart:
4260 ret = lttng_poll_wait(&events, -1);
4261 if (ret < 0) {
4262 /*
4263 * Restart interrupted system call.
4264 */
4265 if (errno == EINTR) {
4266 goto restart;
4267 }
4268 goto error;
4269 }
4270
0d9c5d77
DG
4271 nb_fd = ret;
4272
44a5e5eb
DG
4273 for (i = 0; i < nb_fd; i++) {
4274 /* Fetch once the poll data */
4275 revents = LTTNG_POLL_GETEV(&events, i);
4276 pollfd = LTTNG_POLL_GETFD(&events, i);
4277
fd20dac9
MD
4278 if (!revents) {
4279 /* No activity for this FD (poll implementation). */
4280 continue;
4281 }
4282
44a5e5eb 4283 /* Thread quit pipe has been closed. Killing thread. */
d0b96690 4284 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
44a5e5eb 4285 if (ret) {
139ac872
MD
4286 err = 0;
4287 goto exit;
44a5e5eb
DG
4288 }
4289
4290 /* Event on the registration socket */
4291 if (pollfd == sock) {
03e43155
MD
4292 if (revents & LPOLLIN) {
4293 continue;
4294 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
44a5e5eb
DG
4295 ERR("Health socket poll error");
4296 goto error;
03e43155
MD
4297 } else {
4298 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
4299 goto error;
44a5e5eb
DG
4300 }
4301 }
4302 }
4303
4304 new_sock = lttcomm_accept_unix_sock(sock);
4305 if (new_sock < 0) {
4306 goto error;
4307 }
4308
b662582b
DG
4309 /*
4310 * Set the CLOEXEC flag. Return code is useless because either way, the
4311 * show must go on.
4312 */
4313 (void) utils_set_fd_cloexec(new_sock);
4314
44a5e5eb
DG
4315 DBG("Receiving data from client for health...");
4316 ret = lttcomm_recv_unix_sock(new_sock, (void *)&msg, sizeof(msg));
4317 if (ret <= 0) {
4318 DBG("Nothing recv() from client... continuing");
4319 ret = close(new_sock);
4320 if (ret) {
4321 PERROR("close");
4322 }
44a5e5eb
DG
4323 continue;
4324 }
4325
4326 rcu_thread_online();
4327
53efb85a 4328 memset(&reply, 0, sizeof(reply));
6c71277b
MD
4329 for (i = 0; i < NR_HEALTH_SESSIOND_TYPES; i++) {
4330 /*
4331 * health_check_state returns 0 if health is
4332 * bad.
4333 */
4334 if (!health_check_state(health_sessiond, i)) {
4335 reply.ret_code |= 1ULL << i;
4336 }
44a5e5eb
DG
4337 }
4338
6c71277b 4339 DBG2("Health check return value %" PRIx64, reply.ret_code);
44a5e5eb
DG
4340
4341 ret = send_unix_sock(new_sock, (void *) &reply, sizeof(reply));
4342 if (ret < 0) {
4343 ERR("Failed to send health data back to client");
4344 }
4345
4346 /* End of transmission */
4347 ret = close(new_sock);
4348 if (ret) {
4349 PERROR("close");
4350 }
44a5e5eb
DG
4351 }
4352
139ac872 4353exit:
44a5e5eb 4354error:
139ac872
MD
4355 if (err) {
4356 ERR("Health error occurred in %s", __func__);
4357 }
44a5e5eb
DG
4358 DBG("Health check thread dying");
4359 unlink(health_unix_sock_path);
4360 if (sock >= 0) {
4361 ret = close(sock);
4362 if (ret) {
4363 PERROR("close");
4364 }
4365 }
44a5e5eb
DG
4366
4367 lttng_poll_clean(&events);
28538a70 4368 stop_threads();
44a5e5eb
DG
4369 rcu_unregister_thread();
4370 return NULL;
4371}
4372
1d4b027a 4373/*
d063d709
DG
4374 * This thread manage all clients request using the unix client socket for
4375 * communication.
1d4b027a
DG
4376 */
4377static void *thread_manage_clients(void *data)
4378{
139ac872 4379 int sock = -1, ret, i, pollfd, err = -1;
53a80697 4380 int sock_error;
5eb91c98 4381 uint32_t revents, nb_fd;
273ea72c 4382 struct command_ctx *cmd_ctx = NULL;
5eb91c98 4383 struct lttng_poll_event events;
1d4b027a
DG
4384
4385 DBG("[thread] Manage client started");
4386
f6a9efaa
DG
4387 rcu_register_thread();
4388
6c71277b 4389 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_CMD);
927ca06a 4390
840cb59c 4391 health_code_update();
44a5e5eb 4392
1d4b027a
DG
4393 ret = lttcomm_listen_unix_sock(client_sock);
4394 if (ret < 0) {
35df2755 4395 goto error_listen;
1d4b027a
DG
4396 }
4397
5eb91c98
DG
4398 /*
4399 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4400 * more will be added to this poll set.
4401 */
d0b96690 4402 ret = sessiond_set_thread_pollset(&events, 2);
5eb91c98 4403 if (ret < 0) {
35df2755 4404 goto error_create_poll;
5eb91c98 4405 }
273ea72c 4406
5eb91c98
DG
4407 /* Add the application registration socket */
4408 ret = lttng_poll_add(&events, client_sock, LPOLLIN | LPOLLPRI);
4409 if (ret < 0) {
4410 goto error;
4411 }
273ea72c 4412
ef367a93
JG
4413 ret = sem_post(&load_info->message_thread_ready);
4414 if (ret) {
4415 PERROR("sem_post message_thread_ready");
4416 goto error;
4417 }
0bb7724a 4418
09a88287
JG
4419 /*
4420 * Wait until all support threads are initialized before accepting
4421 * commands.
4422 */
4423 while (uatomic_read(&lttng_sessiond_ready) != 0) {
4424 fd_set read_fds;
4425 struct timeval timeout;
4426
4427 FD_ZERO(&read_fds);
4428 FD_SET(thread_quit_pipe[0], &read_fds);
4429 memset(&timeout, 0, sizeof(timeout));
4430 timeout.tv_usec = 1000;
4431
4432 /*
4433 * If a support thread failed to launch, it may signal that
4434 * we must exit and the sessiond would never be marked as
4435 * "ready".
4436 *
4437 * The timeout is set to 1ms, which serves as a way to
4438 * pace down this check.
4439 */
4440 ret = select(thread_quit_pipe[0] + 1, &read_fds, NULL, NULL,
4441 &timeout);
4442 if (ret > 0 || (ret < 0 && errno != EINTR)) {
4443 goto exit;
4444 }
4445 }
70a71a41
JG
4446 /*
4447 * This barrier is paired with the one in sessiond_notify_ready() to
4448 * ensure that loads accessing data initialized by the other threads,
4449 * on which this thread was waiting, are not performed before this point.
4450 *
4451 * Note that this could be a 'read' memory barrier, but a full barrier
4452 * is used in case the code changes. The performance implications of
4453 * this choice are minimal since this is a slow path.
4454 */
4455 cmm_smp_mb();
09a88287 4456
72453c8c
MD
4457 /* This testpoint is after we signal readiness to the parent. */
4458 if (testpoint(sessiond_thread_manage_clients)) {
4459 goto error;
4460 }
4461
e547b070 4462 if (testpoint(sessiond_thread_manage_clients_before_loop)) {
6993eeb3
CB
4463 goto error;
4464 }
8ac94142 4465
840cb59c 4466 health_code_update();
44a5e5eb 4467
1d4b027a 4468 while (1) {
1d4b027a 4469 DBG("Accepting client command ...");
273ea72c
DG
4470
4471 /* Inifinite blocking call, waiting for transmission */
88f2b785 4472 restart:
a78af745 4473 health_poll_entry();
5eb91c98 4474 ret = lttng_poll_wait(&events, -1);
a78af745 4475 health_poll_exit();
273ea72c 4476 if (ret < 0) {
88f2b785
MD
4477 /*
4478 * Restart interrupted system call.
4479 */
4480 if (errno == EINTR) {
4481 goto restart;
4482 }
273ea72c
DG
4483 goto error;
4484 }
4485
0d9c5d77
DG
4486 nb_fd = ret;
4487
5eb91c98
DG
4488 for (i = 0; i < nb_fd; i++) {
4489 /* Fetch once the poll data */
4490 revents = LTTNG_POLL_GETEV(&events, i);
4491 pollfd = LTTNG_POLL_GETFD(&events, i);
4492
840cb59c 4493 health_code_update();
44a5e5eb 4494
fd20dac9
MD
4495 if (!revents) {
4496 /* No activity for this FD (poll implementation). */
4497 continue;
4498 }
4499
5eb91c98 4500 /* Thread quit pipe has been closed. Killing thread. */
d0b96690 4501 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
5eb91c98 4502 if (ret) {
139ac872
MD
4503 err = 0;
4504 goto exit;
5eb91c98
DG
4505 }
4506
4507 /* Event on the registration socket */
4508 if (pollfd == client_sock) {
03e43155
MD
4509 if (revents & LPOLLIN) {
4510 continue;
4511 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
5eb91c98
DG
4512 ERR("Client socket poll error");
4513 goto error;
03e43155
MD
4514 } else {
4515 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
4516 goto error;
5eb91c98
DG
4517 }
4518 }
4519 }
4520
4521 DBG("Wait for client response");
4522
840cb59c 4523 health_code_update();
44a5e5eb 4524
5eb91c98
DG
4525 sock = lttcomm_accept_unix_sock(client_sock);
4526 if (sock < 0) {
273ea72c 4527 goto error;
273ea72c
DG
4528 }
4529
b662582b
DG
4530 /*
4531 * Set the CLOEXEC flag. Return code is useless because either way, the
4532 * show must go on.
4533 */
4534 (void) utils_set_fd_cloexec(sock);
4535
be040666
DG
4536 /* Set socket option for credentials retrieval */
4537 ret = lttcomm_setsockopt_creds_unix_sock(sock);
4538 if (ret < 0) {
4539 goto error;
4540 }
4541
5eb91c98 4542 /* Allocate context command to process the client request */
ba7f0ae5 4543 cmd_ctx = zmalloc(sizeof(struct command_ctx));
5eb91c98 4544 if (cmd_ctx == NULL) {
76d7553f 4545 PERROR("zmalloc cmd_ctx");
1d4b027a 4546 goto error;
5eb91c98 4547 }
1d4b027a 4548
5eb91c98 4549 /* Allocate data buffer for reception */
ba7f0ae5 4550 cmd_ctx->lsm = zmalloc(sizeof(struct lttcomm_session_msg));
5eb91c98 4551 if (cmd_ctx->lsm == NULL) {
76d7553f 4552 PERROR("zmalloc cmd_ctx->lsm");
5eb91c98
DG
4553 goto error;
4554 }
1d4b027a 4555
5eb91c98
DG
4556 cmd_ctx->llm = NULL;
4557 cmd_ctx->session = NULL;
1d4b027a 4558
840cb59c 4559 health_code_update();
44a5e5eb 4560
5eb91c98
DG
4561 /*
4562 * Data is received from the lttng client. The struct
4563 * lttcomm_session_msg (lsm) contains the command and data request of
4564 * the client.
4565 */
4566 DBG("Receiving data from client ...");
be040666
DG
4567 ret = lttcomm_recv_creds_unix_sock(sock, cmd_ctx->lsm,
4568 sizeof(struct lttcomm_session_msg), &cmd_ctx->creds);
5eb91c98
DG
4569 if (ret <= 0) {
4570 DBG("Nothing recv() from client... continuing");
76d7553f
MD
4571 ret = close(sock);
4572 if (ret) {
4573 PERROR("close");
4574 }
4575 sock = -1;
a2c0da86 4576 clean_command_ctx(&cmd_ctx);
5eb91c98
DG
4577 continue;
4578 }
1d4b027a 4579
840cb59c 4580 health_code_update();
44a5e5eb 4581
5eb91c98
DG
4582 // TODO: Validate cmd_ctx including sanity check for
4583 // security purpose.
f7776ea7 4584
f6a9efaa 4585 rcu_thread_online();
5eb91c98
DG
4586 /*
4587 * This function dispatch the work to the kernel or userspace tracer
4588 * libs and fill the lttcomm_lttng_msg data structure of all the needed
4589 * informations for the client. The command context struct contains
4590 * everything this function may needs.
4591 */
53a80697 4592 ret = process_client_msg(cmd_ctx, sock, &sock_error);
f6a9efaa 4593 rcu_thread_offline();
5eb91c98 4594 if (ret < 0) {
36134aa1
DG
4595 ret = close(sock);
4596 if (ret) {
4597 PERROR("close");
53a80697 4598 }
36134aa1 4599 sock = -1;
bbd973c2 4600 /*
5eb91c98 4601 * TODO: Inform client somehow of the fatal error. At
ba7f0ae5 4602 * this point, ret < 0 means that a zmalloc failed
53a80697
MD
4603 * (ENOMEM). Error detected but still accept
4604 * command, unless a socket error has been
4605 * detected.
bbd973c2 4606 */
bbd973c2 4607 clean_command_ctx(&cmd_ctx);
5eb91c98
DG
4608 continue;
4609 }
d6e4fca4 4610
840cb59c 4611 health_code_update();
44a5e5eb 4612
a1ac8b97 4613 DBG("Sending response (size: %d, retcode: %s (%d))",
54d01ffb 4614 cmd_ctx->lttng_msg_size,
a1ac8b97
JG
4615 lttng_strerror(-cmd_ctx->llm->ret_code),
4616 cmd_ctx->llm->ret_code);
54d01ffb 4617 ret = send_unix_sock(sock, cmd_ctx->llm, cmd_ctx->lttng_msg_size);
5eb91c98
DG
4618 if (ret < 0) {
4619 ERR("Failed to send data back to client");
bbd973c2 4620 }
1d4b027a 4621
5eb91c98 4622 /* End of transmission */
76d7553f
MD
4623 ret = close(sock);
4624 if (ret) {
4625 PERROR("close");
4626 }
4627 sock = -1;
be040666
DG
4628
4629 clean_command_ctx(&cmd_ctx);
44a5e5eb 4630
840cb59c 4631 health_code_update();
273ea72c
DG
4632 }
4633
139ac872 4634exit:
5eb91c98 4635error:
35df2755
CB
4636 if (sock >= 0) {
4637 ret = close(sock);
4638 if (ret) {
4639 PERROR("close");
4640 }
139ac872 4641 }
44a5e5eb 4642
35df2755
CB
4643 lttng_poll_clean(&events);
4644 clean_command_ctx(&cmd_ctx);
4645
4646error_listen:
4647error_create_poll:
273ea72c 4648 unlink(client_unix_sock_path);
76d7553f
MD
4649 if (client_sock >= 0) {
4650 ret = close(client_sock);
4651 if (ret) {
4652 PERROR("close");
4653 }
a4b35e07 4654 }
35df2755
CB
4655
4656 if (err) {
840cb59c 4657 health_error();
35df2755 4658 ERR("Health error occurred in %s", __func__);
a4b35e07 4659 }
273ea72c 4660
8782cc74 4661 health_unregister(health_sessiond);
35df2755
CB
4662
4663 DBG("Client thread dying");
f6a9efaa
DG
4664
4665 rcu_unregister_thread();
4a15001e
MD
4666
4667 /*
4668 * Since we are creating the consumer threads, we own them, so we need
4669 * to join them before our thread exits.
4670 */
4671 ret = join_consumer_thread(&kconsumer_data);
4672 if (ret) {
4673 errno = ret;
4674 PERROR("join_consumer");
4675 }
4676
4677 ret = join_consumer_thread(&ustconsumer32_data);
4678 if (ret) {
4679 errno = ret;
4680 PERROR("join_consumer ust32");
4681 }
4682
4683 ret = join_consumer_thread(&ustconsumer64_data);
4684 if (ret) {
4685 errno = ret;
4686 PERROR("join_consumer ust64");
4687 }
1d4b027a
DG
4688 return NULL;
4689}
4690
c0232ea5
JG
4691static int string_match(const char *str1, const char *str2)
4692{
4693 return (str1 && str2) && !strcmp(str1, str2);
4694}
4695
fac6795d 4696/*
26296c48
JG
4697 * Take an option from the getopt output and set it in the right variable to be
4698 * used later.
4699 *
4700 * Return 0 on success else a negative value.
fac6795d 4701 */
26296c48 4702static int set_option(int opt, const char *arg, const char *optname)
fac6795d 4703{
26296c48 4704 int ret = 0;
fac6795d 4705
c0232ea5 4706 if (string_match(optname, "client-sock") || opt == 'c') {
66b2ce8e
JG
4707 if (!arg || *arg == '\0') {
4708 ret = -EINVAL;
4709 goto end;
4710 }
e8fa9fb0
MD
4711 if (lttng_is_setuid_setgid()) {
4712 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4713 "-c, --client-sock");
4714 } else {
4715 snprintf(client_unix_sock_path, PATH_MAX, "%s", arg);
4716 }
c0232ea5 4717 } else if (string_match(optname, "apps-sock") || opt == 'a') {
66b2ce8e
JG
4718 if (!arg || *arg == '\0') {
4719 ret = -EINVAL;
4720 goto end;
4721 }
e8fa9fb0
MD
4722 if (lttng_is_setuid_setgid()) {
4723 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4724 "-a, --apps-sock");
4725 } else {
4726 snprintf(apps_unix_sock_path, PATH_MAX, "%s", arg);
4727 }
c0232ea5 4728 } else if (string_match(optname, "daemonize") || opt == 'd') {
26296c48 4729 opt_daemon = 1;
c0232ea5 4730 } else if (string_match(optname, "background") || opt == 'b') {
72dd7491 4731 opt_background = 1;
c0232ea5 4732 } else if (string_match(optname, "group") || opt == 'g') {
66b2ce8e
JG
4733 if (!arg || *arg == '\0') {
4734 ret = -EINVAL;
4735 goto end;
4736 }
e8fa9fb0
MD
4737 if (lttng_is_setuid_setgid()) {
4738 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4739 "-g, --group");
4740 } else {
4741 /*
4742 * If the override option is set, the pointer points to a
4743 * *non* const thus freeing it even though the variable type is
4744 * set to const.
4745 */
4746 if (tracing_group_name_override) {
4747 free((void *) tracing_group_name);
4748 }
4749 tracing_group_name = strdup(arg);
4750 if (!tracing_group_name) {
4751 PERROR("strdup");
4752 ret = -ENOMEM;
4753 }
4754 tracing_group_name_override = 1;
db322c4d 4755 }
c0232ea5 4756 } else if (string_match(optname, "help") || opt == 'h') {
8190767e
PP
4757 ret = utils_show_man_page(8, "lttng-sessiond");
4758 if (ret) {
4759 ERR("Cannot view man page lttng-sessiond(8)");
4760 perror("exec");
4761 }
4762 exit(ret ? EXIT_FAILURE : EXIT_SUCCESS);
c0232ea5 4763 } else if (string_match(optname, "version") || opt == 'V') {
8ff7624a 4764 opt_print_version = 1;
c0232ea5 4765 } else if (string_match(optname, "sig-parent") || opt == 'S') {
26296c48 4766 opt_sig_parent = 1;
c0232ea5 4767 } else if (string_match(optname, "kconsumerd-err-sock")) {
66b2ce8e
JG
4768 if (!arg || *arg == '\0') {
4769 ret = -EINVAL;
4770 goto end;
4771 }
e8fa9fb0
MD
4772 if (lttng_is_setuid_setgid()) {
4773 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4774 "--kconsumerd-err-sock");
4775 } else {
4776 snprintf(kconsumer_data.err_unix_sock_path, PATH_MAX, "%s", arg);
4777 }
c0232ea5 4778 } else if (string_match(optname, "kconsumerd-cmd-sock")) {
66b2ce8e
JG
4779 if (!arg || *arg == '\0') {
4780 ret = -EINVAL;
4781 goto end;
4782 }
e8fa9fb0
MD
4783 if (lttng_is_setuid_setgid()) {
4784 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4785 "--kconsumerd-cmd-sock");
4786 } else {
4787 snprintf(kconsumer_data.cmd_unix_sock_path, PATH_MAX, "%s", arg);
4788 }
c0232ea5 4789 } else if (string_match(optname, "ustconsumerd64-err-sock")) {
66b2ce8e
JG
4790 if (!arg || *arg == '\0') {
4791 ret = -EINVAL;
4792 goto end;
4793 }
e8fa9fb0
MD
4794 if (lttng_is_setuid_setgid()) {
4795 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4796 "--ustconsumerd64-err-sock");
4797 } else {
4798 snprintf(ustconsumer64_data.err_unix_sock_path, PATH_MAX, "%s", arg);
4799 }
c0232ea5 4800 } else if (string_match(optname, "ustconsumerd64-cmd-sock")) {
66b2ce8e
JG
4801 if (!arg || *arg == '\0') {
4802 ret = -EINVAL;
4803 goto end;
4804 }
e8fa9fb0
MD
4805 if (lttng_is_setuid_setgid()) {
4806 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4807 "--ustconsumerd64-cmd-sock");
4808 } else {
4809 snprintf(ustconsumer64_data.cmd_unix_sock_path, PATH_MAX, "%s", arg);
4810 }
c0232ea5 4811 } else if (string_match(optname, "ustconsumerd32-err-sock")) {
66b2ce8e
JG
4812 if (!arg || *arg == '\0') {
4813 ret = -EINVAL;
4814 goto end;
4815 }
e8fa9fb0
MD
4816 if (lttng_is_setuid_setgid()) {
4817 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4818 "--ustconsumerd32-err-sock");
4819 } else {
4820 snprintf(ustconsumer32_data.err_unix_sock_path, PATH_MAX, "%s", arg);
4821 }
c0232ea5 4822 } else if (string_match(optname, "ustconsumerd32-cmd-sock")) {
66b2ce8e
JG
4823 if (!arg || *arg == '\0') {
4824 ret = -EINVAL;
4825 goto end;
4826 }
e8fa9fb0
MD
4827 if (lttng_is_setuid_setgid()) {
4828 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4829 "--ustconsumerd32-cmd-sock");
4830 } else {
4831 snprintf(ustconsumer32_data.cmd_unix_sock_path, PATH_MAX, "%s", arg);
4832 }
c0232ea5 4833 } else if (string_match(optname, "no-kernel")) {
26296c48 4834 opt_no_kernel = 1;
c0232ea5 4835 } else if (string_match(optname, "quiet") || opt == 'q') {
26296c48 4836 lttng_opt_quiet = 1;
c0232ea5 4837 } else if (string_match(optname, "verbose") || opt == 'v') {
26296c48
JG
4838 /* Verbose level can increase using multiple -v */
4839 if (arg) {
13755a18 4840 /* Value obtained from config file */
26296c48
JG
4841 lttng_opt_verbose = config_parse_value(arg);
4842 } else {
13755a18
JG
4843 /* -v used on command line */
4844 lttng_opt_verbose++;
26296c48 4845 }
13755a18
JG
4846 /* Clamp value to [0, 3] */
4847 lttng_opt_verbose = lttng_opt_verbose < 0 ? 0 :
4848 (lttng_opt_verbose <= 3 ? lttng_opt_verbose : 3);
c0232ea5 4849 } else if (string_match(optname, "verbose-consumer")) {
26296c48
JG
4850 if (arg) {
4851 opt_verbose_consumer = config_parse_value(arg);
4852 } else {
ca939de8 4853 opt_verbose_consumer++;
26296c48 4854 }
c0232ea5 4855 } else if (string_match(optname, "consumerd32-path")) {
66b2ce8e
JG
4856 if (!arg || *arg == '\0') {
4857 ret = -EINVAL;
4858 goto end;
4859 }
e8fa9fb0
MD
4860 if (lttng_is_setuid_setgid()) {
4861 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4862 "--consumerd32-path");
4863 } else {
4864 if (consumerd32_bin_override) {
4865 free((void *) consumerd32_bin);
4866 }
4867 consumerd32_bin = strdup(arg);
4868 if (!consumerd32_bin) {
4869 PERROR("strdup");
4870 ret = -ENOMEM;
4871 }
4872 consumerd32_bin_override = 1;
db322c4d 4873 }
c0232ea5 4874 } else if (string_match(optname, "consumerd32-libdir")) {
66b2ce8e
JG
4875 if (!arg || *arg == '\0') {
4876 ret = -EINVAL;
4877 goto end;
4878 }
e8fa9fb0
MD
4879 if (lttng_is_setuid_setgid()) {
4880 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4881 "--consumerd32-libdir");
4882 } else {
4883 if (consumerd32_libdir_override) {
4884 free((void *) consumerd32_libdir);
4885 }
4886 consumerd32_libdir = strdup(arg);
4887 if (!consumerd32_libdir) {
4888 PERROR("strdup");
4889 ret = -ENOMEM;
4890 }
4891 consumerd32_libdir_override = 1;
db322c4d 4892 }
c0232ea5 4893 } else if (string_match(optname, "consumerd64-path")) {
66b2ce8e
JG
4894 if (!arg || *arg == '\0') {
4895 ret = -EINVAL;
4896 goto end;
4897 }
e8fa9fb0
MD
4898 if (lttng_is_setuid_setgid()) {
4899 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4900 "--consumerd64-path");
4901 } else {
4902 if (consumerd64_bin_override) {
4903 free((void *) consumerd64_bin);
4904 }
4905 consumerd64_bin = strdup(arg);
4906 if (!consumerd64_bin) {
4907 PERROR("strdup");
4908 ret = -ENOMEM;
4909 }
4910 consumerd64_bin_override = 1;
db322c4d 4911 }
c0232ea5 4912 } else if (string_match(optname, "consumerd64-libdir")) {
66b2ce8e
JG
4913 if (!arg || *arg == '\0') {
4914 ret = -EINVAL;
4915 goto end;
4916 }
e8fa9fb0
MD
4917 if (lttng_is_setuid_setgid()) {
4918 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4919 "--consumerd64-libdir");
4920 } else {
4921 if (consumerd64_libdir_override) {
4922 free((void *) consumerd64_libdir);
4923 }
4924 consumerd64_libdir = strdup(arg);
4925 if (!consumerd64_libdir) {
4926 PERROR("strdup");
4927 ret = -ENOMEM;
4928 }
4929 consumerd64_libdir_override = 1;
db322c4d 4930 }
c0232ea5 4931 } else if (string_match(optname, "pidfile") || opt == 'p') {
66b2ce8e
JG
4932 if (!arg || *arg == '\0') {
4933 ret = -EINVAL;
4934 goto end;
4935 }
e8fa9fb0
MD
4936 if (lttng_is_setuid_setgid()) {
4937 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4938 "-p, --pidfile");
4939 } else {
4940 free(opt_pidfile);
4941 opt_pidfile = strdup(arg);
4942 if (!opt_pidfile) {
4943 PERROR("strdup");
4944 ret = -ENOMEM;
4945 }
db322c4d 4946 }
c0232ea5 4947 } else if (string_match(optname, "agent-tcp-port")) {
66b2ce8e
JG
4948 if (!arg || *arg == '\0') {
4949 ret = -EINVAL;
4950 goto end;
4951 }
e8fa9fb0
MD
4952 if (lttng_is_setuid_setgid()) {
4953 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4954 "--agent-tcp-port");
4955 } else {
4956 unsigned long v;
4d076222 4957
e8fa9fb0
MD
4958 errno = 0;
4959 v = strtoul(arg, NULL, 0);
4960 if (errno != 0 || !isdigit(arg[0])) {
4961 ERR("Wrong value in --agent-tcp-port parameter: %s", arg);
4962 return -1;
4963 }
4964 if (v == 0 || v >= 65535) {
4965 ERR("Port overflow in --agent-tcp-port parameter: %s", arg);
4966 return -1;
4967 }
4968 agent_tcp_port = (uint32_t) v;
4969 DBG3("Agent TCP port set to non default: %u", agent_tcp_port);
26296c48 4970 }
c0232ea5 4971 } else if (string_match(optname, "load") || opt == 'l') {
66b2ce8e
JG
4972 if (!arg || *arg == '\0') {
4973 ret = -EINVAL;
4974 goto end;
4975 }
e8fa9fb0
MD
4976 if (lttng_is_setuid_setgid()) {
4977 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4978 "-l, --load");
4979 } else {
4980 free(opt_load_session_path);
4981 opt_load_session_path = strdup(arg);
4982 if (!opt_load_session_path) {
4983 PERROR("strdup");
4984 ret = -ENOMEM;
4985 }
ef367a93 4986 }
c0232ea5 4987 } else if (string_match(optname, "kmod-probes")) {
66b2ce8e
JG
4988 if (!arg || *arg == '\0') {
4989 ret = -EINVAL;
4990 goto end;
4991 }
e8fa9fb0
MD
4992 if (lttng_is_setuid_setgid()) {
4993 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4994 "--kmod-probes");
4995 } else {
4996 free(kmod_probes_list);
4997 kmod_probes_list = strdup(arg);
4998 if (!kmod_probes_list) {
4999 PERROR("strdup");
5000 ret = -ENOMEM;
5001 }
c9d42407 5002 }
c0232ea5 5003 } else if (string_match(optname, "extra-kmod-probes")) {
66b2ce8e
JG
5004 if (!arg || *arg == '\0') {
5005 ret = -EINVAL;
5006 goto end;
5007 }
e8fa9fb0
MD
5008 if (lttng_is_setuid_setgid()) {
5009 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5010 "--extra-kmod-probes");
5011 } else {
5012 free(kmod_extra_probes_list);
5013 kmod_extra_probes_list = strdup(arg);
5014 if (!kmod_extra_probes_list) {
5015 PERROR("strdup");
5016 ret = -ENOMEM;
5017 }
db322c4d 5018 }
c0232ea5
JG
5019 } else if (string_match(optname, "config") || opt == 'f') {
5020 /* This is handled in set_options() thus silent skip. */
5021 goto end;
5022 } else {
26296c48
JG
5023 /* Unknown option or other error.
5024 * Error is printed by getopt, just return */
5025 ret = -1;
5026 }
5027
f3dd7ce7 5028end:
c5d350b2
JG
5029 if (ret == -EINVAL) {
5030 const char *opt_name = "unknown";
5031 int i;
5032
5033 for (i = 0; i < sizeof(long_options) / sizeof(struct option);
5034 i++) {
5035 if (opt == long_options[i].val) {
5036 opt_name = long_options[i].name;
5037 break;
5038 }
5039 }
5040
5041 WARN("Invalid argument provided for option \"%s\", using default value.",
5042 opt_name);
5043 }
f3dd7ce7 5044
26296c48
JG
5045 return ret;
5046}
5047
5048/*
5049 * config_entry_handler_cb used to handle options read from a config file.
f40ef1d5 5050 * See config_entry_handler_cb comment in common/config/session-config.h for the
26296c48
JG
5051 * return value conventions.
5052 */
5053static int config_entry_handler(const struct config_entry *entry, void *unused)
5054{
5055 int ret = 0, i;
5056
5057 if (!entry || !entry->name || !entry->value) {
5058 ret = -EINVAL;
5059 goto end;
5060 }
5061
5062 /* Check if the option is to be ignored */
5063 for (i = 0; i < sizeof(config_ignore_options) / sizeof(char *); i++) {
5064 if (!strcmp(entry->name, config_ignore_options[i])) {
5065 goto end;
5066 }
5067 }
5068
5069 for (i = 0; i < (sizeof(long_options) / sizeof(struct option)) - 1;
5070 i++) {
5071
5072 /* Ignore if not fully matched. */
5073 if (strcmp(entry->name, long_options[i].name)) {
5074 continue;
5075 }
5076
5077 /*
5078 * If the option takes no argument on the command line, we have to
5079 * check if the value is "true". We support non-zero numeric values,
5080 * true, on and yes.
5081 */
5082 if (!long_options[i].has_arg) {
5083 ret = config_parse_value(entry->value);
5084 if (ret <= 0) {
5085 if (ret) {
5086 WARN("Invalid configuration value \"%s\" for option %s",
5087 entry->value, entry->name);
5088 }
5089 /* False, skip boolean config option. */
5090 goto end;
4d076222 5091 }
26296c48
JG
5092 }
5093
5094 ret = set_option(long_options[i].val, entry->value, entry->name);
5095 goto end;
5096 }
5097
5098 WARN("Unrecognized option \"%s\" in daemon configuration file.", entry->name);
5099
5100end:
5101 return ret;
5102}
5103
8ff7624a
JR
5104static void sessiond_config_log(void)
5105{
5106 DBG("LTTng-sessiond " VERSION " - " VERSION_NAME "%s%s",
5107 GIT_VERSION[0] == '\0' ? "" : " - " GIT_VERSION,
5108 EXTRA_VERSION_NAME[0] == '\0' ? "" : " - " EXTRA_VERSION_NAME);
5109 if (EXTRA_VERSION_DESCRIPTION[0] != '\0') {
5110 DBG("LTTng-sessiond extra version description:\n\t" EXTRA_VERSION_DESCRIPTION "\n");
5111 }
2987f328
JR
5112 if (EXTRA_VERSION_PATCHES[0] != '\0') {
5113 DBG("LTTng-sessiond extra patches:\n\t" EXTRA_VERSION_PATCHES "\n");
5114 }
8ff7624a
JR
5115}
5116
5117static void print_version(void) {
5118 fprintf(stdout, "%s\n", VERSION);
5119}
5120
26296c48
JG
5121/*
5122 * daemon configuration loading and argument parsing
5123 */
5124static int set_options(int argc, char **argv)
5125{
5126 int ret = 0, c = 0, option_index = 0;
5127 int orig_optopt = optopt, orig_optind = optind;
5128 char *optstring;
5129 const char *config_path = NULL;
5130
5131 optstring = utils_generate_optstring(long_options,
5132 sizeof(long_options) / sizeof(struct option));
5133 if (!optstring) {
5134 ret = -ENOMEM;
5135 goto end;
5136 }
5137
5138 /* Check for the --config option */
5139 while ((c = getopt_long(argc, argv, optstring, long_options,
5140 &option_index)) != -1) {
5141 if (c == '?') {
5142 ret = -EINVAL;
5143 goto end;
5144 } else if (c != 'f') {
5145 /* if not equal to --config option. */
5146 continue;
5147 }
5148
e8fa9fb0
MD
5149 if (lttng_is_setuid_setgid()) {
5150 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5151 "-f, --config");
5152 } else {
5153 config_path = utils_expand_path(optarg);
5154 if (!config_path) {
5155 ERR("Failed to resolve path: %s", optarg);
5156 }
26296c48
JG
5157 }
5158 }
5159
5160 ret = config_get_section_entries(config_path, config_section_name,
5161 config_entry_handler, NULL);
5162 if (ret) {
5163 if (ret > 0) {
5164 ERR("Invalid configuration option at line %i", ret);
5165 ret = -1;
5166 }
5167 goto end;
5168 }
5169
5170 /* Reset getopt's global state */
5171 optopt = orig_optopt;
5172 optind = orig_optind;
5173 while (1) {
c0232ea5
JG
5174 option_index = -1;
5175 /*
5176 * getopt_long() will not set option_index if it encounters a
5177 * short option.
5178 */
5179 c = getopt_long(argc, argv, optstring, long_options,
5180 &option_index);
26296c48 5181 if (c == -1) {
4d076222
DG
5182 break;
5183 }
26296c48 5184
c0232ea5
JG
5185 /*
5186 * Pass NULL as the long option name if popt left the index
5187 * unset.
5188 */
5189 ret = set_option(c, optarg,
5190 option_index < 0 ? NULL :
5191 long_options[option_index].name);
26296c48
JG
5192 if (ret < 0) {
5193 break;
fac6795d
DG
5194 }
5195 }
5196
26296c48
JG
5197end:
5198 free(optstring);
5199 return ret;
fac6795d
DG
5200}
5201
5202/*
d063d709 5203 * Creates the two needed socket by the daemon.
d6f42150
DG
5204 * apps_sock - The communication socket for all UST apps.
5205 * client_sock - The communication of the cli tool (lttng).
fac6795d 5206 */
cf3af59e 5207static int init_daemon_socket(void)
fac6795d
DG
5208{
5209 int ret = 0;
5210 mode_t old_umask;
5211
5212 old_umask = umask(0);
5213
5214 /* Create client tool unix socket */
d6f42150
DG
5215 client_sock = lttcomm_create_unix_sock(client_unix_sock_path);
5216 if (client_sock < 0) {
5217 ERR("Create unix sock failed: %s", client_unix_sock_path);
fac6795d
DG
5218 ret = -1;
5219 goto end;
5220 }
5221
b662582b
DG
5222 /* Set the cloexec flag */
5223 ret = utils_set_fd_cloexec(client_sock);
5224 if (ret < 0) {
5225 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
5226 "Continuing but note that the consumer daemon will have a "
5227 "reference to this socket on exec()", client_sock);
5228 }
5229
fac6795d
DG
5230 /* File permission MUST be 660 */
5231 ret = chmod(client_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
5232 if (ret < 0) {
d6f42150 5233 ERR("Set file permissions failed: %s", client_unix_sock_path);
76d7553f 5234 PERROR("chmod");
fac6795d
DG
5235 goto end;
5236 }
5237
5238 /* Create the application unix socket */
d6f42150
DG
5239 apps_sock = lttcomm_create_unix_sock(apps_unix_sock_path);
5240 if (apps_sock < 0) {
5241 ERR("Create unix sock failed: %s", apps_unix_sock_path);
fac6795d
DG
5242 ret = -1;
5243 goto end;
5244 }
5245
b662582b
DG
5246 /* Set the cloexec flag */
5247 ret = utils_set_fd_cloexec(apps_sock);
5248 if (ret < 0) {
5249 ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). "
5250 "Continuing but note that the consumer daemon will have a "
5251 "reference to this socket on exec()", apps_sock);
5252 }
5253
d6f42150 5254 /* File permission MUST be 666 */
54d01ffb
DG
5255 ret = chmod(apps_unix_sock_path,
5256 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
fac6795d 5257 if (ret < 0) {
d6f42150 5258 ERR("Set file permissions failed: %s", apps_unix_sock_path);
76d7553f 5259 PERROR("chmod");
fac6795d
DG
5260 goto end;
5261 }
5262
b662582b
DG
5263 DBG3("Session daemon client socket %d and application socket %d created",
5264 client_sock, apps_sock);
5265
fac6795d
DG
5266end:
5267 umask(old_umask);
5268 return ret;
5269}
5270
5271/*
54d01ffb
DG
5272 * Check if the global socket is available, and if a daemon is answering at the
5273 * other side. If yes, error is returned.
fac6795d 5274 */
cf3af59e 5275static int check_existing_daemon(void)
fac6795d 5276{
7d8234d9 5277 /* Is there anybody out there ? */
099e26bd 5278 if (lttng_session_daemon_alive()) {
7d8234d9 5279 return -EEXIST;
099e26bd 5280 }
b09c7c76
DG
5281
5282 return 0;
fac6795d
DG
5283}
5284
fac6795d 5285/*
d063d709 5286 * Set the tracing group gid onto the client socket.
5e16da05 5287 *
d063d709 5288 * Race window between mkdir and chown is OK because we are going from more
d1613cf5 5289 * permissive (root.root) to less permissive (root.tracing).
fac6795d 5290 */
be040666 5291static int set_permissions(char *rundir)
fac6795d
DG
5292{
5293 int ret;
996b65c8 5294 gid_t gid;
fac6795d 5295
6c71277b 5296 gid = utils_get_group_id(tracing_group_name);
fac6795d 5297
d6f42150 5298 /* Set lttng run dir */
be040666 5299 ret = chown(rundir, 0, gid);
d6f42150 5300 if (ret < 0) {
be040666 5301 ERR("Unable to set group on %s", rundir);
76d7553f 5302 PERROR("chown");
d6f42150
DG
5303 }
5304
6c71277b
MD
5305 /*
5306 * Ensure all applications and tracing group can search the run
5307 * dir. Allow everyone to read the directory, since it does not
5308 * buy us anything to hide its content.
5309 */
5310 ret = chmod(rundir, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
d1613cf5
JN
5311 if (ret < 0) {
5312 ERR("Unable to set permissions on %s", rundir);
76d7553f 5313 PERROR("chmod");
d1613cf5
JN
5314 }
5315
d6f42150 5316 /* lttng client socket path */
996b65c8 5317 ret = chown(client_unix_sock_path, 0, gid);
fac6795d 5318 if (ret < 0) {
d6f42150 5319 ERR("Unable to set group on %s", client_unix_sock_path);
76d7553f 5320 PERROR("chown");
d6f42150
DG
5321 }
5322
3bd1e081 5323 /* kconsumer error socket path */
6c71277b 5324 ret = chown(kconsumer_data.err_unix_sock_path, 0, 0);
d6f42150 5325 if (ret < 0) {
3bd1e081 5326 ERR("Unable to set group on %s", kconsumer_data.err_unix_sock_path);
76d7553f 5327 PERROR("chown");
3bd1e081
MD
5328 }
5329
7753dea8 5330 /* 64-bit ustconsumer error socket path */
6c71277b 5331 ret = chown(ustconsumer64_data.err_unix_sock_path, 0, 0);
7753dea8
MD
5332 if (ret < 0) {
5333 ERR("Unable to set group on %s", ustconsumer64_data.err_unix_sock_path);
76d7553f 5334 PERROR("chown");
7753dea8
MD
5335 }
5336
5337 /* 32-bit ustconsumer compat32 error socket path */
6c71277b 5338 ret = chown(ustconsumer32_data.err_unix_sock_path, 0, 0);
3bd1e081 5339 if (ret < 0) {
7753dea8 5340 ERR("Unable to set group on %s", ustconsumer32_data.err_unix_sock_path);
76d7553f 5341 PERROR("chown");
fac6795d
DG
5342 }
5343
d6f42150 5344 DBG("All permissions are set");
e07ae692 5345
fac6795d
DG
5346 return ret;
5347}
5348
d6f42150 5349/*
d063d709 5350 * Create the lttng run directory needed for all global sockets and pipe.
d6f42150 5351 */
67e40797 5352static int create_lttng_rundir(const char *rundir)
d6f42150
DG
5353{
5354 int ret;
5355
67e40797
DG
5356 DBG3("Creating LTTng run directory: %s", rundir);
5357
d1613cf5 5358 ret = mkdir(rundir, S_IRWXU);
d6f42150 5359 if (ret < 0) {
b1f11e69 5360 if (errno != EEXIST) {
67e40797 5361 ERR("Unable to create %s", rundir);
b1f11e69
DG
5362 goto error;
5363 } else {
5364 ret = 0;
5365 }
d6f42150
DG
5366 }
5367
5368error:
5369 return ret;
5370}
5371
5372/*
d063d709
DG
5373 * Setup sockets and directory needed by the kconsumerd communication with the
5374 * session daemon.
d6f42150 5375 */
67e40797
DG
5376static int set_consumer_sockets(struct consumer_data *consumer_data,
5377 const char *rundir)
d6f42150
DG
5378{
5379 int ret;
67e40797 5380 char path[PATH_MAX];
d6f42150 5381
6c71277b 5382 switch (consumer_data->type) {
7753dea8 5383 case LTTNG_CONSUMER_KERNEL:
60922cb0 5384 snprintf(path, PATH_MAX, DEFAULT_KCONSUMERD_PATH, rundir);
7753dea8
MD
5385 break;
5386 case LTTNG_CONSUMER64_UST:
60922cb0 5387 snprintf(path, PATH_MAX, DEFAULT_USTCONSUMERD64_PATH, rundir);
7753dea8
MD
5388 break;
5389 case LTTNG_CONSUMER32_UST:
60922cb0 5390 snprintf(path, PATH_MAX, DEFAULT_USTCONSUMERD32_PATH, rundir);
7753dea8
MD
5391 break;
5392 default:
5393 ERR("Consumer type unknown");
5394 ret = -EINVAL;
5395 goto error;
d6f42150
DG
5396 }
5397
67e40797
DG
5398 DBG2("Creating consumer directory: %s", path);
5399
6c71277b 5400 ret = mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP);
d6f42150 5401 if (ret < 0) {
6beb2242 5402 if (errno != EEXIST) {
f11e84c2 5403 PERROR("mkdir");
3bd1e081 5404 ERR("Failed to create %s", path);
6beb2242
DG
5405 goto error;
5406 }
f11e84c2 5407 ret = -1;
d6f42150 5408 }
6c71277b
MD
5409 if (is_root) {
5410 ret = chown(path, 0, utils_get_group_id(tracing_group_name));
5411 if (ret < 0) {
5412 ERR("Unable to set group on %s", path);
5413 PERROR("chown");
5414 goto error;
5415 }
5416 }
d6f42150
DG
5417
5418 /* Create the kconsumerd error unix socket */
3bd1e081
MD
5419 consumer_data->err_sock =
5420 lttcomm_create_unix_sock(consumer_data->err_unix_sock_path);
5421 if (consumer_data->err_sock < 0) {
5422 ERR("Create unix sock failed: %s", consumer_data->err_unix_sock_path);
d6f42150
DG
5423 ret = -1;
5424 goto error;
5425 }
5426
a24f05ab
MD
5427 /*
5428 * Set the CLOEXEC flag. Return code is useless because either way, the
5429 * show must go on.
5430 */
5431 ret = utils_set_fd_cloexec(consumer_data->err_sock);
5432 if (ret < 0) {
5433 PERROR("utils_set_fd_cloexec");
5434 /* continue anyway */
5435 }
5436
d6f42150 5437 /* File permission MUST be 660 */
3bd1e081 5438 ret = chmod(consumer_data->err_unix_sock_path,
54d01ffb 5439 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
d6f42150 5440 if (ret < 0) {
3bd1e081 5441 ERR("Set file permissions failed: %s", consumer_data->err_unix_sock_path);
67e40797 5442 PERROR("chmod");
d6f42150
DG
5443 goto error;
5444 }
5445
5446error:
5447 return ret;
5448}
5449
fac6795d 5450/*
d063d709 5451 * Signal handler for the daemon
cf3af59e 5452 *
54d01ffb
DG
5453 * Simply stop all worker threads, leaving main() return gracefully after
5454 * joining all threads and calling cleanup().
fac6795d
DG
5455 */
5456static void sighandler(int sig)
5457{
5458 switch (sig) {
cf3af59e 5459 case SIGINT:
af87c45a 5460 DBG("SIGINT caught");
cf3af59e
MD
5461 stop_threads();
5462 break;
5463 case SIGTERM:
af87c45a 5464 DBG("SIGTERM caught");
cf3af59e
MD
5465 stop_threads();
5466 break;
0bb7724a
DG
5467 case SIGUSR1:
5468 CMM_STORE_SHARED(recv_child_signal, 1);
5469 break;
cf3af59e
MD
5470 default:
5471 break;
fac6795d 5472 }
fac6795d
DG
5473}
5474
5475/*
d063d709 5476 * Setup signal handler for :
1d4b027a 5477 * SIGINT, SIGTERM, SIGPIPE
fac6795d 5478 */
1d4b027a 5479static int set_signal_handler(void)
fac6795d 5480{
1d4b027a
DG
5481 int ret = 0;
5482 struct sigaction sa;
5483 sigset_t sigset;
fac6795d 5484
1d4b027a 5485 if ((ret = sigemptyset(&sigset)) < 0) {
76d7553f 5486 PERROR("sigemptyset");
1d4b027a
DG
5487 return ret;
5488 }
d6f42150 5489
1d4b027a
DG
5490 sa.sa_mask = sigset;
5491 sa.sa_flags = 0;
0072e5e2
MD
5492
5493 sa.sa_handler = sighandler;
1d4b027a 5494 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
76d7553f 5495 PERROR("sigaction");
1d4b027a 5496 return ret;
d6f42150
DG
5497 }
5498
1d4b027a 5499 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
76d7553f 5500 PERROR("sigaction");
1d4b027a 5501 return ret;
d6f42150 5502 }
aaf26714 5503
0072e5e2 5504 if ((ret = sigaction(SIGUSR1, &sa, NULL)) < 0) {
76d7553f 5505 PERROR("sigaction");
1d4b027a 5506 return ret;
8c0faa1d
DG
5507 }
5508
0072e5e2
MD
5509 sa.sa_handler = SIG_IGN;
5510 if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) {
0bb7724a
DG
5511 PERROR("sigaction");
5512 return ret;
5513 }
5514
5515 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
1d4b027a
DG
5516
5517 return ret;
fac6795d
DG
5518}
5519
f3ed775e 5520/*
d063d709 5521 * Set open files limit to unlimited. This daemon can open a large number of
514bb9f0 5522 * file descriptors in order to consume multiple kernel traces.
f3ed775e
DG
5523 */
5524static void set_ulimit(void)
5525{
5526 int ret;
5527 struct rlimit lim;
5528
514bb9f0 5529 /* The kernel does not allow an infinite limit for open files */
f3ed775e
DG
5530 lim.rlim_cur = 65535;
5531 lim.rlim_max = 65535;
5532
5533 ret = setrlimit(RLIMIT_NOFILE, &lim);
5534 if (ret < 0) {
76d7553f 5535 PERROR("failed to set open files limit");
f3ed775e
DG
5536 }
5537}
5538
35f90c40
DG
5539/*
5540 * Write pidfile using the rundir and opt_pidfile.
5541 */
4a15001e 5542static int write_pidfile(void)
35f90c40
DG
5543{
5544 int ret;
5545 char pidfile_path[PATH_MAX];
5546
5547 assert(rundir);
5548
5549 if (opt_pidfile) {
b8d76705
MD
5550 if (lttng_strncpy(pidfile_path, opt_pidfile, sizeof(pidfile_path))) {
5551 ret = -1;
5552 goto error;
5553 }
35f90c40
DG
5554 } else {
5555 /* Build pidfile path from rundir and opt_pidfile. */
5556 ret = snprintf(pidfile_path, sizeof(pidfile_path), "%s/"
5557 DEFAULT_LTTNG_SESSIOND_PIDFILE, rundir);
5558 if (ret < 0) {
5559 PERROR("snprintf pidfile path");
5560 goto error;
5561 }
5562 }
5563
5564 /*
4a15001e 5565 * Create pid file in rundir.
35f90c40 5566 */
4a15001e 5567 ret = utils_create_pid_file(getpid(), pidfile_path);
35f90c40 5568error:
4a15001e 5569 return ret;
35f90c40
DG
5570}
5571
c9cb3e7d
JG
5572/*
5573 * Create lockfile using the rundir and return its fd.
5574 */
5575static int create_lockfile(void)
5576{
5577 int ret;
5578 char lockfile_path[PATH_MAX];
5579
5580 ret = generate_lock_file_path(lockfile_path, sizeof(lockfile_path));
5581 if (ret < 0) {
5582 goto error;
5583 }
5584
5585 ret = utils_create_lock_file(lockfile_path);
5586error:
5587 return ret;
5588}
5589
cd9290dd 5590/*
022d91ba 5591 * Write agent TCP port using the rundir.
cd9290dd 5592 */
4a15001e 5593static int write_agent_port(void)
cd9290dd
DG
5594{
5595 int ret;
5596 char path[PATH_MAX];
5597
5598 assert(rundir);
5599
5600 ret = snprintf(path, sizeof(path), "%s/"
022d91ba 5601 DEFAULT_LTTNG_SESSIOND_AGENTPORT_FILE, rundir);
cd9290dd 5602 if (ret < 0) {
022d91ba 5603 PERROR("snprintf agent port path");
cd9290dd
DG
5604 goto error;
5605 }
5606
5607 /*
4a15001e 5608 * Create TCP agent port file in rundir.
cd9290dd 5609 */
4a15001e 5610 ret = utils_create_pid_file(agent_tcp_port, path);
cd9290dd
DG
5611
5612error:
4a15001e 5613 return ret;
ef367a93
JG
5614}
5615
51c59105
JG
5616static int set_clock_plugin_env(void)
5617{
5618 int ret = 0;
5619 const char *original_env_value;
5620 char *full_path = NULL;
5621 char *new_env_value = NULL;
5622
5623 original_env_value = getenv("LTTNG_UST_CLOCK_PLUGIN");
5624 if (!original_env_value) {
5625 goto end;
5626 }
5627
5628 full_path = utils_expand_path(original_env_value);
5629 if (!full_path) {
5630 ERR("Failed to expand LTTNG_UST_CLOCK_PLUGIN path \"%s\"",
5631 original_env_value);
5632 ret = -1;
5633 goto end;
5634 }
5635 ret = asprintf(&new_env_value, "LTTNG_UST_CLOCK_PLUGIN=%s",
5636 full_path);
5637 free(full_path);
5638 if (ret < 0) {
5639 PERROR("asprintf");
5640 goto end;
5641 }
5642
5643 DBG("Updating environment: %s", new_env_value);
5644 ret = putenv(new_env_value);
5645 if (ret) {
5646 free(new_env_value);
5647 PERROR("putenv of LTTNG_UST_CLOCK_PLUGIN");
5648 goto end;
5649 }
5650end:
5651 return ret;
5652}
5653
fac6795d
DG
5654/*
5655 * main
5656 */
5657int main(int argc, char **argv)
5658{
4a15001e 5659 int ret = 0, retval = 0;
fac6795d 5660 void *status;
ae9e45b3 5661 const char *home_path, *env_app_timeout;
fac6795d 5662
335a95b7
MD
5663 init_kernel_workarounds();
5664
f6a9efaa
DG
5665 rcu_register_thread();
5666
4a15001e
MD
5667 if (set_signal_handler()) {
5668 retval = -1;
5669 goto exit_set_signal_handler;
0bb7724a
DG
5670 }
5671
7753dea8 5672 setup_consumerd_path();
fb09408a 5673
12744796
DG
5674 page_size = sysconf(_SC_PAGESIZE);
5675 if (page_size < 0) {
5676 PERROR("sysconf _SC_PAGESIZE");
5677 page_size = LONG_MAX;
5678 WARN("Fallback page size to %ld", page_size);
5679 }
5680
4a15001e
MD
5681 /*
5682 * Parse arguments and load the daemon configuration file.
5683 *
5684 * We have an exit_options exit path to free memory reserved by
5685 * set_options. This is needed because the rest of sessiond_cleanup()
5686 * depends on ht_cleanup_thread, which depends on lttng_daemonize, which
5687 * depends on set_options.
5688 */
fac6795d 5689 progname = argv[0];
4a15001e
MD
5690 if (set_options(argc, argv)) {
5691 retval = -1;
5692 goto exit_options;
fac6795d
DG
5693 }
5694
8ff7624a
JR
5695 sessiond_config_log();
5696
5697 if (opt_print_version) {
5698 print_version();
5699 retval = 0;
5700 goto exit_options;
5701 }
5702
51c59105
JG
5703 ret = set_clock_plugin_env();
5704 if (ret) {
5705 retval = -1;
5706 goto exit_options;
5707 }
5708
fac6795d 5709 /* Daemonize */
72dd7491 5710 if (opt_daemon || opt_background) {
ceed52b5
MD
5711 int i;
5712
72dd7491
MD
5713 ret = lttng_daemonize(&child_ppid, &recv_child_signal,
5714 !opt_background);
53094c05 5715 if (ret < 0) {
4a15001e
MD
5716 retval = -1;
5717 goto exit_options;
53094c05 5718 }
0bb7724a 5719
ceed52b5 5720 /*
0bb7724a 5721 * We are in the child. Make sure all other file descriptors are
4a15001e
MD
5722 * closed, in case we are called with more opened file
5723 * descriptors than the standard ones.
ceed52b5
MD
5724 */
5725 for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) {
5726 (void) close(i);
5727 }
5728 }
5729
7567352f
MD
5730 if (run_as_create_worker(argv[0]) < 0) {
5731 goto exit_create_run_as_worker_cleanup;
5732 }
5733
4a15001e
MD
5734 /*
5735 * Starting from here, we can create threads. This needs to be after
5736 * lttng_daemonize due to RCU.
5737 */
5738
5739 /*
5740 * Initialize the health check subsystem. This call should set the
5741 * appropriate time values.
5742 */
5743 health_sessiond = health_app_create(NR_HEALTH_SESSIOND_TYPES);
5744 if (!health_sessiond) {
5745 PERROR("health_app_create error");
5746 retval = -1;
5747 goto exit_health_sessiond_cleanup;
5748 }
5749
4a15001e 5750 /* Create thread to clean up RCU hash tables */
5e97de00 5751 if (init_ht_cleanup_thread(&ht_cleanup_thread)) {
4a15001e
MD
5752 retval = -1;
5753 goto exit_ht_cleanup;
5754 }
5755
ceed52b5 5756 /* Create thread quit pipe */
4a15001e
MD
5757 if (init_thread_quit_pipe()) {
5758 retval = -1;
5759 goto exit_init_data;
fac6795d
DG
5760 }
5761
5762 /* Check if daemon is UID = 0 */
5763 is_root = !getuid();
5764
fac6795d 5765 if (is_root) {
990570ed 5766 rundir = strdup(DEFAULT_LTTNG_RUNDIR);
4ea184d1 5767 if (!rundir) {
4a15001e
MD
5768 retval = -1;
5769 goto exit_init_data;
4ea184d1 5770 }
67e40797
DG
5771
5772 /* Create global run dir with root access */
4a15001e
MD
5773 if (create_lttng_rundir(rundir)) {
5774 retval = -1;
5775 goto exit_init_data;
d6f42150
DG
5776 }
5777
fac6795d 5778 if (strlen(apps_unix_sock_path) == 0) {
4a15001e 5779 ret = snprintf(apps_unix_sock_path, PATH_MAX,
d6f42150 5780 DEFAULT_GLOBAL_APPS_UNIX_SOCK);
4a15001e
MD
5781 if (ret < 0) {
5782 retval = -1;
5783 goto exit_init_data;
5784 }
fac6795d
DG
5785 }
5786
5787 if (strlen(client_unix_sock_path) == 0) {
4a15001e 5788 ret = snprintf(client_unix_sock_path, PATH_MAX,
d6f42150 5789 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK);
4a15001e
MD
5790 if (ret < 0) {
5791 retval = -1;
5792 goto exit_init_data;
5793 }
d6f42150 5794 }
0fdd1e2c
DG
5795
5796 /* Set global SHM for ust */
5797 if (strlen(wait_shm_path) == 0) {
4a15001e 5798 ret = snprintf(wait_shm_path, PATH_MAX,
0fdd1e2c 5799 DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH);
4a15001e
MD
5800 if (ret < 0) {
5801 retval = -1;
5802 goto exit_init_data;
5803 }
0fdd1e2c 5804 }
67e40797 5805
44a5e5eb 5806 if (strlen(health_unix_sock_path) == 0) {
4a15001e
MD
5807 ret = snprintf(health_unix_sock_path,
5808 sizeof(health_unix_sock_path),
44a5e5eb 5809 DEFAULT_GLOBAL_HEALTH_UNIX_SOCK);
4a15001e
MD
5810 if (ret < 0) {
5811 retval = -1;
5812 goto exit_init_data;
5813 }
44a5e5eb
DG
5814 }
5815
67e40797 5816 /* Setup kernel consumerd path */
4a15001e 5817 ret = snprintf(kconsumer_data.err_unix_sock_path, PATH_MAX,
60922cb0 5818 DEFAULT_KCONSUMERD_ERR_SOCK_PATH, rundir);
4a15001e
MD
5819 if (ret < 0) {
5820 retval = -1;
5821 goto exit_init_data;
5822 }
5823 ret = snprintf(kconsumer_data.cmd_unix_sock_path, PATH_MAX,
60922cb0 5824 DEFAULT_KCONSUMERD_CMD_SOCK_PATH, rundir);
4a15001e
MD
5825 if (ret < 0) {
5826 retval = -1;
5827 goto exit_init_data;
5828 }
67e40797
DG
5829
5830 DBG2("Kernel consumer err path: %s",
5831 kconsumer_data.err_unix_sock_path);
5832 DBG2("Kernel consumer cmd path: %s",
5833 kconsumer_data.cmd_unix_sock_path);
fac6795d 5834 } else {
feb0f3e5 5835 home_path = utils_get_home_dir();
b082db07 5836 if (home_path == NULL) {
273ea72c
DG
5837 /* TODO: Add --socket PATH option */
5838 ERR("Can't get HOME directory for sockets creation.");
4a15001e
MD
5839 retval = -1;
5840 goto exit_init_data;
b082db07
DG
5841 }
5842
67e40797
DG
5843 /*
5844 * Create rundir from home path. This will create something like
5845 * $HOME/.lttng
5846 */
990570ed 5847 ret = asprintf(&rundir, DEFAULT_LTTNG_HOME_RUNDIR, home_path);
67e40797 5848 if (ret < 0) {
4a15001e
MD
5849 retval = -1;
5850 goto exit_init_data;
67e40797
DG
5851 }
5852
4a15001e
MD
5853 if (create_lttng_rundir(rundir)) {
5854 retval = -1;
5855 goto exit_init_data;
67e40797
DG
5856 }
5857
fac6795d 5858 if (strlen(apps_unix_sock_path) == 0) {
4a15001e
MD
5859 ret = snprintf(apps_unix_sock_path, PATH_MAX,
5860 DEFAULT_HOME_APPS_UNIX_SOCK,
5861 home_path);
5862 if (ret < 0) {
5863 retval = -1;
5864 goto exit_init_data;
5865 }
fac6795d
DG
5866 }
5867
5868 /* Set the cli tool unix socket path */
5869 if (strlen(client_unix_sock_path) == 0) {
4a15001e
MD
5870 ret = snprintf(client_unix_sock_path, PATH_MAX,
5871 DEFAULT_HOME_CLIENT_UNIX_SOCK,
5872 home_path);
5873 if (ret < 0) {
5874 retval = -1;
5875 goto exit_init_data;
5876 }
fac6795d 5877 }
0fdd1e2c
DG
5878
5879 /* Set global SHM for ust */
5880 if (strlen(wait_shm_path) == 0) {
4a15001e
MD
5881 ret = snprintf(wait_shm_path, PATH_MAX,
5882 DEFAULT_HOME_APPS_WAIT_SHM_PATH,
5883 getuid());
5884 if (ret < 0) {
5885 retval = -1;
5886 goto exit_init_data;
5887 }
0fdd1e2c 5888 }
44a5e5eb
DG
5889
5890 /* Set health check Unix path */
5891 if (strlen(health_unix_sock_path) == 0) {
4a15001e
MD
5892 ret = snprintf(health_unix_sock_path,
5893 sizeof(health_unix_sock_path),
5894 DEFAULT_HOME_HEALTH_UNIX_SOCK,
5895 home_path);
5896 if (ret < 0) {
5897 retval = -1;
5898 goto exit_init_data;
5899 }
44a5e5eb 5900 }
fac6795d
DG
5901 }
5902
c9cb3e7d
JG
5903 lockfile_fd = create_lockfile();
5904 if (lockfile_fd < 0) {
4a15001e
MD
5905 retval = -1;
5906 goto exit_init_data;
c9cb3e7d
JG
5907 }
5908
5c827ce0
DG
5909 /* Set consumer initial state */
5910 kernel_consumerd_state = CONSUMER_STOPPED;
5911 ust_consumerd_state = CONSUMER_STOPPED;
5912
847177cd
DG
5913 DBG("Client socket path %s", client_unix_sock_path);
5914 DBG("Application socket path %s", apps_unix_sock_path);
d0b96690 5915 DBG("Application wait path %s", wait_shm_path);
67e40797
DG
5916 DBG("LTTng run directory path: %s", rundir);
5917
5918 /* 32 bits consumerd path setup */
4a15001e 5919 ret = snprintf(ustconsumer32_data.err_unix_sock_path, PATH_MAX,
60922cb0 5920 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH, rundir);
4a15001e
MD
5921 if (ret < 0) {
5922 PERROR("snprintf 32-bit consumer error socket path");
5923 retval = -1;
5924 goto exit_init_data;
5925 }
5926 ret = snprintf(ustconsumer32_data.cmd_unix_sock_path, PATH_MAX,
60922cb0 5927 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH, rundir);
4a15001e
MD
5928 if (ret < 0) {
5929 PERROR("snprintf 32-bit consumer command socket path");
5930 retval = -1;
5931 goto exit_init_data;
5932 }
67e40797
DG
5933
5934 DBG2("UST consumer 32 bits err path: %s",
5935 ustconsumer32_data.err_unix_sock_path);
5936 DBG2("UST consumer 32 bits cmd path: %s",
5937 ustconsumer32_data.cmd_unix_sock_path);
5938
5939 /* 64 bits consumerd path setup */
4a15001e 5940 ret = snprintf(ustconsumer64_data.err_unix_sock_path, PATH_MAX,
60922cb0 5941 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH, rundir);
4a15001e
MD
5942 if (ret < 0) {
5943 PERROR("snprintf 64-bit consumer error socket path");
5944 retval = -1;
5945 goto exit_init_data;
5946 }
5947 ret = snprintf(ustconsumer64_data.cmd_unix_sock_path, PATH_MAX,
60922cb0 5948 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH, rundir);
4a15001e
MD
5949 if (ret < 0) {
5950 PERROR("snprintf 64-bit consumer command socket path");
5951 retval = -1;
5952 goto exit_init_data;
5953 }
67e40797
DG
5954
5955 DBG2("UST consumer 64 bits err path: %s",
5956 ustconsumer64_data.err_unix_sock_path);
5957 DBG2("UST consumer 64 bits cmd path: %s",
5958 ustconsumer64_data.cmd_unix_sock_path);
847177cd 5959
273ea72c 5960 /*
7d8234d9 5961 * See if daemon already exist.
fac6795d 5962 */
4a15001e 5963 if (check_existing_daemon()) {
75462a81 5964 ERR("Already running daemon.\n");
273ea72c 5965 /*
cf3af59e
MD
5966 * We do not goto exit because we must not cleanup()
5967 * because a daemon is already running.
ab118b20 5968 */
4a15001e
MD
5969 retval = -1;
5970 goto exit_init_data;
a88df331
DG
5971 }
5972
1427f9b2
DG
5973 /*
5974 * Init UST app hash table. Alloc hash table before this point since
5975 * cleanup() can get called after that point.
5976 */
4a15001e 5977 if (ust_app_ht_alloc()) {
ddbeb0f6 5978 ERR("Failed to allocate UST app hash table");
4a15001e
MD
5979 retval = -1;
5980 goto exit_init_data;
5981 }
1427f9b2 5982
6a4e4039
JG
5983 /*
5984 * Initialize agent app hash table. We allocate the hash table here
5985 * since cleanup() can get called after this point.
5986 */
5987 if (agent_app_ht_alloc()) {
5988 ERR("Failed to allocate Agent app hash table");
4a15001e
MD
5989 retval = -1;
5990 goto exit_init_data;
f20baf8e
DG
5991 }
5992
a88df331
DG
5993 /*
5994 * These actions must be executed as root. We do that *after* setting up
5995 * the sockets path because we MUST make the check for another daemon using
5996 * those paths *before* trying to set the kernel consumer sockets and init
5997 * kernel tracer.
5998 */
5999 if (is_root) {
4a15001e
MD
6000 if (set_consumer_sockets(&kconsumer_data, rundir)) {
6001 retval = -1;
6002 goto exit_init_data;
7753dea8
MD
6003 }
6004
a88df331 6005 /* Setup kernel tracer */
4fba7219
DG
6006 if (!opt_no_kernel) {
6007 init_kernel_tracer();
89588270
DG
6008 if (kernel_tracer_fd >= 0) {
6009 ret = syscall_init_table();
6010 if (ret < 0) {
4a15001e
MD
6011 ERR("Unable to populate syscall table. "
6012 "Syscall tracing won't work "
6013 "for this session daemon.");
89588270 6014 }
834978fd 6015 }
4fba7219 6016 }
a88df331
DG
6017
6018 /* Set ulimit for open files */
6019 set_ulimit();
fac6795d 6020 }
4063050c
MD
6021 /* init lttng_fd tracking must be done after set_ulimit. */
6022 lttng_fd_init();
fac6795d 6023
4a15001e
MD
6024 if (set_consumer_sockets(&ustconsumer64_data, rundir)) {
6025 retval = -1;
6026 goto exit_init_data;
67e40797
DG
6027 }
6028
4a15001e
MD
6029 if (set_consumer_sockets(&ustconsumer32_data, rundir)) {
6030 retval = -1;
6031 goto exit_init_data;
67e40797
DG
6032 }
6033
d6f42150 6034 /* Setup the needed unix socket */
4a15001e
MD
6035 if (init_daemon_socket()) {
6036 retval = -1;
6037 goto exit_init_data;
fac6795d
DG
6038 }
6039
6040 /* Set credentials to socket */
4a15001e
MD
6041 if (is_root && set_permissions(rundir)) {
6042 retval = -1;
6043 goto exit_init_data;
fac6795d
DG
6044 }
6045
5b8719f5
DG
6046 /* Get parent pid if -S, --sig-parent is specified. */
6047 if (opt_sig_parent) {
6048 ppid = getppid();
6049 }
6050
7a485870 6051 /* Setup the kernel pipe for waking up the kernel thread */
6620da75 6052 if (is_root && !opt_no_kernel) {
4a15001e
MD
6053 if (utils_create_pipe_cloexec(kernel_poll_pipe)) {
6054 retval = -1;
6055 goto exit_init_data;
6620da75 6056 }
7a485870
DG
6057 }
6058
099e26bd 6059 /* Setup the thread apps communication pipe. */
4a15001e
MD
6060 if (utils_create_pipe_cloexec(apps_cmd_pipe)) {
6061 retval = -1;
6062 goto exit_init_data;
099e26bd
DG
6063 }
6064
d0b96690 6065 /* Setup the thread apps notify communication pipe. */
4a15001e
MD
6066 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe)) {
6067 retval = -1;
6068 goto exit_init_data;
d0b96690
DG
6069 }
6070
7972aab2
DG
6071 /* Initialize global buffer per UID and PID registry. */
6072 buffer_reg_init_uid_registry();
6073 buffer_reg_init_pid_registry();
6074
099e26bd 6075 /* Init UST command queue. */
8bdee6e2 6076 cds_wfcq_init(&ust_cmd_queue.head, &ust_cmd_queue.tail);
099e26bd 6077
273ea72c 6078 /*
4a15001e
MD
6079 * Get session list pointer. This pointer MUST NOT be free'd. This list
6080 * is statically declared in session.c
273ea72c 6081 */
54d01ffb 6082 session_list_ptr = session_get_list();
b5541356 6083
2f77fc4b 6084 cmd_init();
00e2e675 6085
ae9e45b3
DG
6086 /* Check for the application socket timeout env variable. */
6087 env_app_timeout = getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV);
6088 if (env_app_timeout) {
6089 app_socket_timeout = atoi(env_app_timeout);
6090 } else {
6091 app_socket_timeout = DEFAULT_APP_SOCKET_RW_TIMEOUT;
6092 }
6093
4a15001e
MD
6094 ret = write_pidfile();
6095 if (ret) {
6096 ERR("Error in write_pidfile");
6097 retval = -1;
6098 goto exit_init_data;
6099 }
6100 ret = write_agent_port();
6101 if (ret) {
6102 ERR("Error in write_agent_port");
6103 retval = -1;
6104 goto exit_init_data;
6105 }
35f90c40 6106
554831e7
MD
6107 /* Initialize communication library */
6108 lttcomm_init();
4a15001e 6109 /* Initialize TCP timeout values */
d831c249 6110 lttcomm_inet_init();
554831e7 6111
ef367a93 6112 if (load_session_init_data(&load_info) < 0) {
4a15001e
MD
6113 retval = -1;
6114 goto exit_init_data;
ef367a93
JG
6115 }
6116 load_info->path = opt_load_session_path;
6117
d5b9fd2f 6118 /* Create health-check thread */
1a1a34b4 6119 ret = pthread_create(&health_thread, default_pthread_attr(),
44a5e5eb 6120 thread_manage_health, (void *) NULL);
4a15001e
MD
6121 if (ret) {
6122 errno = ret;
44a5e5eb 6123 PERROR("pthread_create health");
4a15001e 6124 retval = -1;
44a5e5eb
DG
6125 goto exit_health;
6126 }
6127
cf3af59e 6128 /* Create thread to manage the client socket */
1a1a34b4 6129 ret = pthread_create(&client_thread, default_pthread_attr(),
099e26bd 6130 thread_manage_clients, (void *) NULL);
4a15001e
MD
6131 if (ret) {
6132 errno = ret;
76d7553f 6133 PERROR("pthread_create clients");
4a15001e 6134 retval = -1;
cf3af59e
MD
6135 goto exit_client;
6136 }
fac6795d 6137
099e26bd 6138 /* Create thread to dispatch registration */
1a1a34b4 6139 ret = pthread_create(&dispatch_thread, default_pthread_attr(),
099e26bd 6140 thread_dispatch_ust_registration, (void *) NULL);
4a15001e
MD
6141 if (ret) {
6142 errno = ret;
76d7553f 6143 PERROR("pthread_create dispatch");
4a15001e 6144 retval = -1;
099e26bd
DG
6145 goto exit_dispatch;
6146 }
6147
6148 /* Create thread to manage application registration. */
1a1a34b4 6149 ret = pthread_create(&reg_apps_thread, default_pthread_attr(),
099e26bd 6150 thread_registration_apps, (void *) NULL);
4a15001e
MD
6151 if (ret) {
6152 errno = ret;
76d7553f 6153 PERROR("pthread_create registration");
4a15001e 6154 retval = -1;
099e26bd
DG
6155 goto exit_reg_apps;
6156 }
6157
cf3af59e 6158 /* Create thread to manage application socket */
1a1a34b4 6159 ret = pthread_create(&apps_thread, default_pthread_attr(),
54d01ffb 6160 thread_manage_apps, (void *) NULL);
4a15001e
MD
6161 if (ret) {
6162 errno = ret;
d0b96690 6163 PERROR("pthread_create apps");
4a15001e 6164 retval = -1;
d0b96690
DG
6165 goto exit_apps;
6166 }
6167
6168 /* Create thread to manage application notify socket */
1a1a34b4 6169 ret = pthread_create(&apps_notify_thread, default_pthread_attr(),
d0b96690 6170 ust_thread_manage_notify, (void *) NULL);
4a15001e
MD
6171 if (ret) {
6172 errno = ret;
f5c32ef1 6173 PERROR("pthread_create notify");
4a15001e 6174 retval = -1;
9563b0ad 6175 goto exit_apps_notify;
cf3af59e 6176 }
fac6795d 6177
022d91ba 6178 /* Create agent registration thread. */
1a1a34b4 6179 ret = pthread_create(&agent_reg_thread, default_pthread_attr(),
022d91ba 6180 agent_thread_manage_registration, (void *) NULL);
4a15001e
MD
6181 if (ret) {
6182 errno = ret;
022d91ba 6183 PERROR("pthread_create agent");
4a15001e 6184 retval = -1;
022d91ba 6185 goto exit_agent_reg;
4d076222
DG
6186 }
6187
6620da75
DG
6188 /* Don't start this thread if kernel tracing is not requested nor root */
6189 if (is_root && !opt_no_kernel) {
6190 /* Create kernel thread to manage kernel event */
1a1a34b4 6191 ret = pthread_create(&kernel_thread, default_pthread_attr(),
6620da75 6192 thread_manage_kernel, (void *) NULL);
4a15001e
MD
6193 if (ret) {
6194 errno = ret;
6620da75 6195 PERROR("pthread_create kernel");
4a15001e 6196 retval = -1;
6620da75
DG
6197 goto exit_kernel;
6198 }
ef367a93 6199 }
7a485870 6200
4a15001e 6201 /* Create session loading thread. */
1a1a34b4
MJ
6202 ret = pthread_create(&load_session_thread, default_pthread_attr(),
6203 thread_load_session, load_info);
4a15001e
MD
6204 if (ret) {
6205 errno = ret;
6206 PERROR("pthread_create load_session_thread");
6207 retval = -1;
6208 goto exit_load_session;
6209 }
6210
6211 /*
6212 * This is where we start awaiting program completion (e.g. through
6213 * signal that asks threads to teardown).
6214 */
6215
6216 ret = pthread_join(load_session_thread, &status);
6217 if (ret) {
6218 errno = ret;
6219 PERROR("pthread_join load_session_thread");
6220 retval = -1;
6221 }
6222exit_load_session:
ef367a93
JG
6223
6224 if (is_root && !opt_no_kernel) {
6620da75 6225 ret = pthread_join(kernel_thread, &status);
4a15001e
MD
6226 if (ret) {
6227 errno = ret;
6620da75 6228 PERROR("pthread_join");
4a15001e 6229 retval = -1;
6620da75 6230 }
fac6795d 6231 }
cf3af59e 6232exit_kernel:
4a15001e 6233
022d91ba 6234 ret = pthread_join(agent_reg_thread, &status);
4a15001e
MD
6235 if (ret) {
6236 errno = ret;
022d91ba 6237 PERROR("pthread_join agent");
4a15001e 6238 retval = -1;
4d076222 6239 }
022d91ba 6240exit_agent_reg:
4a15001e 6241
9563b0ad 6242 ret = pthread_join(apps_notify_thread, &status);
4a15001e
MD
6243 if (ret) {
6244 errno = ret;
9563b0ad 6245 PERROR("pthread_join apps notify");
4a15001e 6246 retval = -1;
9563b0ad 6247 }
9563b0ad 6248exit_apps_notify:
4a15001e 6249
cf3af59e 6250 ret = pthread_join(apps_thread, &status);
4a15001e
MD
6251 if (ret) {
6252 errno = ret;
9563b0ad 6253 PERROR("pthread_join apps");
4a15001e 6254 retval = -1;
cf3af59e 6255 }
cf3af59e 6256exit_apps:
4a15001e 6257
099e26bd 6258 ret = pthread_join(reg_apps_thread, &status);
4a15001e
MD
6259 if (ret) {
6260 errno = ret;
76d7553f 6261 PERROR("pthread_join");
4a15001e 6262 retval = -1;
099e26bd 6263 }
099e26bd 6264exit_reg_apps:
4a15001e 6265
772b8f4d
MD
6266 /*
6267 * Join dispatch thread after joining reg_apps_thread to ensure
6268 * we don't leak applications in the queue.
6269 */
099e26bd 6270 ret = pthread_join(dispatch_thread, &status);
4a15001e
MD
6271 if (ret) {
6272 errno = ret;
76d7553f 6273 PERROR("pthread_join");
4a15001e 6274 retval = -1;
099e26bd 6275 }
099e26bd 6276exit_dispatch:
4a15001e 6277
cf3af59e 6278 ret = pthread_join(client_thread, &status);
4a15001e
MD
6279 if (ret) {
6280 errno = ret;
76d7553f 6281 PERROR("pthread_join");
4a15001e 6282 retval = -1;
cf3af59e 6283 }
4a15001e 6284exit_client:
a88df331 6285
4a15001e
MD
6286 ret = pthread_join(health_thread, &status);
6287 if (ret) {
6288 errno = ret;
6289 PERROR("pthread_join health thread");
6290 retval = -1;
06f525de 6291 }
4a15001e 6292exit_health:
06f525de 6293
4a15001e 6294exit_init_data:
2668465a
MD
6295 /*
6296 * Wait for all pending call_rcu work to complete before tearing
6297 * down data structures. call_rcu worker may be trying to
6298 * perform lookups in those structures.
6299 */
6300 rcu_barrier();
4a15001e
MD
6301 /*
6302 * sessiond_cleanup() is called when no other thread is running, except
6303 * the ht_cleanup thread, which is needed to destroy the hash tables.
6304 */
6305 rcu_thread_online();
6306 sessiond_cleanup();
6307 rcu_thread_offline();
6308 rcu_unregister_thread();
06f525de 6309
c4d5de9d
JG
6310 /*
6311 * Ensure all prior call_rcu are done. call_rcu callbacks may push
6312 * hash tables to the ht_cleanup thread. Therefore, we ensure that
6313 * the queue is empty before shutting down the clean-up thread.
6314 */
6315 rcu_barrier();
6316
5e97de00 6317 ret = fini_ht_cleanup_thread(&ht_cleanup_thread);
4a15001e 6318 if (ret) {
4a15001e 6319 retval = -1;
0b2dc8df
MD
6320 }
6321exit_ht_cleanup:
4a15001e 6322
4a15001e
MD
6323 health_app_destroy(health_sessiond);
6324exit_health_sessiond_cleanup:
7567352f 6325exit_create_run_as_worker_cleanup:
4a15001e
MD
6326
6327exit_options:
6328 sessiond_cleanup_options();
6329
6330exit_set_signal_handler:
0658924e 6331
4a15001e 6332 if (!retval) {
cf3af59e 6333 exit(EXIT_SUCCESS);
4a15001e
MD
6334 } else {
6335 exit(EXIT_FAILURE);
67e40797 6336 }
fac6795d 6337}
This page took 0.552247 seconds and 5 git commands to generate.