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