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