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