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