Add support for UST application registration
[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_destroy_kernel_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 = asprintf(&session->kernel_session->trace_path, "%s/kernel",
1565 session->path);
1566 if (ret < 0) {
1567 perror("asprintf kernel traces path");
1568 goto error;
1569 }
1570
1571 ret = mkdir_recursive(session->kernel_session->trace_path,
1572 S_IRWXU | S_IRWXG, geteuid(), allowed_group());
1573 if (ret < 0) {
1574 if (ret != -EEXIST) {
1575 ERR("Trace directory creation error");
1576 goto error;
1577 }
1578 }
1579
1580 error:
1581 return ret;
1582 }
1583
1584 /*
1585 * Using the session list, filled a lttng_session array to send back to the
1586 * client for session listing.
1587 *
1588 * The session list lock MUST be acquired before calling this function. Use
1589 * lock_session_list() and unlock_session_list().
1590 */
1591 static void list_lttng_sessions(struct lttng_session *sessions)
1592 {
1593 int i = 0;
1594 struct ltt_session *session;
1595
1596 DBG("Getting all available session");
1597 /*
1598 * Iterate over session list and append data after the control struct in
1599 * the buffer.
1600 */
1601 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
1602 strncpy(sessions[i].path, session->path, PATH_MAX);
1603 sessions[i].path[PATH_MAX - 1] = '\0';
1604 strncpy(sessions[i].name, session->name, NAME_MAX);
1605 sessions[i].name[NAME_MAX - 1] = '\0';
1606 i++;
1607 }
1608 }
1609
1610 /*
1611 * Fill lttng_channel array of all channels.
1612 */
1613 static void list_lttng_channels(struct ltt_session *session,
1614 struct lttng_channel *channels)
1615 {
1616 int i = 0;
1617 struct ltt_kernel_channel *kchan;
1618
1619 DBG("Listing channels for session %s", session->name);
1620
1621 /* Kernel channels */
1622 if (session->kernel_session != NULL) {
1623 cds_list_for_each_entry(kchan, &session->kernel_session->channel_list.head, list) {
1624 /* Copy lttng_channel struct to array */
1625 memcpy(&channels[i], kchan->channel, sizeof(struct lttng_channel));
1626 channels[i].enabled = kchan->enabled;
1627 i++;
1628 }
1629 }
1630
1631 /* TODO: Missing UST listing */
1632 }
1633
1634 /*
1635 * Fill lttng_event array of all events in the channel.
1636 */
1637 static void list_lttng_events(struct ltt_kernel_channel *kchan,
1638 struct lttng_event *events)
1639 {
1640 /*
1641 * TODO: This is ONLY kernel. Need UST support.
1642 */
1643 int i = 0;
1644 struct ltt_kernel_event *event;
1645
1646 DBG("Listing events for channel %s", kchan->channel->name);
1647
1648 /* Kernel channels */
1649 cds_list_for_each_entry(event, &kchan->events_list.head , list) {
1650 strncpy(events[i].name, event->event->name, LTTNG_SYMBOL_NAME_LEN);
1651 events[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
1652 events[i].enabled = event->enabled;
1653 switch (event->event->instrumentation) {
1654 case LTTNG_KERNEL_TRACEPOINT:
1655 events[i].type = LTTNG_EVENT_TRACEPOINT;
1656 break;
1657 case LTTNG_KERNEL_KPROBE:
1658 case LTTNG_KERNEL_KRETPROBE:
1659 events[i].type = LTTNG_EVENT_PROBE;
1660 memcpy(&events[i].attr.probe, &event->event->u.kprobe,
1661 sizeof(struct lttng_kernel_kprobe));
1662 break;
1663 case LTTNG_KERNEL_FUNCTION:
1664 events[i].type = LTTNG_EVENT_FUNCTION;
1665 memcpy(&events[i].attr.ftrace, &event->event->u.ftrace,
1666 sizeof(struct lttng_kernel_function));
1667 break;
1668 }
1669 i++;
1670 }
1671 }
1672
1673 /*
1674 * Process the command requested by the lttng client within the command
1675 * context structure. This function make sure that the return structure (llm)
1676 * is set and ready for transmission before returning.
1677 *
1678 * Return any error encountered or 0 for success.
1679 */
1680 static int process_client_msg(struct command_ctx *cmd_ctx)
1681 {
1682 int ret = LTTCOMM_OK;
1683
1684 DBG("Processing client command %d", cmd_ctx->lsm->cmd_type);
1685
1686 /*
1687 * Commands that DO NOT need a session.
1688 */
1689 switch (cmd_ctx->lsm->cmd_type) {
1690 case LTTNG_CREATE_SESSION:
1691 case LTTNG_LIST_SESSIONS:
1692 case LTTNG_LIST_TRACEPOINTS:
1693 case LTTNG_CALIBRATE:
1694 break;
1695 default:
1696 DBG("Getting session %s by name", cmd_ctx->lsm->session.name);
1697 cmd_ctx->session = find_session_by_name(cmd_ctx->lsm->session.name);
1698 if (cmd_ctx->session == NULL) {
1699 /* If session name not found */
1700 if (cmd_ctx->lsm->session.name != NULL) {
1701 ret = LTTCOMM_SESS_NOT_FOUND;
1702 } else { /* If no session name specified */
1703 ret = LTTCOMM_SELECT_SESS;
1704 }
1705 goto error;
1706 } else {
1707 /* Acquire lock for the session */
1708 lock_session(cmd_ctx->session);
1709 }
1710 break;
1711 }
1712
1713 /*
1714 * Check domain type for specific "pre-action".
1715 */
1716 switch (cmd_ctx->lsm->domain.type) {
1717 case LTTNG_DOMAIN_KERNEL:
1718 /* Kernel tracer check */
1719 if (kernel_tracer_fd == 0) {
1720 init_kernel_tracer();
1721 if (kernel_tracer_fd == 0) {
1722 ret = LTTCOMM_KERN_NA;
1723 goto error;
1724 }
1725 }
1726
1727 /* Need a session for kernel command */
1728 switch (cmd_ctx->lsm->cmd_type) {
1729 case LTTNG_CALIBRATE:
1730 case LTTNG_CREATE_SESSION:
1731 case LTTNG_LIST_SESSIONS:
1732 case LTTNG_LIST_TRACEPOINTS:
1733 break;
1734 default:
1735 if (cmd_ctx->session->kernel_session == NULL) {
1736 ret = create_kernel_session(cmd_ctx->session);
1737 if (ret < 0) {
1738 ret = LTTCOMM_KERN_SESS_FAIL;
1739 goto error;
1740 }
1741
1742 /* Start the kernel consumer daemon */
1743
1744 if (kconsumerd_pid == 0 &&
1745 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
1746 ret = start_kconsumerd();
1747 if (ret < 0) {
1748 goto error;
1749 }
1750 }
1751 }
1752 }
1753 break;
1754 default:
1755 break;
1756 }
1757
1758 /* Process by command type */
1759 switch (cmd_ctx->lsm->cmd_type) {
1760 case LTTNG_ADD_CONTEXT:
1761 {
1762 struct lttng_kernel_context kctx;
1763
1764 /* Setup lttng message with no payload */
1765 ret = setup_lttng_msg(cmd_ctx, 0);
1766 if (ret < 0) {
1767 goto setup_error;
1768 }
1769
1770 switch (cmd_ctx->lsm->domain.type) {
1771 case LTTNG_DOMAIN_KERNEL:
1772 /* Create Kernel context */
1773 kctx.ctx = cmd_ctx->lsm->u.context.ctx.ctx;
1774 kctx.u.perf_counter.type = cmd_ctx->lsm->u.context.ctx.u.perf_counter.type;
1775 kctx.u.perf_counter.config = cmd_ctx->lsm->u.context.ctx.u.perf_counter.config;
1776 strncpy(kctx.u.perf_counter.name,
1777 cmd_ctx->lsm->u.context.ctx.u.perf_counter.name,
1778 LTTNG_SYMBOL_NAME_LEN);
1779 kctx.u.perf_counter.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
1780
1781 /* Add kernel context to kernel tracer. See context.c */
1782 ret = add_kernel_context(cmd_ctx->session->kernel_session, &kctx,
1783 cmd_ctx->lsm->u.context.event_name,
1784 cmd_ctx->lsm->u.context.channel_name);
1785 if (ret != LTTCOMM_OK) {
1786 goto error;
1787 }
1788 break;
1789 default:
1790 /* TODO: Userspace tracing */
1791 ret = LTTCOMM_NOT_IMPLEMENTED;
1792 goto error;
1793 }
1794
1795 ret = LTTCOMM_OK;
1796 break;
1797 }
1798 case LTTNG_DISABLE_CHANNEL:
1799 {
1800 struct ltt_kernel_channel *kchan;
1801
1802 /* Setup lttng message with no payload */
1803 ret = setup_lttng_msg(cmd_ctx, 0);
1804 if (ret < 0) {
1805 goto setup_error;
1806 }
1807
1808 switch (cmd_ctx->lsm->domain.type) {
1809 case LTTNG_DOMAIN_KERNEL:
1810 kchan = get_kernel_channel_by_name(cmd_ctx->lsm->u.disable.channel_name,
1811 cmd_ctx->session->kernel_session);
1812 if (kchan == NULL) {
1813 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
1814 goto error;
1815 } else if (kchan->enabled == 1) {
1816 ret = kernel_disable_channel(kchan);
1817 if (ret < 0) {
1818 if (ret != EEXIST) {
1819 ret = LTTCOMM_KERN_CHAN_DISABLE_FAIL;
1820 }
1821 goto error;
1822 }
1823 }
1824 kernel_wait_quiescent(kernel_tracer_fd);
1825 break;
1826 default:
1827 /* TODO: Userspace tracing */
1828 ret = LTTCOMM_NOT_IMPLEMENTED;
1829 goto error;
1830 }
1831
1832 ret = LTTCOMM_OK;
1833 break;
1834 }
1835 case LTTNG_DISABLE_EVENT:
1836 {
1837 struct ltt_kernel_channel *kchan;
1838 struct ltt_kernel_event *kevent;
1839
1840 /* Setup lttng message with no payload */
1841 ret = setup_lttng_msg(cmd_ctx, 0);
1842 if (ret < 0) {
1843 goto setup_error;
1844 }
1845
1846 switch (cmd_ctx->lsm->domain.type) {
1847 case LTTNG_DOMAIN_KERNEL:
1848 kchan = get_kernel_channel_by_name(cmd_ctx->lsm->u.disable.channel_name,
1849 cmd_ctx->session->kernel_session);
1850 if (kchan == NULL) {
1851 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
1852 goto error;
1853 }
1854
1855 kevent = get_kernel_event_by_name(cmd_ctx->lsm->u.disable.name, kchan);
1856 if (kevent != NULL) {
1857 DBG("Disabling kernel event %s for channel %s.", kevent->event->name,
1858 kchan->channel->name);
1859 ret = kernel_disable_event(kevent);
1860 if (ret < 0) {
1861 ret = LTTCOMM_KERN_ENABLE_FAIL;
1862 goto error;
1863 }
1864 }
1865
1866 kernel_wait_quiescent(kernel_tracer_fd);
1867 break;
1868 default:
1869 /* TODO: Userspace tracing */
1870 ret = LTTCOMM_NOT_IMPLEMENTED;
1871 goto error;
1872 }
1873
1874 ret = LTTCOMM_OK;
1875 break;
1876 }
1877 case LTTNG_DISABLE_ALL_EVENT:
1878 {
1879 struct ltt_kernel_channel *kchan;
1880 struct ltt_kernel_event *kevent;
1881
1882 /* Setup lttng message with no payload */
1883 ret = setup_lttng_msg(cmd_ctx, 0);
1884 if (ret < 0) {
1885 goto setup_error;
1886 }
1887
1888 switch (cmd_ctx->lsm->domain.type) {
1889 case LTTNG_DOMAIN_KERNEL:
1890 DBG("Disabling all enabled kernel events");
1891 kchan = get_kernel_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 /* For each event in the kernel session */
1899 cds_list_for_each_entry(kevent, &kchan->events_list.head, list) {
1900 DBG("Disabling kernel event %s for channel %s.",
1901 kevent->event->name, kchan->channel->name);
1902 ret = kernel_disable_event(kevent);
1903 if (ret < 0) {
1904 continue;
1905 }
1906 }
1907
1908 /* Quiescent wait after event disable */
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_ENABLE_CHANNEL:
1921 {
1922 struct ltt_kernel_channel *kchan;
1923
1924 /* Setup lttng message with no payload */
1925 ret = setup_lttng_msg(cmd_ctx, 0);
1926 if (ret < 0) {
1927 goto setup_error;
1928 }
1929
1930 switch (cmd_ctx->lsm->domain.type) {
1931 case LTTNG_DOMAIN_KERNEL:
1932 kchan = get_kernel_channel_by_name(cmd_ctx->lsm->u.enable.channel_name,
1933 cmd_ctx->session->kernel_session);
1934 if (kchan == NULL) {
1935 /* Channel not found, creating it */
1936 DBG("Creating kernel channel");
1937
1938 ret = kernel_create_channel(cmd_ctx->session->kernel_session,
1939 &cmd_ctx->lsm->u.channel.chan,
1940 cmd_ctx->session->kernel_session->trace_path);
1941 if (ret < 0) {
1942 ret = LTTCOMM_KERN_CHAN_FAIL;
1943 goto error;
1944 }
1945
1946 /* Notify kernel thread that there is a new channel */
1947 ret = notify_kernel_pollfd();
1948 if (ret < 0) {
1949 ret = LTTCOMM_FATAL;
1950 goto error;
1951 }
1952 } else if (kchan->enabled == 0) {
1953 ret = kernel_enable_channel(kchan);
1954 if (ret < 0) {
1955 if (ret != EEXIST) {
1956 ret = LTTCOMM_KERN_CHAN_ENABLE_FAIL;
1957 }
1958 goto error;
1959 }
1960 }
1961
1962 kernel_wait_quiescent(kernel_tracer_fd);
1963 break;
1964 default:
1965 /* TODO: Userspace tracing */
1966 ret = LTTCOMM_NOT_IMPLEMENTED;
1967 goto error;
1968 }
1969
1970 ret = LTTCOMM_OK;
1971 break;
1972 }
1973 case LTTNG_ENABLE_EVENT:
1974 {
1975 char *channel_name;
1976 struct ltt_kernel_channel *kchan;
1977 struct ltt_kernel_event *kevent;
1978 struct lttng_channel *chan;
1979
1980 /* Setup lttng message with no payload */
1981 ret = setup_lttng_msg(cmd_ctx, 0);
1982 if (ret < 0) {
1983 goto setup_error;
1984 }
1985
1986 channel_name = cmd_ctx->lsm->u.enable.channel_name;
1987
1988 switch (cmd_ctx->lsm->domain.type) {
1989 case LTTNG_DOMAIN_KERNEL:
1990 kchan = get_kernel_channel_by_name(channel_name,
1991 cmd_ctx->session->kernel_session);
1992 if (kchan == NULL) {
1993 DBG("Channel not found. Creating channel %s", channel_name);
1994
1995 chan = init_default_channel(cmd_ctx->lsm->domain.type, channel_name);
1996 if (chan == NULL) {
1997 ret = LTTCOMM_FATAL;
1998 goto error;
1999 }
2000
2001 ret = kernel_create_channel(cmd_ctx->session->kernel_session,
2002 chan, cmd_ctx->session->kernel_session->trace_path);
2003 if (ret < 0) {
2004 ret = LTTCOMM_KERN_CHAN_FAIL;
2005 goto error;
2006 }
2007 kchan = get_kernel_channel_by_name(channel_name,
2008 cmd_ctx->session->kernel_session);
2009 if (kchan == NULL) {
2010 ERR("Channel %s not found after creation. Internal error, giving up.",
2011 channel_name);
2012 ret = LTTCOMM_FATAL;
2013 goto error;
2014 }
2015 }
2016
2017 kevent = get_kernel_event_by_name(cmd_ctx->lsm->u.enable.event.name, kchan);
2018 if (kevent == NULL) {
2019 DBG("Creating kernel event %s for channel %s.",
2020 cmd_ctx->lsm->u.enable.event.name, channel_name);
2021 ret = kernel_create_event(&cmd_ctx->lsm->u.enable.event, kchan);
2022 } else {
2023 DBG("Enabling kernel event %s for channel %s.",
2024 kevent->event->name, channel_name);
2025 ret = kernel_enable_event(kevent);
2026 if (ret == -EEXIST) {
2027 ret = LTTCOMM_KERN_EVENT_EXIST;
2028 goto error;
2029 }
2030 }
2031
2032 if (ret < 0) {
2033 ret = LTTCOMM_KERN_ENABLE_FAIL;
2034 goto error;
2035 }
2036
2037 kernel_wait_quiescent(kernel_tracer_fd);
2038 break;
2039 default:
2040 /* TODO: Userspace tracing */
2041 ret = LTTCOMM_NOT_IMPLEMENTED;
2042 goto error;
2043 }
2044 ret = LTTCOMM_OK;
2045 break;
2046 }
2047 case LTTNG_ENABLE_ALL_EVENT:
2048 {
2049 int size, i;
2050 char *channel_name;
2051 struct ltt_kernel_channel *kchan;
2052 struct ltt_kernel_event *kevent;
2053 struct lttng_event *event_list;
2054 struct lttng_channel *chan;
2055
2056 /* Setup lttng message with no payload */
2057 ret = setup_lttng_msg(cmd_ctx, 0);
2058 if (ret < 0) {
2059 goto setup_error;
2060 }
2061
2062 DBG("Enabling all kernel event");
2063
2064 channel_name = cmd_ctx->lsm->u.enable.channel_name;
2065
2066 switch (cmd_ctx->lsm->domain.type) {
2067 case LTTNG_DOMAIN_KERNEL:
2068 kchan = get_kernel_channel_by_name(channel_name,
2069 cmd_ctx->session->kernel_session);
2070 if (kchan == NULL) {
2071 DBG("Channel not found. Creating channel %s", channel_name);
2072
2073 chan = init_default_channel(cmd_ctx->lsm->domain.type, channel_name);
2074 if (chan == NULL) {
2075 ret = LTTCOMM_FATAL;
2076 goto error;
2077 }
2078
2079 ret = kernel_create_channel(cmd_ctx->session->kernel_session,
2080 chan, cmd_ctx->session->kernel_session->trace_path);
2081 if (ret < 0) {
2082 ret = LTTCOMM_KERN_CHAN_FAIL;
2083 goto error;
2084 }
2085 kchan = get_kernel_channel_by_name(channel_name,
2086 cmd_ctx->session->kernel_session);
2087 if (kchan == NULL) {
2088 ERR("Channel %s not found after creation. Internal error, giving up.",
2089 channel_name);
2090 ret = LTTCOMM_FATAL;
2091 goto error;
2092 }
2093 }
2094
2095 /* For each event in the kernel session */
2096 cds_list_for_each_entry(kevent, &kchan->events_list.head, list) {
2097 DBG("Enabling kernel event %s for channel %s.",
2098 kevent->event->name, channel_name);
2099 ret = kernel_enable_event(kevent);
2100 if (ret < 0) {
2101 continue;
2102 }
2103 }
2104
2105 size = kernel_list_events(kernel_tracer_fd, &event_list);
2106 if (size < 0) {
2107 ret = LTTCOMM_KERN_LIST_FAIL;
2108 goto error;
2109 }
2110
2111 for (i = 0; i < size; i++) {
2112 kevent = get_kernel_event_by_name(event_list[i].name, kchan);
2113 if (kevent == NULL) {
2114 /* Default event type for enable all */
2115 event_list[i].type = LTTNG_EVENT_TRACEPOINT;
2116 /* Enable each single tracepoint event */
2117 ret = kernel_create_event(&event_list[i], kchan);
2118 if (ret < 0) {
2119 /* Ignore error here and continue */
2120 }
2121 }
2122 }
2123
2124 free(event_list);
2125
2126 /* Quiescent wait after event enable */
2127 kernel_wait_quiescent(kernel_tracer_fd);
2128 break;
2129 default:
2130 /* TODO: Userspace tracing */
2131 ret = LTTCOMM_NOT_IMPLEMENTED;
2132 goto error;
2133 }
2134
2135 ret = LTTCOMM_OK;
2136 break;
2137 }
2138 case LTTNG_LIST_TRACEPOINTS:
2139 {
2140 struct lttng_event *events;
2141 ssize_t nb_events = 0;
2142
2143 switch (cmd_ctx->lsm->domain.type) {
2144 case LTTNG_DOMAIN_KERNEL:
2145 DBG("Listing kernel events");
2146 nb_events = kernel_list_events(kernel_tracer_fd, &events);
2147 if (nb_events < 0) {
2148 ret = LTTCOMM_KERN_LIST_FAIL;
2149 goto error;
2150 }
2151 break;
2152 default:
2153 /* TODO: Userspace listing */
2154 ret = LTTCOMM_NOT_IMPLEMENTED;
2155 break;
2156 }
2157
2158 /*
2159 * Setup lttng message with payload size set to the event list size in
2160 * bytes and then copy list into the llm payload.
2161 */
2162 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_event) * nb_events);
2163 if (ret < 0) {
2164 free(events);
2165 goto setup_error;
2166 }
2167
2168 /* Copy event list into message payload */
2169 memcpy(cmd_ctx->llm->payload, events,
2170 sizeof(struct lttng_event) * nb_events);
2171
2172 free(events);
2173
2174 ret = LTTCOMM_OK;
2175 break;
2176 }
2177 case LTTNG_START_TRACE:
2178 {
2179 struct ltt_kernel_channel *chan;
2180
2181 /* Setup lttng message with no payload */
2182 ret = setup_lttng_msg(cmd_ctx, 0);
2183 if (ret < 0) {
2184 goto setup_error;
2185 }
2186
2187 /* Kernel tracing */
2188 if (cmd_ctx->session->kernel_session != NULL) {
2189 if (cmd_ctx->session->kernel_session->metadata == NULL) {
2190 DBG("Open kernel metadata");
2191 ret = kernel_open_metadata(cmd_ctx->session->kernel_session,
2192 cmd_ctx->session->kernel_session->trace_path);
2193 if (ret < 0) {
2194 ret = LTTCOMM_KERN_META_FAIL;
2195 goto error;
2196 }
2197 }
2198
2199 if (cmd_ctx->session->kernel_session->metadata_stream_fd == 0) {
2200 DBG("Opening kernel metadata stream");
2201 if (cmd_ctx->session->kernel_session->metadata_stream_fd == 0) {
2202 ret = kernel_open_metadata_stream(cmd_ctx->session->kernel_session);
2203 if (ret < 0) {
2204 ERR("Kernel create metadata stream failed");
2205 ret = LTTCOMM_KERN_STREAM_FAIL;
2206 goto error;
2207 }
2208 }
2209 }
2210
2211 /* For each channel */
2212 cds_list_for_each_entry(chan,
2213 &cmd_ctx->session->kernel_session->channel_list.head, list) {
2214 if (chan->stream_count == 0) {
2215 ret = kernel_open_channel_stream(chan);
2216 if (ret < 0) {
2217 ERR("Kernel create channel stream failed");
2218 ret = LTTCOMM_KERN_STREAM_FAIL;
2219 goto error;
2220 }
2221 /* Update the stream global counter */
2222 cmd_ctx->session->kernel_session->stream_count_global += ret;
2223 }
2224 }
2225
2226 ret = start_kernel_trace(cmd_ctx->session->kernel_session);
2227 if (ret < 0) {
2228 ret = LTTCOMM_KERN_START_FAIL;
2229 goto error;
2230 }
2231
2232 DBG("Start kernel tracing");
2233 ret = kernel_start_session(cmd_ctx->session->kernel_session);
2234 if (ret < 0) {
2235 ERR("Kernel start session failed");
2236 ret = LTTCOMM_KERN_START_FAIL;
2237 goto error;
2238 }
2239
2240 /* Quiescent wait after starting trace */
2241 kernel_wait_quiescent(kernel_tracer_fd);
2242 }
2243
2244 /* TODO: Start all UST traces */
2245
2246 ret = LTTCOMM_OK;
2247 break;
2248 }
2249 case LTTNG_STOP_TRACE:
2250 {
2251 struct ltt_kernel_channel *chan;
2252 /* Setup lttng message with no payload */
2253 ret = setup_lttng_msg(cmd_ctx, 0);
2254 if (ret < 0) {
2255 goto setup_error;
2256 }
2257
2258 /* Kernel tracer */
2259 if (cmd_ctx->session->kernel_session != NULL) {
2260 DBG("Stop kernel tracing");
2261
2262 ret = kernel_metadata_flush_buffer(cmd_ctx->session->kernel_session->metadata_stream_fd);
2263 if (ret < 0) {
2264 ERR("Kernel metadata flush failed");
2265 }
2266
2267 cds_list_for_each_entry(chan, &cmd_ctx->session->kernel_session->channel_list.head, list) {
2268 ret = kernel_flush_buffer(chan);
2269 if (ret < 0) {
2270 ERR("Kernel flush buffer error");
2271 }
2272 }
2273
2274 ret = kernel_stop_session(cmd_ctx->session->kernel_session);
2275 if (ret < 0) {
2276 ERR("Kernel stop session failed");
2277 ret = LTTCOMM_KERN_STOP_FAIL;
2278 goto error;
2279 }
2280
2281 /* Quiescent wait after stopping trace */
2282 kernel_wait_quiescent(kernel_tracer_fd);
2283 }
2284
2285 /* TODO : User-space tracer */
2286
2287 ret = LTTCOMM_OK;
2288 break;
2289 }
2290 case LTTNG_CREATE_SESSION:
2291 {
2292 /* Setup lttng message with no payload */
2293 ret = setup_lttng_msg(cmd_ctx, 0);
2294 if (ret < 0) {
2295 goto setup_error;
2296 }
2297
2298 ret = create_session(cmd_ctx->lsm->session.name, cmd_ctx->lsm->session.path);
2299 if (ret < 0) {
2300 if (ret == -EEXIST) {
2301 ret = LTTCOMM_EXIST_SESS;
2302 } else {
2303 ret = LTTCOMM_FATAL;
2304 }
2305 goto error;
2306 }
2307
2308 ret = LTTCOMM_OK;
2309 break;
2310 }
2311 case LTTNG_DESTROY_SESSION:
2312 {
2313 /* Setup lttng message with no payload */
2314 ret = setup_lttng_msg(cmd_ctx, 0);
2315 if (ret < 0) {
2316 goto setup_error;
2317 }
2318
2319 /* Clean kernel session teardown */
2320 teardown_kernel_session(cmd_ctx->session);
2321
2322 ret = destroy_session(cmd_ctx->lsm->session.name);
2323 if (ret < 0) {
2324 ret = LTTCOMM_FATAL;
2325 goto error;
2326 }
2327
2328 /*
2329 * Must notify the kernel thread here to update it's pollfd in order to
2330 * remove the channel(s)' fd just destroyed.
2331 */
2332 ret = notify_kernel_pollfd();
2333 if (ret < 0) {
2334 ret = LTTCOMM_FATAL;
2335 goto error;
2336 }
2337
2338 ret = LTTCOMM_OK;
2339 break;
2340 }
2341 case LTTNG_LIST_DOMAINS:
2342 {
2343 size_t nb_dom = 0;
2344
2345 if (cmd_ctx->session->kernel_session != NULL) {
2346 nb_dom++;
2347 }
2348
2349 nb_dom += cmd_ctx->session->ust_trace_count;
2350
2351 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_domain) * nb_dom);
2352 if (ret < 0) {
2353 goto setup_error;
2354 }
2355
2356 ((struct lttng_domain *)(cmd_ctx->llm->payload))[0].type =
2357 LTTNG_DOMAIN_KERNEL;
2358
2359 /* TODO: User-space tracer domain support */
2360 ret = LTTCOMM_OK;
2361 break;
2362 }
2363 case LTTNG_LIST_CHANNELS:
2364 {
2365 /*
2366 * TODO: Only kernel channels are listed here. UST listing
2367 * is needed on lttng-ust 2.0 release.
2368 */
2369 size_t nb_chan = 0;
2370 if (cmd_ctx->session->kernel_session != NULL) {
2371 nb_chan += cmd_ctx->session->kernel_session->channel_count;
2372 }
2373
2374 ret = setup_lttng_msg(cmd_ctx,
2375 sizeof(struct lttng_channel) * nb_chan);
2376 if (ret < 0) {
2377 goto setup_error;
2378 }
2379
2380 list_lttng_channels(cmd_ctx->session,
2381 (struct lttng_channel *)(cmd_ctx->llm->payload));
2382
2383 ret = LTTCOMM_OK;
2384 break;
2385 }
2386 case LTTNG_LIST_EVENTS:
2387 {
2388 /*
2389 * TODO: Only kernel events are listed here. UST listing
2390 * is needed on lttng-ust 2.0 release.
2391 */
2392 size_t nb_event = 0;
2393 struct ltt_kernel_channel *kchan = NULL;
2394
2395 if (cmd_ctx->session->kernel_session != NULL) {
2396 kchan = get_kernel_channel_by_name(cmd_ctx->lsm->u.list.channel_name,
2397 cmd_ctx->session->kernel_session);
2398 if (kchan == NULL) {
2399 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
2400 goto error;
2401 }
2402 nb_event += kchan->event_count;
2403 }
2404
2405 ret = setup_lttng_msg(cmd_ctx,
2406 sizeof(struct lttng_event) * nb_event);
2407 if (ret < 0) {
2408 goto setup_error;
2409 }
2410
2411 DBG("Listing events (%zu events)", nb_event);
2412
2413 list_lttng_events(kchan,
2414 (struct lttng_event *)(cmd_ctx->llm->payload));
2415
2416 ret = LTTCOMM_OK;
2417 break;
2418 }
2419 case LTTNG_LIST_SESSIONS:
2420 {
2421 lock_session_list();
2422
2423 if (session_list_ptr->count == 0) {
2424 ret = LTTCOMM_NO_SESSION;
2425 unlock_session_list();
2426 goto error;
2427 }
2428
2429 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_session) *
2430 session_list_ptr->count);
2431 if (ret < 0) {
2432 unlock_session_list();
2433 goto setup_error;
2434 }
2435
2436 /* Filled the session array */
2437 list_lttng_sessions((struct lttng_session *)(cmd_ctx->llm->payload));
2438
2439 unlock_session_list();
2440
2441 ret = LTTCOMM_OK;
2442 break;
2443 }
2444 case LTTNG_CALIBRATE:
2445 {
2446 /* Setup lttng message with no payload */
2447 ret = setup_lttng_msg(cmd_ctx, 0);
2448 if (ret < 0) {
2449 goto setup_error;
2450 }
2451
2452 switch (cmd_ctx->lsm->domain.type) {
2453 case LTTNG_DOMAIN_KERNEL:
2454 {
2455 struct lttng_kernel_calibrate kcalibrate;
2456
2457 kcalibrate.type = cmd_ctx->lsm->u.calibrate.type;
2458 ret = kernel_calibrate(kernel_tracer_fd, &kcalibrate);
2459 if (ret < 0) {
2460 ret = LTTCOMM_KERN_ENABLE_FAIL;
2461 goto error;
2462 }
2463 break;
2464 }
2465 default:
2466 /* TODO: Userspace tracing */
2467 ret = LTTCOMM_NOT_IMPLEMENTED;
2468 goto error;
2469 }
2470 ret = LTTCOMM_OK;
2471 break;
2472 }
2473 case LTTNG_REGISTER_CONSUMER:
2474 {
2475 int sock;
2476
2477 /* Setup lttng message with no payload */
2478 ret = setup_lttng_msg(cmd_ctx, 0);
2479 if (ret < 0) {
2480 goto setup_error;
2481 }
2482
2483 switch (cmd_ctx->lsm->domain.type) {
2484 case LTTNG_DOMAIN_KERNEL:
2485 {
2486 /* Can't register a consumer if there is already one */
2487 if (cmd_ctx->session->kernel_session->consumer_fd != 0) {
2488 ret = LTTCOMM_CONNECT_FAIL;
2489 goto error;
2490 }
2491
2492 sock = lttcomm_connect_unix_sock(cmd_ctx->lsm->u.reg.path);
2493 if (sock < 0) {
2494 ret = LTTCOMM_CONNECT_FAIL;
2495 goto error;
2496 }
2497
2498 cmd_ctx->session->kernel_session->consumer_fd = sock;
2499 break;
2500 }
2501 default:
2502 /* TODO: Userspace tracing */
2503 ret = LTTCOMM_NOT_IMPLEMENTED;
2504 goto error;
2505 }
2506
2507 ret = LTTCOMM_OK;
2508 break;
2509 }
2510
2511 default:
2512 /* Undefined command */
2513 ret = setup_lttng_msg(cmd_ctx, 0);
2514 if (ret < 0) {
2515 goto setup_error;
2516 }
2517
2518 ret = LTTCOMM_UND;
2519 break;
2520 }
2521
2522 /* Set return code */
2523 cmd_ctx->llm->ret_code = ret;
2524
2525 if (cmd_ctx->session) {
2526 unlock_session(cmd_ctx->session);
2527 }
2528
2529 return ret;
2530
2531 error:
2532 if (cmd_ctx->llm == NULL) {
2533 DBG("Missing llm structure. Allocating one.");
2534 if (setup_lttng_msg(cmd_ctx, 0) < 0) {
2535 goto setup_error;
2536 }
2537 }
2538 /* Notify client of error */
2539 cmd_ctx->llm->ret_code = ret;
2540
2541 setup_error:
2542 if (cmd_ctx->session) {
2543 unlock_session(cmd_ctx->session);
2544 }
2545 return ret;
2546 }
2547
2548 /*
2549 * This thread manage all clients request using the unix client socket for
2550 * communication.
2551 */
2552 static void *thread_manage_clients(void *data)
2553 {
2554 int sock = 0, ret;
2555 struct command_ctx *cmd_ctx = NULL;
2556 struct pollfd pollfd[2];
2557
2558 DBG("[thread] Manage client started");
2559
2560 ret = lttcomm_listen_unix_sock(client_sock);
2561 if (ret < 0) {
2562 goto error;
2563 }
2564
2565 /* First fd is always the quit pipe */
2566 pollfd[0].fd = thread_quit_pipe[0];
2567
2568 /* Apps socket */
2569 pollfd[1].fd = client_sock;
2570 pollfd[1].events = POLLIN;
2571
2572 /* Notify parent pid that we are ready
2573 * to accept command for client side.
2574 */
2575 if (opt_sig_parent) {
2576 kill(ppid, SIGCHLD);
2577 }
2578
2579 while (1) {
2580 DBG("Accepting client command ...");
2581
2582 /* Inifinite blocking call, waiting for transmission */
2583 ret = poll(pollfd, 2, -1);
2584 if (ret < 0) {
2585 perror("poll client thread");
2586 goto error;
2587 }
2588
2589 /* Thread quit pipe has been closed. Killing thread. */
2590 if (pollfd[0].revents == POLLNVAL) {
2591 goto error;
2592 } else if (pollfd[1].revents == POLLERR) {
2593 ERR("Client socket poll error");
2594 goto error;
2595 }
2596
2597 sock = lttcomm_accept_unix_sock(client_sock);
2598 if (sock < 0) {
2599 goto error;
2600 }
2601
2602 /* Allocate context command to process the client request */
2603 cmd_ctx = malloc(sizeof(struct command_ctx));
2604
2605 /* Allocate data buffer for reception */
2606 cmd_ctx->lsm = malloc(sizeof(struct lttcomm_session_msg));
2607 cmd_ctx->llm = NULL;
2608 cmd_ctx->session = NULL;
2609
2610 /*
2611 * Data is received from the lttng client. The struct
2612 * lttcomm_session_msg (lsm) contains the command and data request of
2613 * the client.
2614 */
2615 DBG("Receiving data from client ...");
2616 ret = lttcomm_recv_unix_sock(sock, cmd_ctx->lsm, sizeof(struct lttcomm_session_msg));
2617 if (ret <= 0) {
2618 continue;
2619 }
2620
2621 // TODO: Validate cmd_ctx including sanity check for security purpose.
2622
2623 /*
2624 * This function dispatch the work to the kernel or userspace tracer
2625 * libs and fill the lttcomm_lttng_msg data structure of all the needed
2626 * informations for the client. The command context struct contains
2627 * everything this function may needs.
2628 */
2629 ret = process_client_msg(cmd_ctx);
2630 if (ret < 0) {
2631 /* TODO: Inform client somehow of the fatal error. At this point,
2632 * ret < 0 means that a malloc failed (ENOMEM). */
2633 /* Error detected but still accept command */
2634 clean_command_ctx(&cmd_ctx);
2635 continue;
2636 }
2637
2638 DBG("Sending response (size: %d, retcode: %d)",
2639 cmd_ctx->lttng_msg_size, cmd_ctx->llm->ret_code);
2640 ret = send_unix_sock(sock, cmd_ctx->llm, cmd_ctx->lttng_msg_size);
2641 if (ret < 0) {
2642 ERR("Failed to send data back to client");
2643 }
2644
2645 clean_command_ctx(&cmd_ctx);
2646
2647 /* End of transmission */
2648 close(sock);
2649 }
2650
2651 error:
2652 DBG("Client thread dying");
2653 if (client_sock) {
2654 close(client_sock);
2655 }
2656 if (sock) {
2657 close(sock);
2658 }
2659
2660 unlink(client_unix_sock_path);
2661
2662 clean_command_ctx(&cmd_ctx);
2663 return NULL;
2664 }
2665
2666
2667 /*
2668 * usage function on stderr
2669 */
2670 static void usage(void)
2671 {
2672 fprintf(stderr, "Usage: %s OPTIONS\n\nOptions:\n", progname);
2673 fprintf(stderr, " -h, --help Display this usage.\n");
2674 fprintf(stderr, " -c, --client-sock PATH Specify path for the client unix socket\n");
2675 fprintf(stderr, " -a, --apps-sock PATH Specify path for apps unix socket\n");
2676 fprintf(stderr, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
2677 fprintf(stderr, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
2678 fprintf(stderr, " -d, --daemonize Start as a daemon.\n");
2679 fprintf(stderr, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
2680 fprintf(stderr, " -V, --version Show version number.\n");
2681 fprintf(stderr, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
2682 fprintf(stderr, " -q, --quiet No output at all.\n");
2683 fprintf(stderr, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
2684 fprintf(stderr, " --verbose-kconsumerd Verbose mode for kconsumerd. Activate DBG() macro.\n");
2685 }
2686
2687 /*
2688 * daemon argument parsing
2689 */
2690 static int parse_args(int argc, char **argv)
2691 {
2692 int c;
2693
2694 static struct option long_options[] = {
2695 { "client-sock", 1, 0, 'c' },
2696 { "apps-sock", 1, 0, 'a' },
2697 { "kconsumerd-cmd-sock", 1, 0, 0 },
2698 { "kconsumerd-err-sock", 1, 0, 0 },
2699 { "daemonize", 0, 0, 'd' },
2700 { "sig-parent", 0, 0, 'S' },
2701 { "help", 0, 0, 'h' },
2702 { "group", 1, 0, 'g' },
2703 { "version", 0, 0, 'V' },
2704 { "quiet", 0, 0, 'q' },
2705 { "verbose", 0, 0, 'v' },
2706 { "verbose-kconsumerd", 0, 0, 'Z' },
2707 { NULL, 0, 0, 0 }
2708 };
2709
2710 while (1) {
2711 int option_index = 0;
2712 c = getopt_long(argc, argv, "dhqvVS" "a:c:g:s:E:C:Z", long_options, &option_index);
2713 if (c == -1) {
2714 break;
2715 }
2716
2717 switch (c) {
2718 case 0:
2719 fprintf(stderr, "option %s", long_options[option_index].name);
2720 if (optarg) {
2721 fprintf(stderr, " with arg %s\n", optarg);
2722 }
2723 break;
2724 case 'c':
2725 snprintf(client_unix_sock_path, PATH_MAX, "%s", optarg);
2726 break;
2727 case 'a':
2728 snprintf(apps_unix_sock_path, PATH_MAX, "%s", optarg);
2729 break;
2730 case 'd':
2731 opt_daemon = 1;
2732 break;
2733 case 'g':
2734 opt_tracing_group = strdup(optarg);
2735 break;
2736 case 'h':
2737 usage();
2738 exit(EXIT_FAILURE);
2739 case 'V':
2740 fprintf(stdout, "%s\n", VERSION);
2741 exit(EXIT_SUCCESS);
2742 case 'S':
2743 opt_sig_parent = 1;
2744 break;
2745 case 'E':
2746 snprintf(kconsumerd_err_unix_sock_path, PATH_MAX, "%s", optarg);
2747 break;
2748 case 'C':
2749 snprintf(kconsumerd_cmd_unix_sock_path, PATH_MAX, "%s", optarg);
2750 break;
2751 case 'q':
2752 opt_quiet = 1;
2753 break;
2754 case 'v':
2755 /* Verbose level can increase using multiple -v */
2756 opt_verbose += 1;
2757 break;
2758 case 'Z':
2759 opt_verbose_kconsumerd += 1;
2760 break;
2761 default:
2762 /* Unknown option or other error.
2763 * Error is printed by getopt, just return */
2764 return -1;
2765 }
2766 }
2767
2768 return 0;
2769 }
2770
2771 /*
2772 * Creates the two needed socket by the daemon.
2773 * apps_sock - The communication socket for all UST apps.
2774 * client_sock - The communication of the cli tool (lttng).
2775 */
2776 static int init_daemon_socket(void)
2777 {
2778 int ret = 0;
2779 mode_t old_umask;
2780
2781 old_umask = umask(0);
2782
2783 /* Create client tool unix socket */
2784 client_sock = lttcomm_create_unix_sock(client_unix_sock_path);
2785 if (client_sock < 0) {
2786 ERR("Create unix sock failed: %s", client_unix_sock_path);
2787 ret = -1;
2788 goto end;
2789 }
2790
2791 /* File permission MUST be 660 */
2792 ret = chmod(client_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
2793 if (ret < 0) {
2794 ERR("Set file permissions failed: %s", client_unix_sock_path);
2795 perror("chmod");
2796 goto end;
2797 }
2798
2799 /* Create the application unix socket */
2800 apps_sock = lttcomm_create_unix_sock(apps_unix_sock_path);
2801 if (apps_sock < 0) {
2802 ERR("Create unix sock failed: %s", apps_unix_sock_path);
2803 ret = -1;
2804 goto end;
2805 }
2806
2807 /* File permission MUST be 666 */
2808 ret = chmod(apps_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
2809 if (ret < 0) {
2810 ERR("Set file permissions failed: %s", apps_unix_sock_path);
2811 perror("chmod");
2812 goto end;
2813 }
2814
2815 end:
2816 umask(old_umask);
2817 return ret;
2818 }
2819
2820 /*
2821 * Check if the global socket is available, and if a daemon is answering
2822 * at the other side. If yes, error is returned.
2823 */
2824 static int check_existing_daemon(void)
2825 {
2826 if (access(client_unix_sock_path, F_OK) < 0 &&
2827 access(apps_unix_sock_path, F_OK) < 0) {
2828 return 0;
2829 }
2830 /* Is there anybody out there ? */
2831 if (lttng_session_daemon_alive()) {
2832 return -EEXIST;
2833 } else {
2834 return 0;
2835 }
2836 }
2837
2838 /*
2839 * Set the tracing group gid onto the client socket.
2840 *
2841 * Race window between mkdir and chown is OK because we are going from more
2842 * permissive (root.root) to les permissive (root.tracing).
2843 */
2844 static int set_permissions(void)
2845 {
2846 int ret;
2847 gid_t gid;
2848
2849 gid = allowed_group();
2850 if (gid < 0) {
2851 if (is_root) {
2852 WARN("No tracing group detected");
2853 ret = 0;
2854 } else {
2855 ERR("Missing tracing group. Aborting execution.");
2856 ret = -1;
2857 }
2858 goto end;
2859 }
2860
2861 /* Set lttng run dir */
2862 ret = chown(LTTNG_RUNDIR, 0, gid);
2863 if (ret < 0) {
2864 ERR("Unable to set group on " LTTNG_RUNDIR);
2865 perror("chown");
2866 }
2867
2868 /* lttng client socket path */
2869 ret = chown(client_unix_sock_path, 0, gid);
2870 if (ret < 0) {
2871 ERR("Unable to set group on %s", client_unix_sock_path);
2872 perror("chown");
2873 }
2874
2875 /* kconsumerd error socket path */
2876 ret = chown(kconsumerd_err_unix_sock_path, 0, gid);
2877 if (ret < 0) {
2878 ERR("Unable to set group on %s", kconsumerd_err_unix_sock_path);
2879 perror("chown");
2880 }
2881
2882 DBG("All permissions are set");
2883
2884 end:
2885 return ret;
2886 }
2887
2888 /*
2889 * Create the pipe used to wake up the kernel thread.
2890 */
2891 static int create_kernel_poll_pipe(void)
2892 {
2893 return pipe2(kernel_poll_pipe, O_CLOEXEC);
2894 }
2895
2896 /*
2897 * Create the application command pipe to wake thread_manage_apps.
2898 */
2899 static int create_apps_cmd_pipe(void)
2900 {
2901 return pipe2(apps_cmd_pipe, O_CLOEXEC);
2902 }
2903
2904 /*
2905 * Create the lttng run directory needed for all global sockets and pipe.
2906 */
2907 static int create_lttng_rundir(void)
2908 {
2909 int ret;
2910
2911 ret = mkdir(LTTNG_RUNDIR, S_IRWXU | S_IRWXG );
2912 if (ret < 0) {
2913 if (errno != EEXIST) {
2914 ERR("Unable to create " LTTNG_RUNDIR);
2915 goto error;
2916 } else {
2917 ret = 0;
2918 }
2919 }
2920
2921 error:
2922 return ret;
2923 }
2924
2925 /*
2926 * Setup sockets and directory needed by the kconsumerd communication with the
2927 * session daemon.
2928 */
2929 static int set_kconsumerd_sockets(void)
2930 {
2931 int ret;
2932
2933 if (strlen(kconsumerd_err_unix_sock_path) == 0) {
2934 snprintf(kconsumerd_err_unix_sock_path, PATH_MAX, KCONSUMERD_ERR_SOCK_PATH);
2935 }
2936
2937 if (strlen(kconsumerd_cmd_unix_sock_path) == 0) {
2938 snprintf(kconsumerd_cmd_unix_sock_path, PATH_MAX, KCONSUMERD_CMD_SOCK_PATH);
2939 }
2940
2941 ret = mkdir(KCONSUMERD_PATH, S_IRWXU | S_IRWXG);
2942 if (ret < 0) {
2943 if (errno != EEXIST) {
2944 ERR("Failed to create " KCONSUMERD_PATH);
2945 goto error;
2946 }
2947 ret = 0;
2948 }
2949
2950 /* Create the kconsumerd error unix socket */
2951 kconsumerd_err_sock = lttcomm_create_unix_sock(kconsumerd_err_unix_sock_path);
2952 if (kconsumerd_err_sock < 0) {
2953 ERR("Create unix sock failed: %s", kconsumerd_err_unix_sock_path);
2954 ret = -1;
2955 goto error;
2956 }
2957
2958 /* File permission MUST be 660 */
2959 ret = chmod(kconsumerd_err_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
2960 if (ret < 0) {
2961 ERR("Set file permissions failed: %s", kconsumerd_err_unix_sock_path);
2962 perror("chmod");
2963 goto error;
2964 }
2965
2966 error:
2967 return ret;
2968 }
2969
2970 /*
2971 * Signal handler for the daemon
2972 *
2973 * Simply stop all worker threads, leaving main() return gracefully
2974 * after joining all threads and calling cleanup().
2975 */
2976 static void sighandler(int sig)
2977 {
2978 switch (sig) {
2979 case SIGPIPE:
2980 DBG("SIGPIPE catched");
2981 return;
2982 case SIGINT:
2983 DBG("SIGINT catched");
2984 stop_threads();
2985 break;
2986 case SIGTERM:
2987 DBG("SIGTERM catched");
2988 stop_threads();
2989 break;
2990 default:
2991 break;
2992 }
2993 }
2994
2995 /*
2996 * Setup signal handler for :
2997 * SIGINT, SIGTERM, SIGPIPE
2998 */
2999 static int set_signal_handler(void)
3000 {
3001 int ret = 0;
3002 struct sigaction sa;
3003 sigset_t sigset;
3004
3005 if ((ret = sigemptyset(&sigset)) < 0) {
3006 perror("sigemptyset");
3007 return ret;
3008 }
3009
3010 sa.sa_handler = sighandler;
3011 sa.sa_mask = sigset;
3012 sa.sa_flags = 0;
3013 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
3014 perror("sigaction");
3015 return ret;
3016 }
3017
3018 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
3019 perror("sigaction");
3020 return ret;
3021 }
3022
3023 if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) {
3024 perror("sigaction");
3025 return ret;
3026 }
3027
3028 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
3029
3030 return ret;
3031 }
3032
3033 /*
3034 * Set open files limit to unlimited. This daemon can open a large number of
3035 * file descriptors in order to consumer multiple kernel traces.
3036 */
3037 static void set_ulimit(void)
3038 {
3039 int ret;
3040 struct rlimit lim;
3041
3042 /* The kernel does not allowed an infinite limit for open files */
3043 lim.rlim_cur = 65535;
3044 lim.rlim_max = 65535;
3045
3046 ret = setrlimit(RLIMIT_NOFILE, &lim);
3047 if (ret < 0) {
3048 perror("failed to set open files limit");
3049 }
3050 }
3051
3052 /*
3053 * main
3054 */
3055 int main(int argc, char **argv)
3056 {
3057 int ret = 0;
3058 void *status;
3059 const char *home_path;
3060
3061 /* Create thread quit pipe */
3062 if ((ret = init_thread_quit_pipe()) < 0) {
3063 goto error;
3064 }
3065
3066 /* Parse arguments */
3067 progname = argv[0];
3068 if ((ret = parse_args(argc, argv) < 0)) {
3069 goto error;
3070 }
3071
3072 /* Daemonize */
3073 if (opt_daemon) {
3074 ret = daemon(0, 0);
3075 if (ret < 0) {
3076 perror("daemon");
3077 goto error;
3078 }
3079 }
3080
3081 /* Check if daemon is UID = 0 */
3082 is_root = !getuid();
3083
3084 if (is_root) {
3085 ret = create_lttng_rundir();
3086 if (ret < 0) {
3087 goto error;
3088 }
3089
3090 if (strlen(apps_unix_sock_path) == 0) {
3091 snprintf(apps_unix_sock_path, PATH_MAX,
3092 DEFAULT_GLOBAL_APPS_UNIX_SOCK);
3093 }
3094
3095 if (strlen(client_unix_sock_path) == 0) {
3096 snprintf(client_unix_sock_path, PATH_MAX,
3097 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK);
3098 }
3099 } else {
3100 home_path = get_home_dir();
3101 if (home_path == NULL) {
3102 /* TODO: Add --socket PATH option */
3103 ERR("Can't get HOME directory for sockets creation.");
3104 ret = -EPERM;
3105 goto error;
3106 }
3107
3108 if (strlen(apps_unix_sock_path) == 0) {
3109 snprintf(apps_unix_sock_path, PATH_MAX,
3110 DEFAULT_HOME_APPS_UNIX_SOCK, home_path);
3111 }
3112
3113 /* Set the cli tool unix socket path */
3114 if (strlen(client_unix_sock_path) == 0) {
3115 snprintf(client_unix_sock_path, PATH_MAX,
3116 DEFAULT_HOME_CLIENT_UNIX_SOCK, home_path);
3117 }
3118 }
3119
3120 DBG("Client socket path %s", client_unix_sock_path);
3121 DBG("Application socket path %s", apps_unix_sock_path);
3122
3123 /*
3124 * See if daemon already exist.
3125 */
3126 if ((ret = check_existing_daemon()) < 0) {
3127 ERR("Already running daemon.\n");
3128 /*
3129 * We do not goto exit because we must not cleanup()
3130 * because a daemon is already running.
3131 */
3132 goto error;
3133 }
3134
3135 /* After this point, we can safely call cleanup() so goto error is used */
3136
3137 /*
3138 * These actions must be executed as root. We do that *after* setting up
3139 * the sockets path because we MUST make the check for another daemon using
3140 * those paths *before* trying to set the kernel consumer sockets and init
3141 * kernel tracer.
3142 */
3143 if (is_root) {
3144 ret = set_kconsumerd_sockets();
3145 if (ret < 0) {
3146 goto exit;
3147 }
3148
3149 /* Setup kernel tracer */
3150 init_kernel_tracer();
3151
3152 /* Set ulimit for open files */
3153 set_ulimit();
3154 }
3155
3156 if ((ret = set_signal_handler()) < 0) {
3157 goto exit;
3158 }
3159
3160 /* Setup the needed unix socket */
3161 if ((ret = init_daemon_socket()) < 0) {
3162 goto exit;
3163 }
3164
3165 /* Set credentials to socket */
3166 if (is_root && ((ret = set_permissions()) < 0)) {
3167 goto exit;
3168 }
3169
3170 /* Get parent pid if -S, --sig-parent is specified. */
3171 if (opt_sig_parent) {
3172 ppid = getppid();
3173 }
3174
3175 /* Setup the kernel pipe for waking up the kernel thread */
3176 if ((ret = create_kernel_poll_pipe()) < 0) {
3177 goto exit;
3178 }
3179
3180 /* Setup the thread apps communication pipe. */
3181 if ((ret = create_apps_cmd_pipe()) < 0) {
3182 goto exit;
3183 }
3184
3185 /* Init UST command queue. */
3186 cds_wfq_init(&ust_cmd_queue.queue);
3187
3188 /*
3189 * Get session list pointer. This pointer MUST NOT be free().
3190 * This list is statically declared in session.c
3191 */
3192 session_list_ptr = get_session_list();
3193
3194 /* Create thread to manage the client socket */
3195 ret = pthread_create(&client_thread, NULL,
3196 thread_manage_clients, (void *) NULL);
3197 if (ret != 0) {
3198 perror("pthread_create clients");
3199 goto exit_client;
3200 }
3201
3202 /* Create thread to dispatch registration */
3203 ret = pthread_create(&dispatch_thread, NULL,
3204 thread_dispatch_ust_registration, (void *) NULL);
3205 if (ret != 0) {
3206 perror("pthread_create dispatch");
3207 goto exit_dispatch;
3208 }
3209
3210 /* Create thread to manage application registration. */
3211 ret = pthread_create(&reg_apps_thread, NULL,
3212 thread_registration_apps, (void *) NULL);
3213 if (ret != 0) {
3214 perror("pthread_create registration");
3215 goto exit_reg_apps;
3216 }
3217
3218 /* Create thread to manage application socket */
3219 ret = pthread_create(&apps_thread, NULL, thread_manage_apps, (void *) NULL);
3220 if (ret != 0) {
3221 perror("pthread_create apps");
3222 goto exit_apps;
3223 }
3224
3225 /* Create kernel thread to manage kernel event */
3226 ret = pthread_create(&kernel_thread, NULL, thread_manage_kernel, (void *) NULL);
3227 if (ret != 0) {
3228 perror("pthread_create kernel");
3229 goto exit_kernel;
3230 }
3231
3232 ret = pthread_join(kernel_thread, &status);
3233 if (ret != 0) {
3234 perror("pthread_join");
3235 goto error; /* join error, exit without cleanup */
3236 }
3237
3238 exit_kernel:
3239 ret = pthread_join(apps_thread, &status);
3240 if (ret != 0) {
3241 perror("pthread_join");
3242 goto error; /* join error, exit without cleanup */
3243 }
3244
3245 exit_apps:
3246 ret = pthread_join(reg_apps_thread, &status);
3247 if (ret != 0) {
3248 perror("pthread_join");
3249 goto error; /* join error, exit without cleanup */
3250 }
3251
3252 exit_reg_apps:
3253 ret = pthread_join(dispatch_thread, &status);
3254 if (ret != 0) {
3255 perror("pthread_join");
3256 goto error; /* join error, exit without cleanup */
3257 }
3258
3259 exit_dispatch:
3260 ret = pthread_join(client_thread, &status);
3261 if (ret != 0) {
3262 perror("pthread_join");
3263 goto error; /* join error, exit without cleanup */
3264 }
3265
3266 ret = join_kconsumerd_thread();
3267 if (ret != 0) {
3268 perror("join_kconsumerd");
3269 goto error; /* join error, exit without cleanup */
3270 }
3271
3272 exit_client:
3273 exit:
3274 /*
3275 * cleanup() is called when no other thread is running.
3276 */
3277 cleanup();
3278 if (!ret)
3279 exit(EXIT_SUCCESS);
3280 error:
3281 exit(EXIT_FAILURE);
3282 }
This page took 0.184323 seconds and 4 git commands to generate.