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