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