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