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