Cleanup comments and bad indent
[lttng-tools.git] / ltt-sessiond / main.c
CommitLineData
826d496d
MD
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
996b65c8 3 * Copyright (C) 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
fac6795d
DG
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
82a3637f
DG
7 * as published by the Free Software Foundation; only version 2
8 * of the License.
91d76f53 9 *
fac6795d
DG
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
91d76f53 14 *
fac6795d
DG
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
fac6795d
DG
18 */
19
20#define _GNU_SOURCE
21#include <fcntl.h>
22#include <getopt.h>
23#include <grp.h>
24#include <limits.h>
7a485870 25#include <poll.h>
fac6795d 26#include <pthread.h>
8c0faa1d 27#include <semaphore.h>
fac6795d
DG
28#include <signal.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32#include <sys/ipc.h>
b73401da 33#include <sys/mount.h>
fac6795d
DG
34#include <sys/shm.h>
35#include <sys/socket.h>
36#include <sys/stat.h>
37#include <sys/types.h>
f3ed775e
DG
38#include <sys/time.h>
39#include <sys/resource.h>
fac6795d
DG
40#include <unistd.h>
41
42#include <urcu/list.h> /* URCU list library (-lurcu) */
5b97ec60 43#include <lttng/lttng.h>
6533b585 44#include <lttng/lttng-kconsumerd.h>
e88129fc 45#include <lttng-sessiond-comm.h>
fac6795d 46
b579acd9 47#include "context.h"
fac6795d 48#include "ltt-sessiond.h"
75462a81 49#include "lttngerr.h"
20fe2104 50#include "kernel-ctl.h"
9e78d6ae 51#include "ust-ctl.h"
5b74c7b1 52#include "session.h"
91d76f53 53#include "traceable-app.h"
6533b585 54#include "ltt-kconsumerd.h"
8e68d1c8 55#include "utils.h"
fac6795d 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 64int opt_verbose; /* Not static for lttngerr.h */
31f73cc9 65int opt_verbose_kconsumerd; /* Not static for lttngerr.h */
1d4b027a 66int opt_quiet; /* Not static for lttngerr.h */
d063d709 67
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;
7a485870 75static struct pollfd *kernel_pollfd;
fac6795d 76
d6f42150
DG
77static char apps_unix_sock_path[PATH_MAX]; /* Global application Unix socket path */
78static char client_unix_sock_path[PATH_MAX]; /* Global client Unix socket path */
79static char kconsumerd_err_unix_sock_path[PATH_MAX]; /* kconsumerd error Unix socket path */
80static char kconsumerd_cmd_unix_sock_path[PATH_MAX]; /* kconsumerd command Unix socket path */
fac6795d 81
1d4b027a 82/* Sockets and FDs */
d6f42150
DG
83static int client_sock;
84static int apps_sock;
85static int kconsumerd_err_sock;
8c0faa1d 86static int kconsumerd_cmd_sock;
20fe2104 87static int kernel_tracer_fd;
7a485870 88static int kernel_poll_pipe[2];
1d4b027a 89
273ea72c
DG
90/*
91 * Quit pipe for all threads. This permits a single cancellation point
92 * for all threads when receiving an event on the pipe.
93 */
94static int thread_quit_pipe[2];
95
1d4b027a 96/* Pthread, Mutexes and Semaphores */
8c0faa1d 97static pthread_t kconsumerd_thread;
1d4b027a
DG
98static pthread_t apps_thread;
99static pthread_t client_thread;
7a485870 100static pthread_t kernel_thread;
8c0faa1d
DG
101static sem_t kconsumerd_sem;
102
1d4b027a 103static pthread_mutex_t kconsumerd_pid_mutex; /* Mutex to control kconsumerd pid assignation */
fac6795d 104
ab147185
MD
105static int modprobe_remove_kernel_modules(void);
106
b5541356
DG
107/*
108 * Pointer initialized before thread creation.
109 *
110 * This points to the tracing session list containing the session count and a
111 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
112 * MUST NOT be taken if you call a public function in session.c.
04ea676f 113 *
d063d709
DG
114 * The lock is nested inside the structure: session_list_ptr->lock. Please use
115 * lock_session_list and unlock_session_list for lock acquisition.
b5541356
DG
116 */
117static struct ltt_session_list *session_list_ptr;
118
996b65c8
MD
119static gid_t allowed_group(void)
120{
121 struct group *grp;
122
274e143b
MD
123 if (opt_tracing_group) {
124 grp = getgrnam(opt_tracing_group);
125 } else {
126 grp = getgrnam(default_tracing_group);
127 }
996b65c8
MD
128 if (!grp) {
129 return -1;
130 } else {
131 return grp->gr_gid;
132 }
133}
134
273ea72c
DG
135/*
136 * Init quit pipe.
137 *
138 * Return -1 on error or 0 if all pipes are created.
139 */
140static int init_thread_quit_pipe(void)
141{
142 int ret;
143
144 ret = pipe2(thread_quit_pipe, O_CLOEXEC);
145 if (ret < 0) {
146 perror("thread quit pipe");
147 goto error;
148 }
149
150error:
151 return ret;
152}
153
fac6795d 154/*
d063d709
DG
155 * Complete teardown of a kernel session. This free all data structure related
156 * to a kernel session and update counter.
fac6795d 157 */
1d4b027a 158static void teardown_kernel_session(struct ltt_session *session)
fac6795d 159{
1d4b027a
DG
160 if (session->kernel_session != NULL) {
161 DBG("Tearing down kernel session");
d9800920
DG
162
163 /*
164 * If a custom kernel consumer was registered, close the socket before
165 * tearing down the complete kernel session structure
166 */
167 if (session->kernel_session->consumer_fd != kconsumerd_cmd_sock) {
168 lttcomm_close_unix_sock(session->kernel_session->consumer_fd);
169 }
170
c363b55d 171 trace_destroy_kernel_session(session->kernel_session);
1d4b027a
DG
172 /* Extra precaution */
173 session->kernel_session = NULL;
fac6795d 174 }
fac6795d
DG
175}
176
cf3af59e
MD
177static void stop_threads(void)
178{
179 /* Stopping all threads */
180 DBG("Terminating all threads");
181 close(thread_quit_pipe[0]);
182 close(thread_quit_pipe[1]);
183}
184
fac6795d 185/*
d063d709 186 * Cleanup the daemon
fac6795d 187 */
cf3af59e 188static void cleanup(void)
fac6795d 189{
1d4b027a
DG
190 int ret;
191 char *cmd;
af9737e9 192 struct ltt_session *sess, *stmp;
fac6795d 193
1d4b027a 194 DBG("Cleaning up");
e07ae692 195
1d4b027a 196 /* <fun> */
273ea72c
DG
197 MSG("\n%c[%d;%dm*** assert failed *** ==> %c[%dm%c[%d;%dm"
198 "Matthew, BEET driven development works!%c[%dm",
199 27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
1d4b027a 200 /* </fun> */
fac6795d 201
1d4b027a
DG
202 DBG("Removing %s directory", LTTNG_RUNDIR);
203 ret = asprintf(&cmd, "rm -rf " LTTNG_RUNDIR);
204 if (ret < 0) {
205 ERR("asprintf failed. Something is really wrong!");
206 }
5461b305 207
1d4b027a
DG
208 /* Remove lttng run directory */
209 ret = system(cmd);
210 if (ret < 0) {
211 ERR("Unable to clean " LTTNG_RUNDIR);
212 }
5461b305 213
1d4b027a 214 DBG("Cleaning up all session");
fac6795d 215
b5541356 216 /* Destroy session list mutex */
273ea72c
DG
217 if (session_list_ptr != NULL) {
218 pthread_mutex_destroy(&session_list_ptr->lock);
219
220 /* Cleanup ALL session */
af9737e9 221 cds_list_for_each_entry_safe(sess, stmp, &session_list_ptr->head, list) {
273ea72c
DG
222 teardown_kernel_session(sess);
223 // TODO complete session cleanup (including UST)
224 }
225 }
226
227 pthread_mutex_destroy(&kconsumerd_pid_mutex);
b5541356 228
f3ed775e 229 DBG("Closing kernel fd");
1d4b027a 230 close(kernel_tracer_fd);
ab147185
MD
231
232 DBG("Unloading kernel modules");
233 modprobe_remove_kernel_modules();
fac6795d
DG
234}
235
e065084a 236/*
d063d709 237 * Send data on a unix socket using the liblttsessiondcomm API.
e065084a 238 *
d063d709 239 * Return lttcomm error code.
e065084a
DG
240 */
241static int send_unix_sock(int sock, void *buf, size_t len)
242{
243 /* Check valid length */
244 if (len <= 0) {
245 return -1;
246 }
247
248 return lttcomm_send_unix_sock(sock, buf, len);
249}
250
5461b305 251/*
d063d709 252 * Free memory of a command context structure.
5461b305 253 */
a2fb29a5 254static void clean_command_ctx(struct command_ctx **cmd_ctx)
5461b305 255{
a2fb29a5
DG
256 DBG("Clean command context structure");
257 if (*cmd_ctx) {
258 if ((*cmd_ctx)->llm) {
259 free((*cmd_ctx)->llm);
5461b305 260 }
a2fb29a5
DG
261 if ((*cmd_ctx)->lsm) {
262 free((*cmd_ctx)->lsm);
5461b305 263 }
a2fb29a5
DG
264 free(*cmd_ctx);
265 *cmd_ctx = NULL;
5461b305
DG
266 }
267}
268
f3ed775e 269/*
d063d709 270 * Send all stream fds of kernel channel to the consumer.
f3ed775e 271 */
7a485870 272static int send_kconsumerd_channel_fds(int sock, struct ltt_kernel_channel *channel)
f3ed775e
DG
273{
274 int ret;
275 size_t nb_fd;
276 struct ltt_kernel_stream *stream;
f3ed775e
DG
277 struct lttcomm_kconsumerd_header lkh;
278 struct lttcomm_kconsumerd_msg lkm;
279
7a485870
DG
280 DBG("Sending fds of channel %s to kernel consumer", channel->channel->name);
281
282 nb_fd = channel->stream_count;
f3ed775e
DG
283
284 /* Setup header */
7a485870 285 lkh.payload_size = nb_fd * sizeof(struct lttcomm_kconsumerd_msg);
f3ed775e
DG
286 lkh.cmd_type = ADD_STREAM;
287
288 DBG("Sending kconsumerd header");
289
290 ret = lttcomm_send_unix_sock(sock, &lkh, sizeof(struct lttcomm_kconsumerd_header));
291 if (ret < 0) {
292 perror("send kconsumerd header");
293 goto error;
294 }
295
7a485870
DG
296 cds_list_for_each_entry(stream, &channel->stream_list.head, list) {
297 if (stream->fd != 0) {
f3ed775e
DG
298 lkm.fd = stream->fd;
299 lkm.state = stream->state;
7a485870 300 lkm.max_sb_size = channel->channel->attr.subbuf_size;
e8b60857 301 lkm.output = channel->channel->attr.output;
f3ed775e 302 strncpy(lkm.path_name, stream->pathname, PATH_MAX);
99497cd0 303 lkm.path_name[PATH_MAX - 1] = '\0';
f3ed775e
DG
304
305 DBG("Sending fd %d to kconsumerd", lkm.fd);
306
307 ret = lttcomm_send_fds_unix_sock(sock, &lkm, &lkm.fd, 1, sizeof(lkm));
308 if (ret < 0) {
309 perror("send kconsumerd fd");
310 goto error;
311 }
312 }
313 }
314
7a485870 315 DBG("Kconsumerd channel fds sent");
f3ed775e
DG
316
317 return 0;
318
319error:
320 return ret;
321}
322
323/*
d063d709 324 * Send all stream fds of the kernel session to the consumer.
f3ed775e 325 */
d9800920 326static int send_kconsumerd_fds(struct ltt_kernel_session *session)
f3ed775e
DG
327{
328 int ret;
329 struct ltt_kernel_channel *chan;
7a485870
DG
330 struct lttcomm_kconsumerd_header lkh;
331 struct lttcomm_kconsumerd_msg lkm;
332
333 /* Setup header */
334 lkh.payload_size = sizeof(struct lttcomm_kconsumerd_msg);
335 lkh.cmd_type = ADD_STREAM;
336
337 DBG("Sending kconsumerd header for metadata");
338
d9800920 339 ret = lttcomm_send_unix_sock(session->consumer_fd, &lkh, sizeof(struct lttcomm_kconsumerd_header));
7a485870
DG
340 if (ret < 0) {
341 perror("send kconsumerd header");
342 goto error;
343 }
344
345 DBG("Sending metadata stream fd");
346
d9800920
DG
347 /* Extra protection. It's NOT suppose to be set to 0 at this point */
348 if (session->consumer_fd == 0) {
349 session->consumer_fd = kconsumerd_cmd_sock;
350 }
351
7a485870
DG
352 if (session->metadata_stream_fd != 0) {
353 /* Send metadata stream fd first */
354 lkm.fd = session->metadata_stream_fd;
355 lkm.state = ACTIVE_FD;
356 lkm.max_sb_size = session->metadata->conf->attr.subbuf_size;
8b270bdb 357 lkm.output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
7a485870 358 strncpy(lkm.path_name, session->metadata->pathname, PATH_MAX);
99497cd0 359 lkm.path_name[PATH_MAX - 1] = '\0';
7a485870 360
d9800920 361 ret = lttcomm_send_fds_unix_sock(session->consumer_fd, &lkm, &lkm.fd, 1, sizeof(lkm));
7a485870
DG
362 if (ret < 0) {
363 perror("send kconsumerd fd");
364 goto error;
365 }
366 }
f3ed775e 367
f3ed775e 368 cds_list_for_each_entry(chan, &session->channel_list.head, list) {
d9800920 369 ret = send_kconsumerd_channel_fds(session->consumer_fd, chan);
f3ed775e 370 if (ret < 0) {
7a485870 371 goto error;
f3ed775e
DG
372 }
373 }
374
7a485870
DG
375 DBG("Kconsumerd fds (metadata and channel streams) sent");
376
f3ed775e
DG
377 return 0;
378
379error:
380 return ret;
381}
382
97b1a726 383#ifdef DISABLED
fac6795d 384/*
d063d709
DG
385 * Return a socket connected to the libust communication socket of the
386 * application identified by the pid.
fac6795d 387 *
d063d709 388 * If the pid is not found in the traceable list, return -1 to indicate error.
fac6795d 389 */
471d1693 390static int ust_connect_app(pid_t pid)
fac6795d 391{
91d76f53
DG
392 int sock;
393 struct ltt_traceable_app *lta;
379473d2 394
e07ae692
DG
395 DBG("Connect to application pid %d", pid);
396
91d76f53
DG
397 lta = find_app_by_pid(pid);
398 if (lta == NULL) {
399 /* App not found */
7442b2ba 400 DBG("Application pid %d not found", pid);
379473d2
DG
401 return -1;
402 }
fac6795d 403
91d76f53 404 sock = ustctl_connect_pid(lta->pid);
fac6795d 405 if (sock < 0) {
62d3069f 406 ERR("Fail connecting to the PID %d", pid);
fac6795d
DG
407 }
408
409 return sock;
410}
97b1a726 411#endif /* DISABLED */
fac6795d
DG
412
413/*
d063d709
DG
414 * Notify apps by writing 42 to a named pipe using name. Every applications
415 * waiting for a ltt-sessiond will be notified and re-register automatically to
416 * the session daemon.
fac6795d 417 *
d063d709 418 * Return open or write error value.
fac6795d
DG
419 */
420static int notify_apps(const char *name)
421{
422 int fd;
423 int ret = -1;
424
e07ae692
DG
425 DBG("Notify the global application pipe");
426
fac6795d
DG
427 /* Try opening the global pipe */
428 fd = open(name, O_WRONLY);
429 if (fd < 0) {
430 goto error;
431 }
432
433 /* Notify by writing on the pipe */
434 ret = write(fd, "42", 2);
435 if (ret < 0) {
436 perror("write");
437 }
438
439error:
440 return ret;
441}
442
e065084a 443/*
d063d709
DG
444 * Setup the outgoing data buffer for the response (llm) by allocating the
445 * right amount of memory and copying the original information from the lsm
446 * structure.
ca95a216 447 *
d063d709 448 * Return total size of the buffer pointed by buf.
ca95a216 449 */
5461b305 450static int setup_lttng_msg(struct command_ctx *cmd_ctx, size_t size)
ca95a216 451{
f3ed775e 452 int ret, buf_size;
ca95a216 453
f3ed775e 454 buf_size = size;
5461b305
DG
455
456 cmd_ctx->llm = malloc(sizeof(struct lttcomm_lttng_msg) + buf_size);
457 if (cmd_ctx->llm == NULL) {
ca95a216 458 perror("malloc");
5461b305 459 ret = -ENOMEM;
ca95a216
DG
460 goto error;
461 }
462
5461b305
DG
463 /* Copy common data */
464 cmd_ctx->llm->cmd_type = cmd_ctx->lsm->cmd_type;
9f19cc17 465 cmd_ctx->llm->pid = cmd_ctx->lsm->domain.attr.pid;
5461b305 466
5461b305
DG
467 cmd_ctx->llm->data_size = size;
468 cmd_ctx->lttng_msg_size = sizeof(struct lttcomm_lttng_msg) + buf_size;
469
ca95a216
DG
470 return buf_size;
471
472error:
473 return ret;
474}
475
7a485870 476/*
d063d709
DG
477 * Update the kernel pollfd set of all channel fd available over all tracing
478 * session. Add the wakeup pipe at the end of the set.
7a485870
DG
479 */
480static int update_kernel_pollfd(void)
481{
482 int i = 0;
273ea72c
DG
483 /*
484 * The wakup pipe and the quit pipe are needed so the number of fds starts
485 * at 2 for those pipes.
486 */
487 unsigned int nb_fd = 2;
7a485870
DG
488 struct ltt_session *session;
489 struct ltt_kernel_channel *channel;
490
491 DBG("Updating kernel_pollfd");
492
493 /* Get the number of channel of all kernel session */
6c9cc2ab 494 lock_session_list();
b5541356
DG
495 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
496 lock_session(session);
7a485870 497 if (session->kernel_session == NULL) {
b5541356 498 unlock_session(session);
7a485870
DG
499 continue;
500 }
501 nb_fd += session->kernel_session->channel_count;
b5541356 502 unlock_session(session);
7a485870
DG
503 }
504
505 DBG("Resizing kernel_pollfd to size %d", nb_fd);
506
507 kernel_pollfd = realloc(kernel_pollfd, nb_fd * sizeof(struct pollfd));
508 if (kernel_pollfd == NULL) {
509 perror("malloc kernel_pollfd");
510 goto error;
511 }
512
b5541356
DG
513 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
514 lock_session(session);
7a485870 515 if (session->kernel_session == NULL) {
b5541356 516 unlock_session(session);
7a485870
DG
517 continue;
518 }
519 if (i >= nb_fd) {
520 ERR("To much channel for kernel_pollfd size");
b5541356 521 unlock_session(session);
7a485870
DG
522 break;
523 }
524 cds_list_for_each_entry(channel, &session->kernel_session->channel_list.head, list) {
525 kernel_pollfd[i].fd = channel->fd;
526 kernel_pollfd[i].events = POLLIN | POLLRDNORM;
527 i++;
528 }
b5541356 529 unlock_session(session);
7a485870 530 }
6c9cc2ab 531 unlock_session_list();
7a485870
DG
532
533 /* Adding wake up pipe */
273ea72c
DG
534 kernel_pollfd[nb_fd - 2].fd = kernel_poll_pipe[0];
535 kernel_pollfd[nb_fd - 2].events = POLLIN;
536
537 /* Adding the quit pipe */
538 kernel_pollfd[nb_fd - 1].fd = thread_quit_pipe[0];
7a485870
DG
539
540 return nb_fd;
541
542error:
6c9cc2ab 543 unlock_session_list();
7a485870
DG
544 return -1;
545}
546
547/*
d063d709
DG
548 * Find the channel fd from 'fd' over all tracing session. When found, check
549 * for new channel stream and send those stream fds to the kernel consumer.
7a485870 550 *
d063d709 551 * Useful for CPU hotplug feature.
7a485870
DG
552 */
553static int update_kernel_stream(int fd)
554{
555 int ret = 0;
556 struct ltt_session *session;
557 struct ltt_kernel_channel *channel;
558
559 DBG("Updating kernel streams for channel fd %d", fd);
560
6c9cc2ab 561 lock_session_list();
b5541356
DG
562 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
563 lock_session(session);
7a485870 564 if (session->kernel_session == NULL) {
b5541356 565 unlock_session(session);
7a485870
DG
566 continue;
567 }
d9800920
DG
568
569 /* This is not suppose to be 0 but this is an extra security check */
570 if (session->kernel_session->consumer_fd == 0) {
571 session->kernel_session->consumer_fd = kconsumerd_cmd_sock;
572 }
573
7a485870
DG
574 cds_list_for_each_entry(channel, &session->kernel_session->channel_list.head, list) {
575 if (channel->fd == fd) {
576 DBG("Channel found, updating kernel streams");
577 ret = kernel_open_channel_stream(channel);
578 if (ret < 0) {
579 goto end;
580 }
d9800920 581
7a485870
DG
582 /*
583 * Have we already sent fds to the consumer? If yes, it means that
584 * tracing is started so it is safe to send our updated stream fds.
585 */
586 if (session->kernel_session->kconsumer_fds_sent == 1) {
d9800920
DG
587 ret = send_kconsumerd_channel_fds(session->kernel_session->consumer_fd,
588 channel);
7a485870
DG
589 if (ret < 0) {
590 goto end;
591 }
592 }
593 goto end;
594 }
595 }
b5541356 596 unlock_session(session);
7a485870
DG
597 }
598
599end:
6c9cc2ab 600 unlock_session_list();
b5541356
DG
601 if (session) {
602 unlock_session(session);
603 }
7a485870
DG
604 return ret;
605}
606
607/*
d063d709 608 * This thread manage event coming from the kernel.
7a485870 609 *
d063d709
DG
610 * Features supported in this thread:
611 * -) CPU Hotplug
7a485870
DG
612 */
613static void *thread_manage_kernel(void *data)
614{
615 int ret, i, nb_fd = 0;
616 char tmp;
617 int update_poll_flag = 1;
618
619 DBG("Thread manage kernel started");
620
621 while (1) {
622 if (update_poll_flag == 1) {
623 nb_fd = update_kernel_pollfd();
624 if (nb_fd < 0) {
625 goto error;
626 }
627 update_poll_flag = 0;
628 }
629
630 DBG("Polling on %d fds", nb_fd);
631
632 /* Poll infinite value of time */
633 ret = poll(kernel_pollfd, nb_fd, -1);
634 if (ret < 0) {
635 perror("poll kernel thread");
636 goto error;
637 } else if (ret == 0) {
638 /* Should not happen since timeout is infinite */
639 continue;
640 }
641
273ea72c
DG
642 /* Thread quit pipe has been closed. Killing thread. */
643 if (kernel_pollfd[nb_fd - 1].revents == POLLNVAL) {
644 goto error;
645 }
646
7a485870
DG
647 DBG("Kernel poll event triggered");
648
649 /*
650 * Check if the wake up pipe was triggered. If so, the kernel_pollfd
651 * must be updated.
652 */
273ea72c 653 switch (kernel_pollfd[nb_fd - 2].revents) {
b5541356 654 case POLLIN:
7a485870
DG
655 ret = read(kernel_poll_pipe[0], &tmp, 1);
656 update_poll_flag = 1;
657 continue;
b5541356
DG
658 case POLLERR:
659 goto error;
660 default:
661 break;
7a485870
DG
662 }
663
664 for (i = 0; i < nb_fd; i++) {
665 switch (kernel_pollfd[i].revents) {
666 /*
667 * New CPU detected by the kernel. Adding kernel stream to kernel
668 * session and updating the kernel consumer
669 */
670 case POLLIN | POLLRDNORM:
671 ret = update_kernel_stream(kernel_pollfd[i].fd);
672 if (ret < 0) {
673 continue;
674 }
675 break;
676 }
677 }
678 }
679
680error:
681 DBG("Kernel thread dying");
682 if (kernel_pollfd) {
683 free(kernel_pollfd);
684 }
273ea72c
DG
685
686 close(kernel_poll_pipe[0]);
687 close(kernel_poll_pipe[1]);
7a485870
DG
688 return NULL;
689}
690
1d4b027a 691/*
d063d709 692 * This thread manage the kconsumerd error sent back to the session daemon.
1d4b027a
DG
693 */
694static void *thread_manage_kconsumerd(void *data)
695{
273ea72c 696 int sock = 0, ret;
1d4b027a 697 enum lttcomm_return_code code;
273ea72c 698 struct pollfd pollfd[2];
1d4b027a
DG
699
700 DBG("[thread] Manage kconsumerd started");
701
702 ret = lttcomm_listen_unix_sock(kconsumerd_err_sock);
703 if (ret < 0) {
704 goto error;
705 }
706
273ea72c
DG
707 /* First fd is always the quit pipe */
708 pollfd[0].fd = thread_quit_pipe[0];
709
710 /* Apps socket */
711 pollfd[1].fd = kconsumerd_err_sock;
712 pollfd[1].events = POLLIN;
713
714 /* Inifinite blocking call, waiting for transmission */
715 ret = poll(pollfd, 2, -1);
716 if (ret < 0) {
717 perror("poll kconsumerd thread");
718 goto error;
719 }
720
721 /* Thread quit pipe has been closed. Killing thread. */
722 if (pollfd[0].revents == POLLNVAL) {
723 goto error;
724 } else if (pollfd[1].revents == POLLERR) {
725 ERR("Kconsumerd err socket poll error");
726 goto error;
727 }
728
1d4b027a
DG
729 sock = lttcomm_accept_unix_sock(kconsumerd_err_sock);
730 if (sock < 0) {
731 goto error;
732 }
733
712ea556 734 /* Getting status code from kconsumerd */
1d4b027a
DG
735 ret = lttcomm_recv_unix_sock(sock, &code, sizeof(enum lttcomm_return_code));
736 if (ret <= 0) {
737 goto error;
738 }
739
740 if (code == KCONSUMERD_COMMAND_SOCK_READY) {
741 kconsumerd_cmd_sock = lttcomm_connect_unix_sock(kconsumerd_cmd_unix_sock_path);
742 if (kconsumerd_cmd_sock < 0) {
712ea556 743 sem_post(&kconsumerd_sem);
1d4b027a
DG
744 perror("kconsumerd connect");
745 goto error;
746 }
747 /* Signal condition to tell that the kconsumerd is ready */
748 sem_post(&kconsumerd_sem);
749 DBG("Kconsumerd command socket ready");
750 } else {
6f61d021 751 DBG("Kconsumerd error when waiting for SOCK_READY : %s",
1d4b027a
DG
752 lttcomm_get_readable_code(-code));
753 goto error;
754 }
755
72079cae
DG
756 /* Kconsumerd err socket */
757 pollfd[1].fd = sock;
758 pollfd[1].events = POLLIN;
759
760 /* Inifinite blocking call, waiting for transmission */
761 ret = poll(pollfd, 2, -1);
762 if (ret < 0) {
763 perror("poll kconsumerd thread");
764 goto error;
765 }
766
767 /* Thread quit pipe has been closed. Killing thread. */
768 if (pollfd[0].revents == POLLNVAL) {
769 goto error;
770 } else if (pollfd[1].revents == POLLERR) {
771 ERR("Kconsumerd err socket second poll error");
772 goto error;
773 }
774
712ea556
DG
775 /* Wait for any kconsumerd error */
776 ret = lttcomm_recv_unix_sock(sock, &code, sizeof(enum lttcomm_return_code));
777 if (ret <= 0) {
778 ERR("Kconsumerd closed the command socket");
779 goto error;
6f61d021 780 }
1d4b027a 781
712ea556
DG
782 ERR("Kconsumerd return code : %s", lttcomm_get_readable_code(-code));
783
1d4b027a 784error:
6f61d021 785 DBG("Kconsumerd thread dying");
273ea72c
DG
786 if (kconsumerd_err_sock) {
787 close(kconsumerd_err_sock);
788 }
789 if (kconsumerd_cmd_sock) {
790 close(kconsumerd_cmd_sock);
791 }
792 if (sock) {
793 close(sock);
794 }
795
796 unlink(kconsumerd_err_unix_sock_path);
797 unlink(kconsumerd_cmd_unix_sock_path);
798
799 kconsumerd_pid = 0;
1d4b027a
DG
800 return NULL;
801}
802
803/*
1d4b027a
DG
804 * This thread manage the application socket communication
805 */
806static void *thread_manage_apps(void *data)
807{
273ea72c
DG
808 int sock = 0, ret;
809 struct pollfd pollfd[2];
1d4b027a
DG
810
811 /* TODO: Something more elegant is needed but fine for now */
812 /* FIXME: change all types to either uint8_t, uint32_t, uint64_t
813 * for 32-bit vs 64-bit compat processes. */
814 /* replicate in ust with version number */
815 struct {
816 int reg; /* 1:register, 0:unregister */
817 pid_t pid;
818 uid_t uid;
819 } reg_msg;
820
821 DBG("[thread] Manage apps started");
822
823 ret = lttcomm_listen_unix_sock(apps_sock);
824 if (ret < 0) {
825 goto error;
826 }
827
273ea72c
DG
828 /* First fd is always the quit pipe */
829 pollfd[0].fd = thread_quit_pipe[0];
830
831 /* Apps socket */
832 pollfd[1].fd = apps_sock;
833 pollfd[1].events = POLLIN;
834
1d4b027a
DG
835 /* Notify all applications to register */
836 notify_apps(default_global_apps_pipe);
837
838 while (1) {
839 DBG("Accepting application registration");
273ea72c
DG
840
841 /* Inifinite blocking call, waiting for transmission */
842 ret = poll(pollfd, 2, -1);
843 if (ret < 0) {
844 perror("poll apps thread");
845 goto error;
846 }
847
848 /* Thread quit pipe has been closed. Killing thread. */
849 if (pollfd[0].revents == POLLNVAL) {
850 goto error;
851 } else if (pollfd[1].revents == POLLERR) {
852 ERR("Apps socket poll error");
853 goto error;
854 }
855
1d4b027a
DG
856 sock = lttcomm_accept_unix_sock(apps_sock);
857 if (sock < 0) {
858 goto error;
859 }
860
273ea72c 861 /*
809dc281
MD
862 * Using message-based transmissions to ensure we don't
863 * have to deal with partially received messages.
1d4b027a 864 */
809dc281 865 ret = lttcomm_recv_unix_sock(sock, &reg_msg, sizeof(reg_msg));
1d4b027a
DG
866 if (ret < 0) {
867 perror("recv");
868 continue;
869 }
870
871 /* Add application to the global traceable list */
872 if (reg_msg.reg == 1) {
873 /* Registering */
809dc281
MD
874 /*
875 * TODO: socket should be either passed to a
876 * listener thread (for more messages) or
877 * closed. It currently leaks.
878 */
1d4b027a
DG
879 ret = register_traceable_app(reg_msg.pid, reg_msg.uid);
880 if (ret < 0) {
881 /* register_traceable_app only return an error with
882 * ENOMEM. At this point, we better stop everything.
883 */
884 goto error;
885 }
886 } else {
887 /* Unregistering */
888 unregister_traceable_app(reg_msg.pid);
889 }
890 }
891
892error:
273ea72c
DG
893 DBG("Apps thread dying");
894 if (apps_sock) {
895 close(apps_sock);
896 }
897 if (sock) {
898 close(sock);
899 }
1d4b027a 900
273ea72c 901 unlink(apps_unix_sock_path);
1d4b027a
DG
902 return NULL;
903}
904
8c0faa1d 905/*
d063d709
DG
906 * Start the thread_manage_kconsumerd. This must be done after a kconsumerd
907 * exec or it will fails.
8c0faa1d 908 */
693bd40b 909static int spawn_kconsumerd_thread(void)
8c0faa1d
DG
910{
911 int ret;
912
913 /* Setup semaphore */
914 sem_init(&kconsumerd_sem, 0, 0);
915
1d4b027a 916 ret = pthread_create(&kconsumerd_thread, NULL, thread_manage_kconsumerd, (void *) NULL);
8c0faa1d
DG
917 if (ret != 0) {
918 perror("pthread_create kconsumerd");
919 goto error;
920 }
921
693bd40b 922 /* Wait for the kconsumerd thread to be ready */
8c0faa1d
DG
923 sem_wait(&kconsumerd_sem);
924
712ea556
DG
925 if (kconsumerd_pid == 0) {
926 ERR("Kconsumerd did not start");
927 goto error;
928 }
929
8c0faa1d
DG
930 return 0;
931
932error:
712ea556 933 ret = LTTCOMM_KERN_CONSUMER_FAIL;
8c0faa1d
DG
934 return ret;
935}
936
d9800920
DG
937/*
938 * Join kernel consumer thread
939 */
cf3af59e
MD
940static int join_kconsumerd_thread(void)
941{
942 void *status;
943 int ret;
944
945 if (kconsumerd_pid != 0) {
946 ret = kill(kconsumerd_pid, SIGTERM);
947 if (ret) {
948 ERR("Error killing kconsumerd");
949 return ret;
950 }
951 return pthread_join(kconsumerd_thread, &status);
952 } else {
953 return 0;
954 }
955}
956
8c0faa1d 957/*
d063d709 958 * Fork and exec a kernel consumer daemon (kconsumerd).
8c0faa1d 959 *
d063d709 960 * Return pid if successful else -1.
8c0faa1d 961 */
693bd40b 962static pid_t spawn_kconsumerd(void)
8c0faa1d
DG
963{
964 int ret;
965 pid_t pid;
53086306 966 const char *verbosity;
8c0faa1d 967
c49dc785
DG
968 DBG("Spawning kconsumerd");
969
8c0faa1d
DG
970 pid = fork();
971 if (pid == 0) {
972 /*
973 * Exec kconsumerd.
974 */
31f73cc9 975 if (opt_verbose > 1 || opt_verbose_kconsumerd) {
53086306
DG
976 verbosity = "--verbose";
977 } else {
978 verbosity = "--quiet";
979 }
980 execl(INSTALL_BIN_PATH "/ltt-kconsumerd", "ltt-kconsumerd", verbosity, NULL);
8c0faa1d
DG
981 if (errno != 0) {
982 perror("kernel start consumer exec");
983 }
984 exit(EXIT_FAILURE);
985 } else if (pid > 0) {
986 ret = pid;
987 goto error;
988 } else {
989 perror("kernel start consumer fork");
990 ret = -errno;
991 goto error;
992 }
993
994error:
995 return ret;
996}
997
693bd40b 998/*
d063d709 999 * Spawn the kconsumerd daemon and session daemon thread.
693bd40b
DG
1000 */
1001static int start_kconsumerd(void)
1002{
1003 int ret;
1004
693bd40b 1005 pthread_mutex_lock(&kconsumerd_pid_mutex);
c49dc785 1006 if (kconsumerd_pid != 0) {
817153bb 1007 pthread_mutex_unlock(&kconsumerd_pid_mutex);
c49dc785
DG
1008 goto end;
1009 }
693bd40b 1010
c49dc785
DG
1011 ret = spawn_kconsumerd();
1012 if (ret < 0) {
1013 ERR("Spawning kconsumerd failed");
1014 ret = LTTCOMM_KERN_CONSUMER_FAIL;
1015 pthread_mutex_unlock(&kconsumerd_pid_mutex);
1016 goto error;
693bd40b 1017 }
c49dc785
DG
1018
1019 /* Setting up the global kconsumerd_pid */
1020 kconsumerd_pid = ret;
693bd40b
DG
1021 pthread_mutex_unlock(&kconsumerd_pid_mutex);
1022
6f61d021
DG
1023 DBG("Kconsumerd pid %d", ret);
1024
693bd40b 1025 DBG("Spawning kconsumerd thread");
693bd40b
DG
1026 ret = spawn_kconsumerd_thread();
1027 if (ret < 0) {
1028 ERR("Fatal error spawning kconsumerd thread");
693bd40b
DG
1029 goto error;
1030 }
1031
c49dc785 1032end:
693bd40b
DG
1033 return 0;
1034
1035error:
1036 return ret;
1037}
1038
b73401da 1039/*
d063d709 1040 * modprobe_kernel_modules
b73401da
DG
1041 */
1042static int modprobe_kernel_modules(void)
1043{
ab147185 1044 int ret = 0, i;
b73401da
DG
1045 char modprobe[256];
1046
ab147185
MD
1047 for (i = 0; i < ARRAY_SIZE(kernel_modules_list); i++) {
1048 ret = snprintf(modprobe, sizeof(modprobe),
1049 "/sbin/modprobe %s%s",
1050 kernel_modules_list[i].required ? "" : "--quiet ",
1051 kernel_modules_list[i].name);
b73401da
DG
1052 if (ret < 0) {
1053 perror("snprintf modprobe");
1054 goto error;
1055 }
ab147185 1056 modprobe[sizeof(modprobe) - 1] = '\0';
b73401da 1057 ret = system(modprobe);
ab147185
MD
1058 if (ret == -1) {
1059 ERR("Unable to launch modprobe for module %s",
1060 kernel_modules_list[i].name);
1061 } else if (kernel_modules_list[i].required
d9800920 1062 && WEXITSTATUS(ret) != 0) {
ab147185
MD
1063 ERR("Unable to load module %s",
1064 kernel_modules_list[i].name);
1065 } else {
1066 DBG("Modprobe successfully %s",
1067 kernel_modules_list[i].name);
1068 }
1069 }
1070
1071error:
1072 return ret;
1073}
1074
1075/*
1076 * modprobe_remove_kernel_modules
1077 * Remove modules in reverse load order.
1078 */
1079static int modprobe_remove_kernel_modules(void)
1080{
1081 int ret = 0, i;
1082 char modprobe[256];
1083
1084 for (i = ARRAY_SIZE(kernel_modules_list) - 1; i >= 0; i--) {
1085 ret = snprintf(modprobe, sizeof(modprobe),
1086 "/sbin/modprobe --remove --quiet %s",
1087 kernel_modules_list[i].name);
b73401da 1088 if (ret < 0) {
ab147185
MD
1089 perror("snprintf modprobe --remove");
1090 goto error;
1091 }
1092 modprobe[sizeof(modprobe) - 1] = '\0';
1093 ret = system(modprobe);
1094 if (ret == -1) {
1095 ERR("Unable to launch modprobe --remove for module %s",
1096 kernel_modules_list[i].name);
1097 } else if (kernel_modules_list[i].required
d9800920 1098 && WEXITSTATUS(ret) != 0) {
ab147185
MD
1099 ERR("Unable to remove module %s",
1100 kernel_modules_list[i].name);
1101 } else {
1102 DBG("Modprobe removal successful %s",
1103 kernel_modules_list[i].name);
b73401da 1104 }
b73401da
DG
1105 }
1106
1107error:
1108 return ret;
1109}
1110
1111/*
d063d709 1112 * mount_debugfs
b73401da
DG
1113 */
1114static int mount_debugfs(char *path)
1115{
1116 int ret;
1117 char *type = "debugfs";
1118
996b65c8 1119 ret = mkdir_recursive(path, S_IRWXU | S_IRWXG, geteuid(), getegid());
b73401da
DG
1120 if (ret < 0) {
1121 goto error;
1122 }
1123
1124 ret = mount(type, path, type, 0, NULL);
1125 if (ret < 0) {
1126 perror("mount debugfs");
1127 goto error;
1128 }
1129
1130 DBG("Mounted debugfs successfully at %s", path);
1131
1132error:
1133 return ret;
1134}
1135
8c0faa1d 1136/*
d063d709 1137 * Setup necessary data for kernel tracer action.
8c0faa1d 1138 */
f3ed775e 1139static void init_kernel_tracer(void)
8c0faa1d 1140{
b73401da
DG
1141 int ret;
1142 char *proc_mounts = "/proc/mounts";
1143 char line[256];
1144 char *debugfs_path = NULL, *lttng_path;
1145 FILE *fp;
1146
1147 /* Detect debugfs */
1148 fp = fopen(proc_mounts, "r");
1149 if (fp == NULL) {
1150 ERR("Unable to probe %s", proc_mounts);
1151 goto error;
1152 }
1153
1154 while (fgets(line, sizeof(line), fp) != NULL) {
1155 if (strstr(line, "debugfs") != NULL) {
1156 /* Remove first string */
1157 strtok(line, " ");
1158 /* Dup string here so we can reuse line later on */
1159 debugfs_path = strdup(strtok(NULL, " "));
1160 DBG("Got debugfs path : %s", debugfs_path);
1161 break;
1162 }
1163 }
1164
1165 fclose(fp);
1166
1167 /* Mount debugfs if needded */
1168 if (debugfs_path == NULL) {
1169 ret = asprintf(&debugfs_path, "/mnt/debugfs");
1170 if (ret < 0) {
1171 perror("asprintf debugfs path");
1172 goto error;
1173 }
1174 ret = mount_debugfs(debugfs_path);
1175 if (ret < 0) {
1176 goto error;
1177 }
1178 }
1179
1180 /* Modprobe lttng kernel modules */
1181 ret = modprobe_kernel_modules();
1182 if (ret < 0) {
1183 goto error;
1184 }
1185
1186 /* Setup lttng kernel path */
1187 ret = asprintf(&lttng_path, "%s/lttng", debugfs_path);
1188 if (ret < 0) {
1189 perror("asprintf lttng path");
1190 goto error;
1191 }
1192
1193 /* Open debugfs lttng */
1194 kernel_tracer_fd = open(lttng_path, O_RDWR);
f3ed775e 1195 if (kernel_tracer_fd < 0) {
b73401da
DG
1196 DBG("Failed to open %s", lttng_path);
1197 goto error;
8c0faa1d
DG
1198 }
1199
b73401da
DG
1200 free(lttng_path);
1201 free(debugfs_path);
f3ed775e 1202 DBG("Kernel tracer fd %d", kernel_tracer_fd);
b73401da
DG
1203 return;
1204
1205error:
1206 if (lttng_path) {
1207 free(lttng_path);
1208 }
1209 if (debugfs_path) {
1210 free(debugfs_path);
1211 }
1212 WARN("No kernel tracer available");
1213 kernel_tracer_fd = 0;
1214 return;
f3ed775e 1215}
33a2b854 1216
f3ed775e 1217/*
d063d709
DG
1218 * Start tracing by creating trace directory and sending FDs to the kernel
1219 * consumer.
f3ed775e
DG
1220 */
1221static int start_kernel_trace(struct ltt_kernel_session *session)
1222{
f40799e8 1223 int ret = 0;
8c0faa1d 1224
f3ed775e 1225 if (session->kconsumer_fds_sent == 0) {
d9800920
DG
1226 /*
1227 * Assign default kernel consumer if no consumer assigned to the kernel
1228 * session. At this point, it's NOT suppose to be 0 but this is an extra
1229 * security check.
1230 */
1231 if (session->consumer_fd == 0) {
1232 session->consumer_fd = kconsumerd_cmd_sock;
1233 }
1234
1235 ret = send_kconsumerd_fds(session);
f3ed775e
DG
1236 if (ret < 0) {
1237 ERR("Send kconsumerd fds failed");
1238 ret = LTTCOMM_KERN_CONSUMER_FAIL;
1239 goto error;
1240 }
1d4b027a 1241
f3ed775e
DG
1242 session->kconsumer_fds_sent = 1;
1243 }
1d4b027a
DG
1244
1245error:
1246 return ret;
8c0faa1d
DG
1247}
1248
7a485870
DG
1249/*
1250 * Notify kernel thread to update it's pollfd.
1251 */
1252static int notify_kernel_pollfd(void)
1253{
1254 int ret;
1255
1256 /* Inform kernel thread of the new kernel channel */
1257 ret = write(kernel_poll_pipe[1], "!", 1);
1258 if (ret < 0) {
1259 perror("write kernel poll pipe");
1260 }
1261
1262 return ret;
1263}
1264
8c0faa1d 1265/*
d063d709 1266 * Allocate a channel structure and fill it.
8c0faa1d 1267 */
b389abbe
MD
1268static struct lttng_channel *init_default_channel(enum lttng_domain_type domain_type,
1269 char *name)
8c0faa1d 1270{
f3ed775e 1271 struct lttng_channel *chan;
1d4b027a 1272
f3ed775e
DG
1273 chan = malloc(sizeof(struct lttng_channel));
1274 if (chan == NULL) {
1275 perror("init channel malloc");
1276 goto error;
8c0faa1d 1277 }
1d4b027a 1278
ed8f384d 1279 if (snprintf(chan->name, NAME_MAX, "%s", name) < 0) {
9f19cc17 1280 perror("snprintf channel name");
b389abbe 1281 goto error;
f3ed775e
DG
1282 }
1283
1284 chan->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
f3ed775e
DG
1285 chan->attr.switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
1286 chan->attr.read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
1d4b027a 1287
b389abbe
MD
1288 switch (domain_type) {
1289 case LTTNG_DOMAIN_KERNEL:
1290 chan->attr.subbuf_size = DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE;
1291 chan->attr.num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM;
1292 chan->attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
1293 break;
1294 /* TODO: add UST */
1295 default:
1296 goto error; /* Not implemented */
1297 }
1298
f3ed775e 1299 return chan;
b389abbe
MD
1300
1301error:
1302 free(chan);
1303 return NULL;
8c0faa1d
DG
1304}
1305
333f285e 1306/*
d063d709 1307 * Create a kernel tracer session then create the default channel.
333f285e 1308 */
f3ed775e 1309static int create_kernel_session(struct ltt_session *session)
333f285e 1310{
f3ed775e 1311 int ret;
f3ed775e
DG
1312
1313 DBG("Creating kernel session");
1314
1315 ret = kernel_create_session(session, kernel_tracer_fd);
1316 if (ret < 0) {
1317 ret = LTTCOMM_KERN_SESS_FAIL;
1318 goto error;
333f285e
DG
1319 }
1320
d9800920
DG
1321 /* Set kernel consumer socket fd */
1322 if (kconsumerd_cmd_sock) {
1323 session->kernel_session->consumer_fd = kconsumerd_cmd_sock;
1324 }
1325
63053e7c
DG
1326 ret = asprintf(&session->kernel_session->trace_path, "%s/kernel",
1327 session->path);
1328 if (ret < 0) {
1329 perror("asprintf kernel traces path");
1330 goto error;
1331 }
1332
1333 ret = mkdir_recursive(session->kernel_session->trace_path,
1334 S_IRWXU | S_IRWXG, geteuid(), allowed_group());
7a485870 1335 if (ret < 0) {
996b65c8 1336 if (ret != -EEXIST) {
7a485870
DG
1337 ERR("Trace directory creation error");
1338 goto error;
1339 }
1340 }
1341
f3ed775e
DG
1342error:
1343 return ret;
333f285e
DG
1344}
1345
6c9cc2ab
DG
1346/*
1347 * Using the session list, filled a lttng_session array to send back to the
1348 * client for session listing.
1349 *
1350 * The session list lock MUST be acquired before calling this function. Use
1351 * lock_session_list() and unlock_session_list().
1352 */
1353static void list_lttng_sessions(struct lttng_session *sessions)
1354{
1355 int i = 0;
1356 struct ltt_session *session;
1357
1358 DBG("Getting all available session");
1359 /*
1360 * Iterate over session list and append data after the control struct in
1361 * the buffer.
1362 */
1363 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
1364 strncpy(sessions[i].path, session->path, PATH_MAX);
99497cd0 1365 sessions[i].path[PATH_MAX - 1] = '\0';
6c9cc2ab 1366 strncpy(sessions[i].name, session->name, NAME_MAX);
99497cd0 1367 sessions[i].name[NAME_MAX - 1] = '\0';
6c9cc2ab
DG
1368 i++;
1369 }
1370}
1371
9f19cc17
DG
1372/*
1373 * Fill lttng_channel array of all channels.
1374 */
1375static void list_lttng_channels(struct ltt_session *session,
1376 struct lttng_channel *channels)
1377{
1378 int i = 0;
1379 struct ltt_kernel_channel *kchan;
1380
1381 DBG("Listing channels for session %s", session->name);
1382
1383 /* Kernel channels */
1384 if (session->kernel_session != NULL) {
1385 cds_list_for_each_entry(kchan, &session->kernel_session->channel_list.head, list) {
1386 /* Copy lttng_channel struct to array */
1387 memcpy(&channels[i], kchan->channel, sizeof(struct lttng_channel));
1388 channels[i].enabled = kchan->enabled;
1389 i++;
1390 }
1391 }
1392
1393 /* TODO: Missing UST listing */
1394}
1395
1396/*
1397 * Fill lttng_event array of all events in the channel.
1398 */
1399static void list_lttng_events(struct ltt_kernel_channel *kchan,
1400 struct lttng_event *events)
1401{
1402 /*
1403 * TODO: This is ONLY kernel. Need UST support.
1404 */
1405 int i = 0;
1406 struct ltt_kernel_event *event;
1407
1408 DBG("Listing events for channel %s", kchan->channel->name);
1409
1410 /* Kernel channels */
1411 cds_list_for_each_entry(event, &kchan->events_list.head , list) {
1412 strncpy(events[i].name, event->event->name, LTTNG_SYMBOL_NAME_LEN);
99497cd0 1413 events[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
9f19cc17
DG
1414 events[i].enabled = event->enabled;
1415 switch (event->event->instrumentation) {
1416 case LTTNG_KERNEL_TRACEPOINT:
1417 events[i].type = LTTNG_EVENT_TRACEPOINT;
1418 break;
1419 case LTTNG_KERNEL_KPROBE:
1420 case LTTNG_KERNEL_KRETPROBE:
1421 events[i].type = LTTNG_EVENT_PROBE;
1422 memcpy(&events[i].attr.probe, &event->event->u.kprobe,
1423 sizeof(struct lttng_kernel_kprobe));
1424 break;
1425 case LTTNG_KERNEL_FUNCTION:
1426 events[i].type = LTTNG_EVENT_FUNCTION;
1427 memcpy(&events[i].attr.ftrace, &event->event->u.ftrace,
1428 sizeof(struct lttng_kernel_function));
1429 break;
1430 }
1431 i++;
1432 }
1433}
1434
fac6795d 1435/*
d063d709
DG
1436 * Process the command requested by the lttng client within the command
1437 * context structure. This function make sure that the return structure (llm)
1438 * is set and ready for transmission before returning.
fac6795d 1439 *
e065084a 1440 * Return any error encountered or 0 for success.
fac6795d 1441 */
5461b305 1442static int process_client_msg(struct command_ctx *cmd_ctx)
fac6795d 1443{
f40799e8 1444 int ret = LTTCOMM_OK;
fac6795d 1445
5461b305 1446 DBG("Processing client command %d", cmd_ctx->lsm->cmd_type);
fac6795d 1447
63053e7c
DG
1448 /*
1449 * Commands that DO NOT need a session.
1450 */
5461b305 1451 switch (cmd_ctx->lsm->cmd_type) {
5e16da05
MD
1452 case LTTNG_CREATE_SESSION:
1453 case LTTNG_LIST_SESSIONS:
052da939 1454 case LTTNG_LIST_TRACEPOINTS:
d0254c7c 1455 case LTTNG_CALIBRATE:
5e16da05
MD
1456 break;
1457 default:
42abccdb
DG
1458 DBG("Getting session %s by name", cmd_ctx->lsm->session.name);
1459 cmd_ctx->session = find_session_by_name(cmd_ctx->lsm->session.name);
5461b305 1460 if (cmd_ctx->session == NULL) {
f3ed775e 1461 /* If session name not found */
42abccdb 1462 if (cmd_ctx->lsm->session.name != NULL) {
f3ed775e
DG
1463 ret = LTTCOMM_SESS_NOT_FOUND;
1464 } else { /* If no session name specified */
1465 ret = LTTCOMM_SELECT_SESS;
1466 }
5461b305 1467 goto error;
b5541356
DG
1468 } else {
1469 /* Acquire lock for the session */
1470 lock_session(cmd_ctx->session);
379473d2 1471 }
5e16da05 1472 break;
379473d2
DG
1473 }
1474
f3ed775e 1475 /*
d0254c7c 1476 * Check domain type for specific "pre-action".
f3ed775e 1477 */
0d0c377a
DG
1478 switch (cmd_ctx->lsm->domain.type) {
1479 case LTTNG_DOMAIN_KERNEL:
333f285e 1480 /* Kernel tracer check */
20fe2104 1481 if (kernel_tracer_fd == 0) {
333f285e
DG
1482 init_kernel_tracer();
1483 if (kernel_tracer_fd == 0) {
1484 ret = LTTCOMM_KERN_NA;
1485 goto error;
1486 }
20fe2104 1487 }
f3ed775e
DG
1488
1489 /* Need a session for kernel command */
d0254c7c 1490 switch (cmd_ctx->lsm->cmd_type) {
d9800920 1491 case LTTNG_CALIBRATE:
d0254c7c
MD
1492 case LTTNG_CREATE_SESSION:
1493 case LTTNG_LIST_SESSIONS:
1494 case LTTNG_LIST_TRACEPOINTS:
d0254c7c
MD
1495 break;
1496 default:
1497 if (cmd_ctx->session->kernel_session == NULL) {
1498 ret = create_kernel_session(cmd_ctx->session);
f3ed775e 1499 if (ret < 0) {
d0254c7c 1500 ret = LTTCOMM_KERN_SESS_FAIL;
f3ed775e
DG
1501 goto error;
1502 }
d0254c7c
MD
1503
1504 /* Start the kernel consumer daemon */
d9800920
DG
1505
1506 if (kconsumerd_pid == 0 &&
1507 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
d0254c7c
MD
1508 ret = start_kconsumerd();
1509 if (ret < 0) {
1510 goto error;
1511 }
1512 }
f3ed775e
DG
1513 }
1514 }
0d0c377a
DG
1515 break;
1516 default:
1517 break;
20fe2104
DG
1518 }
1519
fac6795d 1520 /* Process by command type */
5461b305 1521 switch (cmd_ctx->lsm->cmd_type) {
b579acd9 1522 case LTTNG_ADD_CONTEXT:
d65106b1 1523 {
b579acd9 1524 struct lttng_kernel_context kctx;
d65106b1
DG
1525
1526 /* Setup lttng message with no payload */
1527 ret = setup_lttng_msg(cmd_ctx, 0);
1528 if (ret < 0) {
1529 goto setup_error;
1530 }
1531
b579acd9
DG
1532 switch (cmd_ctx->lsm->domain.type) {
1533 case LTTNG_DOMAIN_KERNEL:
1534 /* Create Kernel context */
1535 kctx.ctx = cmd_ctx->lsm->u.context.ctx.ctx;
1536 kctx.u.perf_counter.type = cmd_ctx->lsm->u.context.ctx.u.perf_counter.type;
1537 kctx.u.perf_counter.config = cmd_ctx->lsm->u.context.ctx.u.perf_counter.config;
1538 strncpy(kctx.u.perf_counter.name,
1539 cmd_ctx->lsm->u.context.ctx.u.perf_counter.name,
1540 LTTNG_SYMBOL_NAME_LEN);
99497cd0 1541 kctx.u.perf_counter.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
b579acd9
DG
1542
1543 /* Add kernel context to kernel tracer. See context.c */
1544 ret = add_kernel_context(cmd_ctx->session->kernel_session, &kctx,
1545 cmd_ctx->lsm->u.context.event_name,
1546 cmd_ctx->lsm->u.context.channel_name);
1547 if (ret != LTTCOMM_OK) {
d65106b1
DG
1548 goto error;
1549 }
b579acd9
DG
1550 break;
1551 default:
1552 /* TODO: Userspace tracing */
1553 ret = LTTCOMM_NOT_IMPLEMENTED;
0b97ec54 1554 goto error;
d65106b1
DG
1555 }
1556
1557 ret = LTTCOMM_OK;
1558 break;
1559 }
0b97ec54 1560 case LTTNG_DISABLE_CHANNEL:
26cc6b4e 1561 {
0b97ec54 1562 struct ltt_kernel_channel *kchan;
26cc6b4e
DG
1563
1564 /* Setup lttng message with no payload */
1565 ret = setup_lttng_msg(cmd_ctx, 0);
1566 if (ret < 0) {
1567 goto setup_error;
1568 }
1569
0b97ec54
DG
1570 switch (cmd_ctx->lsm->domain.type) {
1571 case LTTNG_DOMAIN_KERNEL:
1572 kchan = get_kernel_channel_by_name(cmd_ctx->lsm->u.disable.channel_name,
1573 cmd_ctx->session->kernel_session);
1574 if (kchan == NULL) {
1575 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
26cc6b4e 1576 goto error;
0b97ec54
DG
1577 } else if (kchan->enabled == 1) {
1578 ret = kernel_disable_channel(kchan);
1579 if (ret < 0) {
1580 if (ret != EEXIST) {
1581 ret = LTTCOMM_KERN_CHAN_DISABLE_FAIL;
1582 }
1583 goto error;
1584 }
26cc6b4e 1585 }
0b97ec54
DG
1586 kernel_wait_quiescent(kernel_tracer_fd);
1587 break;
1588 default:
1589 /* TODO: Userspace tracing */
1590 ret = LTTCOMM_NOT_IMPLEMENTED;
1591 goto error;
26cc6b4e
DG
1592 }
1593
26cc6b4e
DG
1594 ret = LTTCOMM_OK;
1595 break;
1596 }
f5177a38 1597 case LTTNG_DISABLE_EVENT:
e953ef25 1598 {
f5177a38
DG
1599 struct ltt_kernel_channel *kchan;
1600 struct ltt_kernel_event *kevent;
e953ef25
DG
1601
1602 /* Setup lttng message with no payload */
1603 ret = setup_lttng_msg(cmd_ctx, 0);
1604 if (ret < 0) {
1605 goto setup_error;
1606 }
1607
f5177a38
DG
1608 switch (cmd_ctx->lsm->domain.type) {
1609 case LTTNG_DOMAIN_KERNEL:
1610 kchan = get_kernel_channel_by_name(cmd_ctx->lsm->u.disable.channel_name,
1611 cmd_ctx->session->kernel_session);
1612 if (kchan == NULL) {
1613 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
19e70852 1614 goto error;
e953ef25 1615 }
f5177a38
DG
1616
1617 kevent = get_kernel_event_by_name(cmd_ctx->lsm->u.disable.name, kchan);
1618 if (kevent != NULL) {
1619 DBG("Disabling kernel event %s for channel %s.", kevent->event->name,
1620 kchan->channel->name);
1621 ret = kernel_disable_event(kevent);
1622 if (ret < 0) {
1623 ret = LTTCOMM_KERN_ENABLE_FAIL;
1624 goto error;
1625 }
1626 }
1627
1628 kernel_wait_quiescent(kernel_tracer_fd);
1629 break;
1630 default:
1631 /* TODO: Userspace tracing */
1632 ret = LTTCOMM_NOT_IMPLEMENTED;
1633 goto error;
e953ef25
DG
1634 }
1635
19e70852 1636 ret = LTTCOMM_OK;
e953ef25
DG
1637 break;
1638 }
f5177a38 1639 case LTTNG_DISABLE_ALL_EVENT:
950131af 1640 {
f5177a38
DG
1641 struct ltt_kernel_channel *kchan;
1642 struct ltt_kernel_event *kevent;
950131af
DG
1643
1644 /* Setup lttng message with no payload */
1645 ret = setup_lttng_msg(cmd_ctx, 0);
1646 if (ret < 0) {
1647 goto setup_error;
1648 }
1649
f5177a38
DG
1650 switch (cmd_ctx->lsm->domain.type) {
1651 case LTTNG_DOMAIN_KERNEL:
1652 DBG("Disabling all enabled kernel events");
1653 kchan = get_kernel_channel_by_name(cmd_ctx->lsm->u.disable.channel_name,
1654 cmd_ctx->session->kernel_session);
1655 if (kchan == NULL) {
1656 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
1657 goto error;
1658 }
950131af 1659
f5177a38
DG
1660 /* For each event in the kernel session */
1661 cds_list_for_each_entry(kevent, &kchan->events_list.head, list) {
1662 DBG("Disabling kernel event %s for channel %s.",
1663 kevent->event->name, kchan->channel->name);
1664 ret = kernel_disable_event(kevent);
1665 if (ret < 0) {
1666 continue;
1667 }
950131af 1668 }
f5177a38
DG
1669
1670 /* Quiescent wait after event disable */
1671 kernel_wait_quiescent(kernel_tracer_fd);
1672 break;
1673 default:
1674 /* TODO: Userspace tracing */
1675 ret = LTTCOMM_NOT_IMPLEMENTED;
1676 goto error;
950131af
DG
1677 }
1678
950131af
DG
1679 ret = LTTCOMM_OK;
1680 break;
1681 }
0d0c377a 1682 case LTTNG_ENABLE_CHANNEL:
d36b8583 1683 {
0d0c377a 1684 struct ltt_kernel_channel *kchan;
d36b8583
DG
1685
1686 /* Setup lttng message with no payload */
1687 ret = setup_lttng_msg(cmd_ctx, 0);
1688 if (ret < 0) {
1689 goto setup_error;
1690 }
1691
0d0c377a
DG
1692 switch (cmd_ctx->lsm->domain.type) {
1693 case LTTNG_DOMAIN_KERNEL:
1694 kchan = get_kernel_channel_by_name(cmd_ctx->lsm->u.enable.channel_name,
1695 cmd_ctx->session->kernel_session);
1696 if (kchan == NULL) {
1697 /* Channel not found, creating it */
1698 DBG("Creating kernel channel");
7d29a247 1699
0d0c377a 1700 ret = kernel_create_channel(cmd_ctx->session->kernel_session,
63053e7c
DG
1701 &cmd_ctx->lsm->u.channel.chan,
1702 cmd_ctx->session->kernel_session->trace_path);
0d0c377a
DG
1703 if (ret < 0) {
1704 ret = LTTCOMM_KERN_CHAN_FAIL;
1705 goto error;
1706 }
7d29a247 1707
0d0c377a
DG
1708 /* Notify kernel thread that there is a new channel */
1709 ret = notify_kernel_pollfd();
1710 if (ret < 0) {
1711 ret = LTTCOMM_FATAL;
1712 goto error;
1713 }
1714 } else if (kchan->enabled == 0) {
1715 ret = kernel_enable_channel(kchan);
1716 if (ret < 0) {
1717 if (ret != EEXIST) {
1718 ret = LTTCOMM_KERN_CHAN_ENABLE_FAIL;
1719 }
1720 goto error;
d36b8583 1721 }
d36b8583 1722 }
0d0c377a
DG
1723
1724 kernel_wait_quiescent(kernel_tracer_fd);
1725 break;
1726 default:
1727 /* TODO: Userspace tracing */
1728 ret = LTTCOMM_NOT_IMPLEMENTED;
1729 goto error;
d36b8583
DG
1730 }
1731
d36b8583
DG
1732 ret = LTTCOMM_OK;
1733 break;
1734 }
0d0c377a 1735 case LTTNG_ENABLE_EVENT:
894be886 1736 {
7d29a247
DG
1737 char *channel_name;
1738 struct ltt_kernel_channel *kchan;
0d0c377a 1739 struct ltt_kernel_event *kevent;
7d29a247 1740 struct lttng_channel *chan;
f3ed775e 1741
894be886
DG
1742 /* Setup lttng message with no payload */
1743 ret = setup_lttng_msg(cmd_ctx, 0);
1744 if (ret < 0) {
1745 goto setup_error;
1746 }
1747
7d29a247
DG
1748 channel_name = cmd_ctx->lsm->u.enable.channel_name;
1749
0d0c377a
DG
1750 switch (cmd_ctx->lsm->domain.type) {
1751 case LTTNG_DOMAIN_KERNEL:
b389abbe
MD
1752 kchan = get_kernel_channel_by_name(channel_name,
1753 cmd_ctx->session->kernel_session);
1754 if (kchan == NULL) {
1755 DBG("Channel not found. Creating channel %s", channel_name);
1756
1757 chan = init_default_channel(cmd_ctx->lsm->domain.type, channel_name);
1758 if (chan == NULL) {
1759 ret = LTTCOMM_FATAL;
1760 goto error;
1761 }
1762
1763 ret = kernel_create_channel(cmd_ctx->session->kernel_session,
1764 chan, cmd_ctx->session->kernel_session->trace_path);
1765 if (ret < 0) {
1766 ret = LTTCOMM_KERN_CHAN_FAIL;
1767 goto error;
1768 }
0d0c377a
DG
1769 kchan = get_kernel_channel_by_name(channel_name,
1770 cmd_ctx->session->kernel_session);
1771 if (kchan == NULL) {
b389abbe
MD
1772 ERR("Channel %s not found after creation. Internal error, giving up.",
1773 channel_name);
1774 ret = LTTCOMM_FATAL;
1775 goto error;
7d29a247 1776 }
b389abbe 1777 }
7d29a247 1778
0d0c377a
DG
1779 kevent = get_kernel_event_by_name(cmd_ctx->lsm->u.enable.event.name, kchan);
1780 if (kevent == NULL) {
1781 DBG("Creating kernel event %s for channel %s.",
1782 cmd_ctx->lsm->u.enable.event.name, channel_name);
1783 ret = kernel_create_event(&cmd_ctx->lsm->u.enable.event, kchan);
1784 } else {
1785 DBG("Enabling kernel event %s for channel %s.",
1786 kevent->event->name, channel_name);
1787 ret = kernel_enable_event(kevent);
1788 if (ret == -EEXIST) {
1789 ret = LTTCOMM_KERN_EVENT_EXIST;
7d29a247
DG
1790 goto error;
1791 }
1792 }
f34daff7 1793
0d0c377a
DG
1794 if (ret < 0) {
1795 ret = LTTCOMM_KERN_ENABLE_FAIL;
7d29a247
DG
1796 goto error;
1797 }
19e70852 1798
0d0c377a
DG
1799 kernel_wait_quiescent(kernel_tracer_fd);
1800 break;
1801 default:
1802 /* TODO: Userspace tracing */
1803 ret = LTTCOMM_NOT_IMPLEMENTED;
19e70852 1804 goto error;
f3ed775e 1805 }
19e70852 1806 ret = LTTCOMM_OK;
894be886
DG
1807 break;
1808 }
0d0c377a 1809 case LTTNG_ENABLE_ALL_EVENT:
33a2b854 1810 {
9f19cc17
DG
1811 int size, i;
1812 char *channel_name;
7d29a247 1813 struct ltt_kernel_channel *kchan;
0d0c377a 1814 struct ltt_kernel_event *kevent;
9f19cc17 1815 struct lttng_event *event_list;
7d29a247 1816 struct lttng_channel *chan;
33a2b854
DG
1817
1818 /* Setup lttng message with no payload */
1819 ret = setup_lttng_msg(cmd_ctx, 0);
1820 if (ret < 0) {
1821 goto setup_error;
1822 }
1823
1824 DBG("Enabling all kernel event");
1825
7d29a247
DG
1826 channel_name = cmd_ctx->lsm->u.enable.channel_name;
1827
0d0c377a
DG
1828 switch (cmd_ctx->lsm->domain.type) {
1829 case LTTNG_DOMAIN_KERNEL:
b389abbe
MD
1830 kchan = get_kernel_channel_by_name(channel_name,
1831 cmd_ctx->session->kernel_session);
1832 if (kchan == NULL) {
1833 DBG("Channel not found. Creating channel %s", channel_name);
1834
1835 chan = init_default_channel(cmd_ctx->lsm->domain.type, channel_name);
1836 if (chan == NULL) {
1837 ret = LTTCOMM_FATAL;
1838 goto error;
1839 }
1840
1841 ret = kernel_create_channel(cmd_ctx->session->kernel_session,
1842 chan, cmd_ctx->session->kernel_session->trace_path);
1843 if (ret < 0) {
1844 ret = LTTCOMM_KERN_CHAN_FAIL;
1845 goto error;
1846 }
0d0c377a
DG
1847 kchan = get_kernel_channel_by_name(channel_name,
1848 cmd_ctx->session->kernel_session);
1849 if (kchan == NULL) {
b389abbe
MD
1850 ERR("Channel %s not found after creation. Internal error, giving up.",
1851 channel_name);
1852 ret = LTTCOMM_FATAL;
1853 goto error;
7d29a247 1854 }
b389abbe 1855 }
7d29a247 1856
0d0c377a
DG
1857 /* For each event in the kernel session */
1858 cds_list_for_each_entry(kevent, &kchan->events_list.head, list) {
1859 DBG("Enabling kernel event %s for channel %s.",
1860 kevent->event->name, channel_name);
1861 ret = kernel_enable_event(kevent);
7d29a247 1862 if (ret < 0) {
0d0c377a 1863 continue;
7d29a247
DG
1864 }
1865 }
33a2b854 1866
0d0c377a
DG
1867 size = kernel_list_events(kernel_tracer_fd, &event_list);
1868 if (size < 0) {
1869 ret = LTTCOMM_KERN_LIST_FAIL;
1870 goto error;
f3ed775e 1871 }
f3ed775e 1872
0d0c377a
DG
1873 for (i = 0; i < size; i++) {
1874 kevent = get_kernel_event_by_name(event_list[i].name, kchan);
1875 if (kevent == NULL) {
1876 /* Default event type for enable all */
1877 event_list[i].type = LTTNG_EVENT_TRACEPOINT;
1878 /* Enable each single tracepoint event */
1879 ret = kernel_create_event(&event_list[i], kchan);
1880 if (ret < 0) {
1881 /* Ignore error here and continue */
1882 }
950131af 1883 }
33a2b854 1884 }
33a2b854 1885
0d0c377a
DG
1886 free(event_list);
1887
1888 /* Quiescent wait after event enable */
1889 kernel_wait_quiescent(kernel_tracer_fd);
1890 break;
1891 default:
1892 /* TODO: Userspace tracing */
1893 ret = LTTCOMM_NOT_IMPLEMENTED;
1894 goto error;
1895 }
33a2b854
DG
1896
1897 ret = LTTCOMM_OK;
1898 break;
1899 }
052da939 1900 case LTTNG_LIST_TRACEPOINTS:
2ef84c95 1901 {
9f19cc17 1902 struct lttng_event *events;
052da939
DG
1903 ssize_t nb_events = 0;
1904
1905 switch (cmd_ctx->lsm->domain.type) {
1906 case LTTNG_DOMAIN_KERNEL:
1907 DBG("Listing kernel events");
1908 nb_events = kernel_list_events(kernel_tracer_fd, &events);
1909 if (nb_events < 0) {
1910 ret = LTTCOMM_KERN_LIST_FAIL;
1911 goto error;
1912 }
1913 break;
1914 default:
1915 /* TODO: Userspace listing */
1916 ret = LTTCOMM_NOT_IMPLEMENTED;
1917 break;
2ef84c95
DG
1918 }
1919
1920 /*
1921 * Setup lttng message with payload size set to the event list size in
1922 * bytes and then copy list into the llm payload.
1923 */
052da939 1924 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_event) * nb_events);
2ef84c95 1925 if (ret < 0) {
052da939 1926 free(events);
2ef84c95
DG
1927 goto setup_error;
1928 }
1929
1930 /* Copy event list into message payload */
9f19cc17 1931 memcpy(cmd_ctx->llm->payload, events,
052da939 1932 sizeof(struct lttng_event) * nb_events);
2ef84c95 1933
9f19cc17 1934 free(events);
2ef84c95
DG
1935
1936 ret = LTTCOMM_OK;
1937 break;
1938 }
f3ed775e 1939 case LTTNG_START_TRACE:
8c0faa1d
DG
1940 {
1941 struct ltt_kernel_channel *chan;
f3ed775e 1942
8c0faa1d
DG
1943 /* Setup lttng message with no payload */
1944 ret = setup_lttng_msg(cmd_ctx, 0);
1945 if (ret < 0) {
1946 goto setup_error;
1947 }
1948
f3ed775e
DG
1949 /* Kernel tracing */
1950 if (cmd_ctx->session->kernel_session != NULL) {
1951 if (cmd_ctx->session->kernel_session->metadata == NULL) {
1952 DBG("Open kernel metadata");
58a97671 1953 ret = kernel_open_metadata(cmd_ctx->session->kernel_session,
63053e7c 1954 cmd_ctx->session->kernel_session->trace_path);
f3ed775e
DG
1955 if (ret < 0) {
1956 ret = LTTCOMM_KERN_META_FAIL;
1957 goto error;
1958 }
1959 }
8c0faa1d 1960
f3ed775e
DG
1961 if (cmd_ctx->session->kernel_session->metadata_stream_fd == 0) {
1962 DBG("Opening kernel metadata stream");
1963 if (cmd_ctx->session->kernel_session->metadata_stream_fd == 0) {
1964 ret = kernel_open_metadata_stream(cmd_ctx->session->kernel_session);
1965 if (ret < 0) {
1966 ERR("Kernel create metadata stream failed");
1967 ret = LTTCOMM_KERN_STREAM_FAIL;
1968 goto error;
1969 }
1970 }
1971 }
8c0faa1d 1972
f3ed775e 1973 /* For each channel */
0d0c377a
DG
1974 cds_list_for_each_entry(chan,
1975 &cmd_ctx->session->kernel_session->channel_list.head, list) {
f3ed775e
DG
1976 if (chan->stream_count == 0) {
1977 ret = kernel_open_channel_stream(chan);
1978 if (ret < 0) {
1979 ERR("Kernel create channel stream failed");
1980 ret = LTTCOMM_KERN_STREAM_FAIL;
1981 goto error;
1982 }
1983 /* Update the stream global counter */
1984 cmd_ctx->session->kernel_session->stream_count_global += ret;
1985 }
1986 }
1987
283046e0 1988 ret = start_kernel_trace(cmd_ctx->session->kernel_session);
8c0faa1d 1989 if (ret < 0) {
f3ed775e 1990 ret = LTTCOMM_KERN_START_FAIL;
8c0faa1d
DG
1991 goto error;
1992 }
8c0faa1d 1993
283046e0
DG
1994 DBG("Start kernel tracing");
1995 ret = kernel_start_session(cmd_ctx->session->kernel_session);
f3ed775e 1996 if (ret < 0) {
283046e0 1997 ERR("Kernel start session failed");
f3ed775e 1998 ret = LTTCOMM_KERN_START_FAIL;
8c0faa1d
DG
1999 goto error;
2000 }
8c0faa1d 2001
f3ed775e
DG
2002 /* Quiescent wait after starting trace */
2003 kernel_wait_quiescent(kernel_tracer_fd);
8c0faa1d
DG
2004 }
2005
f3ed775e 2006 /* TODO: Start all UST traces */
8c0faa1d
DG
2007
2008 ret = LTTCOMM_OK;
2009 break;
2010 }
f3ed775e 2011 case LTTNG_STOP_TRACE:
8c0faa1d 2012 {
f3ed775e 2013 struct ltt_kernel_channel *chan;
8c0faa1d
DG
2014 /* Setup lttng message with no payload */
2015 ret = setup_lttng_msg(cmd_ctx, 0);
2016 if (ret < 0) {
2017 goto setup_error;
2018 }
2019
f3ed775e
DG
2020 /* Kernel tracer */
2021 if (cmd_ctx->session->kernel_session != NULL) {
2022 DBG("Stop kernel tracing");
84291629 2023
f3ed775e
DG
2024 ret = kernel_metadata_flush_buffer(cmd_ctx->session->kernel_session->metadata_stream_fd);
2025 if (ret < 0) {
2026 ERR("Kernel metadata flush failed");
2027 }
8c0faa1d 2028
f3ed775e
DG
2029 cds_list_for_each_entry(chan, &cmd_ctx->session->kernel_session->channel_list.head, list) {
2030 ret = kernel_flush_buffer(chan);
2031 if (ret < 0) {
2032 ERR("Kernel flush buffer error");
2033 }
2034 }
2035
2036 ret = kernel_stop_session(cmd_ctx->session->kernel_session);
2037 if (ret < 0) {
2038 ERR("Kernel stop session failed");
2039 ret = LTTCOMM_KERN_STOP_FAIL;
2040 goto error;
2041 }
2042
2043 /* Quiescent wait after stopping trace */
2044 kernel_wait_quiescent(kernel_tracer_fd);
8c0faa1d
DG
2045 }
2046
f3ed775e 2047 /* TODO : User-space tracer */
8c0faa1d
DG
2048
2049 ret = LTTCOMM_OK;
2050 break;
2051 }
5e16da05
MD
2052 case LTTNG_CREATE_SESSION:
2053 {
5461b305
DG
2054 /* Setup lttng message with no payload */
2055 ret = setup_lttng_msg(cmd_ctx, 0);
2056 if (ret < 0) {
2057 goto setup_error;
2058 }
2059
42abccdb 2060 ret = create_session(cmd_ctx->lsm->session.name, cmd_ctx->lsm->session.path);
5e16da05 2061 if (ret < 0) {
f3ed775e 2062 if (ret == -EEXIST) {
5e16da05
MD
2063 ret = LTTCOMM_EXIST_SESS;
2064 } else {
aaf97519 2065 ret = LTTCOMM_FATAL;
aaf97519 2066 }
5461b305 2067 goto error;
8028d920 2068 }
1657e9bb 2069
5461b305 2070 ret = LTTCOMM_OK;
5e16da05
MD
2071 break;
2072 }
2073 case LTTNG_DESTROY_SESSION:
2074 {
5461b305
DG
2075 /* Setup lttng message with no payload */
2076 ret = setup_lttng_msg(cmd_ctx, 0);
2077 if (ret < 0) {
2078 goto setup_error;
2079 }
2080
f3ed775e
DG
2081 /* Clean kernel session teardown */
2082 teardown_kernel_session(cmd_ctx->session);
2083
42abccdb 2084 ret = destroy_session(cmd_ctx->lsm->session.name);
5e16da05 2085 if (ret < 0) {
f3ed775e 2086 ret = LTTCOMM_FATAL;
5461b305 2087 goto error;
5e16da05 2088 }
1657e9bb 2089
7a485870
DG
2090 /*
2091 * Must notify the kernel thread here to update it's pollfd in order to
2092 * remove the channel(s)' fd just destroyed.
2093 */
2094 ret = notify_kernel_pollfd();
2095 if (ret < 0) {
2096 ret = LTTCOMM_FATAL;
2097 goto error;
2098 }
2099
5461b305
DG
2100 ret = LTTCOMM_OK;
2101 break;
5e16da05 2102 }
9f19cc17 2103 case LTTNG_LIST_DOMAINS:
5e16da05 2104 {
4b222185 2105 size_t nb_dom = 0;
5461b305 2106
9f19cc17
DG
2107 if (cmd_ctx->session->kernel_session != NULL) {
2108 nb_dom++;
ce3d728c 2109 }
520ff687 2110
9f19cc17
DG
2111 nb_dom += cmd_ctx->session->ust_trace_count;
2112
2113 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_domain) * nb_dom);
5461b305
DG
2114 if (ret < 0) {
2115 goto setup_error;
520ff687 2116 }
57167058 2117
9f19cc17
DG
2118 ((struct lttng_domain *)(cmd_ctx->llm->payload))[0].type =
2119 LTTNG_DOMAIN_KERNEL;
5e16da05 2120
9f19cc17 2121 /* TODO: User-space tracer domain support */
5461b305 2122 ret = LTTCOMM_OK;
5e16da05
MD
2123 break;
2124 }
9f19cc17 2125 case LTTNG_LIST_CHANNELS:
5e16da05 2126 {
9f19cc17
DG
2127 /*
2128 * TODO: Only kernel channels are listed here. UST listing
2129 * is needed on lttng-ust 2.0 release.
2130 */
2131 size_t nb_chan = 0;
2132 if (cmd_ctx->session->kernel_session != NULL) {
2133 nb_chan += cmd_ctx->session->kernel_session->channel_count;
5461b305 2134 }
ca95a216 2135
9f19cc17
DG
2136 ret = setup_lttng_msg(cmd_ctx,
2137 sizeof(struct lttng_channel) * nb_chan);
5461b305
DG
2138 if (ret < 0) {
2139 goto setup_error;
2140 }
9f19cc17
DG
2141
2142 list_lttng_channels(cmd_ctx->session,
2143 (struct lttng_channel *)(cmd_ctx->llm->payload));
2144
2145 ret = LTTCOMM_OK;
5461b305 2146 break;
5e16da05 2147 }
9f19cc17 2148 case LTTNG_LIST_EVENTS:
5e16da05 2149 {
9f19cc17
DG
2150 /*
2151 * TODO: Only kernel events are listed here. UST listing
2152 * is needed on lttng-ust 2.0 release.
2153 */
2154 size_t nb_event = 0;
2155 struct ltt_kernel_channel *kchan = NULL;
2156
2157 if (cmd_ctx->session->kernel_session != NULL) {
2158 kchan = get_kernel_channel_by_name(cmd_ctx->lsm->u.list.channel_name,
2159 cmd_ctx->session->kernel_session);
2160 if (kchan == NULL) {
2161 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
2162 goto error;
2163 }
2164 nb_event += kchan->event_count;
5461b305 2165 }
ca95a216 2166
9f19cc17
DG
2167 ret = setup_lttng_msg(cmd_ctx,
2168 sizeof(struct lttng_event) * nb_event);
5461b305
DG
2169 if (ret < 0) {
2170 goto setup_error;
2171 }
9f19cc17 2172
ced2f820 2173 DBG("Listing events (%zu events)", nb_event);
9f19cc17
DG
2174
2175 list_lttng_events(kchan,
2176 (struct lttng_event *)(cmd_ctx->llm->payload));
2177
2178 ret = LTTCOMM_OK;
5461b305 2179 break;
5e16da05
MD
2180 }
2181 case LTTNG_LIST_SESSIONS:
2182 {
6c9cc2ab 2183 lock_session_list();
5461b305 2184
6c9cc2ab 2185 if (session_list_ptr->count == 0) {
f3ed775e 2186 ret = LTTCOMM_NO_SESSION;
36a83748 2187 unlock_session_list();
5461b305 2188 goto error;
57167058 2189 }
5e16da05 2190
6c9cc2ab
DG
2191 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_session) *
2192 session_list_ptr->count);
5461b305 2193 if (ret < 0) {
36a83748 2194 unlock_session_list();
5461b305 2195 goto setup_error;
e065084a 2196 }
5e16da05 2197
6c9cc2ab
DG
2198 /* Filled the session array */
2199 list_lttng_sessions((struct lttng_session *)(cmd_ctx->llm->payload));
2200
2201 unlock_session_list();
5e16da05 2202
5461b305 2203 ret = LTTCOMM_OK;
5e16da05
MD
2204 break;
2205 }
d0254c7c
MD
2206 case LTTNG_CALIBRATE:
2207 {
2208 /* Setup lttng message with no payload */
2209 ret = setup_lttng_msg(cmd_ctx, 0);
2210 if (ret < 0) {
2211 goto setup_error;
2212 }
2213
2214 switch (cmd_ctx->lsm->domain.type) {
2215 case LTTNG_DOMAIN_KERNEL:
2216 {
2217 struct lttng_kernel_calibrate kcalibrate;
2218
2219 kcalibrate.type = cmd_ctx->lsm->u.calibrate.type;
2220 ret = kernel_calibrate(kernel_tracer_fd, &kcalibrate);
2221 if (ret < 0) {
2222 ret = LTTCOMM_KERN_ENABLE_FAIL;
2223 goto error;
2224 }
2225 break;
2226 }
2227 default:
2228 /* TODO: Userspace tracing */
2229 ret = LTTCOMM_NOT_IMPLEMENTED;
2230 goto error;
2231 }
2232 ret = LTTCOMM_OK;
2233 break;
2234 }
d9800920
DG
2235 case LTTNG_REGISTER_CONSUMER:
2236 {
2237 int sock;
2238
2239 /* Setup lttng message with no payload */
2240 ret = setup_lttng_msg(cmd_ctx, 0);
2241 if (ret < 0) {
2242 goto setup_error;
2243 }
2244
2245 switch (cmd_ctx->lsm->domain.type) {
2246 case LTTNG_DOMAIN_KERNEL:
2247 {
2248 /* Can't register a consumer if there is already one */
2249 if (cmd_ctx->session->kernel_session->consumer_fd != 0) {
2250 ret = LTTCOMM_CONNECT_FAIL;
2251 goto error;
2252 }
2253
2254 sock = lttcomm_connect_unix_sock(cmd_ctx->lsm->u.reg.path);
2255 if (sock < 0) {
2256 ret = LTTCOMM_CONNECT_FAIL;
2257 goto error;
2258 }
2259
2260 cmd_ctx->session->kernel_session->consumer_fd = sock;
2261 break;
2262 }
2263 default:
2264 /* TODO: Userspace tracing */
2265 ret = LTTCOMM_NOT_IMPLEMENTED;
2266 goto error;
2267 }
2268
2269 ret = LTTCOMM_OK;
2270 break;
2271 }
d0254c7c 2272
5e16da05
MD
2273 default:
2274 /* Undefined command */
5461b305
DG
2275 ret = setup_lttng_msg(cmd_ctx, 0);
2276 if (ret < 0) {
2277 goto setup_error;
2278 }
2279
5e16da05 2280 ret = LTTCOMM_UND;
5461b305 2281 break;
fac6795d
DG
2282 }
2283
5461b305
DG
2284 /* Set return code */
2285 cmd_ctx->llm->ret_code = ret;
ca95a216 2286
b5541356
DG
2287 if (cmd_ctx->session) {
2288 unlock_session(cmd_ctx->session);
2289 }
2290
e065084a
DG
2291 return ret;
2292
5461b305 2293error:
5461b305
DG
2294 if (cmd_ctx->llm == NULL) {
2295 DBG("Missing llm structure. Allocating one.");
894be886 2296 if (setup_lttng_msg(cmd_ctx, 0) < 0) {
5461b305
DG
2297 goto setup_error;
2298 }
2299 }
e065084a 2300 /* Notify client of error */
5461b305 2301 cmd_ctx->llm->ret_code = ret;
e065084a 2302
5461b305 2303setup_error:
b5541356
DG
2304 if (cmd_ctx->session) {
2305 unlock_session(cmd_ctx->session);
2306 }
8028d920 2307 return ret;
fac6795d
DG
2308}
2309
1d4b027a 2310/*
d063d709
DG
2311 * This thread manage all clients request using the unix client socket for
2312 * communication.
1d4b027a
DG
2313 */
2314static void *thread_manage_clients(void *data)
2315{
273ea72c
DG
2316 int sock = 0, ret;
2317 struct command_ctx *cmd_ctx = NULL;
2318 struct pollfd pollfd[2];
1d4b027a
DG
2319
2320 DBG("[thread] Manage client started");
2321
2322 ret = lttcomm_listen_unix_sock(client_sock);
2323 if (ret < 0) {
2324 goto error;
2325 }
2326
273ea72c
DG
2327 /* First fd is always the quit pipe */
2328 pollfd[0].fd = thread_quit_pipe[0];
2329
2330 /* Apps socket */
2331 pollfd[1].fd = client_sock;
2332 pollfd[1].events = POLLIN;
2333
1d4b027a
DG
2334 /* Notify parent pid that we are ready
2335 * to accept command for client side.
2336 */
2337 if (opt_sig_parent) {
2338 kill(ppid, SIGCHLD);
2339 }
2340
2341 while (1) {
1d4b027a 2342 DBG("Accepting client command ...");
273ea72c
DG
2343
2344 /* Inifinite blocking call, waiting for transmission */
2345 ret = poll(pollfd, 2, -1);
2346 if (ret < 0) {
2347 perror("poll client thread");
2348 goto error;
2349 }
2350
2351 /* Thread quit pipe has been closed. Killing thread. */
2352 if (pollfd[0].revents == POLLNVAL) {
2353 goto error;
2354 } else if (pollfd[1].revents == POLLERR) {
2355 ERR("Client socket poll error");
2356 goto error;
2357 }
2358
1d4b027a
DG
2359 sock = lttcomm_accept_unix_sock(client_sock);
2360 if (sock < 0) {
2361 goto error;
2362 }
2363
2364 /* Allocate context command to process the client request */
2365 cmd_ctx = malloc(sizeof(struct command_ctx));
2366
2367 /* Allocate data buffer for reception */
2368 cmd_ctx->lsm = malloc(sizeof(struct lttcomm_session_msg));
2369 cmd_ctx->llm = NULL;
2370 cmd_ctx->session = NULL;
2371
2372 /*
2373 * Data is received from the lttng client. The struct
2374 * lttcomm_session_msg (lsm) contains the command and data request of
2375 * the client.
2376 */
2377 DBG("Receiving data from client ...");
2378 ret = lttcomm_recv_unix_sock(sock, cmd_ctx->lsm, sizeof(struct lttcomm_session_msg));
2379 if (ret <= 0) {
2380 continue;
2381 }
2382
f7776ea7
DG
2383 // TODO: Validate cmd_ctx including sanity check for security purpose.
2384
1d4b027a
DG
2385 /*
2386 * This function dispatch the work to the kernel or userspace tracer
2387 * libs and fill the lttcomm_lttng_msg data structure of all the needed
2388 * informations for the client. The command context struct contains
2389 * everything this function may needs.
2390 */
2391 ret = process_client_msg(cmd_ctx);
2392 if (ret < 0) {
2393 /* TODO: Inform client somehow of the fatal error. At this point,
2394 * ret < 0 means that a malloc failed (ENOMEM). */
2395 /* Error detected but still accept command */
a2fb29a5 2396 clean_command_ctx(&cmd_ctx);
1d4b027a
DG
2397 continue;
2398 }
2399
2400 DBG("Sending response (size: %d, retcode: %d)",
2401 cmd_ctx->lttng_msg_size, cmd_ctx->llm->ret_code);
2402 ret = send_unix_sock(sock, cmd_ctx->llm, cmd_ctx->lttng_msg_size);
2403 if (ret < 0) {
2404 ERR("Failed to send data back to client");
2405 }
2406
a2fb29a5 2407 clean_command_ctx(&cmd_ctx);
d6e4fca4
DG
2408
2409 /* End of transmission */
2410 close(sock);
1d4b027a
DG
2411 }
2412
2413error:
273ea72c
DG
2414 DBG("Client thread dying");
2415 if (client_sock) {
2416 close(client_sock);
2417 }
2418 if (sock) {
2419 close(sock);
2420 }
2421
2422 unlink(client_unix_sock_path);
2423
a2fb29a5 2424 clean_command_ctx(&cmd_ctx);
1d4b027a
DG
2425 return NULL;
2426}
2427
2428
fac6795d
DG
2429/*
2430 * usage function on stderr
2431 */
2432static void usage(void)
2433{
b716ce68 2434 fprintf(stderr, "Usage: %s OPTIONS\n\nOptions:\n", progname);
d6f42150
DG
2435 fprintf(stderr, " -h, --help Display this usage.\n");
2436 fprintf(stderr, " -c, --client-sock PATH Specify path for the client unix socket\n");
2437 fprintf(stderr, " -a, --apps-sock PATH Specify path for apps unix socket\n");
2438 fprintf(stderr, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
2439 fprintf(stderr, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
2440 fprintf(stderr, " -d, --daemonize Start as a daemon.\n");
2441 fprintf(stderr, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
2442 fprintf(stderr, " -V, --version Show version number.\n");
2443 fprintf(stderr, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
2444 fprintf(stderr, " -q, --quiet No output at all.\n");
2445 fprintf(stderr, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
31f73cc9 2446 fprintf(stderr, " --verbose-kconsumerd Verbose mode for kconsumerd. Activate DBG() macro.\n");
fac6795d
DG
2447}
2448
2449/*
2450 * daemon argument parsing
2451 */
2452static int parse_args(int argc, char **argv)
2453{
2454 int c;
2455
2456 static struct option long_options[] = {
2457 { "client-sock", 1, 0, 'c' },
2458 { "apps-sock", 1, 0, 'a' },
d6f42150
DG
2459 { "kconsumerd-cmd-sock", 1, 0, 0 },
2460 { "kconsumerd-err-sock", 1, 0, 0 },
fac6795d 2461 { "daemonize", 0, 0, 'd' },
5b8719f5 2462 { "sig-parent", 0, 0, 'S' },
fac6795d
DG
2463 { "help", 0, 0, 'h' },
2464 { "group", 1, 0, 'g' },
2465 { "version", 0, 0, 'V' },
75462a81 2466 { "quiet", 0, 0, 'q' },
3f9947db 2467 { "verbose", 0, 0, 'v' },
31f73cc9 2468 { "verbose-kconsumerd", 0, 0, 'Z' },
fac6795d
DG
2469 { NULL, 0, 0, 0 }
2470 };
2471
2472 while (1) {
2473 int option_index = 0;
31f73cc9 2474 c = getopt_long(argc, argv, "dhqvVS" "a:c:g:s:E:C:Z", long_options, &option_index);
fac6795d
DG
2475 if (c == -1) {
2476 break;
2477 }
2478
2479 switch (c) {
2480 case 0:
2481 fprintf(stderr, "option %s", long_options[option_index].name);
2482 if (optarg) {
2483 fprintf(stderr, " with arg %s\n", optarg);
2484 }
2485 break;
b716ce68 2486 case 'c':
fac6795d
DG
2487 snprintf(client_unix_sock_path, PATH_MAX, "%s", optarg);
2488 break;
2489 case 'a':
2490 snprintf(apps_unix_sock_path, PATH_MAX, "%s", optarg);
2491 break;
2492 case 'd':
2493 opt_daemon = 1;
2494 break;
2495 case 'g':
2496 opt_tracing_group = strdup(optarg);
2497 break;
2498 case 'h':
2499 usage();
2500 exit(EXIT_FAILURE);
2501 case 'V':
2502 fprintf(stdout, "%s\n", VERSION);
2503 exit(EXIT_SUCCESS);
5b8719f5
DG
2504 case 'S':
2505 opt_sig_parent = 1;
2506 break;
d6f42150
DG
2507 case 'E':
2508 snprintf(kconsumerd_err_unix_sock_path, PATH_MAX, "%s", optarg);
2509 break;
2510 case 'C':
2511 snprintf(kconsumerd_cmd_unix_sock_path, PATH_MAX, "%s", optarg);
2512 break;
75462a81
DG
2513 case 'q':
2514 opt_quiet = 1;
2515 break;
3f9947db 2516 case 'v':
53086306
DG
2517 /* Verbose level can increase using multiple -v */
2518 opt_verbose += 1;
3f9947db 2519 break;
31f73cc9
MD
2520 case 'Z':
2521 opt_verbose_kconsumerd += 1;
2522 break;
fac6795d
DG
2523 default:
2524 /* Unknown option or other error.
2525 * Error is printed by getopt, just return */
2526 return -1;
2527 }
2528 }
2529
2530 return 0;
2531}
2532
2533/*
d063d709 2534 * Creates the two needed socket by the daemon.
d6f42150
DG
2535 * apps_sock - The communication socket for all UST apps.
2536 * client_sock - The communication of the cli tool (lttng).
fac6795d 2537 */
cf3af59e 2538static int init_daemon_socket(void)
fac6795d
DG
2539{
2540 int ret = 0;
2541 mode_t old_umask;
2542
2543 old_umask = umask(0);
2544
2545 /* Create client tool unix socket */
d6f42150
DG
2546 client_sock = lttcomm_create_unix_sock(client_unix_sock_path);
2547 if (client_sock < 0) {
2548 ERR("Create unix sock failed: %s", client_unix_sock_path);
fac6795d
DG
2549 ret = -1;
2550 goto end;
2551 }
2552
2553 /* File permission MUST be 660 */
2554 ret = chmod(client_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
2555 if (ret < 0) {
d6f42150 2556 ERR("Set file permissions failed: %s", client_unix_sock_path);
fac6795d
DG
2557 perror("chmod");
2558 goto end;
2559 }
2560
2561 /* Create the application unix socket */
d6f42150
DG
2562 apps_sock = lttcomm_create_unix_sock(apps_unix_sock_path);
2563 if (apps_sock < 0) {
2564 ERR("Create unix sock failed: %s", apps_unix_sock_path);
fac6795d
DG
2565 ret = -1;
2566 goto end;
2567 }
2568
d6f42150 2569 /* File permission MUST be 666 */
fac6795d
DG
2570 ret = chmod(apps_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
2571 if (ret < 0) {
d6f42150 2572 ERR("Set file permissions failed: %s", apps_unix_sock_path);
fac6795d
DG
2573 perror("chmod");
2574 goto end;
2575 }
2576
2577end:
2578 umask(old_umask);
2579 return ret;
2580}
2581
2582/*
7d8234d9
MD
2583 * Check if the global socket is available, and if a daemon is answering
2584 * at the other side. If yes, error is returned.
fac6795d 2585 */
cf3af59e 2586static int check_existing_daemon(void)
fac6795d 2587{
7d8234d9
MD
2588 if (access(client_unix_sock_path, F_OK) < 0 &&
2589 access(apps_unix_sock_path, F_OK) < 0)
2590 return 0;
2591 /* Is there anybody out there ? */
2592 if (lttng_session_daemon_alive())
2593 return -EEXIST;
2594 else
2595 return 0;
fac6795d
DG
2596}
2597
fac6795d 2598/*
d063d709 2599 * Set the tracing group gid onto the client socket.
5e16da05 2600 *
d063d709
DG
2601 * Race window between mkdir and chown is OK because we are going from more
2602 * permissive (root.root) to les permissive (root.tracing).
fac6795d 2603 */
d6f42150 2604static int set_permissions(void)
fac6795d
DG
2605{
2606 int ret;
996b65c8 2607 gid_t gid;
fac6795d 2608
996b65c8
MD
2609 gid = allowed_group();
2610 if (gid < 0) {
a463f419
DG
2611 if (is_root) {
2612 WARN("No tracing group detected");
2613 ret = 0;
2614 } else {
2615 ERR("Missing tracing group. Aborting execution.");
2616 ret = -1;
2617 }
fac6795d
DG
2618 goto end;
2619 }
2620
d6f42150 2621 /* Set lttng run dir */
996b65c8 2622 ret = chown(LTTNG_RUNDIR, 0, gid);
d6f42150
DG
2623 if (ret < 0) {
2624 ERR("Unable to set group on " LTTNG_RUNDIR);
2625 perror("chown");
2626 }
2627
2628 /* lttng client socket path */
996b65c8 2629 ret = chown(client_unix_sock_path, 0, gid);
fac6795d 2630 if (ret < 0) {
d6f42150
DG
2631 ERR("Unable to set group on %s", client_unix_sock_path);
2632 perror("chown");
2633 }
2634
2635 /* kconsumerd error socket path */
996b65c8 2636 ret = chown(kconsumerd_err_unix_sock_path, 0, gid);
d6f42150
DG
2637 if (ret < 0) {
2638 ERR("Unable to set group on %s", kconsumerd_err_unix_sock_path);
fac6795d
DG
2639 perror("chown");
2640 }
2641
d6f42150 2642 DBG("All permissions are set");
e07ae692 2643
fac6795d
DG
2644end:
2645 return ret;
2646}
2647
7a485870 2648/*
d063d709 2649 * Create the pipe used to wake up the kernel thread.
7a485870
DG
2650 */
2651static int create_kernel_poll_pipe(void)
2652{
2653 return pipe2(kernel_poll_pipe, O_CLOEXEC);
2654}
2655
d6f42150 2656/*
d063d709 2657 * Create the lttng run directory needed for all global sockets and pipe.
d6f42150
DG
2658 */
2659static int create_lttng_rundir(void)
2660{
2661 int ret;
2662
2663 ret = mkdir(LTTNG_RUNDIR, S_IRWXU | S_IRWXG );
2664 if (ret < 0) {
b1f11e69
DG
2665 if (errno != EEXIST) {
2666 ERR("Unable to create " LTTNG_RUNDIR);
2667 goto error;
2668 } else {
2669 ret = 0;
2670 }
d6f42150
DG
2671 }
2672
2673error:
2674 return ret;
2675}
2676
2677/*
d063d709
DG
2678 * Setup sockets and directory needed by the kconsumerd communication with the
2679 * session daemon.
d6f42150
DG
2680 */
2681static int set_kconsumerd_sockets(void)
2682{
2683 int ret;
2684
2685 if (strlen(kconsumerd_err_unix_sock_path) == 0) {
2686 snprintf(kconsumerd_err_unix_sock_path, PATH_MAX, KCONSUMERD_ERR_SOCK_PATH);
2687 }
2688
2689 if (strlen(kconsumerd_cmd_unix_sock_path) == 0) {
2690 snprintf(kconsumerd_cmd_unix_sock_path, PATH_MAX, KCONSUMERD_CMD_SOCK_PATH);
2691 }
2692
2693 ret = mkdir(KCONSUMERD_PATH, S_IRWXU | S_IRWXG);
2694 if (ret < 0) {
6beb2242
DG
2695 if (errno != EEXIST) {
2696 ERR("Failed to create " KCONSUMERD_PATH);
2697 goto error;
2698 }
2699 ret = 0;
d6f42150
DG
2700 }
2701
2702 /* Create the kconsumerd error unix socket */
2703 kconsumerd_err_sock = lttcomm_create_unix_sock(kconsumerd_err_unix_sock_path);
2704 if (kconsumerd_err_sock < 0) {
2705 ERR("Create unix sock failed: %s", kconsumerd_err_unix_sock_path);
2706 ret = -1;
2707 goto error;
2708 }
2709
2710 /* File permission MUST be 660 */
2711 ret = chmod(kconsumerd_err_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
2712 if (ret < 0) {
2713 ERR("Set file permissions failed: %s", kconsumerd_err_unix_sock_path);
2714 perror("chmod");
2715 goto error;
2716 }
2717
2718error:
2719 return ret;
2720}
2721
fac6795d 2722/*
d063d709 2723 * Signal handler for the daemon
cf3af59e
MD
2724 *
2725 * Simply stop all worker threads, leaving main() return gracefully
2726 * after joining all threads and calling cleanup().
fac6795d
DG
2727 */
2728static void sighandler(int sig)
2729{
2730 switch (sig) {
cf3af59e
MD
2731 case SIGPIPE:
2732 DBG("SIGPIPE catched");
2733 return;
2734 case SIGINT:
2735 DBG("SIGINT catched");
2736 stop_threads();
2737 break;
2738 case SIGTERM:
2739 DBG("SIGTERM catched");
2740 stop_threads();
2741 break;
2742 default:
2743 break;
fac6795d 2744 }
fac6795d
DG
2745}
2746
2747/*
d063d709 2748 * Setup signal handler for :
1d4b027a 2749 * SIGINT, SIGTERM, SIGPIPE
fac6795d 2750 */
1d4b027a 2751static int set_signal_handler(void)
fac6795d 2752{
1d4b027a
DG
2753 int ret = 0;
2754 struct sigaction sa;
2755 sigset_t sigset;
fac6795d 2756
1d4b027a
DG
2757 if ((ret = sigemptyset(&sigset)) < 0) {
2758 perror("sigemptyset");
2759 return ret;
2760 }
d6f42150 2761
1d4b027a
DG
2762 sa.sa_handler = sighandler;
2763 sa.sa_mask = sigset;
2764 sa.sa_flags = 0;
2765 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
2766 perror("sigaction");
2767 return ret;
d6f42150
DG
2768 }
2769
1d4b027a
DG
2770 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
2771 perror("sigaction");
2772 return ret;
d6f42150 2773 }
aaf26714 2774
1d4b027a
DG
2775 if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) {
2776 perror("sigaction");
2777 return ret;
8c0faa1d
DG
2778 }
2779
1d4b027a
DG
2780 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
2781
2782 return ret;
fac6795d
DG
2783}
2784
f3ed775e 2785/*
d063d709
DG
2786 * Set open files limit to unlimited. This daemon can open a large number of
2787 * file descriptors in order to consumer multiple kernel traces.
f3ed775e
DG
2788 */
2789static void set_ulimit(void)
2790{
2791 int ret;
2792 struct rlimit lim;
2793
a88df331 2794 /* The kernel does not allowed an infinite limit for open files */
f3ed775e
DG
2795 lim.rlim_cur = 65535;
2796 lim.rlim_max = 65535;
2797
2798 ret = setrlimit(RLIMIT_NOFILE, &lim);
2799 if (ret < 0) {
2800 perror("failed to set open files limit");
2801 }
2802}
2803
fac6795d
DG
2804/*
2805 * main
2806 */
2807int main(int argc, char **argv)
2808{
fac6795d
DG
2809 int ret = 0;
2810 void *status;
b082db07 2811 const char *home_path;
fac6795d 2812
273ea72c 2813 /* Create thread quit pipe */
cf3af59e
MD
2814 if ((ret = init_thread_quit_pipe()) < 0) {
2815 goto error;
273ea72c
DG
2816 }
2817
fac6795d
DG
2818 /* Parse arguments */
2819 progname = argv[0];
2820 if ((ret = parse_args(argc, argv) < 0)) {
cf3af59e 2821 goto error;
fac6795d
DG
2822 }
2823
2824 /* Daemonize */
2825 if (opt_daemon) {
53094c05
DG
2826 ret = daemon(0, 0);
2827 if (ret < 0) {
2828 perror("daemon");
cf3af59e 2829 goto error;
53094c05 2830 }
fac6795d
DG
2831 }
2832
2833 /* Check if daemon is UID = 0 */
2834 is_root = !getuid();
2835
fac6795d 2836 if (is_root) {
d6f42150
DG
2837 ret = create_lttng_rundir();
2838 if (ret < 0) {
cf3af59e 2839 goto error;
d6f42150
DG
2840 }
2841
fac6795d 2842 if (strlen(apps_unix_sock_path) == 0) {
d6f42150
DG
2843 snprintf(apps_unix_sock_path, PATH_MAX,
2844 DEFAULT_GLOBAL_APPS_UNIX_SOCK);
fac6795d
DG
2845 }
2846
2847 if (strlen(client_unix_sock_path) == 0) {
d6f42150
DG
2848 snprintf(client_unix_sock_path, PATH_MAX,
2849 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK);
2850 }
fac6795d 2851 } else {
b082db07
DG
2852 home_path = get_home_dir();
2853 if (home_path == NULL) {
273ea72c
DG
2854 /* TODO: Add --socket PATH option */
2855 ERR("Can't get HOME directory for sockets creation.");
cf3af59e
MD
2856 ret = -EPERM;
2857 goto error;
b082db07
DG
2858 }
2859
fac6795d 2860 if (strlen(apps_unix_sock_path) == 0) {
d6f42150 2861 snprintf(apps_unix_sock_path, PATH_MAX,
b082db07 2862 DEFAULT_HOME_APPS_UNIX_SOCK, home_path);
fac6795d
DG
2863 }
2864
2865 /* Set the cli tool unix socket path */
2866 if (strlen(client_unix_sock_path) == 0) {
d6f42150 2867 snprintf(client_unix_sock_path, PATH_MAX,
b082db07 2868 DEFAULT_HOME_CLIENT_UNIX_SOCK, home_path);
fac6795d
DG
2869 }
2870 }
2871
847177cd
DG
2872 DBG("Client socket path %s", client_unix_sock_path);
2873 DBG("Application socket path %s", apps_unix_sock_path);
2874
273ea72c 2875 /*
7d8234d9 2876 * See if daemon already exist.
fac6795d 2877 */
7d8234d9 2878 if ((ret = check_existing_daemon()) < 0) {
75462a81 2879 ERR("Already running daemon.\n");
273ea72c 2880 /*
cf3af59e
MD
2881 * We do not goto exit because we must not cleanup()
2882 * because a daemon is already running.
ab118b20 2883 */
cf3af59e 2884 goto error;
a88df331
DG
2885 }
2886
2887 /* After this point, we can safely call cleanup() so goto error is used */
2888
2889 /*
2890 * These actions must be executed as root. We do that *after* setting up
2891 * the sockets path because we MUST make the check for another daemon using
2892 * those paths *before* trying to set the kernel consumer sockets and init
2893 * kernel tracer.
2894 */
2895 if (is_root) {
2896 ret = set_kconsumerd_sockets();
2897 if (ret < 0) {
cf3af59e 2898 goto exit;
a88df331
DG
2899 }
2900
2901 /* Setup kernel tracer */
2902 init_kernel_tracer();
2903
2904 /* Set ulimit for open files */
2905 set_ulimit();
fac6795d
DG
2906 }
2907
cf3af59e
MD
2908 if ((ret = set_signal_handler()) < 0) {
2909 goto exit;
fac6795d
DG
2910 }
2911
d6f42150 2912 /* Setup the needed unix socket */
cf3af59e
MD
2913 if ((ret = init_daemon_socket()) < 0) {
2914 goto exit;
fac6795d
DG
2915 }
2916
2917 /* Set credentials to socket */
cf3af59e
MD
2918 if (is_root && ((ret = set_permissions()) < 0)) {
2919 goto exit;
fac6795d
DG
2920 }
2921
5b8719f5
DG
2922 /* Get parent pid if -S, --sig-parent is specified. */
2923 if (opt_sig_parent) {
2924 ppid = getppid();
2925 }
2926
7a485870 2927 /* Setup the kernel pipe for waking up the kernel thread */
cf3af59e
MD
2928 if ((ret = create_kernel_poll_pipe()) < 0) {
2929 goto exit;
7a485870
DG
2930 }
2931
273ea72c
DG
2932 /*
2933 * Get session list pointer. This pointer MUST NOT be free().
2934 * This list is statically declared in session.c
2935 */
b5541356
DG
2936 session_list_ptr = get_session_list();
2937
cf3af59e
MD
2938 /* Create thread to manage the client socket */
2939 ret = pthread_create(&client_thread, NULL, thread_manage_clients, (void *) NULL);
2940 if (ret != 0) {
2941 perror("pthread_create");
2942 goto exit_client;
2943 }
fac6795d 2944
cf3af59e
MD
2945 /* Create thread to manage application socket */
2946 ret = pthread_create(&apps_thread, NULL, thread_manage_apps, (void *) NULL);
2947 if (ret != 0) {
2948 perror("pthread_create");
2949 goto exit_apps;
2950 }
fac6795d 2951
cf3af59e
MD
2952 /* Create kernel thread to manage kernel event */
2953 ret = pthread_create(&kernel_thread, NULL, thread_manage_kernel, (void *) NULL);
2954 if (ret != 0) {
2955 perror("pthread_create");
2956 goto exit_kernel;
2957 }
7a485870 2958
cf3af59e
MD
2959 ret = pthread_join(kernel_thread, &status);
2960 if (ret != 0) {
2961 perror("pthread_join");
2962 goto error; /* join error, exit without cleanup */
fac6795d
DG
2963 }
2964
cf3af59e
MD
2965exit_kernel:
2966 ret = pthread_join(apps_thread, &status);
2967 if (ret != 0) {
2968 perror("pthread_join");
2969 goto error; /* join error, exit without cleanup */
2970 }
fac6795d 2971
cf3af59e
MD
2972exit_apps:
2973 ret = pthread_join(client_thread, &status);
2974 if (ret != 0) {
2975 perror("pthread_join");
2976 goto error; /* join error, exit without cleanup */
2977 }
2978
2979 ret = join_kconsumerd_thread();
2980 if (ret != 0) {
2981 perror("join_kconsumerd");
2982 goto error; /* join error, exit without cleanup */
2983 }
a88df331 2984
cf3af59e 2985exit_client:
a88df331 2986exit:
cf3af59e
MD
2987 /*
2988 * cleanup() is called when no other thread is running.
2989 */
2990 cleanup();
2991 if (!ret)
2992 exit(EXIT_SUCCESS);
2993error:
5e16da05 2994 exit(EXIT_FAILURE);
fac6795d 2995}
This page took 0.437859 seconds and 4 git commands to generate.