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