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