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