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