Fix: health subsystem issues with shared code
[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>
fac6795d 4 *
d14d33bf
AM
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
91d76f53 8 *
d14d33bf
AM
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
91d76f53 13 *
d14d33bf
AM
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
fac6795d
DG
17 */
18
19#define _GNU_SOURCE
fac6795d
DG
20#include <getopt.h>
21#include <grp.h>
22#include <limits.h>
23#include <pthread.h>
24#include <signal.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
0fdd1e2c 28#include <sys/mman.h>
b73401da 29#include <sys/mount.h>
1e307fab 30#include <sys/resource.h>
fac6795d
DG
31#include <sys/socket.h>
32#include <sys/stat.h>
33#include <sys/types.h>
0fdd1e2c 34#include <sys/wait.h>
5c827ce0 35#include <urcu/uatomic.h>
fac6795d 36#include <unistd.h>
3bd1e081 37#include <config.h>
fac6795d 38
990570ed 39#include <common/common.h>
0ba98ebc 40#include <common/compat/poll.h>
d27c42b8 41#include <common/compat/socket.h>
db758600
DG
42#include <common/defaults.h>
43#include <common/kernel-consumer/kernel-consumer.h>
50c8f484 44#include <common/futex.h>
00e2e675 45#include <common/relayd/relayd.h>
81b86775 46#include <common/utils.h>
fac6795d 47
10a8a223 48#include "lttng-sessiond.h"
54d01ffb 49#include "channel.h"
2f77fc4b 50#include "cmd.h"
00e2e675 51#include "consumer.h"
099e26bd 52#include "context.h"
54d01ffb 53#include "event.h"
4771f025 54#include "kernel.h"
f1e16794 55#include "kernel-consumer.h"
096102bd 56#include "modprobe.h"
0fdd1e2c 57#include "shm.h"
1e307fab 58#include "ust-ctl.h"
00e2e675 59#include "ust-consumer.h"
8e68d1c8 60#include "utils.h"
4063050c 61#include "fd-limit.h"
44a5e5eb 62#include "health.h"
8ac94142 63#include "testpoint.h"
fac6795d 64
ebaeda94
MD
65#define CONSUMERD_FILE "lttng-consumerd"
66
75462a81 67/* Const values */
686204ab 68const char default_home_dir[] = DEFAULT_HOME_DIR;
bbccc3d2 69const char default_tracing_group[] = DEFAULT_TRACING_GROUP;
686204ab
MD
70const char default_ust_sock_dir[] = DEFAULT_UST_SOCK_DIR;
71const char default_global_apps_pipe[] = DEFAULT_GLOBAL_APPS_PIPE;
72
fac6795d
DG
73const char *progname;
74const char *opt_tracing_group;
35f90c40 75static const char *opt_pidfile;
5b8719f5 76static int opt_sig_parent;
97e19046 77static int opt_verbose_consumer;
fac6795d 78static int opt_daemon;
4fba7219 79static int opt_no_kernel;
fac6795d 80static int is_root; /* Set to 1 if the daemon is running as root */
1d4b027a 81static pid_t ppid; /* Parent PID for --sig-parent option */
67e40797 82static char *rundir;
3bd1e081 83
a23ec3a7
DG
84/*
85 * Consumer daemon specific control data. Every value not initialized here is
86 * set to 0 by the static definition.
87 */
3bd1e081
MD
88static struct consumer_data kconsumer_data = {
89 .type = LTTNG_CONSUMER_KERNEL,
60922cb0
DG
90 .err_unix_sock_path = DEFAULT_KCONSUMERD_ERR_SOCK_PATH,
91 .cmd_unix_sock_path = DEFAULT_KCONSUMERD_CMD_SOCK_PATH,
03550b58
MD
92 .err_sock = -1,
93 .cmd_sock = -1,
173af62f
DG
94 .pid_mutex = PTHREAD_MUTEX_INITIALIZER,
95 .lock = PTHREAD_MUTEX_INITIALIZER,
a23ec3a7
DG
96 .cond = PTHREAD_COND_INITIALIZER,
97 .cond_mutex = PTHREAD_MUTEX_INITIALIZER,
3bd1e081 98};
7753dea8
MD
99static struct consumer_data ustconsumer64_data = {
100 .type = LTTNG_CONSUMER64_UST,
60922cb0
DG
101 .err_unix_sock_path = DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH,
102 .cmd_unix_sock_path = DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH,
03550b58
MD
103 .err_sock = -1,
104 .cmd_sock = -1,
173af62f
DG
105 .pid_mutex = PTHREAD_MUTEX_INITIALIZER,
106 .lock = PTHREAD_MUTEX_INITIALIZER,
a23ec3a7
DG
107 .cond = PTHREAD_COND_INITIALIZER,
108 .cond_mutex = PTHREAD_MUTEX_INITIALIZER,
7753dea8
MD
109};
110static struct consumer_data ustconsumer32_data = {
111 .type = LTTNG_CONSUMER32_UST,
60922cb0
DG
112 .err_unix_sock_path = DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH,
113 .cmd_unix_sock_path = DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH,
03550b58
MD
114 .err_sock = -1,
115 .cmd_sock = -1,
173af62f
DG
116 .pid_mutex = PTHREAD_MUTEX_INITIALIZER,
117 .lock = PTHREAD_MUTEX_INITIALIZER,
a23ec3a7
DG
118 .cond = PTHREAD_COND_INITIALIZER,
119 .cond_mutex = PTHREAD_MUTEX_INITIALIZER,
3bd1e081
MD
120};
121
26c9d55e 122/* Shared between threads */
099e26bd 123static int dispatch_thread_exit;
fac6795d 124
54d01ffb
DG
125/* Global application Unix socket path */
126static char apps_unix_sock_path[PATH_MAX];
127/* Global client Unix socket path */
128static char client_unix_sock_path[PATH_MAX];
54d01ffb
DG
129/* global wait shm path for UST */
130static char wait_shm_path[PATH_MAX];
44a5e5eb
DG
131/* Global health check unix path */
132static char health_unix_sock_path[PATH_MAX];
fac6795d 133
1d4b027a 134/* Sockets and FDs */
a4b35e07
MD
135static int client_sock = -1;
136static int apps_sock = -1;
2f77fc4b 137int kernel_tracer_fd = -1;
76d7553f 138static int kernel_poll_pipe[2] = { -1, -1 };
1d4b027a 139
273ea72c
DG
140/*
141 * Quit pipe for all threads. This permits a single cancellation point
142 * for all threads when receiving an event on the pipe.
143 */
76d7553f 144static int thread_quit_pipe[2] = { -1, -1 };
273ea72c 145
099e26bd
DG
146/*
147 * This pipe is used to inform the thread managing application communication
148 * that a command is queued and ready to be processed.
149 */
76d7553f 150static int apps_cmd_pipe[2] = { -1, -1 };
099e26bd 151
1d4b027a 152/* Pthread, Mutexes and Semaphores */
1d4b027a 153static pthread_t apps_thread;
099e26bd 154static pthread_t reg_apps_thread;
1d4b027a 155static pthread_t client_thread;
7a485870 156static pthread_t kernel_thread;
099e26bd 157static pthread_t dispatch_thread;
44a5e5eb 158static pthread_t health_thread;
5eb91c98 159
099e26bd
DG
160/*
161 * UST registration command queue. This queue is tied with a futex and uses a N
162 * wakers / 1 waiter implemented and detailed in futex.c/.h
163 *
164 * The thread_manage_apps and thread_dispatch_ust_registration interact with
165 * this queue and the wait/wake scheme.
166 */
167static struct ust_cmd_queue ust_cmd_queue;
168
b5541356
DG
169/*
170 * Pointer initialized before thread creation.
171 *
172 * This points to the tracing session list containing the session count and a
173 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
174 * MUST NOT be taken if you call a public function in session.c.
04ea676f 175 *
d063d709 176 * The lock is nested inside the structure: session_list_ptr->lock. Please use
54d01ffb 177 * session_lock_list and session_unlock_list for lock acquisition.
b5541356
DG
178 */
179static struct ltt_session_list *session_list_ptr;
180
7753dea8
MD
181int ust_consumerd64_fd = -1;
182int ust_consumerd32_fd = -1;
183
fb6f1fa2
YB
184static const char *consumerd32_bin = CONFIG_CONSUMERD32_BIN;
185static const char *consumerd64_bin = CONFIG_CONSUMERD64_BIN;
186static const char *consumerd32_libdir = CONFIG_CONSUMERD32_LIBDIR;
187static const char *consumerd64_libdir = CONFIG_CONSUMERD64_LIBDIR;
fb09408a 188
2f77fc4b
DG
189static const char *module_proc_lttng = "/proc/lttng";
190
5c827ce0
DG
191/*
192 * Consumer daemon state which is changed when spawning it, killing it or in
193 * case of a fatal error.
194 */
195enum consumerd_state {
196 CONSUMER_STARTED = 1,
197 CONSUMER_STOPPED = 2,
198 CONSUMER_ERROR = 3,
199};
200
201/*
202 * This consumer daemon state is used to validate if a client command will be
203 * able to reach the consumer. If not, the client is informed. For instance,
204 * doing a "lttng start" when the consumer state is set to ERROR will return an
205 * error to the client.
206 *
207 * The following example shows a possible race condition of this scheme:
208 *
209 * consumer thread error happens
210 * client cmd arrives
211 * client cmd checks state -> still OK
212 * consumer thread exit, sets error
213 * client cmd try to talk to consumer
214 * ...
215 *
216 * However, since the consumer is a different daemon, we have no way of making
217 * sure the command will reach it safely even with this state flag. This is why
218 * we consider that up to the state validation during command processing, the
219 * command is safe. After that, we can not guarantee the correctness of the
220 * client request vis-a-vis the consumer.
221 */
222static enum consumerd_state ust_consumerd_state;
223static enum consumerd_state kernel_consumerd_state;
224
44a5e5eb
DG
225/* Used for the health monitoring of the session daemon. See health.h */
226struct health_state health_thread_cmd;
139ac872 227struct health_state health_thread_app_manage;
44a5e5eb
DG
228struct health_state health_thread_app_reg;
229struct health_state health_thread_kernel;
230
ae9e45b3
DG
231/*
232 * Socket timeout for receiving and sending in seconds.
233 */
234static int app_socket_timeout;
235
fb09408a 236static
7753dea8 237void setup_consumerd_path(void)
fb09408a 238{
fc7a59ce 239 const char *bin, *libdir;
fb09408a 240
7753dea8
MD
241 /*
242 * Allow INSTALL_BIN_PATH to be used as a target path for the
ebaeda94
MD
243 * native architecture size consumer if CONFIG_CONSUMER*_PATH
244 * has not been defined.
7753dea8 245 */
ebaeda94 246#if (CAA_BITS_PER_LONG == 32)
fc7a59ce
AM
247 if (!consumerd32_bin[0]) {
248 consumerd32_bin = INSTALL_BIN_PATH "/" CONSUMERD_FILE;
ebaeda94
MD
249 }
250 if (!consumerd32_libdir[0]) {
251 consumerd32_libdir = INSTALL_LIB_PATH;
252 }
253#elif (CAA_BITS_PER_LONG == 64)
fc7a59ce
AM
254 if (!consumerd64_bin[0]) {
255 consumerd64_bin = INSTALL_BIN_PATH "/" CONSUMERD_FILE;
7753dea8 256 }
ebaeda94
MD
257 if (!consumerd64_libdir[0]) {
258 consumerd64_libdir = INSTALL_LIB_PATH;
7753dea8
MD
259 }
260#else
261#error "Unknown bitness"
262#endif
263
fb09408a
MD
264 /*
265 * runtime env. var. overrides the build default.
266 */
fc7a59ce
AM
267 bin = getenv("LTTNG_CONSUMERD32_BIN");
268 if (bin) {
269 consumerd32_bin = bin;
7753dea8 270 }
fc7a59ce
AM
271 bin = getenv("LTTNG_CONSUMERD64_BIN");
272 if (bin) {
273 consumerd64_bin = bin;
ebaeda94 274 }
72f579ee 275 libdir = getenv("LTTNG_CONSUMERD32_LIBDIR");
ebaeda94
MD
276 if (libdir) {
277 consumerd32_libdir = libdir;
278 }
72f579ee 279 libdir = getenv("LTTNG_CONSUMERD64_LIBDIR");
ebaeda94
MD
280 if (libdir) {
281 consumerd64_libdir = libdir;
fb09408a
MD
282 }
283}
284
5eb91c98
DG
285/*
286 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
287 */
288static int create_thread_poll_set(struct lttng_poll_event *events,
289 unsigned int size)
290{
291 int ret;
292
293 if (events == NULL || size == 0) {
294 ret = -1;
295 goto error;
296 }
297
298 ret = lttng_poll_create(events, size, LTTNG_CLOEXEC);
299 if (ret < 0) {
300 goto error;
301 }
302
303 /* Add quit pipe */
304 ret = lttng_poll_add(events, thread_quit_pipe[0], LPOLLIN);
305 if (ret < 0) {
306 goto error;
307 }
308
309 return 0;
310
311error:
312 return ret;
313}
314
315/*
316 * Check if the thread quit pipe was triggered.
317 *
318 * Return 1 if it was triggered else 0;
319 */
320static int check_thread_quit_pipe(int fd, uint32_t events)
321{
322 if (fd == thread_quit_pipe[0] && (events & LPOLLIN)) {
323 return 1;
324 }
325
326 return 0;
327}
328
0fdd1e2c
DG
329/*
330 * Return group ID of the tracing group or -1 if not found.
331 */
996b65c8
MD
332static gid_t allowed_group(void)
333{
334 struct group *grp;
335
274e143b
MD
336 if (opt_tracing_group) {
337 grp = getgrnam(opt_tracing_group);
338 } else {
339 grp = getgrnam(default_tracing_group);
340 }
996b65c8
MD
341 if (!grp) {
342 return -1;
343 } else {
344 return grp->gr_gid;
345 }
346}
347
273ea72c 348/*
5eb91c98 349 * Init thread quit pipe.
273ea72c
DG
350 *
351 * Return -1 on error or 0 if all pipes are created.
352 */
353static int init_thread_quit_pipe(void)
354{
730389d9 355 int ret, i;
273ea72c 356
730389d9 357 ret = pipe(thread_quit_pipe);
273ea72c 358 if (ret < 0) {
730389d9 359 PERROR("thread quit pipe");
273ea72c
DG
360 goto error;
361 }
362
730389d9
DG
363 for (i = 0; i < 2; i++) {
364 ret = fcntl(thread_quit_pipe[i], F_SETFD, FD_CLOEXEC);
365 if (ret < 0) {
366 PERROR("fcntl");
367 goto error;
368 }
369 }
370
273ea72c
DG
371error:
372 return ret;
373}
374
099e26bd
DG
375/*
376 * Stop all threads by closing the thread quit pipe.
377 */
cf3af59e
MD
378static void stop_threads(void)
379{
5eb91c98
DG
380 int ret;
381
cf3af59e
MD
382 /* Stopping all threads */
383 DBG("Terminating all threads");
54d01ffb 384 ret = notify_thread_pipe(thread_quit_pipe[1]);
5eb91c98
DG
385 if (ret < 0) {
386 ERR("write error on thread quit pipe");
387 }
388
099e26bd 389 /* Dispatch thread */
26c9d55e 390 CMM_STORE_SHARED(dispatch_thread_exit, 1);
099e26bd 391 futex_nto1_wake(&ust_cmd_queue.futex);
cf3af59e
MD
392}
393
fac6795d 394/*
d063d709 395 * Cleanup the daemon
fac6795d 396 */
cf3af59e 397static void cleanup(void)
fac6795d 398{
ef599319 399 int ret;
6620da75 400 char *cmd = NULL;
af9737e9 401 struct ltt_session *sess, *stmp;
fac6795d 402
1d4b027a 403 DBG("Cleaning up");
e07ae692 404
2f77fc4b
DG
405 /* First thing first, stop all threads */
406 utils_close_pipe(thread_quit_pipe);
407
35f90c40
DG
408 /*
409 * If opt_pidfile is undefined, the default file will be wiped when
410 * removing the rundir.
411 */
412 if (opt_pidfile) {
413 ret = remove(opt_pidfile);
414 if (ret < 0) {
415 PERROR("remove pidfile %s", opt_pidfile);
416 }
417 }
418
67e40797
DG
419 DBG("Removing %s directory", rundir);
420 ret = asprintf(&cmd, "rm -rf %s", rundir);
421 if (ret < 0) {
422 ERR("asprintf failed. Something is really wrong!");
423 }
5461b305 424
67e40797
DG
425 /* Remove lttng run directory */
426 ret = system(cmd);
427 if (ret < 0) {
428 ERR("Unable to clean %s", rundir);
1d4b027a 429 }
67e40797 430 free(cmd);
bd69add2 431 free(rundir);
5461b305 432
99bab54f 433 DBG("Cleaning up all sessions");
fac6795d 434
b5541356 435 /* Destroy session list mutex */
273ea72c
DG
436 if (session_list_ptr != NULL) {
437 pthread_mutex_destroy(&session_list_ptr->lock);
438
439 /* Cleanup ALL session */
54d01ffb
DG
440 cds_list_for_each_entry_safe(sess, stmp,
441 &session_list_ptr->head, list) {
2f77fc4b 442 cmd_destroy_session(sess, kernel_poll_pipe[1]);
273ea72c
DG
443 }
444 }
445
099e26bd 446 DBG("Closing all UST sockets");
56fff090 447 ust_app_clean_list();
099e26bd 448
4fba7219
DG
449 if (is_root && !opt_no_kernel) {
450 DBG2("Closing kernel fd");
a4b35e07 451 if (kernel_tracer_fd >= 0) {
76d7553f
MD
452 ret = close(kernel_tracer_fd);
453 if (ret) {
454 PERROR("close");
455 }
a4b35e07 456 }
2f50c8a3 457 DBG("Unloading kernel modules");
096102bd 458 modprobe_remove_lttng_all();
2f50c8a3 459 }
2f77fc4b 460
421cb601 461 /* <fun> */
f56a39af 462 DBG("%c[%d;%dm*** assert failed :-) *** ==> %c[%dm%c[%d;%dm"
421cb601
DG
463 "Matthew, BEET driven development works!%c[%dm",
464 27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
465 /* </fun> */
fac6795d
DG
466}
467
e065084a 468/*
d063d709 469 * Send data on a unix socket using the liblttsessiondcomm API.
e065084a 470 *
d063d709 471 * Return lttcomm error code.
e065084a
DG
472 */
473static int send_unix_sock(int sock, void *buf, size_t len)
474{
475 /* Check valid length */
c617c0c6 476 if (len == 0) {
e065084a
DG
477 return -1;
478 }
479
480 return lttcomm_send_unix_sock(sock, buf, len);
481}
482
5461b305 483/*
d063d709 484 * Free memory of a command context structure.
5461b305 485 */
a2fb29a5 486static void clean_command_ctx(struct command_ctx **cmd_ctx)
5461b305 487{
a2fb29a5
DG
488 DBG("Clean command context structure");
489 if (*cmd_ctx) {
490 if ((*cmd_ctx)->llm) {
491 free((*cmd_ctx)->llm);
5461b305 492 }
a2fb29a5
DG
493 if ((*cmd_ctx)->lsm) {
494 free((*cmd_ctx)->lsm);
5461b305 495 }
a2fb29a5
DG
496 free(*cmd_ctx);
497 *cmd_ctx = NULL;
5461b305
DG
498 }
499}
500
fac6795d 501/*
0fdd1e2c 502 * Notify UST applications using the shm mmap futex.
fac6795d 503 */
0fdd1e2c 504static int notify_ust_apps(int active)
fac6795d 505{
0fdd1e2c 506 char *wait_shm_mmap;
fac6795d 507
0fdd1e2c 508 DBG("Notifying applications of session daemon state: %d", active);
e07ae692 509
0fdd1e2c
DG
510 /* See shm.c for this call implying mmap, shm and futex calls */
511 wait_shm_mmap = shm_ust_get_mmap(wait_shm_path, is_root);
512 if (wait_shm_mmap == NULL) {
fac6795d
DG
513 goto error;
514 }
515
0fdd1e2c
DG
516 /* Wake waiting process */
517 futex_wait_update((int32_t *) wait_shm_mmap, active);
518
519 /* Apps notified successfully */
520 return 0;
fac6795d
DG
521
522error:
0fdd1e2c 523 return -1;
fac6795d
DG
524}
525
e065084a 526/*
d063d709
DG
527 * Setup the outgoing data buffer for the response (llm) by allocating the
528 * right amount of memory and copying the original information from the lsm
529 * structure.
ca95a216 530 *
d063d709 531 * Return total size of the buffer pointed by buf.
ca95a216 532 */
5461b305 533static int setup_lttng_msg(struct command_ctx *cmd_ctx, size_t size)
ca95a216 534{
f3ed775e 535 int ret, buf_size;
ca95a216 536
f3ed775e 537 buf_size = size;
5461b305 538
ba7f0ae5 539 cmd_ctx->llm = zmalloc(sizeof(struct lttcomm_lttng_msg) + buf_size);
5461b305 540 if (cmd_ctx->llm == NULL) {
76d7553f 541 PERROR("zmalloc");
5461b305 542 ret = -ENOMEM;
ca95a216
DG
543 goto error;
544 }
545
5461b305
DG
546 /* Copy common data */
547 cmd_ctx->llm->cmd_type = cmd_ctx->lsm->cmd_type;
9f19cc17 548 cmd_ctx->llm->pid = cmd_ctx->lsm->domain.attr.pid;
5461b305 549
5461b305
DG
550 cmd_ctx->llm->data_size = size;
551 cmd_ctx->lttng_msg_size = sizeof(struct lttcomm_lttng_msg) + buf_size;
552
ca95a216
DG
553 return buf_size;
554
555error:
556 return ret;
557}
558
7a485870 559/*
5eb91c98 560 * Update the kernel poll set of all channel fd available over all tracing
d063d709 561 * session. Add the wakeup pipe at the end of the set.
7a485870 562 */
5eb91c98 563static int update_kernel_poll(struct lttng_poll_event *events)
7a485870 564{
5eb91c98 565 int ret;
7a485870
DG
566 struct ltt_session *session;
567 struct ltt_kernel_channel *channel;
568
5eb91c98 569 DBG("Updating kernel poll set");
7a485870 570
54d01ffb 571 session_lock_list();
b5541356 572 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
54d01ffb 573 session_lock(session);
7a485870 574 if (session->kernel_session == NULL) {
54d01ffb 575 session_unlock(session);
7a485870
DG
576 continue;
577 }
7a485870 578
54d01ffb
DG
579 cds_list_for_each_entry(channel,
580 &session->kernel_session->channel_list.head, list) {
5eb91c98
DG
581 /* Add channel fd to the kernel poll set */
582 ret = lttng_poll_add(events, channel->fd, LPOLLIN | LPOLLRDNORM);
583 if (ret < 0) {
54d01ffb 584 session_unlock(session);
5eb91c98
DG
585 goto error;
586 }
587 DBG("Channel fd %d added to kernel set", channel->fd);
7a485870 588 }
54d01ffb 589 session_unlock(session);
7a485870 590 }
54d01ffb 591 session_unlock_list();
7a485870 592
5eb91c98 593 return 0;
7a485870
DG
594
595error:
54d01ffb 596 session_unlock_list();
7a485870
DG
597 return -1;
598}
599
600/*
54d01ffb 601 * Find the channel fd from 'fd' over all tracing session. When found, check
d063d709 602 * for new channel stream and send those stream fds to the kernel consumer.
7a485870 603 *
d063d709 604 * Useful for CPU hotplug feature.
7a485870 605 */
2bdd86d4 606static int update_kernel_stream(struct consumer_data *consumer_data, int fd)
7a485870
DG
607{
608 int ret = 0;
609 struct ltt_session *session;
173af62f 610 struct ltt_kernel_session *ksess;
7a485870
DG
611 struct ltt_kernel_channel *channel;
612
613 DBG("Updating kernel streams for channel fd %d", fd);
614
54d01ffb 615 session_lock_list();
b5541356 616 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
54d01ffb 617 session_lock(session);
7a485870 618 if (session->kernel_session == NULL) {
54d01ffb 619 session_unlock(session);
7a485870
DG
620 continue;
621 }
173af62f 622 ksess = session->kernel_session;
d9800920 623
173af62f 624 cds_list_for_each_entry(channel, &ksess->channel_list.head, list) {
7a485870
DG
625 if (channel->fd == fd) {
626 DBG("Channel found, updating kernel streams");
627 ret = kernel_open_channel_stream(channel);
628 if (ret < 0) {
b3c750d2 629 goto error;
7a485870 630 }
d9800920 631
7a485870 632 /*
5eb91c98
DG
633 * Have we already sent fds to the consumer? If yes, it means
634 * that tracing is started so it is safe to send our updated
635 * stream fds.
7a485870 636 */
173af62f
DG
637 if (ksess->consumer_fds_sent == 1 && ksess->consumer != NULL) {
638 struct lttng_ht_iter iter;
639 struct consumer_socket *socket;
640
e7fe706f 641 rcu_read_lock();
173af62f
DG
642 cds_lfht_for_each_entry(ksess->consumer->socks->ht,
643 &iter.iter, socket, node.node) {
644 /* Code flow error */
645 assert(socket->fd >= 0);
646
647 pthread_mutex_lock(socket->lock);
f50f23d9 648 ret = kernel_consumer_send_channel_stream(socket,
173af62f
DG
649 channel, ksess);
650 pthread_mutex_unlock(socket->lock);
651 if (ret < 0) {
e7fe706f 652 rcu_read_unlock();
173af62f
DG
653 goto error;
654 }
7a485870 655 }
e7fe706f 656 rcu_read_unlock();
7a485870 657 }
b3c750d2 658 goto error;
7a485870
DG
659 }
660 }
54d01ffb 661 session_unlock(session);
7a485870 662 }
54d01ffb 663 session_unlock_list();
b3c750d2 664 return ret;
7a485870 665
b3c750d2 666error:
54d01ffb
DG
667 session_unlock(session);
668 session_unlock_list();
7a485870
DG
669 return ret;
670}
671
487cf67c
DG
672/*
673 * For each tracing session, update newly registered apps.
674 */
675static void update_ust_app(int app_sock)
676{
677 struct ltt_session *sess, *stmp;
678
4ee14516
DG
679 session_lock_list();
680
487cf67c
DG
681 /* For all tracing session(s) */
682 cds_list_for_each_entry_safe(sess, stmp, &session_list_ptr->head, list) {
4ee14516 683 session_lock(sess);
421cb601
DG
684 if (sess->ust_session) {
685 ust_app_global_update(sess->ust_session, app_sock);
686 }
4ee14516 687 session_unlock(sess);
487cf67c 688 }
4ee14516
DG
689
690 session_unlock_list();
487cf67c
DG
691}
692
7a485870 693/*
d063d709 694 * This thread manage event coming from the kernel.
7a485870 695 *
d063d709
DG
696 * Features supported in this thread:
697 * -) CPU Hotplug
7a485870
DG
698 */
699static void *thread_manage_kernel(void *data)
700{
139ac872 701 int ret, i, pollfd, update_poll_flag = 1, err = -1;
5eb91c98 702 uint32_t revents, nb_fd;
7a485870 703 char tmp;
5eb91c98 704 struct lttng_poll_event events;
7a485870 705
6993eeb3 706 DBG("[thread] Thread manage kernel started");
7a485870 707
927ca06a
DG
708 health_register(HEALTH_TYPE_KERNEL);
709
d5d63bf1
DG
710 /*
711 * This first step of the while is to clean this structure which could free
712 * non NULL pointers so zero it before the loop.
713 */
714 memset(&events, 0, sizeof(events));
715
6993eeb3
CB
716 if (testpoint(thread_manage_kernel)) {
717 goto error_testpoint;
718 }
8ac94142 719
44a5e5eb
DG
720 health_code_update(&health_thread_kernel);
721
6993eeb3 722 if (testpoint(thread_manage_kernel_before_loop)) {
d21b0d71 723 goto error_testpoint;
6993eeb3
CB
724 }
725
7a485870 726 while (1) {
44a5e5eb
DG
727 health_code_update(&health_thread_kernel);
728
7a485870 729 if (update_poll_flag == 1) {
d21b0d71
DG
730 /* Clean events object. We are about to populate it again. */
731 lttng_poll_clean(&events);
732
733 ret = create_thread_poll_set(&events, 2);
734 if (ret < 0) {
735 goto error_poll_create;
736 }
737
738 ret = lttng_poll_add(&events, kernel_poll_pipe[0], LPOLLIN);
739 if (ret < 0) {
740 goto error;
741 }
5f822d0a 742
d21b0d71 743 /* This will add the available kernel channel if any. */
5eb91c98
DG
744 ret = update_kernel_poll(&events);
745 if (ret < 0) {
7a485870
DG
746 goto error;
747 }
748 update_poll_flag = 0;
749 }
750
d21b0d71 751 DBG("Thread kernel polling on %d fds", LTTNG_POLL_GETNB(&events));
7a485870
DG
752
753 /* Poll infinite value of time */
88f2b785 754 restart:
44a5e5eb 755 health_poll_update(&health_thread_kernel);
5eb91c98 756 ret = lttng_poll_wait(&events, -1);
44a5e5eb 757 health_poll_update(&health_thread_kernel);
7a485870 758 if (ret < 0) {
88f2b785
MD
759 /*
760 * Restart interrupted system call.
761 */
762 if (errno == EINTR) {
763 goto restart;
764 }
7a485870
DG
765 goto error;
766 } else if (ret == 0) {
767 /* Should not happen since timeout is infinite */
85611738
DG
768 ERR("Return value of poll is 0 with an infinite timeout.\n"
769 "This should not have happened! Continuing...");
7a485870
DG
770 continue;
771 }
772
0d9c5d77
DG
773 nb_fd = ret;
774
5eb91c98
DG
775 for (i = 0; i < nb_fd; i++) {
776 /* Fetch once the poll data */
777 revents = LTTNG_POLL_GETEV(&events, i);
778 pollfd = LTTNG_POLL_GETFD(&events, i);
7a485870 779
44a5e5eb
DG
780 health_code_update(&health_thread_kernel);
781
5eb91c98
DG
782 /* Thread quit pipe has been closed. Killing thread. */
783 ret = check_thread_quit_pipe(pollfd, revents);
784 if (ret) {
139ac872
MD
785 err = 0;
786 goto exit;
5eb91c98 787 }
7a485870 788
5eb91c98
DG
789 /* Check for data on kernel pipe */
790 if (pollfd == kernel_poll_pipe[0] && (revents & LPOLLIN)) {
f921c78f
DG
791 do {
792 ret = read(kernel_poll_pipe[0], &tmp, 1);
793 } while (ret < 0 && errno == EINTR);
794 /*
795 * Ret value is useless here, if this pipe gets any actions an
796 * update is required anyway.
797 */
5eb91c98
DG
798 update_poll_flag = 1;
799 continue;
800 } else {
801 /*
802 * New CPU detected by the kernel. Adding kernel stream to
803 * kernel session and updating the kernel consumer
804 */
805 if (revents & LPOLLIN) {
2bdd86d4 806 ret = update_kernel_stream(&kconsumer_data, pollfd);
5eb91c98
DG
807 if (ret < 0) {
808 continue;
809 }
810 break;
811 /*
812 * TODO: We might want to handle the LPOLLERR | LPOLLHUP
813 * and unregister kernel stream at this point.
814 */
7a485870 815 }
7a485870
DG
816 }
817 }
818 }
819
139ac872 820exit:
7a485870 821error:
5eb91c98 822 lttng_poll_clean(&events);
76d7553f 823error_poll_create:
6993eeb3 824error_testpoint:
6620da75
DG
825 utils_close_pipe(kernel_poll_pipe);
826 kernel_poll_pipe[0] = kernel_poll_pipe[1] = -1;
139ac872
MD
827 if (err) {
828 health_error(&health_thread_kernel);
829 ERR("Health error occurred in %s", __func__);
6620da75
DG
830 WARN("Kernel thread died unexpectedly. "
831 "Kernel tracing can continue but CPU hotplug is disabled.");
139ac872 832 }
927ca06a 833 health_unregister();
76d7553f 834 DBG("Kernel thread dying");
7a485870
DG
835 return NULL;
836}
837
a23ec3a7
DG
838/*
839 * Signal pthread condition of the consumer data that the thread.
840 */
841static void signal_consumer_condition(struct consumer_data *data, int state)
842{
843 pthread_mutex_lock(&data->cond_mutex);
844
845 /*
846 * The state is set before signaling. It can be any value, it's the waiter
847 * job to correctly interpret this condition variable associated to the
848 * consumer pthread_cond.
849 *
850 * A value of 0 means that the corresponding thread of the consumer data
851 * was not started. 1 indicates that the thread has started and is ready
852 * for action. A negative value means that there was an error during the
853 * thread bootstrap.
854 */
855 data->consumer_thread_is_ready = state;
856 (void) pthread_cond_signal(&data->cond);
857
858 pthread_mutex_unlock(&data->cond_mutex);
859}
860
1d4b027a 861/*
3bd1e081 862 * This thread manage the consumer error sent back to the session daemon.
1d4b027a 863 */
3bd1e081 864static void *thread_manage_consumer(void *data)
1d4b027a 865{
139ac872 866 int sock = -1, i, ret, pollfd, err = -1;
5eb91c98 867 uint32_t revents, nb_fd;
1d4b027a 868 enum lttcomm_return_code code;
5eb91c98 869 struct lttng_poll_event events;
3bd1e081 870 struct consumer_data *consumer_data = data;
1d4b027a 871
3bd1e081 872 DBG("[thread] Manage consumer started");
1d4b027a 873
927ca06a
DG
874 health_register(HEALTH_TYPE_CONSUMER);
875
9449cc75
DG
876 /*
877 * Since the consumer thread can be spawned at any moment in time, we init
878 * the health to a poll status (1, which is a valid health over time).
879 * When the thread starts, we update here the health to a "code" path being
880 * an even value so this thread, when reaching a poll wait, does not
881 * trigger an error with an even value.
882 *
883 * Here is the use case we avoid.
884 *
885 * +1: the first poll update during initialization (main())
886 * +2 * x: multiple code update once in this thread.
887 * +1: poll wait in this thread (being a good health state).
888 * == even number which after the wait period shows as a bad health.
889 *
890 * In a nutshell, the following poll update to the health state brings back
891 * the state to an even value meaning a code path.
892 */
893 health_poll_update(&consumer_data->health);
894
5eb91c98
DG
895 /*
896 * Pass 2 as size here for the thread quit pipe and kconsumerd_err_sock.
897 * Nothing more will be added to this poll set.
898 */
899 ret = create_thread_poll_set(&events, 2);
900 if (ret < 0) {
76d7553f 901 goto error_poll;
5eb91c98 902 }
273ea72c 903
edb8b045
DG
904 /*
905 * The error socket here is already in a listening state which was done
906 * just before spawning this thread to avoid a race between the consumer
907 * daemon exec trying to connect and the listen() call.
908 */
3bd1e081 909 ret = lttng_poll_add(&events, consumer_data->err_sock, LPOLLIN | LPOLLRDHUP);
5eb91c98
DG
910 if (ret < 0) {
911 goto error;
912 }
913
44a5e5eb
DG
914 health_code_update(&consumer_data->health);
915
273ea72c 916 /* Inifinite blocking call, waiting for transmission */
88f2b785 917restart:
44a5e5eb 918 health_poll_update(&consumer_data->health);
8ac94142 919
6993eeb3
CB
920 if (testpoint(thread_manage_consumer)) {
921 goto error;
922 }
8ac94142 923
5eb91c98 924 ret = lttng_poll_wait(&events, -1);
44a5e5eb 925 health_poll_update(&consumer_data->health);
273ea72c 926 if (ret < 0) {
88f2b785
MD
927 /*
928 * Restart interrupted system call.
929 */
930 if (errno == EINTR) {
931 goto restart;
932 }
273ea72c
DG
933 goto error;
934 }
935
0d9c5d77
DG
936 nb_fd = ret;
937
5eb91c98
DG
938 for (i = 0; i < nb_fd; i++) {
939 /* Fetch once the poll data */
940 revents = LTTNG_POLL_GETEV(&events, i);
941 pollfd = LTTNG_POLL_GETFD(&events, i);
942
44a5e5eb
DG
943 health_code_update(&consumer_data->health);
944
5eb91c98
DG
945 /* Thread quit pipe has been closed. Killing thread. */
946 ret = check_thread_quit_pipe(pollfd, revents);
947 if (ret) {
139ac872
MD
948 err = 0;
949 goto exit;
5eb91c98
DG
950 }
951
952 /* Event on the registration socket */
3bd1e081 953 if (pollfd == consumer_data->err_sock) {
5eb91c98 954 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
3bd1e081 955 ERR("consumer err socket poll error");
5eb91c98
DG
956 goto error;
957 }
958 }
273ea72c
DG
959 }
960
3bd1e081 961 sock = lttcomm_accept_unix_sock(consumer_data->err_sock);
1d4b027a
DG
962 if (sock < 0) {
963 goto error;
964 }
965
b662582b
DG
966 /*
967 * Set the CLOEXEC flag. Return code is useless because either way, the
968 * show must go on.
969 */
970 (void) utils_set_fd_cloexec(sock);
971
44a5e5eb
DG
972 health_code_update(&consumer_data->health);
973
3bd1e081 974 DBG2("Receiving code from consumer err_sock");
ee0b0061 975
712ea556 976 /* Getting status code from kconsumerd */
54d01ffb
DG
977 ret = lttcomm_recv_unix_sock(sock, &code,
978 sizeof(enum lttcomm_return_code));
1d4b027a
DG
979 if (ret <= 0) {
980 goto error;
981 }
982
44a5e5eb
DG
983 health_code_update(&consumer_data->health);
984
f73fabfd 985 if (code == LTTCOMM_CONSUMERD_COMMAND_SOCK_READY) {
3bd1e081
MD
986 consumer_data->cmd_sock =
987 lttcomm_connect_unix_sock(consumer_data->cmd_unix_sock_path);
988 if (consumer_data->cmd_sock < 0) {
a23ec3a7
DG
989 /* On error, signal condition and quit. */
990 signal_consumer_condition(consumer_data, -1);
48842b30 991 PERROR("consumer connect");
1d4b027a
DG
992 goto error;
993 }
a23ec3a7
DG
994 signal_consumer_condition(consumer_data, 1);
995 DBG("Consumer command socket ready");
1d4b027a 996 } else {
3bd1e081 997 ERR("consumer error when waiting for SOCK_READY : %s",
1d4b027a
DG
998 lttcomm_get_readable_code(-code));
999 goto error;
1000 }
1001
54d01ffb 1002 /* Remove the kconsumerd error sock since we've established a connexion */
3bd1e081 1003 ret = lttng_poll_del(&events, consumer_data->err_sock);
72079cae 1004 if (ret < 0) {
72079cae
DG
1005 goto error;
1006 }
1007
5eb91c98
DG
1008 ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLRDHUP);
1009 if (ret < 0) {
72079cae 1010 goto error;
5eb91c98
DG
1011 }
1012
44a5e5eb
DG
1013 health_code_update(&consumer_data->health);
1014
5eb91c98 1015 /* Inifinite blocking call, waiting for transmission */
88f2b785 1016restart_poll:
44a5e5eb 1017 health_poll_update(&consumer_data->health);
5eb91c98 1018 ret = lttng_poll_wait(&events, -1);
44a5e5eb 1019 health_poll_update(&consumer_data->health);
5eb91c98 1020 if (ret < 0) {
88f2b785
MD
1021 /*
1022 * Restart interrupted system call.
1023 */
1024 if (errno == EINTR) {
1025 goto restart_poll;
1026 }
72079cae
DG
1027 goto error;
1028 }
1029
0d9c5d77
DG
1030 nb_fd = ret;
1031
5eb91c98
DG
1032 for (i = 0; i < nb_fd; i++) {
1033 /* Fetch once the poll data */
1034 revents = LTTNG_POLL_GETEV(&events, i);
1035 pollfd = LTTNG_POLL_GETFD(&events, i);
1036
44a5e5eb
DG
1037 health_code_update(&consumer_data->health);
1038
5eb91c98
DG
1039 /* Thread quit pipe has been closed. Killing thread. */
1040 ret = check_thread_quit_pipe(pollfd, revents);
1041 if (ret) {
139ac872
MD
1042 err = 0;
1043 goto exit;
5eb91c98
DG
1044 }
1045
1046 /* Event on the kconsumerd socket */
1047 if (pollfd == sock) {
1048 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
3bd1e081 1049 ERR("consumer err socket second poll error");
5eb91c98
DG
1050 goto error;
1051 }
1052 }
1053 }
1054
44a5e5eb
DG
1055 health_code_update(&consumer_data->health);
1056
712ea556 1057 /* Wait for any kconsumerd error */
54d01ffb
DG
1058 ret = lttcomm_recv_unix_sock(sock, &code,
1059 sizeof(enum lttcomm_return_code));
712ea556 1060 if (ret <= 0) {
3bd1e081 1061 ERR("consumer closed the command socket");
712ea556 1062 goto error;
6f61d021 1063 }
1d4b027a 1064
3bd1e081 1065 ERR("consumer return code : %s", lttcomm_get_readable_code(-code));
712ea556 1066
139ac872 1067exit:
1d4b027a 1068error:
5c827ce0
DG
1069 /* Immediately set the consumerd state to stopped */
1070 if (consumer_data->type == LTTNG_CONSUMER_KERNEL) {
1071 uatomic_set(&kernel_consumerd_state, CONSUMER_ERROR);
1072 } else if (consumer_data->type == LTTNG_CONSUMER64_UST ||
1073 consumer_data->type == LTTNG_CONSUMER32_UST) {
1074 uatomic_set(&ust_consumerd_state, CONSUMER_ERROR);
1075 } else {
1076 /* Code flow error... */
1077 assert(0);
1078 }
1079
76d7553f
MD
1080 if (consumer_data->err_sock >= 0) {
1081 ret = close(consumer_data->err_sock);
1082 if (ret) {
1083 PERROR("close");
1084 }
1085 }
1086 if (consumer_data->cmd_sock >= 0) {
1087 ret = close(consumer_data->cmd_sock);
1088 if (ret) {
1089 PERROR("close");
1090 }
1091 }
1092 if (sock >= 0) {
1093 ret = close(sock);
1094 if (ret) {
1095 PERROR("close");
1096 }
1097 }
273ea72c 1098
3bd1e081
MD
1099 unlink(consumer_data->err_unix_sock_path);
1100 unlink(consumer_data->cmd_unix_sock_path);
1101 consumer_data->pid = 0;
1d4b027a 1102
5eb91c98 1103 lttng_poll_clean(&events);
76d7553f 1104error_poll:
139ac872
MD
1105 if (err) {
1106 health_error(&consumer_data->health);
1107 ERR("Health error occurred in %s", __func__);
1108 }
927ca06a 1109 health_unregister();
76d7553f 1110 DBG("consumer thread cleanup completed");
0177d773 1111
5eb91c98 1112 return NULL;
099e26bd
DG
1113}
1114
099e26bd
DG
1115/*
1116 * This thread manage application communication.
1d4b027a
DG
1117 */
1118static void *thread_manage_apps(void *data)
099e26bd 1119{
139ac872 1120 int i, ret, pollfd, err = -1;
5eb91c98 1121 uint32_t revents, nb_fd;
099e26bd 1122 struct ust_command ust_cmd;
5eb91c98 1123 struct lttng_poll_event events;
099e26bd
DG
1124
1125 DBG("[thread] Manage application started");
1126
f6a9efaa
DG
1127 rcu_register_thread();
1128 rcu_thread_online();
1129
927ca06a
DG
1130 health_register(HEALTH_TYPE_APP_MANAGE);
1131
6993eeb3
CB
1132 if (testpoint(thread_manage_apps)) {
1133 goto error_testpoint;
1134 }
1135
139ac872 1136 health_code_update(&health_thread_app_manage);
44a5e5eb 1137
5eb91c98
DG
1138 ret = create_thread_poll_set(&events, 2);
1139 if (ret < 0) {
76d7553f 1140 goto error_poll_create;
5eb91c98 1141 }
099e26bd 1142
5eb91c98
DG
1143 ret = lttng_poll_add(&events, apps_cmd_pipe[0], LPOLLIN | LPOLLRDHUP);
1144 if (ret < 0) {
1145 goto error;
1146 }
099e26bd 1147
6993eeb3
CB
1148 if (testpoint(thread_manage_apps_before_loop)) {
1149 goto error;
1150 }
8ac94142 1151
139ac872 1152 health_code_update(&health_thread_app_manage);
44a5e5eb 1153
5eb91c98 1154 while (1) {
d21b0d71 1155 DBG("Apps thread polling on %d fds", LTTNG_POLL_GETNB(&events));
099e26bd
DG
1156
1157 /* Inifinite blocking call, waiting for transmission */
88f2b785 1158 restart:
139ac872 1159 health_poll_update(&health_thread_app_manage);
5eb91c98 1160 ret = lttng_poll_wait(&events, -1);
139ac872 1161 health_poll_update(&health_thread_app_manage);
099e26bd 1162 if (ret < 0) {
88f2b785
MD
1163 /*
1164 * Restart interrupted system call.
1165 */
1166 if (errno == EINTR) {
1167 goto restart;
1168 }
099e26bd
DG
1169 goto error;
1170 }
1171
0d9c5d77
DG
1172 nb_fd = ret;
1173
5eb91c98
DG
1174 for (i = 0; i < nb_fd; i++) {
1175 /* Fetch once the poll data */
1176 revents = LTTNG_POLL_GETEV(&events, i);
1177 pollfd = LTTNG_POLL_GETFD(&events, i);
1178
139ac872 1179 health_code_update(&health_thread_app_manage);
44a5e5eb 1180
5eb91c98
DG
1181 /* Thread quit pipe has been closed. Killing thread. */
1182 ret = check_thread_quit_pipe(pollfd, revents);
1183 if (ret) {
139ac872
MD
1184 err = 0;
1185 goto exit;
5eb91c98 1186 }
099e26bd 1187
5eb91c98
DG
1188 /* Inspect the apps cmd pipe */
1189 if (pollfd == apps_cmd_pipe[0]) {
1190 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1191 ERR("Apps command pipe error");
0177d773 1192 goto error;
5eb91c98
DG
1193 } else if (revents & LPOLLIN) {
1194 /* Empty pipe */
f921c78f
DG
1195 do {
1196 ret = read(apps_cmd_pipe[0], &ust_cmd, sizeof(ust_cmd));
1197 } while (ret < 0 && errno == EINTR);
5eb91c98 1198 if (ret < 0 || ret < sizeof(ust_cmd)) {
76d7553f 1199 PERROR("read apps cmd pipe");
5eb91c98
DG
1200 goto error;
1201 }
099e26bd 1202
139ac872 1203 health_code_update(&health_thread_app_manage);
44a5e5eb 1204
5eb91c98 1205 /* Register applicaton to the session daemon */
56fff090 1206 ret = ust_app_register(&ust_cmd.reg_msg,
54d01ffb 1207 ust_cmd.sock);
88ff5b7f 1208 if (ret == -ENOMEM) {
5eb91c98 1209 goto error;
88ff5b7f
MD
1210 } else if (ret < 0) {
1211 break;
5eb91c98
DG
1212 }
1213
139ac872 1214 health_code_update(&health_thread_app_manage);
44a5e5eb 1215
acc7b41b 1216 /*
e0c7ec2b 1217 * Validate UST version compatibility.
acc7b41b 1218 */
e0c7ec2b
DG
1219 ret = ust_app_validate_version(ust_cmd.sock);
1220 if (ret >= 0) {
1221 /*
1222 * Add channel(s) and event(s) to newly registered apps
1223 * from lttng global UST domain.
1224 */
1225 update_ust_app(ust_cmd.sock);
1226 }
acc7b41b 1227
139ac872 1228 health_code_update(&health_thread_app_manage);
44a5e5eb 1229
f2ca2e25 1230 ret = ust_app_register_done(ust_cmd.sock);
5eb91c98
DG
1231 if (ret < 0) {
1232 /*
1233 * If the registration is not possible, we simply
1234 * unregister the apps and continue
1235 */
56fff090 1236 ust_app_unregister(ust_cmd.sock);
5eb91c98
DG
1237 } else {
1238 /*
622c9ecf
DG
1239 * We only monitor the error events of the socket. This
1240 * thread does not handle any incoming data from UST
1241 * (POLLIN).
5eb91c98 1242 */
622c9ecf
DG
1243 ret = lttng_poll_add(&events, ust_cmd.sock,
1244 LPOLLERR & LPOLLHUP & LPOLLRDHUP);
5eb91c98
DG
1245 if (ret < 0) {
1246 goto error;
1247 }
1248
ae9e45b3
DG
1249 /* Set socket timeout for both receiving and ending */
1250 (void) lttcomm_setsockopt_rcv_timeout(ust_cmd.sock,
1251 app_socket_timeout);
1252 (void) lttcomm_setsockopt_snd_timeout(ust_cmd.sock,
1253 app_socket_timeout);
1254
54d01ffb
DG
1255 DBG("Apps with sock %d added to poll set",
1256 ust_cmd.sock);
5eb91c98 1257 }
487cf67c 1258
139ac872 1259 health_code_update(&health_thread_app_manage);
44a5e5eb 1260
5eb91c98 1261 break;
0177d773 1262 }
5eb91c98
DG
1263 } else {
1264 /*
54d01ffb
DG
1265 * At this point, we know that a registered application made
1266 * the event at poll_wait.
5eb91c98
DG
1267 */
1268 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1269 /* Removing from the poll set */
1270 ret = lttng_poll_del(&events, pollfd);
1271 if (ret < 0) {
1272 goto error;
1273 }
099e26bd 1274
b9d9b220 1275 /* Socket closed on remote end. */
56fff090 1276 ust_app_unregister(pollfd);
5eb91c98
DG
1277 break;
1278 }
099e26bd 1279 }
44a5e5eb 1280
139ac872 1281 health_code_update(&health_thread_app_manage);
099e26bd 1282 }
099e26bd
DG
1283 }
1284
139ac872 1285exit:
099e26bd 1286error:
5eb91c98 1287 lttng_poll_clean(&events);
76d7553f 1288error_poll_create:
6993eeb3 1289error_testpoint:
6620da75
DG
1290 utils_close_pipe(apps_cmd_pipe);
1291 apps_cmd_pipe[0] = apps_cmd_pipe[1] = -1;
1292
1293 /*
1294 * We don't clean the UST app hash table here since already registered
1295 * applications can still be controlled so let them be until the session
1296 * daemon dies or the applications stop.
1297 */
1298
139ac872
MD
1299 if (err) {
1300 health_error(&health_thread_app_manage);
1301 ERR("Health error occurred in %s", __func__);
1302 }
927ca06a 1303 health_unregister();
76d7553f 1304 DBG("Application communication apps thread cleanup complete");
f6a9efaa
DG
1305 rcu_thread_offline();
1306 rcu_unregister_thread();
099e26bd
DG
1307 return NULL;
1308}
1309
1310/*
1311 * Dispatch request from the registration threads to the application
1312 * communication thread.
1313 */
1314static void *thread_dispatch_ust_registration(void *data)
1315{
1316 int ret;
1317 struct cds_wfq_node *node;
1318 struct ust_command *ust_cmd = NULL;
1319
1320 DBG("[thread] Dispatch UST command started");
1321
26c9d55e 1322 while (!CMM_LOAD_SHARED(dispatch_thread_exit)) {
099e26bd
DG
1323 /* Atomically prepare the queue futex */
1324 futex_nto1_prepare(&ust_cmd_queue.futex);
1325
1326 do {
1327 /* Dequeue command for registration */
1328 node = cds_wfq_dequeue_blocking(&ust_cmd_queue.queue);
1329 if (node == NULL) {
00a17c97 1330 DBG("Woken up but nothing in the UST command queue");
099e26bd
DG
1331 /* Continue thread execution */
1332 break;
1333 }
1334
1335 ust_cmd = caa_container_of(node, struct ust_command, node);
1336
2f50c8a3
DG
1337 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1338 " gid:%d sock:%d name:%s (version %d.%d)",
1339 ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid,
1340 ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid,
1341 ust_cmd->sock, ust_cmd->reg_msg.name,
1342 ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor);
099e26bd
DG
1343 /*
1344 * Inform apps thread of the new application registration. This
1345 * call is blocking so we can be assured that the data will be read
1346 * at some point in time or wait to the end of the world :)
1347 */
6620da75 1348 if (apps_cmd_pipe[1] >= 0) {
fbb3b395
DG
1349 do {
1350 ret = write(apps_cmd_pipe[1], ust_cmd,
1351 sizeof(struct ust_command));
1352 } while (ret < 0 && errno == EINTR);
4cec016f 1353 if (ret < 0 || ret != sizeof(struct ust_command)) {
6620da75
DG
1354 PERROR("write apps cmd pipe");
1355 if (errno == EBADF) {
1356 /*
1357 * We can't inform the application thread to process
1358 * registration. We will exit or else application
1359 * registration will not occur and tracing will never
1360 * start.
1361 */
1362 goto error;
1363 }
1364 }
1365 } else {
1366 /* Application manager thread is not available. */
1367 ret = close(ust_cmd->sock);
1368 if (ret < 0) {
1369 PERROR("close ust_cmd sock");
099e26bd
DG
1370 }
1371 }
1372 free(ust_cmd);
1373 } while (node != NULL);
1374
1375 /* Futex wait on queue. Blocking call on futex() */
1376 futex_nto1_wait(&ust_cmd_queue.futex);
1377 }
1378
1379error:
1380 DBG("Dispatch thread dying");
1381 return NULL;
1382}
1383
1384/*
1385 * This thread manage application registration.
1386 */
1387static void *thread_registration_apps(void *data)
1d4b027a 1388{
139ac872 1389 int sock = -1, i, ret, pollfd, err = -1;
5eb91c98
DG
1390 uint32_t revents, nb_fd;
1391 struct lttng_poll_event events;
099e26bd
DG
1392 /*
1393 * Get allocated in this thread, enqueued to a global queue, dequeued and
1394 * freed in the manage apps thread.
1395 */
1396 struct ust_command *ust_cmd = NULL;
1d4b027a 1397
099e26bd 1398 DBG("[thread] Manage application registration started");
1d4b027a 1399
927ca06a
DG
1400 health_register(HEALTH_TYPE_APP_REG);
1401
6993eeb3
CB
1402 if (testpoint(thread_registration_apps)) {
1403 goto error_testpoint;
1404 }
8ac94142 1405
1d4b027a
DG
1406 ret = lttcomm_listen_unix_sock(apps_sock);
1407 if (ret < 0) {
76d7553f 1408 goto error_listen;
1d4b027a
DG
1409 }
1410
5eb91c98
DG
1411 /*
1412 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
1413 * more will be added to this poll set.
1414 */
1415 ret = create_thread_poll_set(&events, 2);
1416 if (ret < 0) {
76d7553f 1417 goto error_create_poll;
5eb91c98 1418 }
273ea72c 1419
5eb91c98
DG
1420 /* Add the application registration socket */
1421 ret = lttng_poll_add(&events, apps_sock, LPOLLIN | LPOLLRDHUP);
1422 if (ret < 0) {
76d7553f 1423 goto error_poll_add;
5eb91c98 1424 }
273ea72c 1425
1d4b027a 1426 /* Notify all applications to register */
0fdd1e2c
DG
1427 ret = notify_ust_apps(1);
1428 if (ret < 0) {
1429 ERR("Failed to notify applications or create the wait shared memory.\n"
54d01ffb
DG
1430 "Execution continues but there might be problem for already\n"
1431 "running applications that wishes to register.");
0fdd1e2c 1432 }
1d4b027a
DG
1433
1434 while (1) {
1435 DBG("Accepting application registration");
273ea72c
DG
1436
1437 /* Inifinite blocking call, waiting for transmission */
88f2b785 1438 restart:
139ac872 1439 health_poll_update(&health_thread_app_reg);
5eb91c98 1440 ret = lttng_poll_wait(&events, -1);
139ac872 1441 health_poll_update(&health_thread_app_reg);
273ea72c 1442 if (ret < 0) {
88f2b785
MD
1443 /*
1444 * Restart interrupted system call.
1445 */
1446 if (errno == EINTR) {
1447 goto restart;
1448 }
273ea72c
DG
1449 goto error;
1450 }
1451
0d9c5d77
DG
1452 nb_fd = ret;
1453
5eb91c98 1454 for (i = 0; i < nb_fd; i++) {
139ac872
MD
1455 health_code_update(&health_thread_app_reg);
1456
5eb91c98
DG
1457 /* Fetch once the poll data */
1458 revents = LTTNG_POLL_GETEV(&events, i);
1459 pollfd = LTTNG_POLL_GETFD(&events, i);
273ea72c 1460
5eb91c98
DG
1461 /* Thread quit pipe has been closed. Killing thread. */
1462 ret = check_thread_quit_pipe(pollfd, revents);
1463 if (ret) {
139ac872
MD
1464 err = 0;
1465 goto exit;
90014c57 1466 }
1d4b027a 1467
5eb91c98
DG
1468 /* Event on the registration socket */
1469 if (pollfd == apps_sock) {
1470 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1471 ERR("Register apps socket poll error");
1472 goto error;
1473 } else if (revents & LPOLLIN) {
1474 sock = lttcomm_accept_unix_sock(apps_sock);
1475 if (sock < 0) {
1476 goto error;
1477 }
099e26bd 1478
b662582b
DG
1479 /*
1480 * Set the CLOEXEC flag. Return code is useless because
1481 * either way, the show must go on.
1482 */
1483 (void) utils_set_fd_cloexec(sock);
1484
5eb91c98 1485 /* Create UST registration command for enqueuing */
ba7f0ae5 1486 ust_cmd = zmalloc(sizeof(struct ust_command));
5eb91c98 1487 if (ust_cmd == NULL) {
76d7553f 1488 PERROR("ust command zmalloc");
5eb91c98
DG
1489 goto error;
1490 }
1d4b027a 1491
5eb91c98
DG
1492 /*
1493 * Using message-based transmissions to ensure we don't
1494 * have to deal with partially received messages.
1495 */
4063050c
MD
1496 ret = lttng_fd_get(LTTNG_FD_APPS, 1);
1497 if (ret < 0) {
1498 ERR("Exhausted file descriptors allowed for applications.");
1499 free(ust_cmd);
1500 ret = close(sock);
1501 if (ret) {
1502 PERROR("close");
1503 }
1504 sock = -1;
1505 continue;
1506 }
139ac872 1507 health_code_update(&health_thread_app_reg);
5eb91c98
DG
1508 ret = lttcomm_recv_unix_sock(sock, &ust_cmd->reg_msg,
1509 sizeof(struct ust_register_msg));
1510 if (ret < 0 || ret < sizeof(struct ust_register_msg)) {
1511 if (ret < 0) {
76d7553f 1512 PERROR("lttcomm_recv_unix_sock register apps");
5eb91c98
DG
1513 } else {
1514 ERR("Wrong size received on apps register");
1515 }
1516 free(ust_cmd);
76d7553f
MD
1517 ret = close(sock);
1518 if (ret) {
1519 PERROR("close");
1520 }
4063050c 1521 lttng_fd_put(LTTNG_FD_APPS, 1);
76d7553f 1522 sock = -1;
5eb91c98
DG
1523 continue;
1524 }
139ac872 1525 health_code_update(&health_thread_app_reg);
099e26bd 1526
5eb91c98 1527 ust_cmd->sock = sock;
34a2494f 1528 sock = -1;
099e26bd 1529
5eb91c98
DG
1530 DBG("UST registration received with pid:%d ppid:%d uid:%d"
1531 " gid:%d sock:%d name:%s (version %d.%d)",
1532 ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid,
1533 ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid,
1534 ust_cmd->sock, ust_cmd->reg_msg.name,
1535 ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor);
54d01ffb 1536
5eb91c98
DG
1537 /*
1538 * Lock free enqueue the registration request. The red pill
54d01ffb 1539 * has been taken! This apps will be part of the *system*.
5eb91c98
DG
1540 */
1541 cds_wfq_enqueue(&ust_cmd_queue.queue, &ust_cmd->node);
1542
1543 /*
1544 * Wake the registration queue futex. Implicit memory
1545 * barrier with the exchange in cds_wfq_enqueue.
1546 */
1547 futex_nto1_wake(&ust_cmd_queue.futex);
1548 }
1549 }
90014c57 1550 }
1d4b027a
DG
1551 }
1552
139ac872 1553exit:
1d4b027a 1554error:
139ac872
MD
1555 if (err) {
1556 health_error(&health_thread_app_reg);
1557 ERR("Health error occurred in %s", __func__);
1558 }
139ac872 1559
0fdd1e2c
DG
1560 /* Notify that the registration thread is gone */
1561 notify_ust_apps(0);
1562
a4b35e07 1563 if (apps_sock >= 0) {
76d7553f
MD
1564 ret = close(apps_sock);
1565 if (ret) {
1566 PERROR("close");
1567 }
a4b35e07 1568 }
46c3f085 1569 if (sock >= 0) {
76d7553f
MD
1570 ret = close(sock);
1571 if (ret) {
1572 PERROR("close");
1573 }
4063050c 1574 lttng_fd_put(LTTNG_FD_APPS, 1);
a4b35e07 1575 }
273ea72c 1576 unlink(apps_unix_sock_path);
0fdd1e2c 1577
76d7553f 1578error_poll_add:
5eb91c98 1579 lttng_poll_clean(&events);
76d7553f
MD
1580error_listen:
1581error_create_poll:
6993eeb3 1582error_testpoint:
76d7553f 1583 DBG("UST Registration thread cleanup complete");
927ca06a 1584 health_unregister();
5eb91c98 1585
1d4b027a
DG
1586 return NULL;
1587}
1588
8c0faa1d 1589/*
3bd1e081 1590 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
d063d709 1591 * exec or it will fails.
8c0faa1d 1592 */
3bd1e081 1593static int spawn_consumer_thread(struct consumer_data *consumer_data)
8c0faa1d 1594{
a23ec3a7 1595 int ret, clock_ret;
ee0b0061
DG
1596 struct timespec timeout;
1597
a23ec3a7
DG
1598 /* Make sure we set the readiness flag to 0 because we are NOT ready */
1599 consumer_data->consumer_thread_is_ready = 0;
8c0faa1d 1600
a23ec3a7
DG
1601 /* Setup pthread condition */
1602 ret = pthread_condattr_init(&consumer_data->condattr);
1603 if (ret != 0) {
1604 errno = ret;
1605 PERROR("pthread_condattr_init consumer data");
1606 goto error;
1607 }
1608
1609 /*
1610 * Set the monotonic clock in order to make sure we DO NOT jump in time
1611 * between the clock_gettime() call and the timedwait call. See bug #324
1612 * for a more details and how we noticed it.
1613 */
1614 ret = pthread_condattr_setclock(&consumer_data->condattr, CLOCK_MONOTONIC);
1615 if (ret != 0) {
1616 errno = ret;
1617 PERROR("pthread_condattr_setclock consumer data");
ee0b0061
DG
1618 goto error;
1619 }
8c0faa1d 1620
a23ec3a7
DG
1621 ret = pthread_cond_init(&consumer_data->cond, &consumer_data->condattr);
1622 if (ret != 0) {
1623 errno = ret;
1624 PERROR("pthread_cond_init consumer data");
1625 goto error;
1626 }
1627
1628 ret = pthread_create(&consumer_data->thread, NULL, thread_manage_consumer,
1629 consumer_data);
8c0faa1d 1630 if (ret != 0) {
3bd1e081 1631 PERROR("pthread_create consumer");
ee0b0061 1632 ret = -1;
8c0faa1d
DG
1633 goto error;
1634 }
1635
a23ec3a7
DG
1636 /* We are about to wait on a pthread condition */
1637 pthread_mutex_lock(&consumer_data->cond_mutex);
1638
ee0b0061 1639 /* Get time for sem_timedwait absolute timeout */
a23ec3a7
DG
1640 clock_ret = clock_gettime(CLOCK_MONOTONIC, &timeout);
1641 /*
1642 * Set the timeout for the condition timed wait even if the clock gettime
1643 * call fails since we might loop on that call and we want to avoid to
1644 * increment the timeout too many times.
1645 */
1646 timeout.tv_sec += DEFAULT_SEM_WAIT_TIMEOUT;
1647
1648 /*
1649 * The following loop COULD be skipped in some conditions so this is why we
1650 * set ret to 0 in order to make sure at least one round of the loop is
1651 * done.
1652 */
1653 ret = 0;
1654
1655 /*
1656 * Loop until the condition is reached or when a timeout is reached. Note
1657 * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT
1658 * be returned but the pthread_cond(3), from the glibc-doc, says that it is
1659 * possible. This loop does not take any chances and works with both of
1660 * them.
1661 */
1662 while (!consumer_data->consumer_thread_is_ready && ret != ETIMEDOUT) {
1663 if (clock_ret < 0) {
1664 PERROR("clock_gettime spawn consumer");
1665 /* Infinite wait for the consumerd thread to be ready */
1666 ret = pthread_cond_wait(&consumer_data->cond,
1667 &consumer_data->cond_mutex);
1668 } else {
1669 ret = pthread_cond_timedwait(&consumer_data->cond,
1670 &consumer_data->cond_mutex, &timeout);
1671 }
ee0b0061 1672 }
8c0faa1d 1673
a23ec3a7
DG
1674 /* Release the pthread condition */
1675 pthread_mutex_unlock(&consumer_data->cond_mutex);
1676
1677 if (ret != 0) {
1678 errno = ret;
1679 if (ret == ETIMEDOUT) {
ee0b0061
DG
1680 /*
1681 * Call has timed out so we kill the kconsumerd_thread and return
1682 * an error.
1683 */
a23ec3a7
DG
1684 ERR("Condition timed out. The consumer thread was never ready."
1685 " Killing it");
3bd1e081 1686 ret = pthread_cancel(consumer_data->thread);
ee0b0061 1687 if (ret < 0) {
3bd1e081 1688 PERROR("pthread_cancel consumer thread");
ee0b0061
DG
1689 }
1690 } else {
a23ec3a7 1691 PERROR("pthread_cond_wait failed consumer thread");
ee0b0061
DG
1692 }
1693 goto error;
1694 }
1695
3bd1e081
MD
1696 pthread_mutex_lock(&consumer_data->pid_mutex);
1697 if (consumer_data->pid == 0) {
a23ec3a7 1698 ERR("Consumerd did not start");
3bd1e081 1699 pthread_mutex_unlock(&consumer_data->pid_mutex);
712ea556
DG
1700 goto error;
1701 }
3bd1e081 1702 pthread_mutex_unlock(&consumer_data->pid_mutex);
712ea556 1703
8c0faa1d
DG
1704 return 0;
1705
1706error:
1707 return ret;
1708}
1709
d9800920 1710/*
3bd1e081 1711 * Join consumer thread
d9800920 1712 */
3bd1e081 1713static int join_consumer_thread(struct consumer_data *consumer_data)
cf3af59e
MD
1714{
1715 void *status;
cf3af59e 1716
e8209f6b
DG
1717 /* Consumer pid must be a real one. */
1718 if (consumer_data->pid > 0) {
c617c0c6 1719 int ret;
3bd1e081 1720 ret = kill(consumer_data->pid, SIGTERM);
cf3af59e 1721 if (ret) {
3bd1e081 1722 ERR("Error killing consumer daemon");
cf3af59e
MD
1723 return ret;
1724 }
3bd1e081 1725 return pthread_join(consumer_data->thread, &status);
cf3af59e
MD
1726 } else {
1727 return 0;
1728 }
1729}
1730
8c0faa1d 1731/*
3bd1e081 1732 * Fork and exec a consumer daemon (consumerd).
8c0faa1d 1733 *
d063d709 1734 * Return pid if successful else -1.
8c0faa1d 1735 */
3bd1e081 1736static pid_t spawn_consumerd(struct consumer_data *consumer_data)
8c0faa1d
DG
1737{
1738 int ret;
1739 pid_t pid;
94c55f17 1740 const char *consumer_to_use;
53086306 1741 const char *verbosity;
94c55f17 1742 struct stat st;
8c0faa1d 1743
3bd1e081 1744 DBG("Spawning consumerd");
c49dc785 1745
8c0faa1d
DG
1746 pid = fork();
1747 if (pid == 0) {
1748 /*
3bd1e081 1749 * Exec consumerd.
8c0faa1d 1750 */
daee5345 1751 if (opt_verbose_consumer) {
53086306
DG
1752 verbosity = "--verbose";
1753 } else {
1754 verbosity = "--quiet";
1755 }
3bd1e081
MD
1756 switch (consumer_data->type) {
1757 case LTTNG_CONSUMER_KERNEL:
94c55f17 1758 /*
c7704d57
DG
1759 * Find out which consumerd to execute. We will first try the
1760 * 64-bit path, then the sessiond's installation directory, and
1761 * fallback on the 32-bit one,
94c55f17 1762 */
63a799e8
AM
1763 DBG3("Looking for a kernel consumer at these locations:");
1764 DBG3(" 1) %s", consumerd64_bin);
1765 DBG3(" 2) %s/%s", INSTALL_BIN_PATH, CONSUMERD_FILE);
1766 DBG3(" 3) %s", consumerd32_bin);
94c55f17 1767 if (stat(consumerd64_bin, &st) == 0) {
63a799e8 1768 DBG3("Found location #1");
94c55f17 1769 consumer_to_use = consumerd64_bin;
94c55f17 1770 } else if (stat(INSTALL_BIN_PATH "/" CONSUMERD_FILE, &st) == 0) {
63a799e8 1771 DBG3("Found location #2");
94c55f17 1772 consumer_to_use = INSTALL_BIN_PATH "/" CONSUMERD_FILE;
eb1e0bd4 1773 } else if (stat(consumerd32_bin, &st) == 0) {
63a799e8 1774 DBG3("Found location #3");
eb1e0bd4 1775 consumer_to_use = consumerd32_bin;
94c55f17 1776 } else {
63a799e8 1777 DBG("Could not find any valid consumerd executable");
94c55f17
AM
1778 break;
1779 }
1780 DBG("Using kernel consumer at: %s", consumer_to_use);
1781 execl(consumer_to_use,
1782 "lttng-consumerd", verbosity, "-k",
1783 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
1784 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
1785 NULL);
3bd1e081 1786 break;
7753dea8
MD
1787 case LTTNG_CONSUMER64_UST:
1788 {
b1e0b6b6 1789 char *tmpnew = NULL;
8f4905da
MD
1790
1791 if (consumerd64_libdir[0] != '\0') {
1792 char *tmp;
1793 size_t tmplen;
1794
1795 tmp = getenv("LD_LIBRARY_PATH");
1796 if (!tmp) {
1797 tmp = "";
1798 }
1799 tmplen = strlen("LD_LIBRARY_PATH=")
1800 + strlen(consumerd64_libdir) + 1 /* : */ + strlen(tmp);
1801 tmpnew = zmalloc(tmplen + 1 /* \0 */);
1802 if (!tmpnew) {
1803 ret = -ENOMEM;
1804 goto error;
1805 }
1806 strcpy(tmpnew, "LD_LIBRARY_PATH=");
1807 strcat(tmpnew, consumerd64_libdir);
1808 if (tmp[0] != '\0') {
1809 strcat(tmpnew, ":");
1810 strcat(tmpnew, tmp);
1811 }
1812 ret = putenv(tmpnew);
1813 if (ret) {
1814 ret = -errno;
1815 goto error;
1816 }
1817 }
94c55f17 1818 DBG("Using 64-bit UST consumer at: %s", consumerd64_bin);
a5a6aff3 1819 ret = execl(consumerd64_bin, "lttng-consumerd", verbosity, "-u",
7753dea8
MD
1820 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
1821 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
1822 NULL);
8f4905da
MD
1823 if (consumerd64_libdir[0] != '\0') {
1824 free(tmpnew);
1825 }
1826 if (ret) {
1827 goto error;
1828 }
3bd1e081 1829 break;
7753dea8
MD
1830 }
1831 case LTTNG_CONSUMER32_UST:
1832 {
937dde8e 1833 char *tmpnew = NULL;
8f4905da
MD
1834
1835 if (consumerd32_libdir[0] != '\0') {
1836 char *tmp;
1837 size_t tmplen;
1838
1839 tmp = getenv("LD_LIBRARY_PATH");
1840 if (!tmp) {
1841 tmp = "";
1842 }
1843 tmplen = strlen("LD_LIBRARY_PATH=")
1844 + strlen(consumerd32_libdir) + 1 /* : */ + strlen(tmp);
1845 tmpnew = zmalloc(tmplen + 1 /* \0 */);
1846 if (!tmpnew) {
1847 ret = -ENOMEM;
1848 goto error;
1849 }
1850 strcpy(tmpnew, "LD_LIBRARY_PATH=");
1851 strcat(tmpnew, consumerd32_libdir);
1852 if (tmp[0] != '\0') {
1853 strcat(tmpnew, ":");
1854 strcat(tmpnew, tmp);
1855 }
1856 ret = putenv(tmpnew);
1857 if (ret) {
1858 ret = -errno;
1859 goto error;
1860 }
1861 }
94c55f17 1862 DBG("Using 32-bit UST consumer at: %s", consumerd32_bin);
a5a6aff3 1863 ret = execl(consumerd32_bin, "lttng-consumerd", verbosity, "-u",
7753dea8
MD
1864 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
1865 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
1866 NULL);
8f4905da
MD
1867 if (consumerd32_libdir[0] != '\0') {
1868 free(tmpnew);
1869 }
1870 if (ret) {
1871 goto error;
1872 }
7753dea8
MD
1873 break;
1874 }
3bd1e081 1875 default:
76d7553f 1876 PERROR("unknown consumer type");
3bd1e081
MD
1877 exit(EXIT_FAILURE);
1878 }
8c0faa1d 1879 if (errno != 0) {
76d7553f 1880 PERROR("kernel start consumer exec");
8c0faa1d
DG
1881 }
1882 exit(EXIT_FAILURE);
1883 } else if (pid > 0) {
1884 ret = pid;
8c0faa1d 1885 } else {
76d7553f 1886 PERROR("start consumer fork");
8c0faa1d 1887 ret = -errno;
8c0faa1d 1888 }
8f4905da 1889error:
8c0faa1d
DG
1890 return ret;
1891}
1892
693bd40b 1893/*
3bd1e081 1894 * Spawn the consumerd daemon and session daemon thread.
693bd40b 1895 */
3bd1e081 1896static int start_consumerd(struct consumer_data *consumer_data)
693bd40b 1897{
c617c0c6 1898 int ret;
edb8b045
DG
1899
1900 /*
1901 * Set the listen() state on the socket since there is a possible race
1902 * between the exec() of the consumer daemon and this call if place in the
1903 * consumer thread. See bug #366 for more details.
1904 */
1905 ret = lttcomm_listen_unix_sock(consumer_data->err_sock);
1906 if (ret < 0) {
1907 goto error;
1908 }
693bd40b 1909
3bd1e081
MD
1910 pthread_mutex_lock(&consumer_data->pid_mutex);
1911 if (consumer_data->pid != 0) {
1912 pthread_mutex_unlock(&consumer_data->pid_mutex);
c49dc785
DG
1913 goto end;
1914 }
693bd40b 1915
3bd1e081 1916 ret = spawn_consumerd(consumer_data);
c49dc785 1917 if (ret < 0) {
3bd1e081
MD
1918 ERR("Spawning consumerd failed");
1919 pthread_mutex_unlock(&consumer_data->pid_mutex);
c49dc785 1920 goto error;
693bd40b 1921 }
c49dc785 1922
3bd1e081
MD
1923 /* Setting up the consumer_data pid */
1924 consumer_data->pid = ret;
48842b30 1925 DBG2("Consumer pid %d", consumer_data->pid);
3bd1e081 1926 pthread_mutex_unlock(&consumer_data->pid_mutex);
693bd40b 1927
3bd1e081
MD
1928 DBG2("Spawning consumer control thread");
1929 ret = spawn_consumer_thread(consumer_data);
693bd40b 1930 if (ret < 0) {
3bd1e081 1931 ERR("Fatal error spawning consumer control thread");
693bd40b
DG
1932 goto error;
1933 }
1934
c49dc785 1935end:
693bd40b
DG
1936 return 0;
1937
1938error:
edb8b045
DG
1939 /* Cleanup already created socket on error. */
1940 if (consumer_data->err_sock >= 0) {
c617c0c6
MD
1941 int err;
1942
edb8b045
DG
1943 err = close(consumer_data->err_sock);
1944 if (err < 0) {
1945 PERROR("close consumer data error socket");
1946 }
1947 }
693bd40b
DG
1948 return ret;
1949}
1950
44a5e5eb 1951/*
139ac872
MD
1952 * Compute health status of each consumer. If one of them is zero (bad
1953 * state), we return 0.
44a5e5eb
DG
1954 */
1955static int check_consumer_health(void)
1956{
1957 int ret;
1958
927ca06a 1959 ret = health_check_state(HEALTH_TYPE_CONSUMER);
44a5e5eb
DG
1960
1961 DBG3("Health consumer check %d", ret);
1962
1963 return ret;
1964}
1965
b73401da 1966/*
096102bd 1967 * Setup necessary data for kernel tracer action.
b73401da 1968 */
096102bd 1969static int init_kernel_tracer(void)
b73401da
DG
1970{
1971 int ret;
b73401da 1972
096102bd
DG
1973 /* Modprobe lttng kernel modules */
1974 ret = modprobe_lttng_control();
b73401da 1975 if (ret < 0) {
b73401da
DG
1976 goto error;
1977 }
1978
096102bd
DG
1979 /* Open debugfs lttng */
1980 kernel_tracer_fd = open(module_proc_lttng, O_RDWR);
1981 if (kernel_tracer_fd < 0) {
1982 DBG("Failed to open %s", module_proc_lttng);
2f77fc4b
DG
1983 ret = -1;
1984 goto error_open;
54d01ffb
DG
1985 }
1986
2f77fc4b
DG
1987 /* Validate kernel version */
1988 ret = kernel_validate_version(kernel_tracer_fd);
1989 if (ret < 0) {
1990 goto error_version;
b551a063 1991 }
54d01ffb 1992
2f77fc4b
DG
1993 ret = modprobe_lttng_data();
1994 if (ret < 0) {
1995 goto error_modules;
54d01ffb
DG
1996 }
1997
2f77fc4b
DG
1998 DBG("Kernel tracer fd %d", kernel_tracer_fd);
1999 return 0;
2000
2001error_version:
2002 modprobe_remove_lttng_control();
2003 ret = close(kernel_tracer_fd);
2004 if (ret) {
2005 PERROR("close");
b551a063 2006 }
2f77fc4b 2007 kernel_tracer_fd = -1;
f73fabfd 2008 return LTTNG_ERR_KERN_VERSION;
b551a063 2009
2f77fc4b
DG
2010error_modules:
2011 ret = close(kernel_tracer_fd);
2012 if (ret) {
2013 PERROR("close");
b551a063 2014 }
54d01ffb 2015
2f77fc4b
DG
2016error_open:
2017 modprobe_remove_lttng_control();
54d01ffb
DG
2018
2019error:
2f77fc4b
DG
2020 WARN("No kernel tracer available");
2021 kernel_tracer_fd = -1;
2022 if (!is_root) {
f73fabfd 2023 return LTTNG_ERR_NEED_ROOT_SESSIOND;
2f77fc4b 2024 } else {
f73fabfd 2025 return LTTNG_ERR_KERN_NA;
2f77fc4b 2026 }
54d01ffb
DG
2027}
2028
2f77fc4b 2029
54d01ffb 2030/*
2f77fc4b
DG
2031 * Copy consumer output from the tracing session to the domain session. The
2032 * function also applies the right modification on a per domain basis for the
2033 * trace files destination directory.
54d01ffb 2034 */
2f77fc4b 2035static int copy_session_consumer(int domain, struct ltt_session *session)
54d01ffb
DG
2036{
2037 int ret;
2f77fc4b
DG
2038 const char *dir_name;
2039 struct consumer_output *consumer;
2040
2041 assert(session);
2042 assert(session->consumer);
54d01ffb 2043
b551a063
DG
2044 switch (domain) {
2045 case LTTNG_DOMAIN_KERNEL:
2f77fc4b 2046 DBG3("Copying tracing session consumer output in kernel session");
09a90bcd
DG
2047 /*
2048 * XXX: We should audit the session creation and what this function
2049 * does "extra" in order to avoid a destroy since this function is used
2050 * in the domain session creation (kernel and ust) only. Same for UST
2051 * domain.
2052 */
2053 if (session->kernel_session->consumer) {
2054 consumer_destroy_output(session->kernel_session->consumer);
2055 }
2f77fc4b
DG
2056 session->kernel_session->consumer =
2057 consumer_copy_output(session->consumer);
2058 /* Ease our life a bit for the next part */
2059 consumer = session->kernel_session->consumer;
2060 dir_name = DEFAULT_KERNEL_TRACE_DIR;
b551a063
DG
2061 break;
2062 case LTTNG_DOMAIN_UST:
2f77fc4b 2063 DBG3("Copying tracing session consumer output in UST session");
09a90bcd
DG
2064 if (session->ust_session->consumer) {
2065 consumer_destroy_output(session->ust_session->consumer);
2066 }
2f77fc4b
DG
2067 session->ust_session->consumer =
2068 consumer_copy_output(session->consumer);
2069 /* Ease our life a bit for the next part */
2070 consumer = session->ust_session->consumer;
2071 dir_name = DEFAULT_UST_TRACE_DIR;
b551a063
DG
2072 break;
2073 default:
f73fabfd 2074 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
54d01ffb
DG
2075 goto error;
2076 }
2077
2f77fc4b 2078 /* Append correct directory to subdir */
c30ce0b3
CB
2079 strncat(consumer->subdir, dir_name,
2080 sizeof(consumer->subdir) - strlen(consumer->subdir) - 1);
2f77fc4b
DG
2081 DBG3("Copy session consumer subdir %s", consumer->subdir);
2082
f73fabfd 2083 ret = LTTNG_OK;
54d01ffb
DG
2084
2085error:
2086 return ret;
2087}
2088
00e2e675 2089/*
2f77fc4b 2090 * Create an UST session and add it to the session ust list.
00e2e675 2091 */
2f77fc4b
DG
2092static int create_ust_session(struct ltt_session *session,
2093 struct lttng_domain *domain)
00e2e675
DG
2094{
2095 int ret;
2f77fc4b 2096 struct ltt_ust_session *lus = NULL;
00e2e675 2097
a4b92340 2098 assert(session);
2f77fc4b
DG
2099 assert(domain);
2100 assert(session->consumer);
a4b92340 2101
2f77fc4b
DG
2102 switch (domain->type) {
2103 case LTTNG_DOMAIN_UST:
2104 break;
2105 default:
2106 ERR("Unknown UST domain on create session %d", domain->type);
f73fabfd 2107 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
00e2e675
DG
2108 goto error;
2109 }
2110
2f77fc4b
DG
2111 DBG("Creating UST session");
2112
fdd9eb17 2113 lus = trace_ust_create_session(session->path, session->id);
2f77fc4b 2114 if (lus == NULL) {
f73fabfd 2115 ret = LTTNG_ERR_UST_SESS_FAIL;
a4b92340
DG
2116 goto error;
2117 }
2118
2f77fc4b
DG
2119 lus->uid = session->uid;
2120 lus->gid = session->gid;
2121 session->ust_session = lus;
00e2e675 2122
2f77fc4b
DG
2123 /* Copy session output to the newly created UST session */
2124 ret = copy_session_consumer(domain->type, session);
f73fabfd 2125 if (ret != LTTNG_OK) {
00e2e675
DG
2126 goto error;
2127 }
2128
f73fabfd 2129 return LTTNG_OK;
00e2e675
DG
2130
2131error:
2f77fc4b
DG
2132 free(lus);
2133 session->ust_session = NULL;
00e2e675
DG
2134 return ret;
2135}
2136
2137/*
2f77fc4b 2138 * Create a kernel tracer session then create the default channel.
00e2e675 2139 */
2f77fc4b 2140static int create_kernel_session(struct ltt_session *session)
00e2e675
DG
2141{
2142 int ret;
a4b92340 2143
2f77fc4b 2144 DBG("Creating kernel session");
00e2e675 2145
2f77fc4b
DG
2146 ret = kernel_create_session(session, kernel_tracer_fd);
2147 if (ret < 0) {
f73fabfd 2148 ret = LTTNG_ERR_KERN_SESS_FAIL;
00e2e675
DG
2149 goto error;
2150 }
2151
2f77fc4b
DG
2152 /* Code flow safety */
2153 assert(session->kernel_session);
2154
2155 /* Copy session output to the newly created Kernel session */
2156 ret = copy_session_consumer(LTTNG_DOMAIN_KERNEL, session);
f73fabfd 2157 if (ret != LTTNG_OK) {
a4b92340
DG
2158 goto error;
2159 }
2160
2f77fc4b
DG
2161 /* Create directory(ies) on local filesystem. */
2162 if (session->kernel_session->consumer->type == CONSUMER_DST_LOCAL &&
2163 strlen(session->kernel_session->consumer->dst.trace_path) > 0) {
2164 ret = run_as_mkdir_recursive(
2165 session->kernel_session->consumer->dst.trace_path,
2166 S_IRWXU | S_IRWXG, session->uid, session->gid);
2167 if (ret < 0) {
2168 if (ret != -EEXIST) {
2169 ERR("Trace directory creation error");
00e2e675
DG
2170 goto error;
2171 }
00e2e675 2172 }
2f77fc4b 2173 }
00e2e675 2174
2f77fc4b
DG
2175 session->kernel_session->uid = session->uid;
2176 session->kernel_session->gid = session->gid;
00e2e675 2177
f73fabfd 2178 return LTTNG_OK;
00e2e675 2179
2f77fc4b
DG
2180error:
2181 trace_kernel_destroy_session(session->kernel_session);
2182 session->kernel_session = NULL;
2183 return ret;
2184}
00e2e675 2185
2f77fc4b
DG
2186/*
2187 * Count number of session permitted by uid/gid.
2188 */
2189static unsigned int lttng_sessions_count(uid_t uid, gid_t gid)
2190{
2191 unsigned int i = 0;
2192 struct ltt_session *session;
07424f16 2193
2f77fc4b
DG
2194 DBG("Counting number of available session for UID %d GID %d",
2195 uid, gid);
2196 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
00e2e675 2197 /*
2f77fc4b 2198 * Only list the sessions the user can control.
00e2e675 2199 */
2f77fc4b
DG
2200 if (!session_access_ok(session, uid, gid)) {
2201 continue;
2202 }
2203 i++;
a4b92340 2204 }
2f77fc4b 2205 return i;
00e2e675
DG
2206}
2207
54d01ffb
DG
2208/*
2209 * Process the command requested by the lttng client within the command
2210 * context structure. This function make sure that the return structure (llm)
2211 * is set and ready for transmission before returning.
2212 *
2213 * Return any error encountered or 0 for success.
53a80697
MD
2214 *
2215 * "sock" is only used for special-case var. len data.
54d01ffb 2216 */
53a80697
MD
2217static int process_client_msg(struct command_ctx *cmd_ctx, int sock,
2218 int *sock_error)
54d01ffb 2219{
f73fabfd 2220 int ret = LTTNG_OK;
44d3bd01 2221 int need_tracing_session = 1;
2e09ba09 2222 int need_domain;
54d01ffb
DG
2223
2224 DBG("Processing client command %d", cmd_ctx->lsm->cmd_type);
2225
53a80697
MD
2226 *sock_error = 0;
2227
2e09ba09
MD
2228 switch (cmd_ctx->lsm->cmd_type) {
2229 case LTTNG_CREATE_SESSION:
2230 case LTTNG_DESTROY_SESSION:
2231 case LTTNG_LIST_SESSIONS:
2232 case LTTNG_LIST_DOMAINS:
2233 case LTTNG_START_TRACE:
2234 case LTTNG_STOP_TRACE:
6d805429 2235 case LTTNG_DATA_PENDING:
2e09ba09 2236 need_domain = 0;
3aace903 2237 break;
2e09ba09
MD
2238 default:
2239 need_domain = 1;
2240 }
2241
2242 if (opt_no_kernel && need_domain
2243 && cmd_ctx->lsm->domain.type == LTTNG_DOMAIN_KERNEL) {
531d29f9 2244 if (!is_root) {
f73fabfd 2245 ret = LTTNG_ERR_NEED_ROOT_SESSIOND;
531d29f9 2246 } else {
f73fabfd 2247 ret = LTTNG_ERR_KERN_NA;
531d29f9 2248 }
4fba7219
DG
2249 goto error;
2250 }
2251
8d3113b2
DG
2252 /* Deny register consumer if we already have a spawned consumer. */
2253 if (cmd_ctx->lsm->cmd_type == LTTNG_REGISTER_CONSUMER) {
2254 pthread_mutex_lock(&kconsumer_data.pid_mutex);
2255 if (kconsumer_data.pid > 0) {
f73fabfd 2256 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
fa317f24 2257 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
8d3113b2
DG
2258 goto error;
2259 }
2260 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
2261 }
2262
54d01ffb
DG
2263 /*
2264 * Check for command that don't needs to allocate a returned payload. We do
44d3bd01 2265 * this here so we don't have to make the call for no payload at each
54d01ffb
DG
2266 * command.
2267 */
2268 switch(cmd_ctx->lsm->cmd_type) {
2269 case LTTNG_LIST_SESSIONS:
2270 case LTTNG_LIST_TRACEPOINTS:
f37d259d 2271 case LTTNG_LIST_TRACEPOINT_FIELDS:
54d01ffb
DG
2272 case LTTNG_LIST_DOMAINS:
2273 case LTTNG_LIST_CHANNELS:
2274 case LTTNG_LIST_EVENTS:
2275 break;
2276 default:
2277 /* Setup lttng message with no payload */
2278 ret = setup_lttng_msg(cmd_ctx, 0);
2279 if (ret < 0) {
2280 /* This label does not try to unlock the session */
2281 goto init_setup_error;
2282 }
2283 }
2284
2285 /* Commands that DO NOT need a session. */
2286 switch (cmd_ctx->lsm->cmd_type) {
54d01ffb 2287 case LTTNG_CREATE_SESSION:
2e09ba09 2288 case LTTNG_CALIBRATE:
54d01ffb
DG
2289 case LTTNG_LIST_SESSIONS:
2290 case LTTNG_LIST_TRACEPOINTS:
f37d259d 2291 case LTTNG_LIST_TRACEPOINT_FIELDS:
44d3bd01 2292 need_tracing_session = 0;
54d01ffb
DG
2293 break;
2294 default:
2295 DBG("Getting session %s by name", cmd_ctx->lsm->session.name);
256a5576
MD
2296 /*
2297 * We keep the session list lock across _all_ commands
2298 * for now, because the per-session lock does not
2299 * handle teardown properly.
2300 */
74babd95 2301 session_lock_list();
54d01ffb
DG
2302 cmd_ctx->session = session_find_by_name(cmd_ctx->lsm->session.name);
2303 if (cmd_ctx->session == NULL) {
2304 if (cmd_ctx->lsm->session.name != NULL) {
f73fabfd 2305 ret = LTTNG_ERR_SESS_NOT_FOUND;
54d01ffb
DG
2306 } else {
2307 /* If no session name specified */
f73fabfd 2308 ret = LTTNG_ERR_SELECT_SESS;
54d01ffb
DG
2309 }
2310 goto error;
2311 } else {
2312 /* Acquire lock for the session */
2313 session_lock(cmd_ctx->session);
2314 }
2315 break;
2316 }
b389abbe 2317
2e09ba09
MD
2318 if (!need_domain) {
2319 goto skip_domain;
2320 }
a4b92340 2321
54d01ffb
DG
2322 /*
2323 * Check domain type for specific "pre-action".
2324 */
2325 switch (cmd_ctx->lsm->domain.type) {
2326 case LTTNG_DOMAIN_KERNEL:
d1f1c568 2327 if (!is_root) {
f73fabfd 2328 ret = LTTNG_ERR_NEED_ROOT_SESSIOND;
d1f1c568
DG
2329 goto error;
2330 }
2331
54d01ffb 2332 /* Kernel tracer check */
a4b35e07 2333 if (kernel_tracer_fd == -1) {
54d01ffb 2334 /* Basically, load kernel tracer modules */
096102bd
DG
2335 ret = init_kernel_tracer();
2336 if (ret != 0) {
54d01ffb
DG
2337 goto error;
2338 }
2339 }
5eb91c98 2340
5c827ce0
DG
2341 /* Consumer is in an ERROR state. Report back to client */
2342 if (uatomic_read(&kernel_consumerd_state) == CONSUMER_ERROR) {
f73fabfd 2343 ret = LTTNG_ERR_NO_KERNCONSUMERD;
5c827ce0
DG
2344 goto error;
2345 }
2346
54d01ffb 2347 /* Need a session for kernel command */
44d3bd01 2348 if (need_tracing_session) {
54d01ffb 2349 if (cmd_ctx->session->kernel_session == NULL) {
6df2e2c9 2350 ret = create_kernel_session(cmd_ctx->session);
5eb91c98 2351 if (ret < 0) {
f73fabfd 2352 ret = LTTNG_ERR_KERN_SESS_FAIL;
5eb91c98
DG
2353 goto error;
2354 }
b389abbe 2355 }
7d29a247 2356
54d01ffb 2357 /* Start the kernel consumer daemon */
3bd1e081
MD
2358 pthread_mutex_lock(&kconsumer_data.pid_mutex);
2359 if (kconsumer_data.pid == 0 &&
a4b92340
DG
2360 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER &&
2361 cmd_ctx->session->start_consumer) {
3bd1e081
MD
2362 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
2363 ret = start_consumerd(&kconsumer_data);
7d29a247 2364 if (ret < 0) {
f73fabfd 2365 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
54d01ffb 2366 goto error;
950131af 2367 }
5c827ce0 2368 uatomic_set(&kernel_consumerd_state, CONSUMER_STARTED);
3ff2ecac
MD
2369 } else {
2370 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
33a2b854 2371 }
173af62f 2372
a4b92340
DG
2373 /*
2374 * The consumer was just spawned so we need to add the socket to
2375 * the consumer output of the session if exist.
2376 */
2377 ret = consumer_create_socket(&kconsumer_data,
2378 cmd_ctx->session->kernel_session->consumer);
2379 if (ret < 0) {
2380 goto error;
173af62f 2381 }
0d0c377a 2382 }
5c827ce0 2383
54d01ffb 2384 break;
2bdd86d4 2385 case LTTNG_DOMAIN_UST:
44d3bd01 2386 {
5c827ce0
DG
2387 /* Consumer is in an ERROR state. Report back to client */
2388 if (uatomic_read(&ust_consumerd_state) == CONSUMER_ERROR) {
f73fabfd 2389 ret = LTTNG_ERR_NO_USTCONSUMERD;
5c827ce0
DG
2390 goto error;
2391 }
2392
44d3bd01 2393 if (need_tracing_session) {
a4b92340 2394 /* Create UST session if none exist. */
f6a9efaa 2395 if (cmd_ctx->session->ust_session == NULL) {
44d3bd01 2396 ret = create_ust_session(cmd_ctx->session,
6df2e2c9 2397 &cmd_ctx->lsm->domain);
f73fabfd 2398 if (ret != LTTNG_OK) {
44d3bd01
DG
2399 goto error;
2400 }
2401 }
00e2e675 2402
7753dea8
MD
2403 /* Start the UST consumer daemons */
2404 /* 64-bit */
2405 pthread_mutex_lock(&ustconsumer64_data.pid_mutex);
fc7a59ce 2406 if (consumerd64_bin[0] != '\0' &&
7753dea8 2407 ustconsumer64_data.pid == 0 &&
a4b92340
DG
2408 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER &&
2409 cmd_ctx->session->start_consumer) {
7753dea8
MD
2410 pthread_mutex_unlock(&ustconsumer64_data.pid_mutex);
2411 ret = start_consumerd(&ustconsumer64_data);
2bdd86d4 2412 if (ret < 0) {
f73fabfd 2413 ret = LTTNG_ERR_UST_CONSUMER64_FAIL;
173af62f 2414 uatomic_set(&ust_consumerd64_fd, -EINVAL);
2bdd86d4
MD
2415 goto error;
2416 }
48842b30 2417
173af62f 2418 uatomic_set(&ust_consumerd64_fd, ustconsumer64_data.cmd_sock);
5c827ce0 2419 uatomic_set(&ust_consumerd_state, CONSUMER_STARTED);
3ff2ecac 2420 } else {
7753dea8
MD
2421 pthread_mutex_unlock(&ustconsumer64_data.pid_mutex);
2422 }
173af62f
DG
2423
2424 /*
2425 * Setup socket for consumer 64 bit. No need for atomic access
2426 * since it was set above and can ONLY be set in this thread.
2427 */
a4b92340
DG
2428 ret = consumer_create_socket(&ustconsumer64_data,
2429 cmd_ctx->session->ust_session->consumer);
2430 if (ret < 0) {
2431 goto error;
173af62f
DG
2432 }
2433
7753dea8 2434 /* 32-bit */
fc7a59ce 2435 if (consumerd32_bin[0] != '\0' &&
7753dea8 2436 ustconsumer32_data.pid == 0 &&
a4b92340
DG
2437 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER &&
2438 cmd_ctx->session->start_consumer) {
7753dea8
MD
2439 pthread_mutex_unlock(&ustconsumer32_data.pid_mutex);
2440 ret = start_consumerd(&ustconsumer32_data);
2441 if (ret < 0) {
f73fabfd 2442 ret = LTTNG_ERR_UST_CONSUMER32_FAIL;
173af62f 2443 uatomic_set(&ust_consumerd32_fd, -EINVAL);
7753dea8
MD
2444 goto error;
2445 }
5c827ce0 2446
173af62f 2447 uatomic_set(&ust_consumerd32_fd, ustconsumer32_data.cmd_sock);
5c827ce0 2448 uatomic_set(&ust_consumerd_state, CONSUMER_STARTED);
7753dea8
MD
2449 } else {
2450 pthread_mutex_unlock(&ustconsumer32_data.pid_mutex);
2bdd86d4 2451 }
173af62f
DG
2452
2453 /*
2454 * Setup socket for consumer 64 bit. No need for atomic access
2455 * since it was set above and can ONLY be set in this thread.
2456 */
a4b92340
DG
2457 ret = consumer_create_socket(&ustconsumer32_data,
2458 cmd_ctx->session->ust_session->consumer);
2459 if (ret < 0) {
2460 goto error;
173af62f 2461 }
44d3bd01
DG
2462 }
2463 break;
48842b30 2464 }
54d01ffb 2465 default:
54d01ffb
DG
2466 break;
2467 }
2e09ba09 2468skip_domain:
33a2b854 2469
5c827ce0
DG
2470 /* Validate consumer daemon state when start/stop trace command */
2471 if (cmd_ctx->lsm->cmd_type == LTTNG_START_TRACE ||
2472 cmd_ctx->lsm->cmd_type == LTTNG_STOP_TRACE) {
2473 switch (cmd_ctx->lsm->domain.type) {
2474 case LTTNG_DOMAIN_UST:
2475 if (uatomic_read(&ust_consumerd_state) != CONSUMER_STARTED) {
f73fabfd 2476 ret = LTTNG_ERR_NO_USTCONSUMERD;
5c827ce0
DG
2477 goto error;
2478 }
2479 break;
2480 case LTTNG_DOMAIN_KERNEL:
2481 if (uatomic_read(&kernel_consumerd_state) != CONSUMER_STARTED) {
f73fabfd 2482 ret = LTTNG_ERR_NO_KERNCONSUMERD;
5c827ce0
DG
2483 goto error;
2484 }
2485 break;
2486 }
2487 }
2488
8e0af1b4
MD
2489 /*
2490 * Check that the UID or GID match that of the tracing session.
2491 * The root user can interact with all sessions.
2492 */
2493 if (need_tracing_session) {
2494 if (!session_access_ok(cmd_ctx->session,
730389d9
DG
2495 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
2496 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds))) {
f73fabfd 2497 ret = LTTNG_ERR_EPERM;
8e0af1b4
MD
2498 goto error;
2499 }
2500 }
2501
54d01ffb
DG
2502 /* Process by command type */
2503 switch (cmd_ctx->lsm->cmd_type) {
2504 case LTTNG_ADD_CONTEXT:
2505 {
2506 ret = cmd_add_context(cmd_ctx->session, cmd_ctx->lsm->domain.type,
2507 cmd_ctx->lsm->u.context.channel_name,
979e618e 2508 &cmd_ctx->lsm->u.context.ctx, kernel_poll_pipe[1]);
54d01ffb
DG
2509 break;
2510 }
2511 case LTTNG_DISABLE_CHANNEL:
2512 {
2513 ret = cmd_disable_channel(cmd_ctx->session, cmd_ctx->lsm->domain.type,
2514 cmd_ctx->lsm->u.disable.channel_name);
2515 break;
2516 }
2517 case LTTNG_DISABLE_EVENT:
2518 {
2519 ret = cmd_disable_event(cmd_ctx->session, cmd_ctx->lsm->domain.type,
2520 cmd_ctx->lsm->u.disable.channel_name,
2521 cmd_ctx->lsm->u.disable.name);
33a2b854
DG
2522 break;
2523 }
54d01ffb
DG
2524 case LTTNG_DISABLE_ALL_EVENT:
2525 {
2bdd86d4 2526 DBG("Disabling all events");
54d01ffb
DG
2527
2528 ret = cmd_disable_event_all(cmd_ctx->session, cmd_ctx->lsm->domain.type,
2529 cmd_ctx->lsm->u.disable.channel_name);
2530 break;
2531 }
00e2e675
DG
2532 case LTTNG_DISABLE_CONSUMER:
2533 {
2534 ret = cmd_disable_consumer(cmd_ctx->lsm->domain.type, cmd_ctx->session);
2535 break;
2536 }
54d01ffb
DG
2537 case LTTNG_ENABLE_CHANNEL:
2538 {
aea829b3 2539 ret = cmd_enable_channel(cmd_ctx->session, cmd_ctx->lsm->domain.type,
2f77fc4b 2540 &cmd_ctx->lsm->u.channel.chan, kernel_poll_pipe[1]);
54d01ffb
DG
2541 break;
2542 }
00e2e675
DG
2543 case LTTNG_ENABLE_CONSUMER:
2544 {
a4b92340
DG
2545 /*
2546 * XXX: 0 means that this URI should be applied on the session. Should
2547 * be a DOMAIN enuam.
2548 */
00e2e675 2549 ret = cmd_enable_consumer(cmd_ctx->lsm->domain.type, cmd_ctx->session);
f73fabfd 2550 if (ret != LTTNG_OK) {
a4b92340
DG
2551 goto error;
2552 }
2553
2554 if (cmd_ctx->lsm->domain.type == 0) {
2555 /* Add the URI for the UST session if a consumer is present. */
2556 if (cmd_ctx->session->ust_session &&
2557 cmd_ctx->session->ust_session->consumer) {
2558 ret = cmd_enable_consumer(LTTNG_DOMAIN_UST, cmd_ctx->session);
2559 } else if (cmd_ctx->session->kernel_session &&
2560 cmd_ctx->session->kernel_session->consumer) {
2561 ret = cmd_enable_consumer(LTTNG_DOMAIN_KERNEL,
2562 cmd_ctx->session);
2563 }
2564 }
00e2e675
DG
2565 break;
2566 }
54d01ffb
DG
2567 case LTTNG_ENABLE_EVENT:
2568 {
2569 ret = cmd_enable_event(cmd_ctx->session, cmd_ctx->lsm->domain.type,
2570 cmd_ctx->lsm->u.enable.channel_name,
025faf73 2571 &cmd_ctx->lsm->u.enable.event, NULL, kernel_poll_pipe[1]);
54d01ffb
DG
2572 break;
2573 }
2574 case LTTNG_ENABLE_ALL_EVENT:
2575 {
2bdd86d4 2576 DBG("Enabling all events");
54d01ffb
DG
2577
2578 ret = cmd_enable_event_all(cmd_ctx->session, cmd_ctx->lsm->domain.type,
8c9ae521 2579 cmd_ctx->lsm->u.enable.channel_name,
025faf73 2580 cmd_ctx->lsm->u.enable.event.type, NULL, kernel_poll_pipe[1]);
54d01ffb
DG
2581 break;
2582 }
052da939 2583 case LTTNG_LIST_TRACEPOINTS:
2ef84c95 2584 {
9f19cc17 2585 struct lttng_event *events;
54d01ffb 2586 ssize_t nb_events;
052da939 2587
54d01ffb
DG
2588 nb_events = cmd_list_tracepoints(cmd_ctx->lsm->domain.type, &events);
2589 if (nb_events < 0) {
f73fabfd 2590 /* Return value is a negative lttng_error_code. */
54d01ffb
DG
2591 ret = -nb_events;
2592 goto error;
2ef84c95
DG
2593 }
2594
2595 /*
2596 * Setup lttng message with payload size set to the event list size in
2597 * bytes and then copy list into the llm payload.
2598 */
052da939 2599 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_event) * nb_events);
2ef84c95 2600 if (ret < 0) {
052da939 2601 free(events);
2ef84c95
DG
2602 goto setup_error;
2603 }
2604
2605 /* Copy event list into message payload */
9f19cc17 2606 memcpy(cmd_ctx->llm->payload, events,
052da939 2607 sizeof(struct lttng_event) * nb_events);
2ef84c95 2608
9f19cc17 2609 free(events);
2ef84c95 2610
f73fabfd 2611 ret = LTTNG_OK;
2ef84c95
DG
2612 break;
2613 }
f37d259d
MD
2614 case LTTNG_LIST_TRACEPOINT_FIELDS:
2615 {
2616 struct lttng_event_field *fields;
2617 ssize_t nb_fields;
2618
a4b92340
DG
2619 nb_fields = cmd_list_tracepoint_fields(cmd_ctx->lsm->domain.type,
2620 &fields);
f37d259d 2621 if (nb_fields < 0) {
f73fabfd 2622 /* Return value is a negative lttng_error_code. */
f37d259d
MD
2623 ret = -nb_fields;
2624 goto error;
2625 }
2626
2627 /*
2628 * Setup lttng message with payload size set to the event list size in
2629 * bytes and then copy list into the llm payload.
2630 */
a4b92340
DG
2631 ret = setup_lttng_msg(cmd_ctx,
2632 sizeof(struct lttng_event_field) * nb_fields);
f37d259d
MD
2633 if (ret < 0) {
2634 free(fields);
2635 goto setup_error;
2636 }
2637
2638 /* Copy event list into message payload */
2639 memcpy(cmd_ctx->llm->payload, fields,
2640 sizeof(struct lttng_event_field) * nb_fields);
2641
2642 free(fields);
2643
f73fabfd 2644 ret = LTTNG_OK;
f37d259d
MD
2645 break;
2646 }
00e2e675
DG
2647 case LTTNG_SET_CONSUMER_URI:
2648 {
a4b92340
DG
2649 size_t nb_uri, len;
2650 struct lttng_uri *uris;
2651
2652 nb_uri = cmd_ctx->lsm->u.uri.size;
2653 len = nb_uri * sizeof(struct lttng_uri);
2654
2655 if (nb_uri == 0) {
f73fabfd 2656 ret = LTTNG_ERR_INVALID;
a4b92340
DG
2657 goto error;
2658 }
2659
2660 uris = zmalloc(len);
2661 if (uris == NULL) {
f73fabfd 2662 ret = LTTNG_ERR_FATAL;
a4b92340
DG
2663 goto error;
2664 }
2665
2666 /* Receive variable len data */
77c7c900 2667 DBG("Receiving %zu URI(s) from client ...", nb_uri);
a4b92340
DG
2668 ret = lttcomm_recv_unix_sock(sock, uris, len);
2669 if (ret <= 0) {
2670 DBG("No URIs received from client... continuing");
2671 *sock_error = 1;
f73fabfd 2672 ret = LTTNG_ERR_SESSION_FAIL;
b1d41407 2673 free(uris);
a4b92340
DG
2674 goto error;
2675 }
2676
00e2e675 2677 ret = cmd_set_consumer_uri(cmd_ctx->lsm->domain.type, cmd_ctx->session,
a4b92340 2678 nb_uri, uris);
f73fabfd 2679 if (ret != LTTNG_OK) {
b1d41407 2680 free(uris);
a4b92340
DG
2681 goto error;
2682 }
2683
2684 /*
2685 * XXX: 0 means that this URI should be applied on the session. Should
2686 * be a DOMAIN enuam.
2687 */
2688 if (cmd_ctx->lsm->domain.type == 0) {
2689 /* Add the URI for the UST session if a consumer is present. */
2690 if (cmd_ctx->session->ust_session &&
2691 cmd_ctx->session->ust_session->consumer) {
2692 ret = cmd_set_consumer_uri(LTTNG_DOMAIN_UST, cmd_ctx->session,
2693 nb_uri, uris);
2694 } else if (cmd_ctx->session->kernel_session &&
2695 cmd_ctx->session->kernel_session->consumer) {
2696 ret = cmd_set_consumer_uri(LTTNG_DOMAIN_KERNEL,
2697 cmd_ctx->session, nb_uri, uris);
2698 }
2699 }
2700
b1d41407
DG
2701 free(uris);
2702
00e2e675
DG
2703 break;
2704 }
f3ed775e 2705 case LTTNG_START_TRACE:
8c0faa1d 2706 {
54d01ffb 2707 ret = cmd_start_trace(cmd_ctx->session);
8c0faa1d
DG
2708 break;
2709 }
f3ed775e 2710 case LTTNG_STOP_TRACE:
8c0faa1d 2711 {
54d01ffb 2712 ret = cmd_stop_trace(cmd_ctx->session);
8c0faa1d
DG
2713 break;
2714 }
5e16da05
MD
2715 case LTTNG_CREATE_SESSION:
2716 {
a4b92340
DG
2717 size_t nb_uri, len;
2718 struct lttng_uri *uris = NULL;
2719
2720 nb_uri = cmd_ctx->lsm->u.uri.size;
2721 len = nb_uri * sizeof(struct lttng_uri);
2722
2723 if (nb_uri > 0) {
2724 uris = zmalloc(len);
2725 if (uris == NULL) {
f73fabfd 2726 ret = LTTNG_ERR_FATAL;
a4b92340
DG
2727 goto error;
2728 }
2729
2730 /* Receive variable len data */
77c7c900 2731 DBG("Waiting for %zu URIs from client ...", nb_uri);
a4b92340
DG
2732 ret = lttcomm_recv_unix_sock(sock, uris, len);
2733 if (ret <= 0) {
2734 DBG("No URIs received from client... continuing");
2735 *sock_error = 1;
f73fabfd 2736 ret = LTTNG_ERR_SESSION_FAIL;
b1d41407 2737 free(uris);
a4b92340
DG
2738 goto error;
2739 }
2740
2741 if (nb_uri == 1 && uris[0].dtype != LTTNG_DST_PATH) {
2742 DBG("Creating session with ONE network URI is a bad call");
f73fabfd 2743 ret = LTTNG_ERR_SESSION_FAIL;
b1d41407 2744 free(uris);
a4b92340
DG
2745 goto error;
2746 }
2747 }
2748
2749 ret = cmd_create_session_uri(cmd_ctx->lsm->session.name, uris, nb_uri,
2750 &cmd_ctx->creds);
2751
b1d41407
DG
2752 free(uris);
2753
00e2e675
DG
2754 break;
2755 }
5e16da05
MD
2756 case LTTNG_DESTROY_SESSION:
2757 {
2f77fc4b 2758 ret = cmd_destroy_session(cmd_ctx->session, kernel_poll_pipe[1]);
a4b92340
DG
2759
2760 /* Set session to NULL so we do not unlock it after free. */
256a5576 2761 cmd_ctx->session = NULL;
5461b305 2762 break;
5e16da05 2763 }
9f19cc17 2764 case LTTNG_LIST_DOMAINS:
5e16da05 2765 {
54d01ffb
DG
2766 ssize_t nb_dom;
2767 struct lttng_domain *domains;
5461b305 2768
54d01ffb
DG
2769 nb_dom = cmd_list_domains(cmd_ctx->session, &domains);
2770 if (nb_dom < 0) {
f73fabfd 2771 /* Return value is a negative lttng_error_code. */
54d01ffb
DG
2772 ret = -nb_dom;
2773 goto error;
ce3d728c 2774 }
520ff687 2775
54d01ffb 2776 ret = setup_lttng_msg(cmd_ctx, nb_dom * sizeof(struct lttng_domain));
5461b305
DG
2777 if (ret < 0) {
2778 goto setup_error;
520ff687 2779 }
57167058 2780
54d01ffb
DG
2781 /* Copy event list into message payload */
2782 memcpy(cmd_ctx->llm->payload, domains,
2783 nb_dom * sizeof(struct lttng_domain));
2784
2785 free(domains);
5e16da05 2786
f73fabfd 2787 ret = LTTNG_OK;
5e16da05
MD
2788 break;
2789 }
9f19cc17 2790 case LTTNG_LIST_CHANNELS:
5e16da05 2791 {
6775595e 2792 int nb_chan;
54d01ffb
DG
2793 struct lttng_channel *channels;
2794
b551a063
DG
2795 nb_chan = cmd_list_channels(cmd_ctx->lsm->domain.type,
2796 cmd_ctx->session, &channels);
54d01ffb 2797 if (nb_chan < 0) {
f73fabfd 2798 /* Return value is a negative lttng_error_code. */
54d01ffb
DG
2799 ret = -nb_chan;
2800 goto error;
5461b305 2801 }
ca95a216 2802
54d01ffb 2803 ret = setup_lttng_msg(cmd_ctx, nb_chan * sizeof(struct lttng_channel));
5461b305
DG
2804 if (ret < 0) {
2805 goto setup_error;
2806 }
9f19cc17 2807
54d01ffb
DG
2808 /* Copy event list into message payload */
2809 memcpy(cmd_ctx->llm->payload, channels,
2810 nb_chan * sizeof(struct lttng_channel));
2811
2812 free(channels);
9f19cc17 2813
f73fabfd 2814 ret = LTTNG_OK;
5461b305 2815 break;
5e16da05 2816 }
9f19cc17 2817 case LTTNG_LIST_EVENTS:
5e16da05 2818 {
b551a063 2819 ssize_t nb_event;
684d34d2 2820 struct lttng_event *events = NULL;
9f19cc17 2821
b551a063 2822 nb_event = cmd_list_events(cmd_ctx->lsm->domain.type, cmd_ctx->session,
54d01ffb
DG
2823 cmd_ctx->lsm->u.list.channel_name, &events);
2824 if (nb_event < 0) {
f73fabfd 2825 /* Return value is a negative lttng_error_code. */
54d01ffb
DG
2826 ret = -nb_event;
2827 goto error;
5461b305 2828 }
ca95a216 2829
54d01ffb 2830 ret = setup_lttng_msg(cmd_ctx, nb_event * sizeof(struct lttng_event));
5461b305
DG
2831 if (ret < 0) {
2832 goto setup_error;
2833 }
9f19cc17 2834
54d01ffb
DG
2835 /* Copy event list into message payload */
2836 memcpy(cmd_ctx->llm->payload, events,
2837 nb_event * sizeof(struct lttng_event));
9f19cc17 2838
54d01ffb 2839 free(events);
9f19cc17 2840
f73fabfd 2841 ret = LTTNG_OK;
5461b305 2842 break;
5e16da05
MD
2843 }
2844 case LTTNG_LIST_SESSIONS:
2845 {
8e0af1b4 2846 unsigned int nr_sessions;
5461b305 2847
8e0af1b4 2848 session_lock_list();
730389d9
DG
2849 nr_sessions = lttng_sessions_count(
2850 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
2851 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds));
d32fb093 2852
8e0af1b4 2853 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_session) * nr_sessions);
5461b305 2854 if (ret < 0) {
54d01ffb 2855 session_unlock_list();
5461b305 2856 goto setup_error;
e065084a 2857 }
5e16da05 2858
6c9cc2ab 2859 /* Filled the session array */
2f77fc4b 2860 cmd_list_lttng_sessions((struct lttng_session *)(cmd_ctx->llm->payload),
730389d9
DG
2861 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
2862 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds));
6c9cc2ab 2863
54d01ffb 2864 session_unlock_list();
5e16da05 2865
f73fabfd 2866 ret = LTTNG_OK;
5e16da05
MD
2867 break;
2868 }
d0254c7c
MD
2869 case LTTNG_CALIBRATE:
2870 {
54d01ffb
DG
2871 ret = cmd_calibrate(cmd_ctx->lsm->domain.type,
2872 &cmd_ctx->lsm->u.calibrate);
d0254c7c
MD
2873 break;
2874 }
d9800920
DG
2875 case LTTNG_REGISTER_CONSUMER:
2876 {
2f77fc4b
DG
2877 struct consumer_data *cdata;
2878
2879 switch (cmd_ctx->lsm->domain.type) {
2880 case LTTNG_DOMAIN_KERNEL:
2881 cdata = &kconsumer_data;
2882 break;
2883 default:
f73fabfd 2884 ret = LTTNG_ERR_UND;
2f77fc4b
DG
2885 goto error;
2886 }
2887
54d01ffb 2888 ret = cmd_register_consumer(cmd_ctx->session, cmd_ctx->lsm->domain.type,
2f77fc4b 2889 cmd_ctx->lsm->u.reg.path, cdata);
d9800920
DG
2890 break;
2891 }
025faf73 2892 case LTTNG_ENABLE_EVENT_WITH_FILTER:
53a80697
MD
2893 {
2894 struct lttng_filter_bytecode *bytecode;
2895
025faf73 2896 if (cmd_ctx->lsm->u.enable.bytecode_len > LTTNG_FILTER_MAX_LEN) {
f73fabfd 2897 ret = LTTNG_ERR_FILTER_INVAL;
53a80697
MD
2898 goto error;
2899 }
52df2401
MD
2900 if (cmd_ctx->lsm->u.enable.bytecode_len == 0) {
2901 ret = LTTNG_ERR_FILTER_INVAL;
2902 goto error;
2903 }
025faf73 2904 bytecode = zmalloc(cmd_ctx->lsm->u.enable.bytecode_len);
53a80697 2905 if (!bytecode) {
f73fabfd 2906 ret = LTTNG_ERR_FILTER_NOMEM;
53a80697
MD
2907 goto error;
2908 }
2909 /* Receive var. len. data */
2910 DBG("Receiving var len data from client ...");
2911 ret = lttcomm_recv_unix_sock(sock, bytecode,
025faf73 2912 cmd_ctx->lsm->u.enable.bytecode_len);
53a80697
MD
2913 if (ret <= 0) {
2914 DBG("Nothing recv() from client var len data... continuing");
2915 *sock_error = 1;
f73fabfd 2916 ret = LTTNG_ERR_FILTER_INVAL;
53a80697
MD
2917 goto error;
2918 }
2919
2920 if (bytecode->len + sizeof(*bytecode)
025faf73 2921 != cmd_ctx->lsm->u.enable.bytecode_len) {
53a80697 2922 free(bytecode);
f73fabfd 2923 ret = LTTNG_ERR_FILTER_INVAL;
53a80697
MD
2924 goto error;
2925 }
2926
025faf73
DG
2927 ret = cmd_enable_event(cmd_ctx->session, cmd_ctx->lsm->domain.type,
2928 cmd_ctx->lsm->u.enable.channel_name,
2929 &cmd_ctx->lsm->u.enable.event, bytecode, kernel_poll_pipe[1]);
53a80697
MD
2930 break;
2931 }
6d805429 2932 case LTTNG_DATA_PENDING:
806e2684 2933 {
6d805429 2934 ret = cmd_data_pending(cmd_ctx->session);
806e2684
DG
2935 break;
2936 }
5e16da05 2937 default:
f73fabfd 2938 ret = LTTNG_ERR_UND;
5461b305 2939 break;
fac6795d
DG
2940 }
2941
5461b305 2942error:
5461b305
DG
2943 if (cmd_ctx->llm == NULL) {
2944 DBG("Missing llm structure. Allocating one.");
894be886 2945 if (setup_lttng_msg(cmd_ctx, 0) < 0) {
5461b305
DG
2946 goto setup_error;
2947 }
2948 }
54d01ffb 2949 /* Set return code */
5461b305 2950 cmd_ctx->llm->ret_code = ret;
5461b305 2951setup_error:
b5541356 2952 if (cmd_ctx->session) {
54d01ffb 2953 session_unlock(cmd_ctx->session);
b5541356 2954 }
256a5576
MD
2955 if (need_tracing_session) {
2956 session_unlock_list();
2957 }
54d01ffb 2958init_setup_error:
8028d920 2959 return ret;
fac6795d
DG
2960}
2961
44a5e5eb
DG
2962/*
2963 * Thread managing health check socket.
2964 */
2965static void *thread_manage_health(void *data)
2966{
eb4a2943 2967 int sock = -1, new_sock = -1, ret, i, pollfd, err = -1;
44a5e5eb
DG
2968 uint32_t revents, nb_fd;
2969 struct lttng_poll_event events;
2970 struct lttcomm_health_msg msg;
2971 struct lttcomm_health_data reply;
2972
2973 DBG("[thread] Manage health check started");
2974
2975 rcu_register_thread();
2976
2977 /* Create unix socket */
2978 sock = lttcomm_create_unix_sock(health_unix_sock_path);
2979 if (sock < 0) {
2980 ERR("Unable to create health check Unix socket");
2981 ret = -1;
2982 goto error;
2983 }
2984
b662582b
DG
2985 /*
2986 * Set the CLOEXEC flag. Return code is useless because either way, the
2987 * show must go on.
2988 */
2989 (void) utils_set_fd_cloexec(sock);
2990
44a5e5eb
DG
2991 ret = lttcomm_listen_unix_sock(sock);
2992 if (ret < 0) {
2993 goto error;
2994 }
2995
2996 /*
2997 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
2998 * more will be added to this poll set.
2999 */
3000 ret = create_thread_poll_set(&events, 2);
3001 if (ret < 0) {
3002 goto error;
3003 }
3004
3005 /* Add the application registration socket */
3006 ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLPRI);
3007 if (ret < 0) {
3008 goto error;
3009 }
3010
3011 while (1) {
3012 DBG("Health check ready");
3013
44a5e5eb
DG
3014 /* Inifinite blocking call, waiting for transmission */
3015restart:
3016 ret = lttng_poll_wait(&events, -1);
3017 if (ret < 0) {
3018 /*
3019 * Restart interrupted system call.
3020 */
3021 if (errno == EINTR) {
3022 goto restart;
3023 }
3024 goto error;
3025 }
3026
0d9c5d77
DG
3027 nb_fd = ret;
3028
44a5e5eb
DG
3029 for (i = 0; i < nb_fd; i++) {
3030 /* Fetch once the poll data */
3031 revents = LTTNG_POLL_GETEV(&events, i);
3032 pollfd = LTTNG_POLL_GETFD(&events, i);
3033
3034 /* Thread quit pipe has been closed. Killing thread. */
3035 ret = check_thread_quit_pipe(pollfd, revents);
3036 if (ret) {
139ac872
MD
3037 err = 0;
3038 goto exit;
44a5e5eb
DG
3039 }
3040
3041 /* Event on the registration socket */
3042 if (pollfd == sock) {
3043 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
3044 ERR("Health socket poll error");
3045 goto error;
3046 }
3047 }
3048 }
3049
3050 new_sock = lttcomm_accept_unix_sock(sock);
3051 if (new_sock < 0) {
3052 goto error;
3053 }
3054
b662582b
DG
3055 /*
3056 * Set the CLOEXEC flag. Return code is useless because either way, the
3057 * show must go on.
3058 */
3059 (void) utils_set_fd_cloexec(new_sock);
3060
44a5e5eb
DG
3061 DBG("Receiving data from client for health...");
3062 ret = lttcomm_recv_unix_sock(new_sock, (void *)&msg, sizeof(msg));
3063 if (ret <= 0) {
3064 DBG("Nothing recv() from client... continuing");
3065 ret = close(new_sock);
3066 if (ret) {
3067 PERROR("close");
3068 }
3069 new_sock = -1;
3070 continue;
3071 }
3072
3073 rcu_thread_online();
3074
3075 switch (msg.component) {
3076 case LTTNG_HEALTH_CMD:
927ca06a 3077 reply.ret_code = health_check_state(HEALTH_TYPE_CMD);
44a5e5eb 3078 break;
139ac872 3079 case LTTNG_HEALTH_APP_MANAGE:
927ca06a 3080 reply.ret_code = health_check_state(HEALTH_TYPE_APP_MANAGE);
139ac872 3081 break;
44a5e5eb 3082 case LTTNG_HEALTH_APP_REG:
927ca06a 3083 reply.ret_code = health_check_state(HEALTH_TYPE_APP_REG);
44a5e5eb
DG
3084 break;
3085 case LTTNG_HEALTH_KERNEL:
927ca06a 3086 reply.ret_code = health_check_state(HEALTH_TYPE_KERNEL);
44a5e5eb
DG
3087 break;
3088 case LTTNG_HEALTH_CONSUMER:
3089 reply.ret_code = check_consumer_health();
3090 break;
3091 case LTTNG_HEALTH_ALL:
44a5e5eb 3092 reply.ret_code =
927ca06a
DG
3093 health_check_state(HEALTH_TYPE_APP_MANAGE) &&
3094 health_check_state(HEALTH_TYPE_APP_REG) &&
3095 health_check_state(HEALTH_TYPE_CMD) &&
3096 health_check_state(HEALTH_TYPE_KERNEL) &&
139ac872 3097 check_consumer_health();
44a5e5eb
DG
3098 break;
3099 default:
f73fabfd 3100 reply.ret_code = LTTNG_ERR_UND;
44a5e5eb
DG
3101 break;
3102 }
3103
3104 /*
3105 * Flip ret value since 0 is a success and 1 indicates a bad health for
3106 * the client where in the sessiond it is the opposite. Again, this is
3107 * just to make things easier for us poor developer which enjoy a lot
3108 * lazyness.
3109 */
3110 if (reply.ret_code == 0 || reply.ret_code == 1) {
3111 reply.ret_code = !reply.ret_code;
3112 }
3113
3114 DBG2("Health check return value %d", reply.ret_code);
3115
3116 ret = send_unix_sock(new_sock, (void *) &reply, sizeof(reply));
3117 if (ret < 0) {
3118 ERR("Failed to send health data back to client");
3119 }
3120
3121 /* End of transmission */
3122 ret = close(new_sock);
3123 if (ret) {
3124 PERROR("close");
3125 }
3126 new_sock = -1;
3127 }
3128
139ac872 3129exit:
44a5e5eb 3130error:
139ac872
MD
3131 if (err) {
3132 ERR("Health error occurred in %s", __func__);
3133 }
44a5e5eb
DG
3134 DBG("Health check thread dying");
3135 unlink(health_unix_sock_path);
3136 if (sock >= 0) {
3137 ret = close(sock);
3138 if (ret) {
3139 PERROR("close");
3140 }
3141 }
3142 if (new_sock >= 0) {
3143 ret = close(new_sock);
3144 if (ret) {
3145 PERROR("close");
3146 }
3147 }
3148
3149 lttng_poll_clean(&events);
3150
3151 rcu_unregister_thread();
3152 return NULL;
3153}
3154
1d4b027a 3155/*
d063d709
DG
3156 * This thread manage all clients request using the unix client socket for
3157 * communication.
1d4b027a
DG
3158 */
3159static void *thread_manage_clients(void *data)
3160{
139ac872 3161 int sock = -1, ret, i, pollfd, err = -1;
53a80697 3162 int sock_error;
5eb91c98 3163 uint32_t revents, nb_fd;
273ea72c 3164 struct command_ctx *cmd_ctx = NULL;
5eb91c98 3165 struct lttng_poll_event events;
1d4b027a
DG
3166
3167 DBG("[thread] Manage client started");
3168
f6a9efaa
DG
3169 rcu_register_thread();
3170
927ca06a
DG
3171 health_register(HEALTH_TYPE_CMD);
3172
6993eeb3
CB
3173 if (testpoint(thread_manage_clients)) {
3174 goto error_testpoint;
3175 }
3176
44a5e5eb
DG
3177 health_code_update(&health_thread_cmd);
3178
1d4b027a
DG
3179 ret = lttcomm_listen_unix_sock(client_sock);
3180 if (ret < 0) {
35df2755 3181 goto error_listen;
1d4b027a
DG
3182 }
3183
5eb91c98
DG
3184 /*
3185 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
3186 * more will be added to this poll set.
3187 */
3188 ret = create_thread_poll_set(&events, 2);
3189 if (ret < 0) {
35df2755 3190 goto error_create_poll;
5eb91c98 3191 }
273ea72c 3192
5eb91c98
DG
3193 /* Add the application registration socket */
3194 ret = lttng_poll_add(&events, client_sock, LPOLLIN | LPOLLPRI);
3195 if (ret < 0) {
3196 goto error;
3197 }
273ea72c 3198
bbd973c2
DG
3199 /*
3200 * Notify parent pid that we are ready to accept command for client side.
1d4b027a
DG
3201 */
3202 if (opt_sig_parent) {
8db8d1dc 3203 kill(ppid, SIGUSR1);
1d4b027a
DG
3204 }
3205
6993eeb3
CB
3206 if (testpoint(thread_manage_clients_before_loop)) {
3207 goto error;
3208 }
8ac94142 3209
44a5e5eb
DG
3210 health_code_update(&health_thread_cmd);
3211
1d4b027a 3212 while (1) {
1d4b027a 3213 DBG("Accepting client command ...");
273ea72c
DG
3214
3215 /* Inifinite blocking call, waiting for transmission */
88f2b785 3216 restart:
44a5e5eb 3217 health_poll_update(&health_thread_cmd);
5eb91c98 3218 ret = lttng_poll_wait(&events, -1);
44a5e5eb 3219 health_poll_update(&health_thread_cmd);
273ea72c 3220 if (ret < 0) {
88f2b785
MD
3221 /*
3222 * Restart interrupted system call.
3223 */
3224 if (errno == EINTR) {
3225 goto restart;
3226 }
273ea72c
DG
3227 goto error;
3228 }
3229
0d9c5d77
DG
3230 nb_fd = ret;
3231
5eb91c98
DG
3232 for (i = 0; i < nb_fd; i++) {
3233 /* Fetch once the poll data */
3234 revents = LTTNG_POLL_GETEV(&events, i);
3235 pollfd = LTTNG_POLL_GETFD(&events, i);
3236
44a5e5eb
DG
3237 health_code_update(&health_thread_cmd);
3238
5eb91c98
DG
3239 /* Thread quit pipe has been closed. Killing thread. */
3240 ret = check_thread_quit_pipe(pollfd, revents);
3241 if (ret) {
139ac872
MD
3242 err = 0;
3243 goto exit;
5eb91c98
DG
3244 }
3245
3246 /* Event on the registration socket */
3247 if (pollfd == client_sock) {
3248 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
3249 ERR("Client socket poll error");
3250 goto error;
3251 }
3252 }
3253 }
3254
3255 DBG("Wait for client response");
3256
44a5e5eb
DG
3257 health_code_update(&health_thread_cmd);
3258
5eb91c98
DG
3259 sock = lttcomm_accept_unix_sock(client_sock);
3260 if (sock < 0) {
273ea72c 3261 goto error;
273ea72c
DG
3262 }
3263
b662582b
DG
3264 /*
3265 * Set the CLOEXEC flag. Return code is useless because either way, the
3266 * show must go on.
3267 */
3268 (void) utils_set_fd_cloexec(sock);
3269
be040666
DG
3270 /* Set socket option for credentials retrieval */
3271 ret = lttcomm_setsockopt_creds_unix_sock(sock);
3272 if (ret < 0) {
3273 goto error;
3274 }
3275
5eb91c98 3276 /* Allocate context command to process the client request */
ba7f0ae5 3277 cmd_ctx = zmalloc(sizeof(struct command_ctx));
5eb91c98 3278 if (cmd_ctx == NULL) {
76d7553f 3279 PERROR("zmalloc cmd_ctx");
1d4b027a 3280 goto error;
5eb91c98 3281 }
1d4b027a 3282
5eb91c98 3283 /* Allocate data buffer for reception */
ba7f0ae5 3284 cmd_ctx->lsm = zmalloc(sizeof(struct lttcomm_session_msg));
5eb91c98 3285 if (cmd_ctx->lsm == NULL) {
76d7553f 3286 PERROR("zmalloc cmd_ctx->lsm");
5eb91c98
DG
3287 goto error;
3288 }
1d4b027a 3289
5eb91c98
DG
3290 cmd_ctx->llm = NULL;
3291 cmd_ctx->session = NULL;
1d4b027a 3292
44a5e5eb
DG
3293 health_code_update(&health_thread_cmd);
3294
5eb91c98
DG
3295 /*
3296 * Data is received from the lttng client. The struct
3297 * lttcomm_session_msg (lsm) contains the command and data request of
3298 * the client.
3299 */
3300 DBG("Receiving data from client ...");
be040666
DG
3301 ret = lttcomm_recv_creds_unix_sock(sock, cmd_ctx->lsm,
3302 sizeof(struct lttcomm_session_msg), &cmd_ctx->creds);
5eb91c98
DG
3303 if (ret <= 0) {
3304 DBG("Nothing recv() from client... continuing");
76d7553f
MD
3305 ret = close(sock);
3306 if (ret) {
3307 PERROR("close");
3308 }
3309 sock = -1;
a2c0da86 3310 clean_command_ctx(&cmd_ctx);
5eb91c98
DG
3311 continue;
3312 }
1d4b027a 3313
44a5e5eb
DG
3314 health_code_update(&health_thread_cmd);
3315
5eb91c98
DG
3316 // TODO: Validate cmd_ctx including sanity check for
3317 // security purpose.
f7776ea7 3318
f6a9efaa 3319 rcu_thread_online();
5eb91c98
DG
3320 /*
3321 * This function dispatch the work to the kernel or userspace tracer
3322 * libs and fill the lttcomm_lttng_msg data structure of all the needed
3323 * informations for the client. The command context struct contains
3324 * everything this function may needs.
3325 */
53a80697 3326 ret = process_client_msg(cmd_ctx, sock, &sock_error);
f6a9efaa 3327 rcu_thread_offline();
5eb91c98 3328 if (ret < 0) {
53a80697
MD
3329 if (sock_error) {
3330 ret = close(sock);
3331 if (ret) {
3332 PERROR("close");
3333 }
3334 sock = -1;
3335 }
bbd973c2 3336 /*
5eb91c98 3337 * TODO: Inform client somehow of the fatal error. At
ba7f0ae5 3338 * this point, ret < 0 means that a zmalloc failed
53a80697
MD
3339 * (ENOMEM). Error detected but still accept
3340 * command, unless a socket error has been
3341 * detected.
bbd973c2 3342 */
bbd973c2 3343 clean_command_ctx(&cmd_ctx);
5eb91c98
DG
3344 continue;
3345 }
d6e4fca4 3346
44a5e5eb
DG
3347 health_code_update(&health_thread_cmd);
3348
54d01ffb
DG
3349 DBG("Sending response (size: %d, retcode: %s)",
3350 cmd_ctx->lttng_msg_size,
9a745bc7 3351 lttng_strerror(-cmd_ctx->llm->ret_code));
54d01ffb 3352 ret = send_unix_sock(sock, cmd_ctx->llm, cmd_ctx->lttng_msg_size);
5eb91c98
DG
3353 if (ret < 0) {
3354 ERR("Failed to send data back to client");
bbd973c2 3355 }
1d4b027a 3356
5eb91c98 3357 /* End of transmission */
76d7553f
MD
3358 ret = close(sock);
3359 if (ret) {
3360 PERROR("close");
3361 }
3362 sock = -1;
be040666
DG
3363
3364 clean_command_ctx(&cmd_ctx);
44a5e5eb
DG
3365
3366 health_code_update(&health_thread_cmd);
273ea72c
DG
3367 }
3368
139ac872 3369exit:
5eb91c98 3370error:
35df2755
CB
3371 if (sock >= 0) {
3372 ret = close(sock);
3373 if (ret) {
3374 PERROR("close");
3375 }
139ac872 3376 }
44a5e5eb 3377
35df2755
CB
3378 lttng_poll_clean(&events);
3379 clean_command_ctx(&cmd_ctx);
3380
3381error_listen:
3382error_create_poll:
6993eeb3 3383error_testpoint:
273ea72c 3384 unlink(client_unix_sock_path);
76d7553f
MD
3385 if (client_sock >= 0) {
3386 ret = close(client_sock);
3387 if (ret) {
3388 PERROR("close");
3389 }
a4b35e07 3390 }
35df2755
CB
3391
3392 if (err) {
3393 health_error(&health_thread_cmd);
3394 ERR("Health error occurred in %s", __func__);
a4b35e07 3395 }
273ea72c 3396
927ca06a 3397 health_unregister();
35df2755
CB
3398
3399 DBG("Client thread dying");
f6a9efaa
DG
3400
3401 rcu_unregister_thread();
1d4b027a
DG
3402 return NULL;
3403}
3404
3405
fac6795d
DG
3406/*
3407 * usage function on stderr
3408 */
3409static void usage(void)
3410{
b716ce68 3411 fprintf(stderr, "Usage: %s OPTIONS\n\nOptions:\n", progname);
d6f42150
DG
3412 fprintf(stderr, " -h, --help Display this usage.\n");
3413 fprintf(stderr, " -c, --client-sock PATH Specify path for the client unix socket\n");
3414 fprintf(stderr, " -a, --apps-sock PATH Specify path for apps unix socket\n");
3415 fprintf(stderr, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
3416 fprintf(stderr, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
7753dea8
MD
3417 fprintf(stderr, " --ustconsumerd32-err-sock PATH Specify path for the 32-bit UST consumer error socket\n");
3418 fprintf(stderr, " --ustconsumerd64-err-sock PATH Specify path for the 64-bit UST consumer error socket\n");
3419 fprintf(stderr, " --ustconsumerd32-cmd-sock PATH Specify path for the 32-bit UST consumer command socket\n");
3420 fprintf(stderr, " --ustconsumerd64-cmd-sock PATH Specify path for the 64-bit UST consumer command socket\n");
ebaeda94
MD
3421 fprintf(stderr, " --consumerd32-path PATH Specify path for the 32-bit UST consumer daemon binary\n");
3422 fprintf(stderr, " --consumerd32-libdir PATH Specify path for the 32-bit UST consumer daemon libraries\n");
3423 fprintf(stderr, " --consumerd64-path PATH Specify path for the 64-bit UST consumer daemon binary\n");
3424 fprintf(stderr, " --consumerd64-libdir PATH Specify path for the 64-bit UST consumer daemon libraries\n");
d6f42150
DG
3425 fprintf(stderr, " -d, --daemonize Start as a daemon.\n");
3426 fprintf(stderr, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
3427 fprintf(stderr, " -V, --version Show version number.\n");
3428 fprintf(stderr, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
3429 fprintf(stderr, " -q, --quiet No output at all.\n");
3430 fprintf(stderr, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
35f90c40 3431 fprintf(stderr, " -p, --pidfile FILE Write a pid to FILE name overriding the default value.\n");
3bd1e081 3432 fprintf(stderr, " --verbose-consumer Verbose mode for consumer. Activate DBG() macro.\n");
4fba7219 3433 fprintf(stderr, " --no-kernel Disable kernel tracer\n");
fac6795d
DG
3434}
3435
3436/*
3437 * daemon argument parsing
3438 */
3439static int parse_args(int argc, char **argv)
3440{
3441 int c;
3442
3443 static struct option long_options[] = {
3444 { "client-sock", 1, 0, 'c' },
3445 { "apps-sock", 1, 0, 'a' },
3bd1e081
MD
3446 { "kconsumerd-cmd-sock", 1, 0, 'C' },
3447 { "kconsumerd-err-sock", 1, 0, 'E' },
7753dea8
MD
3448 { "ustconsumerd32-cmd-sock", 1, 0, 'G' },
3449 { "ustconsumerd32-err-sock", 1, 0, 'H' },
ebaeda94
MD
3450 { "ustconsumerd64-cmd-sock", 1, 0, 'D' },
3451 { "ustconsumerd64-err-sock", 1, 0, 'F' },
3452 { "consumerd32-path", 1, 0, 'u' },
3453 { "consumerd32-libdir", 1, 0, 'U' },
3454 { "consumerd64-path", 1, 0, 't' },
3455 { "consumerd64-libdir", 1, 0, 'T' },
fac6795d 3456 { "daemonize", 0, 0, 'd' },
5b8719f5 3457 { "sig-parent", 0, 0, 'S' },
fac6795d
DG
3458 { "help", 0, 0, 'h' },
3459 { "group", 1, 0, 'g' },
3460 { "version", 0, 0, 'V' },
75462a81 3461 { "quiet", 0, 0, 'q' },
3f9947db 3462 { "verbose", 0, 0, 'v' },
3bd1e081 3463 { "verbose-consumer", 0, 0, 'Z' },
4fba7219 3464 { "no-kernel", 0, 0, 'N' },
35f90c40 3465 { "pidfile", 1, 0, 'p' },
fac6795d
DG
3466 { NULL, 0, 0, 0 }
3467 };
3468
3469 while (1) {
3470 int option_index = 0;
35f90c40 3471 c = getopt_long(argc, argv, "dhqvVSN" "a:c:g:s:C:E:D:F:Z:u:t:p:",
54d01ffb 3472 long_options, &option_index);
fac6795d
DG
3473 if (c == -1) {
3474 break;
3475 }
3476
3477 switch (c) {
3478 case 0:
3479 fprintf(stderr, "option %s", long_options[option_index].name);
3480 if (optarg) {
3481 fprintf(stderr, " with arg %s\n", optarg);
3482 }
3483 break;
b716ce68 3484 case 'c':
fac6795d
DG
3485 snprintf(client_unix_sock_path, PATH_MAX, "%s", optarg);
3486 break;
3487 case 'a':
3488 snprintf(apps_unix_sock_path, PATH_MAX, "%s", optarg);
3489 break;
3490 case 'd':
3491 opt_daemon = 1;
3492 break;
3493 case 'g':
fb09408a 3494 opt_tracing_group = optarg;
fac6795d
DG
3495 break;
3496 case 'h':
3497 usage();
3498 exit(EXIT_FAILURE);
3499 case 'V':
3500 fprintf(stdout, "%s\n", VERSION);
3501 exit(EXIT_SUCCESS);
5b8719f5
DG
3502 case 'S':
3503 opt_sig_parent = 1;
3504 break;
d6f42150 3505 case 'E':
3bd1e081 3506 snprintf(kconsumer_data.err_unix_sock_path, PATH_MAX, "%s", optarg);
d6f42150
DG
3507 break;
3508 case 'C':
3bd1e081
MD
3509 snprintf(kconsumer_data.cmd_unix_sock_path, PATH_MAX, "%s", optarg);
3510 break;
3511 case 'F':
7753dea8 3512 snprintf(ustconsumer64_data.err_unix_sock_path, PATH_MAX, "%s", optarg);
3bd1e081
MD
3513 break;
3514 case 'D':
7753dea8
MD
3515 snprintf(ustconsumer64_data.cmd_unix_sock_path, PATH_MAX, "%s", optarg);
3516 break;
3517 case 'H':
3518 snprintf(ustconsumer32_data.err_unix_sock_path, PATH_MAX, "%s", optarg);
3519 break;
3520 case 'G':
3521 snprintf(ustconsumer32_data.cmd_unix_sock_path, PATH_MAX, "%s", optarg);
d6f42150 3522 break;
4fba7219
DG
3523 case 'N':
3524 opt_no_kernel = 1;
3525 break;
75462a81 3526 case 'q':
97e19046 3527 lttng_opt_quiet = 1;
75462a81 3528 break;
3f9947db 3529 case 'v':
53086306 3530 /* Verbose level can increase using multiple -v */
97e19046 3531 lttng_opt_verbose += 1;
3f9947db 3532 break;
31f73cc9 3533 case 'Z':
3bd1e081 3534 opt_verbose_consumer += 1;
31f73cc9 3535 break;
fb09408a 3536 case 'u':
fc7a59ce 3537 consumerd32_bin= optarg;
ebaeda94
MD
3538 break;
3539 case 'U':
3540 consumerd32_libdir = optarg;
7753dea8
MD
3541 break;
3542 case 't':
fc7a59ce 3543 consumerd64_bin = optarg;
ebaeda94
MD
3544 break;
3545 case 'T':
3546 consumerd64_libdir = optarg;
fb09408a 3547 break;
35f90c40
DG
3548 case 'p':
3549 opt_pidfile = optarg;
3550 break;
fac6795d
DG
3551 default:
3552 /* Unknown option or other error.
3553 * Error is printed by getopt, just return */
3554 return -1;
3555 }
3556 }
3557
3558 return 0;
3559}
3560
3561/*
d063d709 3562 * Creates the two needed socket by the daemon.
d6f42150
DG
3563 * apps_sock - The communication socket for all UST apps.
3564 * client_sock - The communication of the cli tool (lttng).
fac6795d 3565 */
cf3af59e 3566static int init_daemon_socket(void)
fac6795d
DG
3567{
3568 int ret = 0;
3569 mode_t old_umask;
3570
3571 old_umask = umask(0);
3572
3573 /* Create client tool unix socket */
d6f42150
DG
3574 client_sock = lttcomm_create_unix_sock(client_unix_sock_path);
3575 if (client_sock < 0) {
3576 ERR("Create unix sock failed: %s", client_unix_sock_path);
fac6795d
DG
3577 ret = -1;
3578 goto end;
3579 }
3580
b662582b
DG
3581 /* Set the cloexec flag */
3582 ret = utils_set_fd_cloexec(client_sock);
3583 if (ret < 0) {
3584 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
3585 "Continuing but note that the consumer daemon will have a "
3586 "reference to this socket on exec()", client_sock);
3587 }
3588
fac6795d
DG
3589 /* File permission MUST be 660 */
3590 ret = chmod(client_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
3591 if (ret < 0) {
d6f42150 3592 ERR("Set file permissions failed: %s", client_unix_sock_path);
76d7553f 3593 PERROR("chmod");
fac6795d
DG
3594 goto end;
3595 }
3596
3597 /* Create the application unix socket */
d6f42150
DG
3598 apps_sock = lttcomm_create_unix_sock(apps_unix_sock_path);
3599 if (apps_sock < 0) {
3600 ERR("Create unix sock failed: %s", apps_unix_sock_path);
fac6795d
DG
3601 ret = -1;
3602 goto end;
3603 }
3604
b662582b
DG
3605 /* Set the cloexec flag */
3606 ret = utils_set_fd_cloexec(apps_sock);
3607 if (ret < 0) {
3608 ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). "
3609 "Continuing but note that the consumer daemon will have a "
3610 "reference to this socket on exec()", apps_sock);
3611 }
3612
d6f42150 3613 /* File permission MUST be 666 */
54d01ffb
DG
3614 ret = chmod(apps_unix_sock_path,
3615 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
fac6795d 3616 if (ret < 0) {
d6f42150 3617 ERR("Set file permissions failed: %s", apps_unix_sock_path);
76d7553f 3618 PERROR("chmod");
fac6795d
DG
3619 goto end;
3620 }
3621
b662582b
DG
3622 DBG3("Session daemon client socket %d and application socket %d created",
3623 client_sock, apps_sock);
3624
fac6795d
DG
3625end:
3626 umask(old_umask);
3627 return ret;
3628}
3629
3630/*
54d01ffb
DG
3631 * Check if the global socket is available, and if a daemon is answering at the
3632 * other side. If yes, error is returned.
fac6795d 3633 */
cf3af59e 3634static int check_existing_daemon(void)
fac6795d 3635{
7d8234d9 3636 /* Is there anybody out there ? */
099e26bd 3637 if (lttng_session_daemon_alive()) {
7d8234d9 3638 return -EEXIST;
099e26bd 3639 }
b09c7c76
DG
3640
3641 return 0;
fac6795d
DG
3642}
3643
fac6795d 3644/*
d063d709 3645 * Set the tracing group gid onto the client socket.
5e16da05 3646 *
d063d709 3647 * Race window between mkdir and chown is OK because we are going from more
d1613cf5 3648 * permissive (root.root) to less permissive (root.tracing).
fac6795d 3649 */
be040666 3650static int set_permissions(char *rundir)
fac6795d
DG
3651{
3652 int ret;
996b65c8 3653 gid_t gid;
fac6795d 3654
6775595e
DG
3655 ret = allowed_group();
3656 if (ret < 0) {
be040666
DG
3657 WARN("No tracing group detected");
3658 ret = 0;
fac6795d
DG
3659 goto end;
3660 }
3661
6775595e
DG
3662 gid = ret;
3663
d6f42150 3664 /* Set lttng run dir */
be040666 3665 ret = chown(rundir, 0, gid);
d6f42150 3666 if (ret < 0) {
be040666 3667 ERR("Unable to set group on %s", rundir);
76d7553f 3668 PERROR("chown");
d6f42150
DG
3669 }
3670
d1613cf5 3671 /* Ensure tracing group can search the run dir */
61076f74 3672 ret = chmod(rundir, S_IRWXU | S_IXGRP | S_IXOTH);
d1613cf5
JN
3673 if (ret < 0) {
3674 ERR("Unable to set permissions on %s", rundir);
76d7553f 3675 PERROR("chmod");
d1613cf5
JN
3676 }
3677
d6f42150 3678 /* lttng client socket path */
996b65c8 3679 ret = chown(client_unix_sock_path, 0, gid);
fac6795d 3680 if (ret < 0) {
d6f42150 3681 ERR("Unable to set group on %s", client_unix_sock_path);
76d7553f 3682 PERROR("chown");
d6f42150
DG
3683 }
3684
3bd1e081
MD
3685 /* kconsumer error socket path */
3686 ret = chown(kconsumer_data.err_unix_sock_path, 0, gid);
d6f42150 3687 if (ret < 0) {
3bd1e081 3688 ERR("Unable to set group on %s", kconsumer_data.err_unix_sock_path);
76d7553f 3689 PERROR("chown");
3bd1e081
MD
3690 }
3691
7753dea8
MD
3692 /* 64-bit ustconsumer error socket path */
3693 ret = chown(ustconsumer64_data.err_unix_sock_path, 0, gid);
3694 if (ret < 0) {
3695 ERR("Unable to set group on %s", ustconsumer64_data.err_unix_sock_path);
76d7553f 3696 PERROR("chown");
7753dea8
MD
3697 }
3698
3699 /* 32-bit ustconsumer compat32 error socket path */
3700 ret = chown(ustconsumer32_data.err_unix_sock_path, 0, gid);
3bd1e081 3701 if (ret < 0) {
7753dea8 3702 ERR("Unable to set group on %s", ustconsumer32_data.err_unix_sock_path);
76d7553f 3703 PERROR("chown");
fac6795d
DG
3704 }
3705
d6f42150 3706 DBG("All permissions are set");
e07ae692 3707
fac6795d
DG
3708end:
3709 return ret;
3710}
3711
d6f42150 3712/*
d063d709 3713 * Create the lttng run directory needed for all global sockets and pipe.
d6f42150 3714 */
67e40797 3715static int create_lttng_rundir(const char *rundir)
d6f42150
DG
3716{
3717 int ret;
3718
67e40797
DG
3719 DBG3("Creating LTTng run directory: %s", rundir);
3720
d1613cf5 3721 ret = mkdir(rundir, S_IRWXU);
d6f42150 3722 if (ret < 0) {
b1f11e69 3723 if (errno != EEXIST) {
67e40797 3724 ERR("Unable to create %s", rundir);
b1f11e69
DG
3725 goto error;
3726 } else {
3727 ret = 0;
3728 }
d6f42150
DG
3729 }
3730
3731error:
3732 return ret;
3733}
3734
3735/*
d063d709
DG
3736 * Setup sockets and directory needed by the kconsumerd communication with the
3737 * session daemon.
d6f42150 3738 */
67e40797
DG
3739static int set_consumer_sockets(struct consumer_data *consumer_data,
3740 const char *rundir)
d6f42150
DG
3741{
3742 int ret;
67e40797 3743 char path[PATH_MAX];
d6f42150 3744
67e40797 3745 switch (consumer_data->type) {
7753dea8 3746 case LTTNG_CONSUMER_KERNEL:
60922cb0 3747 snprintf(path, PATH_MAX, DEFAULT_KCONSUMERD_PATH, rundir);
7753dea8
MD
3748 break;
3749 case LTTNG_CONSUMER64_UST:
60922cb0 3750 snprintf(path, PATH_MAX, DEFAULT_USTCONSUMERD64_PATH, rundir);
7753dea8
MD
3751 break;
3752 case LTTNG_CONSUMER32_UST:
60922cb0 3753 snprintf(path, PATH_MAX, DEFAULT_USTCONSUMERD32_PATH, rundir);
7753dea8
MD
3754 break;
3755 default:
3756 ERR("Consumer type unknown");
3757 ret = -EINVAL;
3758 goto error;
d6f42150
DG
3759 }
3760
67e40797
DG
3761 DBG2("Creating consumer directory: %s", path);
3762
d1613cf5 3763 ret = mkdir(path, S_IRWXU);
d6f42150 3764 if (ret < 0) {
6beb2242 3765 if (errno != EEXIST) {
f11e84c2 3766 PERROR("mkdir");
3bd1e081 3767 ERR("Failed to create %s", path);
6beb2242
DG
3768 goto error;
3769 }
f11e84c2 3770 ret = -1;
d6f42150
DG
3771 }
3772
3773 /* Create the kconsumerd error unix socket */
3bd1e081
MD
3774 consumer_data->err_sock =
3775 lttcomm_create_unix_sock(consumer_data->err_unix_sock_path);
3776 if (consumer_data->err_sock < 0) {
3777 ERR("Create unix sock failed: %s", consumer_data->err_unix_sock_path);
d6f42150
DG
3778 ret = -1;
3779 goto error;
3780 }
3781
3782 /* File permission MUST be 660 */
3bd1e081 3783 ret = chmod(consumer_data->err_unix_sock_path,
54d01ffb 3784 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
d6f42150 3785 if (ret < 0) {
3bd1e081 3786 ERR("Set file permissions failed: %s", consumer_data->err_unix_sock_path);
67e40797 3787 PERROR("chmod");
d6f42150
DG
3788 goto error;
3789 }
3790
3791error:
3792 return ret;
3793}
3794
fac6795d 3795/*
d063d709 3796 * Signal handler for the daemon
cf3af59e 3797 *
54d01ffb
DG
3798 * Simply stop all worker threads, leaving main() return gracefully after
3799 * joining all threads and calling cleanup().
fac6795d
DG
3800 */
3801static void sighandler(int sig)
3802{
3803 switch (sig) {
cf3af59e 3804 case SIGPIPE:
af87c45a 3805 DBG("SIGPIPE caught");
cf3af59e
MD
3806 return;
3807 case SIGINT:
af87c45a 3808 DBG("SIGINT caught");
cf3af59e
MD
3809 stop_threads();
3810 break;
3811 case SIGTERM:
af87c45a 3812 DBG("SIGTERM caught");
cf3af59e
MD
3813 stop_threads();
3814 break;
3815 default:
3816 break;
fac6795d 3817 }
fac6795d
DG
3818}
3819
3820/*
d063d709 3821 * Setup signal handler for :
1d4b027a 3822 * SIGINT, SIGTERM, SIGPIPE
fac6795d 3823 */
1d4b027a 3824static int set_signal_handler(void)
fac6795d 3825{
1d4b027a
DG
3826 int ret = 0;
3827 struct sigaction sa;
3828 sigset_t sigset;
fac6795d 3829
1d4b027a 3830 if ((ret = sigemptyset(&sigset)) < 0) {
76d7553f 3831 PERROR("sigemptyset");
1d4b027a
DG
3832 return ret;
3833 }
d6f42150 3834
1d4b027a
DG
3835 sa.sa_handler = sighandler;
3836 sa.sa_mask = sigset;
3837 sa.sa_flags = 0;
3838 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
76d7553f 3839 PERROR("sigaction");
1d4b027a 3840 return ret;
d6f42150
DG
3841 }
3842
1d4b027a 3843 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
76d7553f 3844 PERROR("sigaction");
1d4b027a 3845 return ret;
d6f42150 3846 }
aaf26714 3847
1d4b027a 3848 if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) {
76d7553f 3849 PERROR("sigaction");
1d4b027a 3850 return ret;
8c0faa1d
DG
3851 }
3852
1d4b027a
DG
3853 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
3854
3855 return ret;
fac6795d
DG
3856}
3857
f3ed775e 3858/*
d063d709
DG
3859 * Set open files limit to unlimited. This daemon can open a large number of
3860 * file descriptors in order to consumer multiple kernel traces.
f3ed775e
DG
3861 */
3862static void set_ulimit(void)
3863{
3864 int ret;
3865 struct rlimit lim;
3866
a88df331 3867 /* The kernel does not allowed an infinite limit for open files */
f3ed775e
DG
3868 lim.rlim_cur = 65535;
3869 lim.rlim_max = 65535;
3870
3871 ret = setrlimit(RLIMIT_NOFILE, &lim);
3872 if (ret < 0) {
76d7553f 3873 PERROR("failed to set open files limit");
f3ed775e
DG
3874 }
3875}
3876
35f90c40
DG
3877/*
3878 * Write pidfile using the rundir and opt_pidfile.
3879 */
3880static void write_pidfile(void)
3881{
3882 int ret;
3883 char pidfile_path[PATH_MAX];
3884
3885 assert(rundir);
3886
3887 if (opt_pidfile) {
3888 strncpy(pidfile_path, opt_pidfile, sizeof(pidfile_path));
3889 } else {
3890 /* Build pidfile path from rundir and opt_pidfile. */
3891 ret = snprintf(pidfile_path, sizeof(pidfile_path), "%s/"
3892 DEFAULT_LTTNG_SESSIOND_PIDFILE, rundir);
3893 if (ret < 0) {
3894 PERROR("snprintf pidfile path");
3895 goto error;
3896 }
3897 }
3898
3899 /*
3900 * Create pid file in rundir. Return value is of no importance. The
3901 * execution will continue even though we are not able to write the file.
3902 */
3903 (void) utils_create_pid_file(getpid(), pidfile_path);
3904
3905error:
3906 return;
3907}
3908
fac6795d
DG
3909/*
3910 * main
3911 */
3912int main(int argc, char **argv)
3913{
fac6795d
DG
3914 int ret = 0;
3915 void *status;
ae9e45b3 3916 const char *home_path, *env_app_timeout;
fac6795d 3917
335a95b7
MD
3918 init_kernel_workarounds();
3919
f6a9efaa
DG
3920 rcu_register_thread();
3921
7753dea8 3922 setup_consumerd_path();
fb09408a 3923
fac6795d
DG
3924 /* Parse arguments */
3925 progname = argv[0];
c617c0c6 3926 if ((ret = parse_args(argc, argv)) < 0) {
cf3af59e 3927 goto error;
fac6795d
DG
3928 }
3929
3930 /* Daemonize */
3931 if (opt_daemon) {
ceed52b5
MD
3932 int i;
3933
3934 /*
3935 * fork
3936 * child: setsid, close FD 0, 1, 2, chdir /
3937 * parent: exit (if fork is successful)
3938 */
53094c05
DG
3939 ret = daemon(0, 0);
3940 if (ret < 0) {
76d7553f 3941 PERROR("daemon");
cf3af59e 3942 goto error;
53094c05 3943 }
ceed52b5
MD
3944 /*
3945 * We are in the child. Make sure all other file
3946 * descriptors are closed, in case we are called with
3947 * more opened file descriptors than the standard ones.
3948 */
3949 for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) {
3950 (void) close(i);
3951 }
3952 }
3953
3954 /* Create thread quit pipe */
3955 if ((ret = init_thread_quit_pipe()) < 0) {
3956 goto error;
fac6795d
DG
3957 }
3958
3959 /* Check if daemon is UID = 0 */
3960 is_root = !getuid();
3961
fac6795d 3962 if (is_root) {
990570ed 3963 rundir = strdup(DEFAULT_LTTNG_RUNDIR);
67e40797
DG
3964
3965 /* Create global run dir with root access */
3966 ret = create_lttng_rundir(rundir);
d6f42150 3967 if (ret < 0) {
cf3af59e 3968 goto error;
d6f42150
DG
3969 }
3970
fac6795d 3971 if (strlen(apps_unix_sock_path) == 0) {
d6f42150
DG
3972 snprintf(apps_unix_sock_path, PATH_MAX,
3973 DEFAULT_GLOBAL_APPS_UNIX_SOCK);
fac6795d
DG
3974 }
3975
3976 if (strlen(client_unix_sock_path) == 0) {
d6f42150
DG
3977 snprintf(client_unix_sock_path, PATH_MAX,
3978 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK);
3979 }
0fdd1e2c
DG
3980
3981 /* Set global SHM for ust */
3982 if (strlen(wait_shm_path) == 0) {
3983 snprintf(wait_shm_path, PATH_MAX,
3984 DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH);
3985 }
67e40797 3986
44a5e5eb
DG
3987 if (strlen(health_unix_sock_path) == 0) {
3988 snprintf(health_unix_sock_path, sizeof(health_unix_sock_path),
3989 DEFAULT_GLOBAL_HEALTH_UNIX_SOCK);
3990 }
3991
67e40797
DG
3992 /* Setup kernel consumerd path */
3993 snprintf(kconsumer_data.err_unix_sock_path, PATH_MAX,
60922cb0 3994 DEFAULT_KCONSUMERD_ERR_SOCK_PATH, rundir);
67e40797 3995 snprintf(kconsumer_data.cmd_unix_sock_path, PATH_MAX,
60922cb0 3996 DEFAULT_KCONSUMERD_CMD_SOCK_PATH, rundir);
67e40797
DG
3997
3998 DBG2("Kernel consumer err path: %s",
3999 kconsumer_data.err_unix_sock_path);
4000 DBG2("Kernel consumer cmd path: %s",
4001 kconsumer_data.cmd_unix_sock_path);
fac6795d 4002 } else {
b082db07
DG
4003 home_path = get_home_dir();
4004 if (home_path == NULL) {
273ea72c
DG
4005 /* TODO: Add --socket PATH option */
4006 ERR("Can't get HOME directory for sockets creation.");
cf3af59e
MD
4007 ret = -EPERM;
4008 goto error;
b082db07
DG
4009 }
4010
67e40797
DG
4011 /*
4012 * Create rundir from home path. This will create something like
4013 * $HOME/.lttng
4014 */
990570ed 4015 ret = asprintf(&rundir, DEFAULT_LTTNG_HOME_RUNDIR, home_path);
67e40797
DG
4016 if (ret < 0) {
4017 ret = -ENOMEM;
4018 goto error;
4019 }
4020
4021 ret = create_lttng_rundir(rundir);
4022 if (ret < 0) {
4023 goto error;
4024 }
4025
fac6795d 4026 if (strlen(apps_unix_sock_path) == 0) {
d6f42150 4027 snprintf(apps_unix_sock_path, PATH_MAX,
b082db07 4028 DEFAULT_HOME_APPS_UNIX_SOCK, home_path);
fac6795d
DG
4029 }
4030
4031 /* Set the cli tool unix socket path */
4032 if (strlen(client_unix_sock_path) == 0) {
d6f42150 4033 snprintf(client_unix_sock_path, PATH_MAX,
b082db07 4034 DEFAULT_HOME_CLIENT_UNIX_SOCK, home_path);
fac6795d 4035 }
0fdd1e2c
DG
4036
4037 /* Set global SHM for ust */
4038 if (strlen(wait_shm_path) == 0) {
4039 snprintf(wait_shm_path, PATH_MAX,
4040 DEFAULT_HOME_APPS_WAIT_SHM_PATH, geteuid());
4041 }
44a5e5eb
DG
4042
4043 /* Set health check Unix path */
4044 if (strlen(health_unix_sock_path) == 0) {
4045 snprintf(health_unix_sock_path, sizeof(health_unix_sock_path),
4046 DEFAULT_HOME_HEALTH_UNIX_SOCK, home_path);
4047 }
fac6795d
DG
4048 }
4049
5c827ce0
DG
4050 /* Set consumer initial state */
4051 kernel_consumerd_state = CONSUMER_STOPPED;
4052 ust_consumerd_state = CONSUMER_STOPPED;
4053
847177cd
DG
4054 DBG("Client socket path %s", client_unix_sock_path);
4055 DBG("Application socket path %s", apps_unix_sock_path);
67e40797
DG
4056 DBG("LTTng run directory path: %s", rundir);
4057
4058 /* 32 bits consumerd path setup */
4059 snprintf(ustconsumer32_data.err_unix_sock_path, PATH_MAX,
60922cb0 4060 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH, rundir);
67e40797 4061 snprintf(ustconsumer32_data.cmd_unix_sock_path, PATH_MAX,
60922cb0 4062 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH, rundir);
67e40797
DG
4063
4064 DBG2("UST consumer 32 bits err path: %s",
4065 ustconsumer32_data.err_unix_sock_path);
4066 DBG2("UST consumer 32 bits cmd path: %s",
4067 ustconsumer32_data.cmd_unix_sock_path);
4068
4069 /* 64 bits consumerd path setup */
4070 snprintf(ustconsumer64_data.err_unix_sock_path, PATH_MAX,
60922cb0 4071 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH, rundir);
67e40797 4072 snprintf(ustconsumer64_data.cmd_unix_sock_path, PATH_MAX,
60922cb0 4073 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH, rundir);
67e40797
DG
4074
4075 DBG2("UST consumer 64 bits err path: %s",
4076 ustconsumer64_data.err_unix_sock_path);
4077 DBG2("UST consumer 64 bits cmd path: %s",
4078 ustconsumer64_data.cmd_unix_sock_path);
847177cd 4079
273ea72c 4080 /*
7d8234d9 4081 * See if daemon already exist.
fac6795d 4082 */
7d8234d9 4083 if ((ret = check_existing_daemon()) < 0) {
75462a81 4084 ERR("Already running daemon.\n");
273ea72c 4085 /*
cf3af59e
MD
4086 * We do not goto exit because we must not cleanup()
4087 * because a daemon is already running.
ab118b20 4088 */
cf3af59e 4089 goto error;
a88df331
DG
4090 }
4091
1427f9b2
DG
4092 /*
4093 * Init UST app hash table. Alloc hash table before this point since
4094 * cleanup() can get called after that point.
4095 */
4096 ust_app_ht_alloc();
4097
54d01ffb 4098 /* After this point, we can safely call cleanup() with "goto exit" */
a88df331
DG
4099
4100 /*
4101 * These actions must be executed as root. We do that *after* setting up
4102 * the sockets path because we MUST make the check for another daemon using
4103 * those paths *before* trying to set the kernel consumer sockets and init
4104 * kernel tracer.
4105 */
4106 if (is_root) {
67e40797 4107 ret = set_consumer_sockets(&kconsumer_data, rundir);
7753dea8
MD
4108 if (ret < 0) {
4109 goto exit;
4110 }
4111
a88df331 4112 /* Setup kernel tracer */
4fba7219
DG
4113 if (!opt_no_kernel) {
4114 init_kernel_tracer();
4115 }
a88df331
DG
4116
4117 /* Set ulimit for open files */
4118 set_ulimit();
fac6795d 4119 }
4063050c
MD
4120 /* init lttng_fd tracking must be done after set_ulimit. */
4121 lttng_fd_init();
fac6795d 4122
67e40797
DG
4123 ret = set_consumer_sockets(&ustconsumer64_data, rundir);
4124 if (ret < 0) {
4125 goto exit;
4126 }
4127
4128 ret = set_consumer_sockets(&ustconsumer32_data, rundir);
4129 if (ret < 0) {
4130 goto exit;
4131 }
4132
cf3af59e
MD
4133 if ((ret = set_signal_handler()) < 0) {
4134 goto exit;
fac6795d
DG
4135 }
4136
d6f42150 4137 /* Setup the needed unix socket */
cf3af59e
MD
4138 if ((ret = init_daemon_socket()) < 0) {
4139 goto exit;
fac6795d
DG
4140 }
4141
4142 /* Set credentials to socket */
be040666 4143 if (is_root && ((ret = set_permissions(rundir)) < 0)) {
cf3af59e 4144 goto exit;
fac6795d
DG
4145 }
4146
5b8719f5
DG
4147 /* Get parent pid if -S, --sig-parent is specified. */
4148 if (opt_sig_parent) {
4149 ppid = getppid();
4150 }
4151
7a485870 4152 /* Setup the kernel pipe for waking up the kernel thread */
6620da75
DG
4153 if (is_root && !opt_no_kernel) {
4154 if ((ret = utils_create_pipe_cloexec(kernel_poll_pipe)) < 0) {
4155 goto exit;
4156 }
7a485870
DG
4157 }
4158
099e26bd 4159 /* Setup the thread apps communication pipe. */
ef599319 4160 if ((ret = utils_create_pipe_cloexec(apps_cmd_pipe)) < 0) {
099e26bd
DG
4161 goto exit;
4162 }
4163
4164 /* Init UST command queue. */
4165 cds_wfq_init(&ust_cmd_queue.queue);
4166
273ea72c 4167 /*
54d01ffb
DG
4168 * Get session list pointer. This pointer MUST NOT be free(). This list is
4169 * statically declared in session.c
273ea72c 4170 */
54d01ffb 4171 session_list_ptr = session_get_list();
b5541356 4172
5eb91c98
DG
4173 /* Set up max poll set size */
4174 lttng_poll_set_max_size();
4175
2f77fc4b 4176 cmd_init();
00e2e675 4177
ae9e45b3
DG
4178 /* Check for the application socket timeout env variable. */
4179 env_app_timeout = getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV);
4180 if (env_app_timeout) {
4181 app_socket_timeout = atoi(env_app_timeout);
4182 } else {
4183 app_socket_timeout = DEFAULT_APP_SOCKET_RW_TIMEOUT;
4184 }
4185
35f90c40
DG
4186 write_pidfile();
4187
44a5e5eb
DG
4188 /* Create thread to manage the client socket */
4189 ret = pthread_create(&health_thread, NULL,
4190 thread_manage_health, (void *) NULL);
4191 if (ret != 0) {
4192 PERROR("pthread_create health");
4193 goto exit_health;
4194 }
4195
cf3af59e 4196 /* Create thread to manage the client socket */
099e26bd
DG
4197 ret = pthread_create(&client_thread, NULL,
4198 thread_manage_clients, (void *) NULL);
cf3af59e 4199 if (ret != 0) {
76d7553f 4200 PERROR("pthread_create clients");
cf3af59e
MD
4201 goto exit_client;
4202 }
fac6795d 4203
099e26bd
DG
4204 /* Create thread to dispatch registration */
4205 ret = pthread_create(&dispatch_thread, NULL,
4206 thread_dispatch_ust_registration, (void *) NULL);
4207 if (ret != 0) {
76d7553f 4208 PERROR("pthread_create dispatch");
099e26bd
DG
4209 goto exit_dispatch;
4210 }
4211
4212 /* Create thread to manage application registration. */
4213 ret = pthread_create(&reg_apps_thread, NULL,
4214 thread_registration_apps, (void *) NULL);
4215 if (ret != 0) {
76d7553f 4216 PERROR("pthread_create registration");
099e26bd
DG
4217 goto exit_reg_apps;
4218 }
4219
cf3af59e 4220 /* Create thread to manage application socket */
54d01ffb
DG
4221 ret = pthread_create(&apps_thread, NULL,
4222 thread_manage_apps, (void *) NULL);
cf3af59e 4223 if (ret != 0) {
76d7553f 4224 PERROR("pthread_create apps");
cf3af59e
MD
4225 goto exit_apps;
4226 }
fac6795d 4227
6620da75
DG
4228 /* Don't start this thread if kernel tracing is not requested nor root */
4229 if (is_root && !opt_no_kernel) {
4230 /* Create kernel thread to manage kernel event */
4231 ret = pthread_create(&kernel_thread, NULL,
4232 thread_manage_kernel, (void *) NULL);
4233 if (ret != 0) {
4234 PERROR("pthread_create kernel");
4235 goto exit_kernel;
4236 }
7a485870 4237
6620da75
DG
4238 ret = pthread_join(kernel_thread, &status);
4239 if (ret != 0) {
4240 PERROR("pthread_join");
4241 goto error; /* join error, exit without cleanup */
4242 }
fac6795d
DG
4243 }
4244
cf3af59e
MD
4245exit_kernel:
4246 ret = pthread_join(apps_thread, &status);
4247 if (ret != 0) {
76d7553f 4248 PERROR("pthread_join");
cf3af59e
MD
4249 goto error; /* join error, exit without cleanup */
4250 }
fac6795d 4251
cf3af59e 4252exit_apps:
099e26bd
DG
4253 ret = pthread_join(reg_apps_thread, &status);
4254 if (ret != 0) {
76d7553f 4255 PERROR("pthread_join");
099e26bd
DG
4256 goto error; /* join error, exit without cleanup */
4257 }
4258
4259exit_reg_apps:
4260 ret = pthread_join(dispatch_thread, &status);
4261 if (ret != 0) {
76d7553f 4262 PERROR("pthread_join");
099e26bd
DG
4263 goto error; /* join error, exit without cleanup */
4264 }
4265
4266exit_dispatch:
cf3af59e
MD
4267 ret = pthread_join(client_thread, &status);
4268 if (ret != 0) {
76d7553f 4269 PERROR("pthread_join");
cf3af59e
MD
4270 goto error; /* join error, exit without cleanup */
4271 }
4272
3bd1e081 4273 ret = join_consumer_thread(&kconsumer_data);
cf3af59e 4274 if (ret != 0) {
76d7553f 4275 PERROR("join_consumer");
cf3af59e
MD
4276 goto error; /* join error, exit without cleanup */
4277 }
a88df331 4278
06f525de
DG
4279 ret = join_consumer_thread(&ustconsumer32_data);
4280 if (ret != 0) {
4281 PERROR("join_consumer ust32");
4282 goto error; /* join error, exit without cleanup */
4283 }
4284
4285 ret = join_consumer_thread(&ustconsumer64_data);
4286 if (ret != 0) {
4287 PERROR("join_consumer ust64");
4288 goto error; /* join error, exit without cleanup */
4289 }
4290
cf3af59e 4291exit_client:
06f525de
DG
4292 ret = pthread_join(health_thread, &status);
4293 if (ret != 0) {
4294 PERROR("pthread_join health thread");
4295 goto error; /* join error, exit without cleanup */
4296 }
4297
44a5e5eb 4298exit_health:
a88df331 4299exit:
cf3af59e
MD
4300 /*
4301 * cleanup() is called when no other thread is running.
4302 */
f6a9efaa 4303 rcu_thread_online();
cf3af59e 4304 cleanup();
f6a9efaa
DG
4305 rcu_thread_offline();
4306 rcu_unregister_thread();
67e40797 4307 if (!ret) {
cf3af59e 4308 exit(EXIT_SUCCESS);
67e40797 4309 }
cf3af59e 4310error:
5e16da05 4311 exit(EXIT_FAILURE);
fac6795d 4312}
This page took 0.340388 seconds and 4 git commands to generate.