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