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