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