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