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