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