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