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