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