4b2fb52c2dd256e5a68809af7107d1dc78631c29
[lttng-tools.git] / ltt-sessiond / main.c
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 <semaphore.h>
26 #include <signal.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/ipc.h>
31 #include <sys/shm.h>
32 #include <sys/socket.h>
33 #include <sys/stat.h>
34 #include <sys/types.h>
35 #include <unistd.h>
36
37 #include <urcu/list.h> /* URCU list library (-lurcu) */
38 #include <ust/ustctl.h> /* UST control lib (-lust) */
39 #include <lttng/lttng.h>
40
41 #include "liblttsessiondcomm.h"
42 #include "ltt-sessiond.h"
43 #include "lttngerr.h"
44 #include "kernel-ctl.h"
45 #include "ust-ctl.h"
46 #include "session.h"
47 #include "traceable-app.h"
48
49 /*
50 * TODO:
51 * teardown: signal SIGTERM handler -> write into pipe. Threads waits
52 * with epoll on pipe and on other pipes/sockets for commands. Main
53 * simply waits on pthread join.
54 */
55
56 /* Const values */
57 const char default_home_dir[] = DEFAULT_HOME_DIR;
58 const char default_tracing_group[] = LTTNG_DEFAULT_TRACING_GROUP;
59 const char default_ust_sock_dir[] = DEFAULT_UST_SOCK_DIR;
60 const char default_global_apps_pipe[] = DEFAULT_GLOBAL_APPS_PIPE;
61
62 /* Variables */
63 int opt_verbose; /* Not static for lttngerr.h */
64 int opt_quiet; /* Not static for lttngerr.h */
65 const char *progname;
66 const char *opt_tracing_group;
67 static int opt_sig_parent;
68 static int opt_daemon;
69 static int is_root; /* Set to 1 if the daemon is running as root */
70 static pid_t ppid; /* Parent PID for --sig-parent option */
71 static pid_t kconsumerd_pid;
72
73 static char apps_unix_sock_path[PATH_MAX]; /* Global application Unix socket path */
74 static char client_unix_sock_path[PATH_MAX]; /* Global client Unix socket path */
75 static char kconsumerd_err_unix_sock_path[PATH_MAX]; /* kconsumerd error Unix socket path */
76 static char kconsumerd_cmd_unix_sock_path[PATH_MAX]; /* kconsumerd command Unix socket path */
77
78 /* Sockets and FDs */
79 static int client_sock;
80 static int apps_sock;
81 static int kconsumerd_err_sock;
82 static int kconsumerd_cmd_sock;
83 static int kernel_tracer_fd;
84
85 /* Pthread, Mutexes and Semaphores */
86 static pthread_t kconsumerd_thread;
87 static pthread_t apps_thread;
88 static pthread_t client_thread;
89 static sem_t kconsumerd_sem;
90
91 static pthread_mutex_t kconsumerd_pid_mutex; /* Mutex to control kconsumerd pid assignation */
92
93 /*
94 * teardown_kernel_session
95 *
96 * Complete teardown of a kernel session. This free all data structure
97 * related to a kernel session and update counter.
98 */
99 static void teardown_kernel_session(struct ltt_session *session)
100 {
101 if (session->kernel_session != NULL) {
102 DBG("Tearing down kernel session");
103 trace_destroy_kernel_session(session->kernel_session);
104 /* Extra precaution */
105 session->kernel_session = NULL;
106 /* Decrement session count */
107 session->kern_session_count--;
108 }
109 }
110
111 /*
112 * cleanup
113 *
114 * Cleanup the daemon on exit
115 */
116 static void cleanup()
117 {
118 int ret;
119 char *cmd;
120 struct ltt_session *sess;
121
122 DBG("Cleaning up");
123
124 /* <fun> */
125 MSG("\n%c[%d;%dm*** assert failed *** ==> %c[%dm", 27,1,31,27,0);
126 MSG("%c[%d;%dmMatthew, BEET driven development works!%c[%dm",27,1,33,27,0);
127 /* </fun> */
128
129 /* Stopping all threads */
130 DBG("Terminating all threads");
131 pthread_cancel(client_thread);
132 pthread_cancel(apps_thread);
133 if (kconsumerd_pid != 0) {
134 pthread_cancel(kconsumerd_thread);
135 }
136
137 DBG("Unlinking all unix socket");
138 unlink(client_unix_sock_path);
139 unlink(apps_unix_sock_path);
140 unlink(kconsumerd_err_unix_sock_path);
141
142 DBG("Removing %s directory", LTTNG_RUNDIR);
143 ret = asprintf(&cmd, "rm -rf " LTTNG_RUNDIR);
144 if (ret < 0) {
145 ERR("asprintf failed. Something is really wrong!");
146 }
147
148 /* Remove lttng run directory */
149 ret = system(cmd);
150 if (ret < 0) {
151 ERR("Unable to clean " LTTNG_RUNDIR);
152 }
153
154 DBG("Cleaning up all session");
155 /* Cleanup ALL session */
156 cds_list_for_each_entry(sess, &ltt_session_list.head, list) {
157 teardown_kernel_session(sess);
158 // TODO complete session cleanup (including UST)
159 }
160
161 close(kernel_tracer_fd);
162 }
163
164 /*
165 * send_unix_sock
166 *
167 * Send data on a unix socket using the liblttsessiondcomm API.
168 *
169 * Return lttcomm error code.
170 */
171 static int send_unix_sock(int sock, void *buf, size_t len)
172 {
173 /* Check valid length */
174 if (len <= 0) {
175 return -1;
176 }
177
178 return lttcomm_send_unix_sock(sock, buf, len);
179 }
180
181 /*
182 * clean_command_ctx
183 *
184 * Free memory of a command context structure.
185 */
186 static void clean_command_ctx(struct command_ctx *cmd_ctx)
187 {
188 DBG("Clean command context structure %p", cmd_ctx);
189 if (cmd_ctx) {
190 if (cmd_ctx->llm) {
191 free(cmd_ctx->llm);
192 }
193 if (cmd_ctx->lsm) {
194 free(cmd_ctx->lsm);
195 }
196 free(cmd_ctx);
197 cmd_ctx = NULL;
198 }
199 }
200
201 /*
202 * ust_connect_app
203 *
204 * Return a socket connected to the libust communication socket
205 * of the application identified by the pid.
206 *
207 * If the pid is not found in the traceable list,
208 * return -1 to indicate error.
209 */
210 static int ust_connect_app(pid_t pid)
211 {
212 int sock;
213 struct ltt_traceable_app *lta;
214
215 DBG("Connect to application pid %d", pid);
216
217 lta = find_app_by_pid(pid);
218 if (lta == NULL) {
219 /* App not found */
220 DBG("Application pid %d not found", pid);
221 return -1;
222 }
223
224 sock = ustctl_connect_pid(lta->pid);
225 if (sock < 0) {
226 ERR("Fail connecting to the PID %d\n", pid);
227 }
228
229 return sock;
230 }
231
232 /*
233 * notify_apps
234 *
235 * Notify apps by writing 42 to a named pipe using name.
236 * Every applications waiting for a ltt-sessiond will be notified
237 * and re-register automatically to the session daemon.
238 *
239 * Return open or write error value.
240 */
241 static int notify_apps(const char *name)
242 {
243 int fd;
244 int ret = -1;
245
246 DBG("Notify the global application pipe");
247
248 /* Try opening the global pipe */
249 fd = open(name, O_WRONLY);
250 if (fd < 0) {
251 goto error;
252 }
253
254 /* Notify by writing on the pipe */
255 ret = write(fd, "42", 2);
256 if (ret < 0) {
257 perror("write");
258 }
259
260 error:
261 return ret;
262 }
263
264 /*
265 * setup_lttng_msg
266 *
267 * Setup the outgoing data buffer for the response (llm) by allocating the
268 * right amount of memory and copying the original information from the lsm
269 * structure.
270 *
271 * Return total size of the buffer pointed by buf.
272 */
273 static int setup_lttng_msg(struct command_ctx *cmd_ctx, size_t size)
274 {
275 int ret, buf_size, trace_name_size;
276
277 /*
278 * Check for the trace_name. If defined, it's part of the payload data of
279 * the llm structure.
280 */
281 trace_name_size = strlen(cmd_ctx->lsm->trace_name);
282 buf_size = trace_name_size + size;
283
284 cmd_ctx->llm = malloc(sizeof(struct lttcomm_lttng_msg) + buf_size);
285 if (cmd_ctx->llm == NULL) {
286 perror("malloc");
287 ret = -ENOMEM;
288 goto error;
289 }
290
291 /* Copy common data */
292 cmd_ctx->llm->cmd_type = cmd_ctx->lsm->cmd_type;
293 cmd_ctx->llm->pid = cmd_ctx->lsm->pid;
294 if (!uuid_is_null(cmd_ctx->lsm->session_uuid)) {
295 uuid_copy(cmd_ctx->llm->session_uuid, cmd_ctx->lsm->session_uuid);
296 }
297
298 cmd_ctx->llm->trace_name_offset = trace_name_size;
299 cmd_ctx->llm->data_size = size;
300 cmd_ctx->lttng_msg_size = sizeof(struct lttcomm_lttng_msg) + buf_size;
301
302 /* Copy trace name to the llm structure. Begining of the payload. */
303 memcpy(cmd_ctx->llm->payload, cmd_ctx->lsm->trace_name, trace_name_size);
304
305 return buf_size;
306
307 error:
308 return ret;
309 }
310
311 /*
312 * thread_manage_kconsumerd
313 *
314 * This thread manage the kconsumerd error sent
315 * back to the session daemon.
316 */
317 static void *thread_manage_kconsumerd(void *data)
318 {
319 int sock, ret;
320 enum lttcomm_return_code code;
321
322 DBG("[thread] Manage kconsumerd started");
323
324 ret = lttcomm_listen_unix_sock(kconsumerd_err_sock);
325 if (ret < 0) {
326 goto error;
327 }
328
329 sock = lttcomm_accept_unix_sock(kconsumerd_err_sock);
330 if (sock < 0) {
331 goto error;
332 }
333
334 ret = lttcomm_recv_unix_sock(sock, &code, sizeof(enum lttcomm_return_code));
335 if (ret <= 0) {
336 goto error;
337 }
338
339 if (code == KCONSUMERD_COMMAND_SOCK_READY) {
340 kconsumerd_cmd_sock = lttcomm_connect_unix_sock(kconsumerd_cmd_unix_sock_path);
341 if (kconsumerd_cmd_sock < 0) {
342 perror("kconsumerd connect");
343 goto error;
344 }
345 /* Signal condition to tell that the kconsumerd is ready */
346 sem_post(&kconsumerd_sem);
347 DBG("Kconsumerd command socket ready");
348 } else {
349 DBG("Kconsumerd error when waiting for SOCK_READY : %s",
350 lttcomm_get_readable_code(-code));
351 goto error;
352 }
353
354 while (1) {
355 /* Wait for any kconsumerd error */
356 ret = lttcomm_recv_unix_sock(sock, &code, sizeof(enum lttcomm_return_code));
357 if (ret <= 0) {
358 ERR("Kconsumerd closed the command socket");
359 goto error;
360 }
361
362 ERR("Kconsumerd return code : %s", lttcomm_get_readable_code(-code));
363 if (code != KCONSUMERD_POLL_HUP) {
364 goto error;
365 }
366 }
367
368 error:
369 kconsumerd_pid = 0;
370 DBG("Kconsumerd thread dying");
371 return NULL;
372 }
373
374 /*
375 * thread_manage_apps
376 *
377 * This thread manage the application socket communication
378 */
379 static void *thread_manage_apps(void *data)
380 {
381 int sock, ret;
382
383 /* TODO: Something more elegant is needed but fine for now */
384 /* FIXME: change all types to either uint8_t, uint32_t, uint64_t
385 * for 32-bit vs 64-bit compat processes. */
386 /* replicate in ust with version number */
387 struct {
388 int reg; /* 1:register, 0:unregister */
389 pid_t pid;
390 uid_t uid;
391 } reg_msg;
392
393 DBG("[thread] Manage apps started");
394
395 ret = lttcomm_listen_unix_sock(apps_sock);
396 if (ret < 0) {
397 goto error;
398 }
399
400 /* Notify all applications to register */
401 notify_apps(default_global_apps_pipe);
402
403 while (1) {
404 DBG("Accepting application registration");
405 /* Blocking call, waiting for transmission */
406 sock = lttcomm_accept_unix_sock(apps_sock);
407 if (sock < 0) {
408 goto error;
409 }
410
411 /* Basic recv here to handle the very simple data
412 * that the libust send to register (reg_msg).
413 */
414 ret = recv(sock, &reg_msg, sizeof(reg_msg), 0);
415 if (ret < 0) {
416 perror("recv");
417 continue;
418 }
419
420 /* Add application to the global traceable list */
421 if (reg_msg.reg == 1) {
422 /* Registering */
423 ret = register_traceable_app(reg_msg.pid, reg_msg.uid);
424 if (ret < 0) {
425 /* register_traceable_app only return an error with
426 * ENOMEM. At this point, we better stop everything.
427 */
428 goto error;
429 }
430 } else {
431 /* Unregistering */
432 unregister_traceable_app(reg_msg.pid);
433 }
434 }
435
436 error:
437
438 return NULL;
439 }
440
441 /*
442 * spawn_kconsumerd_thread
443 *
444 * Start the thread_manage_kconsumerd. This must be done after a kconsumerd
445 * exec or it will fails.
446 */
447 static int spawn_kconsumerd_thread(void)
448 {
449 int ret;
450
451 /* Setup semaphore */
452 sem_init(&kconsumerd_sem, 0, 0);
453
454 ret = pthread_create(&kconsumerd_thread, NULL, thread_manage_kconsumerd, (void *) NULL);
455 if (ret != 0) {
456 perror("pthread_create kconsumerd");
457 goto error;
458 }
459
460 /* Wait for the kconsumerd thread to be ready */
461 sem_wait(&kconsumerd_sem);
462
463 return 0;
464
465 error:
466 return ret;
467 }
468
469 /*
470 * spawn_kconsumerd
471 *
472 * Fork and exec a kernel consumer daemon (kconsumerd).
473 *
474 * NOTE: It is very important to fork a kconsumerd BEFORE opening any kernel
475 * file descriptor using the libkernelctl or kernel-ctl functions. So, a
476 * kernel consumer MUST only be spawned before creating a kernel session.
477 *
478 * Return pid if successful else -1.
479 */
480 static pid_t spawn_kconsumerd(void)
481 {
482 int ret;
483 pid_t pid;
484
485 pid = fork();
486 if (pid == 0) {
487 /*
488 * Exec kconsumerd.
489 */
490 execlp("kconsumerd", "kconsumerd", "--daemonize", NULL);
491 if (errno != 0) {
492 perror("kernel start consumer exec");
493 }
494 exit(EXIT_FAILURE);
495 } else if (pid > 0) {
496 ret = pid;
497 goto error;
498 } else {
499 perror("kernel start consumer fork");
500 ret = -errno;
501 goto error;
502 }
503
504 error:
505 return ret;
506 }
507
508 /*
509 * start_kconsumerd
510 *
511 * Spawn the kconsumerd daemon and session daemon thread.
512 */
513 static int start_kconsumerd(void)
514 {
515 int ret;
516
517 DBG("Spawning kconsumerd");
518
519 pthread_mutex_lock(&kconsumerd_pid_mutex);
520 if (kconsumerd_pid == 0) {
521 ret = spawn_kconsumerd();
522 if (ret < 0) {
523 ERR("Spawning kconsumerd failed");
524 ret = LTTCOMM_KERN_CONSUMER_FAIL;
525 pthread_mutex_unlock(&kconsumerd_pid_mutex);
526 goto error;
527 }
528
529 /* Setting up the global kconsumerd_pid */
530 kconsumerd_pid = ret;
531 }
532 pthread_mutex_unlock(&kconsumerd_pid_mutex);
533
534 DBG("Kconsumerd pid %d", ret);
535
536 DBG("Spawning kconsumerd thread");
537
538 ret = spawn_kconsumerd_thread();
539 if (ret < 0) {
540 ERR("Fatal error spawning kconsumerd thread");
541 ret = LTTCOMM_FATAL;
542 goto error;
543 }
544
545 return 0;
546
547 error:
548 return ret;
549 }
550
551 /*
552 * send_kconsumerd_fds
553 *
554 * Send all stream fds of the kernel session to the consumer.
555 */
556 static int send_kconsumerd_fds(int sock, struct ltt_kernel_session *session)
557 {
558 int ret, i = 0;
559 /* Plus one here for the metadata fd */
560 size_t nb_fd = session->stream_count_global + 1;
561 int fds[nb_fd];
562 struct ltt_kernel_stream *stream;
563 struct ltt_kernel_channel *chan;
564 struct lttcomm_kconsumerd_header lkh;
565 struct lttcomm_kconsumerd_msg buf[nb_fd];
566
567 /* Add metadata data */
568 fds[i] = session->metadata_stream_fd;
569 buf[i].fd = fds[i];
570 buf[i].state = ACTIVE_FD;
571 buf[i].max_sb_size = session->metadata->conf->subbuf_size;
572 strncpy(buf[i].path_name, session->metadata->pathname, PATH_MAX);
573
574 cds_list_for_each_entry(chan, &session->channel_list.head, list) {
575 cds_list_for_each_entry(stream, &chan->stream_list.head, list) {
576 i++;
577 fds[i] = stream->fd;
578 buf[i].fd = stream->fd;
579 buf[i].state = stream->state;
580 buf[i].max_sb_size = chan->channel->subbuf_size;
581 strncpy(buf[i].path_name, stream->pathname, PATH_MAX);
582 }
583 }
584
585 /* Setup header */
586 lkh.payload_size = nb_fd * sizeof(struct lttcomm_kconsumerd_msg);
587 lkh.cmd_type = LTTCOMM_ADD_STREAM;
588
589 DBG("Sending kconsumerd header");
590
591 ret = lttcomm_send_unix_sock(sock, &lkh, sizeof(struct lttcomm_kconsumerd_header));
592 if (ret < 0) {
593 perror("send kconsumerd header");
594 goto error;
595 }
596
597 DBG("Sending all fds to kconsumerd");
598
599 ret = lttcomm_send_fds_unix_sock(sock, buf, fds, nb_fd, lkh.payload_size);
600 if (ret < 0) {
601 perror("send kconsumerd fds");
602 goto error;
603 }
604
605 DBG("Kconsumerd fds sent");
606
607 return 0;
608
609 error:
610 return ret;
611 }
612
613 /*
614 * create_trace_dir
615 *
616 * Create the trace output directory.
617 */
618 static int create_trace_dir(struct ltt_kernel_session *session)
619 {
620 int ret;
621 struct ltt_kernel_channel *chan;
622
623 /* Create all channel directories */
624 cds_list_for_each_entry(chan, &session->channel_list.head, list) {
625 DBG("Creating trace directory at %s", chan->pathname);
626 ret = mkdir(chan->pathname, S_IRWXU | S_IRWXG );
627 if (ret < 0) {
628 perror("mkdir trace path");
629 ret = -errno;
630 goto error;
631 }
632 }
633
634 return 0;
635
636 error:
637 return ret;
638 }
639
640 /*
641 * process_client_msg
642 *
643 * Process the command requested by the lttng client within the command
644 * context structure. This function make sure that the return structure (llm)
645 * is set and ready for transmission before returning.
646 *
647 * Return any error encountered or 0 for success.
648 */
649 static int process_client_msg(struct command_ctx *cmd_ctx)
650 {
651 int ret;
652
653 DBG("Processing client command %d", cmd_ctx->lsm->cmd_type);
654
655 /* Check command that needs a session */
656 switch (cmd_ctx->lsm->cmd_type) {
657 case LTTNG_CREATE_SESSION:
658 case LTTNG_LIST_SESSIONS:
659 case UST_LIST_APPS:
660 break;
661 default:
662 cmd_ctx->session = find_session_by_uuid(cmd_ctx->lsm->session_uuid);
663 if (cmd_ctx->session == NULL) {
664 ret = LTTCOMM_SELECT_SESS;
665 goto error;
666 }
667 break;
668 }
669
670 /* Check command for kernel tracing */
671 switch (cmd_ctx->lsm->cmd_type) {
672 case KERNEL_CREATE_SESSION:
673 case KERNEL_CREATE_CHANNEL:
674 case KERNEL_CREATE_STREAM:
675 case KERNEL_DISABLE_EVENT:
676 case KERNEL_ENABLE_EVENT:
677 case KERNEL_OPEN_METADATA:
678 case KERNEL_START_TRACE:
679 case KERNEL_STOP_TRACE:
680 /* TODO: reconnect to kernel tracer to check if
681 * it's loadded */
682 if (kernel_tracer_fd == 0) {
683 ret = LTTCOMM_KERN_NA;
684 goto error;
685 }
686 break;
687 }
688
689 /* Connect to ust apps if available pid */
690 if (cmd_ctx->lsm->pid > 0) {
691 /* Connect to app using ustctl API */
692 cmd_ctx->ust_sock = ust_connect_app(cmd_ctx->lsm->pid);
693 if (cmd_ctx->ust_sock < 0) {
694 ret = LTTCOMM_NO_TRACEABLE;
695 goto error;
696 }
697 }
698
699 /* Process by command type */
700 switch (cmd_ctx->lsm->cmd_type) {
701 case KERNEL_CREATE_SESSION:
702 {
703 ret = setup_lttng_msg(cmd_ctx, 0);
704 if (ret < 0) {
705 goto setup_error;
706 }
707
708 ret = start_kconsumerd();
709 if (ret < 0) {
710 goto error;
711 }
712
713 DBG("Creating kernel session");
714
715 ret = kernel_create_session(cmd_ctx->session, kernel_tracer_fd);
716 if (ret < 0) {
717 ret = LTTCOMM_KERN_SESS_FAIL;
718 goto error;
719 }
720
721 ret = LTTCOMM_OK;
722 break;
723 }
724 case KERNEL_CREATE_CHANNEL:
725 {
726 ret = setup_lttng_msg(cmd_ctx, 0);
727 if (ret < 0) {
728 goto setup_error;
729 }
730
731 DBG("Creating kernel channel");
732
733 ret = kernel_create_channel(cmd_ctx->session->kernel_session);
734
735 if (ret < 0) {
736 ret = LTTCOMM_KERN_CHAN_FAIL;
737 goto error;
738 }
739
740 ret = LTTCOMM_OK;
741 break;
742 }
743 case KERNEL_ENABLE_EVENT:
744 {
745 /* Setup lttng message with no payload */
746 ret = setup_lttng_msg(cmd_ctx, 0);
747 if (ret < 0) {
748 goto setup_error;
749 }
750
751 DBG("Enabling kernel event %s", cmd_ctx->lsm->u.event.event_name);
752
753 ret = kernel_enable_event(cmd_ctx->session->kernel_session, cmd_ctx->lsm->u.event.event_name);
754 if (ret < 0) {
755 ret = LTTCOMM_KERN_ENABLE_FAIL;
756 goto error;
757 }
758
759 ret = LTTCOMM_OK;
760 break;
761 }
762 case KERNEL_OPEN_METADATA:
763 {
764 /* Setup lttng message with no payload */
765 ret = setup_lttng_msg(cmd_ctx, 0);
766 if (ret < 0) {
767 goto setup_error;
768 }
769
770 DBG("Open kernel metadata");
771
772 ret = kernel_open_metadata(cmd_ctx->session->kernel_session);
773 if (ret < 0) {
774 ret = LTTCOMM_KERN_META_FAIL;
775 goto error;
776 }
777
778 ret = LTTCOMM_OK;
779 break;
780 }
781 case KERNEL_CREATE_STREAM:
782 {
783 struct ltt_kernel_channel *chan;
784 /* Setup lttng message with no payload */
785 ret = setup_lttng_msg(cmd_ctx, 0);
786 if (ret < 0) {
787 goto setup_error;
788 }
789
790 DBG("Creating kernel stream");
791
792 ret = kernel_create_metadata_stream(cmd_ctx->session->kernel_session);
793 if (ret < 0) {
794 ERR("Kernel create metadata stream failed");
795 ret = LTTCOMM_KERN_STREAM_FAIL;
796 goto error;
797 }
798
799 /* For each channel */
800 cds_list_for_each_entry(chan, &cmd_ctx->session->kernel_session->channel_list.head, list) {
801 ret = kernel_create_channel_stream(chan);
802 if (ret < 0) {
803 ERR("Kernel create channel stream failed");
804 ret = LTTCOMM_KERN_STREAM_FAIL;
805 goto error;
806 }
807 /* Update the stream global counter */
808 cmd_ctx->session->kernel_session->stream_count_global += ret;
809 }
810
811 ret = LTTCOMM_OK;
812 break;
813 }
814 case KERNEL_START_TRACE:
815 {
816 /* Setup lttng message with no payload */
817 ret = setup_lttng_msg(cmd_ctx, 0);
818 if (ret < 0) {
819 goto setup_error;
820 }
821
822 DBG("Start kernel tracing");
823
824 ret = create_trace_dir(cmd_ctx->session->kernel_session);
825 if (ret < 0) {
826 if (ret == -EEXIST) {
827 ret = LTTCOMM_KERN_DIR_EXIST;
828 } else {
829 ret = LTTCOMM_KERN_DIR_FAIL;
830 goto error;
831 }
832 }
833
834 ret = kernel_start_session(cmd_ctx->session->kernel_session);
835 if (ret < 0) {
836 ERR("Kernel start session failed");
837 ret = LTTCOMM_KERN_START_FAIL;
838 goto error;
839 }
840
841 ret = send_kconsumerd_fds(kconsumerd_cmd_sock, cmd_ctx->session->kernel_session);
842 if (ret < 0) {
843 ERR("Send kconsumerd fds failed");
844 ret = LTTCOMM_KERN_CONSUMER_FAIL;
845 goto error;
846 }
847
848 ret = LTTCOMM_OK;
849 break;
850 }
851 case KERNEL_STOP_TRACE:
852 {
853 /* Setup lttng message with no payload */
854 ret = setup_lttng_msg(cmd_ctx, 0);
855 if (ret < 0) {
856 goto setup_error;
857 }
858
859 if (cmd_ctx->session->kernel_session == NULL) {
860 ret = LTTCOMM_KERN_NO_SESSION;
861 goto error;
862 }
863
864 DBG("Stop kernel tracing");
865
866 ret = kernel_stop_session(cmd_ctx->session->kernel_session);
867 if (ret < 0) {
868 ERR("Kernel stop session failed");
869 ret = LTTCOMM_KERN_STOP_FAIL;
870 goto error;
871 }
872
873 /* Clean kernel session teardown */
874 teardown_kernel_session(cmd_ctx->session);
875
876 ret = LTTCOMM_OK;
877 break;
878 }
879 case LTTNG_CREATE_SESSION:
880 {
881 /* Setup lttng message with no payload */
882 ret = setup_lttng_msg(cmd_ctx, 0);
883 if (ret < 0) {
884 goto setup_error;
885 }
886
887 ret = create_session(cmd_ctx->lsm->session_name, &cmd_ctx->llm->session_uuid);
888 if (ret < 0) {
889 if (ret == -1) {
890 ret = LTTCOMM_EXIST_SESS;
891 } else {
892 ret = LTTCOMM_FATAL;
893 }
894 goto error;
895 }
896
897 ret = LTTCOMM_OK;
898 break;
899 }
900 case LTTNG_DESTROY_SESSION:
901 {
902 /* Setup lttng message with no payload */
903 ret = setup_lttng_msg(cmd_ctx, 0);
904 if (ret < 0) {
905 goto setup_error;
906 }
907
908 ret = destroy_session(&cmd_ctx->lsm->session_uuid);
909 if (ret < 0) {
910 ret = LTTCOMM_NO_SESS;
911 goto error;
912 }
913
914 ret = LTTCOMM_OK;
915 break;
916 }
917 case LTTNG_LIST_TRACES:
918 {
919 unsigned int trace_count;
920
921 trace_count = get_trace_count_per_session(cmd_ctx->session);
922 if (trace_count == 0) {
923 ret = LTTCOMM_NO_TRACE;
924 goto error;
925 }
926
927 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_trace) * trace_count);
928 if (ret < 0) {
929 goto setup_error;
930 }
931
932 get_traces_per_session(cmd_ctx->session,
933 (struct lttng_trace *)(cmd_ctx->llm->payload));
934
935 ret = LTTCOMM_OK;
936 break;
937 }
938 case UST_CREATE_TRACE:
939 {
940 /* Setup lttng message with no payload */
941 ret = setup_lttng_msg(cmd_ctx, 0);
942 if (ret < 0) {
943 goto setup_error;
944 }
945
946 ret = ust_create_trace(cmd_ctx);
947 if (ret < 0) {
948 goto setup_error;
949 }
950 break;
951 }
952 case UST_LIST_APPS:
953 {
954 unsigned int app_count;
955
956 app_count = get_app_count();
957 DBG("Traceable application count : %d", app_count);
958 if (app_count == 0) {
959 ret = LTTCOMM_NO_APPS;
960 goto error;
961 }
962
963 ret = setup_lttng_msg(cmd_ctx, sizeof(pid_t) * app_count);
964 if (ret < 0) {
965 goto setup_error;
966 }
967
968 get_app_list_pids((pid_t *)(cmd_ctx->llm->payload));
969
970 ret = LTTCOMM_OK;
971 break;
972 }
973 case UST_START_TRACE:
974 {
975 /* Setup lttng message with no payload */
976 ret = setup_lttng_msg(cmd_ctx, 0);
977 if (ret < 0) {
978 goto setup_error;
979 }
980
981 ret = ust_start_trace(cmd_ctx);
982 if (ret < 0) {
983 goto setup_error;
984 }
985 break;
986 }
987 case UST_STOP_TRACE:
988 {
989 /* Setup lttng message with no payload */
990 ret = setup_lttng_msg(cmd_ctx, 0);
991 if (ret < 0) {
992 goto setup_error;
993 }
994
995 ret = ust_stop_trace(cmd_ctx);
996 if (ret < 0) {
997 goto setup_error;
998 }
999 break;
1000 }
1001 case LTTNG_LIST_SESSIONS:
1002 {
1003 unsigned int session_count;
1004
1005 session_count = get_session_count();
1006 if (session_count == 0) {
1007 ret = LTTCOMM_NO_SESS;
1008 goto error;
1009 }
1010
1011 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_session) * session_count);
1012 if (ret < 0) {
1013 goto setup_error;
1014 }
1015
1016 get_lttng_session((struct lttng_session *)(cmd_ctx->llm->payload));
1017
1018 ret = LTTCOMM_OK;
1019 break;
1020 }
1021 default:
1022 /* Undefined command */
1023 ret = setup_lttng_msg(cmd_ctx, 0);
1024 if (ret < 0) {
1025 goto setup_error;
1026 }
1027
1028 ret = LTTCOMM_UND;
1029 break;
1030 }
1031
1032 /* Set return code */
1033 cmd_ctx->llm->ret_code = ret;
1034
1035 return ret;
1036
1037 error:
1038 if (cmd_ctx->llm == NULL) {
1039 DBG("Missing llm structure. Allocating one.");
1040 if (setup_lttng_msg(cmd_ctx, 0) < 0) {
1041 goto setup_error;
1042 }
1043 }
1044 /* Notify client of error */
1045 cmd_ctx->llm->ret_code = ret;
1046
1047 setup_error:
1048 return ret;
1049 }
1050
1051 /*
1052 * thread_manage_clients
1053 *
1054 * This thread manage all clients request using the unix
1055 * client socket for communication.
1056 */
1057 static void *thread_manage_clients(void *data)
1058 {
1059 int sock, ret;
1060 struct command_ctx *cmd_ctx;
1061
1062 DBG("[thread] Manage client started");
1063
1064 ret = lttcomm_listen_unix_sock(client_sock);
1065 if (ret < 0) {
1066 goto error;
1067 }
1068
1069 /* Notify parent pid that we are ready
1070 * to accept command for client side.
1071 */
1072 if (opt_sig_parent) {
1073 kill(ppid, SIGCHLD);
1074 }
1075
1076 while (1) {
1077 /* Blocking call, waiting for transmission */
1078 DBG("Accepting client command ...");
1079 sock = lttcomm_accept_unix_sock(client_sock);
1080 if (sock < 0) {
1081 goto error;
1082 }
1083
1084 /* Allocate context command to process the client request */
1085 cmd_ctx = malloc(sizeof(struct command_ctx));
1086
1087 /* Allocate data buffer for reception */
1088 cmd_ctx->lsm = malloc(sizeof(struct lttcomm_session_msg));
1089 cmd_ctx->llm = NULL;
1090 cmd_ctx->session = NULL;
1091
1092 /*
1093 * Data is received from the lttng client. The struct
1094 * lttcomm_session_msg (lsm) contains the command and data request of
1095 * the client.
1096 */
1097 DBG("Receiving data from client ...");
1098 ret = lttcomm_recv_unix_sock(sock, cmd_ctx->lsm, sizeof(struct lttcomm_session_msg));
1099 if (ret <= 0) {
1100 continue;
1101 }
1102
1103 /*
1104 * This function dispatch the work to the kernel or userspace tracer
1105 * libs and fill the lttcomm_lttng_msg data structure of all the needed
1106 * informations for the client. The command context struct contains
1107 * everything this function may needs.
1108 */
1109 ret = process_client_msg(cmd_ctx);
1110 if (ret < 0) {
1111 /* TODO: Inform client somehow of the fatal error. At this point,
1112 * ret < 0 means that a malloc failed (ENOMEM). */
1113 /* Error detected but still accept command */
1114 clean_command_ctx(cmd_ctx);
1115 continue;
1116 }
1117
1118 DBG("Sending response (size: %d, retcode: %d)",
1119 cmd_ctx->lttng_msg_size, cmd_ctx->llm->ret_code);
1120 ret = send_unix_sock(sock, cmd_ctx->llm, cmd_ctx->lttng_msg_size);
1121 if (ret < 0) {
1122 ERR("Failed to send data back to client");
1123 }
1124
1125 clean_command_ctx(cmd_ctx);
1126 }
1127
1128 error:
1129 return NULL;
1130 }
1131
1132
1133 /*
1134 * usage function on stderr
1135 */
1136 static void usage(void)
1137 {
1138 fprintf(stderr, "Usage: %s OPTIONS\n\nOptions:\n", progname);
1139 fprintf(stderr, " -h, --help Display this usage.\n");
1140 fprintf(stderr, " -c, --client-sock PATH Specify path for the client unix socket\n");
1141 fprintf(stderr, " -a, --apps-sock PATH Specify path for apps unix socket\n");
1142 fprintf(stderr, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
1143 fprintf(stderr, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
1144 fprintf(stderr, " -d, --daemonize Start as a daemon.\n");
1145 fprintf(stderr, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
1146 fprintf(stderr, " -V, --version Show version number.\n");
1147 fprintf(stderr, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
1148 fprintf(stderr, " -q, --quiet No output at all.\n");
1149 fprintf(stderr, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
1150 }
1151
1152 /*
1153 * daemon argument parsing
1154 */
1155 static int parse_args(int argc, char **argv)
1156 {
1157 int c;
1158
1159 static struct option long_options[] = {
1160 { "client-sock", 1, 0, 'c' },
1161 { "apps-sock", 1, 0, 'a' },
1162 { "kconsumerd-cmd-sock", 1, 0, 0 },
1163 { "kconsumerd-err-sock", 1, 0, 0 },
1164 { "daemonize", 0, 0, 'd' },
1165 { "sig-parent", 0, 0, 'S' },
1166 { "help", 0, 0, 'h' },
1167 { "group", 1, 0, 'g' },
1168 { "version", 0, 0, 'V' },
1169 { "quiet", 0, 0, 'q' },
1170 { "verbose", 0, 0, 'v' },
1171 { NULL, 0, 0, 0 }
1172 };
1173
1174 while (1) {
1175 int option_index = 0;
1176 c = getopt_long(argc, argv, "dhqvVS" "a:c:g:s:E:C:", long_options, &option_index);
1177 if (c == -1) {
1178 break;
1179 }
1180
1181 switch (c) {
1182 case 0:
1183 fprintf(stderr, "option %s", long_options[option_index].name);
1184 if (optarg) {
1185 fprintf(stderr, " with arg %s\n", optarg);
1186 }
1187 break;
1188 case 'c':
1189 snprintf(client_unix_sock_path, PATH_MAX, "%s", optarg);
1190 break;
1191 case 'a':
1192 snprintf(apps_unix_sock_path, PATH_MAX, "%s", optarg);
1193 break;
1194 case 'd':
1195 opt_daemon = 1;
1196 break;
1197 case 'g':
1198 opt_tracing_group = strdup(optarg);
1199 break;
1200 case 'h':
1201 usage();
1202 exit(EXIT_FAILURE);
1203 case 'V':
1204 fprintf(stdout, "%s\n", VERSION);
1205 exit(EXIT_SUCCESS);
1206 case 'S':
1207 opt_sig_parent = 1;
1208 break;
1209 case 'E':
1210 snprintf(kconsumerd_err_unix_sock_path, PATH_MAX, "%s", optarg);
1211 break;
1212 case 'C':
1213 snprintf(kconsumerd_cmd_unix_sock_path, PATH_MAX, "%s", optarg);
1214 break;
1215 case 'q':
1216 opt_quiet = 1;
1217 break;
1218 case 'v':
1219 opt_verbose = 1;
1220 break;
1221 default:
1222 /* Unknown option or other error.
1223 * Error is printed by getopt, just return */
1224 return -1;
1225 }
1226 }
1227
1228 return 0;
1229 }
1230
1231 /*
1232 * init_daemon_socket
1233 *
1234 * Creates the two needed socket by the daemon.
1235 * apps_sock - The communication socket for all UST apps.
1236 * client_sock - The communication of the cli tool (lttng).
1237 */
1238 static int init_daemon_socket()
1239 {
1240 int ret = 0;
1241 mode_t old_umask;
1242
1243 old_umask = umask(0);
1244
1245 /* Create client tool unix socket */
1246 client_sock = lttcomm_create_unix_sock(client_unix_sock_path);
1247 if (client_sock < 0) {
1248 ERR("Create unix sock failed: %s", client_unix_sock_path);
1249 ret = -1;
1250 goto end;
1251 }
1252
1253 /* File permission MUST be 660 */
1254 ret = chmod(client_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
1255 if (ret < 0) {
1256 ERR("Set file permissions failed: %s", client_unix_sock_path);
1257 perror("chmod");
1258 goto end;
1259 }
1260
1261 /* Create the application unix socket */
1262 apps_sock = lttcomm_create_unix_sock(apps_unix_sock_path);
1263 if (apps_sock < 0) {
1264 ERR("Create unix sock failed: %s", apps_unix_sock_path);
1265 ret = -1;
1266 goto end;
1267 }
1268
1269 /* File permission MUST be 666 */
1270 ret = chmod(apps_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
1271 if (ret < 0) {
1272 ERR("Set file permissions failed: %s", apps_unix_sock_path);
1273 perror("chmod");
1274 goto end;
1275 }
1276
1277 end:
1278 umask(old_umask);
1279 return ret;
1280 }
1281
1282 /*
1283 * check_existing_daemon
1284 *
1285 * Check if the global socket is available.
1286 * If yes, error is returned.
1287 */
1288 static int check_existing_daemon()
1289 {
1290 int ret;
1291
1292 ret = access(client_unix_sock_path, F_OK);
1293 if (ret == 0) {
1294 ret = access(apps_unix_sock_path, F_OK);
1295 }
1296
1297 return ret;
1298 }
1299
1300 /*
1301 * get_home_dir
1302 *
1303 * Return pointer to home directory path using
1304 * the env variable HOME.
1305 *
1306 * Default : /tmp
1307 */
1308 static const char *get_home_dir(void)
1309 {
1310 const char *home_path;
1311
1312 if ((home_path = (const char *) getenv("HOME")) == NULL) {
1313 home_path = default_home_dir;
1314 }
1315
1316 return home_path;
1317 }
1318
1319 /*
1320 * set_permissions
1321 *
1322 * Set the tracing group gid onto the client socket.
1323 *
1324 * Race window between mkdir and chown is OK because we are going from
1325 * less permissive (root.root) to more permissive (root.tracing).
1326 */
1327 static int set_permissions(void)
1328 {
1329 int ret;
1330 struct group *grp;
1331
1332 /* Decide which group name to use */
1333 (opt_tracing_group != NULL) ?
1334 (grp = getgrnam(opt_tracing_group)) :
1335 (grp = getgrnam(default_tracing_group));
1336
1337 if (grp == NULL) {
1338 ERR("Missing tracing group. Aborting execution.\n");
1339 ret = -1;
1340 goto end;
1341 }
1342
1343 /* Set lttng run dir */
1344 ret = chown(LTTNG_RUNDIR, 0, grp->gr_gid);
1345 if (ret < 0) {
1346 ERR("Unable to set group on " LTTNG_RUNDIR);
1347 perror("chown");
1348 }
1349
1350 /* lttng client socket path */
1351 ret = chown(client_unix_sock_path, 0, grp->gr_gid);
1352 if (ret < 0) {
1353 ERR("Unable to set group on %s", client_unix_sock_path);
1354 perror("chown");
1355 }
1356
1357 /* kconsumerd error socket path */
1358 ret = chown(kconsumerd_err_unix_sock_path, 0, grp->gr_gid);
1359 if (ret < 0) {
1360 ERR("Unable to set group on %s", kconsumerd_err_unix_sock_path);
1361 perror("chown");
1362 }
1363
1364 DBG("All permissions are set");
1365
1366 end:
1367 return ret;
1368 }
1369
1370 /*
1371 * create_lttng_rundir
1372 *
1373 * Create the lttng run directory needed for all
1374 * global sockets and pipe.
1375 */
1376 static int create_lttng_rundir(void)
1377 {
1378 int ret;
1379
1380 ret = mkdir(LTTNG_RUNDIR, S_IRWXU | S_IRWXG );
1381 if (ret < 0) {
1382 ERR("Unable to create " LTTNG_RUNDIR);
1383 goto error;
1384 }
1385
1386 error:
1387 return ret;
1388 }
1389
1390 /*
1391 * init_kernel_tracer
1392 *
1393 * Setup necessary data for kernel tracer action.
1394 */
1395 static void init_kernel_tracer(void)
1396 {
1397 /* Set the global kernel tracer fd */
1398 kernel_tracer_fd = open(DEFAULT_KERNEL_TRACER_PATH, O_RDWR);
1399 if (kernel_tracer_fd < 0) {
1400 WARN("No kernel tracer available");
1401 kernel_tracer_fd = 0;
1402 }
1403
1404 DBG("Kernel tracer fd %d", kernel_tracer_fd);
1405 }
1406
1407 /*
1408 * set_kconsumerd_sockets
1409 *
1410 * Setup sockets and directory needed by the kconsumerd
1411 * communication with the session daemon.
1412 */
1413 static int set_kconsumerd_sockets(void)
1414 {
1415 int ret;
1416
1417 if (strlen(kconsumerd_err_unix_sock_path) == 0) {
1418 snprintf(kconsumerd_err_unix_sock_path, PATH_MAX, KCONSUMERD_ERR_SOCK_PATH);
1419 }
1420
1421 if (strlen(kconsumerd_cmd_unix_sock_path) == 0) {
1422 snprintf(kconsumerd_cmd_unix_sock_path, PATH_MAX, KCONSUMERD_CMD_SOCK_PATH);
1423 }
1424
1425 ret = mkdir(KCONSUMERD_PATH, S_IRWXU | S_IRWXG);
1426 if (ret < 0) {
1427 ERR("Failed to create " KCONSUMERD_PATH);
1428 goto error;
1429 }
1430
1431 /* Create the kconsumerd error unix socket */
1432 kconsumerd_err_sock = lttcomm_create_unix_sock(kconsumerd_err_unix_sock_path);
1433 if (kconsumerd_err_sock < 0) {
1434 ERR("Create unix sock failed: %s", kconsumerd_err_unix_sock_path);
1435 ret = -1;
1436 goto error;
1437 }
1438
1439 /* File permission MUST be 660 */
1440 ret = chmod(kconsumerd_err_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
1441 if (ret < 0) {
1442 ERR("Set file permissions failed: %s", kconsumerd_err_unix_sock_path);
1443 perror("chmod");
1444 goto error;
1445 }
1446
1447 error:
1448 return ret;
1449 }
1450
1451 /*
1452 * sighandler
1453 *
1454 * Signal handler for the daemon
1455 */
1456 static void sighandler(int sig)
1457 {
1458 switch (sig) {
1459 case SIGPIPE:
1460 DBG("SIGPIPE catched");
1461 return;
1462 case SIGINT:
1463 DBG("SIGINT catched");
1464 cleanup();
1465 break;
1466 case SIGTERM:
1467 DBG("SIGTERM catched");
1468 cleanup();
1469 break;
1470 default:
1471 break;
1472 }
1473
1474 exit(EXIT_SUCCESS);
1475 }
1476
1477 /*
1478 * set_signal_handler
1479 *
1480 * Setup signal handler for :
1481 * SIGINT, SIGTERM, SIGPIPE
1482 */
1483 static int set_signal_handler(void)
1484 {
1485 int ret = 0;
1486 struct sigaction sa;
1487 sigset_t sigset;
1488
1489 if ((ret = sigemptyset(&sigset)) < 0) {
1490 perror("sigemptyset");
1491 return ret;
1492 }
1493
1494 sa.sa_handler = sighandler;
1495 sa.sa_mask = sigset;
1496 sa.sa_flags = 0;
1497 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
1498 perror("sigaction");
1499 return ret;
1500 }
1501
1502 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
1503 perror("sigaction");
1504 return ret;
1505 }
1506
1507 if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) {
1508 perror("sigaction");
1509 return ret;
1510 }
1511
1512 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
1513
1514 return ret;
1515 }
1516
1517 /*
1518 * main
1519 */
1520 int main(int argc, char **argv)
1521 {
1522 int ret = 0;
1523 void *status;
1524
1525 /* Parse arguments */
1526 progname = argv[0];
1527 if ((ret = parse_args(argc, argv) < 0)) {
1528 goto error;
1529 }
1530
1531 /* Daemonize */
1532 if (opt_daemon) {
1533 ret = daemon(0, 0);
1534 if (ret < 0) {
1535 perror("daemon");
1536 goto error;
1537 }
1538 }
1539
1540 /* Check if daemon is UID = 0 */
1541 is_root = !getuid();
1542
1543 /* Set all sockets path */
1544 if (is_root) {
1545 ret = create_lttng_rundir();
1546 if (ret < 0) {
1547 goto error;
1548 }
1549
1550 if (strlen(apps_unix_sock_path) == 0) {
1551 snprintf(apps_unix_sock_path, PATH_MAX,
1552 DEFAULT_GLOBAL_APPS_UNIX_SOCK);
1553 }
1554
1555 if (strlen(client_unix_sock_path) == 0) {
1556 snprintf(client_unix_sock_path, PATH_MAX,
1557 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK);
1558 }
1559
1560 ret = set_kconsumerd_sockets();
1561 if (ret < 0) {
1562 goto error;
1563 }
1564
1565 /* Setup kernel tracer */
1566 init_kernel_tracer();
1567 } else {
1568 if (strlen(apps_unix_sock_path) == 0) {
1569 snprintf(apps_unix_sock_path, PATH_MAX,
1570 DEFAULT_HOME_APPS_UNIX_SOCK, get_home_dir());
1571 }
1572
1573 /* Set the cli tool unix socket path */
1574 if (strlen(client_unix_sock_path) == 0) {
1575 snprintf(client_unix_sock_path, PATH_MAX,
1576 DEFAULT_HOME_CLIENT_UNIX_SOCK, get_home_dir());
1577 }
1578 }
1579
1580 DBG("Client socket path %s", client_unix_sock_path);
1581 DBG("Application socket path %s", apps_unix_sock_path);
1582
1583 /* See if daemon already exist. If any of the two
1584 * socket needed by the daemon are present, this test fails
1585 */
1586 if ((ret = check_existing_daemon()) == 0) {
1587 ERR("Already running daemon.\n");
1588 /* We do not goto error because we must not
1589 * cleanup() because a daemon is already running.
1590 */
1591 exit(EXIT_FAILURE);
1592 }
1593
1594 if (set_signal_handler() < 0) {
1595 goto error;
1596 }
1597
1598 /* Setup the needed unix socket */
1599 if (init_daemon_socket() < 0) {
1600 goto error;
1601 }
1602
1603 /* Set credentials to socket */
1604 if (is_root && (set_permissions() < 0)) {
1605 goto error;
1606 }
1607
1608 /* Get parent pid if -S, --sig-parent is specified. */
1609 if (opt_sig_parent) {
1610 ppid = getppid();
1611 }
1612
1613 while (1) {
1614 /* Create thread to manage the client socket */
1615 ret = pthread_create(&client_thread, NULL, thread_manage_clients, (void *) NULL);
1616 if (ret != 0) {
1617 perror("pthread_create");
1618 goto error;
1619 }
1620
1621 /* Create thread to manage application socket */
1622 ret = pthread_create(&apps_thread, NULL, thread_manage_apps, (void *) NULL);
1623 if (ret != 0) {
1624 perror("pthread_create");
1625 goto error;
1626 }
1627
1628 ret = pthread_join(client_thread, &status);
1629 if (ret != 0) {
1630 perror("pthread_join");
1631 goto error;
1632 }
1633 }
1634
1635 cleanup();
1636 exit(EXIT_SUCCESS);
1637
1638 error:
1639 cleanup();
1640 exit(EXIT_FAILURE);
1641 }
This page took 0.11827 seconds and 3 git commands to generate.