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