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