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