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