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