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