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