Modify default kernel channel size/number
[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
712ea556
DG
756 /* Wait for any kconsumerd error */
757 ret = lttcomm_recv_unix_sock(sock, &code, sizeof(enum lttcomm_return_code));
758 if (ret <= 0) {
759 ERR("Kconsumerd closed the command socket");
760 goto error;
6f61d021 761 }
1d4b027a 762
712ea556
DG
763 ERR("Kconsumerd return code : %s", lttcomm_get_readable_code(-code));
764
1d4b027a 765error:
6f61d021 766 DBG("Kconsumerd thread dying");
273ea72c
DG
767 if (kconsumerd_err_sock) {
768 close(kconsumerd_err_sock);
769 }
770 if (kconsumerd_cmd_sock) {
771 close(kconsumerd_cmd_sock);
772 }
773 if (sock) {
774 close(sock);
775 }
776
777 unlink(kconsumerd_err_unix_sock_path);
778 unlink(kconsumerd_cmd_unix_sock_path);
779
780 kconsumerd_pid = 0;
1d4b027a
DG
781 return NULL;
782}
783
784/*
1d4b027a
DG
785 * This thread manage the application socket communication
786 */
787static void *thread_manage_apps(void *data)
788{
273ea72c
DG
789 int sock = 0, ret;
790 struct pollfd pollfd[2];
1d4b027a
DG
791
792 /* TODO: Something more elegant is needed but fine for now */
793 /* FIXME: change all types to either uint8_t, uint32_t, uint64_t
794 * for 32-bit vs 64-bit compat processes. */
795 /* replicate in ust with version number */
796 struct {
797 int reg; /* 1:register, 0:unregister */
798 pid_t pid;
799 uid_t uid;
800 } reg_msg;
801
802 DBG("[thread] Manage apps started");
803
804 ret = lttcomm_listen_unix_sock(apps_sock);
805 if (ret < 0) {
806 goto error;
807 }
808
273ea72c
DG
809 /* First fd is always the quit pipe */
810 pollfd[0].fd = thread_quit_pipe[0];
811
812 /* Apps socket */
813 pollfd[1].fd = apps_sock;
814 pollfd[1].events = POLLIN;
815
1d4b027a
DG
816 /* Notify all applications to register */
817 notify_apps(default_global_apps_pipe);
818
819 while (1) {
820 DBG("Accepting application registration");
273ea72c
DG
821
822 /* Inifinite blocking call, waiting for transmission */
823 ret = poll(pollfd, 2, -1);
824 if (ret < 0) {
825 perror("poll apps thread");
826 goto error;
827 }
828
829 /* Thread quit pipe has been closed. Killing thread. */
830 if (pollfd[0].revents == POLLNVAL) {
831 goto error;
832 } else if (pollfd[1].revents == POLLERR) {
833 ERR("Apps socket poll error");
834 goto error;
835 }
836
1d4b027a
DG
837 sock = lttcomm_accept_unix_sock(apps_sock);
838 if (sock < 0) {
839 goto error;
840 }
841
273ea72c
DG
842 /*
843 * Basic recv here to handle the very simple data
1d4b027a
DG
844 * that the libust send to register (reg_msg).
845 */
846 ret = recv(sock, &reg_msg, sizeof(reg_msg), 0);
847 if (ret < 0) {
848 perror("recv");
849 continue;
850 }
851
852 /* Add application to the global traceable list */
853 if (reg_msg.reg == 1) {
854 /* Registering */
855 ret = register_traceable_app(reg_msg.pid, reg_msg.uid);
856 if (ret < 0) {
857 /* register_traceable_app only return an error with
858 * ENOMEM. At this point, we better stop everything.
859 */
860 goto error;
861 }
862 } else {
863 /* Unregistering */
864 unregister_traceable_app(reg_msg.pid);
865 }
866 }
867
868error:
273ea72c
DG
869 DBG("Apps thread dying");
870 if (apps_sock) {
871 close(apps_sock);
872 }
873 if (sock) {
874 close(sock);
875 }
1d4b027a 876
273ea72c 877 unlink(apps_unix_sock_path);
1d4b027a
DG
878 return NULL;
879}
880
8c0faa1d 881/*
d063d709
DG
882 * Start the thread_manage_kconsumerd. This must be done after a kconsumerd
883 * exec or it will fails.
8c0faa1d 884 */
693bd40b 885static int spawn_kconsumerd_thread(void)
8c0faa1d
DG
886{
887 int ret;
888
889 /* Setup semaphore */
890 sem_init(&kconsumerd_sem, 0, 0);
891
1d4b027a 892 ret = pthread_create(&kconsumerd_thread, NULL, thread_manage_kconsumerd, (void *) NULL);
8c0faa1d
DG
893 if (ret != 0) {
894 perror("pthread_create kconsumerd");
895 goto error;
896 }
897
693bd40b 898 /* Wait for the kconsumerd thread to be ready */
8c0faa1d
DG
899 sem_wait(&kconsumerd_sem);
900
712ea556
DG
901 if (kconsumerd_pid == 0) {
902 ERR("Kconsumerd did not start");
903 goto error;
904 }
905
8c0faa1d
DG
906 return 0;
907
908error:
712ea556 909 ret = LTTCOMM_KERN_CONSUMER_FAIL;
8c0faa1d
DG
910 return ret;
911}
912
d9800920
DG
913/*
914 * Join kernel consumer thread
915 */
cf3af59e
MD
916static int join_kconsumerd_thread(void)
917{
918 void *status;
919 int ret;
920
921 if (kconsumerd_pid != 0) {
922 ret = kill(kconsumerd_pid, SIGTERM);
923 if (ret) {
924 ERR("Error killing kconsumerd");
925 return ret;
926 }
927 return pthread_join(kconsumerd_thread, &status);
928 } else {
929 return 0;
930 }
931}
932
8c0faa1d 933/*
d063d709 934 * Fork and exec a kernel consumer daemon (kconsumerd).
8c0faa1d 935 *
d063d709 936 * Return pid if successful else -1.
8c0faa1d 937 */
693bd40b 938static pid_t spawn_kconsumerd(void)
8c0faa1d
DG
939{
940 int ret;
941 pid_t pid;
53086306 942 const char *verbosity;
8c0faa1d 943
c49dc785
DG
944 DBG("Spawning kconsumerd");
945
8c0faa1d
DG
946 pid = fork();
947 if (pid == 0) {
948 /*
949 * Exec kconsumerd.
950 */
31f73cc9 951 if (opt_verbose > 1 || opt_verbose_kconsumerd) {
53086306
DG
952 verbosity = "--verbose";
953 } else {
954 verbosity = "--quiet";
955 }
956 execl(INSTALL_BIN_PATH "/ltt-kconsumerd", "ltt-kconsumerd", verbosity, NULL);
8c0faa1d
DG
957 if (errno != 0) {
958 perror("kernel start consumer exec");
959 }
960 exit(EXIT_FAILURE);
961 } else if (pid > 0) {
962 ret = pid;
963 goto error;
964 } else {
965 perror("kernel start consumer fork");
966 ret = -errno;
967 goto error;
968 }
969
970error:
971 return ret;
972}
973
693bd40b 974/*
d063d709 975 * Spawn the kconsumerd daemon and session daemon thread.
693bd40b
DG
976 */
977static int start_kconsumerd(void)
978{
979 int ret;
980
693bd40b 981 pthread_mutex_lock(&kconsumerd_pid_mutex);
c49dc785 982 if (kconsumerd_pid != 0) {
817153bb 983 pthread_mutex_unlock(&kconsumerd_pid_mutex);
c49dc785
DG
984 goto end;
985 }
693bd40b 986
c49dc785
DG
987 ret = spawn_kconsumerd();
988 if (ret < 0) {
989 ERR("Spawning kconsumerd failed");
990 ret = LTTCOMM_KERN_CONSUMER_FAIL;
991 pthread_mutex_unlock(&kconsumerd_pid_mutex);
992 goto error;
693bd40b 993 }
c49dc785
DG
994
995 /* Setting up the global kconsumerd_pid */
996 kconsumerd_pid = ret;
693bd40b
DG
997 pthread_mutex_unlock(&kconsumerd_pid_mutex);
998
6f61d021
DG
999 DBG("Kconsumerd pid %d", ret);
1000
693bd40b 1001 DBG("Spawning kconsumerd thread");
693bd40b
DG
1002 ret = spawn_kconsumerd_thread();
1003 if (ret < 0) {
1004 ERR("Fatal error spawning kconsumerd thread");
693bd40b
DG
1005 goto error;
1006 }
1007
c49dc785 1008end:
693bd40b
DG
1009 return 0;
1010
1011error:
1012 return ret;
1013}
1014
b73401da 1015/*
d063d709 1016 * modprobe_kernel_modules
b73401da
DG
1017 */
1018static int modprobe_kernel_modules(void)
1019{
ab147185 1020 int ret = 0, i;
b73401da
DG
1021 char modprobe[256];
1022
ab147185
MD
1023 for (i = 0; i < ARRAY_SIZE(kernel_modules_list); i++) {
1024 ret = snprintf(modprobe, sizeof(modprobe),
1025 "/sbin/modprobe %s%s",
1026 kernel_modules_list[i].required ? "" : "--quiet ",
1027 kernel_modules_list[i].name);
b73401da
DG
1028 if (ret < 0) {
1029 perror("snprintf modprobe");
1030 goto error;
1031 }
ab147185 1032 modprobe[sizeof(modprobe) - 1] = '\0';
b73401da 1033 ret = system(modprobe);
ab147185
MD
1034 if (ret == -1) {
1035 ERR("Unable to launch modprobe for module %s",
1036 kernel_modules_list[i].name);
1037 } else if (kernel_modules_list[i].required
d9800920 1038 && WEXITSTATUS(ret) != 0) {
ab147185
MD
1039 ERR("Unable to load module %s",
1040 kernel_modules_list[i].name);
1041 } else {
1042 DBG("Modprobe successfully %s",
1043 kernel_modules_list[i].name);
1044 }
1045 }
1046
1047error:
1048 return ret;
1049}
1050
1051/*
1052 * modprobe_remove_kernel_modules
1053 * Remove modules in reverse load order.
1054 */
1055static int modprobe_remove_kernel_modules(void)
1056{
1057 int ret = 0, i;
1058 char modprobe[256];
1059
1060 for (i = ARRAY_SIZE(kernel_modules_list) - 1; i >= 0; i--) {
1061 ret = snprintf(modprobe, sizeof(modprobe),
1062 "/sbin/modprobe --remove --quiet %s",
1063 kernel_modules_list[i].name);
b73401da 1064 if (ret < 0) {
ab147185
MD
1065 perror("snprintf modprobe --remove");
1066 goto error;
1067 }
1068 modprobe[sizeof(modprobe) - 1] = '\0';
1069 ret = system(modprobe);
1070 if (ret == -1) {
1071 ERR("Unable to launch modprobe --remove for module %s",
1072 kernel_modules_list[i].name);
1073 } else if (kernel_modules_list[i].required
d9800920 1074 && WEXITSTATUS(ret) != 0) {
ab147185
MD
1075 ERR("Unable to remove module %s",
1076 kernel_modules_list[i].name);
1077 } else {
1078 DBG("Modprobe removal successful %s",
1079 kernel_modules_list[i].name);
b73401da 1080 }
b73401da
DG
1081 }
1082
1083error:
1084 return ret;
1085}
1086
1087/*
d063d709 1088 * mount_debugfs
b73401da
DG
1089 */
1090static int mount_debugfs(char *path)
1091{
1092 int ret;
1093 char *type = "debugfs";
1094
996b65c8 1095 ret = mkdir_recursive(path, S_IRWXU | S_IRWXG, geteuid(), getegid());
b73401da
DG
1096 if (ret < 0) {
1097 goto error;
1098 }
1099
1100 ret = mount(type, path, type, 0, NULL);
1101 if (ret < 0) {
1102 perror("mount debugfs");
1103 goto error;
1104 }
1105
1106 DBG("Mounted debugfs successfully at %s", path);
1107
1108error:
1109 return ret;
1110}
1111
8c0faa1d 1112/*
d063d709 1113 * Setup necessary data for kernel tracer action.
8c0faa1d 1114 */
f3ed775e 1115static void init_kernel_tracer(void)
8c0faa1d 1116{
b73401da
DG
1117 int ret;
1118 char *proc_mounts = "/proc/mounts";
1119 char line[256];
1120 char *debugfs_path = NULL, *lttng_path;
1121 FILE *fp;
1122
1123 /* Detect debugfs */
1124 fp = fopen(proc_mounts, "r");
1125 if (fp == NULL) {
1126 ERR("Unable to probe %s", proc_mounts);
1127 goto error;
1128 }
1129
1130 while (fgets(line, sizeof(line), fp) != NULL) {
1131 if (strstr(line, "debugfs") != NULL) {
1132 /* Remove first string */
1133 strtok(line, " ");
1134 /* Dup string here so we can reuse line later on */
1135 debugfs_path = strdup(strtok(NULL, " "));
1136 DBG("Got debugfs path : %s", debugfs_path);
1137 break;
1138 }
1139 }
1140
1141 fclose(fp);
1142
1143 /* Mount debugfs if needded */
1144 if (debugfs_path == NULL) {
1145 ret = asprintf(&debugfs_path, "/mnt/debugfs");
1146 if (ret < 0) {
1147 perror("asprintf debugfs path");
1148 goto error;
1149 }
1150 ret = mount_debugfs(debugfs_path);
1151 if (ret < 0) {
1152 goto error;
1153 }
1154 }
1155
1156 /* Modprobe lttng kernel modules */
1157 ret = modprobe_kernel_modules();
1158 if (ret < 0) {
1159 goto error;
1160 }
1161
1162 /* Setup lttng kernel path */
1163 ret = asprintf(&lttng_path, "%s/lttng", debugfs_path);
1164 if (ret < 0) {
1165 perror("asprintf lttng path");
1166 goto error;
1167 }
1168
1169 /* Open debugfs lttng */
1170 kernel_tracer_fd = open(lttng_path, O_RDWR);
f3ed775e 1171 if (kernel_tracer_fd < 0) {
b73401da
DG
1172 DBG("Failed to open %s", lttng_path);
1173 goto error;
8c0faa1d
DG
1174 }
1175
b73401da
DG
1176 free(lttng_path);
1177 free(debugfs_path);
f3ed775e 1178 DBG("Kernel tracer fd %d", kernel_tracer_fd);
b73401da
DG
1179 return;
1180
1181error:
1182 if (lttng_path) {
1183 free(lttng_path);
1184 }
1185 if (debugfs_path) {
1186 free(debugfs_path);
1187 }
1188 WARN("No kernel tracer available");
1189 kernel_tracer_fd = 0;
1190 return;
f3ed775e 1191}
33a2b854 1192
f3ed775e 1193/*
d063d709
DG
1194 * Start tracing by creating trace directory and sending FDs to the kernel
1195 * consumer.
f3ed775e
DG
1196 */
1197static int start_kernel_trace(struct ltt_kernel_session *session)
1198{
f40799e8 1199 int ret = 0;
8c0faa1d 1200
f3ed775e 1201 if (session->kconsumer_fds_sent == 0) {
d9800920
DG
1202 /*
1203 * Assign default kernel consumer if no consumer assigned to the kernel
1204 * session. At this point, it's NOT suppose to be 0 but this is an extra
1205 * security check.
1206 */
1207 if (session->consumer_fd == 0) {
1208 session->consumer_fd = kconsumerd_cmd_sock;
1209 }
1210
1211 ret = send_kconsumerd_fds(session);
f3ed775e
DG
1212 if (ret < 0) {
1213 ERR("Send kconsumerd fds failed");
1214 ret = LTTCOMM_KERN_CONSUMER_FAIL;
1215 goto error;
1216 }
1d4b027a 1217
f3ed775e
DG
1218 session->kconsumer_fds_sent = 1;
1219 }
1d4b027a
DG
1220
1221error:
1222 return ret;
8c0faa1d
DG
1223}
1224
7a485870
DG
1225/*
1226 * Notify kernel thread to update it's pollfd.
1227 */
1228static int notify_kernel_pollfd(void)
1229{
1230 int ret;
1231
1232 /* Inform kernel thread of the new kernel channel */
1233 ret = write(kernel_poll_pipe[1], "!", 1);
1234 if (ret < 0) {
1235 perror("write kernel poll pipe");
1236 }
1237
1238 return ret;
1239}
1240
8c0faa1d 1241/*
d063d709 1242 * Allocate a channel structure and fill it.
8c0faa1d 1243 */
b389abbe
MD
1244static struct lttng_channel *init_default_channel(enum lttng_domain_type domain_type,
1245 char *name)
8c0faa1d 1246{
f3ed775e 1247 struct lttng_channel *chan;
1d4b027a 1248
f3ed775e
DG
1249 chan = malloc(sizeof(struct lttng_channel));
1250 if (chan == NULL) {
1251 perror("init channel malloc");
1252 goto error;
8c0faa1d 1253 }
1d4b027a 1254
ed8f384d 1255 if (snprintf(chan->name, NAME_MAX, "%s", name) < 0) {
9f19cc17 1256 perror("snprintf channel name");
b389abbe 1257 goto error;
f3ed775e
DG
1258 }
1259
1260 chan->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
f3ed775e
DG
1261 chan->attr.switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
1262 chan->attr.read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
1d4b027a 1263
b389abbe
MD
1264 switch (domain_type) {
1265 case LTTNG_DOMAIN_KERNEL:
1266 chan->attr.subbuf_size = DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE;
1267 chan->attr.num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM;
1268 chan->attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
1269 break;
1270 /* TODO: add UST */
1271 default:
1272 goto error; /* Not implemented */
1273 }
1274
f3ed775e 1275 return chan;
b389abbe
MD
1276
1277error:
1278 free(chan);
1279 return NULL;
8c0faa1d
DG
1280}
1281
333f285e 1282/*
d063d709 1283 * Create a kernel tracer session then create the default channel.
333f285e 1284 */
f3ed775e 1285static int create_kernel_session(struct ltt_session *session)
333f285e 1286{
f3ed775e 1287 int ret;
f3ed775e
DG
1288
1289 DBG("Creating kernel session");
1290
1291 ret = kernel_create_session(session, kernel_tracer_fd);
1292 if (ret < 0) {
1293 ret = LTTCOMM_KERN_SESS_FAIL;
1294 goto error;
333f285e
DG
1295 }
1296
d9800920
DG
1297 /* Set kernel consumer socket fd */
1298 if (kconsumerd_cmd_sock) {
1299 session->kernel_session->consumer_fd = kconsumerd_cmd_sock;
1300 }
1301
63053e7c
DG
1302 ret = asprintf(&session->kernel_session->trace_path, "%s/kernel",
1303 session->path);
1304 if (ret < 0) {
1305 perror("asprintf kernel traces path");
1306 goto error;
1307 }
1308
1309 ret = mkdir_recursive(session->kernel_session->trace_path,
1310 S_IRWXU | S_IRWXG, geteuid(), allowed_group());
7a485870 1311 if (ret < 0) {
996b65c8 1312 if (ret != -EEXIST) {
7a485870
DG
1313 ERR("Trace directory creation error");
1314 goto error;
1315 }
1316 }
1317
f3ed775e
DG
1318error:
1319 return ret;
333f285e
DG
1320}
1321
6c9cc2ab
DG
1322/*
1323 * Using the session list, filled a lttng_session array to send back to the
1324 * client for session listing.
1325 *
1326 * The session list lock MUST be acquired before calling this function. Use
1327 * lock_session_list() and unlock_session_list().
1328 */
1329static void list_lttng_sessions(struct lttng_session *sessions)
1330{
1331 int i = 0;
1332 struct ltt_session *session;
1333
1334 DBG("Getting all available session");
1335 /*
1336 * Iterate over session list and append data after the control struct in
1337 * the buffer.
1338 */
1339 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
1340 strncpy(sessions[i].path, session->path, PATH_MAX);
99497cd0 1341 sessions[i].path[PATH_MAX - 1] = '\0';
6c9cc2ab 1342 strncpy(sessions[i].name, session->name, NAME_MAX);
99497cd0 1343 sessions[i].name[NAME_MAX - 1] = '\0';
6c9cc2ab
DG
1344 i++;
1345 }
1346}
1347
9f19cc17
DG
1348/*
1349 * Fill lttng_channel array of all channels.
1350 */
1351static void list_lttng_channels(struct ltt_session *session,
1352 struct lttng_channel *channels)
1353{
1354 int i = 0;
1355 struct ltt_kernel_channel *kchan;
1356
1357 DBG("Listing channels for session %s", session->name);
1358
1359 /* Kernel channels */
1360 if (session->kernel_session != NULL) {
1361 cds_list_for_each_entry(kchan, &session->kernel_session->channel_list.head, list) {
1362 /* Copy lttng_channel struct to array */
1363 memcpy(&channels[i], kchan->channel, sizeof(struct lttng_channel));
1364 channels[i].enabled = kchan->enabled;
1365 i++;
1366 }
1367 }
1368
1369 /* TODO: Missing UST listing */
1370}
1371
1372/*
1373 * Fill lttng_event array of all events in the channel.
1374 */
1375static void list_lttng_events(struct ltt_kernel_channel *kchan,
1376 struct lttng_event *events)
1377{
1378 /*
1379 * TODO: This is ONLY kernel. Need UST support.
1380 */
1381 int i = 0;
1382 struct ltt_kernel_event *event;
1383
1384 DBG("Listing events for channel %s", kchan->channel->name);
1385
1386 /* Kernel channels */
1387 cds_list_for_each_entry(event, &kchan->events_list.head , list) {
1388 strncpy(events[i].name, event->event->name, LTTNG_SYMBOL_NAME_LEN);
99497cd0 1389 events[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
9f19cc17
DG
1390 events[i].enabled = event->enabled;
1391 switch (event->event->instrumentation) {
1392 case LTTNG_KERNEL_TRACEPOINT:
1393 events[i].type = LTTNG_EVENT_TRACEPOINT;
1394 break;
1395 case LTTNG_KERNEL_KPROBE:
1396 case LTTNG_KERNEL_KRETPROBE:
1397 events[i].type = LTTNG_EVENT_PROBE;
1398 memcpy(&events[i].attr.probe, &event->event->u.kprobe,
1399 sizeof(struct lttng_kernel_kprobe));
1400 break;
1401 case LTTNG_KERNEL_FUNCTION:
1402 events[i].type = LTTNG_EVENT_FUNCTION;
1403 memcpy(&events[i].attr.ftrace, &event->event->u.ftrace,
1404 sizeof(struct lttng_kernel_function));
1405 break;
1406 }
1407 i++;
1408 }
1409}
1410
fac6795d 1411/*
d063d709
DG
1412 * Process the command requested by the lttng client within the command
1413 * context structure. This function make sure that the return structure (llm)
1414 * is set and ready for transmission before returning.
fac6795d 1415 *
e065084a 1416 * Return any error encountered or 0 for success.
fac6795d 1417 */
5461b305 1418static int process_client_msg(struct command_ctx *cmd_ctx)
fac6795d 1419{
f40799e8 1420 int ret = LTTCOMM_OK;
fac6795d 1421
5461b305 1422 DBG("Processing client command %d", cmd_ctx->lsm->cmd_type);
fac6795d 1423
63053e7c
DG
1424 /*
1425 * Commands that DO NOT need a session.
1426 */
5461b305 1427 switch (cmd_ctx->lsm->cmd_type) {
5e16da05
MD
1428 case LTTNG_CREATE_SESSION:
1429 case LTTNG_LIST_SESSIONS:
052da939 1430 case LTTNG_LIST_TRACEPOINTS:
d0254c7c 1431 case LTTNG_CALIBRATE:
5e16da05
MD
1432 break;
1433 default:
42abccdb
DG
1434 DBG("Getting session %s by name", cmd_ctx->lsm->session.name);
1435 cmd_ctx->session = find_session_by_name(cmd_ctx->lsm->session.name);
5461b305 1436 if (cmd_ctx->session == NULL) {
f3ed775e 1437 /* If session name not found */
42abccdb 1438 if (cmd_ctx->lsm->session.name != NULL) {
f3ed775e
DG
1439 ret = LTTCOMM_SESS_NOT_FOUND;
1440 } else { /* If no session name specified */
1441 ret = LTTCOMM_SELECT_SESS;
1442 }
5461b305 1443 goto error;
b5541356
DG
1444 } else {
1445 /* Acquire lock for the session */
1446 lock_session(cmd_ctx->session);
379473d2 1447 }
5e16da05 1448 break;
379473d2
DG
1449 }
1450
f3ed775e 1451 /*
d0254c7c 1452 * Check domain type for specific "pre-action".
f3ed775e 1453 */
0d0c377a
DG
1454 switch (cmd_ctx->lsm->domain.type) {
1455 case LTTNG_DOMAIN_KERNEL:
333f285e 1456 /* Kernel tracer check */
20fe2104 1457 if (kernel_tracer_fd == 0) {
333f285e
DG
1458 init_kernel_tracer();
1459 if (kernel_tracer_fd == 0) {
1460 ret = LTTCOMM_KERN_NA;
1461 goto error;
1462 }
20fe2104 1463 }
f3ed775e
DG
1464
1465 /* Need a session for kernel command */
d0254c7c 1466 switch (cmd_ctx->lsm->cmd_type) {
d9800920 1467 case LTTNG_CALIBRATE:
d0254c7c
MD
1468 case LTTNG_CREATE_SESSION:
1469 case LTTNG_LIST_SESSIONS:
1470 case LTTNG_LIST_TRACEPOINTS:
d0254c7c
MD
1471 break;
1472 default:
1473 if (cmd_ctx->session->kernel_session == NULL) {
1474 ret = create_kernel_session(cmd_ctx->session);
f3ed775e 1475 if (ret < 0) {
d0254c7c 1476 ret = LTTCOMM_KERN_SESS_FAIL;
f3ed775e
DG
1477 goto error;
1478 }
d0254c7c
MD
1479
1480 /* Start the kernel consumer daemon */
d9800920
DG
1481
1482 if (kconsumerd_pid == 0 &&
1483 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
d0254c7c
MD
1484 ret = start_kconsumerd();
1485 if (ret < 0) {
1486 goto error;
1487 }
1488 }
f3ed775e
DG
1489 }
1490 }
0d0c377a
DG
1491 break;
1492 default:
1493 break;
20fe2104
DG
1494 }
1495
fac6795d 1496 /* Process by command type */
5461b305 1497 switch (cmd_ctx->lsm->cmd_type) {
b579acd9 1498 case LTTNG_ADD_CONTEXT:
d65106b1 1499 {
b579acd9 1500 struct lttng_kernel_context kctx;
d65106b1
DG
1501
1502 /* Setup lttng message with no payload */
1503 ret = setup_lttng_msg(cmd_ctx, 0);
1504 if (ret < 0) {
1505 goto setup_error;
1506 }
1507
b579acd9
DG
1508 switch (cmd_ctx->lsm->domain.type) {
1509 case LTTNG_DOMAIN_KERNEL:
1510 /* Create Kernel context */
1511 kctx.ctx = cmd_ctx->lsm->u.context.ctx.ctx;
1512 kctx.u.perf_counter.type = cmd_ctx->lsm->u.context.ctx.u.perf_counter.type;
1513 kctx.u.perf_counter.config = cmd_ctx->lsm->u.context.ctx.u.perf_counter.config;
1514 strncpy(kctx.u.perf_counter.name,
1515 cmd_ctx->lsm->u.context.ctx.u.perf_counter.name,
1516 LTTNG_SYMBOL_NAME_LEN);
99497cd0 1517 kctx.u.perf_counter.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
b579acd9
DG
1518
1519 /* Add kernel context to kernel tracer. See context.c */
1520 ret = add_kernel_context(cmd_ctx->session->kernel_session, &kctx,
1521 cmd_ctx->lsm->u.context.event_name,
1522 cmd_ctx->lsm->u.context.channel_name);
1523 if (ret != LTTCOMM_OK) {
d65106b1
DG
1524 goto error;
1525 }
b579acd9
DG
1526 break;
1527 default:
1528 /* TODO: Userspace tracing */
1529 ret = LTTCOMM_NOT_IMPLEMENTED;
0b97ec54 1530 goto error;
d65106b1
DG
1531 }
1532
1533 ret = LTTCOMM_OK;
1534 break;
1535 }
0b97ec54 1536 case LTTNG_DISABLE_CHANNEL:
26cc6b4e 1537 {
0b97ec54 1538 struct ltt_kernel_channel *kchan;
26cc6b4e
DG
1539
1540 /* Setup lttng message with no payload */
1541 ret = setup_lttng_msg(cmd_ctx, 0);
1542 if (ret < 0) {
1543 goto setup_error;
1544 }
1545
0b97ec54
DG
1546 switch (cmd_ctx->lsm->domain.type) {
1547 case LTTNG_DOMAIN_KERNEL:
1548 kchan = get_kernel_channel_by_name(cmd_ctx->lsm->u.disable.channel_name,
1549 cmd_ctx->session->kernel_session);
1550 if (kchan == NULL) {
1551 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
26cc6b4e 1552 goto error;
0b97ec54
DG
1553 } else if (kchan->enabled == 1) {
1554 ret = kernel_disable_channel(kchan);
1555 if (ret < 0) {
1556 if (ret != EEXIST) {
1557 ret = LTTCOMM_KERN_CHAN_DISABLE_FAIL;
1558 }
1559 goto error;
1560 }
26cc6b4e 1561 }
0b97ec54
DG
1562 kernel_wait_quiescent(kernel_tracer_fd);
1563 break;
1564 default:
1565 /* TODO: Userspace tracing */
1566 ret = LTTCOMM_NOT_IMPLEMENTED;
1567 goto error;
26cc6b4e
DG
1568 }
1569
26cc6b4e
DG
1570 ret = LTTCOMM_OK;
1571 break;
1572 }
f5177a38 1573 case LTTNG_DISABLE_EVENT:
e953ef25 1574 {
f5177a38
DG
1575 struct ltt_kernel_channel *kchan;
1576 struct ltt_kernel_event *kevent;
e953ef25
DG
1577
1578 /* Setup lttng message with no payload */
1579 ret = setup_lttng_msg(cmd_ctx, 0);
1580 if (ret < 0) {
1581 goto setup_error;
1582 }
1583
f5177a38
DG
1584 switch (cmd_ctx->lsm->domain.type) {
1585 case LTTNG_DOMAIN_KERNEL:
1586 kchan = get_kernel_channel_by_name(cmd_ctx->lsm->u.disable.channel_name,
1587 cmd_ctx->session->kernel_session);
1588 if (kchan == NULL) {
1589 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
19e70852 1590 goto error;
e953ef25 1591 }
f5177a38
DG
1592
1593 kevent = get_kernel_event_by_name(cmd_ctx->lsm->u.disable.name, kchan);
1594 if (kevent != NULL) {
1595 DBG("Disabling kernel event %s for channel %s.", kevent->event->name,
1596 kchan->channel->name);
1597 ret = kernel_disable_event(kevent);
1598 if (ret < 0) {
1599 ret = LTTCOMM_KERN_ENABLE_FAIL;
1600 goto error;
1601 }
1602 }
1603
1604 kernel_wait_quiescent(kernel_tracer_fd);
1605 break;
1606 default:
1607 /* TODO: Userspace tracing */
1608 ret = LTTCOMM_NOT_IMPLEMENTED;
1609 goto error;
e953ef25
DG
1610 }
1611
19e70852 1612 ret = LTTCOMM_OK;
e953ef25
DG
1613 break;
1614 }
f5177a38 1615 case LTTNG_DISABLE_ALL_EVENT:
950131af 1616 {
f5177a38
DG
1617 struct ltt_kernel_channel *kchan;
1618 struct ltt_kernel_event *kevent;
950131af
DG
1619
1620 /* Setup lttng message with no payload */
1621 ret = setup_lttng_msg(cmd_ctx, 0);
1622 if (ret < 0) {
1623 goto setup_error;
1624 }
1625
f5177a38
DG
1626 switch (cmd_ctx->lsm->domain.type) {
1627 case LTTNG_DOMAIN_KERNEL:
1628 DBG("Disabling all enabled kernel events");
1629 kchan = get_kernel_channel_by_name(cmd_ctx->lsm->u.disable.channel_name,
1630 cmd_ctx->session->kernel_session);
1631 if (kchan == NULL) {
1632 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
1633 goto error;
1634 }
950131af 1635
f5177a38
DG
1636 /* For each event in the kernel session */
1637 cds_list_for_each_entry(kevent, &kchan->events_list.head, list) {
1638 DBG("Disabling kernel event %s for channel %s.",
1639 kevent->event->name, kchan->channel->name);
1640 ret = kernel_disable_event(kevent);
1641 if (ret < 0) {
1642 continue;
1643 }
950131af 1644 }
f5177a38
DG
1645
1646 /* Quiescent wait after event disable */
1647 kernel_wait_quiescent(kernel_tracer_fd);
1648 break;
1649 default:
1650 /* TODO: Userspace tracing */
1651 ret = LTTCOMM_NOT_IMPLEMENTED;
1652 goto error;
950131af
DG
1653 }
1654
950131af
DG
1655 ret = LTTCOMM_OK;
1656 break;
1657 }
0d0c377a 1658 case LTTNG_ENABLE_CHANNEL:
d36b8583 1659 {
0d0c377a 1660 struct ltt_kernel_channel *kchan;
d36b8583
DG
1661
1662 /* Setup lttng message with no payload */
1663 ret = setup_lttng_msg(cmd_ctx, 0);
1664 if (ret < 0) {
1665 goto setup_error;
1666 }
1667
0d0c377a
DG
1668 switch (cmd_ctx->lsm->domain.type) {
1669 case LTTNG_DOMAIN_KERNEL:
1670 kchan = get_kernel_channel_by_name(cmd_ctx->lsm->u.enable.channel_name,
1671 cmd_ctx->session->kernel_session);
1672 if (kchan == NULL) {
1673 /* Channel not found, creating it */
1674 DBG("Creating kernel channel");
7d29a247 1675
0d0c377a 1676 ret = kernel_create_channel(cmd_ctx->session->kernel_session,
63053e7c
DG
1677 &cmd_ctx->lsm->u.channel.chan,
1678 cmd_ctx->session->kernel_session->trace_path);
0d0c377a
DG
1679 if (ret < 0) {
1680 ret = LTTCOMM_KERN_CHAN_FAIL;
1681 goto error;
1682 }
7d29a247 1683
0d0c377a
DG
1684 /* Notify kernel thread that there is a new channel */
1685 ret = notify_kernel_pollfd();
1686 if (ret < 0) {
1687 ret = LTTCOMM_FATAL;
1688 goto error;
1689 }
1690 } else if (kchan->enabled == 0) {
1691 ret = kernel_enable_channel(kchan);
1692 if (ret < 0) {
1693 if (ret != EEXIST) {
1694 ret = LTTCOMM_KERN_CHAN_ENABLE_FAIL;
1695 }
1696 goto error;
d36b8583 1697 }
d36b8583 1698 }
0d0c377a
DG
1699
1700 kernel_wait_quiescent(kernel_tracer_fd);
1701 break;
1702 default:
1703 /* TODO: Userspace tracing */
1704 ret = LTTCOMM_NOT_IMPLEMENTED;
1705 goto error;
d36b8583
DG
1706 }
1707
d36b8583
DG
1708 ret = LTTCOMM_OK;
1709 break;
1710 }
0d0c377a 1711 case LTTNG_ENABLE_EVENT:
894be886 1712 {
7d29a247
DG
1713 char *channel_name;
1714 struct ltt_kernel_channel *kchan;
0d0c377a 1715 struct ltt_kernel_event *kevent;
7d29a247 1716 struct lttng_channel *chan;
f3ed775e 1717
894be886
DG
1718 /* Setup lttng message with no payload */
1719 ret = setup_lttng_msg(cmd_ctx, 0);
1720 if (ret < 0) {
1721 goto setup_error;
1722 }
1723
7d29a247
DG
1724 channel_name = cmd_ctx->lsm->u.enable.channel_name;
1725
0d0c377a
DG
1726 switch (cmd_ctx->lsm->domain.type) {
1727 case LTTNG_DOMAIN_KERNEL:
b389abbe
MD
1728 kchan = get_kernel_channel_by_name(channel_name,
1729 cmd_ctx->session->kernel_session);
1730 if (kchan == NULL) {
1731 DBG("Channel not found. Creating channel %s", channel_name);
1732
1733 chan = init_default_channel(cmd_ctx->lsm->domain.type, channel_name);
1734 if (chan == NULL) {
1735 ret = LTTCOMM_FATAL;
1736 goto error;
1737 }
1738
1739 ret = kernel_create_channel(cmd_ctx->session->kernel_session,
1740 chan, cmd_ctx->session->kernel_session->trace_path);
1741 if (ret < 0) {
1742 ret = LTTCOMM_KERN_CHAN_FAIL;
1743 goto error;
1744 }
0d0c377a
DG
1745 kchan = get_kernel_channel_by_name(channel_name,
1746 cmd_ctx->session->kernel_session);
1747 if (kchan == NULL) {
b389abbe
MD
1748 ERR("Channel %s not found after creation. Internal error, giving up.",
1749 channel_name);
1750 ret = LTTCOMM_FATAL;
1751 goto error;
7d29a247 1752 }
b389abbe 1753 }
7d29a247 1754
0d0c377a
DG
1755 kevent = get_kernel_event_by_name(cmd_ctx->lsm->u.enable.event.name, kchan);
1756 if (kevent == NULL) {
1757 DBG("Creating kernel event %s for channel %s.",
1758 cmd_ctx->lsm->u.enable.event.name, channel_name);
1759 ret = kernel_create_event(&cmd_ctx->lsm->u.enable.event, kchan);
1760 } else {
1761 DBG("Enabling kernel event %s for channel %s.",
1762 kevent->event->name, channel_name);
1763 ret = kernel_enable_event(kevent);
1764 if (ret == -EEXIST) {
1765 ret = LTTCOMM_KERN_EVENT_EXIST;
7d29a247
DG
1766 goto error;
1767 }
1768 }
f34daff7 1769
0d0c377a
DG
1770 if (ret < 0) {
1771 ret = LTTCOMM_KERN_ENABLE_FAIL;
7d29a247
DG
1772 goto error;
1773 }
19e70852 1774
0d0c377a
DG
1775 kernel_wait_quiescent(kernel_tracer_fd);
1776 break;
1777 default:
1778 /* TODO: Userspace tracing */
1779 ret = LTTCOMM_NOT_IMPLEMENTED;
19e70852 1780 goto error;
f3ed775e 1781 }
19e70852 1782 ret = LTTCOMM_OK;
894be886
DG
1783 break;
1784 }
0d0c377a 1785 case LTTNG_ENABLE_ALL_EVENT:
33a2b854 1786 {
9f19cc17
DG
1787 int size, i;
1788 char *channel_name;
7d29a247 1789 struct ltt_kernel_channel *kchan;
0d0c377a 1790 struct ltt_kernel_event *kevent;
9f19cc17 1791 struct lttng_event *event_list;
7d29a247 1792 struct lttng_channel *chan;
33a2b854
DG
1793
1794 /* Setup lttng message with no payload */
1795 ret = setup_lttng_msg(cmd_ctx, 0);
1796 if (ret < 0) {
1797 goto setup_error;
1798 }
1799
1800 DBG("Enabling all kernel event");
1801
7d29a247
DG
1802 channel_name = cmd_ctx->lsm->u.enable.channel_name;
1803
0d0c377a
DG
1804 switch (cmd_ctx->lsm->domain.type) {
1805 case LTTNG_DOMAIN_KERNEL:
b389abbe
MD
1806 kchan = get_kernel_channel_by_name(channel_name,
1807 cmd_ctx->session->kernel_session);
1808 if (kchan == NULL) {
1809 DBG("Channel not found. Creating channel %s", channel_name);
1810
1811 chan = init_default_channel(cmd_ctx->lsm->domain.type, channel_name);
1812 if (chan == NULL) {
1813 ret = LTTCOMM_FATAL;
1814 goto error;
1815 }
1816
1817 ret = kernel_create_channel(cmd_ctx->session->kernel_session,
1818 chan, cmd_ctx->session->kernel_session->trace_path);
1819 if (ret < 0) {
1820 ret = LTTCOMM_KERN_CHAN_FAIL;
1821 goto error;
1822 }
0d0c377a
DG
1823 kchan = get_kernel_channel_by_name(channel_name,
1824 cmd_ctx->session->kernel_session);
1825 if (kchan == NULL) {
b389abbe
MD
1826 ERR("Channel %s not found after creation. Internal error, giving up.",
1827 channel_name);
1828 ret = LTTCOMM_FATAL;
1829 goto error;
7d29a247 1830 }
b389abbe 1831 }
7d29a247 1832
0d0c377a
DG
1833 /* For each event in the kernel session */
1834 cds_list_for_each_entry(kevent, &kchan->events_list.head, list) {
1835 DBG("Enabling kernel event %s for channel %s.",
1836 kevent->event->name, channel_name);
1837 ret = kernel_enable_event(kevent);
7d29a247 1838 if (ret < 0) {
0d0c377a 1839 continue;
7d29a247
DG
1840 }
1841 }
33a2b854 1842
0d0c377a
DG
1843 size = kernel_list_events(kernel_tracer_fd, &event_list);
1844 if (size < 0) {
1845 ret = LTTCOMM_KERN_LIST_FAIL;
1846 goto error;
f3ed775e 1847 }
f3ed775e 1848
0d0c377a
DG
1849 for (i = 0; i < size; i++) {
1850 kevent = get_kernel_event_by_name(event_list[i].name, kchan);
1851 if (kevent == NULL) {
1852 /* Default event type for enable all */
1853 event_list[i].type = LTTNG_EVENT_TRACEPOINT;
1854 /* Enable each single tracepoint event */
1855 ret = kernel_create_event(&event_list[i], kchan);
1856 if (ret < 0) {
1857 /* Ignore error here and continue */
1858 }
950131af 1859 }
33a2b854 1860 }
33a2b854 1861
0d0c377a
DG
1862 free(event_list);
1863
1864 /* Quiescent wait after event enable */
1865 kernel_wait_quiescent(kernel_tracer_fd);
1866 break;
1867 default:
1868 /* TODO: Userspace tracing */
1869 ret = LTTCOMM_NOT_IMPLEMENTED;
1870 goto error;
1871 }
33a2b854
DG
1872
1873 ret = LTTCOMM_OK;
1874 break;
1875 }
052da939 1876 case LTTNG_LIST_TRACEPOINTS:
2ef84c95 1877 {
9f19cc17 1878 struct lttng_event *events;
052da939
DG
1879 ssize_t nb_events = 0;
1880
1881 switch (cmd_ctx->lsm->domain.type) {
1882 case LTTNG_DOMAIN_KERNEL:
1883 DBG("Listing kernel events");
1884 nb_events = kernel_list_events(kernel_tracer_fd, &events);
1885 if (nb_events < 0) {
1886 ret = LTTCOMM_KERN_LIST_FAIL;
1887 goto error;
1888 }
1889 break;
1890 default:
1891 /* TODO: Userspace listing */
1892 ret = LTTCOMM_NOT_IMPLEMENTED;
1893 break;
2ef84c95
DG
1894 }
1895
1896 /*
1897 * Setup lttng message with payload size set to the event list size in
1898 * bytes and then copy list into the llm payload.
1899 */
052da939 1900 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_event) * nb_events);
2ef84c95 1901 if (ret < 0) {
052da939 1902 free(events);
2ef84c95
DG
1903 goto setup_error;
1904 }
1905
1906 /* Copy event list into message payload */
9f19cc17 1907 memcpy(cmd_ctx->llm->payload, events,
052da939 1908 sizeof(struct lttng_event) * nb_events);
2ef84c95 1909
9f19cc17 1910 free(events);
2ef84c95
DG
1911
1912 ret = LTTCOMM_OK;
1913 break;
1914 }
f3ed775e 1915 case LTTNG_START_TRACE:
8c0faa1d
DG
1916 {
1917 struct ltt_kernel_channel *chan;
f3ed775e 1918
8c0faa1d
DG
1919 /* Setup lttng message with no payload */
1920 ret = setup_lttng_msg(cmd_ctx, 0);
1921 if (ret < 0) {
1922 goto setup_error;
1923 }
1924
f3ed775e
DG
1925 /* Kernel tracing */
1926 if (cmd_ctx->session->kernel_session != NULL) {
1927 if (cmd_ctx->session->kernel_session->metadata == NULL) {
1928 DBG("Open kernel metadata");
58a97671 1929 ret = kernel_open_metadata(cmd_ctx->session->kernel_session,
63053e7c 1930 cmd_ctx->session->kernel_session->trace_path);
f3ed775e
DG
1931 if (ret < 0) {
1932 ret = LTTCOMM_KERN_META_FAIL;
1933 goto error;
1934 }
1935 }
8c0faa1d 1936
f3ed775e
DG
1937 if (cmd_ctx->session->kernel_session->metadata_stream_fd == 0) {
1938 DBG("Opening kernel metadata stream");
1939 if (cmd_ctx->session->kernel_session->metadata_stream_fd == 0) {
1940 ret = kernel_open_metadata_stream(cmd_ctx->session->kernel_session);
1941 if (ret < 0) {
1942 ERR("Kernel create metadata stream failed");
1943 ret = LTTCOMM_KERN_STREAM_FAIL;
1944 goto error;
1945 }
1946 }
1947 }
8c0faa1d 1948
f3ed775e 1949 /* For each channel */
0d0c377a
DG
1950 cds_list_for_each_entry(chan,
1951 &cmd_ctx->session->kernel_session->channel_list.head, list) {
f3ed775e
DG
1952 if (chan->stream_count == 0) {
1953 ret = kernel_open_channel_stream(chan);
1954 if (ret < 0) {
1955 ERR("Kernel create channel stream failed");
1956 ret = LTTCOMM_KERN_STREAM_FAIL;
1957 goto error;
1958 }
1959 /* Update the stream global counter */
1960 cmd_ctx->session->kernel_session->stream_count_global += ret;
1961 }
1962 }
1963
1964 DBG("Start kernel tracing");
1965 ret = kernel_start_session(cmd_ctx->session->kernel_session);
8c0faa1d 1966 if (ret < 0) {
f3ed775e
DG
1967 ERR("Kernel start session failed");
1968 ret = LTTCOMM_KERN_START_FAIL;
8c0faa1d
DG
1969 goto error;
1970 }
8c0faa1d 1971
f3ed775e
DG
1972 ret = start_kernel_trace(cmd_ctx->session->kernel_session);
1973 if (ret < 0) {
1974 ret = LTTCOMM_KERN_START_FAIL;
8c0faa1d
DG
1975 goto error;
1976 }
8c0faa1d 1977
f3ed775e
DG
1978 /* Quiescent wait after starting trace */
1979 kernel_wait_quiescent(kernel_tracer_fd);
8c0faa1d
DG
1980 }
1981
f3ed775e 1982 /* TODO: Start all UST traces */
8c0faa1d
DG
1983
1984 ret = LTTCOMM_OK;
1985 break;
1986 }
f3ed775e 1987 case LTTNG_STOP_TRACE:
8c0faa1d 1988 {
f3ed775e 1989 struct ltt_kernel_channel *chan;
8c0faa1d
DG
1990 /* Setup lttng message with no payload */
1991 ret = setup_lttng_msg(cmd_ctx, 0);
1992 if (ret < 0) {
1993 goto setup_error;
1994 }
1995
f3ed775e
DG
1996 /* Kernel tracer */
1997 if (cmd_ctx->session->kernel_session != NULL) {
1998 DBG("Stop kernel tracing");
84291629 1999
f3ed775e
DG
2000 ret = kernel_metadata_flush_buffer(cmd_ctx->session->kernel_session->metadata_stream_fd);
2001 if (ret < 0) {
2002 ERR("Kernel metadata flush failed");
2003 }
8c0faa1d 2004
f3ed775e
DG
2005 cds_list_for_each_entry(chan, &cmd_ctx->session->kernel_session->channel_list.head, list) {
2006 ret = kernel_flush_buffer(chan);
2007 if (ret < 0) {
2008 ERR("Kernel flush buffer error");
2009 }
2010 }
2011
2012 ret = kernel_stop_session(cmd_ctx->session->kernel_session);
2013 if (ret < 0) {
2014 ERR("Kernel stop session failed");
2015 ret = LTTCOMM_KERN_STOP_FAIL;
2016 goto error;
2017 }
2018
2019 /* Quiescent wait after stopping trace */
2020 kernel_wait_quiescent(kernel_tracer_fd);
8c0faa1d
DG
2021 }
2022
f3ed775e 2023 /* TODO : User-space tracer */
8c0faa1d
DG
2024
2025 ret = LTTCOMM_OK;
2026 break;
2027 }
5e16da05
MD
2028 case LTTNG_CREATE_SESSION:
2029 {
5461b305
DG
2030 /* Setup lttng message with no payload */
2031 ret = setup_lttng_msg(cmd_ctx, 0);
2032 if (ret < 0) {
2033 goto setup_error;
2034 }
2035
42abccdb 2036 ret = create_session(cmd_ctx->lsm->session.name, cmd_ctx->lsm->session.path);
5e16da05 2037 if (ret < 0) {
f3ed775e 2038 if (ret == -EEXIST) {
5e16da05
MD
2039 ret = LTTCOMM_EXIST_SESS;
2040 } else {
aaf97519 2041 ret = LTTCOMM_FATAL;
aaf97519 2042 }
5461b305 2043 goto error;
8028d920 2044 }
1657e9bb 2045
5461b305 2046 ret = LTTCOMM_OK;
5e16da05
MD
2047 break;
2048 }
2049 case LTTNG_DESTROY_SESSION:
2050 {
5461b305
DG
2051 /* Setup lttng message with no payload */
2052 ret = setup_lttng_msg(cmd_ctx, 0);
2053 if (ret < 0) {
2054 goto setup_error;
2055 }
2056
f3ed775e
DG
2057 /* Clean kernel session teardown */
2058 teardown_kernel_session(cmd_ctx->session);
2059
42abccdb 2060 ret = destroy_session(cmd_ctx->lsm->session.name);
5e16da05 2061 if (ret < 0) {
f3ed775e 2062 ret = LTTCOMM_FATAL;
5461b305 2063 goto error;
5e16da05 2064 }
1657e9bb 2065
7a485870
DG
2066 /*
2067 * Must notify the kernel thread here to update it's pollfd in order to
2068 * remove the channel(s)' fd just destroyed.
2069 */
2070 ret = notify_kernel_pollfd();
2071 if (ret < 0) {
2072 ret = LTTCOMM_FATAL;
2073 goto error;
2074 }
2075
5461b305
DG
2076 ret = LTTCOMM_OK;
2077 break;
5e16da05 2078 }
9f19cc17 2079 case LTTNG_LIST_DOMAINS:
5e16da05 2080 {
4b222185 2081 size_t nb_dom = 0;
5461b305 2082
9f19cc17
DG
2083 if (cmd_ctx->session->kernel_session != NULL) {
2084 nb_dom++;
ce3d728c 2085 }
520ff687 2086
9f19cc17
DG
2087 nb_dom += cmd_ctx->session->ust_trace_count;
2088
2089 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_domain) * nb_dom);
5461b305
DG
2090 if (ret < 0) {
2091 goto setup_error;
520ff687 2092 }
57167058 2093
9f19cc17
DG
2094 ((struct lttng_domain *)(cmd_ctx->llm->payload))[0].type =
2095 LTTNG_DOMAIN_KERNEL;
5e16da05 2096
9f19cc17 2097 /* TODO: User-space tracer domain support */
5461b305 2098 ret = LTTCOMM_OK;
5e16da05
MD
2099 break;
2100 }
9f19cc17 2101 case LTTNG_LIST_CHANNELS:
5e16da05 2102 {
9f19cc17
DG
2103 /*
2104 * TODO: Only kernel channels are listed here. UST listing
2105 * is needed on lttng-ust 2.0 release.
2106 */
2107 size_t nb_chan = 0;
2108 if (cmd_ctx->session->kernel_session != NULL) {
2109 nb_chan += cmd_ctx->session->kernel_session->channel_count;
5461b305 2110 }
ca95a216 2111
9f19cc17
DG
2112 ret = setup_lttng_msg(cmd_ctx,
2113 sizeof(struct lttng_channel) * nb_chan);
5461b305
DG
2114 if (ret < 0) {
2115 goto setup_error;
2116 }
9f19cc17
DG
2117
2118 list_lttng_channels(cmd_ctx->session,
2119 (struct lttng_channel *)(cmd_ctx->llm->payload));
2120
2121 ret = LTTCOMM_OK;
5461b305 2122 break;
5e16da05 2123 }
9f19cc17 2124 case LTTNG_LIST_EVENTS:
5e16da05 2125 {
9f19cc17
DG
2126 /*
2127 * TODO: Only kernel events are listed here. UST listing
2128 * is needed on lttng-ust 2.0 release.
2129 */
2130 size_t nb_event = 0;
2131 struct ltt_kernel_channel *kchan = NULL;
2132
2133 if (cmd_ctx->session->kernel_session != NULL) {
2134 kchan = get_kernel_channel_by_name(cmd_ctx->lsm->u.list.channel_name,
2135 cmd_ctx->session->kernel_session);
2136 if (kchan == NULL) {
2137 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
2138 goto error;
2139 }
2140 nb_event += kchan->event_count;
5461b305 2141 }
ca95a216 2142
9f19cc17
DG
2143 ret = setup_lttng_msg(cmd_ctx,
2144 sizeof(struct lttng_event) * nb_event);
5461b305
DG
2145 if (ret < 0) {
2146 goto setup_error;
2147 }
9f19cc17 2148
ced2f820 2149 DBG("Listing events (%zu events)", nb_event);
9f19cc17
DG
2150
2151 list_lttng_events(kchan,
2152 (struct lttng_event *)(cmd_ctx->llm->payload));
2153
2154 ret = LTTCOMM_OK;
5461b305 2155 break;
5e16da05
MD
2156 }
2157 case LTTNG_LIST_SESSIONS:
2158 {
6c9cc2ab 2159 lock_session_list();
5461b305 2160
6c9cc2ab 2161 if (session_list_ptr->count == 0) {
f3ed775e 2162 ret = LTTCOMM_NO_SESSION;
36a83748 2163 unlock_session_list();
5461b305 2164 goto error;
57167058 2165 }
5e16da05 2166
6c9cc2ab
DG
2167 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_session) *
2168 session_list_ptr->count);
5461b305 2169 if (ret < 0) {
36a83748 2170 unlock_session_list();
5461b305 2171 goto setup_error;
e065084a 2172 }
5e16da05 2173
6c9cc2ab
DG
2174 /* Filled the session array */
2175 list_lttng_sessions((struct lttng_session *)(cmd_ctx->llm->payload));
2176
2177 unlock_session_list();
5e16da05 2178
5461b305 2179 ret = LTTCOMM_OK;
5e16da05
MD
2180 break;
2181 }
d0254c7c
MD
2182 case LTTNG_CALIBRATE:
2183 {
2184 /* Setup lttng message with no payload */
2185 ret = setup_lttng_msg(cmd_ctx, 0);
2186 if (ret < 0) {
2187 goto setup_error;
2188 }
2189
2190 switch (cmd_ctx->lsm->domain.type) {
2191 case LTTNG_DOMAIN_KERNEL:
2192 {
2193 struct lttng_kernel_calibrate kcalibrate;
2194
2195 kcalibrate.type = cmd_ctx->lsm->u.calibrate.type;
2196 ret = kernel_calibrate(kernel_tracer_fd, &kcalibrate);
2197 if (ret < 0) {
2198 ret = LTTCOMM_KERN_ENABLE_FAIL;
2199 goto error;
2200 }
2201 break;
2202 }
2203 default:
2204 /* TODO: Userspace tracing */
2205 ret = LTTCOMM_NOT_IMPLEMENTED;
2206 goto error;
2207 }
2208 ret = LTTCOMM_OK;
2209 break;
2210 }
d9800920
DG
2211 case LTTNG_REGISTER_CONSUMER:
2212 {
2213 int sock;
2214
2215 /* Setup lttng message with no payload */
2216 ret = setup_lttng_msg(cmd_ctx, 0);
2217 if (ret < 0) {
2218 goto setup_error;
2219 }
2220
2221 switch (cmd_ctx->lsm->domain.type) {
2222 case LTTNG_DOMAIN_KERNEL:
2223 {
2224 /* Can't register a consumer if there is already one */
2225 if (cmd_ctx->session->kernel_session->consumer_fd != 0) {
2226 ret = LTTCOMM_CONNECT_FAIL;
2227 goto error;
2228 }
2229
2230 sock = lttcomm_connect_unix_sock(cmd_ctx->lsm->u.reg.path);
2231 if (sock < 0) {
2232 ret = LTTCOMM_CONNECT_FAIL;
2233 goto error;
2234 }
2235
2236 cmd_ctx->session->kernel_session->consumer_fd = sock;
2237 break;
2238 }
2239 default:
2240 /* TODO: Userspace tracing */
2241 ret = LTTCOMM_NOT_IMPLEMENTED;
2242 goto error;
2243 }
2244
2245 ret = LTTCOMM_OK;
2246 break;
2247 }
d0254c7c 2248
5e16da05
MD
2249 default:
2250 /* Undefined command */
5461b305
DG
2251 ret = setup_lttng_msg(cmd_ctx, 0);
2252 if (ret < 0) {
2253 goto setup_error;
2254 }
2255
5e16da05 2256 ret = LTTCOMM_UND;
5461b305 2257 break;
fac6795d
DG
2258 }
2259
5461b305
DG
2260 /* Set return code */
2261 cmd_ctx->llm->ret_code = ret;
ca95a216 2262
b5541356
DG
2263 if (cmd_ctx->session) {
2264 unlock_session(cmd_ctx->session);
2265 }
2266
e065084a
DG
2267 return ret;
2268
5461b305 2269error:
5461b305
DG
2270 if (cmd_ctx->llm == NULL) {
2271 DBG("Missing llm structure. Allocating one.");
894be886 2272 if (setup_lttng_msg(cmd_ctx, 0) < 0) {
5461b305
DG
2273 goto setup_error;
2274 }
2275 }
e065084a 2276 /* Notify client of error */
5461b305 2277 cmd_ctx->llm->ret_code = ret;
e065084a 2278
5461b305 2279setup_error:
b5541356
DG
2280 if (cmd_ctx->session) {
2281 unlock_session(cmd_ctx->session);
2282 }
8028d920 2283 return ret;
fac6795d
DG
2284}
2285
1d4b027a 2286/*
d063d709
DG
2287 * This thread manage all clients request using the unix client socket for
2288 * communication.
1d4b027a
DG
2289 */
2290static void *thread_manage_clients(void *data)
2291{
273ea72c
DG
2292 int sock = 0, ret;
2293 struct command_ctx *cmd_ctx = NULL;
2294 struct pollfd pollfd[2];
1d4b027a
DG
2295
2296 DBG("[thread] Manage client started");
2297
2298 ret = lttcomm_listen_unix_sock(client_sock);
2299 if (ret < 0) {
2300 goto error;
2301 }
2302
273ea72c
DG
2303 /* First fd is always the quit pipe */
2304 pollfd[0].fd = thread_quit_pipe[0];
2305
2306 /* Apps socket */
2307 pollfd[1].fd = client_sock;
2308 pollfd[1].events = POLLIN;
2309
1d4b027a
DG
2310 /* Notify parent pid that we are ready
2311 * to accept command for client side.
2312 */
2313 if (opt_sig_parent) {
2314 kill(ppid, SIGCHLD);
2315 }
2316
2317 while (1) {
1d4b027a 2318 DBG("Accepting client command ...");
273ea72c
DG
2319
2320 /* Inifinite blocking call, waiting for transmission */
2321 ret = poll(pollfd, 2, -1);
2322 if (ret < 0) {
2323 perror("poll client thread");
2324 goto error;
2325 }
2326
2327 /* Thread quit pipe has been closed. Killing thread. */
2328 if (pollfd[0].revents == POLLNVAL) {
2329 goto error;
2330 } else if (pollfd[1].revents == POLLERR) {
2331 ERR("Client socket poll error");
2332 goto error;
2333 }
2334
1d4b027a
DG
2335 sock = lttcomm_accept_unix_sock(client_sock);
2336 if (sock < 0) {
2337 goto error;
2338 }
2339
2340 /* Allocate context command to process the client request */
2341 cmd_ctx = malloc(sizeof(struct command_ctx));
2342
2343 /* Allocate data buffer for reception */
2344 cmd_ctx->lsm = malloc(sizeof(struct lttcomm_session_msg));
2345 cmd_ctx->llm = NULL;
2346 cmd_ctx->session = NULL;
2347
2348 /*
2349 * Data is received from the lttng client. The struct
2350 * lttcomm_session_msg (lsm) contains the command and data request of
2351 * the client.
2352 */
2353 DBG("Receiving data from client ...");
2354 ret = lttcomm_recv_unix_sock(sock, cmd_ctx->lsm, sizeof(struct lttcomm_session_msg));
2355 if (ret <= 0) {
2356 continue;
2357 }
2358
f7776ea7
DG
2359 // TODO: Validate cmd_ctx including sanity check for security purpose.
2360
1d4b027a
DG
2361 /*
2362 * This function dispatch the work to the kernel or userspace tracer
2363 * libs and fill the lttcomm_lttng_msg data structure of all the needed
2364 * informations for the client. The command context struct contains
2365 * everything this function may needs.
2366 */
2367 ret = process_client_msg(cmd_ctx);
2368 if (ret < 0) {
2369 /* TODO: Inform client somehow of the fatal error. At this point,
2370 * ret < 0 means that a malloc failed (ENOMEM). */
2371 /* Error detected but still accept command */
a2fb29a5 2372 clean_command_ctx(&cmd_ctx);
1d4b027a
DG
2373 continue;
2374 }
2375
2376 DBG("Sending response (size: %d, retcode: %d)",
2377 cmd_ctx->lttng_msg_size, cmd_ctx->llm->ret_code);
2378 ret = send_unix_sock(sock, cmd_ctx->llm, cmd_ctx->lttng_msg_size);
2379 if (ret < 0) {
2380 ERR("Failed to send data back to client");
2381 }
2382
a2fb29a5 2383 clean_command_ctx(&cmd_ctx);
d6e4fca4
DG
2384
2385 /* End of transmission */
2386 close(sock);
1d4b027a
DG
2387 }
2388
2389error:
273ea72c
DG
2390 DBG("Client thread dying");
2391 if (client_sock) {
2392 close(client_sock);
2393 }
2394 if (sock) {
2395 close(sock);
2396 }
2397
2398 unlink(client_unix_sock_path);
2399
a2fb29a5 2400 clean_command_ctx(&cmd_ctx);
1d4b027a
DG
2401 return NULL;
2402}
2403
2404
fac6795d
DG
2405/*
2406 * usage function on stderr
2407 */
2408static void usage(void)
2409{
b716ce68 2410 fprintf(stderr, "Usage: %s OPTIONS\n\nOptions:\n", progname);
d6f42150
DG
2411 fprintf(stderr, " -h, --help Display this usage.\n");
2412 fprintf(stderr, " -c, --client-sock PATH Specify path for the client unix socket\n");
2413 fprintf(stderr, " -a, --apps-sock PATH Specify path for apps unix socket\n");
2414 fprintf(stderr, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
2415 fprintf(stderr, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
2416 fprintf(stderr, " -d, --daemonize Start as a daemon.\n");
2417 fprintf(stderr, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
2418 fprintf(stderr, " -V, --version Show version number.\n");
2419 fprintf(stderr, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
2420 fprintf(stderr, " -q, --quiet No output at all.\n");
2421 fprintf(stderr, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
31f73cc9 2422 fprintf(stderr, " --verbose-kconsumerd Verbose mode for kconsumerd. Activate DBG() macro.\n");
fac6795d
DG
2423}
2424
2425/*
2426 * daemon argument parsing
2427 */
2428static int parse_args(int argc, char **argv)
2429{
2430 int c;
2431
2432 static struct option long_options[] = {
2433 { "client-sock", 1, 0, 'c' },
2434 { "apps-sock", 1, 0, 'a' },
d6f42150
DG
2435 { "kconsumerd-cmd-sock", 1, 0, 0 },
2436 { "kconsumerd-err-sock", 1, 0, 0 },
fac6795d 2437 { "daemonize", 0, 0, 'd' },
5b8719f5 2438 { "sig-parent", 0, 0, 'S' },
fac6795d
DG
2439 { "help", 0, 0, 'h' },
2440 { "group", 1, 0, 'g' },
2441 { "version", 0, 0, 'V' },
75462a81 2442 { "quiet", 0, 0, 'q' },
3f9947db 2443 { "verbose", 0, 0, 'v' },
31f73cc9 2444 { "verbose-kconsumerd", 0, 0, 'Z' },
fac6795d
DG
2445 { NULL, 0, 0, 0 }
2446 };
2447
2448 while (1) {
2449 int option_index = 0;
31f73cc9 2450 c = getopt_long(argc, argv, "dhqvVS" "a:c:g:s:E:C:Z", long_options, &option_index);
fac6795d
DG
2451 if (c == -1) {
2452 break;
2453 }
2454
2455 switch (c) {
2456 case 0:
2457 fprintf(stderr, "option %s", long_options[option_index].name);
2458 if (optarg) {
2459 fprintf(stderr, " with arg %s\n", optarg);
2460 }
2461 break;
b716ce68 2462 case 'c':
fac6795d
DG
2463 snprintf(client_unix_sock_path, PATH_MAX, "%s", optarg);
2464 break;
2465 case 'a':
2466 snprintf(apps_unix_sock_path, PATH_MAX, "%s", optarg);
2467 break;
2468 case 'd':
2469 opt_daemon = 1;
2470 break;
2471 case 'g':
2472 opt_tracing_group = strdup(optarg);
2473 break;
2474 case 'h':
2475 usage();
2476 exit(EXIT_FAILURE);
2477 case 'V':
2478 fprintf(stdout, "%s\n", VERSION);
2479 exit(EXIT_SUCCESS);
5b8719f5
DG
2480 case 'S':
2481 opt_sig_parent = 1;
2482 break;
d6f42150
DG
2483 case 'E':
2484 snprintf(kconsumerd_err_unix_sock_path, PATH_MAX, "%s", optarg);
2485 break;
2486 case 'C':
2487 snprintf(kconsumerd_cmd_unix_sock_path, PATH_MAX, "%s", optarg);
2488 break;
75462a81
DG
2489 case 'q':
2490 opt_quiet = 1;
2491 break;
3f9947db 2492 case 'v':
53086306
DG
2493 /* Verbose level can increase using multiple -v */
2494 opt_verbose += 1;
3f9947db 2495 break;
31f73cc9
MD
2496 case 'Z':
2497 opt_verbose_kconsumerd += 1;
2498 break;
fac6795d
DG
2499 default:
2500 /* Unknown option or other error.
2501 * Error is printed by getopt, just return */
2502 return -1;
2503 }
2504 }
2505
2506 return 0;
2507}
2508
2509/*
d063d709 2510 * Creates the two needed socket by the daemon.
d6f42150
DG
2511 * apps_sock - The communication socket for all UST apps.
2512 * client_sock - The communication of the cli tool (lttng).
fac6795d 2513 */
cf3af59e 2514static int init_daemon_socket(void)
fac6795d
DG
2515{
2516 int ret = 0;
2517 mode_t old_umask;
2518
2519 old_umask = umask(0);
2520
2521 /* Create client tool unix socket */
d6f42150
DG
2522 client_sock = lttcomm_create_unix_sock(client_unix_sock_path);
2523 if (client_sock < 0) {
2524 ERR("Create unix sock failed: %s", client_unix_sock_path);
fac6795d
DG
2525 ret = -1;
2526 goto end;
2527 }
2528
2529 /* File permission MUST be 660 */
2530 ret = chmod(client_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
2531 if (ret < 0) {
d6f42150 2532 ERR("Set file permissions failed: %s", client_unix_sock_path);
fac6795d
DG
2533 perror("chmod");
2534 goto end;
2535 }
2536
2537 /* Create the application unix socket */
d6f42150
DG
2538 apps_sock = lttcomm_create_unix_sock(apps_unix_sock_path);
2539 if (apps_sock < 0) {
2540 ERR("Create unix sock failed: %s", apps_unix_sock_path);
fac6795d
DG
2541 ret = -1;
2542 goto end;
2543 }
2544
d6f42150 2545 /* File permission MUST be 666 */
fac6795d
DG
2546 ret = chmod(apps_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
2547 if (ret < 0) {
d6f42150 2548 ERR("Set file permissions failed: %s", apps_unix_sock_path);
fac6795d
DG
2549 perror("chmod");
2550 goto end;
2551 }
2552
2553end:
2554 umask(old_umask);
2555 return ret;
2556}
2557
2558/*
7d8234d9
MD
2559 * Check if the global socket is available, and if a daemon is answering
2560 * at the other side. If yes, error is returned.
fac6795d 2561 */
cf3af59e 2562static int check_existing_daemon(void)
fac6795d 2563{
7d8234d9
MD
2564 if (access(client_unix_sock_path, F_OK) < 0 &&
2565 access(apps_unix_sock_path, F_OK) < 0)
2566 return 0;
2567 /* Is there anybody out there ? */
2568 if (lttng_session_daemon_alive())
2569 return -EEXIST;
2570 else
2571 return 0;
fac6795d
DG
2572}
2573
fac6795d 2574/*
d063d709 2575 * Set the tracing group gid onto the client socket.
5e16da05 2576 *
d063d709
DG
2577 * Race window between mkdir and chown is OK because we are going from more
2578 * permissive (root.root) to les permissive (root.tracing).
fac6795d 2579 */
d6f42150 2580static int set_permissions(void)
fac6795d
DG
2581{
2582 int ret;
996b65c8 2583 gid_t gid;
fac6795d 2584
996b65c8
MD
2585 gid = allowed_group();
2586 if (gid < 0) {
a463f419
DG
2587 if (is_root) {
2588 WARN("No tracing group detected");
2589 ret = 0;
2590 } else {
2591 ERR("Missing tracing group. Aborting execution.");
2592 ret = -1;
2593 }
fac6795d
DG
2594 goto end;
2595 }
2596
d6f42150 2597 /* Set lttng run dir */
996b65c8 2598 ret = chown(LTTNG_RUNDIR, 0, gid);
d6f42150
DG
2599 if (ret < 0) {
2600 ERR("Unable to set group on " LTTNG_RUNDIR);
2601 perror("chown");
2602 }
2603
2604 /* lttng client socket path */
996b65c8 2605 ret = chown(client_unix_sock_path, 0, gid);
fac6795d 2606 if (ret < 0) {
d6f42150
DG
2607 ERR("Unable to set group on %s", client_unix_sock_path);
2608 perror("chown");
2609 }
2610
2611 /* kconsumerd error socket path */
996b65c8 2612 ret = chown(kconsumerd_err_unix_sock_path, 0, gid);
d6f42150
DG
2613 if (ret < 0) {
2614 ERR("Unable to set group on %s", kconsumerd_err_unix_sock_path);
fac6795d
DG
2615 perror("chown");
2616 }
2617
d6f42150 2618 DBG("All permissions are set");
e07ae692 2619
fac6795d
DG
2620end:
2621 return ret;
2622}
2623
7a485870 2624/*
d063d709 2625 * Create the pipe used to wake up the kernel thread.
7a485870
DG
2626 */
2627static int create_kernel_poll_pipe(void)
2628{
2629 return pipe2(kernel_poll_pipe, O_CLOEXEC);
2630}
2631
d6f42150 2632/*
d063d709 2633 * Create the lttng run directory needed for all global sockets and pipe.
d6f42150
DG
2634 */
2635static int create_lttng_rundir(void)
2636{
2637 int ret;
2638
2639 ret = mkdir(LTTNG_RUNDIR, S_IRWXU | S_IRWXG );
2640 if (ret < 0) {
b1f11e69
DG
2641 if (errno != EEXIST) {
2642 ERR("Unable to create " LTTNG_RUNDIR);
2643 goto error;
2644 } else {
2645 ret = 0;
2646 }
d6f42150
DG
2647 }
2648
2649error:
2650 return ret;
2651}
2652
2653/*
d063d709
DG
2654 * Setup sockets and directory needed by the kconsumerd communication with the
2655 * session daemon.
d6f42150
DG
2656 */
2657static int set_kconsumerd_sockets(void)
2658{
2659 int ret;
2660
2661 if (strlen(kconsumerd_err_unix_sock_path) == 0) {
2662 snprintf(kconsumerd_err_unix_sock_path, PATH_MAX, KCONSUMERD_ERR_SOCK_PATH);
2663 }
2664
2665 if (strlen(kconsumerd_cmd_unix_sock_path) == 0) {
2666 snprintf(kconsumerd_cmd_unix_sock_path, PATH_MAX, KCONSUMERD_CMD_SOCK_PATH);
2667 }
2668
2669 ret = mkdir(KCONSUMERD_PATH, S_IRWXU | S_IRWXG);
2670 if (ret < 0) {
6beb2242
DG
2671 if (errno != EEXIST) {
2672 ERR("Failed to create " KCONSUMERD_PATH);
2673 goto error;
2674 }
2675 ret = 0;
d6f42150
DG
2676 }
2677
2678 /* Create the kconsumerd error unix socket */
2679 kconsumerd_err_sock = lttcomm_create_unix_sock(kconsumerd_err_unix_sock_path);
2680 if (kconsumerd_err_sock < 0) {
2681 ERR("Create unix sock failed: %s", kconsumerd_err_unix_sock_path);
2682 ret = -1;
2683 goto error;
2684 }
2685
2686 /* File permission MUST be 660 */
2687 ret = chmod(kconsumerd_err_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
2688 if (ret < 0) {
2689 ERR("Set file permissions failed: %s", kconsumerd_err_unix_sock_path);
2690 perror("chmod");
2691 goto error;
2692 }
2693
2694error:
2695 return ret;
2696}
2697
fac6795d 2698/*
d063d709 2699 * Signal handler for the daemon
cf3af59e
MD
2700 *
2701 * Simply stop all worker threads, leaving main() return gracefully
2702 * after joining all threads and calling cleanup().
fac6795d
DG
2703 */
2704static void sighandler(int sig)
2705{
2706 switch (sig) {
cf3af59e
MD
2707 case SIGPIPE:
2708 DBG("SIGPIPE catched");
2709 return;
2710 case SIGINT:
2711 DBG("SIGINT catched");
2712 stop_threads();
2713 break;
2714 case SIGTERM:
2715 DBG("SIGTERM catched");
2716 stop_threads();
2717 break;
2718 default:
2719 break;
fac6795d 2720 }
fac6795d
DG
2721}
2722
2723/*
d063d709 2724 * Setup signal handler for :
1d4b027a 2725 * SIGINT, SIGTERM, SIGPIPE
fac6795d 2726 */
1d4b027a 2727static int set_signal_handler(void)
fac6795d 2728{
1d4b027a
DG
2729 int ret = 0;
2730 struct sigaction sa;
2731 sigset_t sigset;
fac6795d 2732
1d4b027a
DG
2733 if ((ret = sigemptyset(&sigset)) < 0) {
2734 perror("sigemptyset");
2735 return ret;
2736 }
d6f42150 2737
1d4b027a
DG
2738 sa.sa_handler = sighandler;
2739 sa.sa_mask = sigset;
2740 sa.sa_flags = 0;
2741 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
2742 perror("sigaction");
2743 return ret;
d6f42150
DG
2744 }
2745
1d4b027a
DG
2746 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
2747 perror("sigaction");
2748 return ret;
d6f42150 2749 }
aaf26714 2750
1d4b027a
DG
2751 if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) {
2752 perror("sigaction");
2753 return ret;
8c0faa1d
DG
2754 }
2755
1d4b027a
DG
2756 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
2757
2758 return ret;
fac6795d
DG
2759}
2760
f3ed775e 2761/*
d063d709
DG
2762 * Set open files limit to unlimited. This daemon can open a large number of
2763 * file descriptors in order to consumer multiple kernel traces.
f3ed775e
DG
2764 */
2765static void set_ulimit(void)
2766{
2767 int ret;
2768 struct rlimit lim;
2769
a88df331 2770 /* The kernel does not allowed an infinite limit for open files */
f3ed775e
DG
2771 lim.rlim_cur = 65535;
2772 lim.rlim_max = 65535;
2773
2774 ret = setrlimit(RLIMIT_NOFILE, &lim);
2775 if (ret < 0) {
2776 perror("failed to set open files limit");
2777 }
2778}
2779
fac6795d
DG
2780/*
2781 * main
2782 */
2783int main(int argc, char **argv)
2784{
fac6795d
DG
2785 int ret = 0;
2786 void *status;
b082db07 2787 const char *home_path;
fac6795d 2788
273ea72c 2789 /* Create thread quit pipe */
cf3af59e
MD
2790 if ((ret = init_thread_quit_pipe()) < 0) {
2791 goto error;
273ea72c
DG
2792 }
2793
fac6795d
DG
2794 /* Parse arguments */
2795 progname = argv[0];
2796 if ((ret = parse_args(argc, argv) < 0)) {
cf3af59e 2797 goto error;
fac6795d
DG
2798 }
2799
2800 /* Daemonize */
2801 if (opt_daemon) {
53094c05
DG
2802 ret = daemon(0, 0);
2803 if (ret < 0) {
2804 perror("daemon");
cf3af59e 2805 goto error;
53094c05 2806 }
fac6795d
DG
2807 }
2808
2809 /* Check if daemon is UID = 0 */
2810 is_root = !getuid();
2811
fac6795d 2812 if (is_root) {
d6f42150
DG
2813 ret = create_lttng_rundir();
2814 if (ret < 0) {
cf3af59e 2815 goto error;
d6f42150
DG
2816 }
2817
fac6795d 2818 if (strlen(apps_unix_sock_path) == 0) {
d6f42150
DG
2819 snprintf(apps_unix_sock_path, PATH_MAX,
2820 DEFAULT_GLOBAL_APPS_UNIX_SOCK);
fac6795d
DG
2821 }
2822
2823 if (strlen(client_unix_sock_path) == 0) {
d6f42150
DG
2824 snprintf(client_unix_sock_path, PATH_MAX,
2825 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK);
2826 }
fac6795d 2827 } else {
b082db07
DG
2828 home_path = get_home_dir();
2829 if (home_path == NULL) {
273ea72c
DG
2830 /* TODO: Add --socket PATH option */
2831 ERR("Can't get HOME directory for sockets creation.");
cf3af59e
MD
2832 ret = -EPERM;
2833 goto error;
b082db07
DG
2834 }
2835
fac6795d 2836 if (strlen(apps_unix_sock_path) == 0) {
d6f42150 2837 snprintf(apps_unix_sock_path, PATH_MAX,
b082db07 2838 DEFAULT_HOME_APPS_UNIX_SOCK, home_path);
fac6795d
DG
2839 }
2840
2841 /* Set the cli tool unix socket path */
2842 if (strlen(client_unix_sock_path) == 0) {
d6f42150 2843 snprintf(client_unix_sock_path, PATH_MAX,
b082db07 2844 DEFAULT_HOME_CLIENT_UNIX_SOCK, home_path);
fac6795d
DG
2845 }
2846 }
2847
847177cd
DG
2848 DBG("Client socket path %s", client_unix_sock_path);
2849 DBG("Application socket path %s", apps_unix_sock_path);
2850
273ea72c 2851 /*
7d8234d9 2852 * See if daemon already exist.
fac6795d 2853 */
7d8234d9 2854 if ((ret = check_existing_daemon()) < 0) {
75462a81 2855 ERR("Already running daemon.\n");
273ea72c 2856 /*
cf3af59e
MD
2857 * We do not goto exit because we must not cleanup()
2858 * because a daemon is already running.
ab118b20 2859 */
cf3af59e 2860 goto error;
a88df331
DG
2861 }
2862
2863 /* After this point, we can safely call cleanup() so goto error is used */
2864
2865 /*
2866 * These actions must be executed as root. We do that *after* setting up
2867 * the sockets path because we MUST make the check for another daemon using
2868 * those paths *before* trying to set the kernel consumer sockets and init
2869 * kernel tracer.
2870 */
2871 if (is_root) {
2872 ret = set_kconsumerd_sockets();
2873 if (ret < 0) {
cf3af59e 2874 goto exit;
a88df331
DG
2875 }
2876
2877 /* Setup kernel tracer */
2878 init_kernel_tracer();
2879
2880 /* Set ulimit for open files */
2881 set_ulimit();
fac6795d
DG
2882 }
2883
cf3af59e
MD
2884 if ((ret = set_signal_handler()) < 0) {
2885 goto exit;
fac6795d
DG
2886 }
2887
d6f42150 2888 /* Setup the needed unix socket */
cf3af59e
MD
2889 if ((ret = init_daemon_socket()) < 0) {
2890 goto exit;
fac6795d
DG
2891 }
2892
2893 /* Set credentials to socket */
cf3af59e
MD
2894 if (is_root && ((ret = set_permissions()) < 0)) {
2895 goto exit;
fac6795d
DG
2896 }
2897
5b8719f5
DG
2898 /* Get parent pid if -S, --sig-parent is specified. */
2899 if (opt_sig_parent) {
2900 ppid = getppid();
2901 }
2902
7a485870 2903 /* Setup the kernel pipe for waking up the kernel thread */
cf3af59e
MD
2904 if ((ret = create_kernel_poll_pipe()) < 0) {
2905 goto exit;
7a485870
DG
2906 }
2907
273ea72c
DG
2908 /*
2909 * Get session list pointer. This pointer MUST NOT be free().
2910 * This list is statically declared in session.c
2911 */
b5541356
DG
2912 session_list_ptr = get_session_list();
2913
cf3af59e
MD
2914 /* Create thread to manage the client socket */
2915 ret = pthread_create(&client_thread, NULL, thread_manage_clients, (void *) NULL);
2916 if (ret != 0) {
2917 perror("pthread_create");
2918 goto exit_client;
2919 }
fac6795d 2920
cf3af59e
MD
2921 /* Create thread to manage application socket */
2922 ret = pthread_create(&apps_thread, NULL, thread_manage_apps, (void *) NULL);
2923 if (ret != 0) {
2924 perror("pthread_create");
2925 goto exit_apps;
2926 }
fac6795d 2927
cf3af59e
MD
2928 /* Create kernel thread to manage kernel event */
2929 ret = pthread_create(&kernel_thread, NULL, thread_manage_kernel, (void *) NULL);
2930 if (ret != 0) {
2931 perror("pthread_create");
2932 goto exit_kernel;
2933 }
7a485870 2934
cf3af59e
MD
2935 ret = pthread_join(kernel_thread, &status);
2936 if (ret != 0) {
2937 perror("pthread_join");
2938 goto error; /* join error, exit without cleanup */
fac6795d
DG
2939 }
2940
cf3af59e
MD
2941exit_kernel:
2942 ret = pthread_join(apps_thread, &status);
2943 if (ret != 0) {
2944 perror("pthread_join");
2945 goto error; /* join error, exit without cleanup */
2946 }
fac6795d 2947
cf3af59e
MD
2948exit_apps:
2949 ret = pthread_join(client_thread, &status);
2950 if (ret != 0) {
2951 perror("pthread_join");
2952 goto error; /* join error, exit without cleanup */
2953 }
2954
2955 ret = join_kconsumerd_thread();
2956 if (ret != 0) {
2957 perror("join_kconsumerd");
2958 goto error; /* join error, exit without cleanup */
2959 }
a88df331 2960
cf3af59e 2961exit_client:
a88df331 2962exit:
cf3af59e
MD
2963 /*
2964 * cleanup() is called when no other thread is running.
2965 */
2966 cleanup();
2967 if (!ret)
2968 exit(EXIT_SUCCESS);
2969error:
5e16da05 2970 exit(EXIT_FAILURE);
fac6795d 2971}
This page took 0.181274 seconds and 5 git commands to generate.