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