Add CPU hotplug support
[lttng-tools.git] / ltt-sessiond / main.c
1 /*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 #define _GNU_SOURCE
20 #include <fcntl.h>
21 #include <getopt.h>
22 #include <grp.h>
23 #include <limits.h>
24 #include <poll.h>
25 #include <pthread.h>
26 #include <semaphore.h>
27 #include <signal.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <sys/ipc.h>
32 #include <sys/mount.h>
33 #include <sys/shm.h>
34 #include <sys/socket.h>
35 #include <sys/stat.h>
36 #include <sys/types.h>
37 #include <sys/time.h>
38 #include <sys/resource.h>
39 #include <unistd.h>
40
41 #include <urcu/list.h> /* URCU list library (-lurcu) */
42 #include <lttng/lttng.h>
43
44 #include "liblttsessiondcomm.h"
45 #include "ltt-sessiond.h"
46 #include "lttngerr.h"
47 #include "kernel-ctl.h"
48 #include "ust-ctl.h"
49 #include "session.h"
50 #include "traceable-app.h"
51 #include "lttng-kconsumerd.h"
52 #include "libustctl.h"
53 #include "utils.h"
54
55 /*
56 * TODO:
57 * teardown: signal SIGTERM handler -> write into pipe. Threads waits
58 * with epoll on pipe and on other pipes/sockets for commands. Main
59 * simply waits on pthread join.
60 */
61
62 /* Const values */
63 const char default_home_dir[] = DEFAULT_HOME_DIR;
64 const char default_tracing_group[] = LTTNG_DEFAULT_TRACING_GROUP;
65 const char default_ust_sock_dir[] = DEFAULT_UST_SOCK_DIR;
66 const char default_global_apps_pipe[] = DEFAULT_GLOBAL_APPS_PIPE;
67
68 /* Variables */
69 int opt_verbose; /* Not static for lttngerr.h */
70 int opt_quiet; /* Not static for lttngerr.h */
71 const char *progname;
72 const char *opt_tracing_group;
73 static int opt_sig_parent;
74 static int opt_daemon;
75 static int is_root; /* Set to 1 if the daemon is running as root */
76 static pid_t ppid; /* Parent PID for --sig-parent option */
77 static pid_t kconsumerd_pid;
78 static struct pollfd *kernel_pollfd;
79
80 static char apps_unix_sock_path[PATH_MAX]; /* Global application Unix socket path */
81 static char client_unix_sock_path[PATH_MAX]; /* Global client Unix socket path */
82 static char kconsumerd_err_unix_sock_path[PATH_MAX]; /* kconsumerd error Unix socket path */
83 static char kconsumerd_cmd_unix_sock_path[PATH_MAX]; /* kconsumerd command Unix socket path */
84
85 /* Sockets and FDs */
86 static int client_sock;
87 static int apps_sock;
88 static int kconsumerd_err_sock;
89 static int kconsumerd_cmd_sock;
90 static int kernel_tracer_fd;
91 static int kernel_poll_pipe[2];
92
93 /* Pthread, Mutexes and Semaphores */
94 static pthread_t kconsumerd_thread;
95 static pthread_t apps_thread;
96 static pthread_t client_thread;
97 static pthread_t kernel_thread;
98 static sem_t kconsumerd_sem;
99
100 static pthread_mutex_t kconsumerd_pid_mutex; /* Mutex to control kconsumerd pid assignation */
101
102 /*
103 * teardown_kernel_session
104 *
105 * Complete teardown of a kernel session. This free all data structure
106 * related to a kernel session and update counter.
107 */
108 static void teardown_kernel_session(struct ltt_session *session)
109 {
110 if (session->kernel_session != NULL) {
111 DBG("Tearing down kernel session");
112 trace_destroy_kernel_session(session->kernel_session);
113 /* Extra precaution */
114 session->kernel_session = NULL;
115 }
116 }
117
118 /*
119 * cleanup
120 *
121 * Cleanup the daemon on exit
122 */
123 static void cleanup()
124 {
125 int ret;
126 char *cmd;
127 struct ltt_session *sess;
128
129 DBG("Cleaning up");
130
131 /* <fun> */
132 MSG("\n%c[%d;%dm*** assert failed *** ==> %c[%dm", 27,1,31,27,0);
133 MSG("%c[%d;%dmMatthew, BEET driven development works!%c[%dm",27,1,33,27,0);
134 /* </fun> */
135
136 /* Stopping all threads */
137 DBG("Terminating all threads");
138 pthread_cancel(client_thread);
139 pthread_cancel(apps_thread);
140 pthread_cancel(kernel_thread);
141 if (kconsumerd_pid != 0) {
142 pthread_cancel(kconsumerd_thread);
143 }
144
145 DBG("Unlinking all unix socket");
146 unlink(client_unix_sock_path);
147 unlink(apps_unix_sock_path);
148 unlink(kconsumerd_err_unix_sock_path);
149
150 DBG("Removing %s directory", LTTNG_RUNDIR);
151 ret = asprintf(&cmd, "rm -rf " LTTNG_RUNDIR);
152 if (ret < 0) {
153 ERR("asprintf failed. Something is really wrong!");
154 }
155
156 /* Remove lttng run directory */
157 ret = system(cmd);
158 if (ret < 0) {
159 ERR("Unable to clean " LTTNG_RUNDIR);
160 }
161
162 DBG("Cleaning up all session");
163 /* Cleanup ALL session */
164 cds_list_for_each_entry(sess, &ltt_session_list.head, list) {
165 teardown_kernel_session(sess);
166 // TODO complete session cleanup (including UST)
167 }
168
169 DBG("Closing kernel fd");
170 close(kernel_tracer_fd);
171 close(kernel_poll_pipe[0]);
172 close(kernel_poll_pipe[1]);
173 }
174
175 /*
176 * send_unix_sock
177 *
178 * Send data on a unix socket using the liblttsessiondcomm API.
179 *
180 * Return lttcomm error code.
181 */
182 static int send_unix_sock(int sock, void *buf, size_t len)
183 {
184 /* Check valid length */
185 if (len <= 0) {
186 return -1;
187 }
188
189 return lttcomm_send_unix_sock(sock, buf, len);
190 }
191
192 /*
193 * clean_command_ctx
194 *
195 * Free memory of a command context structure.
196 */
197 static void clean_command_ctx(struct command_ctx *cmd_ctx)
198 {
199 DBG("Clean command context structure %p", cmd_ctx);
200 if (cmd_ctx) {
201 if (cmd_ctx->llm) {
202 free(cmd_ctx->llm);
203 }
204 if (cmd_ctx->lsm) {
205 free(cmd_ctx->lsm);
206 }
207 free(cmd_ctx);
208 cmd_ctx = NULL;
209 }
210 }
211
212 /*
213 * send_kconsumerd_channel_fds
214 *
215 * Send all stream fds of kernel channel to the consumer.
216 */
217 static int send_kconsumerd_channel_fds(int sock, struct ltt_kernel_channel *channel)
218 {
219 int ret;
220 size_t nb_fd;
221 struct ltt_kernel_stream *stream;
222 struct lttcomm_kconsumerd_header lkh;
223 struct lttcomm_kconsumerd_msg lkm;
224
225 DBG("Sending fds of channel %s to kernel consumer", channel->channel->name);
226
227 nb_fd = channel->stream_count;
228
229 /* Setup header */
230 lkh.payload_size = nb_fd * sizeof(struct lttcomm_kconsumerd_msg);
231 lkh.cmd_type = ADD_STREAM;
232
233 DBG("Sending kconsumerd header");
234
235 ret = lttcomm_send_unix_sock(sock, &lkh, sizeof(struct lttcomm_kconsumerd_header));
236 if (ret < 0) {
237 perror("send kconsumerd header");
238 goto error;
239 }
240
241 cds_list_for_each_entry(stream, &channel->stream_list.head, list) {
242 if (stream->fd != 0) {
243 lkm.fd = stream->fd;
244 lkm.state = stream->state;
245 lkm.max_sb_size = channel->channel->attr.subbuf_size;
246 strncpy(lkm.path_name, stream->pathname, PATH_MAX);
247
248 DBG("Sending fd %d to kconsumerd", lkm.fd);
249
250 ret = lttcomm_send_fds_unix_sock(sock, &lkm, &lkm.fd, 1, sizeof(lkm));
251 if (ret < 0) {
252 perror("send kconsumerd fd");
253 goto error;
254 }
255 }
256 }
257
258 DBG("Kconsumerd channel fds sent");
259
260 return 0;
261
262 error:
263 return ret;
264 }
265
266 /*
267 * send_kconsumerd_fds
268 *
269 * Send all stream fds of the kernel session to the consumer.
270 */
271 static int send_kconsumerd_fds(int sock, struct ltt_kernel_session *session)
272 {
273 int ret;
274 struct ltt_kernel_channel *chan;
275 struct lttcomm_kconsumerd_header lkh;
276 struct lttcomm_kconsumerd_msg lkm;
277
278 /* Setup header */
279 lkh.payload_size = sizeof(struct lttcomm_kconsumerd_msg);
280 lkh.cmd_type = ADD_STREAM;
281
282 DBG("Sending kconsumerd header for metadata");
283
284 ret = lttcomm_send_unix_sock(sock, &lkh, sizeof(struct lttcomm_kconsumerd_header));
285 if (ret < 0) {
286 perror("send kconsumerd header");
287 goto error;
288 }
289
290 DBG("Sending metadata stream fd");
291
292 if (session->metadata_stream_fd != 0) {
293 /* Send metadata stream fd first */
294 lkm.fd = session->metadata_stream_fd;
295 lkm.state = ACTIVE_FD;
296 lkm.max_sb_size = session->metadata->conf->attr.subbuf_size;
297 strncpy(lkm.path_name, session->metadata->pathname, PATH_MAX);
298
299 ret = lttcomm_send_fds_unix_sock(sock, &lkm, &lkm.fd, 1, sizeof(lkm));
300 if (ret < 0) {
301 perror("send kconsumerd fd");
302 goto error;
303 }
304 }
305
306 cds_list_for_each_entry(chan, &session->channel_list.head, list) {
307 ret = send_kconsumerd_channel_fds(sock, chan);
308 if (ret < 0) {
309 goto error;
310 }
311 }
312
313 DBG("Kconsumerd fds (metadata and channel streams) sent");
314
315 return 0;
316
317 error:
318 return ret;
319 }
320
321 /*
322 * ust_connect_app
323 *
324 * Return a socket connected to the libust communication socket
325 * of the application identified by the pid.
326 *
327 * If the pid is not found in the traceable list,
328 * return -1 to indicate error.
329 */
330 static int ust_connect_app(pid_t pid)
331 {
332 int sock;
333 struct ltt_traceable_app *lta;
334
335 DBG("Connect to application pid %d", pid);
336
337 lta = find_app_by_pid(pid);
338 if (lta == NULL) {
339 /* App not found */
340 DBG("Application pid %d not found", pid);
341 return -1;
342 }
343
344 sock = ustctl_connect_pid(lta->pid);
345 if (sock < 0) {
346 ERR("Fail connecting to the PID %d", pid);
347 }
348
349 return sock;
350 }
351
352 /*
353 * notify_apps
354 *
355 * Notify apps by writing 42 to a named pipe using name.
356 * Every applications waiting for a ltt-sessiond will be notified
357 * and re-register automatically to the session daemon.
358 *
359 * Return open or write error value.
360 */
361 static int notify_apps(const char *name)
362 {
363 int fd;
364 int ret = -1;
365
366 DBG("Notify the global application pipe");
367
368 /* Try opening the global pipe */
369 fd = open(name, O_WRONLY);
370 if (fd < 0) {
371 goto error;
372 }
373
374 /* Notify by writing on the pipe */
375 ret = write(fd, "42", 2);
376 if (ret < 0) {
377 perror("write");
378 }
379
380 error:
381 return ret;
382 }
383
384 /*
385 * setup_lttng_msg
386 *
387 * Setup the outgoing data buffer for the response (llm) by allocating the
388 * right amount of memory and copying the original information from the lsm
389 * structure.
390 *
391 * Return total size of the buffer pointed by buf.
392 */
393 static int setup_lttng_msg(struct command_ctx *cmd_ctx, size_t size)
394 {
395 int ret, buf_size;
396
397 buf_size = size;
398
399 cmd_ctx->llm = malloc(sizeof(struct lttcomm_lttng_msg) + buf_size);
400 if (cmd_ctx->llm == NULL) {
401 perror("malloc");
402 ret = -ENOMEM;
403 goto error;
404 }
405
406 /* Copy common data */
407 cmd_ctx->llm->cmd_type = cmd_ctx->lsm->cmd_type;
408 cmd_ctx->llm->pid = cmd_ctx->lsm->pid;
409
410 cmd_ctx->llm->data_size = size;
411 cmd_ctx->lttng_msg_size = sizeof(struct lttcomm_lttng_msg) + buf_size;
412
413 return buf_size;
414
415 error:
416 return ret;
417 }
418
419 /*
420 * update_kernel_pollfd
421 *
422 * Update the kernel pollfd set of all channel fd available over
423 * all tracing session. Add the wakeup pipe at the end of the set.
424 */
425 static int update_kernel_pollfd(void)
426 {
427 int i = 0;
428 unsigned int nb_fd = 1;
429 struct ltt_session *session;
430 struct ltt_kernel_channel *channel;
431
432 DBG("Updating kernel_pollfd");
433
434 /* Get the number of channel of all kernel session */
435 cds_list_for_each_entry(session, &ltt_session_list.head, list) {
436 if (session->kernel_session == NULL) {
437 continue;
438 }
439 nb_fd += session->kernel_session->channel_count;
440 }
441
442 DBG("Resizing kernel_pollfd to size %d", nb_fd);
443
444 kernel_pollfd = realloc(kernel_pollfd, nb_fd * sizeof(struct pollfd));
445 if (kernel_pollfd == NULL) {
446 perror("malloc kernel_pollfd");
447 goto error;
448 }
449
450 cds_list_for_each_entry(session, &ltt_session_list.head, list) {
451 if (session->kernel_session == NULL) {
452 continue;
453 }
454 if (i >= nb_fd) {
455 ERR("To much channel for kernel_pollfd size");
456 break;
457 }
458 cds_list_for_each_entry(channel, &session->kernel_session->channel_list.head, list) {
459 kernel_pollfd[i].fd = channel->fd;
460 kernel_pollfd[i].events = POLLIN | POLLRDNORM;
461 i++;
462 }
463 }
464
465 /* Adding wake up pipe */
466 kernel_pollfd[nb_fd - 1].fd = kernel_poll_pipe[0];
467 kernel_pollfd[nb_fd - 1].events = POLLIN;
468
469 return nb_fd;
470
471 error:
472 return -1;
473 }
474
475 /*
476 * update_kernel_stream
477 *
478 * Find the channel fd from 'fd' over all tracing session. When found, check
479 * for new channel stream and send those stream fds to the kernel consumer.
480 *
481 * Useful for CPU hotplug feature.
482 */
483 static int update_kernel_stream(int fd)
484 {
485 int ret = 0;
486 struct ltt_session *session;
487 struct ltt_kernel_channel *channel;
488
489 DBG("Updating kernel streams for channel fd %d", fd);
490
491 cds_list_for_each_entry(session, &ltt_session_list.head, list) {
492 if (session->kernel_session == NULL) {
493 continue;
494 }
495 cds_list_for_each_entry(channel, &session->kernel_session->channel_list.head, list) {
496 if (channel->fd == fd) {
497 DBG("Channel found, updating kernel streams");
498 ret = kernel_open_channel_stream(channel);
499 if (ret < 0) {
500 goto end;
501 }
502 /*
503 * Have we already sent fds to the consumer? If yes, it means that
504 * tracing is started so it is safe to send our updated stream fds.
505 */
506 if (session->kernel_session->kconsumer_fds_sent == 1) {
507 ret = send_kconsumerd_channel_fds(kconsumerd_cmd_sock, channel);
508 if (ret < 0) {
509 goto end;
510 }
511 }
512 goto end;
513 }
514 }
515 }
516
517 end:
518 return ret;
519 }
520
521 /*
522 * thread_manage_kernel
523 *
524 * This thread manage event coming from the kernel.
525 *
526 * Features supported in this thread:
527 * -) CPU Hotplug
528 */
529 static void *thread_manage_kernel(void *data)
530 {
531 int ret, i, nb_fd = 0;
532 char tmp;
533 int update_poll_flag = 1;
534
535 DBG("Thread manage kernel started");
536
537 while (1) {
538 if (update_poll_flag == 1) {
539 nb_fd = update_kernel_pollfd();
540 if (nb_fd < 0) {
541 goto error;
542 }
543 update_poll_flag = 0;
544 }
545
546 DBG("Polling on %d fds", nb_fd);
547
548 /* Poll infinite value of time */
549 ret = poll(kernel_pollfd, nb_fd, -1);
550 if (ret < 0) {
551 perror("poll kernel thread");
552 goto error;
553 } else if (ret == 0) {
554 /* Should not happen since timeout is infinite */
555 continue;
556 }
557
558 DBG("Kernel poll event triggered");
559
560 /*
561 * Check if the wake up pipe was triggered. If so, the kernel_pollfd
562 * must be updated.
563 */
564 if (kernel_pollfd[nb_fd - 1].revents == POLLIN) {
565 ret = read(kernel_poll_pipe[0], &tmp, 1);
566 update_poll_flag = 1;
567 continue;
568 }
569
570 for (i = 0; i < nb_fd; i++) {
571 switch (kernel_pollfd[i].revents) {
572 /*
573 * New CPU detected by the kernel. Adding kernel stream to kernel
574 * session and updating the kernel consumer
575 */
576 case POLLIN | POLLRDNORM:
577 ret = update_kernel_stream(kernel_pollfd[i].fd);
578 if (ret < 0) {
579 continue;
580 }
581 break;
582 }
583 }
584 }
585
586 error:
587 DBG("Kernel thread dying");
588 if (kernel_pollfd) {
589 free(kernel_pollfd);
590 }
591 return NULL;
592 }
593
594 /*
595 * thread_manage_kconsumerd
596 *
597 * This thread manage the kconsumerd error sent
598 * back to the session daemon.
599 */
600 static void *thread_manage_kconsumerd(void *data)
601 {
602 int sock, ret;
603 enum lttcomm_return_code code;
604
605 DBG("[thread] Manage kconsumerd started");
606
607 ret = lttcomm_listen_unix_sock(kconsumerd_err_sock);
608 if (ret < 0) {
609 goto error;
610 }
611
612 sock = lttcomm_accept_unix_sock(kconsumerd_err_sock);
613 if (sock < 0) {
614 goto error;
615 }
616
617 /* Getting status code from kconsumerd */
618 ret = lttcomm_recv_unix_sock(sock, &code, sizeof(enum lttcomm_return_code));
619 if (ret <= 0) {
620 goto error;
621 }
622
623 if (code == KCONSUMERD_COMMAND_SOCK_READY) {
624 kconsumerd_cmd_sock = lttcomm_connect_unix_sock(kconsumerd_cmd_unix_sock_path);
625 if (kconsumerd_cmd_sock < 0) {
626 sem_post(&kconsumerd_sem);
627 perror("kconsumerd connect");
628 goto error;
629 }
630 /* Signal condition to tell that the kconsumerd is ready */
631 sem_post(&kconsumerd_sem);
632 DBG("Kconsumerd command socket ready");
633 } else {
634 DBG("Kconsumerd error when waiting for SOCK_READY : %s",
635 lttcomm_get_readable_code(-code));
636 goto error;
637 }
638
639 /* Wait for any kconsumerd error */
640 ret = lttcomm_recv_unix_sock(sock, &code, sizeof(enum lttcomm_return_code));
641 if (ret <= 0) {
642 ERR("Kconsumerd closed the command socket");
643 goto error;
644 }
645
646 ERR("Kconsumerd return code : %s", lttcomm_get_readable_code(-code));
647
648 error:
649 kconsumerd_pid = 0;
650 DBG("Kconsumerd thread dying");
651 return NULL;
652 }
653
654 /*
655 * thread_manage_apps
656 *
657 * This thread manage the application socket communication
658 */
659 static void *thread_manage_apps(void *data)
660 {
661 int sock, ret;
662
663 /* TODO: Something more elegant is needed but fine for now */
664 /* FIXME: change all types to either uint8_t, uint32_t, uint64_t
665 * for 32-bit vs 64-bit compat processes. */
666 /* replicate in ust with version number */
667 struct {
668 int reg; /* 1:register, 0:unregister */
669 pid_t pid;
670 uid_t uid;
671 } reg_msg;
672
673 DBG("[thread] Manage apps started");
674
675 ret = lttcomm_listen_unix_sock(apps_sock);
676 if (ret < 0) {
677 goto error;
678 }
679
680 /* Notify all applications to register */
681 notify_apps(default_global_apps_pipe);
682
683 while (1) {
684 DBG("Accepting application registration");
685 /* Blocking call, waiting for transmission */
686 sock = lttcomm_accept_unix_sock(apps_sock);
687 if (sock < 0) {
688 goto error;
689 }
690
691 /* Basic recv here to handle the very simple data
692 * that the libust send to register (reg_msg).
693 */
694 ret = recv(sock, &reg_msg, sizeof(reg_msg), 0);
695 if (ret < 0) {
696 perror("recv");
697 continue;
698 }
699
700 /* Add application to the global traceable list */
701 if (reg_msg.reg == 1) {
702 /* Registering */
703 ret = register_traceable_app(reg_msg.pid, reg_msg.uid);
704 if (ret < 0) {
705 /* register_traceable_app only return an error with
706 * ENOMEM. At this point, we better stop everything.
707 */
708 goto error;
709 }
710 } else {
711 /* Unregistering */
712 unregister_traceable_app(reg_msg.pid);
713 }
714 }
715
716 error:
717
718 return NULL;
719 }
720
721 /*
722 * spawn_kconsumerd_thread
723 *
724 * Start the thread_manage_kconsumerd. This must be done after a kconsumerd
725 * exec or it will fails.
726 */
727 static int spawn_kconsumerd_thread(void)
728 {
729 int ret;
730
731 /* Setup semaphore */
732 sem_init(&kconsumerd_sem, 0, 0);
733
734 ret = pthread_create(&kconsumerd_thread, NULL, thread_manage_kconsumerd, (void *) NULL);
735 if (ret != 0) {
736 perror("pthread_create kconsumerd");
737 goto error;
738 }
739
740 /* Wait for the kconsumerd thread to be ready */
741 sem_wait(&kconsumerd_sem);
742
743 if (kconsumerd_pid == 0) {
744 ERR("Kconsumerd did not start");
745 goto error;
746 }
747
748 return 0;
749
750 error:
751 ret = LTTCOMM_KERN_CONSUMER_FAIL;
752 return ret;
753 }
754
755 /*
756 * spawn_kconsumerd
757 *
758 * Fork and exec a kernel consumer daemon (kconsumerd).
759 *
760 * NOTE: It is very important to fork a kconsumerd BEFORE opening any kernel
761 * file descriptor using the libkernelctl or kernel-ctl functions. So, a
762 * kernel consumer MUST only be spawned before creating a kernel session.
763 *
764 * Return pid if successful else -1.
765 */
766 static pid_t spawn_kconsumerd(void)
767 {
768 int ret;
769 pid_t pid;
770
771 DBG("Spawning kconsumerd");
772
773 pid = fork();
774 if (pid == 0) {
775 /*
776 * Exec kconsumerd.
777 */
778 execlp("ltt-kconsumerd", "ltt-kconsumerd", "--verbose", NULL);
779 if (errno != 0) {
780 perror("kernel start consumer exec");
781 }
782 exit(EXIT_FAILURE);
783 } else if (pid > 0) {
784 ret = pid;
785 goto error;
786 } else {
787 perror("kernel start consumer fork");
788 ret = -errno;
789 goto error;
790 }
791
792 error:
793 return ret;
794 }
795
796 /*
797 * start_kconsumerd
798 *
799 * Spawn the kconsumerd daemon and session daemon thread.
800 */
801 static int start_kconsumerd(void)
802 {
803 int ret;
804
805 pthread_mutex_lock(&kconsumerd_pid_mutex);
806 if (kconsumerd_pid != 0) {
807 goto end;
808 }
809
810 ret = spawn_kconsumerd();
811 if (ret < 0) {
812 ERR("Spawning kconsumerd failed");
813 ret = LTTCOMM_KERN_CONSUMER_FAIL;
814 pthread_mutex_unlock(&kconsumerd_pid_mutex);
815 goto error;
816 }
817
818 /* Setting up the global kconsumerd_pid */
819 kconsumerd_pid = ret;
820 pthread_mutex_unlock(&kconsumerd_pid_mutex);
821
822 DBG("Kconsumerd pid %d", ret);
823
824 DBG("Spawning kconsumerd thread");
825 ret = spawn_kconsumerd_thread();
826 if (ret < 0) {
827 ERR("Fatal error spawning kconsumerd thread");
828 goto error;
829 }
830
831 end:
832 pthread_mutex_unlock(&kconsumerd_pid_mutex);
833 return 0;
834
835 error:
836 return ret;
837 }
838
839 /*
840 * modprobe_kernel_modules
841 */
842 static int modprobe_kernel_modules(void)
843 {
844 int ret = 0, i = 0;
845 char modprobe[256];
846
847 while (kernel_modules_list[i] != NULL) {
848 ret = snprintf(modprobe, sizeof(modprobe), "/sbin/modprobe %s",
849 kernel_modules_list[i]);
850 if (ret < 0) {
851 perror("snprintf modprobe");
852 goto error;
853 }
854 ret = system(modprobe);
855 if (ret < 0) {
856 ERR("Unable to load module %s", kernel_modules_list[i]);
857 }
858 DBG("Modprobe successfully %s", kernel_modules_list[i]);
859 i++;
860 }
861
862 error:
863 return ret;
864 }
865
866 /*
867 * mount_debugfs
868 */
869 static int mount_debugfs(char *path)
870 {
871 int ret;
872 char *type = "debugfs";
873
874 ret = mkdir_recursive(path, S_IRWXU | S_IRWXG);
875 if (ret < 0) {
876 goto error;
877 }
878
879 ret = mount(type, path, type, 0, NULL);
880 if (ret < 0) {
881 perror("mount debugfs");
882 goto error;
883 }
884
885 DBG("Mounted debugfs successfully at %s", path);
886
887 error:
888 return ret;
889 }
890
891 /*
892 * init_kernel_tracer
893 *
894 * Setup necessary data for kernel tracer action.
895 */
896 static void init_kernel_tracer(void)
897 {
898 int ret;
899 char *proc_mounts = "/proc/mounts";
900 char line[256];
901 char *debugfs_path = NULL, *lttng_path;
902 FILE *fp;
903
904 /* Detect debugfs */
905 fp = fopen(proc_mounts, "r");
906 if (fp == NULL) {
907 ERR("Unable to probe %s", proc_mounts);
908 goto error;
909 }
910
911 while (fgets(line, sizeof(line), fp) != NULL) {
912 if (strstr(line, "debugfs") != NULL) {
913 /* Remove first string */
914 strtok(line, " ");
915 /* Dup string here so we can reuse line later on */
916 debugfs_path = strdup(strtok(NULL, " "));
917 DBG("Got debugfs path : %s", debugfs_path);
918 break;
919 }
920 }
921
922 fclose(fp);
923
924 /* Mount debugfs if needded */
925 if (debugfs_path == NULL) {
926 ret = asprintf(&debugfs_path, "/mnt/debugfs");
927 if (ret < 0) {
928 perror("asprintf debugfs path");
929 goto error;
930 }
931 ret = mount_debugfs(debugfs_path);
932 if (ret < 0) {
933 goto error;
934 }
935 }
936
937 /* Modprobe lttng kernel modules */
938 ret = modprobe_kernel_modules();
939 if (ret < 0) {
940 goto error;
941 }
942
943 /* Setup lttng kernel path */
944 ret = asprintf(&lttng_path, "%s/lttng", debugfs_path);
945 if (ret < 0) {
946 perror("asprintf lttng path");
947 goto error;
948 }
949
950 /* Open debugfs lttng */
951 kernel_tracer_fd = open(lttng_path, O_RDWR);
952 if (kernel_tracer_fd < 0) {
953 DBG("Failed to open %s", lttng_path);
954 goto error;
955 }
956
957 free(lttng_path);
958 free(debugfs_path);
959 DBG("Kernel tracer fd %d", kernel_tracer_fd);
960 return;
961
962 error:
963 if (lttng_path) {
964 free(lttng_path);
965 }
966 if (debugfs_path) {
967 free(debugfs_path);
968 }
969 WARN("No kernel tracer available");
970 kernel_tracer_fd = 0;
971 return;
972 }
973
974 /*
975 * start_kernel_trace
976 *
977 * Start tracing by creating trace directory and sending FDs to the kernel
978 * consumer.
979 */
980 static int start_kernel_trace(struct ltt_kernel_session *session)
981 {
982 int ret;
983
984 if (session->kconsumer_fds_sent == 0) {
985 ret = send_kconsumerd_fds(kconsumerd_cmd_sock, session);
986 if (ret < 0) {
987 ERR("Send kconsumerd fds failed");
988 ret = LTTCOMM_KERN_CONSUMER_FAIL;
989 goto error;
990 }
991
992 session->kconsumer_fds_sent = 1;
993 }
994
995 error:
996 return ret;
997 }
998
999 /*
1000 * Notify kernel thread to update it's pollfd.
1001 */
1002 static int notify_kernel_pollfd(void)
1003 {
1004 int ret;
1005
1006 /* Inform kernel thread of the new kernel channel */
1007 ret = write(kernel_poll_pipe[1], "!", 1);
1008 if (ret < 0) {
1009 perror("write kernel poll pipe");
1010 }
1011
1012 return ret;
1013 }
1014
1015 /*
1016 * init_default_channel
1017 *
1018 * Allocate a channel structure and fill it.
1019 */
1020 static struct lttng_channel *init_default_channel(void)
1021 {
1022 struct lttng_channel *chan;
1023
1024 chan = malloc(sizeof(struct lttng_channel));
1025 if (chan == NULL) {
1026 perror("init channel malloc");
1027 goto error;
1028 }
1029
1030 if (snprintf(chan->name, NAME_MAX, DEFAULT_CHANNEL_NAME) < 0) {
1031 perror("snprintf defautl channel name");
1032 return NULL;
1033 }
1034
1035 chan->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
1036 chan->attr.subbuf_size = DEFAULT_CHANNEL_SUBBUF_SIZE;
1037 chan->attr.num_subbuf = DEFAULT_CHANNEL_SUBBUF_NUM;
1038 chan->attr.switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
1039 chan->attr.read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
1040 chan->attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
1041
1042 error:
1043 return chan;
1044 }
1045
1046 /*
1047 * create_kernel_session
1048 *
1049 * Create a kernel tracer session then create the default channel.
1050 */
1051 static int create_kernel_session(struct ltt_session *session)
1052 {
1053 int ret;
1054 struct lttng_channel *chan;
1055
1056 DBG("Creating kernel session");
1057
1058 ret = kernel_create_session(session, kernel_tracer_fd);
1059 if (ret < 0) {
1060 ret = LTTCOMM_KERN_SESS_FAIL;
1061 goto error;
1062 }
1063
1064 chan = init_default_channel();
1065 if (chan == NULL) {
1066 ret = LTTCOMM_FATAL;
1067 goto error;
1068 }
1069
1070 ret = mkdir_recursive(session->path, S_IRWXU | S_IRWXG );
1071 if (ret < 0) {
1072 if (ret != EEXIST) {
1073 ERR("Trace directory creation error");
1074 goto error;
1075 }
1076 }
1077
1078 DBG("Creating default kernel channel %s", DEFAULT_CHANNEL_NAME);
1079
1080 ret = kernel_create_channel(session->kernel_session, chan, session->path);
1081 if (ret < 0) {
1082 ret = LTTCOMM_KERN_CHAN_FAIL;
1083 goto error;
1084 }
1085
1086 ret = notify_kernel_pollfd();
1087
1088 error:
1089 return ret;
1090 }
1091
1092 /*
1093 * process_client_msg
1094 *
1095 * Process the command requested by the lttng client within the command
1096 * context structure. This function make sure that the return structure (llm)
1097 * is set and ready for transmission before returning.
1098 *
1099 * Return any error encountered or 0 for success.
1100 */
1101 static int process_client_msg(struct command_ctx *cmd_ctx)
1102 {
1103 int ret;
1104
1105 DBG("Processing client command %d", cmd_ctx->lsm->cmd_type);
1106
1107 /* Listing commands don't need a session */
1108 switch (cmd_ctx->lsm->cmd_type) {
1109 case LTTNG_CREATE_SESSION:
1110 case LTTNG_LIST_SESSIONS:
1111 case LTTNG_LIST_EVENTS:
1112 case LTTNG_KERNEL_LIST_EVENTS:
1113 case LTTNG_LIST_TRACEABLE_APPS:
1114 break;
1115 default:
1116 DBG("Getting session %s by name", cmd_ctx->lsm->session_name);
1117 cmd_ctx->session = find_session_by_name(cmd_ctx->lsm->session_name);
1118 if (cmd_ctx->session == NULL) {
1119 /* If session name not found */
1120 if (cmd_ctx->lsm->session_name != NULL) {
1121 ret = LTTCOMM_SESS_NOT_FOUND;
1122 } else { /* If no session name specified */
1123 ret = LTTCOMM_SELECT_SESS;
1124 }
1125 goto error;
1126 }
1127 break;
1128 }
1129
1130 /*
1131 * Check kernel command for kernel session.
1132 */
1133 switch (cmd_ctx->lsm->cmd_type) {
1134 case LTTNG_KERNEL_ADD_CONTEXT:
1135 case LTTNG_KERNEL_CREATE_CHANNEL:
1136 case LTTNG_KERNEL_DISABLE_ALL_EVENT:
1137 case LTTNG_KERNEL_DISABLE_CHANNEL:
1138 case LTTNG_KERNEL_DISABLE_EVENT:
1139 case LTTNG_KERNEL_ENABLE_ALL_EVENT:
1140 case LTTNG_KERNEL_ENABLE_CHANNEL:
1141 case LTTNG_KERNEL_ENABLE_EVENT:
1142 case LTTNG_KERNEL_LIST_EVENTS:
1143 /* Kernel tracer check */
1144 if (kernel_tracer_fd == 0) {
1145 init_kernel_tracer();
1146 if (kernel_tracer_fd == 0) {
1147 ret = LTTCOMM_KERN_NA;
1148 goto error;
1149 }
1150 }
1151
1152 /* Need a session for kernel command */
1153 if (cmd_ctx->lsm->cmd_type != LTTNG_KERNEL_LIST_EVENTS &&
1154 cmd_ctx->session->kernel_session == NULL) {
1155
1156 ret = create_kernel_session(cmd_ctx->session);
1157 if (ret < 0) {
1158 ret = LTTCOMM_KERN_SESS_FAIL;
1159 goto error;
1160 }
1161
1162 /* Start the kernel consumer daemon */
1163 if (kconsumerd_pid == 0) {
1164 ret = start_kconsumerd();
1165 if (ret < 0) {
1166 goto error;
1167 }
1168 }
1169 }
1170 }
1171
1172 /* Connect to ust apps if available pid */
1173 if (cmd_ctx->lsm->pid > 0) {
1174 /* Connect to app using ustctl API */
1175 cmd_ctx->ust_sock = ust_connect_app(cmd_ctx->lsm->pid);
1176 if (cmd_ctx->ust_sock < 0) {
1177 ret = LTTCOMM_NO_TRACEABLE;
1178 goto error;
1179 }
1180 }
1181
1182 /* Process by command type */
1183 switch (cmd_ctx->lsm->cmd_type) {
1184 case LTTNG_KERNEL_ADD_CONTEXT:
1185 {
1186 int found = 0, no_event = 0;
1187 struct ltt_kernel_channel *chan;
1188 struct ltt_kernel_event *event;
1189
1190 /* Setup lttng message with no payload */
1191 ret = setup_lttng_msg(cmd_ctx, 0);
1192 if (ret < 0) {
1193 goto setup_error;
1194 }
1195
1196 /* Check if event name is given */
1197 if (strlen(cmd_ctx->lsm->u.context.event_name) == 0) {
1198 no_event = 1;
1199 }
1200
1201 if (strlen(cmd_ctx->lsm->u.context.channel_name) == 0) {
1202 /* Go over all channels */
1203 DBG("Adding context to all channels");
1204 cds_list_for_each_entry(chan,
1205 &cmd_ctx->session->kernel_session->channel_list.head, list) {
1206 if (no_event) {
1207 ret = kernel_add_channel_context(chan,
1208 &cmd_ctx->lsm->u.context.ctx);
1209 if (ret < 0) {
1210 continue;
1211 }
1212 } else {
1213 event = get_kernel_event_by_name(cmd_ctx->lsm->u.context.event_name, chan);
1214 if (event != NULL) {
1215 ret = kernel_add_event_context(event,
1216 &cmd_ctx->lsm->u.context.ctx);
1217 if (ret < 0) {
1218 ret = LTTCOMM_KERN_CONTEXT_FAIL;
1219 goto error;
1220 }
1221 found = 1;
1222 break;
1223 }
1224 }
1225 }
1226 } else {
1227 chan = get_kernel_channel_by_name(cmd_ctx->lsm->u.context.channel_name,
1228 cmd_ctx->session->kernel_session);
1229 if (chan == NULL) {
1230 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
1231 goto error;
1232 }
1233
1234 if (no_event) {
1235 ret = kernel_add_channel_context(chan,
1236 &cmd_ctx->lsm->u.context.ctx);
1237 if (ret < 0) {
1238 ret = LTTCOMM_KERN_CONTEXT_FAIL;
1239 goto error;
1240 }
1241 } else {
1242 event = get_kernel_event_by_name(cmd_ctx->lsm->u.context.event_name, chan);
1243 if (event != NULL) {
1244 ret = kernel_add_event_context(event,
1245 &cmd_ctx->lsm->u.context.ctx);
1246 if (ret < 0) {
1247 ret = LTTCOMM_KERN_CONTEXT_FAIL;
1248 goto error;
1249 }
1250 }
1251 }
1252 }
1253
1254 if (!found && !no_event) {
1255 ret = LTTCOMM_NO_EVENT;
1256 goto error;
1257 }
1258
1259 ret = LTTCOMM_OK;
1260 break;
1261 }
1262 case LTTNG_KERNEL_CREATE_CHANNEL:
1263 {
1264 /* Setup lttng message with no payload */
1265 ret = setup_lttng_msg(cmd_ctx, 0);
1266 if (ret < 0) {
1267 goto setup_error;
1268 }
1269
1270 /* Kernel tracer */
1271 DBG("Creating kernel channel");
1272
1273 ret = kernel_create_channel(cmd_ctx->session->kernel_session,
1274 &cmd_ctx->lsm->u.channel.chan, cmd_ctx->session->path);
1275 if (ret < 0) {
1276 ret = LTTCOMM_KERN_CHAN_FAIL;
1277 goto error;
1278 }
1279
1280 ret = notify_kernel_pollfd();
1281 if (ret < 0) {
1282 ret = LTTCOMM_FATAL;
1283 goto error;
1284 }
1285
1286 ret = LTTCOMM_OK;
1287 break;
1288 }
1289 case LTTNG_KERNEL_DISABLE_CHANNEL:
1290 {
1291 struct ltt_kernel_channel *chan;
1292
1293 /* Setup lttng message with no payload */
1294 ret = setup_lttng_msg(cmd_ctx, 0);
1295 if (ret < 0) {
1296 goto setup_error;
1297 }
1298
1299 chan = get_kernel_channel_by_name(cmd_ctx->lsm->u.disable.channel_name,
1300 cmd_ctx->session->kernel_session);
1301 if (chan == NULL) {
1302 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
1303 goto error;
1304 } else if (chan->enabled == 1) {
1305 ret = kernel_disable_channel(chan);
1306 if (ret < 0) {
1307 if (ret != EEXIST) {
1308 ret = LTTCOMM_KERN_CHAN_DISABLE_FAIL;
1309 }
1310 goto error;
1311 }
1312 }
1313
1314 kernel_wait_quiescent(kernel_tracer_fd);
1315 ret = LTTCOMM_OK;
1316 break;
1317 }
1318 case LTTNG_KERNEL_DISABLE_EVENT:
1319 {
1320 struct ltt_kernel_channel *chan;
1321 struct ltt_kernel_event *ev;
1322
1323 /* Setup lttng message with no payload */
1324 ret = setup_lttng_msg(cmd_ctx, 0);
1325 if (ret < 0) {
1326 goto setup_error;
1327 }
1328
1329 chan = get_kernel_channel_by_name(cmd_ctx->lsm->u.disable.channel_name,
1330 cmd_ctx->session->kernel_session);
1331 if (chan == NULL) {
1332 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
1333 goto error;
1334 }
1335
1336 ev = get_kernel_event_by_name(cmd_ctx->lsm->u.disable.name, chan);
1337 if (ev != NULL) {
1338 DBG("Disabling kernel event %s for channel %s.",
1339 cmd_ctx->lsm->u.disable.name, cmd_ctx->lsm->u.disable.channel_name);
1340 ret = kernel_disable_event(ev);
1341 if (ret < 0) {
1342 ret = LTTCOMM_KERN_ENABLE_FAIL;
1343 goto error;
1344 }
1345 }
1346
1347 kernel_wait_quiescent(kernel_tracer_fd);
1348 ret = LTTCOMM_OK;
1349 break;
1350 }
1351 case LTTNG_KERNEL_DISABLE_ALL_EVENT:
1352 {
1353 struct ltt_kernel_channel *chan;
1354 struct ltt_kernel_event *ev;
1355
1356 /* Setup lttng message with no payload */
1357 ret = setup_lttng_msg(cmd_ctx, 0);
1358 if (ret < 0) {
1359 goto setup_error;
1360 }
1361
1362 DBG("Disabling all enabled kernel events");
1363
1364 chan = get_kernel_channel_by_name(cmd_ctx->lsm->u.disable.channel_name,
1365 cmd_ctx->session->kernel_session);
1366 if (chan == NULL) {
1367 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
1368 goto error;
1369 }
1370
1371 /* For each event in the kernel session */
1372 cds_list_for_each_entry(ev, &chan->events_list.head, list) {
1373 DBG("Disabling kernel event %s for channel %s.",
1374 ev->event->name, cmd_ctx->lsm->u.disable.channel_name);
1375 ret = kernel_disable_event(ev);
1376 if (ret < 0) {
1377 continue;
1378 }
1379 }
1380
1381 /* Quiescent wait after event disable */
1382 kernel_wait_quiescent(kernel_tracer_fd);
1383 ret = LTTCOMM_OK;
1384 break;
1385 }
1386 case LTTNG_KERNEL_ENABLE_CHANNEL:
1387 {
1388 struct ltt_kernel_channel *chan;
1389
1390 /* Setup lttng message with no payload */
1391 ret = setup_lttng_msg(cmd_ctx, 0);
1392 if (ret < 0) {
1393 goto setup_error;
1394 }
1395
1396 chan = get_kernel_channel_by_name(cmd_ctx->lsm->u.enable.channel_name,
1397 cmd_ctx->session->kernel_session);
1398 if (chan == NULL) {
1399 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
1400 goto error;
1401 } else if (chan->enabled == 0) {
1402 ret = kernel_enable_channel(chan);
1403 if (ret < 0) {
1404 if (ret != EEXIST) {
1405 ret = LTTCOMM_KERN_CHAN_ENABLE_FAIL;
1406 }
1407 goto error;
1408 }
1409 }
1410
1411 kernel_wait_quiescent(kernel_tracer_fd);
1412 ret = LTTCOMM_OK;
1413 break;
1414 }
1415 case LTTNG_KERNEL_ENABLE_EVENT:
1416 {
1417 struct ltt_kernel_channel *chan;
1418 struct ltt_kernel_event *ev;
1419
1420 /* Setup lttng message with no payload */
1421 ret = setup_lttng_msg(cmd_ctx, 0);
1422 if (ret < 0) {
1423 goto setup_error;
1424 }
1425
1426 chan = get_kernel_channel_by_name(cmd_ctx->lsm->u.enable.channel_name,
1427 cmd_ctx->session->kernel_session);
1428 if (chan == NULL) {
1429 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
1430 goto error;
1431 }
1432
1433 ev = get_kernel_event_by_name(cmd_ctx->lsm->u.enable.event.name, chan);
1434 if (ev == NULL) {
1435 DBG("Creating kernel event %s for channel %s.",
1436 cmd_ctx->lsm->u.enable.event.name, chan->channel->name);
1437 ret = kernel_create_event(&cmd_ctx->lsm->u.enable.event, chan);
1438 } else {
1439 DBG("Enabling kernel event %s for channel %s.",
1440 cmd_ctx->lsm->u.enable.event.name, chan->channel->name);
1441 ret = kernel_enable_event(ev);
1442 }
1443
1444 if (ret < 0) {
1445 ret = LTTCOMM_KERN_ENABLE_FAIL;
1446 goto error;
1447 }
1448
1449 kernel_wait_quiescent(kernel_tracer_fd);
1450 ret = LTTCOMM_OK;
1451 break;
1452 }
1453 case LTTNG_KERNEL_ENABLE_ALL_EVENT:
1454 {
1455 int pos, size;
1456 char *event_list, *event, *ptr;
1457 struct ltt_kernel_channel *chan;
1458 struct ltt_kernel_event *ev;
1459 struct lttng_event ev_attr;
1460
1461 /* Setup lttng message with no payload */
1462 ret = setup_lttng_msg(cmd_ctx, 0);
1463 if (ret < 0) {
1464 goto setup_error;
1465 }
1466
1467 DBG("Enabling all kernel event");
1468
1469 chan = get_kernel_channel_by_name(cmd_ctx->lsm->u.enable.channel_name,
1470 cmd_ctx->session->kernel_session);
1471 if (chan == NULL) {
1472 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
1473 goto error;
1474 }
1475
1476 /* For each event in the kernel session */
1477 cds_list_for_each_entry(ev, &chan->events_list.head, list) {
1478 DBG("Enabling kernel event %s for channel %s.",
1479 ev->event->name, chan->channel->name);
1480 ret = kernel_enable_event(ev);
1481 if (ret < 0) {
1482 continue;
1483 }
1484 }
1485
1486 size = kernel_list_events(kernel_tracer_fd, &event_list);
1487 if (size < 0) {
1488 ret = LTTCOMM_KERN_LIST_FAIL;
1489 goto error;
1490 }
1491
1492 ptr = event_list;
1493 while ((size = sscanf(ptr, "event { name = %m[^;]; };%n\n", &event, &pos)) == 1) {
1494 ev = get_kernel_event_by_name(event, chan);
1495 if (ev == NULL) {
1496 strncpy(ev_attr.name, event, LTTNG_SYM_NAME_LEN);
1497 /* Default event type for enable all */
1498 ev_attr.type = LTTNG_EVENT_TRACEPOINTS;
1499 /* Enable each single tracepoint event */
1500 ret = kernel_create_event(&ev_attr, chan);
1501 if (ret < 0) {
1502 /* Ignore error here and continue */
1503 }
1504 }
1505
1506 /* Move pointer to the next line */
1507 ptr += pos + 1;
1508 free(event);
1509 }
1510
1511 free(event_list);
1512
1513 /* Quiescent wait after event enable */
1514 kernel_wait_quiescent(kernel_tracer_fd);
1515 ret = LTTCOMM_OK;
1516 break;
1517 }
1518 case LTTNG_KERNEL_LIST_EVENTS:
1519 {
1520 char *event_list;
1521 ssize_t size = 0;
1522
1523 DBG("Listing kernel events");
1524
1525 size = kernel_list_events(kernel_tracer_fd, &event_list);
1526 if (size < 0) {
1527 ret = LTTCOMM_KERN_LIST_FAIL;
1528 goto error;
1529 }
1530
1531 /*
1532 * Setup lttng message with payload size set to the event list size in
1533 * bytes and then copy list into the llm payload.
1534 */
1535 ret = setup_lttng_msg(cmd_ctx, size);
1536 if (ret < 0) {
1537 goto setup_error;
1538 }
1539
1540 /* Copy event list into message payload */
1541 memcpy(cmd_ctx->llm->payload, event_list, size);
1542
1543 free(event_list);
1544
1545 ret = LTTCOMM_OK;
1546 break;
1547 }
1548 case LTTNG_START_TRACE:
1549 {
1550 struct ltt_kernel_channel *chan;
1551
1552 /* Setup lttng message with no payload */
1553 ret = setup_lttng_msg(cmd_ctx, 0);
1554 if (ret < 0) {
1555 goto setup_error;
1556 }
1557
1558 /* Kernel tracing */
1559 if (cmd_ctx->session->kernel_session != NULL) {
1560 if (cmd_ctx->session->kernel_session->metadata == NULL) {
1561 DBG("Open kernel metadata");
1562 ret = kernel_open_metadata(cmd_ctx->session->kernel_session,
1563 cmd_ctx->session->path);
1564 if (ret < 0) {
1565 ret = LTTCOMM_KERN_META_FAIL;
1566 goto error;
1567 }
1568 }
1569
1570 if (cmd_ctx->session->kernel_session->metadata_stream_fd == 0) {
1571 DBG("Opening kernel metadata stream");
1572 if (cmd_ctx->session->kernel_session->metadata_stream_fd == 0) {
1573 ret = kernel_open_metadata_stream(cmd_ctx->session->kernel_session);
1574 if (ret < 0) {
1575 ERR("Kernel create metadata stream failed");
1576 ret = LTTCOMM_KERN_STREAM_FAIL;
1577 goto error;
1578 }
1579 }
1580 }
1581
1582 /* For each channel */
1583 cds_list_for_each_entry(chan, &cmd_ctx->session->kernel_session->channel_list.head, list) {
1584 if (chan->stream_count == 0) {
1585 ret = kernel_open_channel_stream(chan);
1586 if (ret < 0) {
1587 ERR("Kernel create channel stream failed");
1588 ret = LTTCOMM_KERN_STREAM_FAIL;
1589 goto error;
1590 }
1591 /* Update the stream global counter */
1592 cmd_ctx->session->kernel_session->stream_count_global += ret;
1593 }
1594 }
1595
1596 DBG("Start kernel tracing");
1597 ret = kernel_start_session(cmd_ctx->session->kernel_session);
1598 if (ret < 0) {
1599 ERR("Kernel start session failed");
1600 ret = LTTCOMM_KERN_START_FAIL;
1601 goto error;
1602 }
1603
1604 ret = start_kernel_trace(cmd_ctx->session->kernel_session);
1605 if (ret < 0) {
1606 ret = LTTCOMM_KERN_START_FAIL;
1607 goto error;
1608 }
1609
1610 /* Quiescent wait after starting trace */
1611 kernel_wait_quiescent(kernel_tracer_fd);
1612 }
1613
1614 /* TODO: Start all UST traces */
1615
1616 ret = LTTCOMM_OK;
1617 break;
1618 }
1619 case LTTNG_STOP_TRACE:
1620 {
1621 struct ltt_kernel_channel *chan;
1622 /* Setup lttng message with no payload */
1623 ret = setup_lttng_msg(cmd_ctx, 0);
1624 if (ret < 0) {
1625 goto setup_error;
1626 }
1627
1628 /* Kernel tracer */
1629 if (cmd_ctx->session->kernel_session != NULL) {
1630 DBG("Stop kernel tracing");
1631
1632 ret = kernel_metadata_flush_buffer(cmd_ctx->session->kernel_session->metadata_stream_fd);
1633 if (ret < 0) {
1634 ERR("Kernel metadata flush failed");
1635 }
1636
1637 cds_list_for_each_entry(chan, &cmd_ctx->session->kernel_session->channel_list.head, list) {
1638 ret = kernel_flush_buffer(chan);
1639 if (ret < 0) {
1640 ERR("Kernel flush buffer error");
1641 }
1642 }
1643
1644 ret = kernel_stop_session(cmd_ctx->session->kernel_session);
1645 if (ret < 0) {
1646 ERR("Kernel stop session failed");
1647 ret = LTTCOMM_KERN_STOP_FAIL;
1648 goto error;
1649 }
1650
1651 /* Quiescent wait after stopping trace */
1652 kernel_wait_quiescent(kernel_tracer_fd);
1653 }
1654
1655 /* TODO : User-space tracer */
1656
1657 ret = LTTCOMM_OK;
1658 break;
1659 }
1660 case LTTNG_CREATE_SESSION:
1661 {
1662 /* Setup lttng message with no payload */
1663 ret = setup_lttng_msg(cmd_ctx, 0);
1664 if (ret < 0) {
1665 goto setup_error;
1666 }
1667
1668 ret = create_session(cmd_ctx->lsm->session_name, cmd_ctx->lsm->path);
1669 if (ret < 0) {
1670 if (ret == -EEXIST) {
1671 ret = LTTCOMM_EXIST_SESS;
1672 } else {
1673 ret = LTTCOMM_FATAL;
1674 }
1675 goto error;
1676 }
1677
1678 ret = LTTCOMM_OK;
1679 break;
1680 }
1681 case LTTNG_DESTROY_SESSION:
1682 {
1683 /* Setup lttng message with no payload */
1684 ret = setup_lttng_msg(cmd_ctx, 0);
1685 if (ret < 0) {
1686 goto setup_error;
1687 }
1688
1689 /* Clean kernel session teardown */
1690 teardown_kernel_session(cmd_ctx->session);
1691
1692 ret = destroy_session(cmd_ctx->lsm->session_name);
1693 if (ret < 0) {
1694 ret = LTTCOMM_FATAL;
1695 goto error;
1696 }
1697
1698 /*
1699 * Must notify the kernel thread here to update it's pollfd in order to
1700 * remove the channel(s)' fd just destroyed.
1701 */
1702 ret = notify_kernel_pollfd();
1703 if (ret < 0) {
1704 ret = LTTCOMM_FATAL;
1705 goto error;
1706 }
1707
1708 ret = LTTCOMM_OK;
1709 break;
1710 }
1711 /*
1712 case LTTNG_LIST_TRACES:
1713 {
1714 unsigned int trace_count;
1715
1716 trace_count = get_trace_count_per_session(cmd_ctx->session);
1717 if (trace_count == 0) {
1718 ret = LTTCOMM_NO_TRACE;
1719 goto error;
1720 }
1721
1722 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_trace) * trace_count);
1723 if (ret < 0) {
1724 goto setup_error;
1725 }
1726
1727 get_traces_per_session(cmd_ctx->session,
1728 (struct lttng_trace *)(cmd_ctx->llm->payload));
1729
1730 ret = LTTCOMM_OK;
1731 break;
1732 }
1733 */
1734 /*
1735 case UST_CREATE_TRACE:
1736 {
1737 ret = setup_lttng_msg(cmd_ctx, 0);
1738 if (ret < 0) {
1739 goto setup_error;
1740 }
1741
1742 ret = ust_create_trace(cmd_ctx);
1743 if (ret < 0) {
1744 goto error;
1745 }
1746 break;
1747 }
1748 */
1749 case LTTNG_LIST_TRACEABLE_APPS:
1750 {
1751 unsigned int app_count;
1752
1753 app_count = get_app_count();
1754 DBG("Traceable application count : %d", app_count);
1755 if (app_count == 0) {
1756 ret = LTTCOMM_NO_APPS;
1757 goto error;
1758 }
1759
1760 ret = setup_lttng_msg(cmd_ctx, sizeof(pid_t) * app_count);
1761 if (ret < 0) {
1762 goto setup_error;
1763 }
1764
1765 get_app_list_pids((pid_t *)(cmd_ctx->llm->payload));
1766
1767 ret = LTTCOMM_OK;
1768 break;
1769 }
1770 /*
1771 case UST_START_TRACE:
1772 {
1773 ret = setup_lttng_msg(cmd_ctx, 0);
1774 if (ret < 0) {
1775 goto setup_error;
1776 }
1777
1778 ret = ust_start_trace(cmd_ctx);
1779 if (ret < 0) {
1780 goto setup_error;
1781 }
1782 break;
1783 }
1784 case UST_STOP_TRACE:
1785 {
1786 ret = setup_lttng_msg(cmd_ctx, 0);
1787 if (ret < 0) {
1788 goto setup_error;
1789 }
1790
1791 ret = ust_stop_trace(cmd_ctx);
1792 if (ret < 0) {
1793 goto setup_error;
1794 }
1795 break;
1796 }
1797 */
1798 case LTTNG_LIST_SESSIONS:
1799 {
1800 unsigned int session_count;
1801
1802 session_count = get_session_count();
1803 if (session_count == 0) {
1804 ret = LTTCOMM_NO_SESSION;
1805 goto error;
1806 }
1807
1808 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_session) * session_count);
1809 if (ret < 0) {
1810 goto setup_error;
1811 }
1812
1813 get_lttng_session((struct lttng_session *)(cmd_ctx->llm->payload));
1814
1815 ret = LTTCOMM_OK;
1816 break;
1817 }
1818 default:
1819 /* Undefined command */
1820 ret = setup_lttng_msg(cmd_ctx, 0);
1821 if (ret < 0) {
1822 goto setup_error;
1823 }
1824
1825 ret = LTTCOMM_UND;
1826 break;
1827 }
1828
1829 /* Set return code */
1830 cmd_ctx->llm->ret_code = ret;
1831
1832 return ret;
1833
1834 error:
1835 if (cmd_ctx->llm == NULL) {
1836 DBG("Missing llm structure. Allocating one.");
1837 if (setup_lttng_msg(cmd_ctx, 0) < 0) {
1838 goto setup_error;
1839 }
1840 }
1841 /* Notify client of error */
1842 cmd_ctx->llm->ret_code = ret;
1843
1844 setup_error:
1845 return ret;
1846 }
1847
1848 /*
1849 * thread_manage_clients
1850 *
1851 * This thread manage all clients request using the unix
1852 * client socket for communication.
1853 */
1854 static void *thread_manage_clients(void *data)
1855 {
1856 int sock, ret;
1857 struct command_ctx *cmd_ctx;
1858
1859 DBG("[thread] Manage client started");
1860
1861 ret = lttcomm_listen_unix_sock(client_sock);
1862 if (ret < 0) {
1863 goto error;
1864 }
1865
1866 /* Notify parent pid that we are ready
1867 * to accept command for client side.
1868 */
1869 if (opt_sig_parent) {
1870 kill(ppid, SIGCHLD);
1871 }
1872
1873 while (1) {
1874 /* Blocking call, waiting for transmission */
1875 DBG("Accepting client command ...");
1876 sock = lttcomm_accept_unix_sock(client_sock);
1877 if (sock < 0) {
1878 goto error;
1879 }
1880
1881 /* Allocate context command to process the client request */
1882 cmd_ctx = malloc(sizeof(struct command_ctx));
1883
1884 /* Allocate data buffer for reception */
1885 cmd_ctx->lsm = malloc(sizeof(struct lttcomm_session_msg));
1886 cmd_ctx->llm = NULL;
1887 cmd_ctx->session = NULL;
1888
1889 /*
1890 * Data is received from the lttng client. The struct
1891 * lttcomm_session_msg (lsm) contains the command and data request of
1892 * the client.
1893 */
1894 DBG("Receiving data from client ...");
1895 ret = lttcomm_recv_unix_sock(sock, cmd_ctx->lsm, sizeof(struct lttcomm_session_msg));
1896 if (ret <= 0) {
1897 continue;
1898 }
1899
1900 // TODO: Validate cmd_ctx including sanity check for security purpose.
1901
1902 /*
1903 * This function dispatch the work to the kernel or userspace tracer
1904 * libs and fill the lttcomm_lttng_msg data structure of all the needed
1905 * informations for the client. The command context struct contains
1906 * everything this function may needs.
1907 */
1908 ret = process_client_msg(cmd_ctx);
1909 if (ret < 0) {
1910 /* TODO: Inform client somehow of the fatal error. At this point,
1911 * ret < 0 means that a malloc failed (ENOMEM). */
1912 /* Error detected but still accept command */
1913 clean_command_ctx(cmd_ctx);
1914 continue;
1915 }
1916
1917 DBG("Sending response (size: %d, retcode: %d)",
1918 cmd_ctx->lttng_msg_size, cmd_ctx->llm->ret_code);
1919 ret = send_unix_sock(sock, cmd_ctx->llm, cmd_ctx->lttng_msg_size);
1920 if (ret < 0) {
1921 ERR("Failed to send data back to client");
1922 }
1923
1924 clean_command_ctx(cmd_ctx);
1925
1926 /* End of transmission */
1927 close(sock);
1928 }
1929
1930 error:
1931 return NULL;
1932 }
1933
1934
1935 /*
1936 * usage function on stderr
1937 */
1938 static void usage(void)
1939 {
1940 fprintf(stderr, "Usage: %s OPTIONS\n\nOptions:\n", progname);
1941 fprintf(stderr, " -h, --help Display this usage.\n");
1942 fprintf(stderr, " -c, --client-sock PATH Specify path for the client unix socket\n");
1943 fprintf(stderr, " -a, --apps-sock PATH Specify path for apps unix socket\n");
1944 fprintf(stderr, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
1945 fprintf(stderr, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
1946 fprintf(stderr, " -d, --daemonize Start as a daemon.\n");
1947 fprintf(stderr, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
1948 fprintf(stderr, " -V, --version Show version number.\n");
1949 fprintf(stderr, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
1950 fprintf(stderr, " -q, --quiet No output at all.\n");
1951 fprintf(stderr, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
1952 }
1953
1954 /*
1955 * daemon argument parsing
1956 */
1957 static int parse_args(int argc, char **argv)
1958 {
1959 int c;
1960
1961 static struct option long_options[] = {
1962 { "client-sock", 1, 0, 'c' },
1963 { "apps-sock", 1, 0, 'a' },
1964 { "kconsumerd-cmd-sock", 1, 0, 0 },
1965 { "kconsumerd-err-sock", 1, 0, 0 },
1966 { "daemonize", 0, 0, 'd' },
1967 { "sig-parent", 0, 0, 'S' },
1968 { "help", 0, 0, 'h' },
1969 { "group", 1, 0, 'g' },
1970 { "version", 0, 0, 'V' },
1971 { "quiet", 0, 0, 'q' },
1972 { "verbose", 0, 0, 'v' },
1973 { NULL, 0, 0, 0 }
1974 };
1975
1976 while (1) {
1977 int option_index = 0;
1978 c = getopt_long(argc, argv, "dhqvVS" "a:c:g:s:E:C:", long_options, &option_index);
1979 if (c == -1) {
1980 break;
1981 }
1982
1983 switch (c) {
1984 case 0:
1985 fprintf(stderr, "option %s", long_options[option_index].name);
1986 if (optarg) {
1987 fprintf(stderr, " with arg %s\n", optarg);
1988 }
1989 break;
1990 case 'c':
1991 snprintf(client_unix_sock_path, PATH_MAX, "%s", optarg);
1992 break;
1993 case 'a':
1994 snprintf(apps_unix_sock_path, PATH_MAX, "%s", optarg);
1995 break;
1996 case 'd':
1997 opt_daemon = 1;
1998 break;
1999 case 'g':
2000 opt_tracing_group = strdup(optarg);
2001 break;
2002 case 'h':
2003 usage();
2004 exit(EXIT_FAILURE);
2005 case 'V':
2006 fprintf(stdout, "%s\n", VERSION);
2007 exit(EXIT_SUCCESS);
2008 case 'S':
2009 opt_sig_parent = 1;
2010 break;
2011 case 'E':
2012 snprintf(kconsumerd_err_unix_sock_path, PATH_MAX, "%s", optarg);
2013 break;
2014 case 'C':
2015 snprintf(kconsumerd_cmd_unix_sock_path, PATH_MAX, "%s", optarg);
2016 break;
2017 case 'q':
2018 opt_quiet = 1;
2019 break;
2020 case 'v':
2021 opt_verbose = 1;
2022 break;
2023 default:
2024 /* Unknown option or other error.
2025 * Error is printed by getopt, just return */
2026 return -1;
2027 }
2028 }
2029
2030 return 0;
2031 }
2032
2033 /*
2034 * init_daemon_socket
2035 *
2036 * Creates the two needed socket by the daemon.
2037 * apps_sock - The communication socket for all UST apps.
2038 * client_sock - The communication of the cli tool (lttng).
2039 */
2040 static int init_daemon_socket()
2041 {
2042 int ret = 0;
2043 mode_t old_umask;
2044
2045 old_umask = umask(0);
2046
2047 /* Create client tool unix socket */
2048 client_sock = lttcomm_create_unix_sock(client_unix_sock_path);
2049 if (client_sock < 0) {
2050 ERR("Create unix sock failed: %s", client_unix_sock_path);
2051 ret = -1;
2052 goto end;
2053 }
2054
2055 /* File permission MUST be 660 */
2056 ret = chmod(client_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
2057 if (ret < 0) {
2058 ERR("Set file permissions failed: %s", client_unix_sock_path);
2059 perror("chmod");
2060 goto end;
2061 }
2062
2063 /* Create the application unix socket */
2064 apps_sock = lttcomm_create_unix_sock(apps_unix_sock_path);
2065 if (apps_sock < 0) {
2066 ERR("Create unix sock failed: %s", apps_unix_sock_path);
2067 ret = -1;
2068 goto end;
2069 }
2070
2071 /* File permission MUST be 666 */
2072 ret = chmod(apps_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
2073 if (ret < 0) {
2074 ERR("Set file permissions failed: %s", apps_unix_sock_path);
2075 perror("chmod");
2076 goto end;
2077 }
2078
2079 end:
2080 umask(old_umask);
2081 return ret;
2082 }
2083
2084 /*
2085 * check_existing_daemon
2086 *
2087 * Check if the global socket is available.
2088 * If yes, error is returned.
2089 */
2090 static int check_existing_daemon()
2091 {
2092 int ret;
2093
2094 ret = access(client_unix_sock_path, F_OK);
2095 if (ret == 0) {
2096 ret = access(apps_unix_sock_path, F_OK);
2097 }
2098
2099 return ret;
2100 }
2101
2102 /*
2103 * set_permissions
2104 *
2105 * Set the tracing group gid onto the client socket.
2106 *
2107 * Race window between mkdir and chown is OK because we are going from
2108 * more permissive (root.root) to les permissive (root.tracing).
2109 */
2110 static int set_permissions(void)
2111 {
2112 int ret;
2113 struct group *grp;
2114
2115 /* Decide which group name to use */
2116 (opt_tracing_group != NULL) ?
2117 (grp = getgrnam(opt_tracing_group)) :
2118 (grp = getgrnam(default_tracing_group));
2119
2120 if (grp == NULL) {
2121 if (is_root) {
2122 WARN("No tracing group detected");
2123 ret = 0;
2124 } else {
2125 ERR("Missing tracing group. Aborting execution.");
2126 ret = -1;
2127 }
2128 goto end;
2129 }
2130
2131 /* Set lttng run dir */
2132 ret = chown(LTTNG_RUNDIR, 0, grp->gr_gid);
2133 if (ret < 0) {
2134 ERR("Unable to set group on " LTTNG_RUNDIR);
2135 perror("chown");
2136 }
2137
2138 /* lttng client socket path */
2139 ret = chown(client_unix_sock_path, 0, grp->gr_gid);
2140 if (ret < 0) {
2141 ERR("Unable to set group on %s", client_unix_sock_path);
2142 perror("chown");
2143 }
2144
2145 /* kconsumerd error socket path */
2146 ret = chown(kconsumerd_err_unix_sock_path, 0, grp->gr_gid);
2147 if (ret < 0) {
2148 ERR("Unable to set group on %s", kconsumerd_err_unix_sock_path);
2149 perror("chown");
2150 }
2151
2152 DBG("All permissions are set");
2153
2154 end:
2155 return ret;
2156 }
2157
2158 /*
2159 * create_kernel_poll_pipe
2160 *
2161 * Create the pipe used to wake up the kernel thread.
2162 */
2163 static int create_kernel_poll_pipe(void)
2164 {
2165 return pipe2(kernel_poll_pipe, O_CLOEXEC);
2166 }
2167
2168 /*
2169 * create_lttng_rundir
2170 *
2171 * Create the lttng run directory needed for all
2172 * global sockets and pipe.
2173 */
2174 static int create_lttng_rundir(void)
2175 {
2176 int ret;
2177
2178 ret = mkdir(LTTNG_RUNDIR, S_IRWXU | S_IRWXG );
2179 if (ret < 0) {
2180 if (errno != EEXIST) {
2181 ERR("Unable to create " LTTNG_RUNDIR);
2182 goto error;
2183 } else {
2184 ret = 0;
2185 }
2186 }
2187
2188 error:
2189 return ret;
2190 }
2191
2192 /*
2193 * set_kconsumerd_sockets
2194 *
2195 * Setup sockets and directory needed by the kconsumerd
2196 * communication with the session daemon.
2197 */
2198 static int set_kconsumerd_sockets(void)
2199 {
2200 int ret;
2201
2202 if (strlen(kconsumerd_err_unix_sock_path) == 0) {
2203 snprintf(kconsumerd_err_unix_sock_path, PATH_MAX, KCONSUMERD_ERR_SOCK_PATH);
2204 }
2205
2206 if (strlen(kconsumerd_cmd_unix_sock_path) == 0) {
2207 snprintf(kconsumerd_cmd_unix_sock_path, PATH_MAX, KCONSUMERD_CMD_SOCK_PATH);
2208 }
2209
2210 ret = mkdir(KCONSUMERD_PATH, S_IRWXU | S_IRWXG);
2211 if (ret < 0) {
2212 if (errno != EEXIST) {
2213 ERR("Failed to create " KCONSUMERD_PATH);
2214 goto error;
2215 }
2216 ret = 0;
2217 }
2218
2219 /* Create the kconsumerd error unix socket */
2220 kconsumerd_err_sock = lttcomm_create_unix_sock(kconsumerd_err_unix_sock_path);
2221 if (kconsumerd_err_sock < 0) {
2222 ERR("Create unix sock failed: %s", kconsumerd_err_unix_sock_path);
2223 ret = -1;
2224 goto error;
2225 }
2226
2227 /* File permission MUST be 660 */
2228 ret = chmod(kconsumerd_err_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
2229 if (ret < 0) {
2230 ERR("Set file permissions failed: %s", kconsumerd_err_unix_sock_path);
2231 perror("chmod");
2232 goto error;
2233 }
2234
2235 error:
2236 return ret;
2237 }
2238
2239 /*
2240 * sighandler
2241 *
2242 * Signal handler for the daemon
2243 */
2244 static void sighandler(int sig)
2245 {
2246 switch (sig) {
2247 case SIGPIPE:
2248 DBG("SIGPIPE catched");
2249 return;
2250 case SIGINT:
2251 DBG("SIGINT catched");
2252 cleanup();
2253 break;
2254 case SIGTERM:
2255 DBG("SIGTERM catched");
2256 cleanup();
2257 break;
2258 default:
2259 break;
2260 }
2261
2262 exit(EXIT_SUCCESS);
2263 }
2264
2265 /*
2266 * set_signal_handler
2267 *
2268 * Setup signal handler for :
2269 * SIGINT, SIGTERM, SIGPIPE
2270 */
2271 static int set_signal_handler(void)
2272 {
2273 int ret = 0;
2274 struct sigaction sa;
2275 sigset_t sigset;
2276
2277 if ((ret = sigemptyset(&sigset)) < 0) {
2278 perror("sigemptyset");
2279 return ret;
2280 }
2281
2282 sa.sa_handler = sighandler;
2283 sa.sa_mask = sigset;
2284 sa.sa_flags = 0;
2285 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
2286 perror("sigaction");
2287 return ret;
2288 }
2289
2290 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
2291 perror("sigaction");
2292 return ret;
2293 }
2294
2295 if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) {
2296 perror("sigaction");
2297 return ret;
2298 }
2299
2300 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
2301
2302 return ret;
2303 }
2304
2305 /*
2306 * set_ulimit
2307 *
2308 * Set open files limit to unlimited. This daemon can open a large number of
2309 * file descriptors in order to consumer multiple kernel traces.
2310 */
2311 static void set_ulimit(void)
2312 {
2313 int ret;
2314 struct rlimit lim;
2315
2316 lim.rlim_cur = 65535;
2317 lim.rlim_max = 65535;
2318
2319 ret = setrlimit(RLIMIT_NOFILE, &lim);
2320 if (ret < 0) {
2321 perror("failed to set open files limit");
2322 }
2323 }
2324
2325 /*
2326 * main
2327 */
2328 int main(int argc, char **argv)
2329 {
2330 int ret = 0;
2331 void *status;
2332 const char *home_path;
2333
2334 /* Parse arguments */
2335 progname = argv[0];
2336 if ((ret = parse_args(argc, argv) < 0)) {
2337 goto error;
2338 }
2339
2340 /* Daemonize */
2341 if (opt_daemon) {
2342 ret = daemon(0, 0);
2343 if (ret < 0) {
2344 perror("daemon");
2345 goto error;
2346 }
2347 }
2348
2349 /* Check if daemon is UID = 0 */
2350 is_root = !getuid();
2351
2352 if (is_root) {
2353 ret = create_lttng_rundir();
2354 if (ret < 0) {
2355 goto error;
2356 }
2357
2358 if (strlen(apps_unix_sock_path) == 0) {
2359 snprintf(apps_unix_sock_path, PATH_MAX,
2360 DEFAULT_GLOBAL_APPS_UNIX_SOCK);
2361 }
2362
2363 if (strlen(client_unix_sock_path) == 0) {
2364 snprintf(client_unix_sock_path, PATH_MAX,
2365 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK);
2366 }
2367
2368 ret = set_kconsumerd_sockets();
2369 if (ret < 0) {
2370 goto error;
2371 }
2372
2373 /* Setup kernel tracer */
2374 init_kernel_tracer();
2375
2376 /* Set ulimit for open files */
2377 set_ulimit();
2378 } else {
2379 home_path = get_home_dir();
2380 if (home_path == NULL) {
2381 ERR("Can't get HOME directory for sockets creation.\n \
2382 Please specify --socket PATH.");
2383 goto error;
2384 }
2385
2386 if (strlen(apps_unix_sock_path) == 0) {
2387 snprintf(apps_unix_sock_path, PATH_MAX,
2388 DEFAULT_HOME_APPS_UNIX_SOCK, home_path);
2389 }
2390
2391 /* Set the cli tool unix socket path */
2392 if (strlen(client_unix_sock_path) == 0) {
2393 snprintf(client_unix_sock_path, PATH_MAX,
2394 DEFAULT_HOME_CLIENT_UNIX_SOCK, home_path);
2395 }
2396 }
2397
2398 DBG("Client socket path %s", client_unix_sock_path);
2399 DBG("Application socket path %s", apps_unix_sock_path);
2400
2401 /* See if daemon already exist. If any of the two
2402 * socket needed by the daemon are present, this test fails
2403 */
2404 if ((ret = check_existing_daemon()) == 0) {
2405 ERR("Already running daemon.\n");
2406 /* We do not goto error because we must not
2407 * cleanup() because a daemon is already running.
2408 */
2409 exit(EXIT_FAILURE);
2410 }
2411
2412 if (set_signal_handler() < 0) {
2413 goto error;
2414 }
2415
2416 /* Setup the needed unix socket */
2417 if (init_daemon_socket() < 0) {
2418 goto error;
2419 }
2420
2421 /* Set credentials to socket */
2422 if (is_root && (set_permissions() < 0)) {
2423 goto error;
2424 }
2425
2426 /* Get parent pid if -S, --sig-parent is specified. */
2427 if (opt_sig_parent) {
2428 ppid = getppid();
2429 }
2430
2431 /* Setup the kernel pipe for waking up the kernel thread */
2432 if (create_kernel_poll_pipe() < 0) {
2433 goto error;
2434 }
2435
2436 while (1) {
2437 /* Create thread to manage the client socket */
2438 ret = pthread_create(&client_thread, NULL, thread_manage_clients, (void *) NULL);
2439 if (ret != 0) {
2440 perror("pthread_create");
2441 goto error;
2442 }
2443
2444 /* Create thread to manage application socket */
2445 ret = pthread_create(&apps_thread, NULL, thread_manage_apps, (void *) NULL);
2446 if (ret != 0) {
2447 perror("pthread_create");
2448 goto error;
2449 }
2450
2451 /* Create kernel thread to manage kernel event */
2452 ret = pthread_create(&kernel_thread, NULL, thread_manage_kernel, (void *) NULL);
2453 if (ret != 0) {
2454 perror("pthread_create");
2455 goto error;
2456 }
2457
2458 ret = pthread_join(client_thread, &status);
2459 if (ret != 0) {
2460 perror("pthread_join");
2461 goto error;
2462 }
2463 }
2464
2465 cleanup();
2466 exit(EXIT_SUCCESS);
2467
2468 error:
2469 cleanup();
2470 exit(EXIT_FAILURE);
2471 }
This page took 0.123674 seconds and 6 git commands to generate.