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