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