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