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