Add ust start trace feature
[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 <signal.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sys/ipc.h>
30 #include <sys/shm.h>
31 #include <sys/socket.h>
32 #include <sys/stat.h>
33 #include <sys/types.h>
34 #include <unistd.h>
35
36 #include <urcu/list.h> /* URCU list library (-lurcu) */
37 #include <ust/ustctl.h> /* UST control lib (-lust) */
38 #include <lttng/liblttngctl.h>
39
40 #include "liblttsessiondcomm.h"
41 #include "ltt-sessiond.h"
42 #include "lttngerr.h"
43 #include "session.h"
44 #include "trace.h"
45 #include "traceable-app.h"
46
47 /* Const values */
48 const char default_home_dir[] = DEFAULT_HOME_DIR;
49 const char default_tracing_group[] = DEFAULT_TRACING_GROUP;
50 const char default_ust_sock_dir[] = DEFAULT_UST_SOCK_DIR;
51 const char default_global_apps_pipe[] = DEFAULT_GLOBAL_APPS_PIPE;
52
53 /* Static functions */
54 static int check_existing_daemon(void);
55 static int connect_app(pid_t pid);
56 static int init_daemon_socket(void);
57 static int notify_apps(const char* name);
58 static int process_client_msg(int sock, struct lttcomm_session_msg*);
59 static int send_unix_sock(int sock, void *buf, size_t len);
60 static int set_signal_handler(void);
61 static int set_socket_perms(void);
62 static int setup_data_buffer(char **buf, size_t size, struct lttcomm_lttng_msg *llm);
63 static void cleanup(void);
64 static void copy_common_data(struct lttcomm_lttng_msg *llm, struct lttcomm_session_msg *lsm);
65 static void sighandler(int sig);
66
67 static void *thread_manage_clients(void *data);
68 static void *thread_manage_apps(void *data);
69
70 /* Variables */
71 int opt_verbose;
72 int opt_quiet;
73 const char *progname;
74 const char *opt_tracing_group;
75 static int opt_sig_parent;
76 static int opt_daemon;
77 static int is_root; /* Set to 1 if the daemon is running as root */
78 static pid_t ppid;
79
80 static char apps_unix_sock_path[PATH_MAX]; /* Global application Unix socket path */
81 static char client_unix_sock_path[PATH_MAX]; /* Global client Unix socket path */
82
83 static int client_socket;
84 static int apps_socket;
85
86 static struct ltt_session *current_session;
87
88 /*
89 * thread_manage_apps
90 *
91 * This thread manage the application socket communication
92 */
93 static void *thread_manage_apps(void *data)
94 {
95 int sock, ret;
96
97 /* TODO: Something more elegant is needed but fine for now */
98 struct {
99 int reg; /* 1:register, 0:unregister */
100 pid_t pid;
101 uid_t uid;
102 } reg_msg;
103
104 /* Notify all applications to register */
105 notify_apps(default_global_apps_pipe);
106
107 ret = lttcomm_listen_unix_sock(apps_socket);
108 if (ret < 0) {
109 goto error;
110 }
111
112 while (1) {
113 /* Blocking call, waiting for transmission */
114 sock = lttcomm_accept_unix_sock(apps_socket);
115 if (sock < 0) {
116 goto error;
117 }
118
119 /* Basic recv here to handle the very simple data
120 * that the libust send to register (reg_msg).
121 */
122 ret = recv(sock, &reg_msg, sizeof(reg_msg), 0);
123 if (ret < 0) {
124 perror("recv");
125 continue;
126 }
127
128 /* Add application to the global traceable list */
129 if (reg_msg.reg == 1) {
130 /* Registering */
131 ret = register_traceable_app(reg_msg.pid, reg_msg.uid);
132 if (ret < 0) {
133 /* register_traceable_app only return an error with
134 * ENOMEM. At this point, we better stop everything.
135 */
136 goto error;
137 }
138 } else {
139 /* Unregistering */
140 unregister_traceable_app(reg_msg.pid);
141 }
142 }
143
144 error:
145
146 return NULL;
147 }
148
149 /*
150 * thread_manage_clients
151 *
152 * This thread manage all clients request using the unix
153 * client socket for communication.
154 */
155 static void *thread_manage_clients(void *data)
156 {
157 int sock, ret;
158 struct lttcomm_session_msg lsm;
159
160 ret = lttcomm_listen_unix_sock(client_socket);
161 if (ret < 0) {
162 goto error;
163 }
164
165 /* Notify parent pid that we are ready
166 * to accept command for client side.
167 */
168 if (opt_sig_parent) {
169 kill(ppid, SIGCHLD);
170 }
171
172 while (1) {
173 /* Blocking call, waiting for transmission */
174 sock = lttcomm_accept_unix_sock(client_socket);
175 if (sock < 0) {
176 goto error;
177 }
178
179 /*
180 * Data is received from the lttng client. The struct
181 * lttcomm_session_msg (lsm) contains the command and data
182 * request of the client.
183 */
184 ret = lttcomm_recv_unix_sock(sock, &lsm, sizeof(lsm));
185 if (ret <= 0) {
186 continue;
187 }
188
189 /* This function dispatch the work to the LTTng or UST libs
190 * and then sends back the response to the client. This is needed
191 * because there might be more then one lttcomm_lttng_msg to
192 * send out so process_client_msg do both jobs.
193 */
194 ret = process_client_msg(sock, &lsm);
195 if (ret < 0) {
196 /* Error detected but still accept command */
197 continue;
198 }
199 }
200
201 error:
202 return NULL;
203 }
204
205 /*
206 * send_unix_sock
207 *
208 * Send data on a unix socket using the liblttsessiondcomm API.
209 *
210 * Return lttcomm error code.
211 */
212 static int send_unix_sock(int sock, void *buf, size_t len)
213 {
214 /* Check valid length */
215 if (len <= 0) {
216 return -1;
217 }
218
219 return lttcomm_send_unix_sock(sock, buf, len);
220 }
221
222 /*
223 * connect_app
224 *
225 * Return a socket connected to the libust communication socket
226 * of the application identified by the pid.
227 *
228 * If the pid is not found in the traceable list,
229 * return -1 to indicate error.
230 */
231 static int connect_app(pid_t pid)
232 {
233 int sock;
234 struct ltt_traceable_app *lta;
235
236 lta = find_app_by_pid(pid);
237 if (lta == NULL) {
238 /* App not found */
239 return -1;
240 }
241
242 sock = ustctl_connect_pid(lta->pid);
243 if (sock < 0) {
244 ERR("Fail connecting to the PID %d\n", pid);
245 }
246
247 return sock;
248 }
249
250 /*
251 * notify_apps
252 *
253 * Notify apps by writing 42 to a named pipe using name.
254 * Every applications waiting for a ltt-sessiond will be notified
255 * and re-register automatically to the session daemon.
256 *
257 * Return open or write error value.
258 */
259 static int notify_apps(const char *name)
260 {
261 int fd;
262 int ret = -1;
263
264 /* Try opening the global pipe */
265 fd = open(name, O_WRONLY);
266 if (fd < 0) {
267 goto error;
268 }
269
270 /* Notify by writing on the pipe */
271 ret = write(fd, "42", 2);
272 if (ret < 0) {
273 perror("write");
274 }
275
276 error:
277 return ret;
278 }
279
280 /*
281 * ust_create_trace
282 *
283 * Create an userspace trace using pid.
284 * This trace is then appended to the current session
285 * ust trace list.
286 */
287 static int ust_create_trace(pid_t pid)
288 {
289 int sock, ret;
290 struct ltt_ust_trace *trace;
291
292 trace = malloc(sizeof(struct ltt_ust_trace));
293 if (trace == NULL) {
294 perror("malloc");
295 ret = -1;
296 goto error;
297 }
298
299 /* Init */
300 trace->pid = pid;
301 trace->shmid = 0;
302
303 /* Connect to app using ustctl API */
304 sock = connect_app(pid);
305 if (sock < 0) {
306 ret = LTTCOMM_NO_TRACEABLE;
307 goto error;
308 }
309
310 ret = ustctl_create_trace(sock, "auto");
311 if (ret < 0) {
312 ret = LTTCOMM_CREATE_FAIL;
313 goto error;
314 }
315
316 /* Check if current session is valid */
317 if (current_session) {
318 cds_list_add(&trace->list, &current_session->ust_traces);
319 }
320
321 error:
322 return ret;
323 }
324
325 /*
326 * ust_start_trace
327 *
328 * Start a trace. This trace, identified by the pid, must be
329 * in the current session ust_traces list.
330 */
331 static int ust_start_trace(pid_t pid)
332 {
333 int sock, ret;
334 struct ltt_ust_trace *trace;
335
336 DBG("Starting trace for pid %d", pid);
337
338 trace = find_session_ust_trace_by_pid(current_session, pid);
339 if (trace == NULL) {
340 ret = LTTCOMM_NO_TRACE;
341 goto error;
342 }
343
344 /* Connect to app using ustctl API */
345 sock = connect_app(pid);
346 if (sock < 0) {
347 ret = LTTCOMM_NO_TRACEABLE;
348 goto error;
349 }
350
351 ret = ustctl_start_trace(sock, "auto");
352 if (ret < 0) {
353 ret = LTTCOMM_START_FAIL;
354 goto error;
355 }
356
357 error:
358 return ret;
359 }
360
361 /*
362 * copy_common_data
363 *
364 * Copy common data between lttcomm_lttng_msg and lttcomm_session_msg
365 */
366 static void copy_common_data(struct lttcomm_lttng_msg *llm, struct lttcomm_session_msg *lsm)
367 {
368 llm->cmd_type = lsm->cmd_type;
369 llm->pid = lsm->pid;
370
371 /* Manage uuid */
372 if (!uuid_is_null(lsm->session_id)) {
373 uuid_copy(llm->session_id, lsm->session_id);
374 }
375
376 strncpy(llm->trace_name, lsm->trace_name, strlen(llm->trace_name));
377 llm->trace_name[strlen(llm->trace_name) - 1] = '\0';
378 }
379
380 /*
381 * setup_data_buffer
382 *
383 * Setup the outgoing data buffer for the response
384 * data allocating the right amount of memory.
385 *
386 * Return total size of the buffer pointed by buf.
387 */
388 static int setup_data_buffer(char **buf, size_t s_data, struct lttcomm_lttng_msg *llm)
389 {
390 int ret = 0;
391 size_t buf_size;
392
393 buf_size = sizeof(struct lttcomm_lttng_msg) + s_data;
394 *buf = malloc(buf_size);
395 if (*buf == NULL) {
396 perror("malloc");
397 ret = -1;
398 goto error;
399 }
400
401 /* Setup lttcomm_lttng_msg data and copy
402 * it to the newly allocated buffer.
403 */
404 llm->size_payload = s_data;
405 memcpy(*buf, llm, sizeof(struct lttcomm_lttng_msg));
406
407 return buf_size;
408
409 error:
410 return ret;
411 }
412
413 /*
414 * process_client_msg
415 *
416 * This takes the lttcomm_session_msg struct and process the command requested
417 * by the client. It then creates response(s) and send it back to the
418 * given socket (sock).
419 *
420 * Return any error encountered or 0 for success.
421 */
422 static int process_client_msg(int sock, struct lttcomm_session_msg *lsm)
423 {
424 int ret;
425 int buf_size;
426 size_t header_size;
427 char *send_buf = NULL;
428 struct lttcomm_lttng_msg llm;
429
430 /* Copy common data to identify the response
431 * on the lttng client side.
432 */
433 copy_common_data(&llm, lsm);
434
435 /* Check command that needs a session */
436 if (lsm->cmd_type != LTTNG_CREATE_SESSION &&
437 lsm->cmd_type != LTTNG_LIST_SESSIONS &&
438 lsm->cmd_type != UST_LIST_APPS)
439 {
440 current_session = find_session_by_uuid(lsm->session_id);
441 if (current_session == NULL) {
442 ret = LTTCOMM_SELECT_SESS;
443 goto end;
444 }
445 }
446
447 /* Default return code.
448 * In our world, everything is OK... right? ;)
449 */
450 llm.ret_code = LTTCOMM_OK;
451
452 header_size = sizeof(struct lttcomm_lttng_msg);
453
454 /* Process by command type */
455 switch (lsm->cmd_type) {
456 case LTTNG_CREATE_SESSION:
457 {
458 ret = create_session(lsm->session_name, &llm.session_id);
459 if (ret < 0) {
460 if (ret == -1) {
461 ret = LTTCOMM_EXIST_SESS;
462 } else {
463 ret = LTTCOMM_FATAL;
464 }
465 goto end;
466 }
467
468 buf_size = setup_data_buffer(&send_buf, 0, &llm);
469 if (buf_size < 0) {
470 ret = LTTCOMM_FATAL;
471 goto end;
472 }
473
474 break;
475 }
476 case LTTNG_DESTROY_SESSION:
477 {
478 ret = destroy_session(&lsm->session_id);
479 if (ret < 0) {
480 ret = LTTCOMM_NO_SESS;
481 } else {
482 ret = LTTCOMM_OK;
483 }
484
485 /* No auxiliary data so only send the llm struct. */
486 goto end;
487 }
488 case UST_CREATE_TRACE:
489 {
490 ret = ust_create_trace(lsm->pid);
491 if (ret < 0) {
492 /* If -1 is returned from ust_create_trace, malloc
493 * failed so it's pretty much a fatal error.
494 */
495 ret = LTTCOMM_FATAL;
496 goto end;
497 }
498
499 /* No auxiliary data so only send the llm struct. */
500 goto end;
501 }
502 case UST_LIST_APPS:
503 {
504 unsigned int app_count = get_app_count();
505 /* Stop right now if no apps */
506 if (app_count == 0) {
507 ret = LTTCOMM_NO_APPS;
508 goto end;
509 }
510
511 /* Setup data buffer and details for transmission */
512 buf_size = setup_data_buffer(&send_buf,
513 sizeof(pid_t) * app_count, &llm);
514 if (buf_size < 0) {
515 ret = LTTCOMM_FATAL;
516 goto end;
517 }
518
519 get_app_list_pids((pid_t *)(send_buf + header_size));
520
521 break;
522 }
523 case UST_START_TRACE:
524 {
525 ret = ust_start_trace(lsm->pid);
526
527 /* No auxiliary data so only send the llm struct. */
528 goto end;
529 }
530 case LTTNG_LIST_SESSIONS:
531 {
532 unsigned int session_count = get_session_count();
533 /* Stop right now if no session */
534 if (session_count == 0) {
535 ret = LTTCOMM_NO_SESS;
536 goto end;
537 }
538
539 /* Setup data buffer and details for transmission */
540 buf_size = setup_data_buffer(&send_buf,
541 (sizeof(struct lttng_session) * session_count), &llm);
542 if (buf_size < 0) {
543 ret = LTTCOMM_FATAL;
544 goto end;
545 }
546
547 get_lttng_session((struct lttng_session *)(send_buf + header_size));
548
549 break;
550 }
551 default:
552 {
553 /* Undefined command */
554 ret = LTTCOMM_UND;
555 goto end;
556 }
557 }
558
559 ret = send_unix_sock(sock, send_buf, buf_size);
560
561 if (send_buf != NULL) {
562 free(send_buf);
563 }
564
565 return ret;
566
567 end:
568 /* Notify client of error */
569 llm.ret_code = ret;
570 llm.size_payload = 0;
571 send_unix_sock(sock, (void*) &llm, sizeof(llm));
572
573 return ret;
574 }
575
576 /*
577 * usage function on stderr
578 */
579 static void usage(void)
580 {
581 fprintf(stderr, "Usage: %s OPTIONS\n\nOptions:\n", progname);
582 fprintf(stderr, " -h, --help Display this usage.\n");
583 fprintf(stderr, " -c, --client-sock PATH Specify path for the client unix socket\n");
584 fprintf(stderr, " -a, --apps-sock PATH Specify path for apps unix socket.\n");
585 fprintf(stderr, " -d, --daemonize Start as a daemon.\n");
586 fprintf(stderr, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
587 fprintf(stderr, " -V, --version Show version number.\n");
588 fprintf(stderr, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
589 fprintf(stderr, " -q, --quiet No output at all.\n");
590 }
591
592 /*
593 * daemon argument parsing
594 */
595 static int parse_args(int argc, char **argv)
596 {
597 int c;
598
599 static struct option long_options[] = {
600 { "client-sock", 1, 0, 'c' },
601 { "apps-sock", 1, 0, 'a' },
602 { "daemonize", 0, 0, 'd' },
603 { "sig-parent", 0, 0, 'S' },
604 { "help", 0, 0, 'h' },
605 { "group", 1, 0, 'g' },
606 { "version", 0, 0, 'V' },
607 { "quiet", 0, 0, 'q' },
608 { NULL, 0, 0, 0 }
609 };
610
611 while (1) {
612 int option_index = 0;
613 c = getopt_long(argc, argv, "dhqVS" "a:c:g:s:", long_options, &option_index);
614 if (c == -1) {
615 break;
616 }
617
618 switch (c) {
619 case 0:
620 fprintf(stderr, "option %s", long_options[option_index].name);
621 if (optarg) {
622 fprintf(stderr, " with arg %s\n", optarg);
623 }
624 break;
625 case 'c':
626 snprintf(client_unix_sock_path, PATH_MAX, "%s", optarg);
627 break;
628 case 'a':
629 snprintf(apps_unix_sock_path, PATH_MAX, "%s", optarg);
630 break;
631 case 'd':
632 opt_daemon = 1;
633 break;
634 case 'g':
635 opt_tracing_group = strdup(optarg);
636 break;
637 case 'h':
638 usage();
639 exit(EXIT_FAILURE);
640 case 'V':
641 fprintf(stdout, "%s\n", VERSION);
642 exit(EXIT_SUCCESS);
643 case 'S':
644 opt_sig_parent = 1;
645 break;
646 case 'q':
647 opt_quiet = 1;
648 break;
649 default:
650 /* Unknown option or other error.
651 * Error is printed by getopt, just return */
652 return -1;
653 }
654 }
655
656 return 0;
657 }
658
659 /*
660 * init_daemon_socket
661 *
662 * Creates the two needed socket by the daemon.
663 * apps_socket - The communication socket for all UST apps.
664 * client_socket - The communication of the cli tool (lttng).
665 */
666 static int init_daemon_socket()
667 {
668 int ret = 0;
669 mode_t old_umask;
670
671 old_umask = umask(0);
672
673 /* Create client tool unix socket */
674 client_socket = lttcomm_create_unix_sock(client_unix_sock_path);
675 if (client_socket < 0) {
676 ret = -1;
677 goto end;
678 }
679
680 /* File permission MUST be 660 */
681 ret = chmod(client_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
682 if (ret < 0) {
683 perror("chmod");
684 goto end;
685 }
686
687 /* Create the application unix socket */
688 apps_socket = lttcomm_create_unix_sock(apps_unix_sock_path);
689 if (apps_socket < 0) {
690 ret = -1;
691 goto end;
692 }
693
694 /* File permission MUST be 660 */
695 ret = chmod(apps_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
696 if (ret < 0) {
697 perror("chmod");
698 goto end;
699 }
700
701 end:
702 umask(old_umask);
703 return ret;
704 }
705
706 /*
707 * check_existing_daemon
708 *
709 * Check if the global socket is available.
710 * If yes, error is returned.
711 */
712 static int check_existing_daemon()
713 {
714 int ret;
715
716 ret = access(client_unix_sock_path, F_OK);
717 if (ret == 0) {
718 ret = access(apps_unix_sock_path, F_OK);
719 }
720
721 return ret;
722 }
723
724 /*
725 * get_home_dir
726 *
727 * Return pointer to home directory path using
728 * the env variable HOME.
729 *
730 * Default : /tmp
731 */
732 static const char *get_home_dir(void)
733 {
734 const char *home_path;
735
736 if ((home_path = (const char *) getenv("HOME")) == NULL) {
737 home_path = default_home_dir;
738 }
739
740 return home_path;
741 }
742
743 /*
744 * set_socket_perms
745 *
746 * Set the tracing group gid onto the client socket.
747 */
748 static int set_socket_perms(void)
749 {
750 int ret;
751 struct group *grp;
752
753 /* Decide which group name to use */
754 (opt_tracing_group != NULL) ?
755 (grp = getgrnam(opt_tracing_group)) :
756 (grp = getgrnam(default_tracing_group));
757
758 if (grp == NULL) {
759 ERR("Missing tracing group. Aborting execution.\n");
760 ret = -1;
761 goto end;
762 }
763
764 ret = chown(client_unix_sock_path, 0, grp->gr_gid);
765 if (ret < 0) {
766 perror("chown");
767 }
768
769 end:
770 return ret;
771 }
772
773 /*
774 * set_signal_handler
775 *
776 * Setup signal handler for :
777 * SIGINT, SIGTERM, SIGPIPE
778 */
779 static int set_signal_handler(void)
780 {
781 int ret = 0;
782 struct sigaction sa;
783 sigset_t sigset;
784
785 if ((ret = sigemptyset(&sigset)) < 0) {
786 perror("sigemptyset");
787 return ret;
788 }
789
790 sa.sa_handler = sighandler;
791 sa.sa_mask = sigset;
792 sa.sa_flags = 0;
793 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
794 perror("sigaction");
795 return ret;
796 }
797
798 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
799 perror("sigaction");
800 return ret;
801 }
802
803 if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) {
804 perror("sigaction");
805 return ret;
806 }
807
808 return ret;
809 }
810
811 /**
812 * sighandler
813 *
814 * Signal handler for the daemon
815 */
816 static void sighandler(int sig)
817 {
818 switch (sig) {
819 case SIGPIPE:
820 return;
821 case SIGINT:
822 case SIGTERM:
823 cleanup();
824 break;
825 default:
826 break;
827 }
828
829 exit(EXIT_SUCCESS);
830 }
831
832 /*
833 * cleanup
834 *
835 * Cleanup the daemon on exit
836 */
837 static void cleanup()
838 {
839 /* <fun> */
840 MSG("\n%c[%d;%dm*** assert failed *** ==> %c[%dm", 27,1,31,27,0);
841 MSG("%c[%d;%dmMatthew, BEET driven development works!%c[%dm",27,1,33,27,0);
842 /* </fun> */
843
844 unlink(client_unix_sock_path);
845 unlink(apps_unix_sock_path);
846 }
847
848 /*
849 * main
850 */
851 int main(int argc, char **argv)
852 {
853 int i;
854 int ret = 0;
855 void *status;
856 pthread_t threads[2];
857
858 /* Parse arguments */
859 progname = argv[0];
860 if ((ret = parse_args(argc, argv) < 0)) {
861 goto error;
862 }
863
864 /* Daemonize */
865 if (opt_daemon) {
866 ret = daemon(0, 0);
867 if (ret < 0) {
868 perror("daemon");
869 goto error;
870 }
871 }
872
873 /* Check if daemon is UID = 0 */
874 is_root = !getuid();
875
876 /* Set all sockets path */
877 if (is_root) {
878 if (strlen(apps_unix_sock_path) == 0) {
879 (snprintf(apps_unix_sock_path, PATH_MAX,
880 DEFAULT_GLOBAL_APPS_UNIX_SOCK));
881 }
882
883 if (strlen(client_unix_sock_path) == 0) {
884 (snprintf(client_unix_sock_path, PATH_MAX,
885 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK));
886 }
887 } else {
888 if (strlen(apps_unix_sock_path) == 0) {
889 (snprintf(apps_unix_sock_path, PATH_MAX,
890 DEFAULT_HOME_APPS_UNIX_SOCK, get_home_dir()));
891 }
892
893 /* Set the cli tool unix socket path */
894 if (strlen(client_unix_sock_path) == 0) {
895 (snprintf(client_unix_sock_path, PATH_MAX,
896 DEFAULT_HOME_CLIENT_UNIX_SOCK, get_home_dir()));
897 }
898 }
899
900 /* See if daemon already exist. If any of the two
901 * socket needed by the daemon are present, this test fails
902 */
903 if ((ret = check_existing_daemon()) == 0) {
904 ERR("Already running daemon.\n");
905 /* We do not goto error because we must not
906 * cleanup() because a daemon is already working.
907 */
908 return EXIT_FAILURE;
909 }
910
911 if (set_signal_handler() < 0) {
912 goto error;
913 }
914
915 /* Setup the two needed unix socket */
916 if (init_daemon_socket() < 0) {
917 goto error;
918 }
919
920 /* Set credentials to socket */
921 if (is_root && (set_socket_perms() < 0)) {
922 goto error;
923 }
924
925 /* Get parent pid if -S, --sig-parent is specified. */
926 if (opt_sig_parent) {
927 ppid = getppid();
928 }
929
930 while (1) {
931 /* Create thread to manage the client socket */
932 ret = pthread_create(&threads[0], NULL, thread_manage_clients, (void *) NULL);
933 if (ret != 0) {
934 perror("pthread_create");
935 goto error;
936 }
937
938 /* Create thread to manage application socket */
939 ret = pthread_create(&threads[1], NULL, thread_manage_apps, (void *) NULL);
940 if (ret != 0) {
941 perror("pthread_create");
942 goto error;
943 }
944
945 for (i = 0; i < 2; i++) {
946 ret = pthread_join(threads[i], &status);
947 if (ret != 0) {
948 perror("pthread_join");
949 goto error;
950 }
951 }
952 }
953
954 cleanup();
955 return 0;
956
957 error:
958 cleanup();
959
960 return EXIT_FAILURE;
961 }
This page took 0.047554 seconds and 4 git commands to generate.