Implement $PATH binary searching function for userspace-probe
[lttng-tools.git] / src / bin / lttng-sessiond / main.c
CommitLineData
826d496d
MD
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
0fdd1e2c 3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
26296c48 4 * 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
fac6795d 5 *
d14d33bf
AM
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 only,
8 * as published by the Free Software Foundation.
91d76f53 9 *
d14d33bf
AM
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
91d76f53 14 *
d14d33bf
AM
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
fac6795d
DG
18 */
19
6c1c0768 20#define _LGPL_SOURCE
fac6795d
DG
21#include <getopt.h>
22#include <grp.h>
23#include <limits.h>
0bb7724a 24#include <paths.h>
fac6795d
DG
25#include <pthread.h>
26#include <signal.h>
27#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
331744e3 30#include <inttypes.h>
0fdd1e2c 31#include <sys/mman.h>
b73401da 32#include <sys/mount.h>
1e307fab 33#include <sys/resource.h>
fac6795d
DG
34#include <sys/socket.h>
35#include <sys/stat.h>
36#include <sys/types.h>
0fdd1e2c 37#include <sys/wait.h>
5c827ce0 38#include <urcu/uatomic.h>
fac6795d 39#include <unistd.h>
4f0b90ee 40#include <ctype.h>
fac6795d 41
990570ed 42#include <common/common.h>
d27c42b8 43#include <common/compat/socket.h>
e8fa9fb0 44#include <common/compat/getenv.h>
db758600
DG
45#include <common/defaults.h>
46#include <common/kernel-consumer/kernel-consumer.h>
50c8f484 47#include <common/futex.h>
00e2e675 48#include <common/relayd/relayd.h>
81b86775 49#include <common/utils.h>
3ccdf997 50#include <common/daemonize.h>
f40ef1d5 51#include <common/config/session-config.h>
fac6795d 52
10a8a223 53#include "lttng-sessiond.h"
7972aab2 54#include "buffer-registry.h"
54d01ffb 55#include "channel.h"
2f77fc4b 56#include "cmd.h"
00e2e675 57#include "consumer.h"
099e26bd 58#include "context.h"
54d01ffb 59#include "event.h"
4771f025 60#include "kernel.h"
f1e16794 61#include "kernel-consumer.h"
096102bd 62#include "modprobe.h"
0fdd1e2c 63#include "shm.h"
1e307fab 64#include "ust-ctl.h"
00e2e675 65#include "ust-consumer.h"
8e68d1c8 66#include "utils.h"
4063050c 67#include "fd-limit.h"
8782cc74 68#include "health-sessiond.h"
8ac94142 69#include "testpoint.h"
d0b96690 70#include "ust-thread.h"
022d91ba 71#include "agent-thread.h"
fb198a11 72#include "save.h"
ef367a93 73#include "load-session-thread.h"
b3530820
JG
74#include "notification-thread.h"
75#include "notification-thread-commands.h"
db66e574 76#include "rotation-thread.h"
0dbc2034 77#include "lttng-syscall.h"
7c1d2758 78#include "agent.h"
5e97de00 79#include "ht-cleanup.h"
e6142f2e 80#include "sessiond-config.h"
d086f507 81#include "sessiond-timer.h"
ebaeda94 82
4fc83d94
PP
83static const char *help_msg =
84#ifdef LTTNG_EMBED_HELP
85#include <lttng-sessiond.8.h>
86#else
87NULL
88#endif
89;
90
fac6795d 91const char *progname;
1d4b027a 92static pid_t ppid; /* Parent PID for --sig-parent option */
0bb7724a 93static pid_t child_ppid; /* Internal parent PID use with daemonize. */
c9cb3e7d 94static int lockfile_fd = -1;
3bd1e081 95
0bb7724a
DG
96/* Set to 1 when a SIGUSR1 signal is received. */
97static int recv_child_signal;
98
88076e89
JD
99static struct lttng_kernel_tracer_version kernel_tracer_version;
100static struct lttng_kernel_tracer_abi_version kernel_tracer_abi_version;
101
a23ec3a7
DG
102/*
103 * Consumer daemon specific control data. Every value not initialized here is
104 * set to 0 by the static definition.
105 */
3bd1e081
MD
106static struct consumer_data kconsumer_data = {
107 .type = LTTNG_CONSUMER_KERNEL,
03550b58
MD
108 .err_sock = -1,
109 .cmd_sock = -1,
b3530820 110 .channel_monitor_pipe = -1,
62c43103 111 .channel_rotate_pipe = -1,
173af62f
DG
112 .pid_mutex = PTHREAD_MUTEX_INITIALIZER,
113 .lock = PTHREAD_MUTEX_INITIALIZER,
a23ec3a7
DG
114 .cond = PTHREAD_COND_INITIALIZER,
115 .cond_mutex = PTHREAD_MUTEX_INITIALIZER,
3bd1e081 116};
7753dea8
MD
117static struct consumer_data ustconsumer64_data = {
118 .type = LTTNG_CONSUMER64_UST,
03550b58
MD
119 .err_sock = -1,
120 .cmd_sock = -1,
b3530820 121 .channel_monitor_pipe = -1,
62c43103 122 .channel_rotate_pipe = -1,
173af62f
DG
123 .pid_mutex = PTHREAD_MUTEX_INITIALIZER,
124 .lock = PTHREAD_MUTEX_INITIALIZER,
a23ec3a7
DG
125 .cond = PTHREAD_COND_INITIALIZER,
126 .cond_mutex = PTHREAD_MUTEX_INITIALIZER,
7753dea8
MD
127};
128static struct consumer_data ustconsumer32_data = {
129 .type = LTTNG_CONSUMER32_UST,
03550b58
MD
130 .err_sock = -1,
131 .cmd_sock = -1,
b3530820 132 .channel_monitor_pipe = -1,
62c43103 133 .channel_rotate_pipe = -1,
173af62f
DG
134 .pid_mutex = PTHREAD_MUTEX_INITIALIZER,
135 .lock = PTHREAD_MUTEX_INITIALIZER,
a23ec3a7
DG
136 .cond = PTHREAD_COND_INITIALIZER,
137 .cond_mutex = PTHREAD_MUTEX_INITIALIZER,
3bd1e081
MD
138};
139
26296c48
JG
140/* Command line options */
141static const struct option long_options[] = {
0f5ea17c
JG
142 { "client-sock", required_argument, 0, 'c' },
143 { "apps-sock", required_argument, 0, 'a' },
144 { "kconsumerd-cmd-sock", required_argument, 0, '\0' },
145 { "kconsumerd-err-sock", required_argument, 0, '\0' },
146 { "ustconsumerd32-cmd-sock", required_argument, 0, '\0' },
147 { "ustconsumerd32-err-sock", required_argument, 0, '\0' },
148 { "ustconsumerd64-cmd-sock", required_argument, 0, '\0' },
149 { "ustconsumerd64-err-sock", required_argument, 0, '\0' },
150 { "consumerd32-path", required_argument, 0, '\0' },
151 { "consumerd32-libdir", required_argument, 0, '\0' },
152 { "consumerd64-path", required_argument, 0, '\0' },
153 { "consumerd64-libdir", required_argument, 0, '\0' },
154 { "daemonize", no_argument, 0, 'd' },
155 { "background", no_argument, 0, 'b' },
156 { "sig-parent", no_argument, 0, 'S' },
157 { "help", no_argument, 0, 'h' },
158 { "group", required_argument, 0, 'g' },
159 { "version", no_argument, 0, 'V' },
160 { "quiet", no_argument, 0, 'q' },
161 { "verbose", no_argument, 0, 'v' },
162 { "verbose-consumer", no_argument, 0, '\0' },
163 { "no-kernel", no_argument, 0, '\0' },
164 { "pidfile", required_argument, 0, 'p' },
165 { "agent-tcp-port", required_argument, 0, '\0' },
166 { "config", required_argument, 0, 'f' },
167 { "load", required_argument, 0, 'l' },
168 { "kmod-probes", required_argument, 0, '\0' },
169 { "extra-kmod-probes", required_argument, 0, '\0' },
26296c48
JG
170 { NULL, 0, 0, 0 }
171};
172
e6142f2e
JG
173struct sessiond_config config;
174
26296c48
JG
175/* Command line options to ignore from configuration file */
176static const char *config_ignore_options[] = { "help", "version", "config" };
177
26c9d55e 178/* Shared between threads */
099e26bd 179static int dispatch_thread_exit;
fac6795d 180
1d4b027a 181/* Sockets and FDs */
a4b35e07
MD
182static int client_sock = -1;
183static int apps_sock = -1;
2f77fc4b 184int kernel_tracer_fd = -1;
76d7553f 185static int kernel_poll_pipe[2] = { -1, -1 };
1d4b027a 186
273ea72c
DG
187/*
188 * Quit pipe for all threads. This permits a single cancellation point
189 * for all threads when receiving an event on the pipe.
190 */
76d7553f 191static int thread_quit_pipe[2] = { -1, -1 };
273ea72c 192
099e26bd
DG
193/*
194 * This pipe is used to inform the thread managing application communication
195 * that a command is queued and ready to be processed.
196 */
76d7553f 197static int apps_cmd_pipe[2] = { -1, -1 };
099e26bd 198
d0b96690
DG
199int apps_cmd_notify_pipe[2] = { -1, -1 };
200
1d4b027a 201/* Pthread, Mutexes and Semaphores */
1d4b027a 202static pthread_t apps_thread;
d0b96690 203static pthread_t apps_notify_thread;
099e26bd 204static pthread_t reg_apps_thread;
1d4b027a 205static pthread_t client_thread;
7a485870 206static pthread_t kernel_thread;
099e26bd 207static pthread_t dispatch_thread;
44a5e5eb 208static pthread_t health_thread;
0b2dc8df 209static pthread_t ht_cleanup_thread;
022d91ba 210static pthread_t agent_reg_thread;
ef367a93 211static pthread_t load_session_thread;
b3530820 212static pthread_t notification_thread;
db66e574 213static pthread_t rotation_thread;
d086f507 214static pthread_t timer_thread;
5eb91c98 215
099e26bd
DG
216/*
217 * UST registration command queue. This queue is tied with a futex and uses a N
218 * wakers / 1 waiter implemented and detailed in futex.c/.h
219 *
b22c5da8
DG
220 * The thread_registration_apps and thread_dispatch_ust_registration uses this
221 * queue along with the wait/wake scheme. The thread_manage_apps receives down
222 * the line new application socket and monitors it for any I/O error or clean
223 * close that triggers an unregistration of the application.
099e26bd
DG
224 */
225static struct ust_cmd_queue ust_cmd_queue;
226
b5541356
DG
227/*
228 * Pointer initialized before thread creation.
229 *
230 * This points to the tracing session list containing the session count and a
231 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
232 * MUST NOT be taken if you call a public function in session.c.
04ea676f 233 *
d063d709 234 * The lock is nested inside the structure: session_list_ptr->lock. Please use
54d01ffb 235 * session_lock_list and session_unlock_list for lock acquisition.
b5541356
DG
236 */
237static struct ltt_session_list *session_list_ptr;
238
7753dea8
MD
239int ust_consumerd64_fd = -1;
240int ust_consumerd32_fd = -1;
241
2f77fc4b
DG
242static const char *module_proc_lttng = "/proc/lttng";
243
5c827ce0
DG
244/*
245 * Consumer daemon state which is changed when spawning it, killing it or in
246 * case of a fatal error.
247 */
248enum consumerd_state {
249 CONSUMER_STARTED = 1,
250 CONSUMER_STOPPED = 2,
251 CONSUMER_ERROR = 3,
252};
253
254/*
255 * This consumer daemon state is used to validate if a client command will be
256 * able to reach the consumer. If not, the client is informed. For instance,
257 * doing a "lttng start" when the consumer state is set to ERROR will return an
258 * error to the client.
259 *
260 * The following example shows a possible race condition of this scheme:
261 *
262 * consumer thread error happens
263 * client cmd arrives
264 * client cmd checks state -> still OK
265 * consumer thread exit, sets error
266 * client cmd try to talk to consumer
267 * ...
268 *
269 * However, since the consumer is a different daemon, we have no way of making
270 * sure the command will reach it safely even with this state flag. This is why
271 * we consider that up to the state validation during command processing, the
272 * command is safe. After that, we can not guarantee the correctness of the
273 * client request vis-a-vis the consumer.
274 */
275static enum consumerd_state ust_consumerd_state;
276static enum consumerd_state kernel_consumerd_state;
277
12744796
DG
278/* Set in main() with the current page size. */
279long page_size;
280
8782cc74
MD
281/* Application health monitoring */
282struct health_app *health_sessiond;
283
f43f95a9
DG
284/* Am I root or not. */
285int is_root; /* Set to 1 if the daemon is running as root */
286
26296c48
JG
287const char * const config_section_name = "sessiond";
288
ef367a93
JG
289/* Load session thread information to operate. */
290struct load_session_thread_data *load_info;
291
b3530820
JG
292/* Notification thread handle. */
293struct notification_thread_handle *notification_thread_handle;
294
db66e574
JD
295/* Rotation thread handle. */
296struct rotation_thread_handle *rotation_thread_handle;
297
7c1d2758
JG
298/* Global hash tables */
299struct lttng_ht *agent_apps_ht_by_sock = NULL;
300
97bc1426 301/*
9cc1ba3b
JG
302 * The initialization of the session daemon is done in multiple phases.
303 *
304 * While all threads are launched near-simultaneously, only some of them
305 * are needed to ensure the session daemon can start to respond to client
b3530820 306 * requests.
9cc1ba3b
JG
307 *
308 * There are two important guarantees that we wish to offer with respect
309 * to the initialisation of the session daemon:
310 * - When the daemonize/background launcher process exits, the sessiond
311 * is fully able to respond to client requests,
312 * - Auto-loaded sessions are visible to clients.
313 *
314 * In order to achieve this, a number of support threads have to be launched
315 * to allow the "client" thread to function properly. Moreover, since the
316 * "load session" thread needs the client thread, we must provide a way
317 * for the "load session" thread to know that the "client" thread is up
318 * and running.
319 *
320 * Hence, the support threads decrement the lttng_sessiond_ready counter
321 * while the "client" threads waits for it to reach 0. Once the "client" thread
322 * unblocks, it posts the message_thread_ready semaphore which allows the
323 * "load session" thread to progress.
324 *
325 * This implies that the "load session" thread is the last to be initialized
326 * and will explicitly call sessiond_signal_parents(), which signals the parents
327 * that the session daemon is fully initialized.
328 *
329 * The four (4) support threads are:
330 * - agent_thread
331 * - notification_thread
332 * - rotation_thread
333 * - health_thread
97bc1426 334 */
bc4b3ff1
JG
335#define NR_LTTNG_SESSIOND_SUPPORT_THREADS 4
336int lttng_sessiond_ready = NR_LTTNG_SESSIOND_SUPPORT_THREADS;
97bc1426 337
5e97de00
JG
338int sessiond_check_thread_quit_pipe(int fd, uint32_t events)
339{
340 return (fd == thread_quit_pipe[0] && (events & LPOLLIN)) ? 1 : 0;
341}
342
97bc1426 343/* Notify parents that we are ready for cmd and health check */
ef367a93 344LTTNG_HIDDEN
9cc1ba3b 345void sessiond_signal_parents(void)
97bc1426 346{
9cc1ba3b
JG
347 /*
348 * Notify parent pid that we are ready to accept command
349 * for client side. This ppid is the one from the
350 * external process that spawned us.
351 */
352 if (config.sig_parent) {
353 kill(ppid, SIGUSR1);
354 }
97bc1426 355
9cc1ba3b
JG
356 /*
357 * Notify the parent of the fork() process that we are
358 * ready.
359 */
360 if (config.daemonize || config.background) {
361 kill(child_ppid, SIGUSR1);
97bc1426
MD
362 }
363}
364
9cc1ba3b
JG
365LTTNG_HIDDEN
366void sessiond_notify_ready(void)
367{
368 /*
524423d6
JG
369 * This memory barrier is paired with the one performed by
370 * the client thread after it has seen that 'lttng_sessiond_ready' is 0.
371 *
372 * The purpose of these memory barriers is to ensure that all
373 * initialization operations of the various threads that call this
374 * function to signal that they are ready are commited/published
375 * before the client thread can see the 'lttng_sessiond_ready' counter
376 * reach 0.
377 *
378 * Note that this could be a 'write' memory barrier, but a full barrier
379 * is used in case the code using this utility changes. The performance
380 * implications of this choice are minimal since this is a slow path.
9cc1ba3b 381 */
524423d6
JG
382 cmm_smp_mb();
383 uatomic_sub(&lttng_sessiond_ready, 1);
9cc1ba3b
JG
384}
385
4a15001e
MD
386static
387int __sessiond_set_thread_pollset(struct lttng_poll_event *events, size_t size,
388 int *a_pipe)
5eb91c98
DG
389{
390 int ret;
391
d0b96690 392 assert(events);
5eb91c98
DG
393
394 ret = lttng_poll_create(events, size, LTTNG_CLOEXEC);
395 if (ret < 0) {
396 goto error;
397 }
398
399 /* Add quit pipe */
4a15001e 400 ret = lttng_poll_add(events, a_pipe[0], LPOLLIN | LPOLLERR);
5eb91c98
DG
401 if (ret < 0) {
402 goto error;
403 }
404
405 return 0;
406
407error:
408 return ret;
409}
410
4a15001e
MD
411/*
412 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
413 */
414int sessiond_set_thread_pollset(struct lttng_poll_event *events, size_t size)
415{
416 return __sessiond_set_thread_pollset(events, size, thread_quit_pipe);
417}
418
273ea72c 419/*
5eb91c98 420 * Init thread quit pipe.
273ea72c
DG
421 *
422 * Return -1 on error or 0 if all pipes are created.
423 */
4a15001e 424static int __init_thread_quit_pipe(int *a_pipe)
273ea72c 425{
730389d9 426 int ret, i;
273ea72c 427
4a15001e 428 ret = pipe(a_pipe);
273ea72c 429 if (ret < 0) {
730389d9 430 PERROR("thread quit pipe");
273ea72c
DG
431 goto error;
432 }
433
730389d9 434 for (i = 0; i < 2; i++) {
4a15001e 435 ret = fcntl(a_pipe[i], F_SETFD, FD_CLOEXEC);
730389d9
DG
436 if (ret < 0) {
437 PERROR("fcntl");
438 goto error;
439 }
440 }
441
273ea72c
DG
442error:
443 return ret;
444}
445
4a15001e
MD
446static int init_thread_quit_pipe(void)
447{
448 return __init_thread_quit_pipe(thread_quit_pipe);
449}
450
099e26bd
DG
451/*
452 * Stop all threads by closing the thread quit pipe.
453 */
cf3af59e
MD
454static void stop_threads(void)
455{
5eb91c98
DG
456 int ret;
457
cf3af59e
MD
458 /* Stopping all threads */
459 DBG("Terminating all threads");
54d01ffb 460 ret = notify_thread_pipe(thread_quit_pipe[1]);
5eb91c98
DG
461 if (ret < 0) {
462 ERR("write error on thread quit pipe");
463 }
464
099e26bd 465 /* Dispatch thread */
26c9d55e 466 CMM_STORE_SHARED(dispatch_thread_exit, 1);
099e26bd 467 futex_nto1_wake(&ust_cmd_queue.futex);
cf3af59e
MD
468}
469
e975f9f8
DG
470/*
471 * Close every consumer sockets.
472 */
473static void close_consumer_sockets(void)
474{
475 int ret;
476
477 if (kconsumer_data.err_sock >= 0) {
478 ret = close(kconsumer_data.err_sock);
479 if (ret < 0) {
480 PERROR("kernel consumer err_sock close");
481 }
482 }
483 if (ustconsumer32_data.err_sock >= 0) {
484 ret = close(ustconsumer32_data.err_sock);
485 if (ret < 0) {
a76cbd9f 486 PERROR("UST consumerd32 err_sock close");
e975f9f8
DG
487 }
488 }
489 if (ustconsumer64_data.err_sock >= 0) {
490 ret = close(ustconsumer64_data.err_sock);
491 if (ret < 0) {
a76cbd9f 492 PERROR("UST consumerd64 err_sock close");
e975f9f8
DG
493 }
494 }
495 if (kconsumer_data.cmd_sock >= 0) {
496 ret = close(kconsumer_data.cmd_sock);
497 if (ret < 0) {
498 PERROR("kernel consumer cmd_sock close");
499 }
500 }
501 if (ustconsumer32_data.cmd_sock >= 0) {
502 ret = close(ustconsumer32_data.cmd_sock);
503 if (ret < 0) {
a76cbd9f 504 PERROR("UST consumerd32 cmd_sock close");
e975f9f8
DG
505 }
506 }
507 if (ustconsumer64_data.cmd_sock >= 0) {
508 ret = close(ustconsumer64_data.cmd_sock);
509 if (ret < 0) {
a76cbd9f 510 PERROR("UST consumerd64 cmd_sock close");
e975f9f8
DG
511 }
512 }
b3530820
JG
513 if (kconsumer_data.channel_monitor_pipe >= 0) {
514 ret = close(kconsumer_data.channel_monitor_pipe);
515 if (ret < 0) {
516 PERROR("kernel consumer channel monitor pipe close");
517 }
518 }
519 if (ustconsumer32_data.channel_monitor_pipe >= 0) {
520 ret = close(ustconsumer32_data.channel_monitor_pipe);
521 if (ret < 0) {
522 PERROR("UST consumerd32 channel monitor pipe close");
523 }
524 }
525 if (ustconsumer64_data.channel_monitor_pipe >= 0) {
526 ret = close(ustconsumer64_data.channel_monitor_pipe);
527 if (ret < 0) {
528 PERROR("UST consumerd64 channel monitor pipe close");
529 }
530 }
62c43103
JD
531 if (kconsumer_data.channel_rotate_pipe >= 0) {
532 ret = close(kconsumer_data.channel_rotate_pipe);
533 if (ret < 0) {
534 PERROR("kernel consumer channel rotate pipe close");
535 }
536 }
537 if (ustconsumer32_data.channel_rotate_pipe >= 0) {
538 ret = close(ustconsumer32_data.channel_rotate_pipe);
539 if (ret < 0) {
540 PERROR("UST consumerd32 channel rotate pipe close");
541 }
542 }
543 if (ustconsumer64_data.channel_rotate_pipe >= 0) {
544 ret = close(ustconsumer64_data.channel_rotate_pipe);
545 if (ret < 0) {
546 PERROR("UST consumerd64 channel rotate pipe close");
547 }
548 }
e975f9f8
DG
549}
550
4e4714cb
JR
551/*
552 * Wait on consumer process termination.
553 *
554 * Need to be called with the consumer data lock held or from a context
555 * ensuring no concurrent access to data (e.g: cleanup).
556 */
557static void wait_consumer(struct consumer_data *consumer_data)
558{
559 pid_t ret;
560 int status;
561
562 if (consumer_data->pid <= 0) {
563 return;
564 }
565
566 DBG("Waiting for complete teardown of consumerd (PID: %d)",
567 consumer_data->pid);
568 ret = waitpid(consumer_data->pid, &status, 0);
569 if (ret == -1) {
570 PERROR("consumerd waitpid pid: %d", consumer_data->pid)
1640c24c 571 } else if (!WIFEXITED(status)) {
4e4714cb
JR
572 ERR("consumerd termination with error: %d",
573 WEXITSTATUS(ret));
574 }
575 consumer_data->pid = 0;
576}
577
fac6795d 578/*
4a15001e 579 * Cleanup the session daemon's data structures.
fac6795d 580 */
4a15001e 581static void sessiond_cleanup(void)
fac6795d 582{
ef599319 583 int ret;
af9737e9 584 struct ltt_session *sess, *stmp;
fac6795d 585
4a15001e 586 DBG("Cleanup sessiond");
e07ae692 587
4e449f3f
MD
588 /*
589 * Close the thread quit pipe. It has already done its job,
590 * since we are now called.
591 */
2f77fc4b
DG
592 utils_close_pipe(thread_quit_pipe);
593
c9a2957d
JG
594 ret = remove(config.pid_file_path.value);
595 if (ret < 0) {
596 PERROR("remove pidfile %s", config.pid_file_path.value);
35f90c40
DG
597 }
598
e6142f2e
JG
599 DBG("Removing sessiond and consumerd content of directory %s",
600 config.rundir.value);
8c6c56c2
MD
601
602 /* sessiond */
e6142f2e
JG
603 DBG("Removing %s", config.pid_file_path.value);
604 (void) unlink(config.pid_file_path.value);
8c6c56c2 605
e6142f2e
JG
606 DBG("Removing %s", config.agent_port_file_path.value);
607 (void) unlink(config.agent_port_file_path.value);
cd9290dd 608
8c6c56c2 609 /* kconsumerd */
e6142f2e
JG
610 DBG("Removing %s", kconsumer_data.err_unix_sock_path);
611 (void) unlink(kconsumer_data.err_unix_sock_path);
612
613 DBG("Removing directory %s", config.kconsumerd_path.value);
614 (void) rmdir(config.kconsumerd_path.value);
8c6c56c2
MD
615
616 /* ust consumerd 32 */
e6142f2e
JG
617 DBG("Removing %s", config.consumerd32_err_unix_sock_path.value);
618 (void) unlink(config.consumerd32_err_unix_sock_path.value);
619
620 DBG("Removing directory %s", config.consumerd32_path.value);
621 (void) rmdir(config.consumerd32_path.value);
8c6c56c2
MD
622
623 /* ust consumerd 64 */
e6142f2e
JG
624 DBG("Removing %s", config.consumerd64_err_unix_sock_path.value);
625 (void) unlink(config.consumerd64_err_unix_sock_path.value);
626
627 DBG("Removing directory %s", config.consumerd64_path.value);
628 (void) rmdir(config.consumerd64_path.value);
5461b305 629
99bab54f 630 DBG("Cleaning up all sessions");
fac6795d 631
b5541356 632 /* Destroy session list mutex */
273ea72c
DG
633 if (session_list_ptr != NULL) {
634 pthread_mutex_destroy(&session_list_ptr->lock);
635
636 /* Cleanup ALL session */
54d01ffb
DG
637 cds_list_for_each_entry_safe(sess, stmp,
638 &session_list_ptr->head, list) {
90936dcf
JD
639 cmd_destroy_session(sess, kernel_poll_pipe[1],
640 notification_thread_handle);
273ea72c
DG
641 }
642 }
643
4e4714cb
JR
644 wait_consumer(&kconsumer_data);
645 wait_consumer(&ustconsumer64_data);
646 wait_consumer(&ustconsumer32_data);
647
6a4e4039
JG
648 DBG("Cleaning up all agent apps");
649 agent_app_ht_clean();
650
099e26bd 651 DBG("Closing all UST sockets");
56fff090 652 ust_app_clean_list();
7972aab2 653 buffer_reg_destroy_registries();
099e26bd 654
e6142f2e 655 if (is_root && !config.no_kernel) {
4fba7219 656 DBG2("Closing kernel fd");
a4b35e07 657 if (kernel_tracer_fd >= 0) {
76d7553f
MD
658 ret = close(kernel_tracer_fd);
659 if (ret) {
660 PERROR("close");
661 }
a4b35e07 662 }
2f50c8a3 663 DBG("Unloading kernel modules");
096102bd 664 modprobe_remove_lttng_all();
834978fd 665 free(syscall_table);
2f50c8a3 666 }
2f77fc4b 667
e975f9f8
DG
668 close_consumer_sockets();
669
ef367a93
JG
670 if (load_info) {
671 load_session_destroy_data(load_info);
672 free(load_info);
673 }
674
c9cb3e7d
JG
675 /*
676 * We do NOT rmdir rundir because there are other processes
677 * using it, for instance lttng-relayd, which can start in
678 * parallel with this teardown.
679 */
4a15001e
MD
680}
681
682/*
683 * Cleanup the daemon's option data structures.
684 */
685static void sessiond_cleanup_options(void)
686{
687 DBG("Cleaning up options");
688
e6142f2e 689 sessiond_config_fini(&config);
c9cb3e7d 690
7567352f 691 run_as_destroy_worker();
fac6795d
DG
692}
693
e065084a 694/*
d063d709 695 * Send data on a unix socket using the liblttsessiondcomm API.
e065084a 696 *
d063d709 697 * Return lttcomm error code.
e065084a
DG
698 */
699static int send_unix_sock(int sock, void *buf, size_t len)
700{
701 /* Check valid length */
c617c0c6 702 if (len == 0) {
e065084a
DG
703 return -1;
704 }
705
706 return lttcomm_send_unix_sock(sock, buf, len);
707}
708
5461b305 709/*
d063d709 710 * Free memory of a command context structure.
5461b305 711 */
a2fb29a5 712static void clean_command_ctx(struct command_ctx **cmd_ctx)
5461b305 713{
a2fb29a5
DG
714 DBG("Clean command context structure");
715 if (*cmd_ctx) {
716 if ((*cmd_ctx)->llm) {
717 free((*cmd_ctx)->llm);
5461b305 718 }
a2fb29a5
DG
719 if ((*cmd_ctx)->lsm) {
720 free((*cmd_ctx)->lsm);
5461b305 721 }
a2fb29a5
DG
722 free(*cmd_ctx);
723 *cmd_ctx = NULL;
5461b305
DG
724 }
725}
726
fac6795d 727/*
0fdd1e2c 728 * Notify UST applications using the shm mmap futex.
fac6795d 729 */
0fdd1e2c 730static int notify_ust_apps(int active)
fac6795d 731{
0fdd1e2c 732 char *wait_shm_mmap;
fac6795d 733
0fdd1e2c 734 DBG("Notifying applications of session daemon state: %d", active);
e07ae692 735
0fdd1e2c 736 /* See shm.c for this call implying mmap, shm and futex calls */
e6142f2e 737 wait_shm_mmap = shm_ust_get_mmap(config.wait_shm_path.value, is_root);
0fdd1e2c 738 if (wait_shm_mmap == NULL) {
fac6795d
DG
739 goto error;
740 }
741
0fdd1e2c
DG
742 /* Wake waiting process */
743 futex_wait_update((int32_t *) wait_shm_mmap, active);
744
745 /* Apps notified successfully */
746 return 0;
fac6795d
DG
747
748error:
0fdd1e2c 749 return -1;
fac6795d
DG
750}
751
e065084a 752/*
d063d709
DG
753 * Setup the outgoing data buffer for the response (llm) by allocating the
754 * right amount of memory and copying the original information from the lsm
755 * structure.
ca95a216 756 *
6e10c9b9 757 * Return 0 on success, negative value on error.
ca95a216 758 */
6e10c9b9
PP
759static int setup_lttng_msg(struct command_ctx *cmd_ctx,
760 const void *payload_buf, size_t payload_len,
761 const void *cmd_header_buf, size_t cmd_header_len)
ca95a216 762{
6e10c9b9
PP
763 int ret = 0;
764 const size_t header_len = sizeof(struct lttcomm_lttng_msg);
765 const size_t cmd_header_offset = header_len;
766 const size_t payload_offset = cmd_header_offset + cmd_header_len;
767 const size_t total_msg_size = header_len + cmd_header_len + payload_len;
ca95a216 768
6e10c9b9 769 cmd_ctx->llm = zmalloc(total_msg_size);
5461b305 770
5461b305 771 if (cmd_ctx->llm == NULL) {
76d7553f 772 PERROR("zmalloc");
5461b305 773 ret = -ENOMEM;
6e10c9b9 774 goto end;
ca95a216
DG
775 }
776
5461b305
DG
777 /* Copy common data */
778 cmd_ctx->llm->cmd_type = cmd_ctx->lsm->cmd_type;
9f19cc17 779 cmd_ctx->llm->pid = cmd_ctx->lsm->domain.attr.pid;
6e10c9b9
PP
780 cmd_ctx->llm->cmd_header_size = cmd_header_len;
781 cmd_ctx->llm->data_size = payload_len;
782 cmd_ctx->lttng_msg_size = total_msg_size;
5461b305 783
6e10c9b9 784 /* Copy command header */
b4e3ceb9
PP
785 if (cmd_header_len) {
786 memcpy(((uint8_t *) cmd_ctx->llm) + cmd_header_offset, cmd_header_buf,
787 cmd_header_len);
788 }
5461b305 789
6e10c9b9 790 /* Copy payload */
b4e3ceb9
PP
791 if (payload_len) {
792 memcpy(((uint8_t *) cmd_ctx->llm) + payload_offset, payload_buf,
793 payload_len);
794 }
ca95a216 795
6e10c9b9 796end:
ca95a216
DG
797 return ret;
798}
799
6e10c9b9
PP
800/*
801 * Version of setup_lttng_msg() without command header.
802 */
803static int setup_lttng_msg_no_cmd_header(struct command_ctx *cmd_ctx,
804 void *payload_buf, size_t payload_len)
805{
806 return setup_lttng_msg(cmd_ctx, payload_buf, payload_len, NULL, 0);
807}
7a485870 808/*
5eb91c98 809 * Update the kernel poll set of all channel fd available over all tracing
d063d709 810 * session. Add the wakeup pipe at the end of the set.
7a485870 811 */
5eb91c98 812static int update_kernel_poll(struct lttng_poll_event *events)
7a485870 813{
5eb91c98 814 int ret;
7a485870
DG
815 struct ltt_session *session;
816 struct ltt_kernel_channel *channel;
817
5eb91c98 818 DBG("Updating kernel poll set");
7a485870 819
54d01ffb 820 session_lock_list();
b5541356 821 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
54d01ffb 822 session_lock(session);
7a485870 823 if (session->kernel_session == NULL) {
54d01ffb 824 session_unlock(session);
7a485870
DG
825 continue;
826 }
7a485870 827
54d01ffb
DG
828 cds_list_for_each_entry(channel,
829 &session->kernel_session->channel_list.head, list) {
5eb91c98
DG
830 /* Add channel fd to the kernel poll set */
831 ret = lttng_poll_add(events, channel->fd, LPOLLIN | LPOLLRDNORM);
832 if (ret < 0) {
54d01ffb 833 session_unlock(session);
5eb91c98
DG
834 goto error;
835 }
836 DBG("Channel fd %d added to kernel set", channel->fd);
7a485870 837 }
54d01ffb 838 session_unlock(session);
7a485870 839 }
54d01ffb 840 session_unlock_list();
7a485870 841
5eb91c98 842 return 0;
7a485870
DG
843
844error:
54d01ffb 845 session_unlock_list();
7a485870
DG
846 return -1;
847}
848
849/*
54d01ffb 850 * Find the channel fd from 'fd' over all tracing session. When found, check
d063d709 851 * for new channel stream and send those stream fds to the kernel consumer.
7a485870 852 *
d063d709 853 * Useful for CPU hotplug feature.
7a485870 854 */
7f032058 855static int update_kernel_stream(int fd)
7a485870
DG
856{
857 int ret = 0;
858 struct ltt_session *session;
173af62f 859 struct ltt_kernel_session *ksess;
7a485870
DG
860 struct ltt_kernel_channel *channel;
861
862 DBG("Updating kernel streams for channel fd %d", fd);
863
54d01ffb 864 session_lock_list();
b5541356 865 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
54d01ffb 866 session_lock(session);
7a485870 867 if (session->kernel_session == NULL) {
54d01ffb 868 session_unlock(session);
7a485870
DG
869 continue;
870 }
173af62f 871 ksess = session->kernel_session;
d9800920 872
4a15001e
MD
873 cds_list_for_each_entry(channel,
874 &ksess->channel_list.head, list) {
875 struct lttng_ht_iter iter;
876 struct consumer_socket *socket;
d9800920 877
4a15001e
MD
878 if (channel->fd != fd) {
879 continue;
880 }
881 DBG("Channel found, updating kernel streams");
882 ret = kernel_open_channel_stream(channel);
883 if (ret < 0) {
884 goto error;
885 }
886 /* Update the stream global counter */
887 ksess->stream_count_global += ret;
888
889 /*
890 * Have we already sent fds to the consumer? If yes, it
891 * means that tracing is started so it is safe to send
892 * our updated stream fds.
893 */
894 if (ksess->consumer_fds_sent != 1
895 || ksess->consumer == NULL) {
896 ret = -1;
897 goto error;
898 }
899
900 rcu_read_lock();
901 cds_lfht_for_each_entry(ksess->consumer->socks->ht,
902 &iter.iter, socket, node.node) {
903 pthread_mutex_lock(socket->lock);
1fc1b7c8 904 ret = kernel_consumer_send_channel_streams(socket,
4a15001e
MD
905 channel, ksess,
906 session->output_traces ? 1 : 0);
907 pthread_mutex_unlock(socket->lock);
908 if (ret < 0) {
e7fe706f 909 rcu_read_unlock();
4a15001e 910 goto error;
7a485870 911 }
7a485870 912 }
4a15001e 913 rcu_read_unlock();
7a485870 914 }
54d01ffb 915 session_unlock(session);
7a485870 916 }
54d01ffb 917 session_unlock_list();
b3c750d2 918 return ret;
7a485870 919
b3c750d2 920error:
54d01ffb
DG
921 session_unlock(session);
922 session_unlock_list();
7a485870
DG
923 return ret;
924}
925
487cf67c 926/*
ffe60014
DG
927 * For each tracing session, update newly registered apps. The session list
928 * lock MUST be acquired before calling this.
487cf67c
DG
929 */
930static void update_ust_app(int app_sock)
931{
932 struct ltt_session *sess, *stmp;
933
fdadac08
DG
934 /* Consumer is in an ERROR state. Stop any application update. */
935 if (uatomic_read(&ust_consumerd_state) == CONSUMER_ERROR) {
936 /* Stop the update process since the consumer is dead. */
937 return;
938 }
939
487cf67c
DG
940 /* For all tracing session(s) */
941 cds_list_for_each_entry_safe(sess, stmp, &session_list_ptr->head, list) {
a9ad0c8f
MD
942 struct ust_app *app;
943
4ee14516 944 session_lock(sess);
a9ad0c8f
MD
945 if (!sess->ust_session) {
946 goto unlock_session;
947 }
948
949 rcu_read_lock();
950 assert(app_sock >= 0);
951 app = ust_app_find_by_sock(app_sock);
952 if (app == NULL) {
953 /*
954 * Application can be unregistered before so
955 * this is possible hence simply stopping the
956 * update.
957 */
958 DBG3("UST app update failed to find app sock %d",
959 app_sock);
960 goto unlock_rcu;
421cb601 961 }
a9ad0c8f
MD
962 ust_app_global_update(sess->ust_session, app);
963 unlock_rcu:
964 rcu_read_unlock();
965 unlock_session:
4ee14516 966 session_unlock(sess);
487cf67c
DG
967 }
968}
969
7a485870 970/*
d063d709 971 * This thread manage event coming from the kernel.
7a485870 972 *
d063d709
DG
973 * Features supported in this thread:
974 * -) CPU Hotplug
7a485870
DG
975 */
976static void *thread_manage_kernel(void *data)
977{
139ac872 978 int ret, i, pollfd, update_poll_flag = 1, err = -1;
5eb91c98 979 uint32_t revents, nb_fd;
7a485870 980 char tmp;
5eb91c98 981 struct lttng_poll_event events;
7a485870 982
6993eeb3 983 DBG("[thread] Thread manage kernel started");
7a485870 984
6c71277b 985 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_KERNEL);
927ca06a 986
d5d63bf1
DG
987 /*
988 * This first step of the while is to clean this structure which could free
6d737ce4 989 * non NULL pointers so initialize it before the loop.
d5d63bf1 990 */
6d737ce4 991 lttng_poll_init(&events);
d5d63bf1 992
e547b070 993 if (testpoint(sessiond_thread_manage_kernel)) {
6993eeb3
CB
994 goto error_testpoint;
995 }
8ac94142 996
840cb59c 997 health_code_update();
44a5e5eb 998
e547b070 999 if (testpoint(sessiond_thread_manage_kernel_before_loop)) {
d21b0d71 1000 goto error_testpoint;
6993eeb3
CB
1001 }
1002
7a485870 1003 while (1) {
840cb59c 1004 health_code_update();
44a5e5eb 1005
7a485870 1006 if (update_poll_flag == 1) {
d21b0d71
DG
1007 /* Clean events object. We are about to populate it again. */
1008 lttng_poll_clean(&events);
1009
d0b96690 1010 ret = sessiond_set_thread_pollset(&events, 2);
d21b0d71
DG
1011 if (ret < 0) {
1012 goto error_poll_create;
1013 }
1014
1015 ret = lttng_poll_add(&events, kernel_poll_pipe[0], LPOLLIN);
1016 if (ret < 0) {
1017 goto error;
1018 }
5f822d0a 1019
d21b0d71 1020 /* This will add the available kernel channel if any. */
5eb91c98
DG
1021 ret = update_kernel_poll(&events);
1022 if (ret < 0) {
7a485870
DG
1023 goto error;
1024 }
1025 update_poll_flag = 0;
1026 }
1027
7fa2082e 1028 DBG("Thread kernel polling");
7a485870
DG
1029
1030 /* Poll infinite value of time */
88f2b785 1031 restart:
a78af745 1032 health_poll_entry();
5eb91c98 1033 ret = lttng_poll_wait(&events, -1);
7fa2082e
MD
1034 DBG("Thread kernel return from poll on %d fds",
1035 LTTNG_POLL_GETNB(&events));
a78af745 1036 health_poll_exit();
7a485870 1037 if (ret < 0) {
88f2b785
MD
1038 /*
1039 * Restart interrupted system call.
1040 */
1041 if (errno == EINTR) {
1042 goto restart;
1043 }
7a485870
DG
1044 goto error;
1045 } else if (ret == 0) {
1046 /* Should not happen since timeout is infinite */
85611738
DG
1047 ERR("Return value of poll is 0 with an infinite timeout.\n"
1048 "This should not have happened! Continuing...");
7a485870
DG
1049 continue;
1050 }
1051
0d9c5d77
DG
1052 nb_fd = ret;
1053
5eb91c98
DG
1054 for (i = 0; i < nb_fd; i++) {
1055 /* Fetch once the poll data */
1056 revents = LTTNG_POLL_GETEV(&events, i);
1057 pollfd = LTTNG_POLL_GETFD(&events, i);
7a485870 1058
840cb59c 1059 health_code_update();
44a5e5eb 1060
fd20dac9
MD
1061 if (!revents) {
1062 /* No activity for this FD (poll implementation). */
1063 continue;
1064 }
1065
5eb91c98 1066 /* Thread quit pipe has been closed. Killing thread. */
d0b96690 1067 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
5eb91c98 1068 if (ret) {
139ac872
MD
1069 err = 0;
1070 goto exit;
5eb91c98 1071 }
7a485870 1072
5eb91c98 1073 /* Check for data on kernel pipe */
03e43155
MD
1074 if (revents & LPOLLIN) {
1075 if (pollfd == kernel_poll_pipe[0]) {
1076 (void) lttng_read(kernel_poll_pipe[0],
1077 &tmp, 1);
1078 /*
1079 * Ret value is useless here, if this pipe gets any actions an
1080 * update is required anyway.
1081 */
1082 update_poll_flag = 1;
1083 continue;
1084 } else {
1085 /*
1086 * New CPU detected by the kernel. Adding kernel stream to
1087 * kernel session and updating the kernel consumer
1088 */
7f032058 1089 ret = update_kernel_stream(pollfd);
5eb91c98
DG
1090 if (ret < 0) {
1091 continue;
1092 }
1093 break;
7a485870 1094 }
03e43155
MD
1095 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1096 update_poll_flag = 1;
1097 continue;
1098 } else {
1099 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
1100 goto error;
7a485870
DG
1101 }
1102 }
1103 }
1104
139ac872 1105exit:
7a485870 1106error:
5eb91c98 1107 lttng_poll_clean(&events);
76d7553f 1108error_poll_create:
6993eeb3 1109error_testpoint:
6620da75
DG
1110 utils_close_pipe(kernel_poll_pipe);
1111 kernel_poll_pipe[0] = kernel_poll_pipe[1] = -1;
139ac872 1112 if (err) {
840cb59c 1113 health_error();
139ac872 1114 ERR("Health error occurred in %s", __func__);
6620da75
DG
1115 WARN("Kernel thread died unexpectedly. "
1116 "Kernel tracing can continue but CPU hotplug is disabled.");
139ac872 1117 }
8782cc74 1118 health_unregister(health_sessiond);
76d7553f 1119 DBG("Kernel thread dying");
7a485870
DG
1120 return NULL;
1121}
1122
a23ec3a7
DG
1123/*
1124 * Signal pthread condition of the consumer data that the thread.
1125 */
1126static void signal_consumer_condition(struct consumer_data *data, int state)
1127{
1128 pthread_mutex_lock(&data->cond_mutex);
1129
1130 /*
1131 * The state is set before signaling. It can be any value, it's the waiter
1132 * job to correctly interpret this condition variable associated to the
1133 * consumer pthread_cond.
1134 *
1135 * A value of 0 means that the corresponding thread of the consumer data
1136 * was not started. 1 indicates that the thread has started and is ready
1137 * for action. A negative value means that there was an error during the
1138 * thread bootstrap.
1139 */
1140 data->consumer_thread_is_ready = state;
1141 (void) pthread_cond_signal(&data->cond);
1142
1143 pthread_mutex_unlock(&data->cond_mutex);
1144}
1145
1d4b027a 1146/*
3bd1e081 1147 * This thread manage the consumer error sent back to the session daemon.
1d4b027a 1148 */
3bd1e081 1149static void *thread_manage_consumer(void *data)
1d4b027a 1150{
42fc1d0b 1151 int sock = -1, i, ret, pollfd, err = -1, should_quit = 0;
5eb91c98 1152 uint32_t revents, nb_fd;
1d4b027a 1153 enum lttcomm_return_code code;
5eb91c98 1154 struct lttng_poll_event events;
3bd1e081 1155 struct consumer_data *consumer_data = data;
b3530820 1156 struct consumer_socket *cmd_socket_wrapper = NULL;
1d4b027a 1157
3bd1e081 1158 DBG("[thread] Manage consumer started");
1d4b027a 1159
34c1e15a
MD
1160 rcu_register_thread();
1161 rcu_thread_online();
1162
6c71277b 1163 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_CONSUMER);
927ca06a 1164
855060f8 1165 health_code_update();
9449cc75 1166
5eb91c98 1167 /*
331744e3
JD
1168 * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the
1169 * metadata_sock. Nothing more will be added to this poll set.
5eb91c98 1170 */
331744e3 1171 ret = sessiond_set_thread_pollset(&events, 3);
5eb91c98 1172 if (ret < 0) {
76d7553f 1173 goto error_poll;
5eb91c98 1174 }
273ea72c 1175
edb8b045
DG
1176 /*
1177 * The error socket here is already in a listening state which was done
1178 * just before spawning this thread to avoid a race between the consumer
1179 * daemon exec trying to connect and the listen() call.
1180 */
3bd1e081 1181 ret = lttng_poll_add(&events, consumer_data->err_sock, LPOLLIN | LPOLLRDHUP);
5eb91c98
DG
1182 if (ret < 0) {
1183 goto error;
1184 }
1185
840cb59c 1186 health_code_update();
44a5e5eb 1187
331744e3 1188 /* Infinite blocking call, waiting for transmission */
88f2b785 1189restart:
a78af745 1190 health_poll_entry();
8ac94142 1191
e547b070 1192 if (testpoint(sessiond_thread_manage_consumer)) {
6993eeb3
CB
1193 goto error;
1194 }
8ac94142 1195
5eb91c98 1196 ret = lttng_poll_wait(&events, -1);
a78af745 1197 health_poll_exit();
273ea72c 1198 if (ret < 0) {
88f2b785
MD
1199 /*
1200 * Restart interrupted system call.
1201 */
1202 if (errno == EINTR) {
1203 goto restart;
1204 }
273ea72c
DG
1205 goto error;
1206 }
1207
0d9c5d77
DG
1208 nb_fd = ret;
1209
5eb91c98
DG
1210 for (i = 0; i < nb_fd; i++) {
1211 /* Fetch once the poll data */
1212 revents = LTTNG_POLL_GETEV(&events, i);
1213 pollfd = LTTNG_POLL_GETFD(&events, i);
1214
840cb59c 1215 health_code_update();
44a5e5eb 1216
fd20dac9
MD
1217 if (!revents) {
1218 /* No activity for this FD (poll implementation). */
1219 continue;
1220 }
1221
5eb91c98 1222 /* Thread quit pipe has been closed. Killing thread. */
d0b96690 1223 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
5eb91c98 1224 if (ret) {
139ac872
MD
1225 err = 0;
1226 goto exit;
5eb91c98
DG
1227 }
1228
1229 /* Event on the registration socket */
3bd1e081 1230 if (pollfd == consumer_data->err_sock) {
03e43155
MD
1231 if (revents & LPOLLIN) {
1232 continue;
1233 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
3bd1e081 1234 ERR("consumer err socket poll error");
5eb91c98 1235 goto error;
03e43155
MD
1236 } else {
1237 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
1238 goto error;
5eb91c98
DG
1239 }
1240 }
273ea72c
DG
1241 }
1242
3bd1e081 1243 sock = lttcomm_accept_unix_sock(consumer_data->err_sock);
1d4b027a
DG
1244 if (sock < 0) {
1245 goto error;
1246 }
1247
b662582b
DG
1248 /*
1249 * Set the CLOEXEC flag. Return code is useless because either way, the
1250 * show must go on.
1251 */
1252 (void) utils_set_fd_cloexec(sock);
1253
840cb59c 1254 health_code_update();
44a5e5eb 1255
3bd1e081 1256 DBG2("Receiving code from consumer err_sock");
ee0b0061 1257
712ea556 1258 /* Getting status code from kconsumerd */
54d01ffb
DG
1259 ret = lttcomm_recv_unix_sock(sock, &code,
1260 sizeof(enum lttcomm_return_code));
1d4b027a
DG
1261 if (ret <= 0) {
1262 goto error;
1263 }
1264
840cb59c 1265 health_code_update();
b3530820 1266 if (code != LTTCOMM_CONSUMERD_COMMAND_SOCK_READY) {
3bd1e081 1267 ERR("consumer error when waiting for SOCK_READY : %s",
1d4b027a
DG
1268 lttcomm_get_readable_code(-code));
1269 goto error;
1270 }
1271
b3530820
JG
1272 /* Connect both command and metadata sockets. */
1273 consumer_data->cmd_sock =
1274 lttcomm_connect_unix_sock(
1275 consumer_data->cmd_unix_sock_path);
1276 consumer_data->metadata_fd =
1277 lttcomm_connect_unix_sock(
1278 consumer_data->cmd_unix_sock_path);
1279 if (consumer_data->cmd_sock < 0 || consumer_data->metadata_fd < 0) {
1280 PERROR("consumer connect cmd socket");
1281 /* On error, signal condition and quit. */
1282 signal_consumer_condition(consumer_data, -1);
1283 goto error;
1284 }
1285
1286 consumer_data->metadata_sock.fd_ptr = &consumer_data->metadata_fd;
1287
1288 /* Create metadata socket lock. */
1289 consumer_data->metadata_sock.lock = zmalloc(sizeof(pthread_mutex_t));
1290 if (consumer_data->metadata_sock.lock == NULL) {
1291 PERROR("zmalloc pthread mutex");
1292 goto error;
1293 }
1294 pthread_mutex_init(consumer_data->metadata_sock.lock, NULL);
1295
1296 DBG("Consumer command socket ready (fd: %d", consumer_data->cmd_sock);
1297 DBG("Consumer metadata socket ready (fd: %d)",
1298 consumer_data->metadata_fd);
1299
1300 /*
1301 * Remove the consumerd error sock since we've established a connection.
1302 */
3bd1e081 1303 ret = lttng_poll_del(&events, consumer_data->err_sock);
72079cae 1304 if (ret < 0) {
72079cae
DG
1305 goto error;
1306 }
1307
331744e3 1308 /* Add new accepted error socket. */
5eb91c98
DG
1309 ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLRDHUP);
1310 if (ret < 0) {
72079cae 1311 goto error;
5eb91c98
DG
1312 }
1313
331744e3 1314 /* Add metadata socket that is successfully connected. */
4ce514c4 1315 ret = lttng_poll_add(&events, consumer_data->metadata_fd,
331744e3
JD
1316 LPOLLIN | LPOLLRDHUP);
1317 if (ret < 0) {
1318 goto error;
1319 }
1320
840cb59c 1321 health_code_update();
44a5e5eb 1322
b3530820 1323 /*
62c43103
JD
1324 * Transfer the write-end of the channel monitoring and rotate pipe
1325 * to the consumer by issuing a SET_CHANNEL_MONITOR_PIPE and
1326 * SET_CHANNEL_ROTATE_PIPE commands.
b3530820
JG
1327 */
1328 cmd_socket_wrapper = consumer_allocate_socket(&consumer_data->cmd_sock);
1329 if (!cmd_socket_wrapper) {
1330 goto error;
1331 }
3e4dc117 1332 cmd_socket_wrapper->lock = &consumer_data->lock;
b3530820
JG
1333
1334 ret = consumer_send_channel_monitor_pipe(cmd_socket_wrapper,
1335 consumer_data->channel_monitor_pipe);
1336 if (ret) {
1337 goto error;
1338 }
62c43103
JD
1339
1340 ret = consumer_send_channel_rotate_pipe(cmd_socket_wrapper,
1341 consumer_data->channel_rotate_pipe);
1342 if (ret) {
1343 goto error;
1344 }
1345
b3530820
JG
1346 /* Discard the socket wrapper as it is no longer needed. */
1347 consumer_destroy_socket(cmd_socket_wrapper);
1348 cmd_socket_wrapper = NULL;
1349
1350 /* The thread is completely initialized, signal that it is ready. */
1351 signal_consumer_condition(consumer_data, 1);
1352
331744e3 1353 /* Infinite blocking call, waiting for transmission */
88f2b785 1354restart_poll:
331744e3 1355 while (1) {
42fc1d0b
DG
1356 health_code_update();
1357
1358 /* Exit the thread because the thread quit pipe has been triggered. */
1359 if (should_quit) {
1360 /* Not a health error. */
1361 err = 0;
1362 goto exit;
1363 }
1364
331744e3
JD
1365 health_poll_entry();
1366 ret = lttng_poll_wait(&events, -1);
1367 health_poll_exit();
1368 if (ret < 0) {
1369 /*
1370 * Restart interrupted system call.
1371 */
1372 if (errno == EINTR) {
1373 goto restart_poll;
1374 }
1375 goto error;
88f2b785 1376 }
72079cae 1377
331744e3 1378 nb_fd = ret;
0d9c5d77 1379
331744e3
JD
1380 for (i = 0; i < nb_fd; i++) {
1381 /* Fetch once the poll data */
1382 revents = LTTNG_POLL_GETEV(&events, i);
1383 pollfd = LTTNG_POLL_GETFD(&events, i);
5eb91c98 1384
331744e3 1385 health_code_update();
44a5e5eb 1386
fd20dac9
MD
1387 if (!revents) {
1388 /* No activity for this FD (poll implementation). */
1389 continue;
1390 }
1391
42fc1d0b
DG
1392 /*
1393 * Thread quit pipe has been triggered, flag that we should stop
1394 * but continue the current loop to handle potential data from
1395 * consumer.
1396 */
1397 should_quit = sessiond_check_thread_quit_pipe(pollfd, revents);
5eb91c98 1398
331744e3
JD
1399 if (pollfd == sock) {
1400 /* Event on the consumerd socket */
03e43155
MD
1401 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)
1402 && !(revents & LPOLLIN)) {
331744e3
JD
1403 ERR("consumer err socket second poll error");
1404 goto error;
1405 }
1406 health_code_update();
1407 /* Wait for any kconsumerd error */
1408 ret = lttcomm_recv_unix_sock(sock, &code,
1409 sizeof(enum lttcomm_return_code));
1410 if (ret <= 0) {
1411 ERR("consumer closed the command socket");
1412 goto error;
1413 }
1414
1415 ERR("consumer return code : %s",
1416 lttcomm_get_readable_code(-code));
1417
1418 goto exit;
4ce514c4 1419 } else if (pollfd == consumer_data->metadata_fd) {
03e43155
MD
1420 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)
1421 && !(revents & LPOLLIN)) {
1422 ERR("consumer err metadata socket second poll error");
1423 goto error;
1424 }
331744e3
JD
1425 /* UST metadata requests */
1426 ret = ust_consumer_metadata_request(
1427 &consumer_data->metadata_sock);
1428 if (ret < 0) {
1429 ERR("Handling metadata request");
1430 goto error;
1431 }
5eb91c98 1432 }
42fc1d0b 1433 /* No need for an else branch all FDs are tested prior. */
5eb91c98 1434 }
331744e3 1435 health_code_update();
5eb91c98
DG
1436 }
1437
139ac872 1438exit:
1d4b027a 1439error:
fdadac08
DG
1440 /*
1441 * We lock here because we are about to close the sockets and some other
92db7cdc
DG
1442 * thread might be using them so get exclusive access which will abort all
1443 * other consumer command by other threads.
fdadac08
DG
1444 */
1445 pthread_mutex_lock(&consumer_data->lock);
1446
5c827ce0
DG
1447 /* Immediately set the consumerd state to stopped */
1448 if (consumer_data->type == LTTNG_CONSUMER_KERNEL) {
1449 uatomic_set(&kernel_consumerd_state, CONSUMER_ERROR);
1450 } else if (consumer_data->type == LTTNG_CONSUMER64_UST ||
1451 consumer_data->type == LTTNG_CONSUMER32_UST) {
1452 uatomic_set(&ust_consumerd_state, CONSUMER_ERROR);
1453 } else {
1454 /* Code flow error... */
1455 assert(0);
1456 }
1457
76d7553f
MD
1458 if (consumer_data->err_sock >= 0) {
1459 ret = close(consumer_data->err_sock);
1460 if (ret) {
1461 PERROR("close");
1462 }
a76cbd9f 1463 consumer_data->err_sock = -1;
76d7553f
MD
1464 }
1465 if (consumer_data->cmd_sock >= 0) {
1466 ret = close(consumer_data->cmd_sock);
1467 if (ret) {
1468 PERROR("close");
1469 }
a76cbd9f 1470 consumer_data->cmd_sock = -1;
76d7553f 1471 }
96544455
SS
1472 if (consumer_data->metadata_sock.fd_ptr &&
1473 *consumer_data->metadata_sock.fd_ptr >= 0) {
9363801e 1474 ret = close(*consumer_data->metadata_sock.fd_ptr);
331744e3
JD
1475 if (ret) {
1476 PERROR("close");
1477 }
1478 }
76d7553f
MD
1479 if (sock >= 0) {
1480 ret = close(sock);
1481 if (ret) {
1482 PERROR("close");
1483 }
1484 }
273ea72c 1485
3bd1e081
MD
1486 unlink(consumer_data->err_unix_sock_path);
1487 unlink(consumer_data->cmd_unix_sock_path);
fdadac08 1488 pthread_mutex_unlock(&consumer_data->lock);
92db7cdc 1489
fdadac08 1490 /* Cleanup metadata socket mutex. */
96544455
SS
1491 if (consumer_data->metadata_sock.lock) {
1492 pthread_mutex_destroy(consumer_data->metadata_sock.lock);
1493 free(consumer_data->metadata_sock.lock);
1494 }
5eb91c98 1495 lttng_poll_clean(&events);
b3530820
JG
1496
1497 if (cmd_socket_wrapper) {
1498 consumer_destroy_socket(cmd_socket_wrapper);
1499 }
76d7553f 1500error_poll:
139ac872 1501 if (err) {
840cb59c 1502 health_error();
139ac872
MD
1503 ERR("Health error occurred in %s", __func__);
1504 }
8782cc74 1505 health_unregister(health_sessiond);
76d7553f 1506 DBG("consumer thread cleanup completed");
0177d773 1507
34c1e15a
MD
1508 rcu_thread_offline();
1509 rcu_unregister_thread();
1510
5eb91c98 1511 return NULL;
099e26bd
DG
1512}
1513
099e26bd 1514/*
81f04d5f
JG
1515 * This thread receives application command sockets (FDs) on the
1516 * apps_cmd_pipe and waits (polls) on them until they are closed
1517 * or an error occurs.
1518 *
1519 * At that point, it flushes the data (tracing and metadata) associated
1520 * with this application and tears down ust app sessions and other
1521 * associated data structures through ust_app_unregister().
1522 *
1523 * Note that this thread never sends commands to the applications
1524 * through the command sockets; it merely listens for hang-ups
1525 * and errors on those sockets and cleans-up as they occur.
1d4b027a
DG
1526 */
1527static void *thread_manage_apps(void *data)
099e26bd 1528{
139ac872 1529 int i, ret, pollfd, err = -1;
6cd525e8 1530 ssize_t size_ret;
5eb91c98 1531 uint32_t revents, nb_fd;
5eb91c98 1532 struct lttng_poll_event events;
099e26bd
DG
1533
1534 DBG("[thread] Manage application started");
1535
f6a9efaa
DG
1536 rcu_register_thread();
1537 rcu_thread_online();
1538
6c71277b 1539 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_MANAGE);
927ca06a 1540
e547b070 1541 if (testpoint(sessiond_thread_manage_apps)) {
6993eeb3
CB
1542 goto error_testpoint;
1543 }
1544
840cb59c 1545 health_code_update();
44a5e5eb 1546
d0b96690 1547 ret = sessiond_set_thread_pollset(&events, 2);
5eb91c98 1548 if (ret < 0) {
76d7553f 1549 goto error_poll_create;
5eb91c98 1550 }
099e26bd 1551
5eb91c98
DG
1552 ret = lttng_poll_add(&events, apps_cmd_pipe[0], LPOLLIN | LPOLLRDHUP);
1553 if (ret < 0) {
1554 goto error;
1555 }
099e26bd 1556
e547b070 1557 if (testpoint(sessiond_thread_manage_apps_before_loop)) {
6993eeb3
CB
1558 goto error;
1559 }
8ac94142 1560
840cb59c 1561 health_code_update();
44a5e5eb 1562
5eb91c98 1563 while (1) {
7fa2082e 1564 DBG("Apps thread polling");
099e26bd
DG
1565
1566 /* Inifinite blocking call, waiting for transmission */
88f2b785 1567 restart:
a78af745 1568 health_poll_entry();
5eb91c98 1569 ret = lttng_poll_wait(&events, -1);
7fa2082e
MD
1570 DBG("Apps thread return from poll on %d fds",
1571 LTTNG_POLL_GETNB(&events));
a78af745 1572 health_poll_exit();
099e26bd 1573 if (ret < 0) {
88f2b785
MD
1574 /*
1575 * Restart interrupted system call.
1576 */
1577 if (errno == EINTR) {
1578 goto restart;
1579 }
099e26bd
DG
1580 goto error;
1581 }
1582
0d9c5d77
DG
1583 nb_fd = ret;
1584
5eb91c98
DG
1585 for (i = 0; i < nb_fd; i++) {
1586 /* Fetch once the poll data */
1587 revents = LTTNG_POLL_GETEV(&events, i);
1588 pollfd = LTTNG_POLL_GETFD(&events, i);
1589
840cb59c 1590 health_code_update();
44a5e5eb 1591
fd20dac9
MD
1592 if (!revents) {
1593 /* No activity for this FD (poll implementation). */
1594 continue;
1595 }
1596
5eb91c98 1597 /* Thread quit pipe has been closed. Killing thread. */
d0b96690 1598 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
5eb91c98 1599 if (ret) {
139ac872
MD
1600 err = 0;
1601 goto exit;
5eb91c98 1602 }
099e26bd 1603
5eb91c98
DG
1604 /* Inspect the apps cmd pipe */
1605 if (pollfd == apps_cmd_pipe[0]) {
03e43155 1606 if (revents & LPOLLIN) {
d0b96690
DG
1607 int sock;
1608
5eb91c98 1609 /* Empty pipe */
6cd525e8
MD
1610 size_ret = lttng_read(apps_cmd_pipe[0], &sock, sizeof(sock));
1611 if (size_ret < sizeof(sock)) {
76d7553f 1612 PERROR("read apps cmd pipe");
5eb91c98
DG
1613 goto error;
1614 }
099e26bd 1615
840cb59c 1616 health_code_update();
44a5e5eb 1617
ffe60014 1618 /*
03e43155
MD
1619 * Since this is a command socket (write then read),
1620 * we only monitor the error events of the socket.
ffe60014 1621 */
d0b96690
DG
1622 ret = lttng_poll_add(&events, sock,
1623 LPOLLERR | LPOLLHUP | LPOLLRDHUP);
1624 if (ret < 0) {
5eb91c98 1625 goto error;
e0c7ec2b 1626 }
acc7b41b 1627
d0b96690 1628 DBG("Apps with sock %d added to poll set", sock);
03e43155
MD
1629 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1630 ERR("Apps command pipe error");
1631 goto error;
1632 } else {
1633 ERR("Unknown poll events %u for sock %d", revents, pollfd);
1634 goto error;
0177d773 1635 }
5eb91c98
DG
1636 } else {
1637 /*
54d01ffb
DG
1638 * At this point, we know that a registered application made
1639 * the event at poll_wait.
5eb91c98
DG
1640 */
1641 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1642 /* Removing from the poll set */
1643 ret = lttng_poll_del(&events, pollfd);
1644 if (ret < 0) {
1645 goto error;
1646 }
099e26bd 1647
b9d9b220 1648 /* Socket closed on remote end. */
56fff090 1649 ust_app_unregister(pollfd);
03e43155
MD
1650 } else {
1651 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
1652 goto error;
5eb91c98 1653 }
099e26bd 1654 }
44a5e5eb 1655
840cb59c 1656 health_code_update();
099e26bd 1657 }
099e26bd
DG
1658 }
1659
139ac872 1660exit:
099e26bd 1661error:
5eb91c98 1662 lttng_poll_clean(&events);
76d7553f 1663error_poll_create:
6993eeb3 1664error_testpoint:
6620da75
DG
1665 utils_close_pipe(apps_cmd_pipe);
1666 apps_cmd_pipe[0] = apps_cmd_pipe[1] = -1;
1667
1668 /*
1669 * We don't clean the UST app hash table here since already registered
1670 * applications can still be controlled so let them be until the session
1671 * daemon dies or the applications stop.
1672 */
1673
139ac872 1674 if (err) {
840cb59c 1675 health_error();
139ac872
MD
1676 ERR("Health error occurred in %s", __func__);
1677 }
8782cc74 1678 health_unregister(health_sessiond);
76d7553f 1679 DBG("Application communication apps thread cleanup complete");
f6a9efaa
DG
1680 rcu_thread_offline();
1681 rcu_unregister_thread();
099e26bd
DG
1682 return NULL;
1683}
1684
d0b96690 1685/*
d88aee68
DG
1686 * Send a socket to a thread This is called from the dispatch UST registration
1687 * thread once all sockets are set for the application.
d0b96690 1688 *
b85dc84c
DG
1689 * The sock value can be invalid, we don't really care, the thread will handle
1690 * it and make the necessary cleanup if so.
1691 *
d0b96690
DG
1692 * On success, return 0 else a negative value being the errno message of the
1693 * write().
1694 */
d88aee68 1695static int send_socket_to_thread(int fd, int sock)
d0b96690 1696{
6cd525e8 1697 ssize_t ret;
d0b96690 1698
b85dc84c
DG
1699 /*
1700 * It's possible that the FD is set as invalid with -1 concurrently just
1701 * before calling this function being a shutdown state of the thread.
1702 */
1703 if (fd < 0) {
1704 ret = -EBADF;
1705 goto error;
1706 }
d0b96690 1707
6cd525e8
MD
1708 ret = lttng_write(fd, &sock, sizeof(sock));
1709 if (ret < sizeof(sock)) {
d88aee68 1710 PERROR("write apps pipe %d", fd);
d0b96690
DG
1711 if (ret < 0) {
1712 ret = -errno;
1713 }
1714 goto error;
1715 }
1716
1717 /* All good. Don't send back the write positive ret value. */
1718 ret = 0;
1719error:
6cd525e8 1720 return (int) ret;
d0b96690
DG
1721}
1722
f45e313d
DG
1723/*
1724 * Sanitize the wait queue of the dispatch registration thread meaning removing
1725 * invalid nodes from it. This is to avoid memory leaks for the case the UST
1726 * notify socket is never received.
1727 */
1728static void sanitize_wait_queue(struct ust_reg_wait_queue *wait_queue)
1729{
1730 int ret, nb_fd = 0, i;
1731 unsigned int fd_added = 0;
1732 struct lttng_poll_event events;
1733 struct ust_reg_wait_node *wait_node = NULL, *tmp_wait_node;
1734
1735 assert(wait_queue);
1736
1737 lttng_poll_init(&events);
1738
1739 /* Just skip everything for an empty queue. */
1740 if (!wait_queue->count) {
1741 goto end;
1742 }
1743
1744 ret = lttng_poll_create(&events, wait_queue->count, LTTNG_CLOEXEC);
1745 if (ret < 0) {
1746 goto error_create;
1747 }
1748
1749 cds_list_for_each_entry_safe(wait_node, tmp_wait_node,
1750 &wait_queue->head, head) {
1751 assert(wait_node->app);
1752 ret = lttng_poll_add(&events, wait_node->app->sock,
1753 LPOLLHUP | LPOLLERR);
1754 if (ret < 0) {
1755 goto error;
1756 }
1757
1758 fd_added = 1;
1759 }
1760
1761 if (!fd_added) {
1762 goto end;
1763 }
1764
1765 /*
1766 * Poll but don't block so we can quickly identify the faulty events and
1767 * clean them afterwards from the wait queue.
1768 */
1769 ret = lttng_poll_wait(&events, 0);
1770 if (ret < 0) {
1771 goto error;
1772 }
1773 nb_fd = ret;
1774
1775 for (i = 0; i < nb_fd; i++) {
1776 /* Get faulty FD. */
1777 uint32_t revents = LTTNG_POLL_GETEV(&events, i);
1778 int pollfd = LTTNG_POLL_GETFD(&events, i);
1779
fd20dac9
MD
1780 if (!revents) {
1781 /* No activity for this FD (poll implementation). */
1782 continue;
1783 }
1784
f45e313d
DG
1785 cds_list_for_each_entry_safe(wait_node, tmp_wait_node,
1786 &wait_queue->head, head) {
1787 if (pollfd == wait_node->app->sock &&
1788 (revents & (LPOLLHUP | LPOLLERR))) {
1789 cds_list_del(&wait_node->head);
1790 wait_queue->count--;
1791 ust_app_destroy(wait_node->app);
1792 free(wait_node);
48b40bcf
JG
1793 /*
1794 * Silence warning of use-after-free in
1795 * cds_list_for_each_entry_safe which uses
1796 * __typeof__(*wait_node).
1797 */
1798 wait_node = NULL;
f45e313d 1799 break;
03e43155
MD
1800 } else {
1801 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
1802 goto error;
f45e313d
DG
1803 }
1804 }
1805 }
1806
1807 if (nb_fd > 0) {
1808 DBG("Wait queue sanitized, %d node were cleaned up", nb_fd);
1809 }
1810
1811end:
1812 lttng_poll_clean(&events);
1813 return;
1814
1815error:
1816 lttng_poll_clean(&events);
1817error_create:
1818 ERR("Unable to sanitize wait queue");
1819 return;
1820}
1821
099e26bd
DG
1822/*
1823 * Dispatch request from the registration threads to the application
1824 * communication thread.
1825 */
1826static void *thread_dispatch_ust_registration(void *data)
1827{
12e2b881 1828 int ret, err = -1;
8bdee6e2 1829 struct cds_wfcq_node *node;
099e26bd 1830 struct ust_command *ust_cmd = NULL;
f45e313d
DG
1831 struct ust_reg_wait_node *wait_node = NULL, *tmp_wait_node;
1832 struct ust_reg_wait_queue wait_queue = {
1833 .count = 0,
1834 };
d0b96690 1835
967e3668
MD
1836 rcu_register_thread();
1837
6c71277b 1838 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_REG_DISPATCH);
12e2b881 1839
9ad42ec1
MD
1840 if (testpoint(sessiond_thread_app_reg_dispatch)) {
1841 goto error_testpoint;
1842 }
1843
12e2b881
MD
1844 health_code_update();
1845
f45e313d 1846 CDS_INIT_LIST_HEAD(&wait_queue.head);
099e26bd
DG
1847
1848 DBG("[thread] Dispatch UST command started");
1849
0ed3b1a8 1850 for (;;) {
12e2b881
MD
1851 health_code_update();
1852
099e26bd
DG
1853 /* Atomically prepare the queue futex */
1854 futex_nto1_prepare(&ust_cmd_queue.futex);
1855
0ed3b1a8
MD
1856 if (CMM_LOAD_SHARED(dispatch_thread_exit)) {
1857 break;
1858 }
1859
099e26bd 1860 do {
d0b96690 1861 struct ust_app *app = NULL;
7972aab2 1862 ust_cmd = NULL;
d0b96690 1863
f45e313d
DG
1864 /*
1865 * Make sure we don't have node(s) that have hung up before receiving
1866 * the notify socket. This is to clean the list in order to avoid
1867 * memory leaks from notify socket that are never seen.
1868 */
1869 sanitize_wait_queue(&wait_queue);
1870
12e2b881 1871 health_code_update();
099e26bd 1872 /* Dequeue command for registration */
8bdee6e2 1873 node = cds_wfcq_dequeue_blocking(&ust_cmd_queue.head, &ust_cmd_queue.tail);
099e26bd 1874 if (node == NULL) {
00a17c97 1875 DBG("Woken up but nothing in the UST command queue");
099e26bd
DG
1876 /* Continue thread execution */
1877 break;
1878 }
1879
1880 ust_cmd = caa_container_of(node, struct ust_command, node);
1881
2f50c8a3
DG
1882 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1883 " gid:%d sock:%d name:%s (version %d.%d)",
1884 ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid,
1885 ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid,
1886 ust_cmd->sock, ust_cmd->reg_msg.name,
1887 ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor);
d0b96690
DG
1888
1889 if (ust_cmd->reg_msg.type == USTCTL_SOCKET_CMD) {
1890 wait_node = zmalloc(sizeof(*wait_node));
1891 if (!wait_node) {
1892 PERROR("zmalloc wait_node dispatch");
020d7f60
DG
1893 ret = close(ust_cmd->sock);
1894 if (ret < 0) {
1895 PERROR("close ust sock dispatch %d", ust_cmd->sock);
1896 }
51dec90d 1897 lttng_fd_put(LTTNG_FD_APPS, 1);
7972aab2 1898 free(ust_cmd);
d0b96690
DG
1899 goto error;
1900 }
1901 CDS_INIT_LIST_HEAD(&wait_node->head);
1902
1903 /* Create application object if socket is CMD. */
1904 wait_node->app = ust_app_create(&ust_cmd->reg_msg,
1905 ust_cmd->sock);
1906 if (!wait_node->app) {
1907 ret = close(ust_cmd->sock);
1908 if (ret < 0) {
1909 PERROR("close ust sock dispatch %d", ust_cmd->sock);
6620da75 1910 }
51dec90d 1911 lttng_fd_put(LTTNG_FD_APPS, 1);
d88aee68 1912 free(wait_node);
7972aab2 1913 free(ust_cmd);
d0b96690
DG
1914 continue;
1915 }
1916 /*
1917 * Add application to the wait queue so we can set the notify
1918 * socket before putting this object in the global ht.
1919 */
f45e313d
DG
1920 cds_list_add(&wait_node->head, &wait_queue.head);
1921 wait_queue.count++;
d0b96690 1922
7972aab2 1923 free(ust_cmd);
d0b96690
DG
1924 /*
1925 * We have to continue here since we don't have the notify
1926 * socket and the application MUST be added to the hash table
1927 * only at that moment.
1928 */
1929 continue;
1930 } else {
1931 /*
1932 * Look for the application in the local wait queue and set the
1933 * notify socket if found.
1934 */
d88aee68 1935 cds_list_for_each_entry_safe(wait_node, tmp_wait_node,
f45e313d 1936 &wait_queue.head, head) {
12e2b881 1937 health_code_update();
d0b96690
DG
1938 if (wait_node->app->pid == ust_cmd->reg_msg.pid) {
1939 wait_node->app->notify_sock = ust_cmd->sock;
1940 cds_list_del(&wait_node->head);
f45e313d 1941 wait_queue.count--;
d0b96690
DG
1942 app = wait_node->app;
1943 free(wait_node);
1944 DBG3("UST app notify socket %d is set", ust_cmd->sock);
1945 break;
1946 }
1947 }
020d7f60
DG
1948
1949 /*
1950 * With no application at this stage the received socket is
1951 * basically useless so close it before we free the cmd data
1952 * structure for good.
1953 */
1954 if (!app) {
1955 ret = close(ust_cmd->sock);
1956 if (ret < 0) {
1957 PERROR("close ust sock dispatch %d", ust_cmd->sock);
1958 }
51dec90d 1959 lttng_fd_put(LTTNG_FD_APPS, 1);
020d7f60 1960 }
7972aab2 1961 free(ust_cmd);
d0b96690
DG
1962 }
1963
1964 if (app) {
d0b96690
DG
1965 /*
1966 * @session_lock_list
1967 *
1968 * Lock the global session list so from the register up to the
1969 * registration done message, no thread can see the application
1970 * and change its state.
1971 */
1972 session_lock_list();
1973 rcu_read_lock();
d88aee68 1974
d0b96690
DG
1975 /*
1976 * Add application to the global hash table. This needs to be
1977 * done before the update to the UST registry can locate the
1978 * application.
1979 */
1980 ust_app_add(app);
d88aee68
DG
1981
1982 /* Set app version. This call will print an error if needed. */
1983 (void) ust_app_version(app);
1984
1985 /* Send notify socket through the notify pipe. */
1986 ret = send_socket_to_thread(apps_cmd_notify_pipe[1],
1987 app->notify_sock);
1988 if (ret < 0) {
1989 rcu_read_unlock();
1990 session_unlock_list();
b85dc84c
DG
1991 /*
1992 * No notify thread, stop the UST tracing. However, this is
1993 * not an internal error of the this thread thus setting
1994 * the health error code to a normal exit.
1995 */
1996 err = 0;
d88aee68 1997 goto error;
6620da75 1998 }
d88aee68 1999
d0b96690
DG
2000 /*
2001 * Update newly registered application with the tracing
2002 * registry info already enabled information.
2003 */
2004 update_ust_app(app->sock);
d88aee68
DG
2005
2006 /*
2007 * Don't care about return value. Let the manage apps threads
2008 * handle app unregistration upon socket close.
2009 */
fb45065e 2010 (void) ust_app_register_done(app);
d88aee68
DG
2011
2012 /*
2013 * Even if the application socket has been closed, send the app
2014 * to the thread and unregistration will take place at that
2015 * place.
2016 */
2017 ret = send_socket_to_thread(apps_cmd_pipe[1], app->sock);
d0b96690 2018 if (ret < 0) {
d88aee68
DG
2019 rcu_read_unlock();
2020 session_unlock_list();
b85dc84c
DG
2021 /*
2022 * No apps. thread, stop the UST tracing. However, this is
2023 * not an internal error of the this thread thus setting
2024 * the health error code to a normal exit.
2025 */
2026 err = 0;
d88aee68 2027 goto error;
d0b96690 2028 }
d88aee68 2029
d0b96690
DG
2030 rcu_read_unlock();
2031 session_unlock_list();
099e26bd 2032 }
099e26bd
DG
2033 } while (node != NULL);
2034
12e2b881 2035 health_poll_entry();
099e26bd
DG
2036 /* Futex wait on queue. Blocking call on futex() */
2037 futex_nto1_wait(&ust_cmd_queue.futex);
12e2b881 2038 health_poll_exit();
099e26bd 2039 }
12e2b881
MD
2040 /* Normal exit, no error */
2041 err = 0;
099e26bd
DG
2042
2043error:
d88aee68
DG
2044 /* Clean up wait queue. */
2045 cds_list_for_each_entry_safe(wait_node, tmp_wait_node,
f45e313d 2046 &wait_queue.head, head) {
d88aee68 2047 cds_list_del(&wait_node->head);
f45e313d 2048 wait_queue.count--;
d88aee68
DG
2049 free(wait_node);
2050 }
2051
772b8f4d
MD
2052 /* Empty command queue. */
2053 for (;;) {
2054 /* Dequeue command for registration */
2055 node = cds_wfcq_dequeue_blocking(&ust_cmd_queue.head, &ust_cmd_queue.tail);
2056 if (node == NULL) {
2057 break;
2058 }
2059 ust_cmd = caa_container_of(node, struct ust_command, node);
2060 ret = close(ust_cmd->sock);
2061 if (ret < 0) {
2062 PERROR("close ust sock exit dispatch %d", ust_cmd->sock);
2063 }
2064 lttng_fd_put(LTTNG_FD_APPS, 1);
2065 free(ust_cmd);
2066 }
2067
9ad42ec1 2068error_testpoint:
099e26bd 2069 DBG("Dispatch thread dying");
12e2b881
MD
2070 if (err) {
2071 health_error();
2072 ERR("Health error occurred in %s", __func__);
2073 }
8782cc74 2074 health_unregister(health_sessiond);
967e3668 2075 rcu_unregister_thread();
099e26bd
DG
2076 return NULL;
2077}
2078
2079/*
2080 * This thread manage application registration.
2081 */
2082static void *thread_registration_apps(void *data)
1d4b027a 2083{
139ac872 2084 int sock = -1, i, ret, pollfd, err = -1;
5eb91c98
DG
2085 uint32_t revents, nb_fd;
2086 struct lttng_poll_event events;
099e26bd
DG
2087 /*
2088 * Get allocated in this thread, enqueued to a global queue, dequeued and
2089 * freed in the manage apps thread.
2090 */
2091 struct ust_command *ust_cmd = NULL;
1d4b027a 2092
099e26bd 2093 DBG("[thread] Manage application registration started");
1d4b027a 2094
6c71277b 2095 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_REG);
927ca06a 2096
e547b070 2097 if (testpoint(sessiond_thread_registration_apps)) {
6993eeb3
CB
2098 goto error_testpoint;
2099 }
8ac94142 2100
1d4b027a
DG
2101 ret = lttcomm_listen_unix_sock(apps_sock);
2102 if (ret < 0) {
76d7553f 2103 goto error_listen;
1d4b027a
DG
2104 }
2105
5eb91c98
DG
2106 /*
2107 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
2108 * more will be added to this poll set.
2109 */
d0b96690 2110 ret = sessiond_set_thread_pollset(&events, 2);
5eb91c98 2111 if (ret < 0) {
76d7553f 2112 goto error_create_poll;
5eb91c98 2113 }
273ea72c 2114
5eb91c98
DG
2115 /* Add the application registration socket */
2116 ret = lttng_poll_add(&events, apps_sock, LPOLLIN | LPOLLRDHUP);
2117 if (ret < 0) {
76d7553f 2118 goto error_poll_add;
5eb91c98 2119 }
273ea72c 2120
1d4b027a 2121 /* Notify all applications to register */
0fdd1e2c
DG
2122 ret = notify_ust_apps(1);
2123 if (ret < 0) {
2124 ERR("Failed to notify applications or create the wait shared memory.\n"
54d01ffb
DG
2125 "Execution continues but there might be problem for already\n"
2126 "running applications that wishes to register.");
0fdd1e2c 2127 }
1d4b027a
DG
2128
2129 while (1) {
2130 DBG("Accepting application registration");
273ea72c
DG
2131
2132 /* Inifinite blocking call, waiting for transmission */
88f2b785 2133 restart:
a78af745 2134 health_poll_entry();
5eb91c98 2135 ret = lttng_poll_wait(&events, -1);
a78af745 2136 health_poll_exit();
273ea72c 2137 if (ret < 0) {
88f2b785
MD
2138 /*
2139 * Restart interrupted system call.
2140 */
2141 if (errno == EINTR) {
2142 goto restart;
2143 }
273ea72c
DG
2144 goto error;
2145 }
2146
0d9c5d77
DG
2147 nb_fd = ret;
2148
5eb91c98 2149 for (i = 0; i < nb_fd; i++) {
840cb59c 2150 health_code_update();
139ac872 2151
5eb91c98
DG
2152 /* Fetch once the poll data */
2153 revents = LTTNG_POLL_GETEV(&events, i);
2154 pollfd = LTTNG_POLL_GETFD(&events, i);
273ea72c 2155
fd20dac9
MD
2156 if (!revents) {
2157 /* No activity for this FD (poll implementation). */
2158 continue;
2159 }
2160
5eb91c98 2161 /* Thread quit pipe has been closed. Killing thread. */
d0b96690 2162 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
5eb91c98 2163 if (ret) {
139ac872
MD
2164 err = 0;
2165 goto exit;
90014c57 2166 }
1d4b027a 2167
5eb91c98
DG
2168 /* Event on the registration socket */
2169 if (pollfd == apps_sock) {
03e43155 2170 if (revents & LPOLLIN) {
5eb91c98
DG
2171 sock = lttcomm_accept_unix_sock(apps_sock);
2172 if (sock < 0) {
2173 goto error;
2174 }
099e26bd 2175
16c5c8fa
DG
2176 /*
2177 * Set socket timeout for both receiving and ending.
2178 * app_socket_timeout is in seconds, whereas
2179 * lttcomm_setsockopt_rcv_timeout and
2180 * lttcomm_setsockopt_snd_timeout expect msec as
2181 * parameter.
2182 */
e6142f2e 2183 if (config.app_socket_timeout >= 0) {
28ce0ff2 2184 (void) lttcomm_setsockopt_rcv_timeout(sock,
e6142f2e 2185 config.app_socket_timeout * 1000);
28ce0ff2 2186 (void) lttcomm_setsockopt_snd_timeout(sock,
e6142f2e 2187 config.app_socket_timeout * 1000);
28ce0ff2 2188 }
16c5c8fa 2189
b662582b
DG
2190 /*
2191 * Set the CLOEXEC flag. Return code is useless because
2192 * either way, the show must go on.
2193 */
2194 (void) utils_set_fd_cloexec(sock);
2195
5eb91c98 2196 /* Create UST registration command for enqueuing */
ba7f0ae5 2197 ust_cmd = zmalloc(sizeof(struct ust_command));
5eb91c98 2198 if (ust_cmd == NULL) {
76d7553f 2199 PERROR("ust command zmalloc");
41ed8e47
MD
2200 ret = close(sock);
2201 if (ret) {
2202 PERROR("close");
2203 }
5eb91c98
DG
2204 goto error;
2205 }
1d4b027a 2206
5eb91c98
DG
2207 /*
2208 * Using message-based transmissions to ensure we don't
2209 * have to deal with partially received messages.
2210 */
4063050c
MD
2211 ret = lttng_fd_get(LTTNG_FD_APPS, 1);
2212 if (ret < 0) {
2213 ERR("Exhausted file descriptors allowed for applications.");
2214 free(ust_cmd);
2215 ret = close(sock);
2216 if (ret) {
2217 PERROR("close");
2218 }
2219 sock = -1;
2220 continue;
2221 }
d88aee68 2222
840cb59c 2223 health_code_update();
d0b96690
DG
2224 ret = ust_app_recv_registration(sock, &ust_cmd->reg_msg);
2225 if (ret < 0) {
5eb91c98 2226 free(ust_cmd);
d0b96690 2227 /* Close socket of the application. */
76d7553f
MD
2228 ret = close(sock);
2229 if (ret) {
2230 PERROR("close");
2231 }
4063050c 2232 lttng_fd_put(LTTNG_FD_APPS, 1);
76d7553f 2233 sock = -1;
5eb91c98
DG
2234 continue;
2235 }
840cb59c 2236 health_code_update();
099e26bd 2237
5eb91c98 2238 ust_cmd->sock = sock;
34a2494f 2239 sock = -1;
099e26bd 2240
5eb91c98
DG
2241 DBG("UST registration received with pid:%d ppid:%d uid:%d"
2242 " gid:%d sock:%d name:%s (version %d.%d)",
2243 ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid,
2244 ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid,
2245 ust_cmd->sock, ust_cmd->reg_msg.name,
2246 ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor);
54d01ffb 2247
5eb91c98
DG
2248 /*
2249 * Lock free enqueue the registration request. The red pill
54d01ffb 2250 * has been taken! This apps will be part of the *system*.
5eb91c98 2251 */
8bdee6e2 2252 cds_wfcq_enqueue(&ust_cmd_queue.head, &ust_cmd_queue.tail, &ust_cmd->node);
5eb91c98
DG
2253
2254 /*
2255 * Wake the registration queue futex. Implicit memory
8bdee6e2 2256 * barrier with the exchange in cds_wfcq_enqueue.
5eb91c98
DG
2257 */
2258 futex_nto1_wake(&ust_cmd_queue.futex);
03e43155
MD
2259 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
2260 ERR("Register apps socket poll error");
2261 goto error;
2262 } else {
2263 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
2264 goto error;
5eb91c98
DG
2265 }
2266 }
90014c57 2267 }
1d4b027a
DG
2268 }
2269
139ac872 2270exit:
1d4b027a 2271error:
0fdd1e2c
DG
2272 /* Notify that the registration thread is gone */
2273 notify_ust_apps(0);
2274
a4b35e07 2275 if (apps_sock >= 0) {
76d7553f
MD
2276 ret = close(apps_sock);
2277 if (ret) {
2278 PERROR("close");
2279 }
a4b35e07 2280 }
46c3f085 2281 if (sock >= 0) {
76d7553f
MD
2282 ret = close(sock);
2283 if (ret) {
2284 PERROR("close");
2285 }
4063050c 2286 lttng_fd_put(LTTNG_FD_APPS, 1);
a4b35e07 2287 }
e6142f2e 2288 unlink(config.apps_unix_sock_path.value);
0fdd1e2c 2289
76d7553f 2290error_poll_add:
5eb91c98 2291 lttng_poll_clean(&events);
76d7553f
MD
2292error_listen:
2293error_create_poll:
6993eeb3 2294error_testpoint:
76d7553f 2295 DBG("UST Registration thread cleanup complete");
9ad42ec1
MD
2296 if (err) {
2297 health_error();
2298 ERR("Health error occurred in %s", __func__);
2299 }
8782cc74 2300 health_unregister(health_sessiond);
5eb91c98 2301
1d4b027a
DG
2302 return NULL;
2303}
2304
8c0faa1d 2305/*
3bd1e081 2306 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
d063d709 2307 * exec or it will fails.
8c0faa1d 2308 */
3bd1e081 2309static int spawn_consumer_thread(struct consumer_data *consumer_data)
8c0faa1d 2310{
a23ec3a7 2311 int ret, clock_ret;
ee0b0061
DG
2312 struct timespec timeout;
2313
13a7bce3
JG
2314 /*
2315 * Make sure we set the readiness flag to 0 because we are NOT ready.
2316 * This access to consumer_thread_is_ready does not need to be
2317 * protected by consumer_data.cond_mutex (yet) since the consumer
2318 * management thread has not been started at this point.
2319 */
a23ec3a7 2320 consumer_data->consumer_thread_is_ready = 0;
8c0faa1d 2321
a23ec3a7
DG
2322 /* Setup pthread condition */
2323 ret = pthread_condattr_init(&consumer_data->condattr);
4a15001e 2324 if (ret) {
a23ec3a7
DG
2325 errno = ret;
2326 PERROR("pthread_condattr_init consumer data");
2327 goto error;
2328 }
2329
2330 /*
2331 * Set the monotonic clock in order to make sure we DO NOT jump in time
2332 * between the clock_gettime() call and the timedwait call. See bug #324
2333 * for a more details and how we noticed it.
2334 */
2335 ret = pthread_condattr_setclock(&consumer_data->condattr, CLOCK_MONOTONIC);
4a15001e 2336 if (ret) {
a23ec3a7
DG
2337 errno = ret;
2338 PERROR("pthread_condattr_setclock consumer data");
ee0b0061
DG
2339 goto error;
2340 }
8c0faa1d 2341
a23ec3a7 2342 ret = pthread_cond_init(&consumer_data->cond, &consumer_data->condattr);
4a15001e 2343 if (ret) {
a23ec3a7
DG
2344 errno = ret;
2345 PERROR("pthread_cond_init consumer data");
2346 goto error;
2347 }
2348
1a1a34b4
MJ
2349 ret = pthread_create(&consumer_data->thread, default_pthread_attr(),
2350 thread_manage_consumer, consumer_data);
4a15001e
MD
2351 if (ret) {
2352 errno = ret;
3bd1e081 2353 PERROR("pthread_create consumer");
ee0b0061 2354 ret = -1;
8c0faa1d
DG
2355 goto error;
2356 }
2357
a23ec3a7
DG
2358 /* We are about to wait on a pthread condition */
2359 pthread_mutex_lock(&consumer_data->cond_mutex);
2360
ee0b0061 2361 /* Get time for sem_timedwait absolute timeout */
389fbf04 2362 clock_ret = lttng_clock_gettime(CLOCK_MONOTONIC, &timeout);
a23ec3a7
DG
2363 /*
2364 * Set the timeout for the condition timed wait even if the clock gettime
2365 * call fails since we might loop on that call and we want to avoid to
2366 * increment the timeout too many times.
2367 */
2368 timeout.tv_sec += DEFAULT_SEM_WAIT_TIMEOUT;
2369
2370 /*
2371 * The following loop COULD be skipped in some conditions so this is why we
2372 * set ret to 0 in order to make sure at least one round of the loop is
2373 * done.
2374 */
2375 ret = 0;
2376
2377 /*
2378 * Loop until the condition is reached or when a timeout is reached. Note
2379 * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT
2380 * be returned but the pthread_cond(3), from the glibc-doc, says that it is
2381 * possible. This loop does not take any chances and works with both of
2382 * them.
2383 */
2384 while (!consumer_data->consumer_thread_is_ready && ret != ETIMEDOUT) {
2385 if (clock_ret < 0) {
2386 PERROR("clock_gettime spawn consumer");
2387 /* Infinite wait for the consumerd thread to be ready */
2388 ret = pthread_cond_wait(&consumer_data->cond,
2389 &consumer_data->cond_mutex);
2390 } else {
2391 ret = pthread_cond_timedwait(&consumer_data->cond,
2392 &consumer_data->cond_mutex, &timeout);
2393 }
ee0b0061 2394 }
8c0faa1d 2395
a23ec3a7
DG
2396 /* Release the pthread condition */
2397 pthread_mutex_unlock(&consumer_data->cond_mutex);
2398
2399 if (ret != 0) {
2400 errno = ret;
2401 if (ret == ETIMEDOUT) {
4282f9a3
DG
2402 int pth_ret;
2403
ee0b0061
DG
2404 /*
2405 * Call has timed out so we kill the kconsumerd_thread and return
2406 * an error.
2407 */
a23ec3a7
DG
2408 ERR("Condition timed out. The consumer thread was never ready."
2409 " Killing it");
4282f9a3
DG
2410 pth_ret = pthread_cancel(consumer_data->thread);
2411 if (pth_ret < 0) {
3bd1e081 2412 PERROR("pthread_cancel consumer thread");
ee0b0061
DG
2413 }
2414 } else {
a23ec3a7 2415 PERROR("pthread_cond_wait failed consumer thread");
ee0b0061 2416 }
4282f9a3
DG
2417 /* Caller is expecting a negative value on failure. */
2418 ret = -1;
ee0b0061
DG
2419 goto error;
2420 }
2421
3bd1e081
MD
2422 pthread_mutex_lock(&consumer_data->pid_mutex);
2423 if (consumer_data->pid == 0) {
a23ec3a7 2424 ERR("Consumerd did not start");
3bd1e081 2425 pthread_mutex_unlock(&consumer_data->pid_mutex);
712ea556
DG
2426 goto error;
2427 }
3bd1e081 2428 pthread_mutex_unlock(&consumer_data->pid_mutex);
712ea556 2429
8c0faa1d
DG
2430 return 0;
2431
2432error:
2433 return ret;
2434}
2435
d9800920 2436/*
3bd1e081 2437 * Join consumer thread
d9800920 2438 */
3bd1e081 2439static int join_consumer_thread(struct consumer_data *consumer_data)
cf3af59e
MD
2440{
2441 void *status;
cf3af59e 2442
e8209f6b
DG
2443 /* Consumer pid must be a real one. */
2444 if (consumer_data->pid > 0) {
c617c0c6 2445 int ret;
3bd1e081 2446 ret = kill(consumer_data->pid, SIGTERM);
cf3af59e 2447 if (ret) {
4a15001e 2448 PERROR("Error killing consumer daemon");
cf3af59e
MD
2449 return ret;
2450 }
3bd1e081 2451 return pthread_join(consumer_data->thread, &status);
cf3af59e
MD
2452 } else {
2453 return 0;
2454 }
2455}
2456
8c0faa1d 2457/*
3bd1e081 2458 * Fork and exec a consumer daemon (consumerd).
8c0faa1d 2459 *
d063d709 2460 * Return pid if successful else -1.
8c0faa1d 2461 */
3bd1e081 2462static pid_t spawn_consumerd(struct consumer_data *consumer_data)
8c0faa1d
DG
2463{
2464 int ret;
2465 pid_t pid;
94c55f17 2466 const char *consumer_to_use;
53086306 2467 const char *verbosity;
94c55f17 2468 struct stat st;
8c0faa1d 2469
3bd1e081 2470 DBG("Spawning consumerd");
c49dc785 2471
8c0faa1d
DG
2472 pid = fork();
2473 if (pid == 0) {
2474 /*
3bd1e081 2475 * Exec consumerd.
8c0faa1d 2476 */
e6142f2e 2477 if (config.verbose_consumer) {
53086306 2478 verbosity = "--verbose";
4421f712 2479 } else if (lttng_opt_quiet) {
53086306 2480 verbosity = "--quiet";
4421f712
DG
2481 } else {
2482 verbosity = "";
53086306 2483 }
4421f712 2484
3bd1e081
MD
2485 switch (consumer_data->type) {
2486 case LTTNG_CONSUMER_KERNEL:
94c55f17 2487 /*
c7704d57
DG
2488 * Find out which consumerd to execute. We will first try the
2489 * 64-bit path, then the sessiond's installation directory, and
2490 * fallback on the 32-bit one,
94c55f17 2491 */
63a799e8 2492 DBG3("Looking for a kernel consumer at these locations:");
59ee5091 2493 DBG3(" 1) %s", config.consumerd64_bin_path.value ? : "NULL");
e6142f2e 2494 DBG3(" 2) %s/%s", INSTALL_BIN_PATH, DEFAULT_CONSUMERD_FILE);
59ee5091 2495 DBG3(" 3) %s", config.consumerd32_bin_path.value ? : "NULL");
e6142f2e 2496 if (stat(config.consumerd64_bin_path.value, &st) == 0) {
63a799e8 2497 DBG3("Found location #1");
e6142f2e
JG
2498 consumer_to_use = config.consumerd64_bin_path.value;
2499 } else if (stat(INSTALL_BIN_PATH "/" DEFAULT_CONSUMERD_FILE, &st) == 0) {
63a799e8 2500 DBG3("Found location #2");
e6142f2e
JG
2501 consumer_to_use = INSTALL_BIN_PATH "/" DEFAULT_CONSUMERD_FILE;
2502 } else if (stat(config.consumerd32_bin_path.value, &st) == 0) {
63a799e8 2503 DBG3("Found location #3");
e6142f2e 2504 consumer_to_use = config.consumerd32_bin_path.value;
94c55f17 2505 } else {
63a799e8 2506 DBG("Could not find any valid consumerd executable");
4282f9a3 2507 ret = -EINVAL;
3d678709 2508 goto error;
94c55f17
AM
2509 }
2510 DBG("Using kernel consumer at: %s", consumer_to_use);
6e2cc8d8 2511 (void) execl(consumer_to_use,
94c55f17
AM
2512 "lttng-consumerd", verbosity, "-k",
2513 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
2514 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
e6142f2e 2515 "--group", config.tracing_group_name.value,
94c55f17 2516 NULL);
3bd1e081 2517 break;
7753dea8
MD
2518 case LTTNG_CONSUMER64_UST:
2519 {
46b23495 2520 if (config.consumerd64_lib_dir.value) {
8f4905da
MD
2521 char *tmp;
2522 size_t tmplen;
ddaec4a2 2523 char *tmpnew;
8f4905da 2524
e8fa9fb0 2525 tmp = lttng_secure_getenv("LD_LIBRARY_PATH");
8f4905da
MD
2526 if (!tmp) {
2527 tmp = "";
2528 }
d222983e 2529 tmplen = strlen(config.consumerd64_lib_dir.value) + 1 /* : */ + strlen(tmp);
8f4905da
MD
2530 tmpnew = zmalloc(tmplen + 1 /* \0 */);
2531 if (!tmpnew) {
2532 ret = -ENOMEM;
2533 goto error;
2534 }
e6142f2e 2535 strcat(tmpnew, config.consumerd64_lib_dir.value);
8f4905da
MD
2536 if (tmp[0] != '\0') {
2537 strcat(tmpnew, ":");
2538 strcat(tmpnew, tmp);
2539 }
d222983e 2540 ret = setenv("LD_LIBRARY_PATH", tmpnew, 1);
ddaec4a2 2541 free(tmpnew);
8f4905da
MD
2542 if (ret) {
2543 ret = -errno;
2544 goto error;
2545 }
2546 }
e6142f2e
JG
2547 DBG("Using 64-bit UST consumer at: %s", config.consumerd64_bin_path.value);
2548 (void) execl(config.consumerd64_bin_path.value, "lttng-consumerd", verbosity, "-u",
7753dea8
MD
2549 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
2550 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
e6142f2e 2551 "--group", config.tracing_group_name.value,
7753dea8 2552 NULL);
3bd1e081 2553 break;
7753dea8
MD
2554 }
2555 case LTTNG_CONSUMER32_UST:
2556 {
46b23495 2557 if (config.consumerd32_lib_dir.value) {
8f4905da
MD
2558 char *tmp;
2559 size_t tmplen;
ddaec4a2 2560 char *tmpnew;
8f4905da 2561
e8fa9fb0 2562 tmp = lttng_secure_getenv("LD_LIBRARY_PATH");
8f4905da
MD
2563 if (!tmp) {
2564 tmp = "";
2565 }
d222983e 2566 tmplen = strlen(config.consumerd32_lib_dir.value) + 1 /* : */ + strlen(tmp);
8f4905da
MD
2567 tmpnew = zmalloc(tmplen + 1 /* \0 */);
2568 if (!tmpnew) {
2569 ret = -ENOMEM;
2570 goto error;
2571 }
e6142f2e 2572 strcat(tmpnew, config.consumerd32_lib_dir.value);
8f4905da
MD
2573 if (tmp[0] != '\0') {
2574 strcat(tmpnew, ":");
2575 strcat(tmpnew, tmp);
2576 }
d222983e 2577 ret = setenv("LD_LIBRARY_PATH", tmpnew, 1);
ddaec4a2 2578 free(tmpnew);
8f4905da
MD
2579 if (ret) {
2580 ret = -errno;
2581 goto error;
2582 }
2583 }
e6142f2e
JG
2584 DBG("Using 32-bit UST consumer at: %s", config.consumerd32_bin_path.value);
2585 (void) execl(config.consumerd32_bin_path.value, "lttng-consumerd", verbosity, "-u",
7753dea8
MD
2586 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
2587 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
e6142f2e 2588 "--group", config.tracing_group_name.value,
7753dea8
MD
2589 NULL);
2590 break;
2591 }
3bd1e081 2592 default:
b60e7183 2593 ERR("unknown consumer type");
e9d6b496 2594 errno = 0;
3bd1e081 2595 }
8c0faa1d 2596 if (errno != 0) {
4282f9a3 2597 PERROR("Consumer execl()");
8c0faa1d 2598 }
4282f9a3 2599 /* Reaching this point, we got a failure on our execl(). */
8c0faa1d
DG
2600 exit(EXIT_FAILURE);
2601 } else if (pid > 0) {
2602 ret = pid;
8c0faa1d 2603 } else {
76d7553f 2604 PERROR("start consumer fork");
8c0faa1d 2605 ret = -errno;
8c0faa1d 2606 }
8f4905da 2607error:
8c0faa1d
DG
2608 return ret;
2609}
2610
693bd40b 2611/*
3bd1e081 2612 * Spawn the consumerd daemon and session daemon thread.
693bd40b 2613 */
3bd1e081 2614static int start_consumerd(struct consumer_data *consumer_data)
693bd40b 2615{
c617c0c6 2616 int ret;
edb8b045
DG
2617
2618 /*
2619 * Set the listen() state on the socket since there is a possible race
2620 * between the exec() of the consumer daemon and this call if place in the
2621 * consumer thread. See bug #366 for more details.
2622 */
2623 ret = lttcomm_listen_unix_sock(consumer_data->err_sock);
2624 if (ret < 0) {
2625 goto error;
2626 }
693bd40b 2627
3bd1e081
MD
2628 pthread_mutex_lock(&consumer_data->pid_mutex);
2629 if (consumer_data->pid != 0) {
2630 pthread_mutex_unlock(&consumer_data->pid_mutex);
c49dc785
DG
2631 goto end;
2632 }
693bd40b 2633
3bd1e081 2634 ret = spawn_consumerd(consumer_data);
c49dc785 2635 if (ret < 0) {
3bd1e081
MD
2636 ERR("Spawning consumerd failed");
2637 pthread_mutex_unlock(&consumer_data->pid_mutex);
c49dc785 2638 goto error;
693bd40b 2639 }
c49dc785 2640
3bd1e081
MD
2641 /* Setting up the consumer_data pid */
2642 consumer_data->pid = ret;
48842b30 2643 DBG2("Consumer pid %d", consumer_data->pid);
3bd1e081 2644 pthread_mutex_unlock(&consumer_data->pid_mutex);
693bd40b 2645
3bd1e081
MD
2646 DBG2("Spawning consumer control thread");
2647 ret = spawn_consumer_thread(consumer_data);
693bd40b 2648 if (ret < 0) {
3bd1e081 2649 ERR("Fatal error spawning consumer control thread");
693bd40b
DG
2650 goto error;
2651 }
2652
c49dc785 2653end:
693bd40b
DG
2654 return 0;
2655
2656error:
331744e3 2657 /* Cleanup already created sockets on error. */
edb8b045 2658 if (consumer_data->err_sock >= 0) {
c617c0c6
MD
2659 int err;
2660
edb8b045
DG
2661 err = close(consumer_data->err_sock);
2662 if (err < 0) {
2663 PERROR("close consumer data error socket");
2664 }
2665 }
693bd40b
DG
2666 return ret;
2667}
2668
b73401da 2669/*
096102bd 2670 * Setup necessary data for kernel tracer action.
b73401da 2671 */
096102bd 2672static int init_kernel_tracer(void)
b73401da
DG
2673{
2674 int ret;
b73401da 2675
096102bd
DG
2676 /* Modprobe lttng kernel modules */
2677 ret = modprobe_lttng_control();
b73401da 2678 if (ret < 0) {
b73401da
DG
2679 goto error;
2680 }
2681
096102bd
DG
2682 /* Open debugfs lttng */
2683 kernel_tracer_fd = open(module_proc_lttng, O_RDWR);
2684 if (kernel_tracer_fd < 0) {
2685 DBG("Failed to open %s", module_proc_lttng);
2f77fc4b 2686 goto error_open;
54d01ffb
DG
2687 }
2688
2f77fc4b 2689 /* Validate kernel version */
88076e89
JD
2690 ret = kernel_validate_version(kernel_tracer_fd, &kernel_tracer_version,
2691 &kernel_tracer_abi_version);
2f77fc4b
DG
2692 if (ret < 0) {
2693 goto error_version;
b551a063 2694 }
54d01ffb 2695
2f77fc4b
DG
2696 ret = modprobe_lttng_data();
2697 if (ret < 0) {
2698 goto error_modules;
54d01ffb
DG
2699 }
2700
6e21424e
JR
2701 ret = kernel_supports_ring_buffer_snapshot_sample_positions(
2702 kernel_tracer_fd);
2703 if (ret < 0) {
2704 goto error_modules;
2705 }
2706
2707 if (ret < 1) {
2708 WARN("Kernel tracer does not support buffer monitoring. "
2709 "The monitoring timer of channels in the kernel domain "
2710 "will be set to 0 (disabled).");
2711 }
2712
2f77fc4b
DG
2713 DBG("Kernel tracer fd %d", kernel_tracer_fd);
2714 return 0;
2715
2716error_version:
2717 modprobe_remove_lttng_control();
2718 ret = close(kernel_tracer_fd);
2719 if (ret) {
2720 PERROR("close");
b551a063 2721 }
2f77fc4b 2722 kernel_tracer_fd = -1;
f73fabfd 2723 return LTTNG_ERR_KERN_VERSION;
b551a063 2724
2f77fc4b
DG
2725error_modules:
2726 ret = close(kernel_tracer_fd);
2727 if (ret) {
2728 PERROR("close");
b551a063 2729 }
54d01ffb 2730
2f77fc4b
DG
2731error_open:
2732 modprobe_remove_lttng_control();
54d01ffb
DG
2733
2734error:
2f77fc4b
DG
2735 WARN("No kernel tracer available");
2736 kernel_tracer_fd = -1;
2737 if (!is_root) {
f73fabfd 2738 return LTTNG_ERR_NEED_ROOT_SESSIOND;
2f77fc4b 2739 } else {
f73fabfd 2740 return LTTNG_ERR_KERN_NA;
2f77fc4b 2741 }
54d01ffb
DG
2742}
2743
2f77fc4b 2744
54d01ffb 2745/*
2f77fc4b
DG
2746 * Copy consumer output from the tracing session to the domain session. The
2747 * function also applies the right modification on a per domain basis for the
2748 * trace files destination directory.
36b588ed
MD
2749 *
2750 * Should *NOT* be called with RCU read-side lock held.
54d01ffb 2751 */
2f77fc4b 2752static int copy_session_consumer(int domain, struct ltt_session *session)
54d01ffb
DG
2753{
2754 int ret;
2f77fc4b
DG
2755 const char *dir_name;
2756 struct consumer_output *consumer;
2757
2758 assert(session);
2759 assert(session->consumer);
54d01ffb 2760
b551a063
DG
2761 switch (domain) {
2762 case LTTNG_DOMAIN_KERNEL:
2f77fc4b 2763 DBG3("Copying tracing session consumer output in kernel session");
09a90bcd
DG
2764 /*
2765 * XXX: We should audit the session creation and what this function
2766 * does "extra" in order to avoid a destroy since this function is used
2767 * in the domain session creation (kernel and ust) only. Same for UST
2768 * domain.
2769 */
2770 if (session->kernel_session->consumer) {
6addfa37 2771 consumer_output_put(session->kernel_session->consumer);
09a90bcd 2772 }
2f77fc4b
DG
2773 session->kernel_session->consumer =
2774 consumer_copy_output(session->consumer);
2775 /* Ease our life a bit for the next part */
2776 consumer = session->kernel_session->consumer;
2777 dir_name = DEFAULT_KERNEL_TRACE_DIR;
b551a063 2778 break;
f20baf8e 2779 case LTTNG_DOMAIN_JUL:
5cdb6027 2780 case LTTNG_DOMAIN_LOG4J:
0e115563 2781 case LTTNG_DOMAIN_PYTHON:
b551a063 2782 case LTTNG_DOMAIN_UST:
2f77fc4b 2783 DBG3("Copying tracing session consumer output in UST session");
09a90bcd 2784 if (session->ust_session->consumer) {
6addfa37 2785 consumer_output_put(session->ust_session->consumer);
09a90bcd 2786 }
2f77fc4b
DG
2787 session->ust_session->consumer =
2788 consumer_copy_output(session->consumer);
2789 /* Ease our life a bit for the next part */
2790 consumer = session->ust_session->consumer;
2791 dir_name = DEFAULT_UST_TRACE_DIR;
b551a063
DG
2792 break;
2793 default:
f73fabfd 2794 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
54d01ffb
DG
2795 goto error;
2796 }
2797
2f77fc4b 2798 /* Append correct directory to subdir */
c30ce0b3
CB
2799 strncat(consumer->subdir, dir_name,
2800 sizeof(consumer->subdir) - strlen(consumer->subdir) - 1);
2f77fc4b
DG
2801 DBG3("Copy session consumer subdir %s", consumer->subdir);
2802
f73fabfd 2803 ret = LTTNG_OK;
54d01ffb
DG
2804
2805error:
2806 return ret;
2807}
2808
00e2e675 2809/*
2f77fc4b 2810 * Create an UST session and add it to the session ust list.
36b588ed
MD
2811 *
2812 * Should *NOT* be called with RCU read-side lock held.
00e2e675 2813 */
2f77fc4b
DG
2814static int create_ust_session(struct ltt_session *session,
2815 struct lttng_domain *domain)
00e2e675
DG
2816{
2817 int ret;
2f77fc4b 2818 struct ltt_ust_session *lus = NULL;
00e2e675 2819
a4b92340 2820 assert(session);
2f77fc4b
DG
2821 assert(domain);
2822 assert(session->consumer);
a4b92340 2823
2f77fc4b 2824 switch (domain->type) {
f20baf8e 2825 case LTTNG_DOMAIN_JUL:
5cdb6027 2826 case LTTNG_DOMAIN_LOG4J:
0e115563 2827 case LTTNG_DOMAIN_PYTHON:
2f77fc4b
DG
2828 case LTTNG_DOMAIN_UST:
2829 break;
2830 default:
2831 ERR("Unknown UST domain on create session %d", domain->type);
f73fabfd 2832 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
00e2e675
DG
2833 goto error;
2834 }
2835
2f77fc4b
DG
2836 DBG("Creating UST session");
2837
dec56f6c 2838 lus = trace_ust_create_session(session->id);
2f77fc4b 2839 if (lus == NULL) {
f73fabfd 2840 ret = LTTNG_ERR_UST_SESS_FAIL;
a4b92340
DG
2841 goto error;
2842 }
2843
2f77fc4b
DG
2844 lus->uid = session->uid;
2845 lus->gid = session->gid;
2bba9e53 2846 lus->output_traces = session->output_traces;
27babd3a 2847 lus->snapshot_mode = session->snapshot_mode;
ecc48a90 2848 lus->live_timer_interval = session->live_timer;
2f77fc4b 2849 session->ust_session = lus;
d7ba1388 2850 if (session->shm_path[0]) {
3d071855
MD
2851 strncpy(lus->root_shm_path, session->shm_path,
2852 sizeof(lus->root_shm_path));
2853 lus->root_shm_path[sizeof(lus->root_shm_path) - 1] = '\0';
d7ba1388
MD
2854 strncpy(lus->shm_path, session->shm_path,
2855 sizeof(lus->shm_path));
2856 lus->shm_path[sizeof(lus->shm_path) - 1] = '\0';
2857 strncat(lus->shm_path, "/ust",
2858 sizeof(lus->shm_path) - strlen(lus->shm_path) - 1);
2859 }
2f77fc4b
DG
2860 /* Copy session output to the newly created UST session */
2861 ret = copy_session_consumer(domain->type, session);
f73fabfd 2862 if (ret != LTTNG_OK) {
00e2e675
DG
2863 goto error;
2864 }
2865
f73fabfd 2866 return LTTNG_OK;
00e2e675
DG
2867
2868error:
2f77fc4b
DG
2869 free(lus);
2870 session->ust_session = NULL;
00e2e675
DG
2871 return ret;
2872}
2873
2874/*
2f77fc4b 2875 * Create a kernel tracer session then create the default channel.
00e2e675 2876 */
2f77fc4b 2877static int create_kernel_session(struct ltt_session *session)
00e2e675
DG
2878{
2879 int ret;
a4b92340 2880
2f77fc4b 2881 DBG("Creating kernel session");
00e2e675 2882
2f77fc4b
DG
2883 ret = kernel_create_session(session, kernel_tracer_fd);
2884 if (ret < 0) {
f73fabfd 2885 ret = LTTNG_ERR_KERN_SESS_FAIL;
00e2e675
DG
2886 goto error;
2887 }
2888
2f77fc4b
DG
2889 /* Code flow safety */
2890 assert(session->kernel_session);
2891
2892 /* Copy session output to the newly created Kernel session */
2893 ret = copy_session_consumer(LTTNG_DOMAIN_KERNEL, session);
f73fabfd 2894 if (ret != LTTNG_OK) {
a4b92340
DG
2895 goto error;
2896 }
2897
2f77fc4b
DG
2898 session->kernel_session->uid = session->uid;
2899 session->kernel_session->gid = session->gid;
2bba9e53 2900 session->kernel_session->output_traces = session->output_traces;
27babd3a 2901 session->kernel_session->snapshot_mode = session->snapshot_mode;
00e2e675 2902
f73fabfd 2903 return LTTNG_OK;
00e2e675 2904
2f77fc4b
DG
2905error:
2906 trace_kernel_destroy_session(session->kernel_session);
2907 session->kernel_session = NULL;
2908 return ret;
2909}
00e2e675 2910
2f77fc4b
DG
2911/*
2912 * Count number of session permitted by uid/gid.
2913 */
2914static unsigned int lttng_sessions_count(uid_t uid, gid_t gid)
2915{
2916 unsigned int i = 0;
2917 struct ltt_session *session;
07424f16 2918
2f77fc4b
DG
2919 DBG("Counting number of available session for UID %d GID %d",
2920 uid, gid);
2921 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
00e2e675 2922 /*
2f77fc4b 2923 * Only list the sessions the user can control.
00e2e675 2924 */
2f77fc4b
DG
2925 if (!session_access_ok(session, uid, gid)) {
2926 continue;
2927 }
2928 i++;
a4b92340 2929 }
2f77fc4b 2930 return i;
00e2e675
DG
2931}
2932
5c408ad8
JD
2933/*
2934 * Check if the current kernel tracer supports the session rotation feature.
2935 * Return 1 if it does, 0 otherwise.
2936 */
2937static int check_rotate_compatible(void)
2938{
2939 int ret = 1;
2940
2941 if (kernel_tracer_version.major != 2 || kernel_tracer_version.minor < 11) {
2942 DBG("Kernel tracer version is not compatible with the rotation feature");
2943 ret = 0;
2944 }
2945
2946 return ret;
2947}
2948
54d01ffb
DG
2949/*
2950 * Process the command requested by the lttng client within the command
2951 * context structure. This function make sure that the return structure (llm)
2952 * is set and ready for transmission before returning.
2953 *
2954 * Return any error encountered or 0 for success.
53a80697
MD
2955 *
2956 * "sock" is only used for special-case var. len data.
36b588ed
MD
2957 *
2958 * Should *NOT* be called with RCU read-side lock held.
54d01ffb 2959 */
53a80697
MD
2960static int process_client_msg(struct command_ctx *cmd_ctx, int sock,
2961 int *sock_error)
54d01ffb 2962{
f73fabfd 2963 int ret = LTTNG_OK;
44d3bd01 2964 int need_tracing_session = 1;
2e09ba09 2965 int need_domain;
54d01ffb
DG
2966
2967 DBG("Processing client command %d", cmd_ctx->lsm->cmd_type);
2968
3745d315
MD
2969 assert(!rcu_read_ongoing());
2970
53a80697
MD
2971 *sock_error = 0;
2972
2e09ba09
MD
2973 switch (cmd_ctx->lsm->cmd_type) {
2974 case LTTNG_CREATE_SESSION:
27babd3a 2975 case LTTNG_CREATE_SESSION_SNAPSHOT:
ecc48a90 2976 case LTTNG_CREATE_SESSION_LIVE:
2e09ba09
MD
2977 case LTTNG_DESTROY_SESSION:
2978 case LTTNG_LIST_SESSIONS:
2979 case LTTNG_LIST_DOMAINS:
2980 case LTTNG_START_TRACE:
2981 case LTTNG_STOP_TRACE:
6d805429 2982 case LTTNG_DATA_PENDING:
da3c9ec1
DG
2983 case LTTNG_SNAPSHOT_ADD_OUTPUT:
2984 case LTTNG_SNAPSHOT_DEL_OUTPUT:
2985 case LTTNG_SNAPSHOT_LIST_OUTPUT:
2986 case LTTNG_SNAPSHOT_RECORD:
fb198a11 2987 case LTTNG_SAVE_SESSION:
d7ba1388 2988 case LTTNG_SET_SESSION_SHM_PATH:
eded6438 2989 case LTTNG_REGENERATE_METADATA:
c2561365 2990 case LTTNG_REGENERATE_STATEDUMP:
b3530820
JG
2991 case LTTNG_REGISTER_TRIGGER:
2992 case LTTNG_UNREGISTER_TRIGGER:
5c408ad8 2993 case LTTNG_ROTATE_SESSION:
d68c9a04
JD
2994 case LTTNG_ROTATION_GET_INFO:
2995 case LTTNG_SESSION_GET_CURRENT_OUTPUT:
259c2674 2996 case LTTNG_ROTATION_SET_SCHEDULE:
66ea93b1 2997 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES:
2e09ba09 2998 need_domain = 0;
3aace903 2999 break;
2e09ba09
MD
3000 default:
3001 need_domain = 1;
3002 }
3003
e6142f2e 3004 if (config.no_kernel && need_domain
2e09ba09 3005 && cmd_ctx->lsm->domain.type == LTTNG_DOMAIN_KERNEL) {
531d29f9 3006 if (!is_root) {
f73fabfd 3007 ret = LTTNG_ERR_NEED_ROOT_SESSIOND;
531d29f9 3008 } else {
f73fabfd 3009 ret = LTTNG_ERR_KERN_NA;
531d29f9 3010 }
4fba7219
DG
3011 goto error;
3012 }
3013
8d3113b2
DG
3014 /* Deny register consumer if we already have a spawned consumer. */
3015 if (cmd_ctx->lsm->cmd_type == LTTNG_REGISTER_CONSUMER) {
3016 pthread_mutex_lock(&kconsumer_data.pid_mutex);
3017 if (kconsumer_data.pid > 0) {
f73fabfd 3018 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
fa317f24 3019 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
8d3113b2
DG
3020 goto error;
3021 }
3022 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
3023 }
3024
54d01ffb
DG
3025 /*
3026 * Check for command that don't needs to allocate a returned payload. We do
44d3bd01 3027 * this here so we don't have to make the call for no payload at each
54d01ffb
DG
3028 * command.
3029 */
3030 switch(cmd_ctx->lsm->cmd_type) {
3031 case LTTNG_LIST_SESSIONS:
3032 case LTTNG_LIST_TRACEPOINTS:
f37d259d 3033 case LTTNG_LIST_TRACEPOINT_FIELDS:
54d01ffb
DG
3034 case LTTNG_LIST_DOMAINS:
3035 case LTTNG_LIST_CHANNELS:
3036 case LTTNG_LIST_EVENTS:
834978fd 3037 case LTTNG_LIST_SYSCALLS:
a5dfbb9d 3038 case LTTNG_LIST_TRACKER_PIDS:
5cd0780d 3039 case LTTNG_DATA_PENDING:
5c408ad8 3040 case LTTNG_ROTATE_SESSION:
d68c9a04 3041 case LTTNG_ROTATION_GET_INFO:
66ea93b1 3042 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES:
54d01ffb
DG
3043 break;
3044 default:
3045 /* Setup lttng message with no payload */
6e10c9b9 3046 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, NULL, 0);
54d01ffb
DG
3047 if (ret < 0) {
3048 /* This label does not try to unlock the session */
3049 goto init_setup_error;
3050 }
3051 }
3052
3053 /* Commands that DO NOT need a session. */
3054 switch (cmd_ctx->lsm->cmd_type) {
54d01ffb 3055 case LTTNG_CREATE_SESSION:
27babd3a 3056 case LTTNG_CREATE_SESSION_SNAPSHOT:
ecc48a90 3057 case LTTNG_CREATE_SESSION_LIVE:
54d01ffb
DG
3058 case LTTNG_LIST_SESSIONS:
3059 case LTTNG_LIST_TRACEPOINTS:
834978fd 3060 case LTTNG_LIST_SYSCALLS:
f37d259d 3061 case LTTNG_LIST_TRACEPOINT_FIELDS:
fb198a11 3062 case LTTNG_SAVE_SESSION:
b3530820
JG
3063 case LTTNG_REGISTER_TRIGGER:
3064 case LTTNG_UNREGISTER_TRIGGER:
44d3bd01 3065 need_tracing_session = 0;
54d01ffb
DG
3066 break;
3067 default:
3068 DBG("Getting session %s by name", cmd_ctx->lsm->session.name);
256a5576
MD
3069 /*
3070 * We keep the session list lock across _all_ commands
3071 * for now, because the per-session lock does not
3072 * handle teardown properly.
3073 */
74babd95 3074 session_lock_list();
54d01ffb
DG
3075 cmd_ctx->session = session_find_by_name(cmd_ctx->lsm->session.name);
3076 if (cmd_ctx->session == NULL) {
bba2d65f 3077 ret = LTTNG_ERR_SESS_NOT_FOUND;
54d01ffb
DG
3078 goto error;
3079 } else {<