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