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