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