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