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