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