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