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