e7ad6f9b140e1c48f05f2befcda5d73fc1ce2803
[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 DBG("Spawning kconsumerd");
490
491 pid = fork();
492 if (pid == 0) {
493 /*
494 * Exec kconsumerd.
495 */
496 execlp("kconsumerd", "kconsumerd", "--verbose", NULL);
497 if (errno != 0) {
498 perror("kernel start consumer exec");
499 }
500 exit(EXIT_FAILURE);
501 } else if (pid > 0) {
502 ret = pid;
503 goto error;
504 } else {
505 perror("kernel start consumer fork");
506 ret = -errno;
507 goto error;
508 }
509
510 error:
511 return ret;
512 }
513
514 /*
515 * start_kconsumerd
516 *
517 * Spawn the kconsumerd daemon and session daemon thread.
518 */
519 static int start_kconsumerd(void)
520 {
521 int ret;
522
523 pthread_mutex_lock(&kconsumerd_pid_mutex);
524 if (kconsumerd_pid != 0) {
525 goto end;
526 }
527
528 ret = spawn_kconsumerd();
529 if (ret < 0) {
530 ERR("Spawning kconsumerd failed");
531 ret = LTTCOMM_KERN_CONSUMER_FAIL;
532 pthread_mutex_unlock(&kconsumerd_pid_mutex);
533 goto error;
534 }
535
536 /* Setting up the global kconsumerd_pid */
537 kconsumerd_pid = ret;
538 pthread_mutex_unlock(&kconsumerd_pid_mutex);
539
540 DBG("Kconsumerd pid %d", ret);
541
542 DBG("Spawning kconsumerd thread");
543 ret = spawn_kconsumerd_thread();
544 if (ret < 0) {
545 ERR("Fatal error spawning kconsumerd thread");
546 goto error;
547 }
548
549 end:
550 pthread_mutex_unlock(&kconsumerd_pid_mutex);
551 return 0;
552
553 error:
554 return ret;
555 }
556
557 /*
558 * send_kconsumerd_fds
559 *
560 * Send all stream fds of the kernel session to the consumer.
561 */
562 static int send_kconsumerd_fds(int sock, struct ltt_kernel_session *session)
563 {
564 int ret;
565 size_t nb_fd;
566 struct ltt_kernel_stream *stream;
567 struct ltt_kernel_channel *chan;
568 struct lttcomm_kconsumerd_header lkh;
569 struct lttcomm_kconsumerd_msg lkm;
570
571 nb_fd = session->stream_count_global;
572
573 /* Setup header */
574 lkh.payload_size = (nb_fd + 1) * sizeof(struct lttcomm_kconsumerd_msg);
575 lkh.cmd_type = ADD_STREAM;
576
577 DBG("Sending kconsumerd header");
578
579 ret = lttcomm_send_unix_sock(sock, &lkh, sizeof(struct lttcomm_kconsumerd_header));
580 if (ret < 0) {
581 perror("send kconsumerd header");
582 goto error;
583 }
584
585 DBG("Sending metadata stream fd");
586
587 /* Send metadata stream fd first */
588 lkm.fd = session->metadata_stream_fd;
589 lkm.state = ACTIVE_FD;
590 lkm.max_sb_size = session->metadata->conf->subbuf_size;
591 strncpy(lkm.path_name, session->metadata->pathname, PATH_MAX);
592
593 ret = lttcomm_send_fds_unix_sock(sock, &lkm, &lkm.fd, 1, sizeof(lkm));
594 if (ret < 0) {
595 perror("send kconsumerd fd");
596 goto error;
597 }
598
599 cds_list_for_each_entry(chan, &session->channel_list.head, list) {
600 cds_list_for_each_entry(stream, &chan->stream_list.head, list) {
601 lkm.fd = stream->fd;
602 lkm.state = stream->state;
603 lkm.max_sb_size = chan->channel->subbuf_size;
604 strncpy(lkm.path_name, stream->pathname, PATH_MAX);
605
606 DBG("Sending fd %d to kconsumerd", lkm.fd);
607
608 ret = lttcomm_send_fds_unix_sock(sock, &lkm, &lkm.fd, 1, sizeof(lkm));
609 if (ret < 0) {
610 perror("send kconsumerd fd");
611 goto error;
612 }
613 }
614 }
615
616 DBG("Kconsumerd fds sent");
617
618 return 0;
619
620 error:
621 return ret;
622 }
623
624 /*
625 * create_trace_dir
626 *
627 * Create the trace output directory.
628 */
629 static int create_trace_dir(struct ltt_kernel_session *session)
630 {
631 int ret;
632 struct ltt_kernel_channel *chan;
633
634 /* Create all channel directories */
635 cds_list_for_each_entry(chan, &session->channel_list.head, list) {
636 DBG("Creating trace directory at %s", chan->pathname);
637 // TODO: recursive create dir
638 ret = mkdir(chan->pathname, S_IRWXU | S_IRWXG );
639 if (ret < 0) {
640 perror("mkdir trace path");
641 ret = -errno;
642 goto error;
643 }
644 }
645
646 return 0;
647
648 error:
649 return ret;
650 }
651
652 /*
653 * init_kernel_tracer
654 *
655 * Setup necessary data for kernel tracer action.
656 */
657 static void init_kernel_tracer(void)
658 {
659 /* Set the global kernel tracer fd */
660 kernel_tracer_fd = open(DEFAULT_KERNEL_TRACER_PATH, O_RDWR);
661 if (kernel_tracer_fd < 0) {
662 WARN("No kernel tracer available");
663 kernel_tracer_fd = 0;
664 }
665
666 DBG("Kernel tracer fd %d", kernel_tracer_fd);
667 }
668
669 /*
670 * process_client_msg
671 *
672 * Process the command requested by the lttng client within the command
673 * context structure. This function make sure that the return structure (llm)
674 * is set and ready for transmission before returning.
675 *
676 * Return any error encountered or 0 for success.
677 */
678 static int process_client_msg(struct command_ctx *cmd_ctx)
679 {
680 int ret;
681
682 DBG("Processing client command %d", cmd_ctx->lsm->cmd_type);
683
684 /* Check command that needs a session */
685 switch (cmd_ctx->lsm->cmd_type) {
686 case LTTNG_CREATE_SESSION:
687 case LTTNG_LIST_SESSIONS:
688 case KERNEL_LIST_EVENTS:
689 case UST_LIST_APPS:
690 break;
691 default:
692 cmd_ctx->session = find_session_by_uuid(cmd_ctx->lsm->session_uuid);
693 if (cmd_ctx->session == NULL) {
694 ret = LTTCOMM_SELECT_SESS;
695 goto error;
696 }
697 break;
698 }
699
700 /* Check command for kernel tracing */
701 switch (cmd_ctx->lsm->cmd_type) {
702 case KERNEL_CREATE_SESSION:
703 case KERNEL_CREATE_CHANNEL:
704 case KERNEL_CREATE_STREAM:
705 case KERNEL_DISABLE_EVENT:
706 case KERNEL_ENABLE_EVENT:
707 case KERNEL_LIST_EVENTS:
708 case KERNEL_OPEN_METADATA:
709 case KERNEL_START_TRACE:
710 case KERNEL_STOP_TRACE:
711 /* Kernel tracer check */
712 if (kernel_tracer_fd == 0) {
713 init_kernel_tracer();
714 if (kernel_tracer_fd == 0) {
715 ret = LTTCOMM_KERN_NA;
716 goto error;
717 }
718 }
719 break;
720 }
721
722 /* Connect to ust apps if available pid */
723 if (cmd_ctx->lsm->pid > 0) {
724 /* Connect to app using ustctl API */
725 cmd_ctx->ust_sock = ust_connect_app(cmd_ctx->lsm->pid);
726 if (cmd_ctx->ust_sock < 0) {
727 ret = LTTCOMM_NO_TRACEABLE;
728 goto error;
729 }
730 }
731
732 /* Process by command type */
733 switch (cmd_ctx->lsm->cmd_type) {
734 case KERNEL_CREATE_SESSION:
735 {
736 ret = setup_lttng_msg(cmd_ctx, 0);
737 if (ret < 0) {
738 goto setup_error;
739 }
740
741 ret = start_kconsumerd();
742 if (ret < 0) {
743 goto error;
744 }
745
746 DBG("Creating kernel session");
747
748 ret = kernel_create_session(cmd_ctx->session, kernel_tracer_fd);
749 if (ret < 0) {
750 ret = LTTCOMM_KERN_SESS_FAIL;
751 goto error;
752 }
753
754 ret = LTTCOMM_OK;
755 break;
756 }
757 case KERNEL_CREATE_CHANNEL:
758 {
759 ret = setup_lttng_msg(cmd_ctx, 0);
760 if (ret < 0) {
761 goto setup_error;
762 }
763
764 DBG("Creating kernel channel");
765
766 ret = kernel_create_channel(cmd_ctx->session->kernel_session);
767
768 if (ret < 0) {
769 ret = LTTCOMM_KERN_CHAN_FAIL;
770 goto error;
771 }
772
773 ret = LTTCOMM_OK;
774 break;
775 }
776 case KERNEL_ENABLE_EVENT:
777 {
778 /* Setup lttng message with no payload */
779 ret = setup_lttng_msg(cmd_ctx, 0);
780 if (ret < 0) {
781 goto setup_error;
782 }
783
784 DBG("Enabling kernel event %s", cmd_ctx->lsm->u.event.event_name);
785
786 ret = kernel_enable_event(cmd_ctx->session->kernel_session, cmd_ctx->lsm->u.event.event_name);
787 if (ret < 0) {
788 ret = LTTCOMM_KERN_ENABLE_FAIL;
789 goto error;
790 }
791
792 ret = LTTCOMM_OK;
793 break;
794 }
795 case KERNEL_ENABLE_ALL_EVENT:
796 {
797 int pos, size;
798 char *event_list, *event, *ptr;
799
800 /* Setup lttng message with no payload */
801 ret = setup_lttng_msg(cmd_ctx, 0);
802 if (ret < 0) {
803 goto setup_error;
804 }
805
806 DBG("Enabling all kernel event");
807
808 size = kernel_list_events(kernel_tracer_fd, &event_list);
809 if (size < 0) {
810 ret = LTTCOMM_KERN_LIST_FAIL;
811 goto error;
812 }
813
814 ptr = event_list;
815 while ((size = sscanf(ptr, "event { name = %m[^;]; };%n\n", &event, &pos)) == 1) {
816 /* Enable each single event */
817 ret = kernel_enable_event(cmd_ctx->session->kernel_session, event);
818 if (ret < 0) {
819 ret = LTTCOMM_KERN_ENABLE_FAIL;
820 goto error;
821 }
822 /* Move pointer to the next line */
823 ptr += pos + 1;
824 free(event);
825 }
826
827 free(event_list);
828
829 ret = LTTCOMM_OK;
830 break;
831 }
832 case KERNEL_LIST_EVENTS:
833 {
834 char *event_list;
835 ssize_t size;
836
837 size = kernel_list_events(kernel_tracer_fd, &event_list);
838 if (size < 0) {
839 ret = LTTCOMM_KERN_LIST_FAIL;
840 goto error;
841 }
842
843 /*
844 * Setup lttng message with payload size set to the event list size in
845 * bytes and then copy list into the llm payload.
846 */
847 ret = setup_lttng_msg(cmd_ctx, size);
848 if (ret < 0) {
849 goto setup_error;
850 }
851
852 /* Copy event list into message payload */
853 memcpy(cmd_ctx->llm->payload, event_list, size);
854
855 free(event_list);
856
857 ret = LTTCOMM_OK;
858 break;
859 }
860 case KERNEL_OPEN_METADATA:
861 {
862 /* Setup lttng message with no payload */
863 ret = setup_lttng_msg(cmd_ctx, 0);
864 if (ret < 0) {
865 goto setup_error;
866 }
867
868 DBG("Open kernel metadata");
869
870 ret = kernel_open_metadata(cmd_ctx->session->kernel_session);
871 if (ret < 0) {
872 ret = LTTCOMM_KERN_META_FAIL;
873 goto error;
874 }
875
876 ret = LTTCOMM_OK;
877 break;
878 }
879 case KERNEL_CREATE_STREAM:
880 {
881 struct ltt_kernel_channel *chan;
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 DBG("Creating kernel stream");
889
890 ret = kernel_create_metadata_stream(cmd_ctx->session->kernel_session);
891 if (ret < 0) {
892 ERR("Kernel create metadata stream failed");
893 ret = LTTCOMM_KERN_STREAM_FAIL;
894 goto error;
895 }
896
897 /* For each channel */
898 cds_list_for_each_entry(chan, &cmd_ctx->session->kernel_session->channel_list.head, list) {
899 ret = kernel_create_channel_stream(chan);
900 if (ret < 0) {
901 ERR("Kernel create channel stream failed");
902 ret = LTTCOMM_KERN_STREAM_FAIL;
903 goto error;
904 }
905 /* Update the stream global counter */
906 cmd_ctx->session->kernel_session->stream_count_global += ret;
907 }
908
909 ret = LTTCOMM_OK;
910 break;
911 }
912 case KERNEL_START_TRACE:
913 {
914 /* Setup lttng message with no payload */
915 ret = setup_lttng_msg(cmd_ctx, 0);
916 if (ret < 0) {
917 goto setup_error;
918 }
919
920 DBG("Start kernel tracing");
921
922 ret = create_trace_dir(cmd_ctx->session->kernel_session);
923 if (ret < 0) {
924 if (ret == -EEXIST) {
925 ret = LTTCOMM_KERN_DIR_EXIST;
926 } else {
927 ret = LTTCOMM_KERN_DIR_FAIL;
928 goto error;
929 }
930 }
931
932 ret = kernel_start_session(cmd_ctx->session->kernel_session);
933 if (ret < 0) {
934 ERR("Kernel start session failed");
935 ret = LTTCOMM_KERN_START_FAIL;
936 goto error;
937 }
938
939 ret = send_kconsumerd_fds(kconsumerd_cmd_sock, cmd_ctx->session->kernel_session);
940 if (ret < 0) {
941 ERR("Send kconsumerd fds failed");
942 ret = LTTCOMM_KERN_CONSUMER_FAIL;
943 goto error;
944 }
945
946 ret = LTTCOMM_OK;
947 break;
948 }
949 case KERNEL_STOP_TRACE:
950 {
951 /* Setup lttng message with no payload */
952 ret = setup_lttng_msg(cmd_ctx, 0);
953 if (ret < 0) {
954 goto setup_error;
955 }
956
957 if (cmd_ctx->session->kernel_session == NULL) {
958 ret = LTTCOMM_KERN_NO_SESSION;
959 goto error;
960 }
961
962 DBG("Stop kernel tracing");
963
964 ret = kernel_stop_session(cmd_ctx->session->kernel_session);
965 if (ret < 0) {
966 ERR("Kernel stop session failed");
967 ret = LTTCOMM_KERN_STOP_FAIL;
968 goto error;
969 }
970
971 /* Clean kernel session teardown */
972 teardown_kernel_session(cmd_ctx->session);
973
974 ret = LTTCOMM_OK;
975 break;
976 }
977 case LTTNG_CREATE_SESSION:
978 {
979 /* Setup lttng message with no payload */
980 ret = setup_lttng_msg(cmd_ctx, 0);
981 if (ret < 0) {
982 goto setup_error;
983 }
984
985 ret = create_session(cmd_ctx->lsm->session_name, &cmd_ctx->llm->session_uuid);
986 if (ret < 0) {
987 if (ret == -1) {
988 ret = LTTCOMM_EXIST_SESS;
989 } else {
990 ret = LTTCOMM_FATAL;
991 }
992 goto error;
993 }
994
995 ret = LTTCOMM_OK;
996 break;
997 }
998 case LTTNG_DESTROY_SESSION:
999 {
1000 /* Setup lttng message with no payload */
1001 ret = setup_lttng_msg(cmd_ctx, 0);
1002 if (ret < 0) {
1003 goto setup_error;
1004 }
1005
1006 ret = destroy_session(&cmd_ctx->lsm->session_uuid);
1007 if (ret < 0) {
1008 ret = LTTCOMM_NO_SESS;
1009 goto error;
1010 }
1011
1012 ret = LTTCOMM_OK;
1013 break;
1014 }
1015 case LTTNG_LIST_TRACES:
1016 {
1017 unsigned int trace_count;
1018
1019 trace_count = get_trace_count_per_session(cmd_ctx->session);
1020 if (trace_count == 0) {
1021 ret = LTTCOMM_NO_TRACE;
1022 goto error;
1023 }
1024
1025 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_trace) * trace_count);
1026 if (ret < 0) {
1027 goto setup_error;
1028 }
1029
1030 get_traces_per_session(cmd_ctx->session,
1031 (struct lttng_trace *)(cmd_ctx->llm->payload));
1032
1033 ret = LTTCOMM_OK;
1034 break;
1035 }
1036 case UST_CREATE_TRACE:
1037 {
1038 /* Setup lttng message with no payload */
1039 ret = setup_lttng_msg(cmd_ctx, 0);
1040 if (ret < 0) {
1041 goto setup_error;
1042 }
1043
1044 ret = ust_create_trace(cmd_ctx);
1045 if (ret < 0) {
1046 goto setup_error;
1047 }
1048 break;
1049 }
1050 case UST_LIST_APPS:
1051 {
1052 unsigned int app_count;
1053
1054 app_count = get_app_count();
1055 DBG("Traceable application count : %d", app_count);
1056 if (app_count == 0) {
1057 ret = LTTCOMM_NO_APPS;
1058 goto error;
1059 }
1060
1061 ret = setup_lttng_msg(cmd_ctx, sizeof(pid_t) * app_count);
1062 if (ret < 0) {
1063 goto setup_error;
1064 }
1065
1066 get_app_list_pids((pid_t *)(cmd_ctx->llm->payload));
1067
1068 ret = LTTCOMM_OK;
1069 break;
1070 }
1071 case UST_START_TRACE:
1072 {
1073 /* Setup lttng message with no payload */
1074 ret = setup_lttng_msg(cmd_ctx, 0);
1075 if (ret < 0) {
1076 goto setup_error;
1077 }
1078
1079 ret = ust_start_trace(cmd_ctx);
1080 if (ret < 0) {
1081 goto setup_error;
1082 }
1083 break;
1084 }
1085 case UST_STOP_TRACE:
1086 {
1087 /* Setup lttng message with no payload */
1088 ret = setup_lttng_msg(cmd_ctx, 0);
1089 if (ret < 0) {
1090 goto setup_error;
1091 }
1092
1093 ret = ust_stop_trace(cmd_ctx);
1094 if (ret < 0) {
1095 goto setup_error;
1096 }
1097 break;
1098 }
1099 case LTTNG_LIST_SESSIONS:
1100 {
1101 unsigned int session_count;
1102
1103 session_count = get_session_count();
1104 if (session_count == 0) {
1105 ret = LTTCOMM_NO_SESS;
1106 goto error;
1107 }
1108
1109 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_session) * session_count);
1110 if (ret < 0) {
1111 goto setup_error;
1112 }
1113
1114 get_lttng_session((struct lttng_session *)(cmd_ctx->llm->payload));
1115
1116 ret = LTTCOMM_OK;
1117 break;
1118 }
1119 default:
1120 /* Undefined command */
1121 ret = setup_lttng_msg(cmd_ctx, 0);
1122 if (ret < 0) {
1123 goto setup_error;
1124 }
1125
1126 ret = LTTCOMM_UND;
1127 break;
1128 }
1129
1130 /* Set return code */
1131 cmd_ctx->llm->ret_code = ret;
1132
1133 return ret;
1134
1135 error:
1136 if (cmd_ctx->llm == NULL) {
1137 DBG("Missing llm structure. Allocating one.");
1138 if (setup_lttng_msg(cmd_ctx, 0) < 0) {
1139 goto setup_error;
1140 }
1141 }
1142 /* Notify client of error */
1143 cmd_ctx->llm->ret_code = ret;
1144
1145 setup_error:
1146 return ret;
1147 }
1148
1149 /*
1150 * thread_manage_clients
1151 *
1152 * This thread manage all clients request using the unix
1153 * client socket for communication.
1154 */
1155 static void *thread_manage_clients(void *data)
1156 {
1157 int sock, ret;
1158 struct command_ctx *cmd_ctx;
1159
1160 DBG("[thread] Manage client started");
1161
1162 ret = lttcomm_listen_unix_sock(client_sock);
1163 if (ret < 0) {
1164 goto error;
1165 }
1166
1167 /* Notify parent pid that we are ready
1168 * to accept command for client side.
1169 */
1170 if (opt_sig_parent) {
1171 kill(ppid, SIGCHLD);
1172 }
1173
1174 while (1) {
1175 /* Blocking call, waiting for transmission */
1176 DBG("Accepting client command ...");
1177 sock = lttcomm_accept_unix_sock(client_sock);
1178 if (sock < 0) {
1179 goto error;
1180 }
1181
1182 /* Allocate context command to process the client request */
1183 cmd_ctx = malloc(sizeof(struct command_ctx));
1184
1185 /* Allocate data buffer for reception */
1186 cmd_ctx->lsm = malloc(sizeof(struct lttcomm_session_msg));
1187 cmd_ctx->llm = NULL;
1188 cmd_ctx->session = NULL;
1189
1190 /*
1191 * Data is received from the lttng client. The struct
1192 * lttcomm_session_msg (lsm) contains the command and data request of
1193 * the client.
1194 */
1195 DBG("Receiving data from client ...");
1196 ret = lttcomm_recv_unix_sock(sock, cmd_ctx->lsm, sizeof(struct lttcomm_session_msg));
1197 if (ret <= 0) {
1198 continue;
1199 }
1200
1201 // TODO: Validate cmd_ctx including sanity check for security purpose.
1202
1203 /*
1204 * This function dispatch the work to the kernel or userspace tracer
1205 * libs and fill the lttcomm_lttng_msg data structure of all the needed
1206 * informations for the client. The command context struct contains
1207 * everything this function may needs.
1208 */
1209 ret = process_client_msg(cmd_ctx);
1210 if (ret < 0) {
1211 /* TODO: Inform client somehow of the fatal error. At this point,
1212 * ret < 0 means that a malloc failed (ENOMEM). */
1213 /* Error detected but still accept command */
1214 clean_command_ctx(cmd_ctx);
1215 continue;
1216 }
1217
1218 DBG("Sending response (size: %d, retcode: %d)",
1219 cmd_ctx->lttng_msg_size, cmd_ctx->llm->ret_code);
1220 ret = send_unix_sock(sock, cmd_ctx->llm, cmd_ctx->lttng_msg_size);
1221 if (ret < 0) {
1222 ERR("Failed to send data back to client");
1223 }
1224
1225 clean_command_ctx(cmd_ctx);
1226
1227 /* End of transmission */
1228 close(sock);
1229 }
1230
1231 error:
1232 return NULL;
1233 }
1234
1235
1236 /*
1237 * usage function on stderr
1238 */
1239 static void usage(void)
1240 {
1241 fprintf(stderr, "Usage: %s OPTIONS\n\nOptions:\n", progname);
1242 fprintf(stderr, " -h, --help Display this usage.\n");
1243 fprintf(stderr, " -c, --client-sock PATH Specify path for the client unix socket\n");
1244 fprintf(stderr, " -a, --apps-sock PATH Specify path for apps unix socket\n");
1245 fprintf(stderr, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
1246 fprintf(stderr, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
1247 fprintf(stderr, " -d, --daemonize Start as a daemon.\n");
1248 fprintf(stderr, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
1249 fprintf(stderr, " -V, --version Show version number.\n");
1250 fprintf(stderr, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
1251 fprintf(stderr, " -q, --quiet No output at all.\n");
1252 fprintf(stderr, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
1253 }
1254
1255 /*
1256 * daemon argument parsing
1257 */
1258 static int parse_args(int argc, char **argv)
1259 {
1260 int c;
1261
1262 static struct option long_options[] = {
1263 { "client-sock", 1, 0, 'c' },
1264 { "apps-sock", 1, 0, 'a' },
1265 { "kconsumerd-cmd-sock", 1, 0, 0 },
1266 { "kconsumerd-err-sock", 1, 0, 0 },
1267 { "daemonize", 0, 0, 'd' },
1268 { "sig-parent", 0, 0, 'S' },
1269 { "help", 0, 0, 'h' },
1270 { "group", 1, 0, 'g' },
1271 { "version", 0, 0, 'V' },
1272 { "quiet", 0, 0, 'q' },
1273 { "verbose", 0, 0, 'v' },
1274 { NULL, 0, 0, 0 }
1275 };
1276
1277 while (1) {
1278 int option_index = 0;
1279 c = getopt_long(argc, argv, "dhqvVS" "a:c:g:s:E:C:", long_options, &option_index);
1280 if (c == -1) {
1281 break;
1282 }
1283
1284 switch (c) {
1285 case 0:
1286 fprintf(stderr, "option %s", long_options[option_index].name);
1287 if (optarg) {
1288 fprintf(stderr, " with arg %s\n", optarg);
1289 }
1290 break;
1291 case 'c':
1292 snprintf(client_unix_sock_path, PATH_MAX, "%s", optarg);
1293 break;
1294 case 'a':
1295 snprintf(apps_unix_sock_path, PATH_MAX, "%s", optarg);
1296 break;
1297 case 'd':
1298 opt_daemon = 1;
1299 break;
1300 case 'g':
1301 opt_tracing_group = strdup(optarg);
1302 break;
1303 case 'h':
1304 usage();
1305 exit(EXIT_FAILURE);
1306 case 'V':
1307 fprintf(stdout, "%s\n", VERSION);
1308 exit(EXIT_SUCCESS);
1309 case 'S':
1310 opt_sig_parent = 1;
1311 break;
1312 case 'E':
1313 snprintf(kconsumerd_err_unix_sock_path, PATH_MAX, "%s", optarg);
1314 break;
1315 case 'C':
1316 snprintf(kconsumerd_cmd_unix_sock_path, PATH_MAX, "%s", optarg);
1317 break;
1318 case 'q':
1319 opt_quiet = 1;
1320 break;
1321 case 'v':
1322 opt_verbose = 1;
1323 break;
1324 default:
1325 /* Unknown option or other error.
1326 * Error is printed by getopt, just return */
1327 return -1;
1328 }
1329 }
1330
1331 return 0;
1332 }
1333
1334 /*
1335 * init_daemon_socket
1336 *
1337 * Creates the two needed socket by the daemon.
1338 * apps_sock - The communication socket for all UST apps.
1339 * client_sock - The communication of the cli tool (lttng).
1340 */
1341 static int init_daemon_socket()
1342 {
1343 int ret = 0;
1344 mode_t old_umask;
1345
1346 old_umask = umask(0);
1347
1348 /* Create client tool unix socket */
1349 client_sock = lttcomm_create_unix_sock(client_unix_sock_path);
1350 if (client_sock < 0) {
1351 ERR("Create unix sock failed: %s", client_unix_sock_path);
1352 ret = -1;
1353 goto end;
1354 }
1355
1356 /* File permission MUST be 660 */
1357 ret = chmod(client_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
1358 if (ret < 0) {
1359 ERR("Set file permissions failed: %s", client_unix_sock_path);
1360 perror("chmod");
1361 goto end;
1362 }
1363
1364 /* Create the application unix socket */
1365 apps_sock = lttcomm_create_unix_sock(apps_unix_sock_path);
1366 if (apps_sock < 0) {
1367 ERR("Create unix sock failed: %s", apps_unix_sock_path);
1368 ret = -1;
1369 goto end;
1370 }
1371
1372 /* File permission MUST be 666 */
1373 ret = chmod(apps_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
1374 if (ret < 0) {
1375 ERR("Set file permissions failed: %s", apps_unix_sock_path);
1376 perror("chmod");
1377 goto end;
1378 }
1379
1380 end:
1381 umask(old_umask);
1382 return ret;
1383 }
1384
1385 /*
1386 * check_existing_daemon
1387 *
1388 * Check if the global socket is available.
1389 * If yes, error is returned.
1390 */
1391 static int check_existing_daemon()
1392 {
1393 int ret;
1394
1395 ret = access(client_unix_sock_path, F_OK);
1396 if (ret == 0) {
1397 ret = access(apps_unix_sock_path, F_OK);
1398 }
1399
1400 return ret;
1401 }
1402
1403 /*
1404 * get_home_dir
1405 *
1406 * Return pointer to home directory path using
1407 * the env variable HOME.
1408 *
1409 * Default : /tmp
1410 */
1411 static const char *get_home_dir(void)
1412 {
1413 const char *home_path;
1414
1415 if ((home_path = (const char *) getenv("HOME")) == NULL) {
1416 home_path = default_home_dir;
1417 }
1418
1419 return home_path;
1420 }
1421
1422 /*
1423 * set_permissions
1424 *
1425 * Set the tracing group gid onto the client socket.
1426 *
1427 * Race window between mkdir and chown is OK because we are going from
1428 * less permissive (root.root) to more permissive (root.tracing).
1429 */
1430 static int set_permissions(void)
1431 {
1432 int ret;
1433 struct group *grp;
1434
1435 /* Decide which group name to use */
1436 (opt_tracing_group != NULL) ?
1437 (grp = getgrnam(opt_tracing_group)) :
1438 (grp = getgrnam(default_tracing_group));
1439
1440 if (grp == NULL) {
1441 ERR("Missing tracing group. Aborting execution.\n");
1442 ret = -1;
1443 goto end;
1444 }
1445
1446 /* Set lttng run dir */
1447 ret = chown(LTTNG_RUNDIR, 0, grp->gr_gid);
1448 if (ret < 0) {
1449 ERR("Unable to set group on " LTTNG_RUNDIR);
1450 perror("chown");
1451 }
1452
1453 /* lttng client socket path */
1454 ret = chown(client_unix_sock_path, 0, grp->gr_gid);
1455 if (ret < 0) {
1456 ERR("Unable to set group on %s", client_unix_sock_path);
1457 perror("chown");
1458 }
1459
1460 /* kconsumerd error socket path */
1461 ret = chown(kconsumerd_err_unix_sock_path, 0, grp->gr_gid);
1462 if (ret < 0) {
1463 ERR("Unable to set group on %s", kconsumerd_err_unix_sock_path);
1464 perror("chown");
1465 }
1466
1467 DBG("All permissions are set");
1468
1469 end:
1470 return ret;
1471 }
1472
1473 /*
1474 * create_lttng_rundir
1475 *
1476 * Create the lttng run directory needed for all
1477 * global sockets and pipe.
1478 */
1479 static int create_lttng_rundir(void)
1480 {
1481 int ret;
1482
1483 ret = mkdir(LTTNG_RUNDIR, S_IRWXU | S_IRWXG );
1484 if (ret < 0) {
1485 if (errno != EEXIST) {
1486 ERR("Unable to create " LTTNG_RUNDIR);
1487 goto error;
1488 } else {
1489 ret = 0;
1490 }
1491 }
1492
1493 error:
1494 return ret;
1495 }
1496
1497 /*
1498 * set_kconsumerd_sockets
1499 *
1500 * Setup sockets and directory needed by the kconsumerd
1501 * communication with the session daemon.
1502 */
1503 static int set_kconsumerd_sockets(void)
1504 {
1505 int ret;
1506
1507 if (strlen(kconsumerd_err_unix_sock_path) == 0) {
1508 snprintf(kconsumerd_err_unix_sock_path, PATH_MAX, KCONSUMERD_ERR_SOCK_PATH);
1509 }
1510
1511 if (strlen(kconsumerd_cmd_unix_sock_path) == 0) {
1512 snprintf(kconsumerd_cmd_unix_sock_path, PATH_MAX, KCONSUMERD_CMD_SOCK_PATH);
1513 }
1514
1515 ret = mkdir(KCONSUMERD_PATH, S_IRWXU | S_IRWXG);
1516 if (ret < 0) {
1517 ERR("Failed to create " KCONSUMERD_PATH);
1518 goto error;
1519 }
1520
1521 /* Create the kconsumerd error unix socket */
1522 kconsumerd_err_sock = lttcomm_create_unix_sock(kconsumerd_err_unix_sock_path);
1523 if (kconsumerd_err_sock < 0) {
1524 ERR("Create unix sock failed: %s", kconsumerd_err_unix_sock_path);
1525 ret = -1;
1526 goto error;
1527 }
1528
1529 /* File permission MUST be 660 */
1530 ret = chmod(kconsumerd_err_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
1531 if (ret < 0) {
1532 ERR("Set file permissions failed: %s", kconsumerd_err_unix_sock_path);
1533 perror("chmod");
1534 goto error;
1535 }
1536
1537 error:
1538 return ret;
1539 }
1540
1541 /*
1542 * sighandler
1543 *
1544 * Signal handler for the daemon
1545 */
1546 static void sighandler(int sig)
1547 {
1548 switch (sig) {
1549 case SIGPIPE:
1550 DBG("SIGPIPE catched");
1551 return;
1552 case SIGINT:
1553 DBG("SIGINT catched");
1554 cleanup();
1555 break;
1556 case SIGTERM:
1557 DBG("SIGTERM catched");
1558 cleanup();
1559 break;
1560 default:
1561 break;
1562 }
1563
1564 exit(EXIT_SUCCESS);
1565 }
1566
1567 /*
1568 * set_signal_handler
1569 *
1570 * Setup signal handler for :
1571 * SIGINT, SIGTERM, SIGPIPE
1572 */
1573 static int set_signal_handler(void)
1574 {
1575 int ret = 0;
1576 struct sigaction sa;
1577 sigset_t sigset;
1578
1579 if ((ret = sigemptyset(&sigset)) < 0) {
1580 perror("sigemptyset");
1581 return ret;
1582 }
1583
1584 sa.sa_handler = sighandler;
1585 sa.sa_mask = sigset;
1586 sa.sa_flags = 0;
1587 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
1588 perror("sigaction");
1589 return ret;
1590 }
1591
1592 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
1593 perror("sigaction");
1594 return ret;
1595 }
1596
1597 if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) {
1598 perror("sigaction");
1599 return ret;
1600 }
1601
1602 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
1603
1604 return ret;
1605 }
1606
1607 /*
1608 * main
1609 */
1610 int main(int argc, char **argv)
1611 {
1612 int ret = 0;
1613 void *status;
1614
1615 /* Parse arguments */
1616 progname = argv[0];
1617 if ((ret = parse_args(argc, argv) < 0)) {
1618 goto error;
1619 }
1620
1621 /* Daemonize */
1622 if (opt_daemon) {
1623 ret = daemon(0, 0);
1624 if (ret < 0) {
1625 perror("daemon");
1626 goto error;
1627 }
1628 }
1629
1630 /* Check if daemon is UID = 0 */
1631 is_root = !getuid();
1632
1633 /* Set all sockets path */
1634 if (is_root) {
1635 ret = create_lttng_rundir();
1636 if (ret < 0) {
1637 goto error;
1638 }
1639
1640 if (strlen(apps_unix_sock_path) == 0) {
1641 snprintf(apps_unix_sock_path, PATH_MAX,
1642 DEFAULT_GLOBAL_APPS_UNIX_SOCK);
1643 }
1644
1645 if (strlen(client_unix_sock_path) == 0) {
1646 snprintf(client_unix_sock_path, PATH_MAX,
1647 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK);
1648 }
1649
1650 ret = set_kconsumerd_sockets();
1651 if (ret < 0) {
1652 goto error;
1653 }
1654
1655 /* Setup kernel tracer */
1656 init_kernel_tracer();
1657 } else {
1658 if (strlen(apps_unix_sock_path) == 0) {
1659 snprintf(apps_unix_sock_path, PATH_MAX,
1660 DEFAULT_HOME_APPS_UNIX_SOCK, get_home_dir());
1661 }
1662
1663 /* Set the cli tool unix socket path */
1664 if (strlen(client_unix_sock_path) == 0) {
1665 snprintf(client_unix_sock_path, PATH_MAX,
1666 DEFAULT_HOME_CLIENT_UNIX_SOCK, get_home_dir());
1667 }
1668 }
1669
1670 DBG("Client socket path %s", client_unix_sock_path);
1671 DBG("Application socket path %s", apps_unix_sock_path);
1672
1673 /* See if daemon already exist. If any of the two
1674 * socket needed by the daemon are present, this test fails
1675 */
1676 if ((ret = check_existing_daemon()) == 0) {
1677 ERR("Already running daemon.\n");
1678 /* We do not goto error because we must not
1679 * cleanup() because a daemon is already running.
1680 */
1681 exit(EXIT_FAILURE);
1682 }
1683
1684 if (set_signal_handler() < 0) {
1685 goto error;
1686 }
1687
1688 /* Setup the needed unix socket */
1689 if (init_daemon_socket() < 0) {
1690 goto error;
1691 }
1692
1693 /* Set credentials to socket */
1694 if (is_root && (set_permissions() < 0)) {
1695 goto error;
1696 }
1697
1698 /* Get parent pid if -S, --sig-parent is specified. */
1699 if (opt_sig_parent) {
1700 ppid = getppid();
1701 }
1702
1703 while (1) {
1704 /* Create thread to manage the client socket */
1705 ret = pthread_create(&client_thread, NULL, thread_manage_clients, (void *) NULL);
1706 if (ret != 0) {
1707 perror("pthread_create");
1708 goto error;
1709 }
1710
1711 /* Create thread to manage application socket */
1712 ret = pthread_create(&apps_thread, NULL, thread_manage_apps, (void *) NULL);
1713 if (ret != 0) {
1714 perror("pthread_create");
1715 goto error;
1716 }
1717
1718 ret = pthread_join(client_thread, &status);
1719 if (ret != 0) {
1720 perror("pthread_join");
1721 goto error;
1722 }
1723 }
1724
1725 cleanup();
1726 exit(EXIT_SUCCESS);
1727
1728 error:
1729 cleanup();
1730 exit(EXIT_FAILURE);
1731 }
This page took 0.066339 seconds and 4 git commands to generate.