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