Add enable kernel channel support
[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/shm.h>
32 #include <sys/socket.h>
33 #include <sys/stat.h>
34 #include <sys/types.h>
35 #include <sys/time.h>
36 #include <sys/resource.h>
37 #include <unistd.h>
38
39 #include <urcu/list.h> /* URCU list library (-lurcu) */
40 #include <lttng/lttng.h>
41
42 #include "liblttsessiondcomm.h"
43 #include "ltt-sessiond.h"
44 #include "lttngerr.h"
45 #include "kernel-ctl.h"
46 #include "ust-ctl.h"
47 #include "session.h"
48 #include "traceable-app.h"
49 #include "lttng-kconsumerd.h"
50 #include "libustctl.h"
51
52 /*
53 * TODO:
54 * teardown: signal SIGTERM handler -> write into pipe. Threads waits
55 * with epoll on pipe and on other pipes/sockets for commands. Main
56 * simply waits on pthread join.
57 */
58
59 /* Const values */
60 const char default_home_dir[] = DEFAULT_HOME_DIR;
61 const char default_tracing_group[] = LTTNG_DEFAULT_TRACING_GROUP;
62 const char default_ust_sock_dir[] = DEFAULT_UST_SOCK_DIR;
63 const char default_global_apps_pipe[] = DEFAULT_GLOBAL_APPS_PIPE;
64
65 /* Variables */
66 int opt_verbose; /* Not static for lttngerr.h */
67 int opt_quiet; /* Not static for lttngerr.h */
68 const char *progname;
69 const char *opt_tracing_group;
70 static int opt_sig_parent;
71 static int opt_daemon;
72 static int is_root; /* Set to 1 if the daemon is running as root */
73 static pid_t ppid; /* Parent PID for --sig-parent option */
74 static pid_t kconsumerd_pid;
75
76 static char apps_unix_sock_path[PATH_MAX]; /* Global application Unix socket path */
77 static char client_unix_sock_path[PATH_MAX]; /* Global client Unix socket path */
78 static char kconsumerd_err_unix_sock_path[PATH_MAX]; /* kconsumerd error Unix socket path */
79 static char kconsumerd_cmd_unix_sock_path[PATH_MAX]; /* kconsumerd command Unix socket path */
80
81 /* Sockets and FDs */
82 static int client_sock;
83 static int apps_sock;
84 static int kconsumerd_err_sock;
85 static int kconsumerd_cmd_sock;
86 static int kernel_tracer_fd;
87
88 /* Pthread, Mutexes and Semaphores */
89 static pthread_t kconsumerd_thread;
90 static pthread_t apps_thread;
91 static pthread_t client_thread;
92 static sem_t kconsumerd_sem;
93
94 static pthread_mutex_t kconsumerd_pid_mutex; /* Mutex to control kconsumerd pid assignation */
95
96 /*
97 * teardown_kernel_session
98 *
99 * Complete teardown of a kernel session. This free all data structure
100 * related to a kernel session and update counter.
101 */
102 static void teardown_kernel_session(struct ltt_session *session)
103 {
104 if (session->kernel_session != NULL) {
105 DBG("Tearing down kernel session");
106 trace_destroy_kernel_session(session->kernel_session);
107 /* Extra precaution */
108 session->kernel_session = NULL;
109 /* Decrement session count */
110 session->kern_session_count--;
111 }
112 }
113
114 /*
115 * cleanup
116 *
117 * Cleanup the daemon on exit
118 */
119 static void cleanup()
120 {
121 int ret;
122 char *cmd;
123 struct ltt_session *sess;
124
125 DBG("Cleaning up");
126
127 /* <fun> */
128 MSG("\n%c[%d;%dm*** assert failed *** ==> %c[%dm", 27,1,31,27,0);
129 MSG("%c[%d;%dmMatthew, BEET driven development works!%c[%dm",27,1,33,27,0);
130 /* </fun> */
131
132 /* Stopping all threads */
133 DBG("Terminating all threads");
134 pthread_cancel(client_thread);
135 pthread_cancel(apps_thread);
136 if (kconsumerd_pid != 0) {
137 pthread_cancel(kconsumerd_thread);
138 }
139
140 DBG("Unlinking all unix socket");
141 unlink(client_unix_sock_path);
142 unlink(apps_unix_sock_path);
143 unlink(kconsumerd_err_unix_sock_path);
144
145 DBG("Removing %s directory", LTTNG_RUNDIR);
146 ret = asprintf(&cmd, "rm -rf " LTTNG_RUNDIR);
147 if (ret < 0) {
148 ERR("asprintf failed. Something is really wrong!");
149 }
150
151 /* Remove lttng run directory */
152 ret = system(cmd);
153 if (ret < 0) {
154 ERR("Unable to clean " LTTNG_RUNDIR);
155 }
156
157 DBG("Cleaning up all session");
158 /* Cleanup ALL session */
159 cds_list_for_each_entry(sess, &ltt_session_list.head, list) {
160 teardown_kernel_session(sess);
161 // TODO complete session cleanup (including UST)
162 }
163
164 DBG("Closing kernel fd");
165 close(kernel_tracer_fd);
166 }
167
168 /*
169 * send_unix_sock
170 *
171 * Send data on a unix socket using the liblttsessiondcomm API.
172 *
173 * Return lttcomm error code.
174 */
175 static int send_unix_sock(int sock, void *buf, size_t len)
176 {
177 /* Check valid length */
178 if (len <= 0) {
179 return -1;
180 }
181
182 return lttcomm_send_unix_sock(sock, buf, len);
183 }
184
185 /*
186 * clean_command_ctx
187 *
188 * Free memory of a command context structure.
189 */
190 static void clean_command_ctx(struct command_ctx *cmd_ctx)
191 {
192 DBG("Clean command context structure %p", cmd_ctx);
193 if (cmd_ctx) {
194 if (cmd_ctx->llm) {
195 free(cmd_ctx->llm);
196 }
197 if (cmd_ctx->lsm) {
198 free(cmd_ctx->lsm);
199 }
200 free(cmd_ctx);
201 cmd_ctx = NULL;
202 }
203 }
204
205 /*
206 * send_kconsumerd_fds
207 *
208 * Send all stream fds of the kernel session to the consumer.
209 */
210 static int send_kconsumerd_fds(int sock, struct ltt_kernel_session *session)
211 {
212 int ret;
213 size_t nb_fd;
214 struct ltt_kernel_stream *stream;
215 struct ltt_kernel_channel *chan;
216 struct lttcomm_kconsumerd_header lkh;
217 struct lttcomm_kconsumerd_msg lkm;
218
219 nb_fd = session->stream_count_global;
220
221 /* Setup header */
222 lkh.payload_size = (nb_fd + 1) * sizeof(struct lttcomm_kconsumerd_msg);
223 lkh.cmd_type = ADD_STREAM;
224
225 DBG("Sending kconsumerd header");
226
227 ret = lttcomm_send_unix_sock(sock, &lkh, sizeof(struct lttcomm_kconsumerd_header));
228 if (ret < 0) {
229 perror("send kconsumerd header");
230 goto error;
231 }
232
233 DBG("Sending metadata stream fd");
234
235 /* Send metadata stream fd first */
236 lkm.fd = session->metadata_stream_fd;
237 lkm.state = ACTIVE_FD;
238 lkm.max_sb_size = session->metadata->conf->attr.subbuf_size;
239 strncpy(lkm.path_name, session->metadata->pathname, PATH_MAX);
240
241 ret = lttcomm_send_fds_unix_sock(sock, &lkm, &lkm.fd, 1, sizeof(lkm));
242 if (ret < 0) {
243 perror("send kconsumerd fd");
244 goto error;
245 }
246
247 cds_list_for_each_entry(chan, &session->channel_list.head, list) {
248 cds_list_for_each_entry(stream, &chan->stream_list.head, list) {
249 lkm.fd = stream->fd;
250 lkm.state = stream->state;
251 lkm.max_sb_size = chan->channel->attr.subbuf_size;
252 strncpy(lkm.path_name, stream->pathname, PATH_MAX);
253
254 DBG("Sending fd %d to kconsumerd", lkm.fd);
255
256 ret = lttcomm_send_fds_unix_sock(sock, &lkm, &lkm.fd, 1, sizeof(lkm));
257 if (ret < 0) {
258 perror("send kconsumerd fd");
259 goto error;
260 }
261 }
262 }
263
264 DBG("Kconsumerd fds sent");
265
266 return 0;
267
268 error:
269 return ret;
270 }
271
272 /*
273 * create_trace_dir
274 *
275 * Create the trace output directory.
276 */
277 static int create_trace_dir(struct ltt_kernel_session *session)
278 {
279 int ret;
280 struct ltt_kernel_channel *chan;
281
282 /* Create all channel directories */
283 cds_list_for_each_entry(chan, &session->channel_list.head, list) {
284 DBG("Creating trace directory at %s", chan->pathname);
285 // TODO: recursive create dir
286 ret = mkdir(chan->pathname, S_IRWXU | S_IRWXG );
287 if (ret < 0) {
288 if (ret != EEXIST) {
289 perror("mkdir trace path");
290 ret = -errno;
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("kconsumerd", "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 * init_kernel_tracer
647 *
648 * Setup necessary data for kernel tracer action.
649 */
650 static void init_kernel_tracer(void)
651 {
652 /* Set the global kernel tracer fd */
653 kernel_tracer_fd = open(DEFAULT_KERNEL_TRACER_PATH, O_RDWR);
654 if (kernel_tracer_fd < 0) {
655 WARN("No kernel tracer available");
656 kernel_tracer_fd = 0;
657 }
658
659 DBG("Kernel tracer fd %d", kernel_tracer_fd);
660 }
661
662 /*
663 * start_kernel_trace
664 *
665 * Start tracing by creating trace directory and sending FDs to the kernel
666 * consumer.
667 */
668 static int start_kernel_trace(struct ltt_kernel_session *session)
669 {
670 int ret;
671
672 /* Create trace directory */
673 ret = create_trace_dir(session);
674 if (ret < 0) {
675 if (ret == -EEXIST) {
676 ret = LTTCOMM_KERN_DIR_EXIST;
677 } else {
678 ret = LTTCOMM_KERN_DIR_FAIL;
679 goto error;
680 }
681 }
682
683 if (session->kconsumer_fds_sent == 0) {
684 ret = send_kconsumerd_fds(kconsumerd_cmd_sock, session);
685 if (ret < 0) {
686 ERR("Send kconsumerd fds failed");
687 ret = LTTCOMM_KERN_CONSUMER_FAIL;
688 goto error;
689 }
690
691 session->kconsumer_fds_sent = 1;
692 }
693
694 error:
695 return ret;
696 }
697
698 /*
699 * init_default_channel
700 *
701 * Allocate a channel structure and fill it.
702 */
703 static struct lttng_channel *init_default_channel(void)
704 {
705 struct lttng_channel *chan;
706
707 chan = malloc(sizeof(struct lttng_channel));
708 if (chan == NULL) {
709 perror("init channel malloc");
710 goto error;
711 }
712
713 if (snprintf(chan->name, NAME_MAX, DEFAULT_CHANNEL_NAME) < 0) {
714 perror("snprintf defautl channel name");
715 return NULL;
716 }
717
718 chan->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
719 chan->attr.subbuf_size = DEFAULT_CHANNEL_SUBBUF_SIZE;
720 chan->attr.num_subbuf = DEFAULT_CHANNEL_SUBBUF_NUM;
721 chan->attr.switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
722 chan->attr.read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
723
724 error:
725 return chan;
726 }
727
728 /*
729 * create_kernel_session
730 *
731 * Create a kernel tracer session then create the default channel.
732 */
733 static int create_kernel_session(struct ltt_session *session)
734 {
735 int ret;
736 struct lttng_channel *chan;
737
738 DBG("Creating kernel session");
739
740 ret = kernel_create_session(session, kernel_tracer_fd);
741 if (ret < 0) {
742 ret = LTTCOMM_KERN_SESS_FAIL;
743 goto error;
744 }
745
746 chan = init_default_channel();
747 if (chan == NULL) {
748 ret = LTTCOMM_FATAL;
749 goto error;
750 }
751
752 DBG("Creating default kernel channel %s", DEFAULT_CHANNEL_NAME);
753
754 ret = kernel_create_channel(session->kernel_session, chan);
755 if (ret < 0) {
756 ret = LTTCOMM_KERN_CHAN_FAIL;
757 goto error;
758 }
759
760 error:
761 return ret;
762 }
763
764 /*
765 * process_client_msg
766 *
767 * Process the command requested by the lttng client within the command
768 * context structure. This function make sure that the return structure (llm)
769 * is set and ready for transmission before returning.
770 *
771 * Return any error encountered or 0 for success.
772 */
773 static int process_client_msg(struct command_ctx *cmd_ctx)
774 {
775 int ret;
776
777 DBG("Processing client command %d", cmd_ctx->lsm->cmd_type);
778
779 /* Listing commands don't need a session */
780 switch (cmd_ctx->lsm->cmd_type) {
781 case LTTNG_CREATE_SESSION:
782 case LTTNG_LIST_SESSIONS:
783 case LTTNG_LIST_EVENTS:
784 case LTTNG_KERNEL_LIST_EVENTS:
785 case LTTNG_LIST_TRACEABLE_APPS:
786 break;
787 default:
788 DBG("Getting session %s by name", cmd_ctx->lsm->session_name);
789 cmd_ctx->session = find_session_by_name(cmd_ctx->lsm->session_name);
790 if (cmd_ctx->session == NULL) {
791 /* If session name not found */
792 if (cmd_ctx->lsm->session_name != NULL) {
793 ret = LTTCOMM_SESS_NOT_FOUND;
794 } else { /* If no session name specified */
795 ret = LTTCOMM_SELECT_SESS;
796 }
797 goto error;
798 }
799 break;
800 }
801
802 /*
803 * Check kernel command for kernel session.
804 */
805 switch (cmd_ctx->lsm->cmd_type) {
806 case LTTNG_KERNEL_CREATE_CHANNEL:
807 case LTTNG_KERNEL_DISABLE_ALL_EVENT:
808 case LTTNG_KERNEL_DISABLE_CHANNEL:
809 case LTTNG_KERNEL_DISABLE_EVENT:
810 case LTTNG_KERNEL_ENABLE_ALL_EVENT:
811 case LTTNG_KERNEL_ENABLE_CHANNEL:
812 case LTTNG_KERNEL_ENABLE_EVENT:
813 case LTTNG_KERNEL_LIST_EVENTS:
814 /* Kernel tracer check */
815 if (kernel_tracer_fd == 0) {
816 init_kernel_tracer();
817 if (kernel_tracer_fd == 0) {
818 ret = LTTCOMM_KERN_NA;
819 goto error;
820 }
821 }
822
823 /* Need a session for kernel command */
824 if (cmd_ctx->lsm->cmd_type != LTTNG_KERNEL_LIST_EVENTS &&
825 cmd_ctx->session->kernel_session == NULL) {
826
827 ret = create_kernel_session(cmd_ctx->session);
828 if (ret < 0) {
829 ret = LTTCOMM_KERN_SESS_FAIL;
830 goto error;
831 }
832
833 /* Start the kernel consumer daemon */
834 if (kconsumerd_pid == 0) {
835 ret = start_kconsumerd();
836 if (ret < 0) {
837 goto error;
838 }
839 }
840 }
841 }
842
843 /* Connect to ust apps if available pid */
844 if (cmd_ctx->lsm->pid > 0) {
845 /* Connect to app using ustctl API */
846 cmd_ctx->ust_sock = ust_connect_app(cmd_ctx->lsm->pid);
847 if (cmd_ctx->ust_sock < 0) {
848 ret = LTTCOMM_NO_TRACEABLE;
849 goto error;
850 }
851 }
852
853 /* Process by command type */
854 switch (cmd_ctx->lsm->cmd_type) {
855 case LTTNG_KERNEL_CREATE_CHANNEL:
856 {
857 /* Setup lttng message with no payload */
858 ret = setup_lttng_msg(cmd_ctx, 0);
859 if (ret < 0) {
860 goto setup_error;
861 }
862
863 /* Kernel tracer */
864 DBG("Creating kernel channel");
865
866 ret = kernel_create_channel(cmd_ctx->session->kernel_session,
867 &cmd_ctx->lsm->u.channel.chan);
868 if (ret < 0) {
869 ret = LTTCOMM_KERN_CHAN_FAIL;
870 goto error;
871 }
872
873 ret = LTTCOMM_OK;
874 break;
875 }
876 case LTTNG_KERNEL_DISABLE_EVENT:
877 {
878 struct ltt_kernel_channel *chan;
879 struct ltt_kernel_event *ev;
880
881 /* Setup lttng message with no payload */
882 ret = setup_lttng_msg(cmd_ctx, 0);
883 if (ret < 0) {
884 goto setup_error;
885 }
886
887 chan = get_kernel_channel_by_name(cmd_ctx->lsm->u.disable.channel_name,
888 cmd_ctx->session->kernel_session);
889 if (chan == NULL) {
890 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
891 goto error;
892 }
893
894 ev = get_kernel_event_by_name(cmd_ctx->lsm->u.disable.name, chan);
895 if (ev != NULL) {
896 DBG("Disabling kernel event %s for channel %s.",
897 cmd_ctx->lsm->u.disable.name, cmd_ctx->lsm->u.disable.channel_name);
898 ret = kernel_disable_event(ev);
899 if (ret < 0) {
900 ret = LTTCOMM_KERN_ENABLE_FAIL;
901 goto error;
902 }
903 }
904
905 kernel_wait_quiescent(kernel_tracer_fd);
906 ret = LTTCOMM_OK;
907 break;
908 }
909 case LTTNG_KERNEL_DISABLE_ALL_EVENT:
910 {
911 struct ltt_kernel_channel *chan;
912 struct ltt_kernel_event *ev;
913
914 /* Setup lttng message with no payload */
915 ret = setup_lttng_msg(cmd_ctx, 0);
916 if (ret < 0) {
917 goto setup_error;
918 }
919
920 DBG("Disabling all enabled kernel events");
921
922 chan = get_kernel_channel_by_name(cmd_ctx->lsm->u.disable.channel_name,
923 cmd_ctx->session->kernel_session);
924 if (chan == NULL) {
925 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
926 goto error;
927 }
928
929 /* For each event in the kernel session */
930 cds_list_for_each_entry(ev, &chan->events_list.head, list) {
931 DBG("Disabling kernel event %s for channel %s.",
932 ev->event->name, cmd_ctx->lsm->u.disable.channel_name);
933 ret = kernel_disable_event(ev);
934 if (ret < 0) {
935 continue;
936 }
937 }
938
939 /* Quiescent wait after event disable */
940 kernel_wait_quiescent(kernel_tracer_fd);
941 ret = LTTCOMM_OK;
942 break;
943 }
944 case LTTNG_KERNEL_ENABLE_CHANNEL:
945 {
946 struct ltt_kernel_channel *chan;
947
948 /* Setup lttng message with no payload */
949 ret = setup_lttng_msg(cmd_ctx, 0);
950 if (ret < 0) {
951 goto setup_error;
952 }
953
954 chan = get_kernel_channel_by_name(cmd_ctx->lsm->u.enable.channel_name,
955 cmd_ctx->session->kernel_session);
956 if (chan == NULL) {
957 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
958 goto error;
959 } else if (chan->enabled == 0) {
960 ret = kernel_enable_channel(chan);
961 if (ret < 0) {
962 if (ret != EEXIST) {
963 ret = LTTCOMM_KERN_CHAN_ENABLE_FAIL;
964 }
965 goto error;
966 }
967 }
968
969 kernel_wait_quiescent(kernel_tracer_fd);
970 ret = LTTCOMM_OK;
971 break;
972 }
973 case LTTNG_KERNEL_ENABLE_EVENT:
974 {
975 struct ltt_kernel_channel *chan;
976 struct ltt_kernel_event *ev;
977
978 /* Setup lttng message with no payload */
979 ret = setup_lttng_msg(cmd_ctx, 0);
980 if (ret < 0) {
981 goto setup_error;
982 }
983
984 chan = get_kernel_channel_by_name(cmd_ctx->lsm->u.enable.channel_name,
985 cmd_ctx->session->kernel_session);
986 if (chan == NULL) {
987 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
988 goto error;
989 }
990
991 ev = get_kernel_event_by_name(cmd_ctx->lsm->u.enable.event.name, chan);
992 if (ev == NULL) {
993 DBG("Creating kernel event %s for channel %s.",
994 cmd_ctx->lsm->u.enable.event.name, chan->channel->name);
995 ret = kernel_create_event(&cmd_ctx->lsm->u.enable.event, chan);
996 } else {
997 DBG("Enabling kernel event %s for channel %s.",
998 cmd_ctx->lsm->u.enable.event.name, chan->channel->name);
999 ret = kernel_enable_event(ev);
1000 }
1001
1002 if (ret < 0) {
1003 ret = LTTCOMM_KERN_ENABLE_FAIL;
1004 goto error;
1005 }
1006
1007 kernel_wait_quiescent(kernel_tracer_fd);
1008 ret = LTTCOMM_OK;
1009 break;
1010 }
1011 case LTTNG_KERNEL_ENABLE_ALL_EVENT:
1012 {
1013 int pos, size;
1014 char *event_list, *event, *ptr;
1015 struct ltt_kernel_channel *chan;
1016 struct ltt_kernel_event *ev;
1017 struct lttng_event ev_attr;
1018
1019 /* Setup lttng message with no payload */
1020 ret = setup_lttng_msg(cmd_ctx, 0);
1021 if (ret < 0) {
1022 goto setup_error;
1023 }
1024
1025 DBG("Enabling all kernel event");
1026
1027 chan = get_kernel_channel_by_name(cmd_ctx->lsm->u.enable.channel_name,
1028 cmd_ctx->session->kernel_session);
1029 if (chan == NULL) {
1030 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
1031 goto error;
1032 }
1033
1034 /* For each event in the kernel session */
1035 cds_list_for_each_entry(ev, &chan->events_list.head, list) {
1036 DBG("Enabling kernel event %s for channel %s.",
1037 ev->event->name, chan->channel->name);
1038 ret = kernel_enable_event(ev);
1039 if (ret < 0) {
1040 continue;
1041 }
1042 }
1043
1044 size = kernel_list_events(kernel_tracer_fd, &event_list);
1045 if (size < 0) {
1046 ret = LTTCOMM_KERN_LIST_FAIL;
1047 goto error;
1048 }
1049
1050 ptr = event_list;
1051 while ((size = sscanf(ptr, "event { name = %m[^;]; };%n\n", &event, &pos)) == 1) {
1052 ev = get_kernel_event_by_name(event, chan);
1053 if (ev == NULL) {
1054 strncpy(ev_attr.name, event, LTTNG_SYM_NAME_LEN);
1055 /* Default event type for enable all */
1056 ev_attr.type = LTTNG_EVENT_TRACEPOINTS;
1057 /* Enable each single tracepoint event */
1058 ret = kernel_create_event(&ev_attr, chan);
1059 if (ret < 0) {
1060 /* Ignore error here and continue */
1061 continue;
1062 }
1063 }
1064
1065 /* Move pointer to the next line */
1066 ptr += pos + 1;
1067 free(event);
1068 }
1069
1070 free(event_list);
1071
1072 /* Quiescent wait after event enable */
1073 kernel_wait_quiescent(kernel_tracer_fd);
1074 ret = LTTCOMM_OK;
1075 break;
1076 }
1077 case LTTNG_KERNEL_LIST_EVENTS:
1078 {
1079 char *event_list;
1080 ssize_t size = 0;
1081
1082 DBG("Listing kernel events");
1083
1084 size = kernel_list_events(kernel_tracer_fd, &event_list);
1085 if (size < 0) {
1086 ret = LTTCOMM_KERN_LIST_FAIL;
1087 goto error;
1088 }
1089
1090 /*
1091 * Setup lttng message with payload size set to the event list size in
1092 * bytes and then copy list into the llm payload.
1093 */
1094 ret = setup_lttng_msg(cmd_ctx, size);
1095 if (ret < 0) {
1096 goto setup_error;
1097 }
1098
1099 /* Copy event list into message payload */
1100 memcpy(cmd_ctx->llm->payload, event_list, size);
1101
1102 free(event_list);
1103
1104 ret = LTTCOMM_OK;
1105 break;
1106 }
1107 case LTTNG_START_TRACE:
1108 {
1109 struct ltt_kernel_channel *chan;
1110
1111 /* Setup lttng message with no payload */
1112 ret = setup_lttng_msg(cmd_ctx, 0);
1113 if (ret < 0) {
1114 goto setup_error;
1115 }
1116
1117 /* Kernel tracing */
1118 if (cmd_ctx->session->kernel_session != NULL) {
1119 if (cmd_ctx->session->kernel_session->metadata == NULL) {
1120 DBG("Open kernel metadata");
1121 ret = kernel_open_metadata(cmd_ctx->session->kernel_session);
1122 if (ret < 0) {
1123 ret = LTTCOMM_KERN_META_FAIL;
1124 goto error;
1125 }
1126 }
1127
1128 if (cmd_ctx->session->kernel_session->metadata_stream_fd == 0) {
1129 DBG("Opening kernel metadata stream");
1130 if (cmd_ctx->session->kernel_session->metadata_stream_fd == 0) {
1131 ret = kernel_open_metadata_stream(cmd_ctx->session->kernel_session);
1132 if (ret < 0) {
1133 ERR("Kernel create metadata stream failed");
1134 ret = LTTCOMM_KERN_STREAM_FAIL;
1135 goto error;
1136 }
1137 }
1138 }
1139
1140 /* For each channel */
1141 cds_list_for_each_entry(chan, &cmd_ctx->session->kernel_session->channel_list.head, list) {
1142 if (chan->stream_count == 0) {
1143 ret = kernel_open_channel_stream(chan);
1144 if (ret < 0) {
1145 ERR("Kernel create channel stream failed");
1146 ret = LTTCOMM_KERN_STREAM_FAIL;
1147 goto error;
1148 }
1149 /* Update the stream global counter */
1150 cmd_ctx->session->kernel_session->stream_count_global += ret;
1151 }
1152 }
1153
1154 DBG("Start kernel tracing");
1155 ret = kernel_start_session(cmd_ctx->session->kernel_session);
1156 if (ret < 0) {
1157 ERR("Kernel start session failed");
1158 ret = LTTCOMM_KERN_START_FAIL;
1159 goto error;
1160 }
1161
1162 ret = start_kernel_trace(cmd_ctx->session->kernel_session);
1163 if (ret < 0) {
1164 ret = LTTCOMM_KERN_START_FAIL;
1165 goto error;
1166 }
1167
1168 /* Quiescent wait after starting trace */
1169 kernel_wait_quiescent(kernel_tracer_fd);
1170 }
1171
1172 /* TODO: Start all UST traces */
1173
1174 ret = LTTCOMM_OK;
1175 break;
1176 }
1177 case LTTNG_STOP_TRACE:
1178 {
1179 struct ltt_kernel_channel *chan;
1180 /* Setup lttng message with no payload */
1181 ret = setup_lttng_msg(cmd_ctx, 0);
1182 if (ret < 0) {
1183 goto setup_error;
1184 }
1185
1186 /* Kernel tracer */
1187 if (cmd_ctx->session->kernel_session != NULL) {
1188 DBG("Stop kernel tracing");
1189
1190 ret = kernel_metadata_flush_buffer(cmd_ctx->session->kernel_session->metadata_stream_fd);
1191 if (ret < 0) {
1192 ERR("Kernel metadata flush failed");
1193 }
1194
1195 cds_list_for_each_entry(chan, &cmd_ctx->session->kernel_session->channel_list.head, list) {
1196 ret = kernel_flush_buffer(chan);
1197 if (ret < 0) {
1198 ERR("Kernel flush buffer error");
1199 }
1200 }
1201
1202 ret = kernel_stop_session(cmd_ctx->session->kernel_session);
1203 if (ret < 0) {
1204 ERR("Kernel stop session failed");
1205 ret = LTTCOMM_KERN_STOP_FAIL;
1206 goto error;
1207 }
1208
1209 /* Quiescent wait after stopping trace */
1210 kernel_wait_quiescent(kernel_tracer_fd);
1211 }
1212
1213 /* TODO : User-space tracer */
1214
1215 ret = LTTCOMM_OK;
1216 break;
1217 }
1218 case LTTNG_CREATE_SESSION:
1219 {
1220 /* Setup lttng message with no payload */
1221 ret = setup_lttng_msg(cmd_ctx, 0);
1222 if (ret < 0) {
1223 goto setup_error;
1224 }
1225
1226 ret = create_session(cmd_ctx->lsm->session_name, cmd_ctx->lsm->path);
1227 if (ret < 0) {
1228 if (ret == -EEXIST) {
1229 ret = LTTCOMM_EXIST_SESS;
1230 } else {
1231 ret = LTTCOMM_FATAL;
1232 }
1233 goto error;
1234 }
1235
1236 ret = LTTCOMM_OK;
1237 break;
1238 }
1239 case LTTNG_DESTROY_SESSION:
1240 {
1241 /* Setup lttng message with no payload */
1242 ret = setup_lttng_msg(cmd_ctx, 0);
1243 if (ret < 0) {
1244 goto setup_error;
1245 }
1246
1247 /* Clean kernel session teardown */
1248 teardown_kernel_session(cmd_ctx->session);
1249
1250 ret = destroy_session(cmd_ctx->lsm->session_name);
1251 if (ret < 0) {
1252 ret = LTTCOMM_FATAL;
1253 goto error;
1254 }
1255
1256 ret = LTTCOMM_OK;
1257 break;
1258 }
1259 /*
1260 case LTTNG_LIST_TRACES:
1261 {
1262 unsigned int trace_count;
1263
1264 trace_count = get_trace_count_per_session(cmd_ctx->session);
1265 if (trace_count == 0) {
1266 ret = LTTCOMM_NO_TRACE;
1267 goto error;
1268 }
1269
1270 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_trace) * trace_count);
1271 if (ret < 0) {
1272 goto setup_error;
1273 }
1274
1275 get_traces_per_session(cmd_ctx->session,
1276 (struct lttng_trace *)(cmd_ctx->llm->payload));
1277
1278 ret = LTTCOMM_OK;
1279 break;
1280 }
1281 */
1282 /*
1283 case UST_CREATE_TRACE:
1284 {
1285 ret = setup_lttng_msg(cmd_ctx, 0);
1286 if (ret < 0) {
1287 goto setup_error;
1288 }
1289
1290 ret = ust_create_trace(cmd_ctx);
1291 if (ret < 0) {
1292 goto error;
1293 }
1294 break;
1295 }
1296 */
1297 case LTTNG_LIST_TRACEABLE_APPS:
1298 {
1299 unsigned int app_count;
1300
1301 app_count = get_app_count();
1302 DBG("Traceable application count : %d", app_count);
1303 if (app_count == 0) {
1304 ret = LTTCOMM_NO_APPS;
1305 goto error;
1306 }
1307
1308 ret = setup_lttng_msg(cmd_ctx, sizeof(pid_t) * app_count);
1309 if (ret < 0) {
1310 goto setup_error;
1311 }
1312
1313 get_app_list_pids((pid_t *)(cmd_ctx->llm->payload));
1314
1315 ret = LTTCOMM_OK;
1316 break;
1317 }
1318 /*
1319 case UST_START_TRACE:
1320 {
1321 ret = setup_lttng_msg(cmd_ctx, 0);
1322 if (ret < 0) {
1323 goto setup_error;
1324 }
1325
1326 ret = ust_start_trace(cmd_ctx);
1327 if (ret < 0) {
1328 goto setup_error;
1329 }
1330 break;
1331 }
1332 case UST_STOP_TRACE:
1333 {
1334 ret = setup_lttng_msg(cmd_ctx, 0);
1335 if (ret < 0) {
1336 goto setup_error;
1337 }
1338
1339 ret = ust_stop_trace(cmd_ctx);
1340 if (ret < 0) {
1341 goto setup_error;
1342 }
1343 break;
1344 }
1345 */
1346 case LTTNG_LIST_SESSIONS:
1347 {
1348 unsigned int session_count;
1349
1350 session_count = get_session_count();
1351 if (session_count == 0) {
1352 ret = LTTCOMM_NO_SESSION;
1353 goto error;
1354 }
1355
1356 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_session) * session_count);
1357 if (ret < 0) {
1358 goto setup_error;
1359 }
1360
1361 get_lttng_session((struct lttng_session *)(cmd_ctx->llm->payload));
1362
1363 ret = LTTCOMM_OK;
1364 break;
1365 }
1366 default:
1367 /* Undefined command */
1368 ret = setup_lttng_msg(cmd_ctx, 0);
1369 if (ret < 0) {
1370 goto setup_error;
1371 }
1372
1373 ret = LTTCOMM_UND;
1374 break;
1375 }
1376
1377 /* Set return code */
1378 cmd_ctx->llm->ret_code = ret;
1379
1380 return ret;
1381
1382 error:
1383 if (cmd_ctx->llm == NULL) {
1384 DBG("Missing llm structure. Allocating one.");
1385 if (setup_lttng_msg(cmd_ctx, 0) < 0) {
1386 goto setup_error;
1387 }
1388 }
1389 /* Notify client of error */
1390 cmd_ctx->llm->ret_code = ret;
1391
1392 setup_error:
1393 return ret;
1394 }
1395
1396 /*
1397 * thread_manage_clients
1398 *
1399 * This thread manage all clients request using the unix
1400 * client socket for communication.
1401 */
1402 static void *thread_manage_clients(void *data)
1403 {
1404 int sock, ret;
1405 struct command_ctx *cmd_ctx;
1406
1407 DBG("[thread] Manage client started");
1408
1409 ret = lttcomm_listen_unix_sock(client_sock);
1410 if (ret < 0) {
1411 goto error;
1412 }
1413
1414 /* Notify parent pid that we are ready
1415 * to accept command for client side.
1416 */
1417 if (opt_sig_parent) {
1418 kill(ppid, SIGCHLD);
1419 }
1420
1421 while (1) {
1422 /* Blocking call, waiting for transmission */
1423 DBG("Accepting client command ...");
1424 sock = lttcomm_accept_unix_sock(client_sock);
1425 if (sock < 0) {
1426 goto error;
1427 }
1428
1429 /* Allocate context command to process the client request */
1430 cmd_ctx = malloc(sizeof(struct command_ctx));
1431
1432 /* Allocate data buffer for reception */
1433 cmd_ctx->lsm = malloc(sizeof(struct lttcomm_session_msg));
1434 cmd_ctx->llm = NULL;
1435 cmd_ctx->session = NULL;
1436
1437 /*
1438 * Data is received from the lttng client. The struct
1439 * lttcomm_session_msg (lsm) contains the command and data request of
1440 * the client.
1441 */
1442 DBG("Receiving data from client ...");
1443 ret = lttcomm_recv_unix_sock(sock, cmd_ctx->lsm, sizeof(struct lttcomm_session_msg));
1444 if (ret <= 0) {
1445 continue;
1446 }
1447
1448 // TODO: Validate cmd_ctx including sanity check for security purpose.
1449
1450 /*
1451 * This function dispatch the work to the kernel or userspace tracer
1452 * libs and fill the lttcomm_lttng_msg data structure of all the needed
1453 * informations for the client. The command context struct contains
1454 * everything this function may needs.
1455 */
1456 ret = process_client_msg(cmd_ctx);
1457 if (ret < 0) {
1458 /* TODO: Inform client somehow of the fatal error. At this point,
1459 * ret < 0 means that a malloc failed (ENOMEM). */
1460 /* Error detected but still accept command */
1461 clean_command_ctx(cmd_ctx);
1462 continue;
1463 }
1464
1465 DBG("Sending response (size: %d, retcode: %d)",
1466 cmd_ctx->lttng_msg_size, cmd_ctx->llm->ret_code);
1467 ret = send_unix_sock(sock, cmd_ctx->llm, cmd_ctx->lttng_msg_size);
1468 if (ret < 0) {
1469 ERR("Failed to send data back to client");
1470 }
1471
1472 clean_command_ctx(cmd_ctx);
1473
1474 /* End of transmission */
1475 close(sock);
1476 }
1477
1478 error:
1479 return NULL;
1480 }
1481
1482
1483 /*
1484 * usage function on stderr
1485 */
1486 static void usage(void)
1487 {
1488 fprintf(stderr, "Usage: %s OPTIONS\n\nOptions:\n", progname);
1489 fprintf(stderr, " -h, --help Display this usage.\n");
1490 fprintf(stderr, " -c, --client-sock PATH Specify path for the client unix socket\n");
1491 fprintf(stderr, " -a, --apps-sock PATH Specify path for apps unix socket\n");
1492 fprintf(stderr, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
1493 fprintf(stderr, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
1494 fprintf(stderr, " -d, --daemonize Start as a daemon.\n");
1495 fprintf(stderr, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
1496 fprintf(stderr, " -V, --version Show version number.\n");
1497 fprintf(stderr, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
1498 fprintf(stderr, " -q, --quiet No output at all.\n");
1499 fprintf(stderr, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
1500 }
1501
1502 /*
1503 * daemon argument parsing
1504 */
1505 static int parse_args(int argc, char **argv)
1506 {
1507 int c;
1508
1509 static struct option long_options[] = {
1510 { "client-sock", 1, 0, 'c' },
1511 { "apps-sock", 1, 0, 'a' },
1512 { "kconsumerd-cmd-sock", 1, 0, 0 },
1513 { "kconsumerd-err-sock", 1, 0, 0 },
1514 { "daemonize", 0, 0, 'd' },
1515 { "sig-parent", 0, 0, 'S' },
1516 { "help", 0, 0, 'h' },
1517 { "group", 1, 0, 'g' },
1518 { "version", 0, 0, 'V' },
1519 { "quiet", 0, 0, 'q' },
1520 { "verbose", 0, 0, 'v' },
1521 { NULL, 0, 0, 0 }
1522 };
1523
1524 while (1) {
1525 int option_index = 0;
1526 c = getopt_long(argc, argv, "dhqvVS" "a:c:g:s:E:C:", long_options, &option_index);
1527 if (c == -1) {
1528 break;
1529 }
1530
1531 switch (c) {
1532 case 0:
1533 fprintf(stderr, "option %s", long_options[option_index].name);
1534 if (optarg) {
1535 fprintf(stderr, " with arg %s\n", optarg);
1536 }
1537 break;
1538 case 'c':
1539 snprintf(client_unix_sock_path, PATH_MAX, "%s", optarg);
1540 break;
1541 case 'a':
1542 snprintf(apps_unix_sock_path, PATH_MAX, "%s", optarg);
1543 break;
1544 case 'd':
1545 opt_daemon = 1;
1546 break;
1547 case 'g':
1548 opt_tracing_group = strdup(optarg);
1549 break;
1550 case 'h':
1551 usage();
1552 exit(EXIT_FAILURE);
1553 case 'V':
1554 fprintf(stdout, "%s\n", VERSION);
1555 exit(EXIT_SUCCESS);
1556 case 'S':
1557 opt_sig_parent = 1;
1558 break;
1559 case 'E':
1560 snprintf(kconsumerd_err_unix_sock_path, PATH_MAX, "%s", optarg);
1561 break;
1562 case 'C':
1563 snprintf(kconsumerd_cmd_unix_sock_path, PATH_MAX, "%s", optarg);
1564 break;
1565 case 'q':
1566 opt_quiet = 1;
1567 break;
1568 case 'v':
1569 opt_verbose = 1;
1570 break;
1571 default:
1572 /* Unknown option or other error.
1573 * Error is printed by getopt, just return */
1574 return -1;
1575 }
1576 }
1577
1578 return 0;
1579 }
1580
1581 /*
1582 * init_daemon_socket
1583 *
1584 * Creates the two needed socket by the daemon.
1585 * apps_sock - The communication socket for all UST apps.
1586 * client_sock - The communication of the cli tool (lttng).
1587 */
1588 static int init_daemon_socket()
1589 {
1590 int ret = 0;
1591 mode_t old_umask;
1592
1593 old_umask = umask(0);
1594
1595 /* Create client tool unix socket */
1596 client_sock = lttcomm_create_unix_sock(client_unix_sock_path);
1597 if (client_sock < 0) {
1598 ERR("Create unix sock failed: %s", client_unix_sock_path);
1599 ret = -1;
1600 goto end;
1601 }
1602
1603 /* File permission MUST be 660 */
1604 ret = chmod(client_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
1605 if (ret < 0) {
1606 ERR("Set file permissions failed: %s", client_unix_sock_path);
1607 perror("chmod");
1608 goto end;
1609 }
1610
1611 /* Create the application unix socket */
1612 apps_sock = lttcomm_create_unix_sock(apps_unix_sock_path);
1613 if (apps_sock < 0) {
1614 ERR("Create unix sock failed: %s", apps_unix_sock_path);
1615 ret = -1;
1616 goto end;
1617 }
1618
1619 /* File permission MUST be 666 */
1620 ret = chmod(apps_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
1621 if (ret < 0) {
1622 ERR("Set file permissions failed: %s", apps_unix_sock_path);
1623 perror("chmod");
1624 goto end;
1625 }
1626
1627 end:
1628 umask(old_umask);
1629 return ret;
1630 }
1631
1632 /*
1633 * check_existing_daemon
1634 *
1635 * Check if the global socket is available.
1636 * If yes, error is returned.
1637 */
1638 static int check_existing_daemon()
1639 {
1640 int ret;
1641
1642 ret = access(client_unix_sock_path, F_OK);
1643 if (ret == 0) {
1644 ret = access(apps_unix_sock_path, F_OK);
1645 }
1646
1647 return ret;
1648 }
1649
1650 /*
1651 * get_home_dir
1652 *
1653 * Return pointer to home directory path using
1654 * the env variable HOME.
1655 *
1656 * Default : /tmp
1657 */
1658 static const char *get_home_dir(void)
1659 {
1660 const char *home_path;
1661
1662 if ((home_path = (const char *) getenv("HOME")) == NULL) {
1663 home_path = default_home_dir;
1664 }
1665
1666 return home_path;
1667 }
1668
1669 /*
1670 * set_permissions
1671 *
1672 * Set the tracing group gid onto the client socket.
1673 *
1674 * Race window between mkdir and chown is OK because we are going from
1675 * less permissive (root.root) to more permissive (root.tracing).
1676 */
1677 static int set_permissions(void)
1678 {
1679 int ret;
1680 struct group *grp;
1681
1682 /* Decide which group name to use */
1683 (opt_tracing_group != NULL) ?
1684 (grp = getgrnam(opt_tracing_group)) :
1685 (grp = getgrnam(default_tracing_group));
1686
1687 if (grp == NULL) {
1688 ERR("Missing tracing group. Aborting execution.\n");
1689 ret = -1;
1690 goto end;
1691 }
1692
1693 /* Set lttng run dir */
1694 ret = chown(LTTNG_RUNDIR, 0, grp->gr_gid);
1695 if (ret < 0) {
1696 ERR("Unable to set group on " LTTNG_RUNDIR);
1697 perror("chown");
1698 }
1699
1700 /* lttng client socket path */
1701 ret = chown(client_unix_sock_path, 0, grp->gr_gid);
1702 if (ret < 0) {
1703 ERR("Unable to set group on %s", client_unix_sock_path);
1704 perror("chown");
1705 }
1706
1707 /* kconsumerd error socket path */
1708 ret = chown(kconsumerd_err_unix_sock_path, 0, grp->gr_gid);
1709 if (ret < 0) {
1710 ERR("Unable to set group on %s", kconsumerd_err_unix_sock_path);
1711 perror("chown");
1712 }
1713
1714 DBG("All permissions are set");
1715
1716 end:
1717 return ret;
1718 }
1719
1720 /*
1721 * create_lttng_rundir
1722 *
1723 * Create the lttng run directory needed for all
1724 * global sockets and pipe.
1725 */
1726 static int create_lttng_rundir(void)
1727 {
1728 int ret;
1729
1730 ret = mkdir(LTTNG_RUNDIR, S_IRWXU | S_IRWXG );
1731 if (ret < 0) {
1732 if (errno != EEXIST) {
1733 ERR("Unable to create " LTTNG_RUNDIR);
1734 goto error;
1735 } else {
1736 ret = 0;
1737 }
1738 }
1739
1740 error:
1741 return ret;
1742 }
1743
1744 /*
1745 * set_kconsumerd_sockets
1746 *
1747 * Setup sockets and directory needed by the kconsumerd
1748 * communication with the session daemon.
1749 */
1750 static int set_kconsumerd_sockets(void)
1751 {
1752 int ret;
1753
1754 if (strlen(kconsumerd_err_unix_sock_path) == 0) {
1755 snprintf(kconsumerd_err_unix_sock_path, PATH_MAX, KCONSUMERD_ERR_SOCK_PATH);
1756 }
1757
1758 if (strlen(kconsumerd_cmd_unix_sock_path) == 0) {
1759 snprintf(kconsumerd_cmd_unix_sock_path, PATH_MAX, KCONSUMERD_CMD_SOCK_PATH);
1760 }
1761
1762 ret = mkdir(KCONSUMERD_PATH, S_IRWXU | S_IRWXG);
1763 if (ret < 0) {
1764 if (errno != EEXIST) {
1765 ERR("Failed to create " KCONSUMERD_PATH);
1766 goto error;
1767 }
1768 ret = 0;
1769 }
1770
1771 /* Create the kconsumerd error unix socket */
1772 kconsumerd_err_sock = lttcomm_create_unix_sock(kconsumerd_err_unix_sock_path);
1773 if (kconsumerd_err_sock < 0) {
1774 ERR("Create unix sock failed: %s", kconsumerd_err_unix_sock_path);
1775 ret = -1;
1776 goto error;
1777 }
1778
1779 /* File permission MUST be 660 */
1780 ret = chmod(kconsumerd_err_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
1781 if (ret < 0) {
1782 ERR("Set file permissions failed: %s", kconsumerd_err_unix_sock_path);
1783 perror("chmod");
1784 goto error;
1785 }
1786
1787 error:
1788 return ret;
1789 }
1790
1791 /*
1792 * sighandler
1793 *
1794 * Signal handler for the daemon
1795 */
1796 static void sighandler(int sig)
1797 {
1798 switch (sig) {
1799 case SIGPIPE:
1800 DBG("SIGPIPE catched");
1801 return;
1802 case SIGINT:
1803 DBG("SIGINT catched");
1804 cleanup();
1805 break;
1806 case SIGTERM:
1807 DBG("SIGTERM catched");
1808 cleanup();
1809 break;
1810 default:
1811 break;
1812 }
1813
1814 exit(EXIT_SUCCESS);
1815 }
1816
1817 /*
1818 * set_signal_handler
1819 *
1820 * Setup signal handler for :
1821 * SIGINT, SIGTERM, SIGPIPE
1822 */
1823 static int set_signal_handler(void)
1824 {
1825 int ret = 0;
1826 struct sigaction sa;
1827 sigset_t sigset;
1828
1829 if ((ret = sigemptyset(&sigset)) < 0) {
1830 perror("sigemptyset");
1831 return ret;
1832 }
1833
1834 sa.sa_handler = sighandler;
1835 sa.sa_mask = sigset;
1836 sa.sa_flags = 0;
1837 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
1838 perror("sigaction");
1839 return ret;
1840 }
1841
1842 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
1843 perror("sigaction");
1844 return ret;
1845 }
1846
1847 if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) {
1848 perror("sigaction");
1849 return ret;
1850 }
1851
1852 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
1853
1854 return ret;
1855 }
1856
1857 /*
1858 * set_ulimit
1859 *
1860 * Set open files limit to unlimited. This daemon can open a large number of
1861 * file descriptors in order to consumer multiple kernel traces.
1862 */
1863 static void set_ulimit(void)
1864 {
1865 int ret;
1866 struct rlimit lim;
1867
1868 lim.rlim_cur = 65535;
1869 lim.rlim_max = 65535;
1870
1871 ret = setrlimit(RLIMIT_NOFILE, &lim);
1872 if (ret < 0) {
1873 perror("failed to set open files limit");
1874 }
1875 }
1876
1877 /*
1878 * main
1879 */
1880 int main(int argc, char **argv)
1881 {
1882 int ret = 0;
1883 void *status;
1884
1885 /* Parse arguments */
1886 progname = argv[0];
1887 if ((ret = parse_args(argc, argv) < 0)) {
1888 goto error;
1889 }
1890
1891 /* Daemonize */
1892 if (opt_daemon) {
1893 ret = daemon(0, 0);
1894 if (ret < 0) {
1895 perror("daemon");
1896 goto error;
1897 }
1898 }
1899
1900 /* Check if daemon is UID = 0 */
1901 is_root = !getuid();
1902
1903 /* Set all sockets path */
1904 if (is_root) {
1905 ret = create_lttng_rundir();
1906 if (ret < 0) {
1907 goto error;
1908 }
1909
1910 if (strlen(apps_unix_sock_path) == 0) {
1911 snprintf(apps_unix_sock_path, PATH_MAX,
1912 DEFAULT_GLOBAL_APPS_UNIX_SOCK);
1913 }
1914
1915 if (strlen(client_unix_sock_path) == 0) {
1916 snprintf(client_unix_sock_path, PATH_MAX,
1917 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK);
1918 }
1919
1920 ret = set_kconsumerd_sockets();
1921 if (ret < 0) {
1922 goto error;
1923 }
1924
1925 /* Setup kernel tracer */
1926 init_kernel_tracer();
1927
1928 /* Set ulimit for open files */
1929 set_ulimit();
1930 } else {
1931 if (strlen(apps_unix_sock_path) == 0) {
1932 snprintf(apps_unix_sock_path, PATH_MAX,
1933 DEFAULT_HOME_APPS_UNIX_SOCK, get_home_dir());
1934 }
1935
1936 /* Set the cli tool unix socket path */
1937 if (strlen(client_unix_sock_path) == 0) {
1938 snprintf(client_unix_sock_path, PATH_MAX,
1939 DEFAULT_HOME_CLIENT_UNIX_SOCK, get_home_dir());
1940 }
1941 }
1942
1943 DBG("Client socket path %s", client_unix_sock_path);
1944 DBG("Application socket path %s", apps_unix_sock_path);
1945
1946 /* See if daemon already exist. If any of the two
1947 * socket needed by the daemon are present, this test fails
1948 */
1949 if ((ret = check_existing_daemon()) == 0) {
1950 ERR("Already running daemon.\n");
1951 /* We do not goto error because we must not
1952 * cleanup() because a daemon is already running.
1953 */
1954 exit(EXIT_FAILURE);
1955 }
1956
1957 if (set_signal_handler() < 0) {
1958 goto error;
1959 }
1960
1961 /* Setup the needed unix socket */
1962 if (init_daemon_socket() < 0) {
1963 goto error;
1964 }
1965
1966 /* Set credentials to socket */
1967 if (is_root && (set_permissions() < 0)) {
1968 goto error;
1969 }
1970
1971 /* Get parent pid if -S, --sig-parent is specified. */
1972 if (opt_sig_parent) {
1973 ppid = getppid();
1974 }
1975
1976 while (1) {
1977 /* Create thread to manage the client socket */
1978 ret = pthread_create(&client_thread, NULL, thread_manage_clients, (void *) NULL);
1979 if (ret != 0) {
1980 perror("pthread_create");
1981 goto error;
1982 }
1983
1984 /* Create thread to manage application socket */
1985 ret = pthread_create(&apps_thread, NULL, thread_manage_apps, (void *) NULL);
1986 if (ret != 0) {
1987 perror("pthread_create");
1988 goto error;
1989 }
1990
1991 ret = pthread_join(client_thread, &status);
1992 if (ret != 0) {
1993 perror("pthread_join");
1994 goto error;
1995 }
1996 }
1997
1998 cleanup();
1999 exit(EXIT_SUCCESS);
2000
2001 error:
2002 cleanup();
2003 exit(EXIT_FAILURE);
2004 }
This page took 0.087221 seconds and 5 git commands to generate.