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