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