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