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