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