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