Clean some part of the code
[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);
1fd70b72 53static void sighandler(int sig);
fac6795d 54static void cleanup(void);
e065084a 55static void copy_common_data(struct lttcomm_lttng_msg *llm, struct lttcomm_session_msg *lsm);
fac6795d 56static int check_existing_daemon(void);
1fd70b72
DG
57static int notify_apps(const char* name);
58static int connect_app(pid_t pid);
fac6795d 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 67
1fd70b72
DG
68static void *thread_manage_clients(void *data);
69static void *thread_manage_apps(void *data);
fac6795d 70
aaf97519 71static int create_session(char *name, uuid_t *session_id);
1fd70b72 72static void destroy_session(uuid_t session_id);
fac6795d 73
1fd70b72 74static struct ltt_session *find_session(uuid_t session_id);
fac6795d
DG
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
1fd70b72 543 goto send;
aaf97519
DG
544 break;
545 }
fac6795d
DG
546 case UST_LIST_APPS:
547 {
ca95a216
DG
548 /* Stop right now if no apps */
549 if (traceable_app_count == 0) {
e065084a
DG
550 ret = LTTCOMM_NO_APPS;
551 goto error;
552 }
553
ca95a216
DG
554 /* Setup data buffer and details for transmission */
555 buf_size = setup_data_buffer(&send_buf,
556 sizeof(pid_t) * traceable_app_count, &llm);
557 if (buf_size < 0) {
558 ret = LTTCOMM_FATAL;
559 goto error;
560 }
561
aaf97519 562 get_list_apps((pid_t *)(send_buf + sizeof(struct lttcomm_lttng_msg)));
ca95a216 563
1fd70b72 564 goto send;
fac6795d
DG
565 break;
566 }
57167058
DG
567 case LTTNG_LIST_SESSIONS:
568 {
ca95a216
DG
569 /* Stop right now if no session */
570 if (session_count == 0) {
57167058
DG
571 ret = LTTCOMM_NO_SESS;
572 goto error;
573 }
574
ca95a216
DG
575 /* Setup data buffer and details for transmission */
576 buf_size = setup_data_buffer(&send_buf,
577 (sizeof(struct lttng_session) * session_count), &llm);
578 if (buf_size < 0) {
579 ret = LTTCOMM_FATAL;
580 goto error;
581 }
582
aaf97519 583 get_list_sessions((struct lttng_session *)(send_buf + sizeof(struct lttcomm_lttng_msg)));
ca95a216 584
1fd70b72 585 goto send;
57167058
DG
586 break;
587 }
fac6795d 588 default:
e065084a 589 {
fac6795d 590 /* Undefined command */
e065084a 591 ret = LTTCOMM_UND;
ca95a216 592 goto error;
e065084a 593 }
fac6795d
DG
594 }
595
1fd70b72
DG
596send:
597 ret = send_unix_sock(sock, send_buf, buf_size);
598
ca95a216
DG
599 if (send_buf != NULL) {
600 free(send_buf);
601 }
602
e065084a
DG
603 return ret;
604
605error:
606 /* Notify client of error */
607 llm.ret_code = ret;
ca95a216 608 llm.size_payload = 0;
e065084a
DG
609 send_unix_sock(sock, (void*) &llm, sizeof(llm));
610
611 return -1;
fac6795d
DG
612}
613
614/*
615 * usage function on stderr
616 */
617static void usage(void)
618{
619 fprintf(stderr, "Usage:\n%s OPTIONS\n\nOptions:\n"
620 "\t-h, --help\t\tDisplay this usage.\n"
621 "\t-c, --client-sock PATH\t\tSpecify path for the client unix socket\n"
622 "\t-a, --apps-sock PATH\t\tSpecify path for apps unix socket.\n"
623 "\t-d, --daemonize\t\tStart as a daemon.\n"
624 "\t-g, --group NAME\t\tSpecify the tracing group name. (default: tracing)\n"
5b8719f5 625 "\t-V, --version\t\tShow version number.\n"
75462a81
DG
626 "\t-S, --sig-parent\t\tSend SIGCHLD to parent pid to notify readiness.\n"
627 "\t-q, --quiet\t\tNo output at all.\n",
fac6795d
DG
628 progname);
629}
630
631/*
632 * daemon argument parsing
633 */
634static int parse_args(int argc, char **argv)
635{
636 int c;
637
638 static struct option long_options[] = {
639 { "client-sock", 1, 0, 'c' },
640 { "apps-sock", 1, 0, 'a' },
641 { "daemonize", 0, 0, 'd' },
5b8719f5 642 { "sig-parent", 0, 0, 'S' },
fac6795d
DG
643 { "help", 0, 0, 'h' },
644 { "group", 1, 0, 'g' },
645 { "version", 0, 0, 'V' },
75462a81 646 { "quiet", 0, 0, 'q' },
fac6795d
DG
647 { NULL, 0, 0, 0 }
648 };
649
650 while (1) {
651 int option_index = 0;
75462a81 652 c = getopt_long(argc, argv, "dhqVS" "a:c:g:s:", long_options, &option_index);
fac6795d
DG
653 if (c == -1) {
654 break;
655 }
656
657 switch (c) {
658 case 0:
659 fprintf(stderr, "option %s", long_options[option_index].name);
660 if (optarg) {
661 fprintf(stderr, " with arg %s\n", optarg);
662 }
663 break;
664 case 's':
665 snprintf(client_unix_sock_path, PATH_MAX, "%s", optarg);
666 break;
667 case 'a':
668 snprintf(apps_unix_sock_path, PATH_MAX, "%s", optarg);
669 break;
670 case 'd':
671 opt_daemon = 1;
672 break;
673 case 'g':
674 opt_tracing_group = strdup(optarg);
675 break;
676 case 'h':
677 usage();
678 exit(EXIT_FAILURE);
679 case 'V':
680 fprintf(stdout, "%s\n", VERSION);
681 exit(EXIT_SUCCESS);
5b8719f5
DG
682 case 'S':
683 opt_sig_parent = 1;
684 break;
75462a81
DG
685 case 'q':
686 opt_quiet = 1;
687 break;
fac6795d
DG
688 default:
689 /* Unknown option or other error.
690 * Error is printed by getopt, just return */
691 return -1;
692 }
693 }
694
695 return 0;
696}
697
698/*
699 * init_daemon_socket
700 *
701 * Creates the two needed socket by the daemon.
702 * apps_socket - The communication socket for all UST apps.
703 * client_socket - The communication of the cli tool (lttng).
704 */
705static int init_daemon_socket()
706{
707 int ret = 0;
708 mode_t old_umask;
709
710 old_umask = umask(0);
711
712 /* Create client tool unix socket */
713 client_socket = lttcomm_create_unix_sock(client_unix_sock_path);
714 if (client_socket < 0) {
715 ret = -1;
716 goto end;
717 }
718
719 /* File permission MUST be 660 */
720 ret = chmod(client_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
721 if (ret < 0) {
722 perror("chmod");
723 goto end;
724 }
725
726 /* Create the application unix socket */
727 apps_socket = lttcomm_create_unix_sock(apps_unix_sock_path);
728 if (apps_socket < 0) {
729 ret = -1;
730 goto end;
731 }
732
733 /* File permission MUST be 660 */
734 ret = chmod(apps_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
735 if (ret < 0) {
736 perror("chmod");
737 goto end;
738 }
739
740end:
741 umask(old_umask);
742 return ret;
743}
744
745/*
746 * check_existing_daemon
747 *
748 * Check if the global socket is available.
749 * If yes, error is returned.
750 */
751static int check_existing_daemon()
752{
753 int ret;
754
755 ret = access(client_unix_sock_path, F_OK);
756 if (ret == 0) {
757 ret = access(apps_unix_sock_path, F_OK);
758 }
759
760 return ret;
761}
762
763/*
764 * get_home_dir
765 *
766 * Return pointer to home directory path using
767 * the env variable HOME.
768 *
769 * Default : /tmp
770 */
771static const char *get_home_dir(void)
772{
773 const char *home_path;
774
686204ab 775 if ((home_path = (const char *) getenv("HOME")) == NULL) {
fac6795d
DG
776 home_path = default_home_dir;
777 }
778
779 return home_path;
780}
781
782/*
783 * set_socket_perms
784 *
785 * Set the tracing group gid onto the client socket.
786 */
787static int set_socket_perms(void)
788{
789 int ret;
790 struct group *grp;
791
792 /* Decide which group name to use */
793 (opt_tracing_group != NULL) ?
794 (grp = getgrnam(opt_tracing_group)) :
795 (grp = getgrnam(default_tracing_group));
796
797 if (grp == NULL) {
75462a81 798 ERR("Missing tracing group. Aborting execution.\n");
fac6795d
DG
799 ret = -1;
800 goto end;
801 }
802
803 ret = chown(client_unix_sock_path, 0, grp->gr_gid);
804 if (ret < 0) {
805 perror("chown");
806 }
807
808end:
809 return ret;
810}
811
fac6795d
DG
812/*
813 * set_signal_handler
814 *
815 * Setup signal handler for :
816 * SIGINT, SIGTERM, SIGPIPE
817 */
818static int set_signal_handler(void)
819{
820 int ret = 0;
821 struct sigaction sa;
822 sigset_t sigset;
823
824 if ((ret = sigemptyset(&sigset)) < 0) {
825 perror("sigemptyset");
826 return ret;
827 }
828
829 sa.sa_handler = sighandler;
830 sa.sa_mask = sigset;
831 sa.sa_flags = 0;
832 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
833 perror("sigaction");
834 return ret;
835 }
836
837 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
838 perror("sigaction");
839 return ret;
840 }
841
842 if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) {
843 perror("sigaction");
844 return ret;
845 }
846
847 return ret;
848}
849
850/**
851 * sighandler
852 *
853 * Signal handler for the daemon
854 */
855static void sighandler(int sig)
856{
857 switch (sig) {
858 case SIGPIPE:
859 case SIGINT:
860 case SIGTERM:
861 cleanup();
686204ab 862 break;
fac6795d
DG
863 default:
864 break;
865 }
866
867 exit(EXIT_SUCCESS);
868}
869
870/*
871 * cleanup
872 *
873 * Cleanup the daemon on exit
874 */
875static void cleanup()
876{
877 /* <fun> */
75462a81
DG
878 MSG("\n\n%c[%d;%dm*** assert failed *** ==> %c[%dm", 27,1,31,27,0);
879 MSG("%c[%d;%dm Matthew, BEET driven development works!%c[%dm\n",27,1,33,27,0);
fac6795d
DG
880 /* </fun> */
881
882 unlink(client_unix_sock_path);
883 unlink(apps_unix_sock_path);
884}
885
886/*
887 * main
888 */
889int main(int argc, char **argv)
890{
891 int i;
892 int ret = 0;
893 void *status;
894 pthread_t threads[2];
895
896 /* Parse arguments */
897 progname = argv[0];
898 if ((ret = parse_args(argc, argv) < 0)) {
899 goto error;
900 }
901
902 /* Daemonize */
903 if (opt_daemon) {
53094c05
DG
904 ret = daemon(0, 0);
905 if (ret < 0) {
906 perror("daemon");
907 goto error;
908 }
fac6795d
DG
909 }
910
911 /* Check if daemon is UID = 0 */
912 is_root = !getuid();
913
914 /* Set all sockets path */
915 if (is_root) {
916 if (strlen(apps_unix_sock_path) == 0) {
917 (snprintf(apps_unix_sock_path, PATH_MAX,
918 DEFAULT_GLOBAL_APPS_UNIX_SOCK));
919 }
920
921 if (strlen(client_unix_sock_path) == 0) {
922 (snprintf(client_unix_sock_path, PATH_MAX,
923 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK));
924 }
925 } else {
926 if (strlen(apps_unix_sock_path) == 0) {
927 (snprintf(apps_unix_sock_path, PATH_MAX,
928 DEFAULT_HOME_APPS_UNIX_SOCK, get_home_dir()));
929 }
930
931 /* Set the cli tool unix socket path */
932 if (strlen(client_unix_sock_path) == 0) {
933 (snprintf(client_unix_sock_path, PATH_MAX,
934 DEFAULT_HOME_CLIENT_UNIX_SOCK, get_home_dir()));
935 }
936 }
937
938 /* See if daemon already exist. If any of the two
939 * socket needed by the daemon are present, this test fails
940 */
941 if ((ret = check_existing_daemon()) == 0) {
75462a81 942 ERR("Already running daemon.\n");
fac6795d
DG
943 goto error;
944 }
945
946 if (set_signal_handler() < 0) {
947 goto error;
948 }
949
950 /* Setup the two needed unix socket */
951 if (init_daemon_socket() < 0) {
952 goto error;
953 }
954
955 /* Set credentials to socket */
956 if (is_root && (set_socket_perms() < 0)) {
957 goto error;
958 }
959
5b8719f5
DG
960 /* Get parent pid if -S, --sig-parent is specified. */
961 if (opt_sig_parent) {
962 ppid = getppid();
963 }
964
fac6795d
DG
965 while (1) {
966 /* Create thread to manage the client socket */
967 ret = pthread_create(&threads[0], NULL, thread_manage_clients, (void *) NULL);
968 if (ret != 0) {
969 perror("pthread_create");
970 goto error;
971 }
972
973 /* Create thread to manage application socket */
974 ret = pthread_create(&threads[1], NULL, thread_manage_apps, (void *) NULL);
975 if (ret != 0) {
976 perror("pthread_create");
977 goto error;
978 }
979
980 for (i = 0; i < 2; i++) {
981 ret = pthread_join(threads[i], &status);
982 if (ret != 0) {
983 perror("pthread_join");
984 goto error;
985 }
986 }
987 }
988
989 cleanup();
990 return 0;
991
992error:
993 cleanup();
994
995 return EXIT_FAILURE;
996}
This page took 0.06545 seconds and 5 git commands to generate.