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