Merge branch 'master' of git://git.lttng.org/lttng-tools
[lttng-tools.git] / ltt-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
DG
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
82a3637f
DG
7 * as published by the Free Software Foundation; only version 2
8 * of the License.
91d76f53 9 *
fac6795d
DG
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
91d76f53 14 *
fac6795d
DG
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
fac6795d
DG
18 */
19
20#define _GNU_SOURCE
21#include <fcntl.h>
22#include <getopt.h>
23#include <grp.h>
24#include <limits.h>
7a485870 25#include <poll.h>
fac6795d 26#include <pthread.h>
8c0faa1d 27#include <semaphore.h>
fac6795d
DG
28#include <signal.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
0fdd1e2c 32#include <sys/mman.h>
b73401da 33#include <sys/mount.h>
1e307fab 34#include <sys/resource.h>
fac6795d
DG
35#include <sys/socket.h>
36#include <sys/stat.h>
37#include <sys/types.h>
0fdd1e2c
DG
38#include <sys/wait.h>
39#include <urcu/futex.h>
fac6795d
DG
40#include <unistd.h>
41
1e307fab 42#include <ltt-kconsumerd.h>
e88129fc 43#include <lttng-sessiond-comm.h>
1e307fab
DG
44#include <lttng/lttng-kconsumerd.h>
45#include <lttngerr.h>
fac6795d 46
b579acd9 47#include "context.h"
099e26bd 48#include "futex.h"
20fe2104 49#include "kernel-ctl.h"
1e307fab 50#include "ltt-sessiond.h"
0fdd1e2c 51#include "shm.h"
91d76f53 52#include "traceable-app.h"
1e307fab 53#include "ust-ctl.h"
8e68d1c8 54#include "utils.h"
0177d773 55#include "ust-ctl.h"
fac6795d 56
b25a8868
DG
57#include "benchmark.h"
58
75462a81 59/* Const values */
686204ab 60const char default_home_dir[] = DEFAULT_HOME_DIR;
64a23ac4 61const char default_tracing_group[] = LTTNG_DEFAULT_TRACING_GROUP;
686204ab
MD
62const char default_ust_sock_dir[] = DEFAULT_UST_SOCK_DIR;
63const char default_global_apps_pipe[] = DEFAULT_GLOBAL_APPS_PIPE;
64
fac6795d 65/* Variables */
1d4b027a 66int opt_verbose; /* Not static for lttngerr.h */
31f73cc9 67int opt_verbose_kconsumerd; /* Not static for lttngerr.h */
1d4b027a 68int opt_quiet; /* Not static for lttngerr.h */
d063d709 69
fac6795d
DG
70const char *progname;
71const char *opt_tracing_group;
5b8719f5 72static int opt_sig_parent;
fac6795d
DG
73static int opt_daemon;
74static int is_root; /* Set to 1 if the daemon is running as root */
1d4b027a
DG
75static pid_t ppid; /* Parent PID for --sig-parent option */
76static pid_t kconsumerd_pid;
7a485870 77static struct pollfd *kernel_pollfd;
099e26bd 78static int dispatch_thread_exit;
fac6795d 79
d6f42150
DG
80static char apps_unix_sock_path[PATH_MAX]; /* Global application Unix socket path */
81static char client_unix_sock_path[PATH_MAX]; /* Global client Unix socket path */
82static char kconsumerd_err_unix_sock_path[PATH_MAX]; /* kconsumerd error Unix socket path */
83static char kconsumerd_cmd_unix_sock_path[PATH_MAX]; /* kconsumerd command Unix socket path */
0fdd1e2c 84static char wait_shm_path[PATH_MAX]; /* global wait shm path for UST */
fac6795d 85
1d4b027a 86/* Sockets and FDs */
d6f42150
DG
87static int client_sock;
88static int apps_sock;
89static int kconsumerd_err_sock;
8c0faa1d 90static int kconsumerd_cmd_sock;
20fe2104 91static int kernel_tracer_fd;
7a485870 92static int kernel_poll_pipe[2];
1d4b027a 93
273ea72c
DG
94/*
95 * Quit pipe for all threads. This permits a single cancellation point
96 * for all threads when receiving an event on the pipe.
97 */
98static int thread_quit_pipe[2];
99
099e26bd
DG
100/*
101 * This pipe is used to inform the thread managing application communication
102 * that a command is queued and ready to be processed.
103 */
104static int apps_cmd_pipe[2];
105
1d4b027a 106/* Pthread, Mutexes and Semaphores */
8c0faa1d 107static pthread_t kconsumerd_thread;
1d4b027a 108static pthread_t apps_thread;
099e26bd 109static pthread_t reg_apps_thread;
1d4b027a 110static pthread_t client_thread;
7a485870 111static pthread_t kernel_thread;
099e26bd 112static pthread_t dispatch_thread;
8c0faa1d
DG
113static sem_t kconsumerd_sem;
114
1d4b027a 115static pthread_mutex_t kconsumerd_pid_mutex; /* Mutex to control kconsumerd pid assignation */
fac6795d 116
099e26bd
DG
117/*
118 * UST registration command queue. This queue is tied with a futex and uses a N
119 * wakers / 1 waiter implemented and detailed in futex.c/.h
120 *
121 * The thread_manage_apps and thread_dispatch_ust_registration interact with
122 * this queue and the wait/wake scheme.
123 */
124static struct ust_cmd_queue ust_cmd_queue;
125
b5541356
DG
126/*
127 * Pointer initialized before thread creation.
128 *
129 * This points to the tracing session list containing the session count and a
130 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
131 * MUST NOT be taken if you call a public function in session.c.
04ea676f 132 *
d063d709
DG
133 * The lock is nested inside the structure: session_list_ptr->lock. Please use
134 * lock_session_list and unlock_session_list for lock acquisition.
b5541356
DG
135 */
136static struct ltt_session_list *session_list_ptr;
137
0fdd1e2c
DG
138/*
139 * Remove modules in reverse load order.
140 */
141static int modprobe_remove_kernel_modules(void)
142{
143 int ret = 0, i;
144 char modprobe[256];
145
146 for (i = ARRAY_SIZE(kernel_modules_list) - 1; i >= 0; i--) {
147 ret = snprintf(modprobe, sizeof(modprobe),
148 "/sbin/modprobe --remove --quiet %s",
149 kernel_modules_list[i].name);
150 if (ret < 0) {
151 perror("snprintf modprobe --remove");
152 goto error;
153 }
154 modprobe[sizeof(modprobe) - 1] = '\0';
155 ret = system(modprobe);
156 if (ret == -1) {
157 ERR("Unable to launch modprobe --remove for module %s",
158 kernel_modules_list[i].name);
159 } else if (kernel_modules_list[i].required
160 && WEXITSTATUS(ret) != 0) {
161 ERR("Unable to remove module %s",
162 kernel_modules_list[i].name);
163 } else {
164 DBG("Modprobe removal successful %s",
165 kernel_modules_list[i].name);
166 }
167 }
168
169error:
170 return ret;
171}
172
173/*
174 * Return group ID of the tracing group or -1 if not found.
175 */
996b65c8
MD
176static gid_t allowed_group(void)
177{
178 struct group *grp;
179
274e143b
MD
180 if (opt_tracing_group) {
181 grp = getgrnam(opt_tracing_group);
182 } else {
183 grp = getgrnam(default_tracing_group);
184 }
996b65c8
MD
185 if (!grp) {
186 return -1;
187 } else {
188 return grp->gr_gid;
189 }
190}
191
273ea72c
DG
192/*
193 * Init quit pipe.
194 *
195 * Return -1 on error or 0 if all pipes are created.
196 */
197static int init_thread_quit_pipe(void)
198{
199 int ret;
200
201 ret = pipe2(thread_quit_pipe, O_CLOEXEC);
202 if (ret < 0) {
203 perror("thread quit pipe");
204 goto error;
205 }
206
207error:
208 return ret;
209}
210
fac6795d 211/*
d063d709
DG
212 * Complete teardown of a kernel session. This free all data structure related
213 * to a kernel session and update counter.
fac6795d 214 */
1d4b027a 215static void teardown_kernel_session(struct ltt_session *session)
fac6795d 216{
1d4b027a
DG
217 if (session->kernel_session != NULL) {
218 DBG("Tearing down kernel session");
d9800920
DG
219
220 /*
221 * If a custom kernel consumer was registered, close the socket before
222 * tearing down the complete kernel session structure
223 */
224 if (session->kernel_session->consumer_fd != kconsumerd_cmd_sock) {
225 lttcomm_close_unix_sock(session->kernel_session->consumer_fd);
226 }
227
62499ad6 228 trace_kernel_destroy_session(session->kernel_session);
1d4b027a
DG
229 /* Extra precaution */
230 session->kernel_session = NULL;
fac6795d 231 }
fac6795d
DG
232}
233
099e26bd
DG
234/*
235 * Stop all threads by closing the thread quit pipe.
236 */
cf3af59e
MD
237static void stop_threads(void)
238{
239 /* Stopping all threads */
240 DBG("Terminating all threads");
241 close(thread_quit_pipe[0]);
242 close(thread_quit_pipe[1]);
099e26bd
DG
243 /* Dispatch thread */
244 dispatch_thread_exit = 1;
245 futex_nto1_wake(&ust_cmd_queue.futex);
cf3af59e
MD
246}
247
fac6795d 248/*
d063d709 249 * Cleanup the daemon
fac6795d 250 */
cf3af59e 251static void cleanup(void)
fac6795d 252{
1d4b027a
DG
253 int ret;
254 char *cmd;
af9737e9 255 struct ltt_session *sess, *stmp;
fac6795d 256
1d4b027a 257 DBG("Cleaning up");
e07ae692 258
1d4b027a 259 /* <fun> */
2f50c8a3 260 MSG("%c[%d;%dm*** assert failed *** ==> %c[%dm%c[%d;%dm"
273ea72c
DG
261 "Matthew, BEET driven development works!%c[%dm",
262 27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
1d4b027a 263 /* </fun> */
fac6795d 264
1d4b027a
DG
265 DBG("Removing %s directory", LTTNG_RUNDIR);
266 ret = asprintf(&cmd, "rm -rf " LTTNG_RUNDIR);
267 if (ret < 0) {
268 ERR("asprintf failed. Something is really wrong!");
269 }
5461b305 270
1d4b027a
DG
271 /* Remove lttng run directory */
272 ret = system(cmd);
273 if (ret < 0) {
274 ERR("Unable to clean " LTTNG_RUNDIR);
275 }
5461b305 276
1d4b027a 277 DBG("Cleaning up all session");
fac6795d 278
b5541356 279 /* Destroy session list mutex */
273ea72c
DG
280 if (session_list_ptr != NULL) {
281 pthread_mutex_destroy(&session_list_ptr->lock);
282
283 /* Cleanup ALL session */
af9737e9 284 cds_list_for_each_entry_safe(sess, stmp, &session_list_ptr->head, list) {
273ea72c
DG
285 teardown_kernel_session(sess);
286 // TODO complete session cleanup (including UST)
287 }
288 }
289
099e26bd
DG
290 DBG("Closing all UST sockets");
291 clean_traceable_apps_list();
292
273ea72c 293 pthread_mutex_destroy(&kconsumerd_pid_mutex);
b5541356 294
f3ed775e 295 DBG("Closing kernel fd");
1d4b027a 296 close(kernel_tracer_fd);
ab147185 297
2f50c8a3
DG
298 if (is_root) {
299 DBG("Unloading kernel modules");
300 modprobe_remove_kernel_modules();
301 }
b25a8868 302
3c6bae61
DG
303 /* OUTPUT BENCHMARK RESULTS */
304 bench_init();
305
62d53818
DG
306 if (getenv("BENCH_UST_NOTIFY")) {
307 bench_print_ust_notification();
308 }
309
310 if (getenv("BENCH_UST_REGISTER")) {
311 bench_print_ust_register();
312 }
313
314 if (getenv("BENCH_BOOT_PROCESS")) {
315 bench_print_boot_process();
316 }
3c6bae61
DG
317
318 bench_close();
319 /* END BENCHMARK */
fac6795d
DG
320}
321
e065084a 322/*
d063d709 323 * Send data on a unix socket using the liblttsessiondcomm API.
e065084a 324 *
d063d709 325 * Return lttcomm error code.
e065084a
DG
326 */
327static int send_unix_sock(int sock, void *buf, size_t len)
328{
329 /* Check valid length */
330 if (len <= 0) {
331 return -1;
332 }
333
334 return lttcomm_send_unix_sock(sock, buf, len);
335}
336
5461b305 337/*
d063d709 338 * Free memory of a command context structure.
5461b305 339 */
a2fb29a5 340static void clean_command_ctx(struct command_ctx **cmd_ctx)
5461b305 341{
a2fb29a5
DG
342 DBG("Clean command context structure");
343 if (*cmd_ctx) {
344 if ((*cmd_ctx)->llm) {
345 free((*cmd_ctx)->llm);
5461b305 346 }
a2fb29a5
DG
347 if ((*cmd_ctx)->lsm) {
348 free((*cmd_ctx)->lsm);
5461b305 349 }
a2fb29a5
DG
350 free(*cmd_ctx);
351 *cmd_ctx = NULL;
5461b305
DG
352 }
353}
354
f3ed775e 355/*
d063d709 356 * Send all stream fds of kernel channel to the consumer.
f3ed775e 357 */
7a485870 358static int send_kconsumerd_channel_fds(int sock, struct ltt_kernel_channel *channel)
f3ed775e
DG
359{
360 int ret;
361 size_t nb_fd;
362 struct ltt_kernel_stream *stream;
f3ed775e
DG
363 struct lttcomm_kconsumerd_header lkh;
364 struct lttcomm_kconsumerd_msg lkm;
365
7a485870
DG
366 DBG("Sending fds of channel %s to kernel consumer", channel->channel->name);
367
368 nb_fd = channel->stream_count;
f3ed775e
DG
369
370 /* Setup header */
7a485870 371 lkh.payload_size = nb_fd * sizeof(struct lttcomm_kconsumerd_msg);
f3ed775e
DG
372 lkh.cmd_type = ADD_STREAM;
373
374 DBG("Sending kconsumerd header");
375
376 ret = lttcomm_send_unix_sock(sock, &lkh, sizeof(struct lttcomm_kconsumerd_header));
377 if (ret < 0) {
378 perror("send kconsumerd header");
379 goto error;
380 }
381
7a485870
DG
382 cds_list_for_each_entry(stream, &channel->stream_list.head, list) {
383 if (stream->fd != 0) {
f3ed775e
DG
384 lkm.fd = stream->fd;
385 lkm.state = stream->state;
7a485870 386 lkm.max_sb_size = channel->channel->attr.subbuf_size;
e8b60857 387 lkm.output = channel->channel->attr.output;
f3ed775e 388 strncpy(lkm.path_name, stream->pathname, PATH_MAX);
99497cd0 389 lkm.path_name[PATH_MAX - 1] = '\0';
f3ed775e
DG
390
391 DBG("Sending fd %d to kconsumerd", lkm.fd);
392
393 ret = lttcomm_send_fds_unix_sock(sock, &lkm, &lkm.fd, 1, sizeof(lkm));
394 if (ret < 0) {
395 perror("send kconsumerd fd");
396 goto error;
397 }
398 }
399 }
400
7a485870 401 DBG("Kconsumerd channel fds sent");
f3ed775e
DG
402
403 return 0;
404
405error:
406 return ret;
407}
408
409/*
d063d709 410 * Send all stream fds of the kernel session to the consumer.
f3ed775e 411 */
d9800920 412static int send_kconsumerd_fds(struct ltt_kernel_session *session)
f3ed775e
DG
413{
414 int ret;
415 struct ltt_kernel_channel *chan;
7a485870
DG
416 struct lttcomm_kconsumerd_header lkh;
417 struct lttcomm_kconsumerd_msg lkm;
418
419 /* Setup header */
420 lkh.payload_size = sizeof(struct lttcomm_kconsumerd_msg);
421 lkh.cmd_type = ADD_STREAM;
422
423 DBG("Sending kconsumerd header for metadata");
424
d9800920 425 ret = lttcomm_send_unix_sock(session->consumer_fd, &lkh, sizeof(struct lttcomm_kconsumerd_header));
7a485870
DG
426 if (ret < 0) {
427 perror("send kconsumerd header");
428 goto error;
429 }
430
431 DBG("Sending metadata stream fd");
432
d9800920
DG
433 /* Extra protection. It's NOT suppose to be set to 0 at this point */
434 if (session->consumer_fd == 0) {
435 session->consumer_fd = kconsumerd_cmd_sock;
436 }
437
7a485870
DG
438 if (session->metadata_stream_fd != 0) {
439 /* Send metadata stream fd first */
440 lkm.fd = session->metadata_stream_fd;
441 lkm.state = ACTIVE_FD;
442 lkm.max_sb_size = session->metadata->conf->attr.subbuf_size;
8b270bdb 443 lkm.output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
7a485870 444 strncpy(lkm.path_name, session->metadata->pathname, PATH_MAX);
99497cd0 445 lkm.path_name[PATH_MAX - 1] = '\0';
7a485870 446
d9800920 447 ret = lttcomm_send_fds_unix_sock(session->consumer_fd, &lkm, &lkm.fd, 1, sizeof(lkm));
7a485870
DG
448 if (ret < 0) {
449 perror("send kconsumerd fd");
450 goto error;
451 }
452 }
f3ed775e 453
f3ed775e 454 cds_list_for_each_entry(chan, &session->channel_list.head, list) {
d9800920 455 ret = send_kconsumerd_channel_fds(session->consumer_fd, chan);
f3ed775e 456 if (ret < 0) {
7a485870 457 goto error;
f3ed775e
DG
458 }
459 }
460
7a485870
DG
461 DBG("Kconsumerd fds (metadata and channel streams) sent");
462
f3ed775e
DG
463 return 0;
464
465error:
466 return ret;
467}
468
fac6795d 469/*
0fdd1e2c 470 * Notify UST applications using the shm mmap futex.
fac6795d 471 */
0fdd1e2c 472static int notify_ust_apps(int active)
fac6795d 473{
0fdd1e2c 474 char *wait_shm_mmap;
fac6795d 475
0fdd1e2c 476 DBG("Notifying applications of session daemon state: %d", active);
fac6795d 477
62d53818
DG
478 tracepoint(ust_notify_apps_start);
479
0fdd1e2c
DG
480 /* See shm.c for this call implying mmap, shm and futex calls */
481 wait_shm_mmap = shm_ust_get_mmap(wait_shm_path, is_root);
482 if (wait_shm_mmap == NULL) {
fac6795d
DG
483 goto error;
484 }
485
0fdd1e2c
DG
486 /* Wake waiting process */
487 futex_wait_update((int32_t *) wait_shm_mmap, active);
488
62d53818
DG
489 tracepoint(ust_notify_apps_stop);
490
0fdd1e2c
DG
491 /* Apps notified successfully */
492 return 0;
fac6795d
DG
493
494error:
0fdd1e2c 495 return -1;
fac6795d
DG
496}
497
e065084a 498/*
d063d709
DG
499 * Setup the outgoing data buffer for the response (llm) by allocating the
500 * right amount of memory and copying the original information from the lsm
501 * structure.
ca95a216 502 *
d063d709 503 * Return total size of the buffer pointed by buf.
ca95a216 504 */
5461b305 505static int setup_lttng_msg(struct command_ctx *cmd_ctx, size_t size)
ca95a216 506{
f3ed775e 507 int ret, buf_size;
ca95a216 508
f3ed775e 509 buf_size = size;
5461b305
DG
510
511 cmd_ctx->llm = malloc(sizeof(struct lttcomm_lttng_msg) + buf_size);
512 if (cmd_ctx->llm == NULL) {
ca95a216 513 perror("malloc");
5461b305 514 ret = -ENOMEM;
ca95a216
DG
515 goto error;
516 }
517
5461b305
DG
518 /* Copy common data */
519 cmd_ctx->llm->cmd_type = cmd_ctx->lsm->cmd_type;
9f19cc17 520 cmd_ctx->llm->pid = cmd_ctx->lsm->domain.attr.pid;
5461b305 521
5461b305
DG
522 cmd_ctx->llm->data_size = size;
523 cmd_ctx->lttng_msg_size = sizeof(struct lttcomm_lttng_msg) + buf_size;
524
ca95a216
DG
525 return buf_size;
526
527error:
528 return ret;
529}
530
7a485870 531/*
d063d709
DG
532 * Update the kernel pollfd set of all channel fd available over all tracing
533 * session. Add the wakeup pipe at the end of the set.
7a485870
DG
534 */
535static int update_kernel_pollfd(void)
536{
537 int i = 0;
273ea72c
DG
538 /*
539 * The wakup pipe and the quit pipe are needed so the number of fds starts
540 * at 2 for those pipes.
541 */
542 unsigned int nb_fd = 2;
7a485870
DG
543 struct ltt_session *session;
544 struct ltt_kernel_channel *channel;
545
546 DBG("Updating kernel_pollfd");
547
548 /* Get the number of channel of all kernel session */
6c9cc2ab 549 lock_session_list();
b5541356
DG
550 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
551 lock_session(session);
7a485870 552 if (session->kernel_session == NULL) {
b5541356 553 unlock_session(session);
7a485870
DG
554 continue;
555 }
556 nb_fd += session->kernel_session->channel_count;
b5541356 557 unlock_session(session);
7a485870
DG
558 }
559
560 DBG("Resizing kernel_pollfd to size %d", nb_fd);
561
562 kernel_pollfd = realloc(kernel_pollfd, nb_fd * sizeof(struct pollfd));
563 if (kernel_pollfd == NULL) {
564 perror("malloc kernel_pollfd");
565 goto error;
566 }
567
b5541356
DG
568 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
569 lock_session(session);
7a485870 570 if (session->kernel_session == NULL) {
b5541356 571 unlock_session(session);
7a485870
DG
572 continue;
573 }
574 if (i >= nb_fd) {
575 ERR("To much channel for kernel_pollfd size");
b5541356 576 unlock_session(session);
7a485870
DG
577 break;
578 }
579 cds_list_for_each_entry(channel, &session->kernel_session->channel_list.head, list) {
580 kernel_pollfd[i].fd = channel->fd;
581 kernel_pollfd[i].events = POLLIN | POLLRDNORM;
582 i++;
583 }
b5541356 584 unlock_session(session);
7a485870 585 }
6c9cc2ab 586 unlock_session_list();
7a485870
DG
587
588 /* Adding wake up pipe */
273ea72c
DG
589 kernel_pollfd[nb_fd - 2].fd = kernel_poll_pipe[0];
590 kernel_pollfd[nb_fd - 2].events = POLLIN;
591
592 /* Adding the quit pipe */
593 kernel_pollfd[nb_fd - 1].fd = thread_quit_pipe[0];
7a485870
DG
594
595 return nb_fd;
596
597error:
6c9cc2ab 598 unlock_session_list();
7a485870
DG
599 return -1;
600}
601
602/*
d063d709
DG
603 * Find the channel fd from 'fd' over all tracing session. When found, check
604 * for new channel stream and send those stream fds to the kernel consumer.
7a485870 605 *
d063d709 606 * Useful for CPU hotplug feature.
7a485870
DG
607 */
608static int update_kernel_stream(int fd)
609{
610 int ret = 0;
611 struct ltt_session *session;
612 struct ltt_kernel_channel *channel;
613
614 DBG("Updating kernel streams for channel fd %d", fd);
615
6c9cc2ab 616 lock_session_list();
b5541356
DG
617 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
618 lock_session(session);
7a485870 619 if (session->kernel_session == NULL) {
b5541356 620 unlock_session(session);
7a485870
DG
621 continue;
622 }
d9800920
DG
623
624 /* This is not suppose to be 0 but this is an extra security check */
625 if (session->kernel_session->consumer_fd == 0) {
626 session->kernel_session->consumer_fd = kconsumerd_cmd_sock;
627 }
628
7a485870
DG
629 cds_list_for_each_entry(channel, &session->kernel_session->channel_list.head, list) {
630 if (channel->fd == fd) {
631 DBG("Channel found, updating kernel streams");
632 ret = kernel_open_channel_stream(channel);
633 if (ret < 0) {
634 goto end;
635 }
d9800920 636
7a485870
DG
637 /*
638 * Have we already sent fds to the consumer? If yes, it means that
639 * tracing is started so it is safe to send our updated stream fds.
640 */
641 if (session->kernel_session->kconsumer_fds_sent == 1) {
d9800920
DG
642 ret = send_kconsumerd_channel_fds(session->kernel_session->consumer_fd,
643 channel);
7a485870
DG
644 if (ret < 0) {
645 goto end;
646 }
647 }
648 goto end;
649 }
650 }
b5541356 651 unlock_session(session);
7a485870
DG
652 }
653
654end:
6c9cc2ab 655 unlock_session_list();
b5541356
DG
656 if (session) {
657 unlock_session(session);
658 }
7a485870
DG
659 return ret;
660}
661
662/*
d063d709 663 * This thread manage event coming from the kernel.
7a485870 664 *
d063d709
DG
665 * Features supported in this thread:
666 * -) CPU Hotplug
7a485870
DG
667 */
668static void *thread_manage_kernel(void *data)
669{
670 int ret, i, nb_fd = 0;
671 char tmp;
672 int update_poll_flag = 1;
673
b25a8868
DG
674 tracepoint(sessiond_th_kern_start);
675
7a485870
DG
676 DBG("Thread manage kernel started");
677
678 while (1) {
679 if (update_poll_flag == 1) {
680 nb_fd = update_kernel_pollfd();
681 if (nb_fd < 0) {
682 goto error;
683 }
684 update_poll_flag = 0;
685 }
686
687 DBG("Polling on %d fds", nb_fd);
688
b25a8868
DG
689 tracepoint(sessiond_th_kern_poll);
690
7a485870
DG
691 /* Poll infinite value of time */
692 ret = poll(kernel_pollfd, nb_fd, -1);
693 if (ret < 0) {
694 perror("poll kernel thread");
695 goto error;
696 } else if (ret == 0) {
697 /* Should not happen since timeout is infinite */
698 continue;
699 }
700
273ea72c
DG
701 /* Thread quit pipe has been closed. Killing thread. */
702 if (kernel_pollfd[nb_fd - 1].revents == POLLNVAL) {
703 goto error;
704 }
705
7a485870
DG
706 DBG("Kernel poll event triggered");
707
708 /*
709 * Check if the wake up pipe was triggered. If so, the kernel_pollfd
710 * must be updated.
711 */
273ea72c 712 switch (kernel_pollfd[nb_fd - 2].revents) {
b5541356 713 case POLLIN:
7a485870
DG
714 ret = read(kernel_poll_pipe[0], &tmp, 1);
715 update_poll_flag = 1;
716 continue;
b5541356
DG
717 case POLLERR:
718 goto error;
719 default:
720 break;
7a485870
DG
721 }
722
723 for (i = 0; i < nb_fd; i++) {
724 switch (kernel_pollfd[i].revents) {
725 /*
726 * New CPU detected by the kernel. Adding kernel stream to kernel
727 * session and updating the kernel consumer
728 */
729 case POLLIN | POLLRDNORM:
730 ret = update_kernel_stream(kernel_pollfd[i].fd);
731 if (ret < 0) {
732 continue;
733 }
734 break;
735 }
736 }
737 }
738
739error:
740 DBG("Kernel thread dying");
741 if (kernel_pollfd) {
742 free(kernel_pollfd);
743 }
273ea72c
DG
744
745 close(kernel_poll_pipe[0]);
746 close(kernel_poll_pipe[1]);
7a485870
DG
747 return NULL;
748}
749
1d4b027a 750/*
d063d709 751 * This thread manage the kconsumerd error sent back to the session daemon.
1d4b027a
DG
752 */
753static void *thread_manage_kconsumerd(void *data)
754{
273ea72c 755 int sock = 0, ret;
1d4b027a 756 enum lttcomm_return_code code;
273ea72c 757 struct pollfd pollfd[2];
1d4b027a 758
b25a8868
DG
759 tracepoint(sessiond_th_kcon_start);
760
1d4b027a
DG
761 DBG("[thread] Manage kconsumerd started");
762
763 ret = lttcomm_listen_unix_sock(kconsumerd_err_sock);
764 if (ret < 0) {
765 goto error;
766 }
767
273ea72c
DG
768 /* First fd is always the quit pipe */
769 pollfd[0].fd = thread_quit_pipe[0];
770
771 /* Apps socket */
772 pollfd[1].fd = kconsumerd_err_sock;
773 pollfd[1].events = POLLIN;
774
b25a8868
DG
775 tracepoint(sessiond_th_kcon_poll);
776
273ea72c
DG
777 /* Inifinite blocking call, waiting for transmission */
778 ret = poll(pollfd, 2, -1);
779 if (ret < 0) {
780 perror("poll kconsumerd thread");
781 goto error;
782 }
783
784 /* Thread quit pipe has been closed. Killing thread. */
785 if (pollfd[0].revents == POLLNVAL) {
786 goto error;
787 } else if (pollfd[1].revents == POLLERR) {
788 ERR("Kconsumerd err socket poll error");
789 goto error;
790 }
791
1d4b027a
DG
792 sock = lttcomm_accept_unix_sock(kconsumerd_err_sock);
793 if (sock < 0) {
794 goto error;
795 }
796
712ea556 797 /* Getting status code from kconsumerd */
1d4b027a
DG
798 ret = lttcomm_recv_unix_sock(sock, &code, sizeof(enum lttcomm_return_code));
799 if (ret <= 0) {
800 goto error;
801 }
802
803 if (code == KCONSUMERD_COMMAND_SOCK_READY) {
804 kconsumerd_cmd_sock = lttcomm_connect_unix_sock(kconsumerd_cmd_unix_sock_path);
805 if (kconsumerd_cmd_sock < 0) {
712ea556 806 sem_post(&kconsumerd_sem);
1d4b027a
DG
807 perror("kconsumerd connect");
808 goto error;
809 }
810 /* Signal condition to tell that the kconsumerd is ready */
811 sem_post(&kconsumerd_sem);
812 DBG("Kconsumerd command socket ready");
813 } else {
6f61d021 814 DBG("Kconsumerd error when waiting for SOCK_READY : %s",
1d4b027a
DG
815 lttcomm_get_readable_code(-code));
816 goto error;
817 }
818
72079cae
DG
819 /* Kconsumerd err socket */
820 pollfd[1].fd = sock;
821 pollfd[1].events = POLLIN;
822
823 /* Inifinite blocking call, waiting for transmission */
824 ret = poll(pollfd, 2, -1);
825 if (ret < 0) {
826 perror("poll kconsumerd thread");
827 goto error;
828 }
829
830 /* Thread quit pipe has been closed. Killing thread. */
831 if (pollfd[0].revents == POLLNVAL) {
832 goto error;
833 } else if (pollfd[1].revents == POLLERR) {
834 ERR("Kconsumerd err socket second poll error");
835 goto error;
836 }
837
712ea556
DG
838 /* Wait for any kconsumerd error */
839 ret = lttcomm_recv_unix_sock(sock, &code, sizeof(enum lttcomm_return_code));
840 if (ret <= 0) {
841 ERR("Kconsumerd closed the command socket");
842 goto error;
6f61d021 843 }
1d4b027a 844
712ea556
DG
845 ERR("Kconsumerd return code : %s", lttcomm_get_readable_code(-code));
846
1d4b027a 847error:
6f61d021 848 DBG("Kconsumerd thread dying");
273ea72c
DG
849 if (kconsumerd_err_sock) {
850 close(kconsumerd_err_sock);
851 }
852 if (kconsumerd_cmd_sock) {
853 close(kconsumerd_cmd_sock);
854 }
855 if (sock) {
856 close(sock);
857 }
858
859 unlink(kconsumerd_err_unix_sock_path);
860 unlink(kconsumerd_cmd_unix_sock_path);
861
862 kconsumerd_pid = 0;
1d4b027a
DG
863 return NULL;
864}
865
866/*
099e26bd
DG
867 * Reallocate the apps command pollfd structure of nb_fd size.
868 *
869 * The first two fds must be there at all time.
870 */
0177d773
DG
871static int update_apps_cmd_pollfd(unsigned int nb_fd, unsigned int old_nb_fd,
872 struct pollfd **pollfd)
099e26bd 873{
0177d773
DG
874 int i, count;
875 struct pollfd *old_pollfd = NULL;
876
099e26bd
DG
877 /* Can't accept pollfd less than 2 */
878 if (nb_fd < 2) {
879 goto end;
880 }
881
0177d773
DG
882 if (*pollfd) {
883 /* Save pointer */
884 old_pollfd = *pollfd;
885 }
886
887 *pollfd = malloc(nb_fd * sizeof(struct pollfd));
099e26bd 888 if (*pollfd == NULL) {
0177d773 889 perror("malloc manage apps pollfd");
099e26bd
DG
890 goto error;
891 }
892
893 /* First fd is always the quit pipe */
894 (*pollfd)[0].fd = thread_quit_pipe[0];
895 /* Apps command pipe */
896 (*pollfd)[1].fd = apps_cmd_pipe[0];
897 (*pollfd)[1].events = POLLIN;
898
0177d773
DG
899 /* Start count after the two pipes below */
900 count = 2;
901 for (i = 2; i < old_nb_fd; i++) {
902 /* Add to new pollfd */
903 if (old_pollfd[i].fd != -1) {
904 (*pollfd)[count].fd = old_pollfd[i].fd;
905 (*pollfd)[count].events = POLLHUP | POLLNVAL | POLLERR;
906 count++;
907 }
908
909 if (count > nb_fd) {
910 ERR("Updating poll fd wrong size");
911 goto error;
912 }
913 }
914
915 /* Destroy old pollfd */
916 free(old_pollfd);
917
099e26bd
DG
918 DBG("Apps cmd pollfd realloc of size %d", nb_fd);
919
920end:
921 return 0;
922
923error:
0177d773
DG
924 /* Destroy old pollfd */
925 free(old_pollfd);
099e26bd
DG
926 return -1;
927}
928
099e26bd
DG
929/*
930 * This thread manage application communication.
1d4b027a
DG
931 */
932static void *thread_manage_apps(void *data)
099e26bd 933{
28ba3f4e 934 int i, ret, current_nb_fd;
099e26bd
DG
935 unsigned int nb_fd = 2;
936 int update_poll_flag = 1;
937 struct pollfd *pollfd = NULL;
938 struct ust_command ust_cmd;
939
85c434ee
DG
940 tracepoint(sessiond_th_apps_start);
941
099e26bd
DG
942 DBG("[thread] Manage application started");
943
944 ust_cmd.sock = -1;
28ba3f4e 945 current_nb_fd = nb_fd;
099e26bd
DG
946
947 while (1) {
948 /* See if we have a valid socket to add to pollfd */
949 if (ust_cmd.sock != -1) {
950 nb_fd++;
951 update_poll_flag = 1;
952 }
953
954 /* The pollfd struct must be updated */
955 if (update_poll_flag) {
28ba3f4e 956 ret = update_apps_cmd_pollfd(nb_fd, current_nb_fd, &pollfd);
099e26bd
DG
957 if (ret < 0) {
958 /* malloc failed so we quit */
959 goto error;
960 }
0177d773 961
099e26bd
DG
962 if (ust_cmd.sock != -1) {
963 /* Update pollfd with the new UST socket */
964 DBG("Adding sock %d to apps cmd pollfd", ust_cmd.sock);
965 pollfd[nb_fd - 1].fd = ust_cmd.sock;
0177d773 966 pollfd[nb_fd - 1].events = POLLHUP | POLLNVAL | POLLERR;
099e26bd
DG
967 ust_cmd.sock = -1;
968 }
969 }
970
971 DBG("Apps thread polling on %d fds", nb_fd);
972
85c434ee
DG
973 tracepoint(sessiond_th_apps_poll);
974
099e26bd
DG
975 /* Inifinite blocking call, waiting for transmission */
976 ret = poll(pollfd, nb_fd, -1);
977 if (ret < 0) {
978 perror("poll apps thread");
979 goto error;
980 }
981
982 /* Thread quit pipe has been closed. Killing thread. */
983 if (pollfd[0].revents == POLLNVAL) {
984 goto error;
0177d773
DG
985 } else {
986 /* apps_cmd_pipe pipe events */
987 switch (pollfd[1].revents) {
988 case POLLERR:
989 ERR("Apps command pipe poll error");
099e26bd 990 goto error;
0177d773 991 case POLLIN:
85c434ee 992 tracepoint(ust_register_read_start);
62d53818 993
0177d773
DG
994 /* Empty pipe */
995 ret = read(apps_cmd_pipe[0], &ust_cmd, sizeof(ust_cmd));
996 if (ret < 0 || ret < sizeof(ust_cmd)) {
997 perror("read apps cmd pipe");
998 goto error;
999 }
099e26bd 1000
85c434ee
DG
1001 tracepoint(ust_register_read_stop);
1002
1003 tracepoint(ust_register_add_start);
0177d773
DG
1004 /* Register applicaton to the session daemon */
1005 ret = register_traceable_app(&ust_cmd.reg_msg, ust_cmd.sock);
1006 if (ret < 0) {
1007 /* Only critical ENOMEM error can be returned here */
1008 goto error;
1009 }
85c434ee 1010 tracepoint(ust_register_add_stop);
099e26bd 1011
85c434ee 1012 tracepoint(ust_register_done_start);
0177d773
DG
1013 ret = ustctl_register_done(ust_cmd.sock);
1014 if (ret < 0) {
1015 /*
1016 * If the registration is not possible, we simply unregister
1017 * the apps and continue
1018 */
1019 unregister_traceable_app(ust_cmd.sock);
1020 }
62d53818 1021
85c434ee 1022 tracepoint(ust_register_done_stop);
0177d773 1023 break;
099e26bd
DG
1024 }
1025 }
1026
28ba3f4e
DG
1027 current_nb_fd = nb_fd;
1028 for (i = 2; i < current_nb_fd; i++) {
099e26bd
DG
1029 /* Apps socket is closed/hungup */
1030 switch (pollfd[i].revents) {
0177d773 1031 case POLLERR:
099e26bd 1032 case POLLHUP:
0177d773 1033 case POLLNVAL:
099e26bd
DG
1034 /* Pipe closed */
1035 unregister_traceable_app(pollfd[i].fd);
0177d773
DG
1036 /* Indicate to remove this fd from the pollfd */
1037 pollfd[i].fd = -1;
099e26bd 1038 nb_fd--;
0177d773 1039 break;
099e26bd
DG
1040 }
1041 }
1042
28ba3f4e 1043 if (nb_fd != current_nb_fd) {
099e26bd
DG
1044 update_poll_flag = 1;
1045 }
1046 }
1047
1048error:
1049 DBG("Application communication apps dying");
1050 close(apps_cmd_pipe[0]);
1051 close(apps_cmd_pipe[1]);
1052
1053 free(pollfd);
1054
1055 return NULL;
1056}
1057
1058/*
1059 * Dispatch request from the registration threads to the application
1060 * communication thread.
1061 */
1062static void *thread_dispatch_ust_registration(void *data)
1063{
1064 int ret;
1065 struct cds_wfq_node *node;
1066 struct ust_command *ust_cmd = NULL;
1067
85c434ee
DG
1068 tracepoint(sessiond_th_dispatch_start);
1069
099e26bd
DG
1070 DBG("[thread] Dispatch UST command started");
1071
1072 while (!dispatch_thread_exit) {
1073 /* Atomically prepare the queue futex */
1074 futex_nto1_prepare(&ust_cmd_queue.futex);
1075
1076 do {
85c434ee
DG
1077 tracepoint(sessiond_th_dispatch_block);
1078
099e26bd
DG
1079 /* Dequeue command for registration */
1080 node = cds_wfq_dequeue_blocking(&ust_cmd_queue.queue);
1081 if (node == NULL) {
1082 DBG("Waked up but nothing in the UST command queue");
1083 /* Continue thread execution */
1084 break;
1085 }
1086
62d53818
DG
1087 tracepoint(ust_dispatch_register_start);
1088
099e26bd
DG
1089 ust_cmd = caa_container_of(node, struct ust_command, node);
1090
2f50c8a3
DG
1091 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1092 " gid:%d sock:%d name:%s (version %d.%d)",
1093 ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid,
1094 ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid,
1095 ust_cmd->sock, ust_cmd->reg_msg.name,
1096 ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor);
099e26bd
DG
1097 /*
1098 * Inform apps thread of the new application registration. This
1099 * call is blocking so we can be assured that the data will be read
1100 * at some point in time or wait to the end of the world :)
1101 */
1102 ret = write(apps_cmd_pipe[1], ust_cmd,
1103 sizeof(struct ust_command));
1104 if (ret < 0) {
1105 perror("write apps cmd pipe");
1106 if (errno == EBADF) {
1107 /*
1108 * We can't inform the application thread to process
1109 * registration. We will exit or else application
1110 * registration will not occur and tracing will never
1111 * start.
1112 */
1113 goto error;
1114 }
1115 }
1116 free(ust_cmd);
1117 } while (node != NULL);
1118
62d53818
DG
1119 tracepoint(ust_dispatch_register_stop);
1120
099e26bd
DG
1121 /* Futex wait on queue. Blocking call on futex() */
1122 futex_nto1_wait(&ust_cmd_queue.futex);
1123 }
1124
1125error:
1126 DBG("Dispatch thread dying");
1127 return NULL;
1128}
1129
1130/*
1131 * This thread manage application registration.
1132 */
1133static void *thread_registration_apps(void *data)
1d4b027a 1134{
273ea72c
DG
1135 int sock = 0, ret;
1136 struct pollfd pollfd[2];
099e26bd
DG
1137 /*
1138 * Get allocated in this thread, enqueued to a global queue, dequeued and
1139 * freed in the manage apps thread.
1140 */
1141 struct ust_command *ust_cmd = NULL;
1d4b027a 1142
62d53818 1143 tracepoint(sessiond_th_reg_start);
b25a8868 1144
099e26bd 1145 DBG("[thread] Manage application registration started");
1d4b027a
DG
1146
1147 ret = lttcomm_listen_unix_sock(apps_sock);
1148 if (ret < 0) {
1149 goto error;
1150 }
1151
273ea72c
DG
1152 /* First fd is always the quit pipe */
1153 pollfd[0].fd = thread_quit_pipe[0];
1154
1155 /* Apps socket */
1156 pollfd[1].fd = apps_sock;
1157 pollfd[1].events = POLLIN;
1158
1d4b027a 1159 /* Notify all applications to register */
0fdd1e2c
DG
1160 ret = notify_ust_apps(1);
1161 if (ret < 0) {
1162 ERR("Failed to notify applications or create the wait shared memory.\n"
1163 "Execution continues but there might be problem for already running\n"
1164 "applications that wishes to register.");
1165 }
1d4b027a
DG
1166
1167 while (1) {
1168 DBG("Accepting application registration");
273ea72c 1169
62d53818 1170 tracepoint(sessiond_th_reg_poll);
b25a8868 1171
273ea72c
DG
1172 /* Inifinite blocking call, waiting for transmission */
1173 ret = poll(pollfd, 2, -1);
1174 if (ret < 0) {
099e26bd 1175 perror("poll register apps thread");
273ea72c
DG
1176 goto error;
1177 }
1178
1179 /* Thread quit pipe has been closed. Killing thread. */
1180 if (pollfd[0].revents == POLLNVAL) {
1181 goto error;
1182 } else if (pollfd[1].revents == POLLERR) {
099e26bd 1183 ERR("Register apps socket poll error");
273ea72c
DG
1184 goto error;
1185 }
1186
62d53818
DG
1187 /* Registration starts here. Recording cycles */
1188 tracepoint(ust_register_start);
1189
1d4b027a
DG
1190 sock = lttcomm_accept_unix_sock(apps_sock);
1191 if (sock < 0) {
1192 goto error;
1193 }
1194
099e26bd
DG
1195 /* Create UST registration command for enqueuing */
1196 ust_cmd = malloc(sizeof(struct ust_command));
1197 if (ust_cmd == NULL) {
1198 perror("ust command malloc");
1199 goto error;
1200 }
1201
273ea72c 1202 /*
099e26bd
DG
1203 * Using message-based transmissions to ensure we don't have to deal
1204 * with partially received messages.
1d4b027a 1205 */
099e26bd
DG
1206 ret = lttcomm_recv_unix_sock(sock, &ust_cmd->reg_msg,
1207 sizeof(struct ust_register_msg));
2f50c8a3
DG
1208 if (ret < 0 || ret < sizeof(struct ust_register_msg)) {
1209 if (ret < 0) {
1210 perror("lttcomm_recv_unix_sock register apps");
1211 } else {
1212 ERR("Wrong size received on apps register");
1213 }
099e26bd
DG
1214 free(ust_cmd);
1215 close(sock);
1d4b027a
DG
1216 continue;
1217 }
1218
099e26bd
DG
1219 ust_cmd->sock = sock;
1220
2f50c8a3
DG
1221 DBG("UST registration received with pid:%d ppid:%d uid:%d"
1222 " gid:%d sock:%d name:%s (version %d.%d)",
1223 ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid,
1224 ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid,
1225 ust_cmd->sock, ust_cmd->reg_msg.name,
1226 ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor);
099e26bd
DG
1227 /*
1228 * Lock free enqueue the registration request.
1229 * The red pill has been taken! This apps will be part of the *system*
1230 */
1231 cds_wfq_enqueue(&ust_cmd_queue.queue, &ust_cmd->node);
1232
1233 /*
1234 * Wake the registration queue futex.
1235 * Implicit memory barrier with the exchange in cds_wfq_enqueue.
1236 */
1237 futex_nto1_wake(&ust_cmd_queue.futex);
62d53818
DG
1238
1239 tracepoint(ust_register_stop);
1d4b027a
DG
1240 }
1241
1242error:
0fdd1e2c
DG
1243 DBG("UST Registration thread dying");
1244
1245 /* Notify that the registration thread is gone */
1246 notify_ust_apps(0);
1247
1248 close(apps_sock);
1249 close(sock);
1d4b027a 1250
273ea72c 1251 unlink(apps_unix_sock_path);
0fdd1e2c 1252
1d4b027a
DG
1253 return NULL;
1254}
1255
8c0faa1d 1256/*
d063d709
DG
1257 * Start the thread_manage_kconsumerd. This must be done after a kconsumerd
1258 * exec or it will fails.
8c0faa1d 1259 */
693bd40b 1260static int spawn_kconsumerd_thread(void)
8c0faa1d
DG
1261{
1262 int ret;
1263
1264 /* Setup semaphore */
1265 sem_init(&kconsumerd_sem, 0, 0);
1266
1d4b027a 1267 ret = pthread_create(&kconsumerd_thread, NULL, thread_manage_kconsumerd, (void *) NULL);
8c0faa1d
DG
1268 if (ret != 0) {
1269 perror("pthread_create kconsumerd");
1270 goto error;
1271 }
1272
693bd40b 1273 /* Wait for the kconsumerd thread to be ready */
8c0faa1d
DG
1274 sem_wait(&kconsumerd_sem);
1275
712ea556
DG
1276 if (kconsumerd_pid == 0) {
1277 ERR("Kconsumerd did not start");
1278 goto error;
1279 }
1280
8c0faa1d
DG
1281 return 0;
1282
1283error:
712ea556 1284 ret = LTTCOMM_KERN_CONSUMER_FAIL;
8c0faa1d
DG
1285 return ret;
1286}
1287
d9800920
DG
1288/*
1289 * Join kernel consumer thread
1290 */
cf3af59e
MD
1291static int join_kconsumerd_thread(void)
1292{
1293 void *status;
1294 int ret;
1295
1296 if (kconsumerd_pid != 0) {
1297 ret = kill(kconsumerd_pid, SIGTERM);
1298 if (ret) {
1299 ERR("Error killing kconsumerd");
1300 return ret;
1301 }
1302 return pthread_join(kconsumerd_thread, &status);
1303 } else {
1304 return 0;
1305 }
1306}
1307
8c0faa1d 1308/*
d063d709 1309 * Fork and exec a kernel consumer daemon (kconsumerd).
8c0faa1d 1310 *
d063d709 1311 * Return pid if successful else -1.
8c0faa1d 1312 */
693bd40b 1313static pid_t spawn_kconsumerd(void)
8c0faa1d
DG
1314{
1315 int ret;
1316 pid_t pid;
53086306 1317 const char *verbosity;
8c0faa1d 1318
c49dc785
DG
1319 DBG("Spawning kconsumerd");
1320
8c0faa1d
DG
1321 pid = fork();
1322 if (pid == 0) {
1323 /*
1324 * Exec kconsumerd.
1325 */
31f73cc9 1326 if (opt_verbose > 1 || opt_verbose_kconsumerd) {
53086306
DG
1327 verbosity = "--verbose";
1328 } else {
1329 verbosity = "--quiet";
1330 }
1331 execl(INSTALL_BIN_PATH "/ltt-kconsumerd", "ltt-kconsumerd", verbosity, NULL);
8c0faa1d
DG
1332 if (errno != 0) {
1333 perror("kernel start consumer exec");
1334 }
1335 exit(EXIT_FAILURE);
1336 } else if (pid > 0) {
1337 ret = pid;
1338 goto error;
1339 } else {
1340 perror("kernel start consumer fork");
1341 ret = -errno;
1342 goto error;
1343 }
1344
1345error:
1346 return ret;
1347}
1348
693bd40b 1349/*
d063d709 1350 * Spawn the kconsumerd daemon and session daemon thread.
693bd40b
DG
1351 */
1352static int start_kconsumerd(void)
1353{
1354 int ret;
1355
693bd40b 1356 pthread_mutex_lock(&kconsumerd_pid_mutex);
c49dc785 1357 if (kconsumerd_pid != 0) {
817153bb 1358 pthread_mutex_unlock(&kconsumerd_pid_mutex);
c49dc785
DG
1359 goto end;
1360 }
693bd40b 1361
c49dc785
DG
1362 ret = spawn_kconsumerd();
1363 if (ret < 0) {
1364 ERR("Spawning kconsumerd failed");
1365 ret = LTTCOMM_KERN_CONSUMER_FAIL;
1366 pthread_mutex_unlock(&kconsumerd_pid_mutex);
1367 goto error;
693bd40b 1368 }
c49dc785
DG
1369
1370 /* Setting up the global kconsumerd_pid */
1371 kconsumerd_pid = ret;
693bd40b
DG
1372 pthread_mutex_unlock(&kconsumerd_pid_mutex);
1373
6f61d021
DG
1374 DBG("Kconsumerd pid %d", ret);
1375
693bd40b 1376 DBG("Spawning kconsumerd thread");
693bd40b
DG
1377 ret = spawn_kconsumerd_thread();
1378 if (ret < 0) {
1379 ERR("Fatal error spawning kconsumerd thread");
693bd40b
DG
1380 goto error;
1381 }
1382
c49dc785 1383end:
693bd40b
DG
1384 return 0;
1385
1386error:
1387 return ret;
1388}
1389
b73401da 1390/*
d063d709 1391 * modprobe_kernel_modules
b73401da
DG
1392 */
1393static int modprobe_kernel_modules(void)
1394{
ab147185 1395 int ret = 0, i;
b73401da
DG
1396 char modprobe[256];
1397
ab147185
MD
1398 for (i = 0; i < ARRAY_SIZE(kernel_modules_list); i++) {
1399 ret = snprintf(modprobe, sizeof(modprobe),
1400 "/sbin/modprobe %s%s",
1401 kernel_modules_list[i].required ? "" : "--quiet ",
1402 kernel_modules_list[i].name);
b73401da
DG
1403 if (ret < 0) {
1404 perror("snprintf modprobe");
1405 goto error;
1406 }
ab147185 1407 modprobe[sizeof(modprobe) - 1] = '\0';
b73401da 1408 ret = system(modprobe);
ab147185
MD
1409 if (ret == -1) {
1410 ERR("Unable to launch modprobe for module %s",
1411 kernel_modules_list[i].name);
1412 } else if (kernel_modules_list[i].required
d9800920 1413 && WEXITSTATUS(ret) != 0) {
ab147185
MD
1414 ERR("Unable to load module %s",
1415 kernel_modules_list[i].name);
1416 } else {
1417 DBG("Modprobe successfully %s",
1418 kernel_modules_list[i].name);
1419 }
1420 }
1421
1422error:
1423 return ret;
1424}
1425
b73401da 1426/*
d063d709 1427 * mount_debugfs
b73401da
DG
1428 */
1429static int mount_debugfs(char *path)
1430{
1431 int ret;
1432 char *type = "debugfs";
1433
996b65c8 1434 ret = mkdir_recursive(path, S_IRWXU | S_IRWXG, geteuid(), getegid());
b73401da
DG
1435 if (ret < 0) {
1436 goto error;
1437 }
1438
1439 ret = mount(type, path, type, 0, NULL);
1440 if (ret < 0) {
1441 perror("mount debugfs");
1442 goto error;
1443 }
1444
1445 DBG("Mounted debugfs successfully at %s", path);
1446
1447error:
1448 return ret;
1449}
1450
8c0faa1d 1451/*
d063d709 1452 * Setup necessary data for kernel tracer action.
8c0faa1d 1453 */
f3ed775e 1454static void init_kernel_tracer(void)
8c0faa1d 1455{
b73401da
DG
1456 int ret;
1457 char *proc_mounts = "/proc/mounts";
1458 char line[256];
1459 char *debugfs_path = NULL, *lttng_path;
1460 FILE *fp;
1461
1462 /* Detect debugfs */
1463 fp = fopen(proc_mounts, "r");
1464 if (fp == NULL) {
1465 ERR("Unable to probe %s", proc_mounts);
1466 goto error;
1467 }
1468
1469 while (fgets(line, sizeof(line), fp) != NULL) {
1470 if (strstr(line, "debugfs") != NULL) {
1471 /* Remove first string */
1472 strtok(line, " ");
1473 /* Dup string here so we can reuse line later on */
1474 debugfs_path = strdup(strtok(NULL, " "));
1475 DBG("Got debugfs path : %s", debugfs_path);
1476 break;
1477 }
1478 }
1479
1480 fclose(fp);
1481
1482 /* Mount debugfs if needded */
1483 if (debugfs_path == NULL) {
1484 ret = asprintf(&debugfs_path, "/mnt/debugfs");
1485 if (ret < 0) {
1486 perror("asprintf debugfs path");
1487 goto error;
1488 }
1489 ret = mount_debugfs(debugfs_path);
1490 if (ret < 0) {
1491 goto error;
1492 }
1493 }
1494
1495 /* Modprobe lttng kernel modules */
1496 ret = modprobe_kernel_modules();
1497 if (ret < 0) {
1498 goto error;
1499 }
1500
1501 /* Setup lttng kernel path */
1502 ret = asprintf(&lttng_path, "%s/lttng", debugfs_path);
1503 if (ret < 0) {
1504 perror("asprintf lttng path");
1505 goto error;
1506 }
1507
1508 /* Open debugfs lttng */
1509 kernel_tracer_fd = open(lttng_path, O_RDWR);
f3ed775e 1510 if (kernel_tracer_fd < 0) {
b73401da
DG
1511 DBG("Failed to open %s", lttng_path);
1512 goto error;
8c0faa1d
DG
1513 }
1514
b73401da
DG
1515 free(lttng_path);
1516 free(debugfs_path);
f3ed775e 1517 DBG("Kernel tracer fd %d", kernel_tracer_fd);
b73401da
DG
1518 return;
1519
1520error:
1521 if (lttng_path) {
1522 free(lttng_path);
1523 }
1524 if (debugfs_path) {
1525 free(debugfs_path);
1526 }
1527 WARN("No kernel tracer available");
1528 kernel_tracer_fd = 0;
1529 return;
f3ed775e 1530}
33a2b854 1531
f3ed775e 1532/*
d063d709
DG
1533 * Start tracing by creating trace directory and sending FDs to the kernel
1534 * consumer.
f3ed775e
DG
1535 */
1536static int start_kernel_trace(struct ltt_kernel_session *session)
1537{
f40799e8 1538 int ret = 0;
8c0faa1d 1539
f3ed775e 1540 if (session->kconsumer_fds_sent == 0) {
d9800920
DG
1541 /*
1542 * Assign default kernel consumer if no consumer assigned to the kernel
1543 * session. At this point, it's NOT suppose to be 0 but this is an extra
1544 * security check.
1545 */
1546 if (session->consumer_fd == 0) {
1547 session->consumer_fd = kconsumerd_cmd_sock;
1548 }
1549
1550 ret = send_kconsumerd_fds(session);
f3ed775e
DG
1551 if (ret < 0) {
1552 ERR("Send kconsumerd fds failed");
1553 ret = LTTCOMM_KERN_CONSUMER_FAIL;
1554 goto error;
1555 }
1d4b027a 1556
f3ed775e
DG
1557 session->kconsumer_fds_sent = 1;
1558 }
1d4b027a
DG
1559
1560error:
1561 return ret;
8c0faa1d
DG
1562}
1563
7a485870
DG
1564/*
1565 * Notify kernel thread to update it's pollfd.
1566 */
1567static int notify_kernel_pollfd(void)
1568{
1569 int ret;
1570
1571 /* Inform kernel thread of the new kernel channel */
1572 ret = write(kernel_poll_pipe[1], "!", 1);
1573 if (ret < 0) {
1574 perror("write kernel poll pipe");
1575 }
1576
1577 return ret;
1578}
1579
8c0faa1d 1580/*
d063d709 1581 * Allocate a channel structure and fill it.
8c0faa1d 1582 */
b389abbe
MD
1583static struct lttng_channel *init_default_channel(enum lttng_domain_type domain_type,
1584 char *name)
8c0faa1d 1585{
f3ed775e 1586 struct lttng_channel *chan;
1d4b027a 1587
f3ed775e
DG
1588 chan = malloc(sizeof(struct lttng_channel));
1589 if (chan == NULL) {
1590 perror("init channel malloc");
1591 goto error;
8c0faa1d 1592 }
1d4b027a 1593
ed8f384d 1594 if (snprintf(chan->name, NAME_MAX, "%s", name) < 0) {
9f19cc17 1595 perror("snprintf channel name");
b389abbe 1596 goto error;
f3ed775e
DG
1597 }
1598
1599 chan->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
f3ed775e
DG
1600 chan->attr.switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
1601 chan->attr.read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
1d4b027a 1602
b389abbe
MD
1603 switch (domain_type) {
1604 case LTTNG_DOMAIN_KERNEL:
1605 chan->attr.subbuf_size = DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE;
1606 chan->attr.num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM;
1607 chan->attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
1608 break;
1609 /* TODO: add UST */
1610 default:
1611 goto error; /* Not implemented */
1612 }
1613
f3ed775e 1614 return chan;
b389abbe
MD
1615
1616error:
1617 free(chan);
1618 return NULL;
8c0faa1d
DG
1619}
1620
0177d773
DG
1621/*
1622 * Create an UST session and add it to the session ust list.
1623 */
1624static int create_ust_session(pid_t pid, struct ltt_session *session)
1625{
1626 int ret = -1;
1627 struct ltt_ust_session *lus;
1628
1629 DBG("Creating UST session");
1630
1631 lus = trace_ust_create_session(session->path, pid);
1632 if (lus == NULL) {
1633 goto error;
1634 }
1635
1636 ret = mkdir_recursive(lus->path, S_IRWXU | S_IRWXG,
1637 geteuid(), allowed_group());
1638 if (ret < 0) {
1639 if (ret != -EEXIST) {
1640 ERR("Trace directory creation error");
1641 goto error;
1642 }
1643 }
1644
1645 /* Create session on the UST tracer */
1646 ret = ustctl_create_session(lus);
1647 if (ret < 0) {
1648 goto error;
1649 }
1650
1651 return 0;
1652
1653error:
1654 free(lus);
1655 return ret;
1656}
1657
333f285e 1658/*
d063d709 1659 * Create a kernel tracer session then create the default channel.
333f285e 1660 */
f3ed775e 1661static int create_kernel_session(struct ltt_session *session)
333f285e 1662{
f3ed775e 1663 int ret;
f3ed775e
DG
1664
1665 DBG("Creating kernel session");
1666
1667 ret = kernel_create_session(session, kernel_tracer_fd);
1668 if (ret < 0) {
1669 ret = LTTCOMM_KERN_SESS_FAIL;
1670 goto error;
333f285e
DG
1671 }
1672
d9800920
DG
1673 /* Set kernel consumer socket fd */
1674 if (kconsumerd_cmd_sock) {
1675 session->kernel_session->consumer_fd = kconsumerd_cmd_sock;
1676 }
1677
63053e7c
DG
1678 ret = mkdir_recursive(session->kernel_session->trace_path,
1679 S_IRWXU | S_IRWXG, geteuid(), allowed_group());
7a485870 1680 if (ret < 0) {
996b65c8 1681 if (ret != -EEXIST) {
7a485870
DG
1682 ERR("Trace directory creation error");
1683 goto error;
1684 }
1685 }
1686
f3ed775e
DG
1687error:
1688 return ret;
333f285e
DG
1689}
1690
6c9cc2ab
DG
1691/*
1692 * Using the session list, filled a lttng_session array to send back to the
1693 * client for session listing.
1694 *
1695 * The session list lock MUST be acquired before calling this function. Use
1696 * lock_session_list() and unlock_session_list().
1697 */
1698static void list_lttng_sessions(struct lttng_session *sessions)
1699{
1700 int i = 0;
1701 struct ltt_session *session;
1702
1703 DBG("Getting all available session");
1704 /*
1705 * Iterate over session list and append data after the control struct in
1706 * the buffer.
1707 */
1708 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
1709 strncpy(sessions[i].path, session->path, PATH_MAX);
99497cd0 1710 sessions[i].path[PATH_MAX - 1] = '\0';
6c9cc2ab 1711 strncpy(sessions[i].name, session->name, NAME_MAX);
99497cd0 1712 sessions[i].name[NAME_MAX - 1] = '\0';
6c9cc2ab
DG
1713 i++;
1714 }
1715}
1716
9f19cc17
DG
1717/*
1718 * Fill lttng_channel array of all channels.
1719 */
1720static void list_lttng_channels(struct ltt_session *session,
1721 struct lttng_channel *channels)
1722{
1723 int i = 0;
1724 struct ltt_kernel_channel *kchan;
1725
1726 DBG("Listing channels for session %s", session->name);
1727
1728 /* Kernel channels */
1729 if (session->kernel_session != NULL) {
1730 cds_list_for_each_entry(kchan, &session->kernel_session->channel_list.head, list) {
1731 /* Copy lttng_channel struct to array */
1732 memcpy(&channels[i], kchan->channel, sizeof(struct lttng_channel));
1733 channels[i].enabled = kchan->enabled;
1734 i++;
1735 }
1736 }
1737
1738 /* TODO: Missing UST listing */
1739}
1740
1741/*
1742 * Fill lttng_event array of all events in the channel.
1743 */
1744static void list_lttng_events(struct ltt_kernel_channel *kchan,
1745 struct lttng_event *events)
1746{
1747 /*
1748 * TODO: This is ONLY kernel. Need UST support.
1749 */
1750 int i = 0;
1751 struct ltt_kernel_event *event;
1752
1753 DBG("Listing events for channel %s", kchan->channel->name);
1754
1755 /* Kernel channels */
1756 cds_list_for_each_entry(event, &kchan->events_list.head , list) {
1757 strncpy(events[i].name, event->event->name, LTTNG_SYMBOL_NAME_LEN);
99497cd0 1758 events[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
9f19cc17
DG
1759 events[i].enabled = event->enabled;
1760 switch (event->event->instrumentation) {
1761 case LTTNG_KERNEL_TRACEPOINT:
1762 events[i].type = LTTNG_EVENT_TRACEPOINT;
1763 break;
1764 case LTTNG_KERNEL_KPROBE:
1765 case LTTNG_KERNEL_KRETPROBE:
1766 events[i].type = LTTNG_EVENT_PROBE;
1767 memcpy(&events[i].attr.probe, &event->event->u.kprobe,
1768 sizeof(struct lttng_kernel_kprobe));
1769 break;
1770 case LTTNG_KERNEL_FUNCTION:
1771 events[i].type = LTTNG_EVENT_FUNCTION;
1772 memcpy(&events[i].attr.ftrace, &event->event->u.ftrace,
1773 sizeof(struct lttng_kernel_function));
1774 break;
1775 }
1776 i++;
1777 }
1778}
1779
fac6795d 1780/*
d063d709
DG
1781 * Process the command requested by the lttng client within the command
1782 * context structure. This function make sure that the return structure (llm)
1783 * is set and ready for transmission before returning.
fac6795d 1784 *
e065084a 1785 * Return any error encountered or 0 for success.
fac6795d 1786 */
5461b305 1787static int process_client_msg(struct command_ctx *cmd_ctx)
fac6795d 1788{
f40799e8 1789 int ret = LTTCOMM_OK;
fac6795d 1790
5461b305 1791 DBG("Processing client command %d", cmd_ctx->lsm->cmd_type);
fac6795d 1792
63053e7c
DG
1793 /*
1794 * Commands that DO NOT need a session.
1795 */
5461b305 1796 switch (cmd_ctx->lsm->cmd_type) {
5e16da05
MD
1797 case LTTNG_CREATE_SESSION:
1798 case LTTNG_LIST_SESSIONS:
052da939 1799 case LTTNG_LIST_TRACEPOINTS:
d0254c7c 1800 case LTTNG_CALIBRATE:
5e16da05
MD
1801 break;
1802 default:
42abccdb
DG
1803 DBG("Getting session %s by name", cmd_ctx->lsm->session.name);
1804 cmd_ctx->session = find_session_by_name(cmd_ctx->lsm->session.name);
5461b305 1805 if (cmd_ctx->session == NULL) {
f3ed775e 1806 /* If session name not found */
42abccdb 1807 if (cmd_ctx->lsm->session.name != NULL) {
f3ed775e
DG
1808 ret = LTTCOMM_SESS_NOT_FOUND;
1809 } else { /* If no session name specified */
1810 ret = LTTCOMM_SELECT_SESS;
1811 }
5461b305 1812 goto error;
b5541356
DG
1813 } else {
1814 /* Acquire lock for the session */
1815 lock_session(cmd_ctx->session);
379473d2 1816 }
5e16da05 1817 break;
379473d2
DG
1818 }
1819
f3ed775e 1820 /*
d0254c7c 1821 * Check domain type for specific "pre-action".
f3ed775e 1822 */
0d0c377a
DG
1823 switch (cmd_ctx->lsm->domain.type) {
1824 case LTTNG_DOMAIN_KERNEL:
333f285e 1825 /* Kernel tracer check */
20fe2104 1826 if (kernel_tracer_fd == 0) {
333f285e
DG
1827 init_kernel_tracer();
1828 if (kernel_tracer_fd == 0) {
1829 ret = LTTCOMM_KERN_NA;
1830 goto error;
1831 }
20fe2104 1832 }
f3ed775e 1833 /* Need a session for kernel command */
d0254c7c 1834 switch (cmd_ctx->lsm->cmd_type) {
d9800920 1835 case LTTNG_CALIBRATE:
d0254c7c
MD
1836 case LTTNG_CREATE_SESSION:
1837 case LTTNG_LIST_SESSIONS:
1838 case LTTNG_LIST_TRACEPOINTS:
d0254c7c
MD
1839 break;
1840 default:
1841 if (cmd_ctx->session->kernel_session == NULL) {
1842 ret = create_kernel_session(cmd_ctx->session);
f3ed775e 1843 if (ret < 0) {
d0254c7c 1844 ret = LTTCOMM_KERN_SESS_FAIL;
f3ed775e
DG
1845 goto error;
1846 }
d0254c7c 1847 /* Start the kernel consumer daemon */
d9800920
DG
1848 if (kconsumerd_pid == 0 &&
1849 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
d0254c7c
MD
1850 ret = start_kconsumerd();
1851 if (ret < 0) {
1852 goto error;
1853 }
1854 }
f3ed775e
DG
1855 }
1856 }
0d0c377a 1857 break;
0fdd1e2c
DG
1858 case LTTNG_DOMAIN_UST_PID:
1859 break;
0d0c377a
DG
1860 default:
1861 break;
20fe2104
DG
1862 }
1863
fac6795d 1864 /* Process by command type */
5461b305 1865 switch (cmd_ctx->lsm->cmd_type) {
b579acd9 1866 case LTTNG_ADD_CONTEXT:
d65106b1 1867 {
b579acd9 1868 struct lttng_kernel_context kctx;
d65106b1
DG
1869
1870 /* Setup lttng message with no payload */
1871 ret = setup_lttng_msg(cmd_ctx, 0);
1872 if (ret < 0) {
1873 goto setup_error;
1874 }
1875
b579acd9
DG
1876 switch (cmd_ctx->lsm->domain.type) {
1877 case LTTNG_DOMAIN_KERNEL:
1878 /* Create Kernel context */
1879 kctx.ctx = cmd_ctx->lsm->u.context.ctx.ctx;
1880 kctx.u.perf_counter.type = cmd_ctx->lsm->u.context.ctx.u.perf_counter.type;
1881 kctx.u.perf_counter.config = cmd_ctx->lsm->u.context.ctx.u.perf_counter.config;
1882 strncpy(kctx.u.perf_counter.name,
1883 cmd_ctx->lsm->u.context.ctx.u.perf_counter.name,
1884 LTTNG_SYMBOL_NAME_LEN);
99497cd0 1885 kctx.u.perf_counter.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
b579acd9
DG
1886
1887 /* Add kernel context to kernel tracer. See context.c */
1888 ret = add_kernel_context(cmd_ctx->session->kernel_session, &kctx,
1889 cmd_ctx->lsm->u.context.event_name,
1890 cmd_ctx->lsm->u.context.channel_name);
1891 if (ret != LTTCOMM_OK) {
d65106b1
DG
1892 goto error;
1893 }
b579acd9
DG
1894 break;
1895 default:
1896 /* TODO: Userspace tracing */
1897 ret = LTTCOMM_NOT_IMPLEMENTED;
0b97ec54 1898 goto error;
d65106b1
DG
1899 }
1900
1901 ret = LTTCOMM_OK;
1902 break;
1903 }
0b97ec54 1904 case LTTNG_DISABLE_CHANNEL:
26cc6b4e 1905 {
0b97ec54 1906 struct ltt_kernel_channel *kchan;
26cc6b4e
DG
1907
1908 /* Setup lttng message with no payload */
1909 ret = setup_lttng_msg(cmd_ctx, 0);
1910 if (ret < 0) {
1911 goto setup_error;
1912 }
1913
0b97ec54
DG
1914 switch (cmd_ctx->lsm->domain.type) {
1915 case LTTNG_DOMAIN_KERNEL:
62499ad6 1916 kchan = trace_kernel_get_channel_by_name(cmd_ctx->lsm->u.disable.channel_name,
0b97ec54
DG
1917 cmd_ctx->session->kernel_session);
1918 if (kchan == NULL) {
1919 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
26cc6b4e 1920 goto error;
0b97ec54
DG
1921 } else if (kchan->enabled == 1) {
1922 ret = kernel_disable_channel(kchan);
1923 if (ret < 0) {
1924 if (ret != EEXIST) {
1925 ret = LTTCOMM_KERN_CHAN_DISABLE_FAIL;
1926 }
1927 goto error;
1928 }
26cc6b4e 1929 }
0b97ec54
DG
1930 kernel_wait_quiescent(kernel_tracer_fd);
1931 break;
1932 default:
1933 /* TODO: Userspace tracing */
1934 ret = LTTCOMM_NOT_IMPLEMENTED;
1935 goto error;
26cc6b4e
DG
1936 }
1937
26cc6b4e
DG
1938 ret = LTTCOMM_OK;
1939 break;
1940 }
f5177a38 1941 case LTTNG_DISABLE_EVENT:
e953ef25 1942 {
f5177a38
DG
1943 struct ltt_kernel_channel *kchan;
1944 struct ltt_kernel_event *kevent;
e953ef25
DG
1945
1946 /* Setup lttng message with no payload */
1947 ret = setup_lttng_msg(cmd_ctx, 0);
1948 if (ret < 0) {
1949 goto setup_error;
1950 }
1951
f5177a38
DG
1952 switch (cmd_ctx->lsm->domain.type) {
1953 case LTTNG_DOMAIN_KERNEL:
62499ad6 1954 kchan = trace_kernel_get_channel_by_name(cmd_ctx->lsm->u.disable.channel_name,
f5177a38
DG
1955 cmd_ctx->session->kernel_session);
1956 if (kchan == NULL) {
1957 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
19e70852 1958 goto error;
e953ef25 1959 }
f5177a38 1960
62499ad6 1961 kevent = trace_kernel_get_event_by_name(cmd_ctx->lsm->u.disable.name, kchan);
f5177a38
DG
1962 if (kevent != NULL) {
1963 DBG("Disabling kernel event %s for channel %s.", kevent->event->name,
1964 kchan->channel->name);
1965 ret = kernel_disable_event(kevent);
1966 if (ret < 0) {
1967 ret = LTTCOMM_KERN_ENABLE_FAIL;
1968 goto error;
1969 }
1970 }
1971
1972 kernel_wait_quiescent(kernel_tracer_fd);
1973 break;
1974 default:
1975 /* TODO: Userspace tracing */
1976 ret = LTTCOMM_NOT_IMPLEMENTED;
1977 goto error;
e953ef25
DG
1978 }
1979
19e70852 1980 ret = LTTCOMM_OK;
e953ef25
DG
1981 break;
1982 }
f5177a38 1983 case LTTNG_DISABLE_ALL_EVENT:
950131af 1984 {
f5177a38
DG
1985 struct ltt_kernel_channel *kchan;
1986 struct ltt_kernel_event *kevent;
950131af
DG
1987
1988 /* Setup lttng message with no payload */
1989 ret = setup_lttng_msg(cmd_ctx, 0);
1990 if (ret < 0) {
1991 goto setup_error;
1992 }
1993
f5177a38
DG
1994 switch (cmd_ctx->lsm->domain.type) {
1995 case LTTNG_DOMAIN_KERNEL:
1996 DBG("Disabling all enabled kernel events");
62499ad6 1997 kchan = trace_kernel_get_channel_by_name(cmd_ctx->lsm->u.disable.channel_name,
f5177a38
DG
1998 cmd_ctx->session->kernel_session);
1999 if (kchan == NULL) {
2000 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
2001 goto error;
2002 }
950131af 2003
f5177a38
DG
2004 /* For each event in the kernel session */
2005 cds_list_for_each_entry(kevent, &kchan->events_list.head, list) {
2006 DBG("Disabling kernel event %s for channel %s.",
2007 kevent->event->name, kchan->channel->name);
2008 ret = kernel_disable_event(kevent);
2009 if (ret < 0) {
2010 continue;
2011 }
950131af 2012 }
f5177a38
DG
2013
2014 /* Quiescent wait after event disable */
2015 kernel_wait_quiescent(kernel_tracer_fd);
2016 break;
2017 default:
2018 /* TODO: Userspace tracing */
2019 ret = LTTCOMM_NOT_IMPLEMENTED;
2020 goto error;
950131af
DG
2021 }
2022
950131af
DG
2023 ret = LTTCOMM_OK;
2024 break;
2025 }
0d0c377a 2026 case LTTNG_ENABLE_CHANNEL:
d36b8583 2027 {
0d0c377a 2028 struct ltt_kernel_channel *kchan;
d36b8583
DG
2029
2030 /* Setup lttng message with no payload */
2031 ret = setup_lttng_msg(cmd_ctx, 0);
2032 if (ret < 0) {
2033 goto setup_error;
2034 }
2035
0d0c377a
DG
2036 switch (cmd_ctx->lsm->domain.type) {
2037 case LTTNG_DOMAIN_KERNEL:
62499ad6 2038 kchan = trace_kernel_get_channel_by_name(cmd_ctx->lsm->u.enable.channel_name,
0d0c377a
DG
2039 cmd_ctx->session->kernel_session);
2040 if (kchan == NULL) {
2041 /* Channel not found, creating it */
2042 DBG("Creating kernel channel");
7d29a247 2043
0d0c377a 2044 ret = kernel_create_channel(cmd_ctx->session->kernel_session,
63053e7c
DG
2045 &cmd_ctx->lsm->u.channel.chan,
2046 cmd_ctx->session->kernel_session->trace_path);
0d0c377a
DG
2047 if (ret < 0) {
2048 ret = LTTCOMM_KERN_CHAN_FAIL;
2049 goto error;
2050 }
7d29a247 2051
0d0c377a
DG
2052 /* Notify kernel thread that there is a new channel */
2053 ret = notify_kernel_pollfd();
2054 if (ret < 0) {
2055 ret = LTTCOMM_FATAL;
2056 goto error;
2057 }
2058 } else if (kchan->enabled == 0) {
2059 ret = kernel_enable_channel(kchan);
2060 if (ret < 0) {
2061 if (ret != EEXIST) {
2062 ret = LTTCOMM_KERN_CHAN_ENABLE_FAIL;
2063 }
2064 goto error;
d36b8583 2065 }
d36b8583 2066 }
0d0c377a
DG
2067
2068 kernel_wait_quiescent(kernel_tracer_fd);
2069 break;
0177d773 2070 case LTTNG_DOMAIN_UST_PID:
0fdd1e2c 2071
0d0c377a
DG
2072 break;
2073 default:
0d0c377a
DG
2074 ret = LTTCOMM_NOT_IMPLEMENTED;
2075 goto error;
d36b8583
DG
2076 }
2077
d36b8583
DG
2078 ret = LTTCOMM_OK;
2079 break;
2080 }
0d0c377a 2081 case LTTNG_ENABLE_EVENT:
894be886 2082 {
7d29a247
DG
2083 char *channel_name;
2084 struct ltt_kernel_channel *kchan;
0d0c377a 2085 struct ltt_kernel_event *kevent;
7d29a247 2086 struct lttng_channel *chan;
f3ed775e 2087
894be886
DG
2088 /* Setup lttng message with no payload */
2089 ret = setup_lttng_msg(cmd_ctx, 0);
2090 if (ret < 0) {
2091 goto setup_error;
2092 }
2093
7d29a247
DG
2094 channel_name = cmd_ctx->lsm->u.enable.channel_name;
2095
0d0c377a
DG
2096 switch (cmd_ctx->lsm->domain.type) {
2097 case LTTNG_DOMAIN_KERNEL:
62499ad6 2098 kchan = trace_kernel_get_channel_by_name(channel_name,
b389abbe
MD
2099 cmd_ctx->session->kernel_session);
2100 if (kchan == NULL) {
2101 DBG("Channel not found. Creating channel %s", channel_name);
2102
2103 chan = init_default_channel(cmd_ctx->lsm->domain.type, channel_name);
2104 if (chan == NULL) {
2105 ret = LTTCOMM_FATAL;
2106 goto error;
2107 }
2108
2109 ret = kernel_create_channel(cmd_ctx->session->kernel_session,
2110 chan, cmd_ctx->session->kernel_session->trace_path);
2111 if (ret < 0) {
2112 ret = LTTCOMM_KERN_CHAN_FAIL;
2113 goto error;
2114 }
62499ad6 2115 kchan = trace_kernel_get_channel_by_name(channel_name,
0d0c377a
DG
2116 cmd_ctx->session->kernel_session);
2117 if (kchan == NULL) {
b389abbe
MD
2118 ERR("Channel %s not found after creation. Internal error, giving up.",
2119 channel_name);
2120 ret = LTTCOMM_FATAL;
2121 goto error;
7d29a247 2122 }
b389abbe 2123 }
7d29a247 2124
62499ad6 2125 kevent = trace_kernel_get_event_by_name(cmd_ctx->lsm->u.enable.event.name, kchan);
0d0c377a
DG
2126 if (kevent == NULL) {
2127 DBG("Creating kernel event %s for channel %s.",
2128 cmd_ctx->lsm->u.enable.event.name, channel_name);
2129 ret = kernel_create_event(&cmd_ctx->lsm->u.enable.event, kchan);
2130 } else {
2131 DBG("Enabling kernel event %s for channel %s.",
2132 kevent->event->name, channel_name);
2133 ret = kernel_enable_event(kevent);
2134 if (ret == -EEXIST) {
2135 ret = LTTCOMM_KERN_EVENT_EXIST;
7d29a247
DG
2136 goto error;
2137 }
2138 }
f34daff7 2139
0d0c377a
DG
2140 if (ret < 0) {
2141 ret = LTTCOMM_KERN_ENABLE_FAIL;
7d29a247
DG
2142 goto error;
2143 }
19e70852 2144
0d0c377a
DG
2145 kernel_wait_quiescent(kernel_tracer_fd);
2146 break;
2147 default:
2148 /* TODO: Userspace tracing */
2149 ret = LTTCOMM_NOT_IMPLEMENTED;
19e70852 2150 goto error;
f3ed775e 2151 }
19e70852 2152 ret = LTTCOMM_OK;
894be886
DG
2153 break;
2154 }
0d0c377a 2155 case LTTNG_ENABLE_ALL_EVENT:
33a2b854 2156 {
9f19cc17
DG
2157 int size, i;
2158 char *channel_name;
7d29a247 2159 struct ltt_kernel_channel *kchan;
0d0c377a 2160 struct ltt_kernel_event *kevent;
9f19cc17 2161 struct lttng_event *event_list;
7d29a247 2162 struct lttng_channel *chan;
33a2b854
DG
2163
2164 /* Setup lttng message with no payload */
2165 ret = setup_lttng_msg(cmd_ctx, 0);
2166 if (ret < 0) {
2167 goto setup_error;
2168 }
2169
2170 DBG("Enabling all kernel event");
2171
7d29a247
DG
2172 channel_name = cmd_ctx->lsm->u.enable.channel_name;
2173
0d0c377a
DG
2174 switch (cmd_ctx->lsm->domain.type) {
2175 case LTTNG_DOMAIN_KERNEL:
62499ad6 2176 kchan = trace_kernel_get_channel_by_name(channel_name,
b389abbe
MD
2177 cmd_ctx->session->kernel_session);
2178 if (kchan == NULL) {
2179 DBG("Channel not found. Creating channel %s", channel_name);
2180
2181 chan = init_default_channel(cmd_ctx->lsm->domain.type, channel_name);
2182 if (chan == NULL) {
2183 ret = LTTCOMM_FATAL;
2184 goto error;
2185 }
2186
2187 ret = kernel_create_channel(cmd_ctx->session->kernel_session,
2188 chan, cmd_ctx->session->kernel_session->trace_path);
2189 if (ret < 0) {
2190 ret = LTTCOMM_KERN_CHAN_FAIL;
2191 goto error;
2192 }
62499ad6 2193 kchan = trace_kernel_get_channel_by_name(channel_name,
0d0c377a
DG
2194 cmd_ctx->session->kernel_session);
2195 if (kchan == NULL) {
b389abbe
MD
2196 ERR("Channel %s not found after creation. Internal error, giving up.",
2197 channel_name);
2198 ret = LTTCOMM_FATAL;
2199 goto error;
7d29a247 2200 }
b389abbe 2201 }
7d29a247 2202
0d0c377a
DG
2203 /* For each event in the kernel session */
2204 cds_list_for_each_entry(kevent, &kchan->events_list.head, list) {
2205 DBG("Enabling kernel event %s for channel %s.",
2206 kevent->event->name, channel_name);
2207 ret = kernel_enable_event(kevent);
7d29a247 2208 if (ret < 0) {
0d0c377a 2209 continue;
7d29a247
DG
2210 }
2211 }
33a2b854 2212
0d0c377a
DG
2213 size = kernel_list_events(kernel_tracer_fd, &event_list);
2214 if (size < 0) {
2215 ret = LTTCOMM_KERN_LIST_FAIL;
2216 goto error;
f3ed775e 2217 }
f3ed775e 2218
0d0c377a 2219 for (i = 0; i < size; i++) {
62499ad6 2220 kevent = trace_kernel_get_event_by_name(event_list[i].name, kchan);
0d0c377a
DG
2221 if (kevent == NULL) {
2222 /* Default event type for enable all */
2223 event_list[i].type = LTTNG_EVENT_TRACEPOINT;
2224 /* Enable each single tracepoint event */
2225 ret = kernel_create_event(&event_list[i], kchan);
2226 if (ret < 0) {
2227 /* Ignore error here and continue */
2228 }
950131af 2229 }
33a2b854 2230 }
33a2b854 2231
0d0c377a
DG
2232 free(event_list);
2233
2234 /* Quiescent wait after event enable */
2235 kernel_wait_quiescent(kernel_tracer_fd);
2236 break;
2237 default:
2238 /* TODO: Userspace tracing */
2239 ret = LTTCOMM_NOT_IMPLEMENTED;
2240 goto error;
2241 }
33a2b854
DG
2242
2243 ret = LTTCOMM_OK;
2244 break;
2245 }
052da939 2246 case LTTNG_LIST_TRACEPOINTS:
2ef84c95 2247 {
9f19cc17 2248 struct lttng_event *events;
052da939
DG
2249 ssize_t nb_events = 0;
2250
2251 switch (cmd_ctx->lsm->domain.type) {
2252 case LTTNG_DOMAIN_KERNEL:
2253 DBG("Listing kernel events");
2254 nb_events = kernel_list_events(kernel_tracer_fd, &events);
2255 if (nb_events < 0) {
2256 ret = LTTCOMM_KERN_LIST_FAIL;
2257 goto error;
2258 }
2259 break;
2260 default:
2261 /* TODO: Userspace listing */
2262 ret = LTTCOMM_NOT_IMPLEMENTED;
2263 break;
2ef84c95
DG
2264 }
2265
2266 /*
2267 * Setup lttng message with payload size set to the event list size in
2268 * bytes and then copy list into the llm payload.
2269 */
052da939 2270 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_event) * nb_events);
2ef84c95 2271 if (ret < 0) {
052da939 2272 free(events);
2ef84c95
DG
2273 goto setup_error;
2274 }
2275
2276 /* Copy event list into message payload */
9f19cc17 2277 memcpy(cmd_ctx->llm->payload, events,
052da939 2278 sizeof(struct lttng_event) * nb_events);
2ef84c95 2279
9f19cc17 2280 free(events);
2ef84c95
DG
2281
2282 ret = LTTCOMM_OK;
2283 break;
2284 }
f3ed775e 2285 case LTTNG_START_TRACE:
8c0faa1d
DG
2286 {
2287 struct ltt_kernel_channel *chan;
f3ed775e 2288
8c0faa1d
DG
2289 /* Setup lttng message with no payload */
2290 ret = setup_lttng_msg(cmd_ctx, 0);
2291 if (ret < 0) {
2292 goto setup_error;
2293 }
2294
f3ed775e
DG
2295 /* Kernel tracing */
2296 if (cmd_ctx->session->kernel_session != NULL) {
2297 if (cmd_ctx->session->kernel_session->metadata == NULL) {
2298 DBG("Open kernel metadata");
58a97671 2299 ret = kernel_open_metadata(cmd_ctx->session->kernel_session,
63053e7c 2300 cmd_ctx->session->kernel_session->trace_path);
f3ed775e
DG
2301 if (ret < 0) {
2302 ret = LTTCOMM_KERN_META_FAIL;
2303 goto error;
2304 }
2305 }
8c0faa1d 2306
f3ed775e
DG
2307 if (cmd_ctx->session->kernel_session->metadata_stream_fd == 0) {
2308 DBG("Opening kernel metadata stream");
2309 if (cmd_ctx->session->kernel_session->metadata_stream_fd == 0) {
2310 ret = kernel_open_metadata_stream(cmd_ctx->session->kernel_session);
2311 if (ret < 0) {
2312 ERR("Kernel create metadata stream failed");
2313 ret = LTTCOMM_KERN_STREAM_FAIL;
2314 goto error;
2315 }
2316 }
2317 }
8c0faa1d 2318
f3ed775e 2319 /* For each channel */
0d0c377a
DG
2320 cds_list_for_each_entry(chan,
2321 &cmd_ctx->session->kernel_session->channel_list.head, list) {
f3ed775e
DG
2322 if (chan->stream_count == 0) {
2323 ret = kernel_open_channel_stream(chan);
2324 if (ret < 0) {
2325 ERR("Kernel create channel stream failed");
2326 ret = LTTCOMM_KERN_STREAM_FAIL;
2327 goto error;
2328 }
2329 /* Update the stream global counter */
2330 cmd_ctx->session->kernel_session->stream_count_global += ret;
2331 }
2332 }
2333
283046e0 2334 ret = start_kernel_trace(cmd_ctx->session->kernel_session);
8c0faa1d 2335 if (ret < 0) {
f3ed775e 2336 ret = LTTCOMM_KERN_START_FAIL;
8c0faa1d
DG
2337 goto error;
2338 }
8c0faa1d 2339
283046e0
DG
2340 DBG("Start kernel tracing");
2341 ret = kernel_start_session(cmd_ctx->session->kernel_session);
f3ed775e 2342 if (ret < 0) {
283046e0 2343 ERR("Kernel start session failed");
f3ed775e 2344 ret = LTTCOMM_KERN_START_FAIL;
8c0faa1d
DG
2345 goto error;
2346 }
8c0faa1d 2347
f3ed775e
DG
2348 /* Quiescent wait after starting trace */
2349 kernel_wait_quiescent(kernel_tracer_fd);
8c0faa1d
DG
2350 }
2351
f3ed775e 2352 /* TODO: Start all UST traces */
8c0faa1d
DG
2353
2354 ret = LTTCOMM_OK;
2355 break;
2356 }
f3ed775e 2357 case LTTNG_STOP_TRACE:
8c0faa1d 2358 {
f3ed775e 2359 struct ltt_kernel_channel *chan;
8c0faa1d
DG
2360 /* Setup lttng message with no payload */
2361 ret = setup_lttng_msg(cmd_ctx, 0);
2362 if (ret < 0) {
2363 goto setup_error;
2364 }
2365
f3ed775e
DG
2366 /* Kernel tracer */
2367 if (cmd_ctx->session->kernel_session != NULL) {
2368 DBG("Stop kernel tracing");
84291629 2369
f3ed775e
DG
2370 ret = kernel_metadata_flush_buffer(cmd_ctx->session->kernel_session->metadata_stream_fd);
2371 if (ret < 0) {
2372 ERR("Kernel metadata flush failed");
2373 }
8c0faa1d 2374
f3ed775e
DG
2375 cds_list_for_each_entry(chan, &cmd_ctx->session->kernel_session->channel_list.head, list) {
2376 ret = kernel_flush_buffer(chan);
2377 if (ret < 0) {
2378 ERR("Kernel flush buffer error");
2379 }
2380 }
2381
2382 ret = kernel_stop_session(cmd_ctx->session->kernel_session);
2383 if (ret < 0) {
2384 ERR("Kernel stop session failed");
2385 ret = LTTCOMM_KERN_STOP_FAIL;
2386 goto error;
2387 }
2388
2389 /* Quiescent wait after stopping trace */
2390 kernel_wait_quiescent(kernel_tracer_fd);
8c0faa1d
DG
2391 }
2392
f3ed775e 2393 /* TODO : User-space tracer */
8c0faa1d
DG
2394
2395 ret = LTTCOMM_OK;
2396 break;
2397 }
5e16da05
MD
2398 case LTTNG_CREATE_SESSION:
2399 {
5461b305
DG
2400 /* Setup lttng message with no payload */
2401 ret = setup_lttng_msg(cmd_ctx, 0);
2402 if (ret < 0) {
2403 goto setup_error;
2404 }
2405
3c6bae61 2406 tracepoint(create_session_start);
42abccdb 2407 ret = create_session(cmd_ctx->lsm->session.name, cmd_ctx->lsm->session.path);
3c6bae61 2408 tracepoint(create_session_end);
5e16da05 2409 if (ret < 0) {
f3ed775e 2410 if (ret == -EEXIST) {
5e16da05
MD
2411 ret = LTTCOMM_EXIST_SESS;
2412 } else {
aaf97519 2413 ret = LTTCOMM_FATAL;
aaf97519 2414 }
5461b305 2415 goto error;
8028d920 2416 }
1657e9bb 2417
5461b305 2418 ret = LTTCOMM_OK;
5e16da05
MD
2419 break;
2420 }
2421 case LTTNG_DESTROY_SESSION:
2422 {
5461b305
DG
2423 /* Setup lttng message with no payload */
2424 ret = setup_lttng_msg(cmd_ctx, 0);
2425 if (ret < 0) {
2426 goto setup_error;
2427 }
2428
f3ed775e
DG
2429 /* Clean kernel session teardown */
2430 teardown_kernel_session(cmd_ctx->session);
2431
3c6bae61 2432 tracepoint(destroy_session_start);
42abccdb 2433 ret = destroy_session(cmd_ctx->lsm->session.name);
3c6bae61 2434 tracepoint(destroy_session_end);
5e16da05 2435 if (ret < 0) {
f3ed775e 2436 ret = LTTCOMM_FATAL;
5461b305 2437 goto error;
5e16da05 2438 }
1657e9bb 2439
7a485870
DG
2440 /*
2441 * Must notify the kernel thread here to update it's pollfd in order to
2442 * remove the channel(s)' fd just destroyed.
2443 */
2444 ret = notify_kernel_pollfd();
2445 if (ret < 0) {
2446 ret = LTTCOMM_FATAL;
2447 goto error;
2448 }
2449
5461b305
DG
2450 ret = LTTCOMM_OK;
2451 break;
5e16da05 2452 }
9f19cc17 2453 case LTTNG_LIST_DOMAINS:
5e16da05 2454 {
4b222185 2455 size_t nb_dom = 0;
5461b305 2456
9f19cc17
DG
2457 if (cmd_ctx->session->kernel_session != NULL) {
2458 nb_dom++;
ce3d728c 2459 }
520ff687 2460
0177d773 2461 nb_dom += cmd_ctx->session->ust_session_list.count;
9f19cc17
DG
2462
2463 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_domain) * nb_dom);
5461b305
DG
2464 if (ret < 0) {
2465 goto setup_error;
520ff687 2466 }
57167058 2467
9f19cc17
DG
2468 ((struct lttng_domain *)(cmd_ctx->llm->payload))[0].type =
2469 LTTNG_DOMAIN_KERNEL;
5e16da05 2470
9f19cc17 2471 /* TODO: User-space tracer domain support */
5461b305 2472 ret = LTTCOMM_OK;
5e16da05
MD
2473 break;
2474 }
9f19cc17 2475 case LTTNG_LIST_CHANNELS:
5e16da05 2476 {
9f19cc17
DG
2477 /*
2478 * TODO: Only kernel channels are listed here. UST listing
2479 * is needed on lttng-ust 2.0 release.
2480 */
2481 size_t nb_chan = 0;
2482 if (cmd_ctx->session->kernel_session != NULL) {
2483 nb_chan += cmd_ctx->session->kernel_session->channel_count;
5461b305 2484 }
ca95a216 2485
9f19cc17
DG
2486 ret = setup_lttng_msg(cmd_ctx,
2487 sizeof(struct lttng_channel) * nb_chan);
5461b305
DG
2488 if (ret < 0) {
2489 goto setup_error;
2490 }
9f19cc17
DG
2491
2492 list_lttng_channels(cmd_ctx->session,
2493 (struct lttng_channel *)(cmd_ctx->llm->payload));
2494
2495 ret = LTTCOMM_OK;
5461b305 2496 break;
5e16da05 2497 }
9f19cc17 2498 case LTTNG_LIST_EVENTS:
5e16da05 2499 {
9f19cc17
DG
2500 /*
2501 * TODO: Only kernel events are listed here. UST listing
2502 * is needed on lttng-ust 2.0 release.
2503 */
2504 size_t nb_event = 0;
2505 struct ltt_kernel_channel *kchan = NULL;
2506
2507 if (cmd_ctx->session->kernel_session != NULL) {
62499ad6 2508 kchan = trace_kernel_get_channel_by_name(cmd_ctx->lsm->u.list.channel_name,
9f19cc17
DG
2509 cmd_ctx->session->kernel_session);
2510 if (kchan == NULL) {
2511 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
2512 goto error;
2513 }
2514 nb_event += kchan->event_count;
5461b305 2515 }
ca95a216 2516
9f19cc17
DG
2517 ret = setup_lttng_msg(cmd_ctx,
2518 sizeof(struct lttng_event) * nb_event);
5461b305
DG
2519 if (ret < 0) {
2520 goto setup_error;
2521 }
9f19cc17 2522
ced2f820 2523 DBG("Listing events (%zu events)", nb_event);
9f19cc17
DG
2524
2525 list_lttng_events(kchan,
2526 (struct lttng_event *)(cmd_ctx->llm->payload));
2527
2528 ret = LTTCOMM_OK;
5461b305 2529 break;
5e16da05
MD
2530 }
2531 case LTTNG_LIST_SESSIONS:
2532 {
6c9cc2ab 2533 lock_session_list();
5461b305 2534
6c9cc2ab 2535 if (session_list_ptr->count == 0) {
f3ed775e 2536 ret = LTTCOMM_NO_SESSION;
36a83748 2537 unlock_session_list();
5461b305 2538 goto error;
57167058 2539 }
5e16da05 2540
6c9cc2ab
DG
2541 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_session) *
2542 session_list_ptr->count);
5461b305 2543 if (ret < 0) {
36a83748 2544 unlock_session_list();
5461b305 2545 goto setup_error;
e065084a 2546 }
5e16da05 2547
6c9cc2ab
DG
2548 /* Filled the session array */
2549 list_lttng_sessions((struct lttng_session *)(cmd_ctx->llm->payload));
2550
2551 unlock_session_list();
5e16da05 2552
5461b305 2553 ret = LTTCOMM_OK;
5e16da05
MD
2554 break;
2555 }
d0254c7c
MD
2556 case LTTNG_CALIBRATE:
2557 {
2558 /* Setup lttng message with no payload */
2559 ret = setup_lttng_msg(cmd_ctx, 0);
2560 if (ret < 0) {
2561 goto setup_error;
2562 }
2563
2564 switch (cmd_ctx->lsm->domain.type) {
2565 case LTTNG_DOMAIN_KERNEL:
2566 {
2567 struct lttng_kernel_calibrate kcalibrate;
2568
2569 kcalibrate.type = cmd_ctx->lsm->u.calibrate.type;
2570 ret = kernel_calibrate(kernel_tracer_fd, &kcalibrate);
2571 if (ret < 0) {
2572 ret = LTTCOMM_KERN_ENABLE_FAIL;
2573 goto error;
2574 }
2575 break;
2576 }
2577 default:
2578 /* TODO: Userspace tracing */
2579 ret = LTTCOMM_NOT_IMPLEMENTED;
2580 goto error;
2581 }
2582 ret = LTTCOMM_OK;
2583 break;
2584 }
d9800920
DG
2585 case LTTNG_REGISTER_CONSUMER:
2586 {
2587 int sock;
2588
2589 /* Setup lttng message with no payload */
2590 ret = setup_lttng_msg(cmd_ctx, 0);
2591 if (ret < 0) {
2592 goto setup_error;
2593 }
2594
2595 switch (cmd_ctx->lsm->domain.type) {
2596 case LTTNG_DOMAIN_KERNEL:
2597 {
2598 /* Can't register a consumer if there is already one */
2599 if (cmd_ctx->session->kernel_session->consumer_fd != 0) {
2600 ret = LTTCOMM_CONNECT_FAIL;
2601 goto error;
2602 }
2603
2604 sock = lttcomm_connect_unix_sock(cmd_ctx->lsm->u.reg.path);
2605 if (sock < 0) {
2606 ret = LTTCOMM_CONNECT_FAIL;
2607 goto error;
2608 }
2609
2610 cmd_ctx->session->kernel_session->consumer_fd = sock;
2611 break;
2612 }
2613 default:
2614 /* TODO: Userspace tracing */
2615 ret = LTTCOMM_NOT_IMPLEMENTED;
2616 goto error;
2617 }
2618
2619 ret = LTTCOMM_OK;
2620 break;
2621 }
d0254c7c 2622
5e16da05
MD
2623 default:
2624 /* Undefined command */
5461b305
DG
2625 ret = setup_lttng_msg(cmd_ctx, 0);
2626 if (ret < 0) {
2627 goto setup_error;
2628 }
2629
5e16da05 2630 ret = LTTCOMM_UND;
5461b305 2631 break;
fac6795d
DG
2632 }
2633
5461b305
DG
2634 /* Set return code */
2635 cmd_ctx->llm->ret_code = ret;
ca95a216 2636
b5541356
DG
2637 if (cmd_ctx->session) {
2638 unlock_session(cmd_ctx->session);
2639 }
2640
e065084a
DG
2641 return ret;
2642
5461b305 2643error:
5461b305
DG
2644 if (cmd_ctx->llm == NULL) {
2645 DBG("Missing llm structure. Allocating one.");
894be886 2646 if (setup_lttng_msg(cmd_ctx, 0) < 0) {
5461b305
DG
2647 goto setup_error;
2648 }
2649 }
e065084a 2650 /* Notify client of error */
5461b305 2651 cmd_ctx->llm->ret_code = ret;
e065084a 2652
5461b305 2653setup_error:
b5541356
DG
2654 if (cmd_ctx->session) {
2655 unlock_session(cmd_ctx->session);
2656 }
8028d920 2657 return ret;
fac6795d
DG
2658}
2659
1d4b027a 2660/*
d063d709
DG
2661 * This thread manage all clients request using the unix client socket for
2662 * communication.
1d4b027a
DG
2663 */
2664static void *thread_manage_clients(void *data)
2665{
273ea72c
DG
2666 int sock = 0, ret;
2667 struct command_ctx *cmd_ctx = NULL;
2668 struct pollfd pollfd[2];
1d4b027a 2669
b25a8868
DG
2670 tracepoint(sessiond_th_cli_start);
2671
1d4b027a
DG
2672 DBG("[thread] Manage client started");
2673
2674 ret = lttcomm_listen_unix_sock(client_sock);
2675 if (ret < 0) {
2676 goto error;
2677 }
2678
273ea72c
DG
2679 /* First fd is always the quit pipe */
2680 pollfd[0].fd = thread_quit_pipe[0];
2681
2682 /* Apps socket */
2683 pollfd[1].fd = client_sock;
2684 pollfd[1].events = POLLIN;
2685
1d4b027a
DG
2686 /* Notify parent pid that we are ready
2687 * to accept command for client side.
2688 */
2689 if (opt_sig_parent) {
2690 kill(ppid, SIGCHLD);
2691 }
2692
2693 while (1) {
1d4b027a 2694 DBG("Accepting client command ...");
273ea72c 2695
b25a8868
DG
2696 tracepoint(sessiond_th_cli_poll);
2697
273ea72c
DG
2698 /* Inifinite blocking call, waiting for transmission */
2699 ret = poll(pollfd, 2, -1);
2700 if (ret < 0) {
2701 perror("poll client thread");
2702 goto error;
2703 }
2704
2705 /* Thread quit pipe has been closed. Killing thread. */
2706 if (pollfd[0].revents == POLLNVAL) {
2707 goto error;
2708 } else if (pollfd[1].revents == POLLERR) {
2709 ERR("Client socket poll error");
2710 goto error;
2711 }
2712
1d4b027a
DG
2713 sock = lttcomm_accept_unix_sock(client_sock);
2714 if (sock < 0) {
2715 goto error;
2716 }
2717
2718 /* Allocate context command to process the client request */
2719 cmd_ctx = malloc(sizeof(struct command_ctx));
2720
2721 /* Allocate data buffer for reception */
2722 cmd_ctx->lsm = malloc(sizeof(struct lttcomm_session_msg));
2723 cmd_ctx->llm = NULL;
2724 cmd_ctx->session = NULL;
2725
2726 /*
2727 * Data is received from the lttng client. The struct
2728 * lttcomm_session_msg (lsm) contains the command and data request of
2729 * the client.
2730 */
2731 DBG("Receiving data from client ...");
2732 ret = lttcomm_recv_unix_sock(sock, cmd_ctx->lsm, sizeof(struct lttcomm_session_msg));
2733 if (ret <= 0) {
2734 continue;
2735 }
2736
f7776ea7
DG
2737 // TODO: Validate cmd_ctx including sanity check for security purpose.
2738
1d4b027a
DG
2739 /*
2740 * This function dispatch the work to the kernel or userspace tracer
2741 * libs and fill the lttcomm_lttng_msg data structure of all the needed
2742 * informations for the client. The command context struct contains
2743 * everything this function may needs.
2744 */
2745 ret = process_client_msg(cmd_ctx);
2746 if (ret < 0) {
2747 /* TODO: Inform client somehow of the fatal error. At this point,
2748 * ret < 0 means that a malloc failed (ENOMEM). */
2749 /* Error detected but still accept command */
a2fb29a5 2750 clean_command_ctx(&cmd_ctx);
1d4b027a
DG
2751 continue;
2752 }
2753
2754 DBG("Sending response (size: %d, retcode: %d)",
2755 cmd_ctx->lttng_msg_size, cmd_ctx->llm->ret_code);
2756 ret = send_unix_sock(sock, cmd_ctx->llm, cmd_ctx->lttng_msg_size);
2757 if (ret < 0) {
2758 ERR("Failed to send data back to client");
2759 }
2760
a2fb29a5 2761 clean_command_ctx(&cmd_ctx);
d6e4fca4
DG
2762
2763 /* End of transmission */
2764 close(sock);
1d4b027a
DG
2765 }
2766
2767error:
273ea72c
DG
2768 DBG("Client thread dying");
2769 if (client_sock) {
2770 close(client_sock);
2771 }
2772 if (sock) {
2773 close(sock);
2774 }
2775
2776 unlink(client_unix_sock_path);
2777
a2fb29a5 2778 clean_command_ctx(&cmd_ctx);
1d4b027a
DG
2779 return NULL;
2780}
2781
2782
fac6795d
DG
2783/*
2784 * usage function on stderr
2785 */
2786static void usage(void)
2787{
b716ce68 2788 fprintf(stderr, "Usage: %s OPTIONS\n\nOptions:\n", progname);
d6f42150
DG
2789 fprintf(stderr, " -h, --help Display this usage.\n");
2790 fprintf(stderr, " -c, --client-sock PATH Specify path for the client unix socket\n");
2791 fprintf(stderr, " -a, --apps-sock PATH Specify path for apps unix socket\n");
2792 fprintf(stderr, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
2793 fprintf(stderr, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
2794 fprintf(stderr, " -d, --daemonize Start as a daemon.\n");
2795 fprintf(stderr, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
2796 fprintf(stderr, " -V, --version Show version number.\n");
2797 fprintf(stderr, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
2798 fprintf(stderr, " -q, --quiet No output at all.\n");
2799 fprintf(stderr, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
31f73cc9 2800 fprintf(stderr, " --verbose-kconsumerd Verbose mode for kconsumerd. Activate DBG() macro.\n");
fac6795d
DG
2801}
2802
2803/*
2804 * daemon argument parsing
2805 */
2806static int parse_args(int argc, char **argv)
2807{
2808 int c;
2809
2810 static struct option long_options[] = {
2811 { "client-sock", 1, 0, 'c' },
2812 { "apps-sock", 1, 0, 'a' },
d6f42150
DG
2813 { "kconsumerd-cmd-sock", 1, 0, 0 },
2814 { "kconsumerd-err-sock", 1, 0, 0 },
fac6795d 2815 { "daemonize", 0, 0, 'd' },
5b8719f5 2816 { "sig-parent", 0, 0, 'S' },
fac6795d
DG
2817 { "help", 0, 0, 'h' },
2818 { "group", 1, 0, 'g' },
2819 { "version", 0, 0, 'V' },
75462a81 2820 { "quiet", 0, 0, 'q' },
3f9947db 2821 { "verbose", 0, 0, 'v' },
31f73cc9 2822 { "verbose-kconsumerd", 0, 0, 'Z' },
fac6795d
DG
2823 { NULL, 0, 0, 0 }
2824 };
2825
2826 while (1) {
2827 int option_index = 0;
31f73cc9 2828 c = getopt_long(argc, argv, "dhqvVS" "a:c:g:s:E:C:Z", long_options, &option_index);
fac6795d
DG
2829 if (c == -1) {
2830 break;
2831 }
2832
2833 switch (c) {
2834 case 0:
2835 fprintf(stderr, "option %s", long_options[option_index].name);
2836 if (optarg) {
2837 fprintf(stderr, " with arg %s\n", optarg);
2838 }
2839 break;
b716ce68 2840 case 'c':
fac6795d
DG
2841 snprintf(client_unix_sock_path, PATH_MAX, "%s", optarg);
2842 break;
2843 case 'a':
2844 snprintf(apps_unix_sock_path, PATH_MAX, "%s", optarg);
2845 break;
2846 case 'd':
2847 opt_daemon = 1;
2848 break;
2849 case 'g':
2850 opt_tracing_group = strdup(optarg);
2851 break;
2852 case 'h':
2853 usage();
2854 exit(EXIT_FAILURE);
2855 case 'V':
2856 fprintf(stdout, "%s\n", VERSION);
2857 exit(EXIT_SUCCESS);
5b8719f5
DG
2858 case 'S':
2859 opt_sig_parent = 1;
2860 break;
d6f42150
DG
2861 case 'E':
2862 snprintf(kconsumerd_err_unix_sock_path, PATH_MAX, "%s", optarg);
2863 break;
2864 case 'C':
2865 snprintf(kconsumerd_cmd_unix_sock_path, PATH_MAX, "%s", optarg);
2866 break;
75462a81
DG
2867 case 'q':
2868 opt_quiet = 1;
2869 break;
3f9947db 2870 case 'v':
53086306
DG
2871 /* Verbose level can increase using multiple -v */
2872 opt_verbose += 1;
3f9947db 2873 break;
31f73cc9
MD
2874 case 'Z':
2875 opt_verbose_kconsumerd += 1;
2876 break;
fac6795d
DG
2877 default:
2878 /* Unknown option or other error.
2879 * Error is printed by getopt, just return */
2880 return -1;
2881 }
2882 }
2883
2884 return 0;
2885}
2886
2887/*
d063d709 2888 * Creates the two needed socket by the daemon.
d6f42150
DG
2889 * apps_sock - The communication socket for all UST apps.
2890 * client_sock - The communication of the cli tool (lttng).
fac6795d 2891 */
cf3af59e 2892static int init_daemon_socket(void)
fac6795d
DG
2893{
2894 int ret = 0;
2895 mode_t old_umask;
2896
2897 old_umask = umask(0);
2898
2899 /* Create client tool unix socket */
d6f42150
DG
2900 client_sock = lttcomm_create_unix_sock(client_unix_sock_path);
2901 if (client_sock < 0) {
2902 ERR("Create unix sock failed: %s", client_unix_sock_path);
fac6795d
DG
2903 ret = -1;
2904 goto end;
2905 }
2906
2907 /* File permission MUST be 660 */
2908 ret = chmod(client_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
2909 if (ret < 0) {
d6f42150 2910 ERR("Set file permissions failed: %s", client_unix_sock_path);
fac6795d
DG
2911 perror("chmod");
2912 goto end;
2913 }
2914
2915 /* Create the application unix socket */
d6f42150
DG
2916 apps_sock = lttcomm_create_unix_sock(apps_unix_sock_path);
2917 if (apps_sock < 0) {
2918 ERR("Create unix sock failed: %s", apps_unix_sock_path);
fac6795d
DG
2919 ret = -1;
2920 goto end;
2921 }
2922
d6f42150 2923 /* File permission MUST be 666 */
fac6795d
DG
2924 ret = chmod(apps_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
2925 if (ret < 0) {
d6f42150 2926 ERR("Set file permissions failed: %s", apps_unix_sock_path);
fac6795d
DG
2927 perror("chmod");
2928 goto end;
2929 }
2930
2931end:
2932 umask(old_umask);
2933 return ret;
2934}
2935
2936/*
7d8234d9
MD
2937 * Check if the global socket is available, and if a daemon is answering
2938 * at the other side. If yes, error is returned.
fac6795d 2939 */
cf3af59e 2940static int check_existing_daemon(void)
fac6795d 2941{
7d8234d9 2942 if (access(client_unix_sock_path, F_OK) < 0 &&
099e26bd 2943 access(apps_unix_sock_path, F_OK) < 0) {
7d8234d9 2944 return 0;
099e26bd 2945 }
7d8234d9 2946 /* Is there anybody out there ? */
099e26bd 2947 if (lttng_session_daemon_alive()) {
7d8234d9 2948 return -EEXIST;
099e26bd 2949 } else {
7d8234d9 2950 return 0;
099e26bd 2951 }
fac6795d
DG
2952}
2953
fac6795d 2954/*
d063d709 2955 * Set the tracing group gid onto the client socket.
5e16da05 2956 *
d063d709
DG
2957 * Race window between mkdir and chown is OK because we are going from more
2958 * permissive (root.root) to les permissive (root.tracing).
fac6795d 2959 */
d6f42150 2960static int set_permissions(void)
fac6795d
DG
2961{
2962 int ret;
996b65c8 2963 gid_t gid;
fac6795d 2964
996b65c8
MD
2965 gid = allowed_group();
2966 if (gid < 0) {
a463f419
DG
2967 if (is_root) {
2968 WARN("No tracing group detected");
2969 ret = 0;
2970 } else {
2971 ERR("Missing tracing group. Aborting execution.");
2972 ret = -1;
2973 }
fac6795d
DG
2974 goto end;
2975 }
2976
d6f42150 2977 /* Set lttng run dir */
996b65c8 2978 ret = chown(LTTNG_RUNDIR, 0, gid);
d6f42150
DG
2979 if (ret < 0) {
2980 ERR("Unable to set group on " LTTNG_RUNDIR);
2981 perror("chown");
2982 }
2983
2984 /* lttng client socket path */
996b65c8 2985 ret = chown(client_unix_sock_path, 0, gid);
fac6795d 2986 if (ret < 0) {
d6f42150
DG
2987 ERR("Unable to set group on %s", client_unix_sock_path);
2988 perror("chown");
2989 }
2990
2991 /* kconsumerd error socket path */
996b65c8 2992 ret = chown(kconsumerd_err_unix_sock_path, 0, gid);
d6f42150
DG
2993 if (ret < 0) {
2994 ERR("Unable to set group on %s", kconsumerd_err_unix_sock_path);
fac6795d
DG
2995 perror("chown");
2996 }
2997
d6f42150 2998 DBG("All permissions are set");
e07ae692 2999
fac6795d
DG
3000end:
3001 return ret;
3002}
3003
7a485870 3004/*
d063d709 3005 * Create the pipe used to wake up the kernel thread.
7a485870
DG
3006 */
3007static int create_kernel_poll_pipe(void)
3008{
3009 return pipe2(kernel_poll_pipe, O_CLOEXEC);
3010}
3011
099e26bd
DG
3012/*
3013 * Create the application command pipe to wake thread_manage_apps.
3014 */
3015static int create_apps_cmd_pipe(void)
3016{
3017 return pipe2(apps_cmd_pipe, O_CLOEXEC);
3018}
3019
d6f42150 3020/*
d063d709 3021 * Create the lttng run directory needed for all global sockets and pipe.
d6f42150
DG
3022 */
3023static int create_lttng_rundir(void)
3024{
3025 int ret;
3026
3027 ret = mkdir(LTTNG_RUNDIR, S_IRWXU | S_IRWXG );
3028 if (ret < 0) {
b1f11e69
DG
3029 if (errno != EEXIST) {
3030 ERR("Unable to create " LTTNG_RUNDIR);
3031 goto error;
3032 } else {
3033 ret = 0;
3034 }
d6f42150
DG
3035 }
3036
3037error:
3038 return ret;
3039}
3040
3041/*
d063d709
DG
3042 * Setup sockets and directory needed by the kconsumerd communication with the
3043 * session daemon.
d6f42150
DG
3044 */
3045static int set_kconsumerd_sockets(void)
3046{
3047 int ret;
3048
3049 if (strlen(kconsumerd_err_unix_sock_path) == 0) {
3050 snprintf(kconsumerd_err_unix_sock_path, PATH_MAX, KCONSUMERD_ERR_SOCK_PATH);
3051 }
3052
3053 if (strlen(kconsumerd_cmd_unix_sock_path) == 0) {
3054 snprintf(kconsumerd_cmd_unix_sock_path, PATH_MAX, KCONSUMERD_CMD_SOCK_PATH);
3055 }
3056
3057 ret = mkdir(KCONSUMERD_PATH, S_IRWXU | S_IRWXG);
3058 if (ret < 0) {
6beb2242
DG
3059 if (errno != EEXIST) {
3060 ERR("Failed to create " KCONSUMERD_PATH);
3061 goto error;
3062 }
3063 ret = 0;
d6f42150
DG
3064 }
3065
3066 /* Create the kconsumerd error unix socket */
3067 kconsumerd_err_sock = lttcomm_create_unix_sock(kconsumerd_err_unix_sock_path);
3068 if (kconsumerd_err_sock < 0) {
3069 ERR("Create unix sock failed: %s", kconsumerd_err_unix_sock_path);
3070 ret = -1;
3071 goto error;
3072 }
3073
3074 /* File permission MUST be 660 */
3075 ret = chmod(kconsumerd_err_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
3076 if (ret < 0) {
3077 ERR("Set file permissions failed: %s", kconsumerd_err_unix_sock_path);
3078 perror("chmod");
3079 goto error;
3080 }
3081
3082error:
3083 return ret;
3084}
3085
fac6795d 3086/*
d063d709 3087 * Signal handler for the daemon
cf3af59e
MD
3088 *
3089 * Simply stop all worker threads, leaving main() return gracefully
3090 * after joining all threads and calling cleanup().
fac6795d
DG
3091 */
3092static void sighandler(int sig)
3093{
3094 switch (sig) {
cf3af59e
MD
3095 case SIGPIPE:
3096 DBG("SIGPIPE catched");
3097 return;
3098 case SIGINT:
3099 DBG("SIGINT catched");
3100 stop_threads();
3101 break;
3102 case SIGTERM:
3103 DBG("SIGTERM catched");
3104 stop_threads();
3105 break;
3106 default:
3107 break;
fac6795d 3108 }
fac6795d
DG
3109}
3110
3111/*
d063d709 3112 * Setup signal handler for :
1d4b027a 3113 * SIGINT, SIGTERM, SIGPIPE
fac6795d 3114 */
1d4b027a 3115static int set_signal_handler(void)
fac6795d 3116{
1d4b027a
DG
3117 int ret = 0;
3118 struct sigaction sa;
3119 sigset_t sigset;
fac6795d 3120
1d4b027a
DG
3121 if ((ret = sigemptyset(&sigset)) < 0) {
3122 perror("sigemptyset");
3123 return ret;
3124 }
d6f42150 3125
1d4b027a
DG
3126 sa.sa_handler = sighandler;
3127 sa.sa_mask = sigset;
3128 sa.sa_flags = 0;
3129 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
3130 perror("sigaction");
3131 return ret;
d6f42150
DG
3132 }
3133
1d4b027a
DG
3134 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
3135 perror("sigaction");
3136 return ret;
d6f42150 3137 }
aaf26714 3138
1d4b027a
DG
3139 if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) {
3140 perror("sigaction");
3141 return ret;
8c0faa1d
DG
3142 }
3143
1d4b027a
DG
3144 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
3145
3146 return ret;
fac6795d
DG
3147}
3148
f3ed775e 3149/*
d063d709
DG
3150 * Set open files limit to unlimited. This daemon can open a large number of
3151 * file descriptors in order to consumer multiple kernel traces.
f3ed775e
DG
3152 */
3153static void set_ulimit(void)
3154{
3155 int ret;
3156 struct rlimit lim;
3157
a88df331 3158 /* The kernel does not allowed an infinite limit for open files */
f3ed775e
DG
3159 lim.rlim_cur = 65535;
3160 lim.rlim_max = 65535;
3161
3162 ret = setrlimit(RLIMIT_NOFILE, &lim);
3163 if (ret < 0) {
3164 perror("failed to set open files limit");
3165 }
3166}
3167
fac6795d
DG
3168/*
3169 * main
3170 */
3171int main(int argc, char **argv)
3172{
fac6795d
DG
3173 int ret = 0;
3174 void *status;
b082db07 3175 const char *home_path;
fac6795d 3176
b25a8868
DG
3177 tracepoint(sessiond_boot_start);
3178
273ea72c 3179 /* Create thread quit pipe */
cf3af59e
MD
3180 if ((ret = init_thread_quit_pipe()) < 0) {
3181 goto error;
273ea72c
DG
3182 }
3183
fac6795d
DG
3184 /* Parse arguments */
3185 progname = argv[0];
3186 if ((ret = parse_args(argc, argv) < 0)) {
cf3af59e 3187 goto error;
fac6795d
DG
3188 }
3189
3190 /* Daemonize */
3191 if (opt_daemon) {
53094c05
DG
3192 ret = daemon(0, 0);
3193 if (ret < 0) {
3194 perror("daemon");
cf3af59e 3195 goto error;
53094c05 3196 }
fac6795d
DG
3197 }
3198
3199 /* Check if daemon is UID = 0 */
3200 is_root = !getuid();
3201
fac6795d 3202 if (is_root) {
d6f42150
DG
3203 ret = create_lttng_rundir();
3204 if (ret < 0) {
cf3af59e 3205 goto error;
d6f42150
DG
3206 }
3207
fac6795d 3208 if (strlen(apps_unix_sock_path) == 0) {
d6f42150
DG
3209 snprintf(apps_unix_sock_path, PATH_MAX,
3210 DEFAULT_GLOBAL_APPS_UNIX_SOCK);
fac6795d
DG
3211 }
3212
3213 if (strlen(client_unix_sock_path) == 0) {
d6f42150
DG
3214 snprintf(client_unix_sock_path, PATH_MAX,
3215 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK);
3216 }
0fdd1e2c
DG
3217
3218 /* Set global SHM for ust */
3219 if (strlen(wait_shm_path) == 0) {
3220 snprintf(wait_shm_path, PATH_MAX,
3221 DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH);
3222 }
fac6795d 3223 } else {
b082db07
DG
3224 home_path = get_home_dir();
3225 if (home_path == NULL) {
273ea72c
DG
3226 /* TODO: Add --socket PATH option */
3227 ERR("Can't get HOME directory for sockets creation.");
cf3af59e
MD
3228 ret = -EPERM;
3229 goto error;
b082db07
DG
3230 }
3231
fac6795d 3232 if (strlen(apps_unix_sock_path) == 0) {
d6f42150 3233 snprintf(apps_unix_sock_path, PATH_MAX,
b082db07 3234 DEFAULT_HOME_APPS_UNIX_SOCK, home_path);
fac6795d
DG
3235 }
3236
3237 /* Set the cli tool unix socket path */
3238 if (strlen(client_unix_sock_path) == 0) {
d6f42150 3239 snprintf(client_unix_sock_path, PATH_MAX,
b082db07 3240 DEFAULT_HOME_CLIENT_UNIX_SOCK, home_path);
fac6795d 3241 }
0fdd1e2c
DG
3242
3243 /* Set global SHM for ust */
3244 if (strlen(wait_shm_path) == 0) {
3245 snprintf(wait_shm_path, PATH_MAX,
3246 DEFAULT_HOME_APPS_WAIT_SHM_PATH, geteuid());
3247 }
fac6795d
DG
3248 }
3249
847177cd
DG
3250 DBG("Client socket path %s", client_unix_sock_path);
3251 DBG("Application socket path %s", apps_unix_sock_path);
3252
273ea72c 3253 /*
7d8234d9 3254 * See if daemon already exist.
fac6795d 3255 */
7d8234d9 3256 if ((ret = check_existing_daemon()) < 0) {
75462a81 3257 ERR("Already running daemon.\n");
273ea72c 3258 /*
cf3af59e
MD
3259 * We do not goto exit because we must not cleanup()
3260 * because a daemon is already running.
ab118b20 3261 */
cf3af59e 3262 goto error;
a88df331
DG
3263 }
3264
3265 /* After this point, we can safely call cleanup() so goto error is used */
3266
3267 /*
3268 * These actions must be executed as root. We do that *after* setting up
3269 * the sockets path because we MUST make the check for another daemon using
3270 * those paths *before* trying to set the kernel consumer sockets and init
3271 * kernel tracer.
3272 */
3273 if (is_root) {
3274 ret = set_kconsumerd_sockets();
3275 if (ret < 0) {
cf3af59e 3276 goto exit;
a88df331
DG
3277 }
3278
3279 /* Setup kernel tracer */
3280 init_kernel_tracer();
3281
3282 /* Set ulimit for open files */
3283 set_ulimit();
fac6795d
DG
3284 }
3285
cf3af59e
MD
3286 if ((ret = set_signal_handler()) < 0) {
3287 goto exit;
fac6795d
DG
3288 }
3289
d6f42150 3290 /* Setup the needed unix socket */
cf3af59e
MD
3291 if ((ret = init_daemon_socket()) < 0) {
3292 goto exit;
fac6795d
DG
3293 }
3294
3295 /* Set credentials to socket */
cf3af59e
MD
3296 if (is_root && ((ret = set_permissions()) < 0)) {
3297 goto exit;
fac6795d
DG
3298 }
3299
5b8719f5
DG
3300 /* Get parent pid if -S, --sig-parent is specified. */
3301 if (opt_sig_parent) {
3302 ppid = getppid();
3303 }
3304
7a485870 3305 /* Setup the kernel pipe for waking up the kernel thread */
cf3af59e
MD
3306 if ((ret = create_kernel_poll_pipe()) < 0) {
3307 goto exit;
7a485870
DG
3308 }
3309
099e26bd
DG
3310 /* Setup the thread apps communication pipe. */
3311 if ((ret = create_apps_cmd_pipe()) < 0) {
3312 goto exit;
3313 }
3314
3315 /* Init UST command queue. */
3316 cds_wfq_init(&ust_cmd_queue.queue);
3317
273ea72c
DG
3318 /*
3319 * Get session list pointer. This pointer MUST NOT be free().
3320 * This list is statically declared in session.c
3321 */
b5541356
DG
3322 session_list_ptr = get_session_list();
3323
cf3af59e 3324 /* Create thread to manage the client socket */
099e26bd
DG
3325 ret = pthread_create(&client_thread, NULL,
3326 thread_manage_clients, (void *) NULL);
cf3af59e 3327 if (ret != 0) {
099e26bd 3328 perror("pthread_create clients");
cf3af59e
MD
3329 goto exit_client;
3330 }
fac6795d 3331
099e26bd
DG
3332 /* Create thread to dispatch registration */
3333 ret = pthread_create(&dispatch_thread, NULL,
3334 thread_dispatch_ust_registration, (void *) NULL);
3335 if (ret != 0) {
3336 perror("pthread_create dispatch");
3337 goto exit_dispatch;
3338 }
3339
3340 /* Create thread to manage application registration. */
3341 ret = pthread_create(&reg_apps_thread, NULL,
3342 thread_registration_apps, (void *) NULL);
3343 if (ret != 0) {
3344 perror("pthread_create registration");
3345 goto exit_reg_apps;
3346 }
3347
cf3af59e
MD
3348 /* Create thread to manage application socket */
3349 ret = pthread_create(&apps_thread, NULL, thread_manage_apps, (void *) NULL);
3350 if (ret != 0) {
099e26bd 3351 perror("pthread_create apps");
cf3af59e
MD
3352 goto exit_apps;
3353 }
fac6795d 3354
cf3af59e
MD
3355 /* Create kernel thread to manage kernel event */
3356 ret = pthread_create(&kernel_thread, NULL, thread_manage_kernel, (void *) NULL);
3357 if (ret != 0) {
099e26bd 3358 perror("pthread_create kernel");
cf3af59e
MD
3359 goto exit_kernel;
3360 }
7a485870 3361
b25a8868
DG
3362 tracepoint(sessiond_boot_end);
3363
cf3af59e
MD
3364 ret = pthread_join(kernel_thread, &status);
3365 if (ret != 0) {
3366 perror("pthread_join");
3367 goto error; /* join error, exit without cleanup */
fac6795d
DG
3368 }
3369
cf3af59e
MD
3370exit_kernel:
3371 ret = pthread_join(apps_thread, &status);
3372 if (ret != 0) {
3373 perror("pthread_join");
3374 goto error; /* join error, exit without cleanup */
3375 }
fac6795d 3376
cf3af59e 3377exit_apps:
099e26bd
DG
3378 ret = pthread_join(reg_apps_thread, &status);
3379 if (ret != 0) {
3380 perror("pthread_join");
3381 goto error; /* join error, exit without cleanup */
3382 }
3383
3384exit_reg_apps:
3385 ret = pthread_join(dispatch_thread, &status);
3386 if (ret != 0) {
3387 perror("pthread_join");
3388 goto error; /* join error, exit without cleanup */
3389 }
3390
3391exit_dispatch:
cf3af59e
MD
3392 ret = pthread_join(client_thread, &status);
3393 if (ret != 0) {
3394 perror("pthread_join");
3395 goto error; /* join error, exit without cleanup */
3396 }
3397
3398 ret = join_kconsumerd_thread();
3399 if (ret != 0) {
3400 perror("join_kconsumerd");
3401 goto error; /* join error, exit without cleanup */
3402 }
a88df331 3403
cf3af59e 3404exit_client:
a88df331 3405exit:
cf3af59e
MD
3406 /*
3407 * cleanup() is called when no other thread is running.
3408 */
3409 cleanup();
3410 if (!ret)
3411 exit(EXIT_SUCCESS);
3412error:
5e16da05 3413 exit(EXIT_FAILURE);
fac6795d 3414}
This page took 0.207148 seconds and 4 git commands to generate.