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