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