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