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