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