More fine grained time for ust register/notify
[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
DG
933{
934 int i, ret, count;
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;
945
946 while (1) {
947 /* See if we have a valid socket to add to pollfd */
948 if (ust_cmd.sock != -1) {
949 nb_fd++;
950 update_poll_flag = 1;
951 }
952
953 /* The pollfd struct must be updated */
954 if (update_poll_flag) {
0177d773 955 ret = update_apps_cmd_pollfd(nb_fd, ARRAY_SIZE(pollfd), &pollfd);
099e26bd
DG
956 if (ret < 0) {
957 /* malloc failed so we quit */
958 goto error;
959 }
0177d773 960
099e26bd
DG
961 if (ust_cmd.sock != -1) {
962 /* Update pollfd with the new UST socket */
963 DBG("Adding sock %d to apps cmd pollfd", ust_cmd.sock);
964 pollfd[nb_fd - 1].fd = ust_cmd.sock;
0177d773 965 pollfd[nb_fd - 1].events = POLLHUP | POLLNVAL | POLLERR;
099e26bd
DG
966 ust_cmd.sock = -1;
967 }
968 }
969
970 DBG("Apps thread polling on %d fds", nb_fd);
971
85c434ee
DG
972 tracepoint(sessiond_th_apps_poll);
973
099e26bd
DG
974 /* Inifinite blocking call, waiting for transmission */
975 ret = poll(pollfd, nb_fd, -1);
976 if (ret < 0) {
977 perror("poll apps thread");
978 goto error;
979 }
980
981 /* Thread quit pipe has been closed. Killing thread. */
982 if (pollfd[0].revents == POLLNVAL) {
983 goto error;
0177d773
DG
984 } else {
985 /* apps_cmd_pipe pipe events */
986 switch (pollfd[1].revents) {
987 case POLLERR:
988 ERR("Apps command pipe poll error");
099e26bd 989 goto error;
0177d773 990 case POLLIN:
85c434ee 991 tracepoint(ust_register_read_start);
62d53818 992
0177d773
DG
993 /* Empty pipe */
994 ret = read(apps_cmd_pipe[0], &ust_cmd, sizeof(ust_cmd));
995 if (ret < 0 || ret < sizeof(ust_cmd)) {
996 perror("read apps cmd pipe");
997 goto error;
998 }
099e26bd 999
85c434ee
DG
1000 tracepoint(ust_register_read_stop);
1001
1002 tracepoint(ust_register_add_start);
0177d773
DG
1003 /* Register applicaton to the session daemon */
1004 ret = register_traceable_app(&ust_cmd.reg_msg, ust_cmd.sock);
1005 if (ret < 0) {
1006 /* Only critical ENOMEM error can be returned here */
1007 goto error;
1008 }
85c434ee 1009 tracepoint(ust_register_add_stop);
099e26bd 1010
85c434ee 1011 tracepoint(ust_register_done_start);
0177d773
DG
1012 ret = ustctl_register_done(ust_cmd.sock);
1013 if (ret < 0) {
1014 /*
1015 * If the registration is not possible, we simply unregister
1016 * the apps and continue
1017 */
1018 unregister_traceable_app(ust_cmd.sock);
1019 }
62d53818 1020
85c434ee 1021 tracepoint(ust_register_done_stop);
0177d773 1022 break;
099e26bd
DG
1023 }
1024 }
1025
1026 count = nb_fd;
1027 for (i = 2; i < count; i++) {
1028 /* Apps socket is closed/hungup */
1029 switch (pollfd[i].revents) {
0177d773 1030 case POLLERR:
099e26bd 1031 case POLLHUP:
0177d773 1032 case POLLNVAL:
099e26bd
DG
1033 /* Pipe closed */
1034 unregister_traceable_app(pollfd[i].fd);
0177d773
DG
1035 /* Indicate to remove this fd from the pollfd */
1036 pollfd[i].fd = -1;
099e26bd 1037 nb_fd--;
0177d773 1038 break;
099e26bd
DG
1039 }
1040 }
1041
1042 if (nb_fd != count) {
1043 update_poll_flag = 1;
1044 }
1045 }
1046
1047error:
1048 DBG("Application communication apps dying");
1049 close(apps_cmd_pipe[0]);
1050 close(apps_cmd_pipe[1]);
1051
1052 free(pollfd);
1053
1054 return NULL;
1055}
1056
1057/*
1058 * Dispatch request from the registration threads to the application
1059 * communication thread.
1060 */
1061static void *thread_dispatch_ust_registration(void *data)
1062{
1063 int ret;
1064 struct cds_wfq_node *node;
1065 struct ust_command *ust_cmd = NULL;
1066
85c434ee
DG
1067 tracepoint(sessiond_th_dispatch_start);
1068
099e26bd
DG
1069 DBG("[thread] Dispatch UST command started");
1070
1071 while (!dispatch_thread_exit) {
1072 /* Atomically prepare the queue futex */
1073 futex_nto1_prepare(&ust_cmd_queue.futex);
1074
1075 do {
85c434ee
DG
1076 tracepoint(sessiond_th_dispatch_block);
1077
099e26bd
DG
1078 /* Dequeue command for registration */
1079 node = cds_wfq_dequeue_blocking(&ust_cmd_queue.queue);
1080 if (node == NULL) {
1081 DBG("Waked up but nothing in the UST command queue");
1082 /* Continue thread execution */
1083 break;
1084 }
1085
62d53818
DG
1086 tracepoint(ust_dispatch_register_start);
1087
099e26bd
DG
1088 ust_cmd = caa_container_of(node, struct ust_command, node);
1089
2f50c8a3
DG
1090 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1091 " gid:%d sock:%d name:%s (version %d.%d)",
1092 ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid,
1093 ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid,
1094 ust_cmd->sock, ust_cmd->reg_msg.name,
1095 ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor);
099e26bd
DG
1096 /*
1097 * Inform apps thread of the new application registration. This
1098 * call is blocking so we can be assured that the data will be read
1099 * at some point in time or wait to the end of the world :)
1100 */
1101 ret = write(apps_cmd_pipe[1], ust_cmd,
1102 sizeof(struct ust_command));
1103 if (ret < 0) {
1104 perror("write apps cmd pipe");
1105 if (errno == EBADF) {
1106 /*
1107 * We can't inform the application thread to process
1108 * registration. We will exit or else application
1109 * registration will not occur and tracing will never
1110 * start.
1111 */
1112 goto error;
1113 }
1114 }
1115 free(ust_cmd);
1116 } while (node != NULL);
1117
62d53818
DG
1118 tracepoint(ust_dispatch_register_stop);
1119
099e26bd
DG
1120 /* Futex wait on queue. Blocking call on futex() */
1121 futex_nto1_wait(&ust_cmd_queue.futex);
1122 }
1123
1124error:
1125 DBG("Dispatch thread dying");
1126 return NULL;
1127}
1128
1129/*
1130 * This thread manage application registration.
1131 */
1132static void *thread_registration_apps(void *data)
1d4b027a 1133{
273ea72c
DG
1134 int sock = 0, ret;
1135 struct pollfd pollfd[2];
099e26bd
DG
1136 /*
1137 * Get allocated in this thread, enqueued to a global queue, dequeued and
1138 * freed in the manage apps thread.
1139 */
1140 struct ust_command *ust_cmd = NULL;
1d4b027a 1141
62d53818 1142 tracepoint(sessiond_th_reg_start);
b25a8868 1143
099e26bd 1144 DBG("[thread] Manage application registration started");
1d4b027a
DG
1145
1146 ret = lttcomm_listen_unix_sock(apps_sock);
1147 if (ret < 0) {
1148 goto error;
1149 }
1150
273ea72c
DG
1151 /* First fd is always the quit pipe */
1152 pollfd[0].fd = thread_quit_pipe[0];
1153
1154 /* Apps socket */
1155 pollfd[1].fd = apps_sock;
1156 pollfd[1].events = POLLIN;
1157
1d4b027a 1158 /* Notify all applications to register */
0fdd1e2c
DG
1159 ret = notify_ust_apps(1);
1160 if (ret < 0) {
1161 ERR("Failed to notify applications or create the wait shared memory.\n"
1162 "Execution continues but there might be problem for already running\n"
1163 "applications that wishes to register.");
1164 }
1d4b027a
DG
1165
1166 while (1) {
1167 DBG("Accepting application registration");
273ea72c 1168
62d53818 1169 tracepoint(sessiond_th_reg_poll);
b25a8868 1170
273ea72c
DG
1171 /* Inifinite blocking call, waiting for transmission */
1172 ret = poll(pollfd, 2, -1);
1173 if (ret < 0) {
099e26bd 1174 perror("poll register apps thread");
273ea72c
DG
1175 goto error;
1176 }
1177
1178 /* Thread quit pipe has been closed. Killing thread. */
1179 if (pollfd[0].revents == POLLNVAL) {
1180 goto error;
1181 } else if (pollfd[1].revents == POLLERR) {
099e26bd 1182 ERR("Register apps socket poll error");
273ea72c
DG
1183 goto error;
1184 }
1185
62d53818
DG
1186 /* Registration starts here. Recording cycles */
1187 tracepoint(ust_register_start);
1188
1d4b027a
DG
1189 sock = lttcomm_accept_unix_sock(apps_sock);
1190 if (sock < 0) {
1191 goto error;
1192 }
1193
099e26bd
DG
1194 /* Create UST registration command for enqueuing */
1195 ust_cmd = malloc(sizeof(struct ust_command));
1196 if (ust_cmd == NULL) {
1197 perror("ust command malloc");
1198 goto error;
1199 }
1200
273ea72c 1201 /*
099e26bd
DG
1202 * Using message-based transmissions to ensure we don't have to deal
1203 * with partially received messages.
1d4b027a 1204 */
099e26bd
DG
1205 ret = lttcomm_recv_unix_sock(sock, &ust_cmd->reg_msg,
1206 sizeof(struct ust_register_msg));
2f50c8a3
DG
1207 if (ret < 0 || ret < sizeof(struct ust_register_msg)) {
1208 if (ret < 0) {
1209 perror("lttcomm_recv_unix_sock register apps");
1210 } else {
1211 ERR("Wrong size received on apps register");
1212 }
099e26bd
DG
1213 free(ust_cmd);
1214 close(sock);
1d4b027a
DG
1215 continue;
1216 }
1217
099e26bd
DG
1218 ust_cmd->sock = sock;
1219
2f50c8a3
DG
1220 DBG("UST registration received with pid:%d ppid:%d uid:%d"
1221 " gid:%d sock:%d name:%s (version %d.%d)",
1222 ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid,
1223 ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid,
1224 ust_cmd->sock, ust_cmd->reg_msg.name,
1225 ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor);
099e26bd
DG
1226 /*
1227 * Lock free enqueue the registration request.
1228 * The red pill has been taken! This apps will be part of the *system*
1229 */
1230 cds_wfq_enqueue(&ust_cmd_queue.queue, &ust_cmd->node);
1231
1232 /*
1233 * Wake the registration queue futex.
1234 * Implicit memory barrier with the exchange in cds_wfq_enqueue.
1235 */
1236 futex_nto1_wake(&ust_cmd_queue.futex);
62d53818
DG
1237
1238 tracepoint(ust_register_stop);
1d4b027a
DG
1239 }
1240
1241error:
0fdd1e2c
DG
1242 DBG("UST Registration thread dying");
1243
1244 /* Notify that the registration thread is gone */
1245 notify_ust_apps(0);
1246
1247 close(apps_sock);
1248 close(sock);
1d4b027a 1249
273ea72c 1250 unlink(apps_unix_sock_path);
0fdd1e2c 1251
1d4b027a
DG
1252 return NULL;
1253}
1254
8c0faa1d 1255/*
d063d709
DG
1256 * Start the thread_manage_kconsumerd. This must be done after a kconsumerd
1257 * exec or it will fails.
8c0faa1d 1258 */
693bd40b 1259static int spawn_kconsumerd_thread(void)
8c0faa1d
DG
1260{
1261 int ret;
1262
1263 /* Setup semaphore */
1264 sem_init(&kconsumerd_sem, 0, 0);
1265
1d4b027a 1266 ret = pthread_create(&kconsumerd_thread, NULL, thread_manage_kconsumerd, (void *) NULL);
8c0faa1d
DG
1267 if (ret != 0) {
1268 perror("pthread_create kconsumerd");
1269 goto error;
1270 }
1271
693bd40b 1272 /* Wait for the kconsumerd thread to be ready */
8c0faa1d
DG
1273 sem_wait(&kconsumerd_sem);
1274
712ea556
DG
1275 if (kconsumerd_pid == 0) {
1276 ERR("Kconsumerd did not start");
1277 goto error;
1278 }
1279
8c0faa1d
DG
1280 return 0;
1281
1282error:
712ea556 1283 ret = LTTCOMM_KERN_CONSUMER_FAIL;
8c0faa1d
DG
1284 return ret;
1285}
1286
d9800920
DG
1287/*
1288 * Join kernel consumer thread
1289 */
cf3af59e
MD
1290static int join_kconsumerd_thread(void)
1291{
1292 void *status;
1293 int ret;
1294
1295 if (kconsumerd_pid != 0) {
1296 ret = kill(kconsumerd_pid, SIGTERM);
1297 if (ret) {
1298 ERR("Error killing kconsumerd");
1299 return ret;
1300 }
1301 return pthread_join(kconsumerd_thread, &status);
1302 } else {
1303 return 0;
1304 }
1305}
1306
8c0faa1d 1307/*
d063d709 1308 * Fork and exec a kernel consumer daemon (kconsumerd).
8c0faa1d 1309 *
d063d709 1310 * Return pid if successful else -1.
8c0faa1d 1311 */
693bd40b 1312static pid_t spawn_kconsumerd(void)
8c0faa1d
DG
1313{
1314 int ret;
1315 pid_t pid;
53086306 1316 const char *verbosity;
8c0faa1d 1317
c49dc785
DG
1318 DBG("Spawning kconsumerd");
1319
8c0faa1d
DG
1320 pid = fork();
1321 if (pid == 0) {
1322 /*
1323 * Exec kconsumerd.
1324 */
31f73cc9 1325 if (opt_verbose > 1 || opt_verbose_kconsumerd) {
53086306
DG
1326 verbosity = "--verbose";
1327 } else {
1328 verbosity = "--quiet";
1329 }
1330 execl(INSTALL_BIN_PATH "/ltt-kconsumerd", "ltt-kconsumerd", verbosity, NULL);
8c0faa1d
DG
1331 if (errno != 0) {
1332 perror("kernel start consumer exec");
1333 }
1334 exit(EXIT_FAILURE);
1335 } else if (pid > 0) {
1336 ret = pid;
1337 goto error;
1338 } else {
1339 perror("kernel start consumer fork");
1340 ret = -errno;
1341 goto error;
1342 }
1343
1344error:
1345 return ret;
1346}
1347
693bd40b 1348/*
d063d709 1349 * Spawn the kconsumerd daemon and session daemon thread.
693bd40b
DG
1350 */
1351static int start_kconsumerd(void)
1352{
1353 int ret;
1354
693bd40b 1355 pthread_mutex_lock(&kconsumerd_pid_mutex);
c49dc785 1356 if (kconsumerd_pid != 0) {
817153bb 1357 pthread_mutex_unlock(&kconsumerd_pid_mutex);
c49dc785
DG
1358 goto end;
1359 }
693bd40b 1360
c49dc785
DG
1361 ret = spawn_kconsumerd();
1362 if (ret < 0) {
1363 ERR("Spawning kconsumerd failed");
1364 ret = LTTCOMM_KERN_CONSUMER_FAIL;
1365 pthread_mutex_unlock(&kconsumerd_pid_mutex);
1366 goto error;
693bd40b 1367 }
c49dc785
DG
1368
1369 /* Setting up the global kconsumerd_pid */
1370 kconsumerd_pid = ret;
693bd40b
DG
1371 pthread_mutex_unlock(&kconsumerd_pid_mutex);
1372
6f61d021
DG
1373 DBG("Kconsumerd pid %d", ret);
1374
693bd40b 1375 DBG("Spawning kconsumerd thread");
693bd40b
DG
1376 ret = spawn_kconsumerd_thread();
1377 if (ret < 0) {
1378 ERR("Fatal error spawning kconsumerd thread");
693bd40b
DG
1379 goto error;
1380 }
1381
c49dc785 1382end:
693bd40b
DG
1383 return 0;
1384
1385error:
1386 return ret;
1387}
1388
b73401da 1389/*
d063d709 1390 * modprobe_kernel_modules
b73401da
DG
1391 */
1392static int modprobe_kernel_modules(void)
1393{
ab147185 1394 int ret = 0, i;
b73401da
DG
1395 char modprobe[256];
1396
ab147185
MD
1397 for (i = 0; i < ARRAY_SIZE(kernel_modules_list); i++) {
1398 ret = snprintf(modprobe, sizeof(modprobe),
1399 "/sbin/modprobe %s%s",
1400 kernel_modules_list[i].required ? "" : "--quiet ",
1401 kernel_modules_list[i].name);
b73401da
DG
1402 if (ret < 0) {
1403 perror("snprintf modprobe");
1404 goto error;
1405 }
ab147185 1406 modprobe[sizeof(modprobe) - 1] = '\0';
b73401da 1407 ret = system(modprobe);
ab147185
MD
1408 if (ret == -1) {
1409 ERR("Unable to launch modprobe for module %s",
1410 kernel_modules_list[i].name);
1411 } else if (kernel_modules_list[i].required
d9800920 1412 && WEXITSTATUS(ret) != 0) {
ab147185
MD
1413 ERR("Unable to load module %s",
1414 kernel_modules_list[i].name);
1415 } else {
1416 DBG("Modprobe successfully %s",
1417 kernel_modules_list[i].name);
1418 }
1419 }
1420
1421error:
1422 return ret;
1423}
1424
b73401da 1425/*
d063d709 1426 * mount_debugfs
b73401da
DG
1427 */
1428static int mount_debugfs(char *path)
1429{
1430 int ret;
1431 char *type = "debugfs";
1432
996b65c8 1433 ret = mkdir_recursive(path, S_IRWXU | S_IRWXG, geteuid(), getegid());
b73401da
DG
1434 if (ret < 0) {
1435 goto error;
1436 }
1437
1438 ret = mount(type, path, type, 0, NULL);
1439 if (ret < 0) {
1440 perror("mount debugfs");
1441 goto error;
1442 }
1443
1444 DBG("Mounted debugfs successfully at %s", path);
1445
1446error:
1447 return ret;
1448}
1449
8c0faa1d 1450/*
d063d709 1451 * Setup necessary data for kernel tracer action.
8c0faa1d 1452 */
f3ed775e 1453static void init_kernel_tracer(void)
8c0faa1d 1454{
b73401da
DG
1455 int ret;
1456 char *proc_mounts = "/proc/mounts";
1457 char line[256];
1458 char *debugfs_path = NULL, *lttng_path;
1459 FILE *fp;
1460
1461 /* Detect debugfs */
1462 fp = fopen(proc_mounts, "r");
1463 if (fp == NULL) {
1464 ERR("Unable to probe %s", proc_mounts);
1465 goto error;
1466 }
1467
1468 while (fgets(line, sizeof(line), fp) != NULL) {
1469 if (strstr(line, "debugfs") != NULL) {
1470 /* Remove first string */
1471 strtok(line, " ");
1472 /* Dup string here so we can reuse line later on */
1473 debugfs_path = strdup(strtok(NULL, " "));
1474 DBG("Got debugfs path : %s", debugfs_path);
1475 break;
1476 }
1477 }
1478
1479 fclose(fp);
1480
1481 /* Mount debugfs if needded */
1482 if (debugfs_path == NULL) {
1483 ret = asprintf(&debugfs_path, "/mnt/debugfs");
1484 if (ret < 0) {
1485 perror("asprintf debugfs path");
1486 goto error;
1487 }
1488 ret = mount_debugfs(debugfs_path);
1489 if (ret < 0) {
1490 goto error;
1491 }
1492 }
1493
1494 /* Modprobe lttng kernel modules */
1495 ret = modprobe_kernel_modules();
1496 if (ret < 0) {
1497 goto error;
1498 }
1499
1500 /* Setup lttng kernel path */
1501 ret = asprintf(&lttng_path, "%s/lttng", debugfs_path);
1502 if (ret < 0) {
1503 perror("asprintf lttng path");
1504 goto error;
1505 }
1506
1507 /* Open debugfs lttng */
1508 kernel_tracer_fd = open(lttng_path, O_RDWR);
f3ed775e 1509 if (kernel_tracer_fd < 0) {
b73401da
DG
1510 DBG("Failed to open %s", lttng_path);
1511 goto error;
8c0faa1d
DG
1512 }
1513
b73401da
DG
1514 free(lttng_path);
1515 free(debugfs_path);
f3ed775e 1516 DBG("Kernel tracer fd %d", kernel_tracer_fd);
b73401da
DG
1517 return;
1518
1519error:
1520 if (lttng_path) {
1521 free(lttng_path);
1522 }
1523 if (debugfs_path) {
1524 free(debugfs_path);
1525 }
1526 WARN("No kernel tracer available");
1527 kernel_tracer_fd = 0;
1528 return;
f3ed775e 1529}
33a2b854 1530
f3ed775e 1531/*
d063d709
DG
1532 * Start tracing by creating trace directory and sending FDs to the kernel
1533 * consumer.
f3ed775e
DG
1534 */
1535static int start_kernel_trace(struct ltt_kernel_session *session)
1536{
f40799e8 1537 int ret = 0;
8c0faa1d 1538
f3ed775e 1539 if (session->kconsumer_fds_sent == 0) {
d9800920
DG
1540 /*
1541 * Assign default kernel consumer if no consumer assigned to the kernel
1542 * session. At this point, it's NOT suppose to be 0 but this is an extra
1543 * security check.
1544 */
1545 if (session->consumer_fd == 0) {
1546 session->consumer_fd = kconsumerd_cmd_sock;
1547 }
1548
1549 ret = send_kconsumerd_fds(session);
f3ed775e
DG
1550 if (ret < 0) {
1551 ERR("Send kconsumerd fds failed");
1552 ret = LTTCOMM_KERN_CONSUMER_FAIL;
1553 goto error;
1554 }
1d4b027a 1555
f3ed775e
DG
1556 session->kconsumer_fds_sent = 1;
1557 }
1d4b027a
DG
1558
1559error:
1560 return ret;
8c0faa1d
DG
1561}
1562
7a485870
DG
1563/*
1564 * Notify kernel thread to update it's pollfd.
1565 */
1566static int notify_kernel_pollfd(void)
1567{
1568 int ret;
1569
1570 /* Inform kernel thread of the new kernel channel */
1571 ret = write(kernel_poll_pipe[1], "!", 1);
1572 if (ret < 0) {
1573 perror("write kernel poll pipe");
1574 }
1575
1576 return ret;
1577}
1578
8c0faa1d 1579/*
d063d709 1580 * Allocate a channel structure and fill it.
8c0faa1d 1581 */
b389abbe
MD
1582static struct lttng_channel *init_default_channel(enum lttng_domain_type domain_type,
1583 char *name)
8c0faa1d 1584{
f3ed775e 1585 struct lttng_channel *chan;
1d4b027a 1586
f3ed775e
DG
1587 chan = malloc(sizeof(struct lttng_channel));
1588 if (chan == NULL) {
1589 perror("init channel malloc");
1590 goto error;
8c0faa1d 1591 }
1d4b027a 1592
ed8f384d 1593 if (snprintf(chan->name, NAME_MAX, "%s", name) < 0) {
9f19cc17 1594 perror("snprintf channel name");
b389abbe 1595 goto error;
f3ed775e
DG
1596 }
1597
1598 chan->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
f3ed775e
DG
1599 chan->attr.switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
1600 chan->attr.read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
1d4b027a 1601
b389abbe
MD
1602 switch (domain_type) {
1603 case LTTNG_DOMAIN_KERNEL:
1604 chan->attr.subbuf_size = DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE;
1605 chan->attr.num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM;
1606 chan->attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
1607 break;
1608 /* TODO: add UST */
1609 default:
1610 goto error; /* Not implemented */
1611 }
1612
f3ed775e 1613 return chan;
b389abbe
MD
1614
1615error:
1616 free(chan);
1617 return NULL;
8c0faa1d
DG
1618}
1619
0177d773
DG
1620/*
1621 * Create an UST session and add it to the session ust list.
1622 */
1623static int create_ust_session(pid_t pid, struct ltt_session *session)
1624{
1625 int ret = -1;
1626 struct ltt_ust_session *lus;
1627
1628 DBG("Creating UST session");
1629
1630 lus = trace_ust_create_session(session->path, pid);
1631 if (lus == NULL) {
1632 goto error;
1633 }
1634
1635 ret = mkdir_recursive(lus->path, S_IRWXU | S_IRWXG,
1636 geteuid(), allowed_group());
1637 if (ret < 0) {
1638 if (ret != -EEXIST) {
1639 ERR("Trace directory creation error");
1640 goto error;
1641 }
1642 }
1643
1644 /* Create session on the UST tracer */
1645 ret = ustctl_create_session(lus);
1646 if (ret < 0) {
1647 goto error;
1648 }
1649
1650 return 0;
1651
1652error:
1653 free(lus);
1654 return ret;
1655}
1656
333f285e 1657/*
d063d709 1658 * Create a kernel tracer session then create the default channel.
333f285e 1659 */
f3ed775e 1660static int create_kernel_session(struct ltt_session *session)
333f285e 1661{
f3ed775e 1662 int ret;
f3ed775e
DG
1663
1664 DBG("Creating kernel session");
1665
1666 ret = kernel_create_session(session, kernel_tracer_fd);
1667 if (ret < 0) {
1668 ret = LTTCOMM_KERN_SESS_FAIL;
1669 goto error;
333f285e
DG
1670 }
1671
d9800920
DG
1672 /* Set kernel consumer socket fd */
1673 if (kconsumerd_cmd_sock) {
1674 session->kernel_session->consumer_fd = kconsumerd_cmd_sock;
1675 }
1676
63053e7c
DG
1677 ret = mkdir_recursive(session->kernel_session->trace_path,
1678 S_IRWXU | S_IRWXG, geteuid(), allowed_group());
7a485870 1679 if (ret < 0) {
996b65c8 1680 if (ret != -EEXIST) {
7a485870
DG
1681 ERR("Trace directory creation error");
1682 goto error;
1683 }
1684 }
1685
f3ed775e
DG
1686error:
1687 return ret;
333f285e
DG
1688}
1689
6c9cc2ab
DG
1690/*
1691 * Using the session list, filled a lttng_session array to send back to the
1692 * client for session listing.
1693 *
1694 * The session list lock MUST be acquired before calling this function. Use
1695 * lock_session_list() and unlock_session_list().
1696 */
1697static void list_lttng_sessions(struct lttng_session *sessions)
1698{
1699 int i = 0;
1700 struct ltt_session *session;
1701
1702 DBG("Getting all available session");
1703 /*
1704 * Iterate over session list and append data after the control struct in
1705 * the buffer.
1706 */
1707 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
1708 strncpy(sessions[i].path, session->path, PATH_MAX);
99497cd0 1709 sessions[i].path[PATH_MAX - 1] = '\0';
6c9cc2ab 1710 strncpy(sessions[i].name, session->name, NAME_MAX);
99497cd0 1711 sessions[i].name[NAME_MAX - 1] = '\0';
6c9cc2ab
DG
1712 i++;
1713 }
1714}
1715
9f19cc17
DG
1716/*
1717 * Fill lttng_channel array of all channels.
1718 */
1719static void list_lttng_channels(struct ltt_session *session,
1720 struct lttng_channel *channels)
1721{
1722 int i = 0;
1723 struct ltt_kernel_channel *kchan;
1724
1725 DBG("Listing channels for session %s", session->name);
1726
1727 /* Kernel channels */
1728 if (session->kernel_session != NULL) {
1729 cds_list_for_each_entry(kchan, &session->kernel_session->channel_list.head, list) {
1730 /* Copy lttng_channel struct to array */
1731 memcpy(&channels[i], kchan->channel, sizeof(struct lttng_channel));
1732 channels[i].enabled = kchan->enabled;
1733 i++;
1734 }
1735 }
1736
1737 /* TODO: Missing UST listing */
1738}
1739
1740/*
1741 * Fill lttng_event array of all events in the channel.
1742 */
1743static void list_lttng_events(struct ltt_kernel_channel *kchan,
1744 struct lttng_event *events)
1745{
1746 /*
1747 * TODO: This is ONLY kernel. Need UST support.
1748 */
1749 int i = 0;
1750 struct ltt_kernel_event *event;
1751
1752 DBG("Listing events for channel %s", kchan->channel->name);
1753
1754 /* Kernel channels */
1755 cds_list_for_each_entry(event, &kchan->events_list.head , list) {
1756 strncpy(events[i].name, event->event->name, LTTNG_SYMBOL_NAME_LEN);
99497cd0 1757 events[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
9f19cc17
DG
1758 events[i].enabled = event->enabled;
1759 switch (event->event->instrumentation) {
1760 case LTTNG_KERNEL_TRACEPOINT:
1761 events[i].type = LTTNG_EVENT_TRACEPOINT;
1762 break;
1763 case LTTNG_KERNEL_KPROBE:
1764 case LTTNG_KERNEL_KRETPROBE:
1765 events[i].type = LTTNG_EVENT_PROBE;
1766 memcpy(&events[i].attr.probe, &event->event->u.kprobe,
1767 sizeof(struct lttng_kernel_kprobe));
1768 break;
1769 case LTTNG_KERNEL_FUNCTION:
1770 events[i].type = LTTNG_EVENT_FUNCTION;
1771 memcpy(&events[i].attr.ftrace, &event->event->u.ftrace,
1772 sizeof(struct lttng_kernel_function));
1773 break;
1774 }
1775 i++;
1776 }
1777}
1778
fac6795d 1779/*
d063d709
DG
1780 * Process the command requested by the lttng client within the command
1781 * context structure. This function make sure that the return structure (llm)
1782 * is set and ready for transmission before returning.
fac6795d 1783 *
e065084a 1784 * Return any error encountered or 0 for success.
fac6795d 1785 */
5461b305 1786static int process_client_msg(struct command_ctx *cmd_ctx)
fac6795d 1787{
f40799e8 1788 int ret = LTTCOMM_OK;
fac6795d 1789
5461b305 1790 DBG("Processing client command %d", cmd_ctx->lsm->cmd_type);
fac6795d 1791
63053e7c
DG
1792 /*
1793 * Commands that DO NOT need a session.
1794 */
5461b305 1795 switch (cmd_ctx->lsm->cmd_type) {
5e16da05
MD
1796 case LTTNG_CREATE_SESSION:
1797 case LTTNG_LIST_SESSIONS:
052da939 1798 case LTTNG_LIST_TRACEPOINTS:
d0254c7c 1799 case LTTNG_CALIBRATE:
5e16da05
MD
1800 break;
1801 default:
42abccdb
DG
1802 DBG("Getting session %s by name", cmd_ctx->lsm->session.name);
1803 cmd_ctx->session = find_session_by_name(cmd_ctx->lsm->session.name);
5461b305 1804 if (cmd_ctx->session == NULL) {
f3ed775e 1805 /* If session name not found */
42abccdb 1806 if (cmd_ctx->lsm->session.name != NULL) {
f3ed775e
DG
1807 ret = LTTCOMM_SESS_NOT_FOUND;
1808 } else { /* If no session name specified */
1809 ret = LTTCOMM_SELECT_SESS;
1810 }
5461b305 1811 goto error;
b5541356
DG
1812 } else {
1813 /* Acquire lock for the session */
1814 lock_session(cmd_ctx->session);
379473d2 1815 }
5e16da05 1816 break;
379473d2
DG
1817 }
1818
f3ed775e 1819 /*
d0254c7c 1820 * Check domain type for specific "pre-action".
f3ed775e 1821 */
0d0c377a
DG
1822 switch (cmd_ctx->lsm->domain.type) {
1823 case LTTNG_DOMAIN_KERNEL:
333f285e 1824 /* Kernel tracer check */
20fe2104 1825 if (kernel_tracer_fd == 0) {
333f285e
DG
1826 init_kernel_tracer();
1827 if (kernel_tracer_fd == 0) {
1828 ret = LTTCOMM_KERN_NA;
1829 goto error;
1830 }
20fe2104 1831 }
f3ed775e 1832 /* Need a session for kernel command */
d0254c7c 1833 switch (cmd_ctx->lsm->cmd_type) {
d9800920 1834 case LTTNG_CALIBRATE:
d0254c7c
MD
1835 case LTTNG_CREATE_SESSION:
1836 case LTTNG_LIST_SESSIONS:
1837 case LTTNG_LIST_TRACEPOINTS:
d0254c7c
MD
1838 break;
1839 default:
1840 if (cmd_ctx->session->kernel_session == NULL) {
1841 ret = create_kernel_session(cmd_ctx->session);
f3ed775e 1842 if (ret < 0) {
d0254c7c 1843 ret = LTTCOMM_KERN_SESS_FAIL;
f3ed775e
DG
1844 goto error;
1845 }
d0254c7c 1846 /* Start the kernel consumer daemon */
d9800920
DG
1847 if (kconsumerd_pid == 0 &&
1848 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
d0254c7c
MD
1849 ret = start_kconsumerd();
1850 if (ret < 0) {
1851 goto error;
1852 }
1853 }
f3ed775e
DG
1854 }
1855 }
0d0c377a 1856 break;
0fdd1e2c
DG
1857 case LTTNG_DOMAIN_UST_PID:
1858 break;
0d0c377a
DG
1859 default:
1860 break;
20fe2104
DG
1861 }
1862
fac6795d 1863 /* Process by command type */
5461b305 1864 switch (cmd_ctx->lsm->cmd_type) {
b579acd9 1865 case LTTNG_ADD_CONTEXT:
d65106b1 1866 {
b579acd9 1867 struct lttng_kernel_context kctx;
d65106b1
DG
1868
1869 /* Setup lttng message with no payload */
1870 ret = setup_lttng_msg(cmd_ctx, 0);
1871 if (ret < 0) {
1872 goto setup_error;
1873 }
1874
b579acd9
DG
1875 switch (cmd_ctx->lsm->domain.type) {
1876 case LTTNG_DOMAIN_KERNEL:
1877 /* Create Kernel context */
1878 kctx.ctx = cmd_ctx->lsm->u.context.ctx.ctx;
1879 kctx.u.perf_counter.type = cmd_ctx->lsm->u.context.ctx.u.perf_counter.type;
1880 kctx.u.perf_counter.config = cmd_ctx->lsm->u.context.ctx.u.perf_counter.config;
1881 strncpy(kctx.u.perf_counter.name,
1882 cmd_ctx->lsm->u.context.ctx.u.perf_counter.name,
1883 LTTNG_SYMBOL_NAME_LEN);
99497cd0 1884 kctx.u.perf_counter.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
b579acd9
DG
1885
1886 /* Add kernel context to kernel tracer. See context.c */
1887 ret = add_kernel_context(cmd_ctx->session->kernel_session, &kctx,
1888 cmd_ctx->lsm->u.context.event_name,
1889 cmd_ctx->lsm->u.context.channel_name);
1890 if (ret != LTTCOMM_OK) {
d65106b1
DG
1891 goto error;
1892 }
b579acd9
DG
1893 break;
1894 default:
1895 /* TODO: Userspace tracing */
1896 ret = LTTCOMM_NOT_IMPLEMENTED;
0b97ec54 1897 goto error;
d65106b1
DG
1898 }
1899
1900 ret = LTTCOMM_OK;
1901 break;
1902 }
0b97ec54 1903 case LTTNG_DISABLE_CHANNEL:
26cc6b4e 1904 {
0b97ec54 1905 struct ltt_kernel_channel *kchan;
26cc6b4e
DG
1906
1907 /* Setup lttng message with no payload */
1908 ret = setup_lttng_msg(cmd_ctx, 0);
1909 if (ret < 0) {
1910 goto setup_error;
1911 }
1912
0b97ec54
DG
1913 switch (cmd_ctx->lsm->domain.type) {
1914 case LTTNG_DOMAIN_KERNEL:
62499ad6 1915 kchan = trace_kernel_get_channel_by_name(cmd_ctx->lsm->u.disable.channel_name,
0b97ec54
DG
1916 cmd_ctx->session->kernel_session);
1917 if (kchan == NULL) {
1918 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
26cc6b4e 1919 goto error;
0b97ec54
DG
1920 } else if (kchan->enabled == 1) {
1921 ret = kernel_disable_channel(kchan);
1922 if (ret < 0) {
1923 if (ret != EEXIST) {
1924 ret = LTTCOMM_KERN_CHAN_DISABLE_FAIL;
1925 }
1926 goto error;
1927 }
26cc6b4e 1928 }
0b97ec54
DG
1929 kernel_wait_quiescent(kernel_tracer_fd);
1930 break;
1931 default:
1932 /* TODO: Userspace tracing */
1933 ret = LTTCOMM_NOT_IMPLEMENTED;
1934 goto error;
26cc6b4e
DG
1935 }
1936
26cc6b4e
DG
1937 ret = LTTCOMM_OK;
1938 break;
1939 }
f5177a38 1940 case LTTNG_DISABLE_EVENT:
e953ef25 1941 {
f5177a38
DG
1942 struct ltt_kernel_channel *kchan;
1943 struct ltt_kernel_event *kevent;
e953ef25
DG
1944
1945 /* Setup lttng message with no payload */
1946 ret = setup_lttng_msg(cmd_ctx, 0);
1947 if (ret < 0) {
1948 goto setup_error;
1949 }
1950
f5177a38
DG
1951 switch (cmd_ctx->lsm->domain.type) {
1952 case LTTNG_DOMAIN_KERNEL:
62499ad6 1953 kchan = trace_kernel_get_channel_by_name(cmd_ctx->lsm->u.disable.channel_name,
f5177a38
DG
1954 cmd_ctx->session->kernel_session);
1955 if (kchan == NULL) {
1956 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
19e70852 1957 goto error;
e953ef25 1958 }
f5177a38 1959
62499ad6 1960 kevent = trace_kernel_get_event_by_name(cmd_ctx->lsm->u.disable.name, kchan);
f5177a38
DG
1961 if (kevent != NULL) {
1962 DBG("Disabling kernel event %s for channel %s.", kevent->event->name,
1963 kchan->channel->name);
1964 ret = kernel_disable_event(kevent);
1965 if (ret < 0) {
1966 ret = LTTCOMM_KERN_ENABLE_FAIL;
1967 goto error;
1968 }
1969 }
1970
1971 kernel_wait_quiescent(kernel_tracer_fd);
1972 break;
1973 default:
1974 /* TODO: Userspace tracing */
1975 ret = LTTCOMM_NOT_IMPLEMENTED;
1976 goto error;
e953ef25
DG
1977 }
1978
19e70852 1979 ret = LTTCOMM_OK;
e953ef25
DG
1980 break;
1981 }
f5177a38 1982 case LTTNG_DISABLE_ALL_EVENT:
950131af 1983 {
f5177a38
DG
1984 struct ltt_kernel_channel *kchan;
1985 struct ltt_kernel_event *kevent;
950131af
DG
1986
1987 /* Setup lttng message with no payload */
1988 ret = setup_lttng_msg(cmd_ctx, 0);
1989 if (ret < 0) {
1990 goto setup_error;
1991 }
1992
f5177a38
DG
1993 switch (cmd_ctx->lsm->domain.type) {
1994 case LTTNG_DOMAIN_KERNEL:
1995 DBG("Disabling all enabled kernel events");
62499ad6 1996 kchan = trace_kernel_get_channel_by_name(cmd_ctx->lsm->u.disable.channel_name,
f5177a38
DG
1997 cmd_ctx->session->kernel_session);
1998 if (kchan == NULL) {
1999 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
2000 goto error;
2001 }
950131af 2002
f5177a38
DG
2003 /* For each event in the kernel session */
2004 cds_list_for_each_entry(kevent, &kchan->events_list.head, list) {
2005 DBG("Disabling kernel event %s for channel %s.",
2006 kevent->event->name, kchan->channel->name);
2007 ret = kernel_disable_event(kevent);
2008 if (ret < 0) {
2009 continue;
2010 }
950131af 2011 }
f5177a38
DG
2012
2013 /* Quiescent wait after event disable */
2014 kernel_wait_quiescent(kernel_tracer_fd);
2015 break;
2016 default:
2017 /* TODO: Userspace tracing */
2018 ret = LTTCOMM_NOT_IMPLEMENTED;
2019 goto error;
950131af
DG
2020 }
2021
950131af
DG
2022 ret = LTTCOMM_OK;
2023 break;
2024 }
0d0c377a 2025 case LTTNG_ENABLE_CHANNEL:
d36b8583 2026 {
0d0c377a 2027 struct ltt_kernel_channel *kchan;
d36b8583
DG
2028
2029 /* Setup lttng message with no payload */
2030 ret = setup_lttng_msg(cmd_ctx, 0);
2031 if (ret < 0) {
2032 goto setup_error;
2033 }
2034
0d0c377a
DG
2035 switch (cmd_ctx->lsm->domain.type) {
2036 case LTTNG_DOMAIN_KERNEL:
62499ad6 2037 kchan = trace_kernel_get_channel_by_name(cmd_ctx->lsm->u.enable.channel_name,
0d0c377a
DG
2038 cmd_ctx->session->kernel_session);
2039 if (kchan == NULL) {
2040 /* Channel not found, creating it */
2041 DBG("Creating kernel channel");
7d29a247 2042
0d0c377a 2043 ret = kernel_create_channel(cmd_ctx->session->kernel_session,
63053e7c
DG
2044 &cmd_ctx->lsm->u.channel.chan,
2045 cmd_ctx->session->kernel_session->trace_path);
0d0c377a
DG
2046 if (ret < 0) {
2047 ret = LTTCOMM_KERN_CHAN_FAIL;
2048 goto error;
2049 }
7d29a247 2050
0d0c377a
DG
2051 /* Notify kernel thread that there is a new channel */
2052 ret = notify_kernel_pollfd();
2053 if (ret < 0) {
2054 ret = LTTCOMM_FATAL;
2055 goto error;
2056 }
2057 } else if (kchan->enabled == 0) {
2058 ret = kernel_enable_channel(kchan);
2059 if (ret < 0) {
2060 if (ret != EEXIST) {
2061 ret = LTTCOMM_KERN_CHAN_ENABLE_FAIL;
2062 }
2063 goto error;
d36b8583 2064 }
d36b8583 2065 }
0d0c377a
DG
2066
2067 kernel_wait_quiescent(kernel_tracer_fd);
2068 break;
0177d773 2069 case LTTNG_DOMAIN_UST_PID:
0fdd1e2c 2070
0d0c377a
DG
2071 break;
2072 default:
0d0c377a
DG
2073 ret = LTTCOMM_NOT_IMPLEMENTED;
2074 goto error;
d36b8583
DG
2075 }
2076
d36b8583
DG
2077 ret = LTTCOMM_OK;
2078 break;
2079 }
0d0c377a 2080 case LTTNG_ENABLE_EVENT:
894be886 2081 {
7d29a247
DG
2082 char *channel_name;
2083 struct ltt_kernel_channel *kchan;
0d0c377a 2084 struct ltt_kernel_event *kevent;
7d29a247 2085 struct lttng_channel *chan;
f3ed775e 2086
894be886
DG
2087 /* Setup lttng message with no payload */
2088 ret = setup_lttng_msg(cmd_ctx, 0);
2089 if (ret < 0) {
2090 goto setup_error;
2091 }
2092
7d29a247
DG
2093 channel_name = cmd_ctx->lsm->u.enable.channel_name;
2094
0d0c377a
DG
2095 switch (cmd_ctx->lsm->domain.type) {
2096 case LTTNG_DOMAIN_KERNEL:
62499ad6 2097 kchan = trace_kernel_get_channel_by_name(channel_name,
b389abbe
MD
2098 cmd_ctx->session->kernel_session);
2099 if (kchan == NULL) {
2100 DBG("Channel not found. Creating channel %s", channel_name);
2101
2102 chan = init_default_channel(cmd_ctx->lsm->domain.type, channel_name);
2103 if (chan == NULL) {
2104 ret = LTTCOMM_FATAL;
2105 goto error;
2106 }
2107
2108 ret = kernel_create_channel(cmd_ctx->session->kernel_session,
2109 chan, cmd_ctx->session->kernel_session->trace_path);
2110 if (ret < 0) {
2111 ret = LTTCOMM_KERN_CHAN_FAIL;
2112 goto error;
2113 }
62499ad6 2114 kchan = trace_kernel_get_channel_by_name(channel_name,
0d0c377a
DG
2115 cmd_ctx->session->kernel_session);
2116 if (kchan == NULL) {
b389abbe
MD
2117 ERR("Channel %s not found after creation. Internal error, giving up.",
2118 channel_name);
2119 ret = LTTCOMM_FATAL;
2120 goto error;
7d29a247 2121 }
b389abbe 2122 }
7d29a247 2123
62499ad6 2124 kevent = trace_kernel_get_event_by_name(cmd_ctx->lsm->u.enable.event.name, kchan);
0d0c377a
DG
2125 if (kevent == NULL) {
2126 DBG("Creating kernel event %s for channel %s.",
2127 cmd_ctx->lsm->u.enable.event.name, channel_name);
2128 ret = kernel_create_event(&cmd_ctx->lsm->u.enable.event, kchan);
2129 } else {
2130 DBG("Enabling kernel event %s for channel %s.",
2131 kevent->event->name, channel_name);
2132 ret = kernel_enable_event(kevent);
2133 if (ret == -EEXIST) {
2134 ret = LTTCOMM_KERN_EVENT_EXIST;
7d29a247
DG
2135 goto error;
2136 }
2137 }
f34daff7 2138
0d0c377a
DG
2139 if (ret < 0) {
2140 ret = LTTCOMM_KERN_ENABLE_FAIL;
7d29a247
DG
2141 goto error;
2142 }
19e70852 2143
0d0c377a
DG
2144 kernel_wait_quiescent(kernel_tracer_fd);
2145 break;
2146 default:
2147 /* TODO: Userspace tracing */
2148 ret = LTTCOMM_NOT_IMPLEMENTED;
19e70852 2149 goto error;
f3ed775e 2150 }
19e70852 2151 ret = LTTCOMM_OK;
894be886
DG
2152 break;
2153 }
0d0c377a 2154 case LTTNG_ENABLE_ALL_EVENT:
33a2b854 2155 {
9f19cc17
DG
2156 int size, i;
2157 char *channel_name;
7d29a247 2158 struct ltt_kernel_channel *kchan;
0d0c377a 2159 struct ltt_kernel_event *kevent;
9f19cc17 2160 struct lttng_event *event_list;
7d29a247 2161 struct lttng_channel *chan;
33a2b854
DG
2162
2163 /* Setup lttng message with no payload */
2164 ret = setup_lttng_msg(cmd_ctx, 0);
2165 if (ret < 0) {
2166 goto setup_error;
2167 }
2168
2169 DBG("Enabling all kernel event");
2170
7d29a247
DG
2171 channel_name = cmd_ctx->lsm->u.enable.channel_name;
2172
0d0c377a
DG
2173 switch (cmd_ctx->lsm->domain.type) {
2174 case LTTNG_DOMAIN_KERNEL:
62499ad6 2175 kchan = trace_kernel_get_channel_by_name(channel_name,
b389abbe
MD
2176 cmd_ctx->session->kernel_session);
2177 if (kchan == NULL) {
2178 DBG("Channel not found. Creating channel %s", channel_name);
2179
2180 chan = init_default_channel(cmd_ctx->lsm->domain.type, channel_name);
2181 if (chan == NULL) {
2182 ret = LTTCOMM_FATAL;
2183 goto error;
2184 }
2185
2186 ret = kernel_create_channel(cmd_ctx->session->kernel_session,
2187 chan, cmd_ctx->session->kernel_session->trace_path);
2188 if (ret < 0) {
2189 ret = LTTCOMM_KERN_CHAN_FAIL;
2190 goto error;
2191 }
62499ad6 2192 kchan = trace_kernel_get_channel_by_name(channel_name,
0d0c377a
DG
2193 cmd_ctx->session->kernel_session);
2194 if (kchan == NULL) {
b389abbe
MD
2195 ERR("Channel %s not found after creation. Internal error, giving up.",
2196 channel_name);
2197 ret = LTTCOMM_FATAL;
2198 goto error;
7d29a247 2199 }
b389abbe 2200 }
7d29a247 2201
0d0c377a
DG
2202 /* For each event in the kernel session */
2203 cds_list_for_each_entry(kevent, &kchan->events_list.head, list) {
2204 DBG("Enabling kernel event %s for channel %s.",
2205 kevent->event->name, channel_name);
2206 ret = kernel_enable_event(kevent);
7d29a247 2207 if (ret < 0) {
0d0c377a 2208 continue;
7d29a247
DG
2209 }
2210 }
33a2b854 2211
0d0c377a
DG
2212 size = kernel_list_events(kernel_tracer_fd, &event_list);
2213 if (size < 0) {
2214 ret = LTTCOMM_KERN_LIST_FAIL;
2215 goto error;
f3ed775e 2216 }
f3ed775e 2217
0d0c377a 2218 for (i = 0; i < size; i++) {
62499ad6 2219 kevent = trace_kernel_get_event_by_name(event_list[i].name, kchan);
0d0c377a
DG
2220 if (kevent == NULL) {
2221 /* Default event type for enable all */
2222 event_list[i].type = LTTNG_EVENT_TRACEPOINT;
2223 /* Enable each single tracepoint event */
2224 ret = kernel_create_event(&event_list[i], kchan);
2225 if (ret < 0) {
2226 /* Ignore error here and continue */
2227 }
950131af 2228 }
33a2b854 2229 }
33a2b854 2230
0d0c377a
DG
2231 free(event_list);
2232
2233 /* Quiescent wait after event enable */
2234 kernel_wait_quiescent(kernel_tracer_fd);
2235 break;
2236 default:
2237 /* TODO: Userspace tracing */
2238 ret = LTTCOMM_NOT_IMPLEMENTED;
2239 goto error;
2240 }
33a2b854
DG
2241
2242 ret = LTTCOMM_OK;
2243 break;
2244 }
052da939 2245 case LTTNG_LIST_TRACEPOINTS:
2ef84c95 2246 {
9f19cc17 2247 struct lttng_event *events;
052da939
DG
2248 ssize_t nb_events = 0;
2249
2250 switch (cmd_ctx->lsm->domain.type) {
2251 case LTTNG_DOMAIN_KERNEL:
2252 DBG("Listing kernel events");
2253 nb_events = kernel_list_events(kernel_tracer_fd, &events);
2254 if (nb_events < 0) {
2255 ret = LTTCOMM_KERN_LIST_FAIL;
2256 goto error;
2257 }
2258 break;
2259 default:
2260 /* TODO: Userspace listing */
2261 ret = LTTCOMM_NOT_IMPLEMENTED;
2262 break;
2ef84c95
DG
2263 }
2264
2265 /*
2266 * Setup lttng message with payload size set to the event list size in
2267 * bytes and then copy list into the llm payload.
2268 */
052da939 2269 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_event) * nb_events);
2ef84c95 2270 if (ret < 0) {
052da939 2271 free(events);
2ef84c95
DG
2272 goto setup_error;
2273 }
2274
2275 /* Copy event list into message payload */
9f19cc17 2276 memcpy(cmd_ctx->llm->payload, events,
052da939 2277 sizeof(struct lttng_event) * nb_events);
2ef84c95 2278
9f19cc17 2279 free(events);
2ef84c95
DG
2280
2281 ret = LTTCOMM_OK;
2282 break;
2283 }
f3ed775e 2284 case LTTNG_START_TRACE:
8c0faa1d
DG
2285 {
2286 struct ltt_kernel_channel *chan;
f3ed775e 2287
8c0faa1d
DG
2288 /* Setup lttng message with no payload */
2289 ret = setup_lttng_msg(cmd_ctx, 0);
2290 if (ret < 0) {
2291 goto setup_error;
2292 }
2293
f3ed775e
DG
2294 /* Kernel tracing */
2295 if (cmd_ctx->session->kernel_session != NULL) {
2296 if (cmd_ctx->session->kernel_session->metadata == NULL) {
2297 DBG("Open kernel metadata");
58a97671 2298 ret = kernel_open_metadata(cmd_ctx->session->kernel_session,
63053e7c 2299 cmd_ctx->session->kernel_session->trace_path);
f3ed775e
DG
2300 if (ret < 0) {
2301 ret = LTTCOMM_KERN_META_FAIL;
2302 goto error;
2303 }
2304 }
8c0faa1d 2305
f3ed775e
DG
2306 if (cmd_ctx->session->kernel_session->metadata_stream_fd == 0) {
2307 DBG("Opening kernel metadata stream");
2308 if (cmd_ctx->session->kernel_session->metadata_stream_fd == 0) {
2309 ret = kernel_open_metadata_stream(cmd_ctx->session->kernel_session);
2310 if (ret < 0) {
2311 ERR("Kernel create metadata stream failed");
2312 ret = LTTCOMM_KERN_STREAM_FAIL;
2313 goto error;
2314 }
2315 }
2316 }
8c0faa1d 2317
f3ed775e 2318 /* For each channel */
0d0c377a
DG
2319 cds_list_for_each_entry(chan,
2320 &cmd_ctx->session->kernel_session->channel_list.head, list) {
f3ed775e
DG
2321 if (chan->stream_count == 0) {
2322 ret = kernel_open_channel_stream(chan);
2323 if (ret < 0) {
2324 ERR("Kernel create channel stream failed");
2325 ret = LTTCOMM_KERN_STREAM_FAIL;
2326 goto error;
2327 }
2328 /* Update the stream global counter */
2329 cmd_ctx->session->kernel_session->stream_count_global += ret;
2330 }
2331 }
2332
283046e0 2333 ret = start_kernel_trace(cmd_ctx->session->kernel_session);
8c0faa1d 2334 if (ret < 0) {
f3ed775e 2335 ret = LTTCOMM_KERN_START_FAIL;
8c0faa1d
DG
2336 goto error;
2337 }
8c0faa1d 2338
283046e0
DG
2339 DBG("Start kernel tracing");
2340 ret = kernel_start_session(cmd_ctx->session->kernel_session);
f3ed775e 2341 if (ret < 0) {
283046e0 2342 ERR("Kernel start session failed");
f3ed775e 2343 ret = LTTCOMM_KERN_START_FAIL;
8c0faa1d
DG
2344 goto error;
2345 }
8c0faa1d 2346
f3ed775e
DG
2347 /* Quiescent wait after starting trace */
2348 kernel_wait_quiescent(kernel_tracer_fd);
8c0faa1d
DG
2349 }
2350
f3ed775e 2351 /* TODO: Start all UST traces */
8c0faa1d
DG
2352
2353 ret = LTTCOMM_OK;
2354 break;
2355 }
f3ed775e 2356 case LTTNG_STOP_TRACE:
8c0faa1d 2357 {
f3ed775e 2358 struct ltt_kernel_channel *chan;
8c0faa1d
DG
2359 /* Setup lttng message with no payload */
2360 ret = setup_lttng_msg(cmd_ctx, 0);
2361 if (ret < 0) {
2362 goto setup_error;
2363 }
2364
f3ed775e
DG
2365 /* Kernel tracer */
2366 if (cmd_ctx->session->kernel_session != NULL) {
2367 DBG("Stop kernel tracing");
84291629 2368
f3ed775e
DG
2369 ret = kernel_metadata_flush_buffer(cmd_ctx->session->kernel_session->metadata_stream_fd);
2370 if (ret < 0) {
2371 ERR("Kernel metadata flush failed");
2372 }
8c0faa1d 2373
f3ed775e
DG
2374 cds_list_for_each_entry(chan, &cmd_ctx->session->kernel_session->channel_list.head, list) {
2375 ret = kernel_flush_buffer(chan);
2376 if (ret < 0) {
2377 ERR("Kernel flush buffer error");
2378 }
2379 }
2380
2381 ret = kernel_stop_session(cmd_ctx->session->kernel_session);
2382 if (ret < 0) {
2383 ERR("Kernel stop session failed");
2384 ret = LTTCOMM_KERN_STOP_FAIL;
2385 goto error;
2386 }
2387
2388 /* Quiescent wait after stopping trace */
2389 kernel_wait_quiescent(kernel_tracer_fd);
8c0faa1d
DG
2390 }
2391
f3ed775e 2392 /* TODO : User-space tracer */
8c0faa1d
DG
2393
2394 ret = LTTCOMM_OK;
2395 break;
2396 }
5e16da05
MD
2397 case LTTNG_CREATE_SESSION:
2398 {
5461b305
DG
2399 /* Setup lttng message with no payload */
2400 ret = setup_lttng_msg(cmd_ctx, 0);
2401 if (ret < 0) {
2402 goto setup_error;
2403 }
2404
3c6bae61 2405 tracepoint(create_session_start);
42abccdb 2406 ret = create_session(cmd_ctx->lsm->session.name, cmd_ctx->lsm->session.path);
3c6bae61 2407 tracepoint(create_session_end);
5e16da05 2408 if (ret < 0) {
f3ed775e 2409 if (ret == -EEXIST) {
5e16da05
MD
2410 ret = LTTCOMM_EXIST_SESS;
2411 } else {
aaf97519 2412 ret = LTTCOMM_FATAL;
aaf97519 2413 }
5461b305 2414 goto error;
8028d920 2415 }
1657e9bb 2416
5461b305 2417 ret = LTTCOMM_OK;
5e16da05
MD
2418 break;
2419 }
2420 case LTTNG_DESTROY_SESSION:
2421 {
5461b305
DG
2422 /* Setup lttng message with no payload */
2423 ret = setup_lttng_msg(cmd_ctx, 0);
2424 if (ret < 0) {
2425 goto setup_error;
2426 }
2427
f3ed775e
DG
2428 /* Clean kernel session teardown */
2429 teardown_kernel_session(cmd_ctx->session);
2430
3c6bae61 2431 tracepoint(destroy_session_start);
42abccdb 2432 ret = destroy_session(cmd_ctx->lsm->session.name);
3c6bae61 2433 tracepoint(destroy_session_end);
5e16da05 2434 if (ret < 0) {
f3ed775e 2435 ret = LTTCOMM_FATAL;
5461b305 2436 goto error;
5e16da05 2437 }
1657e9bb 2438
7a485870
DG
2439 /*
2440 * Must notify the kernel thread here to update it's pollfd in order to
2441 * remove the channel(s)' fd just destroyed.
2442 */
2443 ret = notify_kernel_pollfd();
2444 if (ret < 0) {
2445 ret = LTTCOMM_FATAL;
2446 goto error;
2447 }
2448
5461b305
DG
2449 ret = LTTCOMM_OK;
2450 break;
5e16da05 2451 }
9f19cc17 2452 case LTTNG_LIST_DOMAINS:
5e16da05 2453 {
4b222185 2454 size_t nb_dom = 0;
5461b305 2455
9f19cc17
DG
2456 if (cmd_ctx->session->kernel_session != NULL) {
2457 nb_dom++;
ce3d728c 2458 }
520ff687 2459
0177d773 2460 nb_dom += cmd_ctx->session->ust_session_list.count;
9f19cc17
DG
2461
2462 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_domain) * nb_dom);
5461b305
DG
2463 if (ret < 0) {
2464 goto setup_error;
520ff687 2465 }
57167058 2466
9f19cc17
DG
2467 ((struct lttng_domain *)(cmd_ctx->llm->payload))[0].type =
2468 LTTNG_DOMAIN_KERNEL;
5e16da05 2469
9f19cc17 2470 /* TODO: User-space tracer domain support */
5461b305 2471 ret = LTTCOMM_OK;
5e16da05
MD
2472 break;
2473 }
9f19cc17 2474 case LTTNG_LIST_CHANNELS:
5e16da05 2475 {
9f19cc17
DG
2476 /*
2477 * TODO: Only kernel channels are listed here. UST listing
2478 * is needed on lttng-ust 2.0 release.
2479 */
2480 size_t nb_chan = 0;
2481 if (cmd_ctx->session->kernel_session != NULL) {
2482 nb_chan += cmd_ctx->session->kernel_session->channel_count;
5461b305 2483 }
ca95a216 2484
9f19cc17
DG
2485 ret = setup_lttng_msg(cmd_ctx,
2486 sizeof(struct lttng_channel) * nb_chan);
5461b305
DG
2487 if (ret < 0) {
2488 goto setup_error;
2489 }
9f19cc17
DG
2490
2491 list_lttng_channels(cmd_ctx->session,
2492 (struct lttng_channel *)(cmd_ctx->llm->payload));
2493
2494 ret = LTTCOMM_OK;
5461b305 2495 break;
5e16da05 2496 }
9f19cc17 2497 case LTTNG_LIST_EVENTS:
5e16da05 2498 {
9f19cc17
DG
2499 /*
2500 * TODO: Only kernel events are listed here. UST listing
2501 * is needed on lttng-ust 2.0 release.
2502 */
2503 size_t nb_event = 0;
2504 struct ltt_kernel_channel *kchan = NULL;
2505
2506 if (cmd_ctx->session->kernel_session != NULL) {
62499ad6 2507 kchan = trace_kernel_get_channel_by_name(cmd_ctx->lsm->u.list.channel_name,
9f19cc17
DG
2508 cmd_ctx->session->kernel_session);
2509 if (kchan == NULL) {
2510 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
2511 goto error;
2512 }
2513 nb_event += kchan->event_count;
5461b305 2514 }
ca95a216 2515
9f19cc17
DG
2516 ret = setup_lttng_msg(cmd_ctx,
2517 sizeof(struct lttng_event) * nb_event);
5461b305
DG
2518 if (ret < 0) {
2519 goto setup_error;
2520 }
9f19cc17 2521
ced2f820 2522 DBG("Listing events (%zu events)", nb_event);
9f19cc17
DG
2523
2524 list_lttng_events(kchan,
2525 (struct lttng_event *)(cmd_ctx->llm->payload));
2526
2527 ret = LTTCOMM_OK;
5461b305 2528 break;
5e16da05
MD
2529 }
2530 case LTTNG_LIST_SESSIONS:
2531 {
6c9cc2ab 2532 lock_session_list();
5461b305 2533
6c9cc2ab 2534 if (session_list_ptr->count == 0) {
f3ed775e 2535 ret = LTTCOMM_NO_SESSION;
36a83748 2536 unlock_session_list();
5461b305 2537 goto error;
57167058 2538 }
5e16da05 2539
6c9cc2ab
DG
2540 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_session) *
2541 session_list_ptr->count);
5461b305 2542 if (ret < 0) {
36a83748 2543 unlock_session_list();
5461b305 2544 goto setup_error;
e065084a 2545 }
5e16da05 2546
6c9cc2ab
DG
2547 /* Filled the session array */
2548 list_lttng_sessions((struct lttng_session *)(cmd_ctx->llm->payload));
2549
2550 unlock_session_list();
5e16da05 2551
5461b305 2552 ret = LTTCOMM_OK;
5e16da05
MD
2553 break;
2554 }
d0254c7c
MD
2555 case LTTNG_CALIBRATE:
2556 {
2557 /* Setup lttng message with no payload */
2558 ret = setup_lttng_msg(cmd_ctx, 0);
2559 if (ret < 0) {
2560 goto setup_error;
2561 }
2562
2563 switch (cmd_ctx->lsm->domain.type) {
2564 case LTTNG_DOMAIN_KERNEL:
2565 {
2566 struct lttng_kernel_calibrate kcalibrate;
2567
2568 kcalibrate.type = cmd_ctx->lsm->u.calibrate.type;
2569 ret = kernel_calibrate(kernel_tracer_fd, &kcalibrate);
2570 if (ret < 0) {
2571 ret = LTTCOMM_KERN_ENABLE_FAIL;
2572 goto error;
2573 }
2574 break;
2575 }
2576 default:
2577 /* TODO: Userspace tracing */
2578 ret = LTTCOMM_NOT_IMPLEMENTED;
2579 goto error;
2580 }
2581 ret = LTTCOMM_OK;
2582 break;
2583 }
d9800920
DG
2584 case LTTNG_REGISTER_CONSUMER:
2585 {
2586 int sock;
2587
2588 /* Setup lttng message with no payload */
2589 ret = setup_lttng_msg(cmd_ctx, 0);
2590 if (ret < 0) {
2591 goto setup_error;
2592 }
2593
2594 switch (cmd_ctx->lsm->domain.type) {
2595 case LTTNG_DOMAIN_KERNEL:
2596 {
2597 /* Can't register a consumer if there is already one */
2598 if (cmd_ctx->session->kernel_session->consumer_fd != 0) {
2599 ret = LTTCOMM_CONNECT_FAIL;
2600 goto error;
2601 }
2602
2603 sock = lttcomm_connect_unix_sock(cmd_ctx->lsm->u.reg.path);
2604 if (sock < 0) {
2605 ret = LTTCOMM_CONNECT_FAIL;
2606 goto error;
2607 }
2608
2609 cmd_ctx->session->kernel_session->consumer_fd = sock;
2610 break;
2611 }
2612 default:
2613 /* TODO: Userspace tracing */
2614 ret = LTTCOMM_NOT_IMPLEMENTED;
2615 goto error;
2616 }
2617
2618 ret = LTTCOMM_OK;
2619 break;
2620 }
d0254c7c 2621
5e16da05
MD
2622 default:
2623 /* Undefined command */
5461b305
DG
2624 ret = setup_lttng_msg(cmd_ctx, 0);
2625 if (ret < 0) {
2626 goto setup_error;
2627 }
2628
5e16da05 2629 ret = LTTCOMM_UND;
5461b305 2630 break;
fac6795d
DG
2631 }
2632
5461b305
DG
2633 /* Set return code */
2634 cmd_ctx->llm->ret_code = ret;
ca95a216 2635
b5541356
DG
2636 if (cmd_ctx->session) {
2637 unlock_session(cmd_ctx->session);
2638 }
2639
e065084a
DG
2640 return ret;
2641
5461b305 2642error:
5461b305
DG
2643 if (cmd_ctx->llm == NULL) {
2644 DBG("Missing llm structure. Allocating one.");
894be886 2645 if (setup_lttng_msg(cmd_ctx, 0) < 0) {
5461b305
DG
2646 goto setup_error;
2647 }
2648 }
e065084a 2649 /* Notify client of error */
5461b305 2650 cmd_ctx->llm->ret_code = ret;
e065084a 2651
5461b305 2652setup_error:
b5541356
DG
2653 if (cmd_ctx->session) {
2654 unlock_session(cmd_ctx->session);
2655 }
8028d920 2656 return ret;
fac6795d
DG
2657}
2658
1d4b027a 2659/*
d063d709
DG
2660 * This thread manage all clients request using the unix client socket for
2661 * communication.
1d4b027a
DG
2662 */
2663static void *thread_manage_clients(void *data)
2664{
273ea72c
DG
2665 int sock = 0, ret;
2666 struct command_ctx *cmd_ctx = NULL;
2667 struct pollfd pollfd[2];
1d4b027a 2668
b25a8868
DG
2669 tracepoint(sessiond_th_cli_start);
2670
1d4b027a
DG
2671 DBG("[thread] Manage client started");
2672
2673 ret = lttcomm_listen_unix_sock(client_sock);
2674 if (ret < 0) {
2675 goto error;
2676 }
2677
273ea72c
DG
2678 /* First fd is always the quit pipe */
2679 pollfd[0].fd = thread_quit_pipe[0];
2680
2681 /* Apps socket */
2682 pollfd[1].fd = client_sock;
2683 pollfd[1].events = POLLIN;
2684
1d4b027a
DG
2685 /* Notify parent pid that we are ready
2686 * to accept command for client side.
2687 */
2688 if (opt_sig_parent) {
2689 kill(ppid, SIGCHLD);
2690 }
2691
2692 while (1) {
1d4b027a 2693 DBG("Accepting client command ...");
273ea72c 2694
b25a8868
DG
2695 tracepoint(sessiond_th_cli_poll);
2696
273ea72c
DG
2697 /* Inifinite blocking call, waiting for transmission */
2698 ret = poll(pollfd, 2, -1);
2699 if (ret < 0) {
2700 perror("poll client thread");
2701 goto error;
2702 }
2703
2704 /* Thread quit pipe has been closed. Killing thread. */
2705 if (pollfd[0].revents == POLLNVAL) {
2706 goto error;
2707 } else if (pollfd[1].revents == POLLERR) {
2708 ERR("Client socket poll error");
2709 goto error;
2710 }
2711
1d4b027a
DG
2712 sock = lttcomm_accept_unix_sock(client_sock);
2713 if (sock < 0) {
2714 goto error;
2715 }
2716
2717 /* Allocate context command to process the client request */
2718 cmd_ctx = malloc(sizeof(struct command_ctx));
2719
2720 /* Allocate data buffer for reception */
2721 cmd_ctx->lsm = malloc(sizeof(struct lttcomm_session_msg));
2722 cmd_ctx->llm = NULL;
2723 cmd_ctx->session = NULL;
2724
2725 /*
2726 * Data is received from the lttng client. The struct
2727 * lttcomm_session_msg (lsm) contains the command and data request of
2728 * the client.
2729 */
2730 DBG("Receiving data from client ...");
2731 ret = lttcomm_recv_unix_sock(sock, cmd_ctx->lsm, sizeof(struct lttcomm_session_msg));
2732 if (ret <= 0) {
2733 continue;
2734 }
2735
f7776ea7
DG
2736 // TODO: Validate cmd_ctx including sanity check for security purpose.
2737
1d4b027a
DG
2738 /*
2739 * This function dispatch the work to the kernel or userspace tracer
2740 * libs and fill the lttcomm_lttng_msg data structure of all the needed
2741 * informations for the client. The command context struct contains
2742 * everything this function may needs.
2743 */
2744 ret = process_client_msg(cmd_ctx);
2745 if (ret < 0) {
2746 /* TODO: Inform client somehow of the fatal error. At this point,
2747 * ret < 0 means that a malloc failed (ENOMEM). */
2748 /* Error detected but still accept command */
a2fb29a5 2749 clean_command_ctx(&cmd_ctx);
1d4b027a
DG
2750 continue;
2751 }
2752
2753 DBG("Sending response (size: %d, retcode: %d)",
2754 cmd_ctx->lttng_msg_size, cmd_ctx->llm->ret_code);
2755 ret = send_unix_sock(sock, cmd_ctx->llm, cmd_ctx->lttng_msg_size);
2756 if (ret < 0) {
2757 ERR("Failed to send data back to client");
2758 }
2759
a2fb29a5 2760 clean_command_ctx(&cmd_ctx);
d6e4fca4
DG
2761
2762 /* End of transmission */
2763 close(sock);
1d4b027a
DG
2764 }
2765
2766error:
273ea72c
DG
2767 DBG("Client thread dying");
2768 if (client_sock) {
2769 close(client_sock);
2770 }
2771 if (sock) {
2772 close(sock);
2773 }
2774
2775 unlink(client_unix_sock_path);
2776
a2fb29a5 2777 clean_command_ctx(&cmd_ctx);
1d4b027a
DG
2778 return NULL;
2779}
2780
2781
fac6795d
DG
2782/*
2783 * usage function on stderr
2784 */
2785static void usage(void)
2786{
b716ce68 2787 fprintf(stderr, "Usage: %s OPTIONS\n\nOptions:\n", progname);
d6f42150
DG
2788 fprintf(stderr, " -h, --help Display this usage.\n");
2789 fprintf(stderr, " -c, --client-sock PATH Specify path for the client unix socket\n");
2790 fprintf(stderr, " -a, --apps-sock PATH Specify path for apps unix socket\n");
2791 fprintf(stderr, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
2792 fprintf(stderr, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
2793 fprintf(stderr, " -d, --daemonize Start as a daemon.\n");
2794 fprintf(stderr, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
2795 fprintf(stderr, " -V, --version Show version number.\n");
2796 fprintf(stderr, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
2797 fprintf(stderr, " -q, --quiet No output at all.\n");
2798 fprintf(stderr, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
31f73cc9 2799 fprintf(stderr, " --verbose-kconsumerd Verbose mode for kconsumerd. Activate DBG() macro.\n");
fac6795d
DG
2800}
2801
2802/*
2803 * daemon argument parsing
2804 */
2805static int parse_args(int argc, char **argv)
2806{
2807 int c;
2808
2809 static struct option long_options[] = {
2810 { "client-sock", 1, 0, 'c' },
2811 { "apps-sock", 1, 0, 'a' },
d6f42150
DG
2812 { "kconsumerd-cmd-sock", 1, 0, 0 },
2813 { "kconsumerd-err-sock", 1, 0, 0 },
fac6795d 2814 { "daemonize", 0, 0, 'd' },
5b8719f5 2815 { "sig-parent", 0, 0, 'S' },
fac6795d
DG
2816 { "help", 0, 0, 'h' },
2817 { "group", 1, 0, 'g' },
2818 { "version", 0, 0, 'V' },
75462a81 2819 { "quiet", 0, 0, 'q' },
3f9947db 2820 { "verbose", 0, 0, 'v' },
31f73cc9 2821 { "verbose-kconsumerd", 0, 0, 'Z' },
fac6795d
DG
2822 { NULL, 0, 0, 0 }
2823 };
2824
2825 while (1) {
2826 int option_index = 0;
31f73cc9 2827 c = getopt_long(argc, argv, "dhqvVS" "a:c:g:s:E:C:Z", long_options, &option_index);
fac6795d
DG
2828 if (c == -1) {
2829 break;
2830 }
2831
2832 switch (c) {
2833 case 0:
2834 fprintf(stderr, "option %s", long_options[option_index].name);
2835 if (optarg) {
2836 fprintf(stderr, " with arg %s\n", optarg);
2837 }
2838 break;
b716ce68 2839 case 'c':
fac6795d
DG
2840 snprintf(client_unix_sock_path, PATH_MAX, "%s", optarg);
2841 break;
2842 case 'a':
2843 snprintf(apps_unix_sock_path, PATH_MAX, "%s", optarg);
2844 break;
2845 case 'd':
2846 opt_daemon = 1;
2847 break;
2848 case 'g':
2849 opt_tracing_group = strdup(optarg);
2850 break;
2851 case 'h':
2852 usage();
2853 exit(EXIT_FAILURE);
2854 case 'V':
2855 fprintf(stdout, "%s\n", VERSION);
2856 exit(EXIT_SUCCESS);
5b8719f5
DG
2857 case 'S':
2858 opt_sig_parent = 1;
2859 break;
d6f42150
DG
2860 case 'E':
2861 snprintf(kconsumerd_err_unix_sock_path, PATH_MAX, "%s", optarg);
2862 break;
2863 case 'C':
2864 snprintf(kconsumerd_cmd_unix_sock_path, PATH_MAX, "%s", optarg);
2865 break;
75462a81
DG
2866 case 'q':
2867 opt_quiet = 1;
2868 break;
3f9947db 2869 case 'v':
53086306
DG
2870 /* Verbose level can increase using multiple -v */
2871 opt_verbose += 1;
3f9947db 2872 break;
31f73cc9
MD
2873 case 'Z':
2874 opt_verbose_kconsumerd += 1;
2875 break;
fac6795d
DG
2876 default:
2877 /* Unknown option or other error.
2878 * Error is printed by getopt, just return */
2879 return -1;
2880 }
2881 }
2882
2883 return 0;
2884}
2885
2886/*
d063d709 2887 * Creates the two needed socket by the daemon.
d6f42150
DG
2888 * apps_sock - The communication socket for all UST apps.
2889 * client_sock - The communication of the cli tool (lttng).
fac6795d 2890 */
cf3af59e 2891static int init_daemon_socket(void)
fac6795d
DG
2892{
2893 int ret = 0;
2894 mode_t old_umask;
2895
2896 old_umask = umask(0);
2897
2898 /* Create client tool unix socket */
d6f42150
DG
2899 client_sock = lttcomm_create_unix_sock(client_unix_sock_path);
2900 if (client_sock < 0) {
2901 ERR("Create unix sock failed: %s", client_unix_sock_path);
fac6795d
DG
2902 ret = -1;
2903 goto end;
2904 }
2905
2906 /* File permission MUST be 660 */
2907 ret = chmod(client_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
2908 if (ret < 0) {
d6f42150 2909 ERR("Set file permissions failed: %s", client_unix_sock_path);
fac6795d
DG
2910 perror("chmod");
2911 goto end;
2912 }
2913
2914 /* Create the application unix socket */
d6f42150
DG
2915 apps_sock = lttcomm_create_unix_sock(apps_unix_sock_path);
2916 if (apps_sock < 0) {
2917 ERR("Create unix sock failed: %s", apps_unix_sock_path);
fac6795d
DG
2918 ret = -1;
2919 goto end;
2920 }
2921
d6f42150 2922 /* File permission MUST be 666 */
fac6795d
DG
2923 ret = chmod(apps_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
2924 if (ret < 0) {
d6f42150 2925 ERR("Set file permissions failed: %s", apps_unix_sock_path);
fac6795d
DG
2926 perror("chmod");
2927 goto end;
2928 }
2929
2930end:
2931 umask(old_umask);
2932 return ret;
2933}
2934
2935/*
7d8234d9
MD
2936 * Check if the global socket is available, and if a daemon is answering
2937 * at the other side. If yes, error is returned.
fac6795d 2938 */
cf3af59e 2939static int check_existing_daemon(void)
fac6795d 2940{
7d8234d9 2941 if (access(client_unix_sock_path, F_OK) < 0 &&
099e26bd 2942 access(apps_unix_sock_path, F_OK) < 0) {
7d8234d9 2943 return 0;
099e26bd 2944 }
7d8234d9 2945 /* Is there anybody out there ? */
099e26bd 2946 if (lttng_session_daemon_alive()) {
7d8234d9 2947 return -EEXIST;
099e26bd 2948 } else {
7d8234d9 2949 return 0;
099e26bd 2950 }
fac6795d
DG
2951}
2952
fac6795d 2953/*
d063d709 2954 * Set the tracing group gid onto the client socket.
5e16da05 2955 *
d063d709
DG
2956 * Race window between mkdir and chown is OK because we are going from more
2957 * permissive (root.root) to les permissive (root.tracing).
fac6795d 2958 */
d6f42150 2959static int set_permissions(void)
fac6795d
DG
2960{
2961 int ret;
996b65c8 2962 gid_t gid;
fac6795d 2963
996b65c8
MD
2964 gid = allowed_group();
2965 if (gid < 0) {
a463f419
DG
2966 if (is_root) {
2967 WARN("No tracing group detected");
2968 ret = 0;
2969 } else {
2970 ERR("Missing tracing group. Aborting execution.");
2971 ret = -1;
2972 }
fac6795d
DG
2973 goto end;
2974 }
2975
d6f42150 2976 /* Set lttng run dir */
996b65c8 2977 ret = chown(LTTNG_RUNDIR, 0, gid);
d6f42150
DG
2978 if (ret < 0) {
2979 ERR("Unable to set group on " LTTNG_RUNDIR);
2980 perror("chown");
2981 }
2982
2983 /* lttng client socket path */
996b65c8 2984 ret = chown(client_unix_sock_path, 0, gid);
fac6795d 2985 if (ret < 0) {
d6f42150
DG
2986 ERR("Unable to set group on %s", client_unix_sock_path);
2987 perror("chown");
2988 }
2989
2990 /* kconsumerd error socket path */
996b65c8 2991 ret = chown(kconsumerd_err_unix_sock_path, 0, gid);
d6f42150
DG
2992 if (ret < 0) {
2993 ERR("Unable to set group on %s", kconsumerd_err_unix_sock_path);
fac6795d
DG
2994 perror("chown");
2995 }
2996
d6f42150 2997 DBG("All permissions are set");
e07ae692 2998
fac6795d
DG
2999end:
3000 return ret;
3001}
3002
7a485870 3003/*
d063d709 3004 * Create the pipe used to wake up the kernel thread.
7a485870
DG
3005 */
3006static int create_kernel_poll_pipe(void)
3007{
3008 return pipe2(kernel_poll_pipe, O_CLOEXEC);
3009}
3010
099e26bd
DG
3011/*
3012 * Create the application command pipe to wake thread_manage_apps.
3013 */
3014static int create_apps_cmd_pipe(void)
3015{
3016 return pipe2(apps_cmd_pipe, O_CLOEXEC);
3017}
3018
d6f42150 3019/*
d063d709 3020 * Create the lttng run directory needed for all global sockets and pipe.
d6f42150
DG
3021 */
3022static int create_lttng_rundir(void)
3023{
3024 int ret;
3025
3026 ret = mkdir(LTTNG_RUNDIR, S_IRWXU | S_IRWXG );
3027 if (ret < 0) {
b1f11e69
DG
3028 if (errno != EEXIST) {
3029 ERR("Unable to create " LTTNG_RUNDIR);
3030 goto error;
3031 } else {
3032 ret = 0;
3033 }
d6f42150
DG
3034 }
3035
3036error:
3037 return ret;
3038}
3039
3040/*
d063d709
DG
3041 * Setup sockets and directory needed by the kconsumerd communication with the
3042 * session daemon.
d6f42150
DG
3043 */
3044static int set_kconsumerd_sockets(void)
3045{
3046 int ret;
3047
3048 if (strlen(kconsumerd_err_unix_sock_path) == 0) {
3049 snprintf(kconsumerd_err_unix_sock_path, PATH_MAX, KCONSUMERD_ERR_SOCK_PATH);
3050 }
3051
3052 if (strlen(kconsumerd_cmd_unix_sock_path) == 0) {
3053 snprintf(kconsumerd_cmd_unix_sock_path, PATH_MAX, KCONSUMERD_CMD_SOCK_PATH);
3054 }
3055
3056 ret = mkdir(KCONSUMERD_PATH, S_IRWXU | S_IRWXG);
3057 if (ret < 0) {
6beb2242
DG
3058 if (errno != EEXIST) {
3059 ERR("Failed to create " KCONSUMERD_PATH);
3060 goto error;
3061 }
3062 ret = 0;
d6f42150
DG
3063 }
3064
3065 /* Create the kconsumerd error unix socket */
3066 kconsumerd_err_sock = lttcomm_create_unix_sock(kconsumerd_err_unix_sock_path);
3067 if (kconsumerd_err_sock < 0) {
3068 ERR("Create unix sock failed: %s", kconsumerd_err_unix_sock_path);
3069 ret = -1;
3070 goto error;
3071 }
3072
3073 /* File permission MUST be 660 */
3074 ret = chmod(kconsumerd_err_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
3075 if (ret < 0) {
3076 ERR("Set file permissions failed: %s", kconsumerd_err_unix_sock_path);
3077 perror("chmod");
3078 goto error;
3079 }
3080
3081error:
3082 return ret;
3083}
3084
fac6795d 3085/*
d063d709 3086 * Signal handler for the daemon
cf3af59e
MD
3087 *
3088 * Simply stop all worker threads, leaving main() return gracefully
3089 * after joining all threads and calling cleanup().
fac6795d
DG
3090 */
3091static void sighandler(int sig)
3092{
3093 switch (sig) {
cf3af59e
MD
3094 case SIGPIPE:
3095 DBG("SIGPIPE catched");
3096 return;
3097 case SIGINT:
3098 DBG("SIGINT catched");
3099 stop_threads();
3100 break;
3101 case SIGTERM:
3102 DBG("SIGTERM catched");
3103 stop_threads();
3104 break;
3105 default:
3106 break;
fac6795d 3107 }
fac6795d
DG
3108}
3109
3110/*
d063d709 3111 * Setup signal handler for :
1d4b027a 3112 * SIGINT, SIGTERM, SIGPIPE
fac6795d 3113 */
1d4b027a 3114static int set_signal_handler(void)
fac6795d 3115{
1d4b027a
DG
3116 int ret = 0;
3117 struct sigaction sa;
3118 sigset_t sigset;
fac6795d 3119
1d4b027a
DG
3120 if ((ret = sigemptyset(&sigset)) < 0) {
3121 perror("sigemptyset");
3122 return ret;
3123 }
d6f42150 3124
1d4b027a
DG
3125 sa.sa_handler = sighandler;
3126 sa.sa_mask = sigset;
3127 sa.sa_flags = 0;
3128 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
3129 perror("sigaction");
3130 return ret;
d6f42150
DG
3131 }
3132
1d4b027a
DG
3133 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
3134 perror("sigaction");
3135 return ret;
d6f42150 3136 }
aaf26714 3137
1d4b027a
DG
3138 if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) {
3139 perror("sigaction");
3140 return ret;
8c0faa1d
DG
3141 }
3142
1d4b027a
DG
3143 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
3144
3145 return ret;
fac6795d
DG
3146}
3147
f3ed775e 3148/*
d063d709
DG
3149 * Set open files limit to unlimited. This daemon can open a large number of
3150 * file descriptors in order to consumer multiple kernel traces.
f3ed775e
DG
3151 */
3152static void set_ulimit(void)
3153{
3154 int ret;
3155 struct rlimit lim;
3156
a88df331 3157 /* The kernel does not allowed an infinite limit for open files */
f3ed775e
DG
3158 lim.rlim_cur = 65535;
3159 lim.rlim_max = 65535;
3160
3161 ret = setrlimit(RLIMIT_NOFILE, &lim);
3162 if (ret < 0) {
3163 perror("failed to set open files limit");
3164 }
3165}
3166
fac6795d
DG
3167/*
3168 * main
3169 */
3170int main(int argc, char **argv)
3171{
fac6795d
DG
3172 int ret = 0;
3173 void *status;
b082db07 3174 const char *home_path;
fac6795d 3175
b25a8868
DG
3176 tracepoint(sessiond_boot_start);
3177
273ea72c 3178 /* Create thread quit pipe */
cf3af59e
MD
3179 if ((ret = init_thread_quit_pipe()) < 0) {
3180 goto error;
273ea72c
DG
3181 }
3182
fac6795d
DG
3183 /* Parse arguments */
3184 progname = argv[0];
3185 if ((ret = parse_args(argc, argv) < 0)) {
cf3af59e 3186 goto error;
fac6795d
DG
3187 }
3188
3189 /* Daemonize */
3190 if (opt_daemon) {
53094c05
DG
3191 ret = daemon(0, 0);
3192 if (ret < 0) {
3193 perror("daemon");
cf3af59e 3194 goto error;
53094c05 3195 }
fac6795d
DG
3196 }
3197
3198 /* Check if daemon is UID = 0 */
3199 is_root = !getuid();
3200
fac6795d 3201 if (is_root) {
d6f42150
DG
3202 ret = create_lttng_rundir();
3203 if (ret < 0) {
cf3af59e 3204 goto error;
d6f42150
DG
3205 }
3206
fac6795d 3207 if (strlen(apps_unix_sock_path) == 0) {
d6f42150
DG
3208 snprintf(apps_unix_sock_path, PATH_MAX,
3209 DEFAULT_GLOBAL_APPS_UNIX_SOCK);
fac6795d
DG
3210 }
3211
3212 if (strlen(client_unix_sock_path) == 0) {
d6f42150
DG
3213 snprintf(client_unix_sock_path, PATH_MAX,
3214 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK);
3215 }
0fdd1e2c
DG
3216
3217 /* Set global SHM for ust */
3218 if (strlen(wait_shm_path) == 0) {
3219 snprintf(wait_shm_path, PATH_MAX,
3220 DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH);
3221 }
fac6795d 3222 } else {
b082db07
DG
3223 home_path = get_home_dir();
3224 if (home_path == NULL) {
273ea72c
DG
3225 /* TODO: Add --socket PATH option */
3226 ERR("Can't get HOME directory for sockets creation.");
cf3af59e
MD
3227 ret = -EPERM;
3228 goto error;
b082db07
DG
3229 }
3230
fac6795d 3231 if (strlen(apps_unix_sock_path) == 0) {
d6f42150 3232 snprintf(apps_unix_sock_path, PATH_MAX,
b082db07 3233 DEFAULT_HOME_APPS_UNIX_SOCK, home_path);
fac6795d
DG
3234 }
3235
3236 /* Set the cli tool unix socket path */
3237 if (strlen(client_unix_sock_path) == 0) {
d6f42150 3238 snprintf(client_unix_sock_path, PATH_MAX,
b082db07 3239 DEFAULT_HOME_CLIENT_UNIX_SOCK, home_path);
fac6795d 3240 }
0fdd1e2c
DG
3241
3242 /* Set global SHM for ust */
3243 if (strlen(wait_shm_path) == 0) {
3244 snprintf(wait_shm_path, PATH_MAX,
3245 DEFAULT_HOME_APPS_WAIT_SHM_PATH, geteuid());
3246 }
fac6795d
DG
3247 }
3248
847177cd
DG
3249 DBG("Client socket path %s", client_unix_sock_path);
3250 DBG("Application socket path %s", apps_unix_sock_path);
3251
273ea72c 3252 /*
7d8234d9 3253 * See if daemon already exist.
fac6795d 3254 */
7d8234d9 3255 if ((ret = check_existing_daemon()) < 0) {
75462a81 3256 ERR("Already running daemon.\n");
273ea72c 3257 /*
cf3af59e
MD
3258 * We do not goto exit because we must not cleanup()
3259 * because a daemon is already running.
ab118b20 3260 */
cf3af59e 3261 goto error;
a88df331
DG
3262 }
3263
3264 /* After this point, we can safely call cleanup() so goto error is used */
3265
3266 /*
3267 * These actions must be executed as root. We do that *after* setting up
3268 * the sockets path because we MUST make the check for another daemon using
3269 * those paths *before* trying to set the kernel consumer sockets and init
3270 * kernel tracer.
3271 */
3272 if (is_root) {
3273 ret = set_kconsumerd_sockets();
3274 if (ret < 0) {
cf3af59e 3275 goto exit;
a88df331
DG
3276 }
3277
3278 /* Setup kernel tracer */
3279 init_kernel_tracer();
3280
3281 /* Set ulimit for open files */
3282 set_ulimit();
fac6795d
DG
3283 }
3284
cf3af59e
MD
3285 if ((ret = set_signal_handler()) < 0) {
3286 goto exit;
fac6795d
DG
3287 }
3288
d6f42150 3289 /* Setup the needed unix socket */
cf3af59e
MD
3290 if ((ret = init_daemon_socket()) < 0) {
3291 goto exit;
fac6795d
DG
3292 }
3293
3294 /* Set credentials to socket */
cf3af59e
MD
3295 if (is_root && ((ret = set_permissions()) < 0)) {
3296 goto exit;
fac6795d
DG
3297 }
3298
5b8719f5
DG
3299 /* Get parent pid if -S, --sig-parent is specified. */
3300 if (opt_sig_parent) {
3301 ppid = getppid();
3302 }
3303
7a485870 3304 /* Setup the kernel pipe for waking up the kernel thread */
cf3af59e
MD
3305 if ((ret = create_kernel_poll_pipe()) < 0) {
3306 goto exit;
7a485870
DG
3307 }
3308
099e26bd
DG
3309 /* Setup the thread apps communication pipe. */
3310 if ((ret = create_apps_cmd_pipe()) < 0) {
3311 goto exit;
3312 }
3313
3314 /* Init UST command queue. */
3315 cds_wfq_init(&ust_cmd_queue.queue);
3316
273ea72c
DG
3317 /*
3318 * Get session list pointer. This pointer MUST NOT be free().
3319 * This list is statically declared in session.c
3320 */
b5541356
DG
3321 session_list_ptr = get_session_list();
3322
cf3af59e 3323 /* Create thread to manage the client socket */
099e26bd
DG
3324 ret = pthread_create(&client_thread, NULL,
3325 thread_manage_clients, (void *) NULL);
cf3af59e 3326 if (ret != 0) {
099e26bd 3327 perror("pthread_create clients");
cf3af59e
MD
3328 goto exit_client;
3329 }
fac6795d 3330
099e26bd
DG
3331 /* Create thread to dispatch registration */
3332 ret = pthread_create(&dispatch_thread, NULL,
3333 thread_dispatch_ust_registration, (void *) NULL);
3334 if (ret != 0) {
3335 perror("pthread_create dispatch");
3336 goto exit_dispatch;
3337 }
3338
3339 /* Create thread to manage application registration. */
3340 ret = pthread_create(&reg_apps_thread, NULL,
3341 thread_registration_apps, (void *) NULL);
3342 if (ret != 0) {
3343 perror("pthread_create registration");
3344 goto exit_reg_apps;
3345 }
3346
cf3af59e
MD
3347 /* Create thread to manage application socket */
3348 ret = pthread_create(&apps_thread, NULL, thread_manage_apps, (void *) NULL);
3349 if (ret != 0) {
099e26bd 3350 perror("pthread_create apps");
cf3af59e
MD
3351 goto exit_apps;
3352 }
fac6795d 3353
cf3af59e
MD
3354 /* Create kernel thread to manage kernel event */
3355 ret = pthread_create(&kernel_thread, NULL, thread_manage_kernel, (void *) NULL);
3356 if (ret != 0) {
099e26bd 3357 perror("pthread_create kernel");
cf3af59e
MD
3358 goto exit_kernel;
3359 }
7a485870 3360
b25a8868
DG
3361 tracepoint(sessiond_boot_end);
3362
cf3af59e
MD
3363 ret = pthread_join(kernel_thread, &status);
3364 if (ret != 0) {
3365 perror("pthread_join");
3366 goto error; /* join error, exit without cleanup */
fac6795d
DG
3367 }
3368
cf3af59e
MD
3369exit_kernel:
3370 ret = pthread_join(apps_thread, &status);
3371 if (ret != 0) {
3372 perror("pthread_join");
3373 goto error; /* join error, exit without cleanup */
3374 }
fac6795d 3375
cf3af59e 3376exit_apps:
099e26bd
DG
3377 ret = pthread_join(reg_apps_thread, &status);
3378 if (ret != 0) {
3379 perror("pthread_join");
3380 goto error; /* join error, exit without cleanup */
3381 }
3382
3383exit_reg_apps:
3384 ret = pthread_join(dispatch_thread, &status);
3385 if (ret != 0) {
3386 perror("pthread_join");
3387 goto error; /* join error, exit without cleanup */
3388 }
3389
3390exit_dispatch:
cf3af59e
MD
3391 ret = pthread_join(client_thread, &status);
3392 if (ret != 0) {
3393 perror("pthread_join");
3394 goto error; /* join error, exit without cleanup */
3395 }
3396
3397 ret = join_kconsumerd_thread();
3398 if (ret != 0) {
3399 perror("join_kconsumerd");
3400 goto error; /* join error, exit without cleanup */
3401 }
a88df331 3402
cf3af59e 3403exit_client:
a88df331 3404exit:
cf3af59e
MD
3405 /*
3406 * cleanup() is called when no other thread is running.
3407 */
3408 cleanup();
3409 if (!ret)
3410 exit(EXIT_SUCCESS);
3411error:
5e16da05 3412 exit(EXIT_FAILURE);
fac6795d 3413}
This page took 0.37412 seconds and 4 git commands to generate.