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