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