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