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