Fix return code to 0 on success and 1 on error
[lttng-tools.git] / ltt-sessiond / main.c
CommitLineData
826d496d
MD
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
fac6795d
DG
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
91d76f53 8 *
fac6795d
DG
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
91d76f53 13 *
fac6795d
DG
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
fac6795d
DG
17 */
18
19#define _GNU_SOURCE
20#include <fcntl.h>
21#include <getopt.h>
22#include <grp.h>
23#include <limits.h>
7a485870 24#include <poll.h>
fac6795d 25#include <pthread.h>
8c0faa1d 26#include <semaphore.h>
fac6795d
DG
27#include <signal.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31#include <sys/ipc.h>
b73401da 32#include <sys/mount.h>
fac6795d
DG
33#include <sys/shm.h>
34#include <sys/socket.h>
35#include <sys/stat.h>
36#include <sys/types.h>
f3ed775e
DG
37#include <sys/time.h>
38#include <sys/resource.h>
fac6795d
DG
39#include <unistd.h>
40
41#include <urcu/list.h> /* URCU list library (-lurcu) */
5b97ec60 42#include <lttng/lttng.h>
fac6795d
DG
43
44#include "liblttsessiondcomm.h"
45#include "ltt-sessiond.h"
75462a81 46#include "lttngerr.h"
20fe2104 47#include "kernel-ctl.h"
9e78d6ae 48#include "ust-ctl.h"
5b74c7b1 49#include "session.h"
91d76f53 50#include "traceable-app.h"
5dc18550 51#include "lttng-kconsumerd.h"
62d3069f 52#include "libustctl.h"
8e68d1c8 53#include "utils.h"
fac6795d 54
5e16da05
MD
55/*
56 * TODO:
57 * teardown: signal SIGTERM handler -> write into pipe. Threads waits
58 * with epoll on pipe and on other pipes/sockets for commands. Main
59 * simply waits on pthread join.
60 */
61
75462a81 62/* Const values */
686204ab 63const char default_home_dir[] = DEFAULT_HOME_DIR;
64a23ac4 64const char default_tracing_group[] = LTTNG_DEFAULT_TRACING_GROUP;
686204ab
MD
65const char default_ust_sock_dir[] = DEFAULT_UST_SOCK_DIR;
66const char default_global_apps_pipe[] = DEFAULT_GLOBAL_APPS_PIPE;
67
fac6795d 68/* Variables */
1d4b027a
DG
69int opt_verbose; /* Not static for lttngerr.h */
70int opt_quiet; /* Not static for lttngerr.h */
fac6795d
DG
71const char *progname;
72const char *opt_tracing_group;
5b8719f5 73static int opt_sig_parent;
fac6795d
DG
74static int opt_daemon;
75static int is_root; /* Set to 1 if the daemon is running as root */
1d4b027a
DG
76static pid_t ppid; /* Parent PID for --sig-parent option */
77static pid_t kconsumerd_pid;
7a485870 78static struct pollfd *kernel_pollfd;
fac6795d 79
d6f42150
DG
80static char apps_unix_sock_path[PATH_MAX]; /* Global application Unix socket path */
81static char client_unix_sock_path[PATH_MAX]; /* Global client Unix socket path */
82static char kconsumerd_err_unix_sock_path[PATH_MAX]; /* kconsumerd error Unix socket path */
83static char kconsumerd_cmd_unix_sock_path[PATH_MAX]; /* kconsumerd command Unix socket path */
fac6795d 84
1d4b027a 85/* Sockets and FDs */
d6f42150
DG
86static int client_sock;
87static int apps_sock;
88static int kconsumerd_err_sock;
8c0faa1d 89static int kconsumerd_cmd_sock;
20fe2104 90static int kernel_tracer_fd;
7a485870 91static int kernel_poll_pipe[2];
1d4b027a 92
273ea72c
DG
93/*
94 * Quit pipe for all threads. This permits a single cancellation point
95 * for all threads when receiving an event on the pipe.
96 */
97static int thread_quit_pipe[2];
98
1d4b027a 99/* Pthread, Mutexes and Semaphores */
8c0faa1d 100static pthread_t kconsumerd_thread;
1d4b027a
DG
101static pthread_t apps_thread;
102static pthread_t client_thread;
7a485870 103static pthread_t kernel_thread;
8c0faa1d
DG
104static sem_t kconsumerd_sem;
105
1d4b027a 106static pthread_mutex_t kconsumerd_pid_mutex; /* Mutex to control kconsumerd pid assignation */
fac6795d 107
b5541356
DG
108/*
109 * Pointer initialized before thread creation.
110 *
111 * This points to the tracing session list containing the session count and a
112 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
113 * MUST NOT be taken if you call a public function in session.c.
04ea676f
DG
114 *
115 * The lock is nested inside the structure: session_list_ptr->lock.
b5541356
DG
116 */
117static struct ltt_session_list *session_list_ptr;
118
273ea72c
DG
119/*
120 * Init quit pipe.
121 *
122 * Return -1 on error or 0 if all pipes are created.
123 */
124static int init_thread_quit_pipe(void)
125{
126 int ret;
127
128 ret = pipe2(thread_quit_pipe, O_CLOEXEC);
129 if (ret < 0) {
130 perror("thread quit pipe");
131 goto error;
132 }
133
134error:
135 return ret;
136}
137
fac6795d 138/*
1d4b027a 139 * teardown_kernel_session
fac6795d 140 *
b5541356
DG
141 * Complete teardown of a kernel session. This free all data structure related
142 * to a kernel session and update counter.
fac6795d 143 */
1d4b027a 144static void teardown_kernel_session(struct ltt_session *session)
fac6795d 145{
1d4b027a
DG
146 if (session->kernel_session != NULL) {
147 DBG("Tearing down kernel session");
c363b55d 148 trace_destroy_kernel_session(session->kernel_session);
1d4b027a
DG
149 /* Extra precaution */
150 session->kernel_session = NULL;
fac6795d 151 }
fac6795d
DG
152}
153
154/*
273ea72c 155 * Cleanup the daemon
fac6795d 156 */
1d4b027a 157static void cleanup()
fac6795d 158{
1d4b027a
DG
159 int ret;
160 char *cmd;
161 struct ltt_session *sess;
fac6795d 162
1d4b027a 163 DBG("Cleaning up");
e07ae692 164
1d4b027a 165 /* <fun> */
273ea72c
DG
166 MSG("\n%c[%d;%dm*** assert failed *** ==> %c[%dm%c[%d;%dm"
167 "Matthew, BEET driven development works!%c[%dm",
168 27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
1d4b027a 169 /* </fun> */
fac6795d 170
1d4b027a
DG
171 /* Stopping all threads */
172 DBG("Terminating all threads");
273ea72c
DG
173 close(thread_quit_pipe[0]);
174 close(thread_quit_pipe[1]);
fac6795d 175
1d4b027a
DG
176 DBG("Removing %s directory", LTTNG_RUNDIR);
177 ret = asprintf(&cmd, "rm -rf " LTTNG_RUNDIR);
178 if (ret < 0) {
179 ERR("asprintf failed. Something is really wrong!");
180 }
5461b305 181
1d4b027a
DG
182 /* Remove lttng run directory */
183 ret = system(cmd);
184 if (ret < 0) {
185 ERR("Unable to clean " LTTNG_RUNDIR);
186 }
5461b305 187
1d4b027a 188 DBG("Cleaning up all session");
fac6795d 189
b5541356 190 /* Destroy session list mutex */
273ea72c
DG
191 if (session_list_ptr != NULL) {
192 pthread_mutex_destroy(&session_list_ptr->lock);
193
194 /* Cleanup ALL session */
195 cds_list_for_each_entry(sess, &session_list_ptr->head, list) {
196 teardown_kernel_session(sess);
197 // TODO complete session cleanup (including UST)
198 }
199 }
200
201 pthread_mutex_destroy(&kconsumerd_pid_mutex);
b5541356 202
f3ed775e 203 DBG("Closing kernel fd");
1d4b027a 204 close(kernel_tracer_fd);
fac6795d
DG
205}
206
e065084a
DG
207/*
208 * send_unix_sock
209 *
210 * Send data on a unix socket using the liblttsessiondcomm API.
211 *
212 * Return lttcomm error code.
213 */
214static int send_unix_sock(int sock, void *buf, size_t len)
215{
216 /* Check valid length */
217 if (len <= 0) {
218 return -1;
219 }
220
221 return lttcomm_send_unix_sock(sock, buf, len);
222}
223
5461b305
DG
224/*
225 * clean_command_ctx
226 *
227 * Free memory of a command context structure.
228 */
a2fb29a5 229static void clean_command_ctx(struct command_ctx **cmd_ctx)
5461b305 230{
a2fb29a5
DG
231 DBG("Clean command context structure");
232 if (*cmd_ctx) {
233 if ((*cmd_ctx)->llm) {
234 free((*cmd_ctx)->llm);
5461b305 235 }
a2fb29a5
DG
236 if ((*cmd_ctx)->lsm) {
237 free((*cmd_ctx)->lsm);
5461b305 238 }
a2fb29a5
DG
239 free(*cmd_ctx);
240 *cmd_ctx = NULL;
5461b305
DG
241 }
242}
243
f3ed775e 244/*
7a485870 245 * send_kconsumerd_channel_fds
f3ed775e 246 *
7a485870 247 * Send all stream fds of kernel channel to the consumer.
f3ed775e 248 */
7a485870 249static int send_kconsumerd_channel_fds(int sock, struct ltt_kernel_channel *channel)
f3ed775e
DG
250{
251 int ret;
252 size_t nb_fd;
253 struct ltt_kernel_stream *stream;
f3ed775e
DG
254 struct lttcomm_kconsumerd_header lkh;
255 struct lttcomm_kconsumerd_msg lkm;
256
7a485870
DG
257 DBG("Sending fds of channel %s to kernel consumer", channel->channel->name);
258
259 nb_fd = channel->stream_count;
f3ed775e
DG
260
261 /* Setup header */
7a485870 262 lkh.payload_size = nb_fd * sizeof(struct lttcomm_kconsumerd_msg);
f3ed775e
DG
263 lkh.cmd_type = ADD_STREAM;
264
265 DBG("Sending kconsumerd header");
266
267 ret = lttcomm_send_unix_sock(sock, &lkh, sizeof(struct lttcomm_kconsumerd_header));
268 if (ret < 0) {
269 perror("send kconsumerd header");
270 goto error;
271 }
272
7a485870
DG
273 cds_list_for_each_entry(stream, &channel->stream_list.head, list) {
274 if (stream->fd != 0) {
f3ed775e
DG
275 lkm.fd = stream->fd;
276 lkm.state = stream->state;
7a485870 277 lkm.max_sb_size = channel->channel->attr.subbuf_size;
f3ed775e
DG
278 strncpy(lkm.path_name, stream->pathname, PATH_MAX);
279
280 DBG("Sending fd %d to kconsumerd", lkm.fd);
281
282 ret = lttcomm_send_fds_unix_sock(sock, &lkm, &lkm.fd, 1, sizeof(lkm));
283 if (ret < 0) {
284 perror("send kconsumerd fd");
285 goto error;
286 }
287 }
288 }
289
7a485870 290 DBG("Kconsumerd channel fds sent");
f3ed775e
DG
291
292 return 0;
293
294error:
295 return ret;
296}
297
298/*
7a485870 299 * send_kconsumerd_fds
f3ed775e 300 *
7a485870 301 * Send all stream fds of the kernel session to the consumer.
f3ed775e 302 */
7a485870 303static int send_kconsumerd_fds(int sock, struct ltt_kernel_session *session)
f3ed775e
DG
304{
305 int ret;
306 struct ltt_kernel_channel *chan;
7a485870
DG
307 struct lttcomm_kconsumerd_header lkh;
308 struct lttcomm_kconsumerd_msg lkm;
309
310 /* Setup header */
311 lkh.payload_size = sizeof(struct lttcomm_kconsumerd_msg);
312 lkh.cmd_type = ADD_STREAM;
313
314 DBG("Sending kconsumerd header for metadata");
315
316 ret = lttcomm_send_unix_sock(sock, &lkh, sizeof(struct lttcomm_kconsumerd_header));
317 if (ret < 0) {
318 perror("send kconsumerd header");
319 goto error;
320 }
321
322 DBG("Sending metadata stream fd");
323
324 if (session->metadata_stream_fd != 0) {
325 /* Send metadata stream fd first */
326 lkm.fd = session->metadata_stream_fd;
327 lkm.state = ACTIVE_FD;
328 lkm.max_sb_size = session->metadata->conf->attr.subbuf_size;
329 strncpy(lkm.path_name, session->metadata->pathname, PATH_MAX);
330
331 ret = lttcomm_send_fds_unix_sock(sock, &lkm, &lkm.fd, 1, sizeof(lkm));
332 if (ret < 0) {
333 perror("send kconsumerd fd");
334 goto error;
335 }
336 }
f3ed775e 337
f3ed775e 338 cds_list_for_each_entry(chan, &session->channel_list.head, list) {
7a485870 339 ret = send_kconsumerd_channel_fds(sock, chan);
f3ed775e 340 if (ret < 0) {
7a485870 341 goto error;
f3ed775e
DG
342 }
343 }
344
7a485870
DG
345 DBG("Kconsumerd fds (metadata and channel streams) sent");
346
f3ed775e
DG
347 return 0;
348
349error:
350 return ret;
351}
352
fac6795d 353/*
471d1693 354 * ust_connect_app
fac6795d
DG
355 *
356 * Return a socket connected to the libust communication socket
357 * of the application identified by the pid.
379473d2
DG
358 *
359 * If the pid is not found in the traceable list,
360 * return -1 to indicate error.
fac6795d 361 */
471d1693 362static int ust_connect_app(pid_t pid)
fac6795d 363{
91d76f53
DG
364 int sock;
365 struct ltt_traceable_app *lta;
379473d2 366
e07ae692
DG
367 DBG("Connect to application pid %d", pid);
368
91d76f53
DG
369 lta = find_app_by_pid(pid);
370 if (lta == NULL) {
371 /* App not found */
7442b2ba 372 DBG("Application pid %d not found", pid);
379473d2
DG
373 return -1;
374 }
fac6795d 375
91d76f53 376 sock = ustctl_connect_pid(lta->pid);
fac6795d 377 if (sock < 0) {
62d3069f 378 ERR("Fail connecting to the PID %d", pid);
fac6795d
DG
379 }
380
381 return sock;
382}
383
384/*
385 * notify_apps
386 *
387 * Notify apps by writing 42 to a named pipe using name.
388 * Every applications waiting for a ltt-sessiond will be notified
389 * and re-register automatically to the session daemon.
390 *
391 * Return open or write error value.
392 */
393static int notify_apps(const char *name)
394{
395 int fd;
396 int ret = -1;
397
e07ae692
DG
398 DBG("Notify the global application pipe");
399
fac6795d
DG
400 /* Try opening the global pipe */
401 fd = open(name, O_WRONLY);
402 if (fd < 0) {
403 goto error;
404 }
405
406 /* Notify by writing on the pipe */
407 ret = write(fd, "42", 2);
408 if (ret < 0) {
409 perror("write");
410 }
411
412error:
413 return ret;
414}
415
e065084a 416/*
5461b305 417 * setup_lttng_msg
ca95a216 418 *
5461b305
DG
419 * Setup the outgoing data buffer for the response (llm) by allocating the
420 * right amount of memory and copying the original information from the lsm
421 * structure.
ca95a216
DG
422 *
423 * Return total size of the buffer pointed by buf.
424 */
5461b305 425static int setup_lttng_msg(struct command_ctx *cmd_ctx, size_t size)
ca95a216 426{
f3ed775e 427 int ret, buf_size;
ca95a216 428
f3ed775e 429 buf_size = size;
5461b305
DG
430
431 cmd_ctx->llm = malloc(sizeof(struct lttcomm_lttng_msg) + buf_size);
432 if (cmd_ctx->llm == NULL) {
ca95a216 433 perror("malloc");
5461b305 434 ret = -ENOMEM;
ca95a216
DG
435 goto error;
436 }
437
5461b305
DG
438 /* Copy common data */
439 cmd_ctx->llm->cmd_type = cmd_ctx->lsm->cmd_type;
440 cmd_ctx->llm->pid = cmd_ctx->lsm->pid;
5461b305 441
5461b305
DG
442 cmd_ctx->llm->data_size = size;
443 cmd_ctx->lttng_msg_size = sizeof(struct lttcomm_lttng_msg) + buf_size;
444
ca95a216
DG
445 return buf_size;
446
447error:
448 return ret;
449}
450
7a485870
DG
451/*
452 * update_kernel_pollfd
453 *
454 * Update the kernel pollfd set of all channel fd available over
455 * all tracing session. Add the wakeup pipe at the end of the set.
456 */
457static int update_kernel_pollfd(void)
458{
459 int i = 0;
273ea72c
DG
460 /*
461 * The wakup pipe and the quit pipe are needed so the number of fds starts
462 * at 2 for those pipes.
463 */
464 unsigned int nb_fd = 2;
7a485870
DG
465 struct ltt_session *session;
466 struct ltt_kernel_channel *channel;
467
468 DBG("Updating kernel_pollfd");
469
470 /* Get the number of channel of all kernel session */
6c9cc2ab 471 lock_session_list();
b5541356
DG
472 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
473 lock_session(session);
7a485870 474 if (session->kernel_session == NULL) {
b5541356 475 unlock_session(session);
7a485870
DG
476 continue;
477 }
478 nb_fd += session->kernel_session->channel_count;
b5541356 479 unlock_session(session);
7a485870
DG
480 }
481
482 DBG("Resizing kernel_pollfd to size %d", nb_fd);
483
484 kernel_pollfd = realloc(kernel_pollfd, nb_fd * sizeof(struct pollfd));
485 if (kernel_pollfd == NULL) {
486 perror("malloc kernel_pollfd");
487 goto error;
488 }
489
b5541356
DG
490 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
491 lock_session(session);
7a485870 492 if (session->kernel_session == NULL) {
b5541356 493 unlock_session(session);
7a485870
DG
494 continue;
495 }
496 if (i >= nb_fd) {
497 ERR("To much channel for kernel_pollfd size");
b5541356 498 unlock_session(session);
7a485870
DG
499 break;
500 }
501 cds_list_for_each_entry(channel, &session->kernel_session->channel_list.head, list) {
502 kernel_pollfd[i].fd = channel->fd;
503 kernel_pollfd[i].events = POLLIN | POLLRDNORM;
504 i++;
505 }
b5541356 506 unlock_session(session);
7a485870 507 }
6c9cc2ab 508 unlock_session_list();
7a485870
DG
509
510 /* Adding wake up pipe */
273ea72c
DG
511 kernel_pollfd[nb_fd - 2].fd = kernel_poll_pipe[0];
512 kernel_pollfd[nb_fd - 2].events = POLLIN;
513
514 /* Adding the quit pipe */
515 kernel_pollfd[nb_fd - 1].fd = thread_quit_pipe[0];
7a485870
DG
516
517 return nb_fd;
518
519error:
6c9cc2ab 520 unlock_session_list();
7a485870
DG
521 return -1;
522}
523
524/*
525 * update_kernel_stream
526 *
527 * Find the channel fd from 'fd' over all tracing session. When found, check
528 * for new channel stream and send those stream fds to the kernel consumer.
529 *
530 * Useful for CPU hotplug feature.
531 */
532static int update_kernel_stream(int fd)
533{
534 int ret = 0;
535 struct ltt_session *session;
536 struct ltt_kernel_channel *channel;
537
538 DBG("Updating kernel streams for channel fd %d", fd);
539
6c9cc2ab 540 lock_session_list();
b5541356
DG
541 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
542 lock_session(session);
7a485870 543 if (session->kernel_session == NULL) {
b5541356 544 unlock_session(session);
7a485870
DG
545 continue;
546 }
547 cds_list_for_each_entry(channel, &session->kernel_session->channel_list.head, list) {
548 if (channel->fd == fd) {
549 DBG("Channel found, updating kernel streams");
550 ret = kernel_open_channel_stream(channel);
551 if (ret < 0) {
552 goto end;
553 }
554 /*
555 * Have we already sent fds to the consumer? If yes, it means that
556 * tracing is started so it is safe to send our updated stream fds.
557 */
558 if (session->kernel_session->kconsumer_fds_sent == 1) {
559 ret = send_kconsumerd_channel_fds(kconsumerd_cmd_sock, channel);
560 if (ret < 0) {
561 goto end;
562 }
563 }
564 goto end;
565 }
566 }
b5541356 567 unlock_session(session);
7a485870
DG
568 }
569
570end:
6c9cc2ab 571 unlock_session_list();
b5541356
DG
572 if (session) {
573 unlock_session(session);
574 }
7a485870
DG
575 return ret;
576}
577
578/*
579 * thread_manage_kernel
580 *
581 * This thread manage event coming from the kernel.
582 *
583 * Features supported in this thread:
584 * -) CPU Hotplug
585 */
586static void *thread_manage_kernel(void *data)
587{
588 int ret, i, nb_fd = 0;
589 char tmp;
590 int update_poll_flag = 1;
591
592 DBG("Thread manage kernel started");
593
594 while (1) {
595 if (update_poll_flag == 1) {
596 nb_fd = update_kernel_pollfd();
597 if (nb_fd < 0) {
598 goto error;
599 }
600 update_poll_flag = 0;
601 }
602
603 DBG("Polling on %d fds", nb_fd);
604
605 /* Poll infinite value of time */
606 ret = poll(kernel_pollfd, nb_fd, -1);
607 if (ret < 0) {
608 perror("poll kernel thread");
609 goto error;
610 } else if (ret == 0) {
611 /* Should not happen since timeout is infinite */
612 continue;
613 }
614
273ea72c
DG
615 /* Thread quit pipe has been closed. Killing thread. */
616 if (kernel_pollfd[nb_fd - 1].revents == POLLNVAL) {
617 goto error;
618 }
619
7a485870
DG
620 DBG("Kernel poll event triggered");
621
622 /*
623 * Check if the wake up pipe was triggered. If so, the kernel_pollfd
624 * must be updated.
625 */
273ea72c 626 switch (kernel_pollfd[nb_fd - 2].revents) {
b5541356 627 case POLLIN:
7a485870
DG
628 ret = read(kernel_poll_pipe[0], &tmp, 1);
629 update_poll_flag = 1;
630 continue;
b5541356
DG
631 case POLLERR:
632 goto error;
633 default:
634 break;
7a485870
DG
635 }
636
637 for (i = 0; i < nb_fd; i++) {
638 switch (kernel_pollfd[i].revents) {
639 /*
640 * New CPU detected by the kernel. Adding kernel stream to kernel
641 * session and updating the kernel consumer
642 */
643 case POLLIN | POLLRDNORM:
644 ret = update_kernel_stream(kernel_pollfd[i].fd);
645 if (ret < 0) {
646 continue;
647 }
648 break;
649 }
650 }
651 }
652
653error:
654 DBG("Kernel thread dying");
655 if (kernel_pollfd) {
656 free(kernel_pollfd);
657 }
273ea72c
DG
658
659 close(kernel_poll_pipe[0]);
660 close(kernel_poll_pipe[1]);
7a485870
DG
661 return NULL;
662}
663
1d4b027a
DG
664/*
665 * thread_manage_kconsumerd
666 *
667 * This thread manage the kconsumerd error sent
668 * back to the session daemon.
669 */
670static void *thread_manage_kconsumerd(void *data)
671{
273ea72c 672 int sock = 0, ret;
1d4b027a 673 enum lttcomm_return_code code;
273ea72c 674 struct pollfd pollfd[2];
1d4b027a
DG
675
676 DBG("[thread] Manage kconsumerd started");
677
678 ret = lttcomm_listen_unix_sock(kconsumerd_err_sock);
679 if (ret < 0) {
680 goto error;
681 }
682
273ea72c
DG
683 /* First fd is always the quit pipe */
684 pollfd[0].fd = thread_quit_pipe[0];
685
686 /* Apps socket */
687 pollfd[1].fd = kconsumerd_err_sock;
688 pollfd[1].events = POLLIN;
689
690 /* Inifinite blocking call, waiting for transmission */
691 ret = poll(pollfd, 2, -1);
692 if (ret < 0) {
693 perror("poll kconsumerd thread");
694 goto error;
695 }
696
697 /* Thread quit pipe has been closed. Killing thread. */
698 if (pollfd[0].revents == POLLNVAL) {
699 goto error;
700 } else if (pollfd[1].revents == POLLERR) {
701 ERR("Kconsumerd err socket poll error");
702 goto error;
703 }
704
1d4b027a
DG
705 sock = lttcomm_accept_unix_sock(kconsumerd_err_sock);
706 if (sock < 0) {
707 goto error;
708 }
709
712ea556 710 /* Getting status code from kconsumerd */
1d4b027a
DG
711 ret = lttcomm_recv_unix_sock(sock, &code, sizeof(enum lttcomm_return_code));
712 if (ret <= 0) {
713 goto error;
714 }
715
716 if (code == KCONSUMERD_COMMAND_SOCK_READY) {
717 kconsumerd_cmd_sock = lttcomm_connect_unix_sock(kconsumerd_cmd_unix_sock_path);
718 if (kconsumerd_cmd_sock < 0) {
712ea556 719 sem_post(&kconsumerd_sem);
1d4b027a
DG
720 perror("kconsumerd connect");
721 goto error;
722 }
723 /* Signal condition to tell that the kconsumerd is ready */
724 sem_post(&kconsumerd_sem);
725 DBG("Kconsumerd command socket ready");
726 } else {
6f61d021 727 DBG("Kconsumerd error when waiting for SOCK_READY : %s",
1d4b027a
DG
728 lttcomm_get_readable_code(-code));
729 goto error;
730 }
731
712ea556
DG
732 /* Wait for any kconsumerd error */
733 ret = lttcomm_recv_unix_sock(sock, &code, sizeof(enum lttcomm_return_code));
734 if (ret <= 0) {
735 ERR("Kconsumerd closed the command socket");
736 goto error;
6f61d021 737 }
1d4b027a 738
712ea556
DG
739 ERR("Kconsumerd return code : %s", lttcomm_get_readable_code(-code));
740
1d4b027a 741error:
6f61d021 742 DBG("Kconsumerd thread dying");
273ea72c
DG
743 if (kconsumerd_err_sock) {
744 close(kconsumerd_err_sock);
745 }
746 if (kconsumerd_cmd_sock) {
747 close(kconsumerd_cmd_sock);
748 }
749 if (sock) {
750 close(sock);
751 }
752
753 unlink(kconsumerd_err_unix_sock_path);
754 unlink(kconsumerd_cmd_unix_sock_path);
755
756 kconsumerd_pid = 0;
1d4b027a
DG
757 return NULL;
758}
759
760/*
761 * thread_manage_apps
762 *
763 * This thread manage the application socket communication
764 */
765static void *thread_manage_apps(void *data)
766{
273ea72c
DG
767 int sock = 0, ret;
768 struct pollfd pollfd[2];
1d4b027a
DG
769
770 /* TODO: Something more elegant is needed but fine for now */
771 /* FIXME: change all types to either uint8_t, uint32_t, uint64_t
772 * for 32-bit vs 64-bit compat processes. */
773 /* replicate in ust with version number */
774 struct {
775 int reg; /* 1:register, 0:unregister */
776 pid_t pid;
777 uid_t uid;
778 } reg_msg;
779
780 DBG("[thread] Manage apps started");
781
782 ret = lttcomm_listen_unix_sock(apps_sock);
783 if (ret < 0) {
784 goto error;
785 }
786
273ea72c
DG
787 /* First fd is always the quit pipe */
788 pollfd[0].fd = thread_quit_pipe[0];
789
790 /* Apps socket */
791 pollfd[1].fd = apps_sock;
792 pollfd[1].events = POLLIN;
793
1d4b027a
DG
794 /* Notify all applications to register */
795 notify_apps(default_global_apps_pipe);
796
797 while (1) {
798 DBG("Accepting application registration");
273ea72c
DG
799
800 /* Inifinite blocking call, waiting for transmission */
801 ret = poll(pollfd, 2, -1);
802 if (ret < 0) {
803 perror("poll apps thread");
804 goto error;
805 }
806
807 /* Thread quit pipe has been closed. Killing thread. */
808 if (pollfd[0].revents == POLLNVAL) {
809 goto error;
810 } else if (pollfd[1].revents == POLLERR) {
811 ERR("Apps socket poll error");
812 goto error;
813 }
814
1d4b027a
DG
815 sock = lttcomm_accept_unix_sock(apps_sock);
816 if (sock < 0) {
817 goto error;
818 }
819
273ea72c
DG
820 /*
821 * Basic recv here to handle the very simple data
1d4b027a
DG
822 * that the libust send to register (reg_msg).
823 */
824 ret = recv(sock, &reg_msg, sizeof(reg_msg), 0);
825 if (ret < 0) {
826 perror("recv");
827 continue;
828 }
829
830 /* Add application to the global traceable list */
831 if (reg_msg.reg == 1) {
832 /* Registering */
833 ret = register_traceable_app(reg_msg.pid, reg_msg.uid);
834 if (ret < 0) {
835 /* register_traceable_app only return an error with
836 * ENOMEM. At this point, we better stop everything.
837 */
838 goto error;
839 }
840 } else {
841 /* Unregistering */
842 unregister_traceable_app(reg_msg.pid);
843 }
844 }
845
846error:
273ea72c
DG
847 DBG("Apps thread dying");
848 if (apps_sock) {
849 close(apps_sock);
850 }
851 if (sock) {
852 close(sock);
853 }
1d4b027a 854
273ea72c 855 unlink(apps_unix_sock_path);
1d4b027a
DG
856 return NULL;
857}
858
8c0faa1d 859/*
693bd40b 860 * spawn_kconsumerd_thread
8c0faa1d
DG
861 *
862 * Start the thread_manage_kconsumerd. This must be done after a kconsumerd
863 * exec or it will fails.
864 */
693bd40b 865static int spawn_kconsumerd_thread(void)
8c0faa1d
DG
866{
867 int ret;
868
869 /* Setup semaphore */
870 sem_init(&kconsumerd_sem, 0, 0);
871
1d4b027a 872 ret = pthread_create(&kconsumerd_thread, NULL, thread_manage_kconsumerd, (void *) NULL);
8c0faa1d
DG
873 if (ret != 0) {
874 perror("pthread_create kconsumerd");
875 goto error;
876 }
877
693bd40b 878 /* Wait for the kconsumerd thread to be ready */
8c0faa1d
DG
879 sem_wait(&kconsumerd_sem);
880
712ea556
DG
881 if (kconsumerd_pid == 0) {
882 ERR("Kconsumerd did not start");
883 goto error;
884 }
885
8c0faa1d
DG
886 return 0;
887
888error:
712ea556 889 ret = LTTCOMM_KERN_CONSUMER_FAIL;
8c0faa1d
DG
890 return ret;
891}
892
893/*
693bd40b 894 * spawn_kconsumerd
8c0faa1d 895 *
693bd40b
DG
896 * Fork and exec a kernel consumer daemon (kconsumerd).
897 *
898 * NOTE: It is very important to fork a kconsumerd BEFORE opening any kernel
899 * file descriptor using the libkernelctl or kernel-ctl functions. So, a
900 * kernel consumer MUST only be spawned before creating a kernel session.
8c0faa1d
DG
901 *
902 * Return pid if successful else -1.
903 */
693bd40b 904static pid_t spawn_kconsumerd(void)
8c0faa1d
DG
905{
906 int ret;
907 pid_t pid;
908
c49dc785
DG
909 DBG("Spawning kconsumerd");
910
8c0faa1d
DG
911 pid = fork();
912 if (pid == 0) {
913 /*
914 * Exec kconsumerd.
915 */
2b1173d8 916 execlp("ltt-kconsumerd", "ltt-kconsumerd", "--verbose", NULL);
8c0faa1d
DG
917 if (errno != 0) {
918 perror("kernel start consumer exec");
919 }
920 exit(EXIT_FAILURE);
921 } else if (pid > 0) {
922 ret = pid;
923 goto error;
924 } else {
925 perror("kernel start consumer fork");
926 ret = -errno;
927 goto error;
928 }
929
930error:
931 return ret;
932}
933
693bd40b
DG
934/*
935 * start_kconsumerd
936 *
937 * Spawn the kconsumerd daemon and session daemon thread.
938 */
939static int start_kconsumerd(void)
940{
941 int ret;
942
693bd40b 943 pthread_mutex_lock(&kconsumerd_pid_mutex);
c49dc785 944 if (kconsumerd_pid != 0) {
817153bb 945 pthread_mutex_unlock(&kconsumerd_pid_mutex);
c49dc785
DG
946 goto end;
947 }
693bd40b 948
c49dc785
DG
949 ret = spawn_kconsumerd();
950 if (ret < 0) {
951 ERR("Spawning kconsumerd failed");
952 ret = LTTCOMM_KERN_CONSUMER_FAIL;
953 pthread_mutex_unlock(&kconsumerd_pid_mutex);
954 goto error;
693bd40b 955 }
c49dc785
DG
956
957 /* Setting up the global kconsumerd_pid */
958 kconsumerd_pid = ret;
693bd40b
DG
959 pthread_mutex_unlock(&kconsumerd_pid_mutex);
960
6f61d021
DG
961 DBG("Kconsumerd pid %d", ret);
962
693bd40b 963 DBG("Spawning kconsumerd thread");
693bd40b
DG
964 ret = spawn_kconsumerd_thread();
965 if (ret < 0) {
966 ERR("Fatal error spawning kconsumerd thread");
693bd40b
DG
967 goto error;
968 }
969
c49dc785 970end:
693bd40b
DG
971 return 0;
972
973error:
974 return ret;
975}
976
b73401da
DG
977/*
978 * modprobe_kernel_modules
979 */
980static int modprobe_kernel_modules(void)
981{
982 int ret = 0, i = 0;
983 char modprobe[256];
984
985 while (kernel_modules_list[i] != NULL) {
986 ret = snprintf(modprobe, sizeof(modprobe), "/sbin/modprobe %s",
987 kernel_modules_list[i]);
988 if (ret < 0) {
989 perror("snprintf modprobe");
990 goto error;
991 }
992 ret = system(modprobe);
993 if (ret < 0) {
994 ERR("Unable to load module %s", kernel_modules_list[i]);
995 }
996 DBG("Modprobe successfully %s", kernel_modules_list[i]);
997 i++;
998 }
999
1000error:
1001 return ret;
1002}
1003
1004/*
1005 * mount_debugfs
1006 */
1007static int mount_debugfs(char *path)
1008{
1009 int ret;
1010 char *type = "debugfs";
1011
1012 ret = mkdir_recursive(path, S_IRWXU | S_IRWXG);
1013 if (ret < 0) {
1014 goto error;
1015 }
1016
1017 ret = mount(type, path, type, 0, NULL);
1018 if (ret < 0) {
1019 perror("mount debugfs");
1020 goto error;
1021 }
1022
1023 DBG("Mounted debugfs successfully at %s", path);
1024
1025error:
1026 return ret;
1027}
1028
8c0faa1d 1029/*
f3ed775e 1030 * init_kernel_tracer
8c0faa1d 1031 *
f3ed775e 1032 * Setup necessary data for kernel tracer action.
8c0faa1d 1033 */
f3ed775e 1034static void init_kernel_tracer(void)
8c0faa1d 1035{
b73401da
DG
1036 int ret;
1037 char *proc_mounts = "/proc/mounts";
1038 char line[256];
1039 char *debugfs_path = NULL, *lttng_path;
1040 FILE *fp;
1041
1042 /* Detect debugfs */
1043 fp = fopen(proc_mounts, "r");
1044 if (fp == NULL) {
1045 ERR("Unable to probe %s", proc_mounts);
1046 goto error;
1047 }
1048
1049 while (fgets(line, sizeof(line), fp) != NULL) {
1050 if (strstr(line, "debugfs") != NULL) {
1051 /* Remove first string */
1052 strtok(line, " ");
1053 /* Dup string here so we can reuse line later on */
1054 debugfs_path = strdup(strtok(NULL, " "));
1055 DBG("Got debugfs path : %s", debugfs_path);
1056 break;
1057 }
1058 }
1059
1060 fclose(fp);
1061
1062 /* Mount debugfs if needded */
1063 if (debugfs_path == NULL) {
1064 ret = asprintf(&debugfs_path, "/mnt/debugfs");
1065 if (ret < 0) {
1066 perror("asprintf debugfs path");
1067 goto error;
1068 }
1069 ret = mount_debugfs(debugfs_path);
1070 if (ret < 0) {
1071 goto error;
1072 }
1073 }
1074
1075 /* Modprobe lttng kernel modules */
1076 ret = modprobe_kernel_modules();
1077 if (ret < 0) {
1078 goto error;
1079 }
1080
1081 /* Setup lttng kernel path */
1082 ret = asprintf(&lttng_path, "%s/lttng", debugfs_path);
1083 if (ret < 0) {
1084 perror("asprintf lttng path");
1085 goto error;
1086 }
1087
1088 /* Open debugfs lttng */
1089 kernel_tracer_fd = open(lttng_path, O_RDWR);
f3ed775e 1090 if (kernel_tracer_fd < 0) {
b73401da
DG
1091 DBG("Failed to open %s", lttng_path);
1092 goto error;
8c0faa1d
DG
1093 }
1094
b73401da
DG
1095 free(lttng_path);
1096 free(debugfs_path);
f3ed775e 1097 DBG("Kernel tracer fd %d", kernel_tracer_fd);
b73401da
DG
1098 return;
1099
1100error:
1101 if (lttng_path) {
1102 free(lttng_path);
1103 }
1104 if (debugfs_path) {
1105 free(debugfs_path);
1106 }
1107 WARN("No kernel tracer available");
1108 kernel_tracer_fd = 0;
1109 return;
f3ed775e 1110}
33a2b854 1111
f3ed775e
DG
1112/*
1113 * start_kernel_trace
1114 *
1115 * Start tracing by creating trace directory and sending FDs to the kernel
1116 * consumer.
1117 */
1118static int start_kernel_trace(struct ltt_kernel_session *session)
1119{
1120 int ret;
8c0faa1d 1121
f3ed775e
DG
1122 if (session->kconsumer_fds_sent == 0) {
1123 ret = send_kconsumerd_fds(kconsumerd_cmd_sock, session);
1124 if (ret < 0) {
1125 ERR("Send kconsumerd fds failed");
1126 ret = LTTCOMM_KERN_CONSUMER_FAIL;
1127 goto error;
1128 }
1d4b027a 1129
f3ed775e
DG
1130 session->kconsumer_fds_sent = 1;
1131 }
1d4b027a
DG
1132
1133error:
1134 return ret;
8c0faa1d
DG
1135}
1136
7a485870
DG
1137/*
1138 * Notify kernel thread to update it's pollfd.
1139 */
1140static int notify_kernel_pollfd(void)
1141{
1142 int ret;
1143
1144 /* Inform kernel thread of the new kernel channel */
1145 ret = write(kernel_poll_pipe[1], "!", 1);
1146 if (ret < 0) {
1147 perror("write kernel poll pipe");
1148 }
1149
1150 return ret;
1151}
1152
8c0faa1d 1153/*
f3ed775e 1154 * init_default_channel
8c0faa1d 1155 *
f3ed775e 1156 * Allocate a channel structure and fill it.
8c0faa1d 1157 */
f3ed775e 1158static struct lttng_channel *init_default_channel(void)
8c0faa1d 1159{
f3ed775e 1160 struct lttng_channel *chan;
1d4b027a 1161
f3ed775e
DG
1162 chan = malloc(sizeof(struct lttng_channel));
1163 if (chan == NULL) {
1164 perror("init channel malloc");
1165 goto error;
8c0faa1d 1166 }
1d4b027a 1167
f3ed775e
DG
1168 if (snprintf(chan->name, NAME_MAX, DEFAULT_CHANNEL_NAME) < 0) {
1169 perror("snprintf defautl channel name");
1170 return NULL;
1171 }
1172
1173 chan->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
1174 chan->attr.subbuf_size = DEFAULT_CHANNEL_SUBBUF_SIZE;
1175 chan->attr.num_subbuf = DEFAULT_CHANNEL_SUBBUF_NUM;
1176 chan->attr.switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
1177 chan->attr.read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
7d452e12 1178 chan->attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
1d4b027a
DG
1179
1180error:
f3ed775e 1181 return chan;
8c0faa1d
DG
1182}
1183
333f285e 1184/*
f3ed775e 1185 * create_kernel_session
333f285e 1186 *
f3ed775e 1187 * Create a kernel tracer session then create the default channel.
333f285e 1188 */
f3ed775e 1189static int create_kernel_session(struct ltt_session *session)
333f285e 1190{
f3ed775e
DG
1191 int ret;
1192 struct lttng_channel *chan;
1193
1194 DBG("Creating kernel session");
1195
1196 ret = kernel_create_session(session, kernel_tracer_fd);
1197 if (ret < 0) {
1198 ret = LTTCOMM_KERN_SESS_FAIL;
1199 goto error;
333f285e
DG
1200 }
1201
f3ed775e
DG
1202 chan = init_default_channel();
1203 if (chan == NULL) {
1204 ret = LTTCOMM_FATAL;
1205 goto error;
1206 }
1207
7a485870
DG
1208 ret = mkdir_recursive(session->path, S_IRWXU | S_IRWXG );
1209 if (ret < 0) {
1210 if (ret != EEXIST) {
1211 ERR("Trace directory creation error");
1212 goto error;
1213 }
1214 }
1215
f3ed775e
DG
1216 DBG("Creating default kernel channel %s", DEFAULT_CHANNEL_NAME);
1217
b082db07 1218 ret = kernel_create_channel(session->kernel_session, chan, session->path);
f3ed775e
DG
1219 if (ret < 0) {
1220 ret = LTTCOMM_KERN_CHAN_FAIL;
1221 goto error;
1222 }
1223
7a485870
DG
1224 ret = notify_kernel_pollfd();
1225
f3ed775e
DG
1226error:
1227 return ret;
333f285e
DG
1228}
1229
6c9cc2ab
DG
1230/*
1231 * Using the session list, filled a lttng_session array to send back to the
1232 * client for session listing.
1233 *
1234 * The session list lock MUST be acquired before calling this function. Use
1235 * lock_session_list() and unlock_session_list().
1236 */
1237static void list_lttng_sessions(struct lttng_session *sessions)
1238{
1239 int i = 0;
1240 struct ltt_session *session;
1241
1242 DBG("Getting all available session");
1243 /*
1244 * Iterate over session list and append data after the control struct in
1245 * the buffer.
1246 */
1247 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
1248 strncpy(sessions[i].path, session->path, PATH_MAX);
1249 strncpy(sessions[i].name, session->name, NAME_MAX);
1250 i++;
1251 }
1252}
1253
fac6795d
DG
1254/*
1255 * process_client_msg
1256 *
5461b305
DG
1257 * Process the command requested by the lttng client within the command
1258 * context structure. This function make sure that the return structure (llm)
1259 * is set and ready for transmission before returning.
fac6795d 1260 *
e065084a 1261 * Return any error encountered or 0 for success.
fac6795d 1262 */
5461b305 1263static int process_client_msg(struct command_ctx *cmd_ctx)
fac6795d 1264{
5461b305 1265 int ret;
fac6795d 1266
5461b305 1267 DBG("Processing client command %d", cmd_ctx->lsm->cmd_type);
fac6795d 1268
f3ed775e 1269 /* Listing commands don't need a session */
5461b305 1270 switch (cmd_ctx->lsm->cmd_type) {
5e16da05
MD
1271 case LTTNG_CREATE_SESSION:
1272 case LTTNG_LIST_SESSIONS:
f3ed775e
DG
1273 case LTTNG_LIST_EVENTS:
1274 case LTTNG_KERNEL_LIST_EVENTS:
1275 case LTTNG_LIST_TRACEABLE_APPS:
5e16da05
MD
1276 break;
1277 default:
f3ed775e
DG
1278 DBG("Getting session %s by name", cmd_ctx->lsm->session_name);
1279 cmd_ctx->session = find_session_by_name(cmd_ctx->lsm->session_name);
5461b305 1280 if (cmd_ctx->session == NULL) {
f3ed775e
DG
1281 /* If session name not found */
1282 if (cmd_ctx->lsm->session_name != NULL) {
1283 ret = LTTCOMM_SESS_NOT_FOUND;
1284 } else { /* If no session name specified */
1285 ret = LTTCOMM_SELECT_SESS;
1286 }
5461b305 1287 goto error;
b5541356
DG
1288 } else {
1289 /* Acquire lock for the session */
1290 lock_session(cmd_ctx->session);
379473d2 1291 }
5e16da05 1292 break;
379473d2
DG
1293 }
1294
f3ed775e
DG
1295 /*
1296 * Check kernel command for kernel session.
1297 */
20fe2104 1298 switch (cmd_ctx->lsm->cmd_type) {
d65106b1 1299 case LTTNG_KERNEL_ADD_CONTEXT:
f3ed775e
DG
1300 case LTTNG_KERNEL_CREATE_CHANNEL:
1301 case LTTNG_KERNEL_DISABLE_ALL_EVENT:
1302 case LTTNG_KERNEL_DISABLE_CHANNEL:
1303 case LTTNG_KERNEL_DISABLE_EVENT:
1304 case LTTNG_KERNEL_ENABLE_ALL_EVENT:
1305 case LTTNG_KERNEL_ENABLE_CHANNEL:
1306 case LTTNG_KERNEL_ENABLE_EVENT:
1307 case LTTNG_KERNEL_LIST_EVENTS:
333f285e 1308 /* Kernel tracer check */
20fe2104 1309 if (kernel_tracer_fd == 0) {
333f285e
DG
1310 init_kernel_tracer();
1311 if (kernel_tracer_fd == 0) {
1312 ret = LTTCOMM_KERN_NA;
1313 goto error;
1314 }
20fe2104 1315 }
f3ed775e
DG
1316
1317 /* Need a session for kernel command */
1318 if (cmd_ctx->lsm->cmd_type != LTTNG_KERNEL_LIST_EVENTS &&
1319 cmd_ctx->session->kernel_session == NULL) {
7b395890 1320
f3ed775e
DG
1321 ret = create_kernel_session(cmd_ctx->session);
1322 if (ret < 0) {
1323 ret = LTTCOMM_KERN_SESS_FAIL;
1324 goto error;
1325 }
1326
7b395890 1327 /* Start the kernel consumer daemon */
f3ed775e
DG
1328 if (kconsumerd_pid == 0) {
1329 ret = start_kconsumerd();
1330 if (ret < 0) {
1331 goto error;
1332 }
1333 }
1334 }
20fe2104
DG
1335 }
1336
471d1693 1337 /* Connect to ust apps if available pid */
5461b305 1338 if (cmd_ctx->lsm->pid > 0) {
471d1693 1339 /* Connect to app using ustctl API */
5461b305
DG
1340 cmd_ctx->ust_sock = ust_connect_app(cmd_ctx->lsm->pid);
1341 if (cmd_ctx->ust_sock < 0) {
471d1693 1342 ret = LTTCOMM_NO_TRACEABLE;
5461b305 1343 goto error;
471d1693
DG
1344 }
1345 }
1346
fac6795d 1347 /* Process by command type */
5461b305 1348 switch (cmd_ctx->lsm->cmd_type) {
d65106b1
DG
1349 case LTTNG_KERNEL_ADD_CONTEXT:
1350 {
1351 int found = 0, no_event = 0;
1352 struct ltt_kernel_channel *chan;
1353 struct ltt_kernel_event *event;
1354
1355 /* Setup lttng message with no payload */
1356 ret = setup_lttng_msg(cmd_ctx, 0);
1357 if (ret < 0) {
1358 goto setup_error;
1359 }
1360
1361 /* Check if event name is given */
1362 if (strlen(cmd_ctx->lsm->u.context.event_name) == 0) {
1363 no_event = 1;
1364 }
1365
1366 if (strlen(cmd_ctx->lsm->u.context.channel_name) == 0) {
1367 /* Go over all channels */
1368 DBG("Adding context to all channels");
1369 cds_list_for_each_entry(chan,
1370 &cmd_ctx->session->kernel_session->channel_list.head, list) {
1371 if (no_event) {
1372 ret = kernel_add_channel_context(chan,
1373 &cmd_ctx->lsm->u.context.ctx);
1374 if (ret < 0) {
1375 continue;
1376 }
1377 } else {
1378 event = get_kernel_event_by_name(cmd_ctx->lsm->u.context.event_name, chan);
1379 if (event != NULL) {
1380 ret = kernel_add_event_context(event,
1381 &cmd_ctx->lsm->u.context.ctx);
1382 if (ret < 0) {
1383 ret = LTTCOMM_KERN_CONTEXT_FAIL;
1384 goto error;
1385 }
1386 found = 1;
1387 break;
1388 }
1389 }
1390 }
1391 } else {
1392 chan = get_kernel_channel_by_name(cmd_ctx->lsm->u.context.channel_name,
1393 cmd_ctx->session->kernel_session);
1394 if (chan == NULL) {
1395 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
1396 goto error;
1397 }
1398
1399 if (no_event) {
1400 ret = kernel_add_channel_context(chan,
1401 &cmd_ctx->lsm->u.context.ctx);
1402 if (ret < 0) {
1403 ret = LTTCOMM_KERN_CONTEXT_FAIL;
1404 goto error;
1405 }
1406 } else {
1407 event = get_kernel_event_by_name(cmd_ctx->lsm->u.context.event_name, chan);
1408 if (event != NULL) {
1409 ret = kernel_add_event_context(event,
1410 &cmd_ctx->lsm->u.context.ctx);
1411 if (ret < 0) {
1412 ret = LTTCOMM_KERN_CONTEXT_FAIL;
1413 goto error;
1414 }
1415 }
1416 }
1417 }
1418
1419 if (!found && !no_event) {
1420 ret = LTTCOMM_NO_EVENT;
1421 goto error;
1422 }
1423
1424 ret = LTTCOMM_OK;
1425 break;
1426 }
f3ed775e 1427 case LTTNG_KERNEL_CREATE_CHANNEL:
20fe2104 1428 {
f3ed775e 1429 /* Setup lttng message with no payload */
20fe2104
DG
1430 ret = setup_lttng_msg(cmd_ctx, 0);
1431 if (ret < 0) {
1432 goto setup_error;
1433 }
1434
f3ed775e 1435 /* Kernel tracer */
8c0faa1d
DG
1436 DBG("Creating kernel channel");
1437
f3ed775e 1438 ret = kernel_create_channel(cmd_ctx->session->kernel_session,
b082db07 1439 &cmd_ctx->lsm->u.channel.chan, cmd_ctx->session->path);
20fe2104
DG
1440 if (ret < 0) {
1441 ret = LTTCOMM_KERN_CHAN_FAIL;
1442 goto error;
1443 }
1444
7a485870
DG
1445 ret = notify_kernel_pollfd();
1446 if (ret < 0) {
1447 ret = LTTCOMM_FATAL;
1448 goto error;
1449 }
1450
20fe2104
DG
1451 ret = LTTCOMM_OK;
1452 break;
1453 }
26cc6b4e
DG
1454 case LTTNG_KERNEL_DISABLE_CHANNEL:
1455 {
1456 struct ltt_kernel_channel *chan;
1457
1458 /* Setup lttng message with no payload */
1459 ret = setup_lttng_msg(cmd_ctx, 0);
1460 if (ret < 0) {
1461 goto setup_error;
1462 }
1463
1464 chan = get_kernel_channel_by_name(cmd_ctx->lsm->u.disable.channel_name,
1465 cmd_ctx->session->kernel_session);
1466 if (chan == NULL) {
1467 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
1468 goto error;
1469 } else if (chan->enabled == 1) {
1470 ret = kernel_disable_channel(chan);
1471 if (ret < 0) {
1472 if (ret != EEXIST) {
1473 ret = LTTCOMM_KERN_CHAN_DISABLE_FAIL;
1474 }
1475 goto error;
1476 }
1477 }
1478
1479 kernel_wait_quiescent(kernel_tracer_fd);
1480 ret = LTTCOMM_OK;
1481 break;
1482 }
e953ef25
DG
1483 case LTTNG_KERNEL_DISABLE_EVENT:
1484 {
e953ef25 1485 struct ltt_kernel_channel *chan;
19e70852 1486 struct ltt_kernel_event *ev;
e953ef25
DG
1487
1488 /* Setup lttng message with no payload */
1489 ret = setup_lttng_msg(cmd_ctx, 0);
1490 if (ret < 0) {
1491 goto setup_error;
1492 }
1493
19e70852
DG
1494 chan = get_kernel_channel_by_name(cmd_ctx->lsm->u.disable.channel_name,
1495 cmd_ctx->session->kernel_session);
1496 if (chan == NULL) {
1497 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
1498 goto error;
1499 }
e953ef25 1500
19e70852
DG
1501 ev = get_kernel_event_by_name(cmd_ctx->lsm->u.disable.name, chan);
1502 if (ev != NULL) {
1503 DBG("Disabling kernel event %s for channel %s.",
1504 cmd_ctx->lsm->u.disable.name, cmd_ctx->lsm->u.disable.channel_name);
1505 ret = kernel_disable_event(ev);
1506 if (ret < 0) {
1507 ret = LTTCOMM_KERN_ENABLE_FAIL;
1508 goto error;
e953ef25
DG
1509 }
1510 }
1511
19e70852
DG
1512 kernel_wait_quiescent(kernel_tracer_fd);
1513 ret = LTTCOMM_OK;
e953ef25
DG
1514 break;
1515 }
950131af
DG
1516 case LTTNG_KERNEL_DISABLE_ALL_EVENT:
1517 {
1518 struct ltt_kernel_channel *chan;
1519 struct ltt_kernel_event *ev;
1520
1521 /* Setup lttng message with no payload */
1522 ret = setup_lttng_msg(cmd_ctx, 0);
1523 if (ret < 0) {
1524 goto setup_error;
1525 }
1526
1527 DBG("Disabling all enabled kernel events");
1528
1529 chan = get_kernel_channel_by_name(cmd_ctx->lsm->u.disable.channel_name,
1530 cmd_ctx->session->kernel_session);
1531 if (chan == NULL) {
1532 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
1533 goto error;
1534 }
1535
1536 /* For each event in the kernel session */
1537 cds_list_for_each_entry(ev, &chan->events_list.head, list) {
1538 DBG("Disabling kernel event %s for channel %s.",
1539 ev->event->name, cmd_ctx->lsm->u.disable.channel_name);
1540 ret = kernel_disable_event(ev);
1541 if (ret < 0) {
1542 continue;
1543 }
1544 }
1545
1546 /* Quiescent wait after event disable */
1547 kernel_wait_quiescent(kernel_tracer_fd);
1548 ret = LTTCOMM_OK;
1549 break;
1550 }
d36b8583
DG
1551 case LTTNG_KERNEL_ENABLE_CHANNEL:
1552 {
1553 struct ltt_kernel_channel *chan;
1554
1555 /* Setup lttng message with no payload */
1556 ret = setup_lttng_msg(cmd_ctx, 0);
1557 if (ret < 0) {
1558 goto setup_error;
1559 }
1560
1561 chan = get_kernel_channel_by_name(cmd_ctx->lsm->u.enable.channel_name,
1562 cmd_ctx->session->kernel_session);
1563 if (chan == NULL) {
1564 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
1565 goto error;
1566 } else if (chan->enabled == 0) {
1567 ret = kernel_enable_channel(chan);
1568 if (ret < 0) {
1569 if (ret != EEXIST) {
1570 ret = LTTCOMM_KERN_CHAN_ENABLE_FAIL;
1571 }
1572 goto error;
1573 }
1574 }
1575
1576 kernel_wait_quiescent(kernel_tracer_fd);
1577 ret = LTTCOMM_OK;
1578 break;
1579 }
f3ed775e 1580 case LTTNG_KERNEL_ENABLE_EVENT:
894be886 1581 {
f3ed775e 1582 struct ltt_kernel_channel *chan;
19e70852 1583 struct ltt_kernel_event *ev;
f3ed775e 1584
894be886
DG
1585 /* Setup lttng message with no payload */
1586 ret = setup_lttng_msg(cmd_ctx, 0);
1587 if (ret < 0) {
1588 goto setup_error;
1589 }
1590
19e70852
DG
1591 chan = get_kernel_channel_by_name(cmd_ctx->lsm->u.enable.channel_name,
1592 cmd_ctx->session->kernel_session);
1593 if (chan == NULL) {
1594 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
1595 goto error;
f34daff7
DG
1596 }
1597
19e70852
DG
1598 ev = get_kernel_event_by_name(cmd_ctx->lsm->u.enable.event.name, chan);
1599 if (ev == NULL) {
1600 DBG("Creating kernel event %s for channel %s.",
950131af 1601 cmd_ctx->lsm->u.enable.event.name, chan->channel->name);
19e70852 1602 ret = kernel_create_event(&cmd_ctx->lsm->u.enable.event, chan);
f3ed775e 1603 } else {
19e70852 1604 DBG("Enabling kernel event %s for channel %s.",
950131af 1605 cmd_ctx->lsm->u.enable.event.name, chan->channel->name);
19e70852
DG
1606 ret = kernel_enable_event(ev);
1607 }
1608
1609 if (ret < 0) {
1610 ret = LTTCOMM_KERN_ENABLE_FAIL;
1611 goto error;
f3ed775e 1612 }
19e70852
DG
1613
1614 kernel_wait_quiescent(kernel_tracer_fd);
1615 ret = LTTCOMM_OK;
894be886
DG
1616 break;
1617 }
f3ed775e 1618 case LTTNG_KERNEL_ENABLE_ALL_EVENT:
33a2b854 1619 {
950131af 1620 int pos, size;
33a2b854 1621 char *event_list, *event, *ptr;
f3ed775e 1622 struct ltt_kernel_channel *chan;
950131af
DG
1623 struct ltt_kernel_event *ev;
1624 struct lttng_event ev_attr;
33a2b854
DG
1625
1626 /* Setup lttng message with no payload */
1627 ret = setup_lttng_msg(cmd_ctx, 0);
1628 if (ret < 0) {
1629 goto setup_error;
1630 }
1631
1632 DBG("Enabling all kernel event");
1633
950131af
DG
1634 chan = get_kernel_channel_by_name(cmd_ctx->lsm->u.enable.channel_name,
1635 cmd_ctx->session->kernel_session);
1636 if (chan == NULL) {
1637 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
33a2b854
DG
1638 goto error;
1639 }
1640
950131af
DG
1641 /* For each event in the kernel session */
1642 cds_list_for_each_entry(ev, &chan->events_list.head, list) {
1643 DBG("Enabling kernel event %s for channel %s.",
1644 ev->event->name, chan->channel->name);
1645 ret = kernel_enable_event(ev);
1646 if (ret < 0) {
1647 continue;
f3ed775e
DG
1648 }
1649 }
1650
950131af
DG
1651 size = kernel_list_events(kernel_tracer_fd, &event_list);
1652 if (size < 0) {
1653 ret = LTTCOMM_KERN_LIST_FAIL;
f3ed775e
DG
1654 goto error;
1655 }
1656
33a2b854
DG
1657 ptr = event_list;
1658 while ((size = sscanf(ptr, "event { name = %m[^;]; };%n\n", &event, &pos)) == 1) {
950131af
DG
1659 ev = get_kernel_event_by_name(event, chan);
1660 if (ev == NULL) {
1661 strncpy(ev_attr.name, event, LTTNG_SYM_NAME_LEN);
1662 /* Default event type for enable all */
1663 ev_attr.type = LTTNG_EVENT_TRACEPOINTS;
1664 /* Enable each single tracepoint event */
1665 ret = kernel_create_event(&ev_attr, chan);
1666 if (ret < 0) {
1667 /* Ignore error here and continue */
950131af 1668 }
33a2b854 1669 }
950131af 1670
33a2b854
DG
1671 /* Move pointer to the next line */
1672 ptr += pos + 1;
1673 free(event);
1674 }
1675
1676 free(event_list);
1677
f3ed775e
DG
1678 /* Quiescent wait after event enable */
1679 kernel_wait_quiescent(kernel_tracer_fd);
33a2b854
DG
1680 ret = LTTCOMM_OK;
1681 break;
1682 }
f3ed775e 1683 case LTTNG_KERNEL_LIST_EVENTS:
2ef84c95
DG
1684 {
1685 char *event_list;
f3ed775e
DG
1686 ssize_t size = 0;
1687
1688 DBG("Listing kernel events");
2ef84c95
DG
1689
1690 size = kernel_list_events(kernel_tracer_fd, &event_list);
1691 if (size < 0) {
1692 ret = LTTCOMM_KERN_LIST_FAIL;
1693 goto error;
1694 }
1695
1696 /*
1697 * Setup lttng message with payload size set to the event list size in
1698 * bytes and then copy list into the llm payload.
1699 */
1700 ret = setup_lttng_msg(cmd_ctx, size);
1701 if (ret < 0) {
1702 goto setup_error;
1703 }
1704
1705 /* Copy event list into message payload */
1706 memcpy(cmd_ctx->llm->payload, event_list, size);
1707
1708 free(event_list);
1709
1710 ret = LTTCOMM_OK;
1711 break;
1712 }
f3ed775e 1713 case LTTNG_START_TRACE:
8c0faa1d
DG
1714 {
1715 struct ltt_kernel_channel *chan;
f3ed775e 1716
8c0faa1d
DG
1717 /* Setup lttng message with no payload */
1718 ret = setup_lttng_msg(cmd_ctx, 0);
1719 if (ret < 0) {
1720 goto setup_error;
1721 }
1722
f3ed775e
DG
1723 /* Kernel tracing */
1724 if (cmd_ctx->session->kernel_session != NULL) {
1725 if (cmd_ctx->session->kernel_session->metadata == NULL) {
1726 DBG("Open kernel metadata");
58a97671
DG
1727 ret = kernel_open_metadata(cmd_ctx->session->kernel_session,
1728 cmd_ctx->session->path);
f3ed775e
DG
1729 if (ret < 0) {
1730 ret = LTTCOMM_KERN_META_FAIL;
1731 goto error;
1732 }
1733 }
8c0faa1d 1734
f3ed775e
DG
1735 if (cmd_ctx->session->kernel_session->metadata_stream_fd == 0) {
1736 DBG("Opening kernel metadata stream");
1737 if (cmd_ctx->session->kernel_session->metadata_stream_fd == 0) {
1738 ret = kernel_open_metadata_stream(cmd_ctx->session->kernel_session);
1739 if (ret < 0) {
1740 ERR("Kernel create metadata stream failed");
1741 ret = LTTCOMM_KERN_STREAM_FAIL;
1742 goto error;
1743 }
1744 }
1745 }
8c0faa1d 1746
f3ed775e
DG
1747 /* For each channel */
1748 cds_list_for_each_entry(chan, &cmd_ctx->session->kernel_session->channel_list.head, list) {
1749 if (chan->stream_count == 0) {
1750 ret = kernel_open_channel_stream(chan);
1751 if (ret < 0) {
1752 ERR("Kernel create channel stream failed");
1753 ret = LTTCOMM_KERN_STREAM_FAIL;
1754 goto error;
1755 }
1756 /* Update the stream global counter */
1757 cmd_ctx->session->kernel_session->stream_count_global += ret;
1758 }
1759 }
1760
1761 DBG("Start kernel tracing");
1762 ret = kernel_start_session(cmd_ctx->session->kernel_session);
8c0faa1d 1763 if (ret < 0) {
f3ed775e
DG
1764 ERR("Kernel start session failed");
1765 ret = LTTCOMM_KERN_START_FAIL;
8c0faa1d
DG
1766 goto error;
1767 }
8c0faa1d 1768
f3ed775e
DG
1769 ret = start_kernel_trace(cmd_ctx->session->kernel_session);
1770 if (ret < 0) {
1771 ret = LTTCOMM_KERN_START_FAIL;
8c0faa1d
DG
1772 goto error;
1773 }
8c0faa1d 1774
f3ed775e
DG
1775 /* Quiescent wait after starting trace */
1776 kernel_wait_quiescent(kernel_tracer_fd);
8c0faa1d
DG
1777 }
1778
f3ed775e 1779 /* TODO: Start all UST traces */
8c0faa1d
DG
1780
1781 ret = LTTCOMM_OK;
1782 break;
1783 }
f3ed775e 1784 case LTTNG_STOP_TRACE:
8c0faa1d 1785 {
f3ed775e 1786 struct ltt_kernel_channel *chan;
8c0faa1d
DG
1787 /* Setup lttng message with no payload */
1788 ret = setup_lttng_msg(cmd_ctx, 0);
1789 if (ret < 0) {
1790 goto setup_error;
1791 }
1792
f3ed775e
DG
1793 /* Kernel tracer */
1794 if (cmd_ctx->session->kernel_session != NULL) {
1795 DBG("Stop kernel tracing");
84291629 1796
f3ed775e
DG
1797 ret = kernel_metadata_flush_buffer(cmd_ctx->session->kernel_session->metadata_stream_fd);
1798 if (ret < 0) {
1799 ERR("Kernel metadata flush failed");
1800 }
8c0faa1d 1801
f3ed775e
DG
1802 cds_list_for_each_entry(chan, &cmd_ctx->session->kernel_session->channel_list.head, list) {
1803 ret = kernel_flush_buffer(chan);
1804 if (ret < 0) {
1805 ERR("Kernel flush buffer error");
1806 }
1807 }
1808
1809 ret = kernel_stop_session(cmd_ctx->session->kernel_session);
1810 if (ret < 0) {
1811 ERR("Kernel stop session failed");
1812 ret = LTTCOMM_KERN_STOP_FAIL;
1813 goto error;
1814 }
1815
1816 /* Quiescent wait after stopping trace */
1817 kernel_wait_quiescent(kernel_tracer_fd);
8c0faa1d
DG
1818 }
1819
f3ed775e 1820 /* TODO : User-space tracer */
8c0faa1d
DG
1821
1822 ret = LTTCOMM_OK;
1823 break;
1824 }
5e16da05
MD
1825 case LTTNG_CREATE_SESSION:
1826 {
5461b305
DG
1827 /* Setup lttng message with no payload */
1828 ret = setup_lttng_msg(cmd_ctx, 0);
1829 if (ret < 0) {
1830 goto setup_error;
1831 }
1832
f3ed775e 1833 ret = create_session(cmd_ctx->lsm->session_name, cmd_ctx->lsm->path);
5e16da05 1834 if (ret < 0) {
f3ed775e 1835 if (ret == -EEXIST) {
5e16da05
MD
1836 ret = LTTCOMM_EXIST_SESS;
1837 } else {
aaf97519 1838 ret = LTTCOMM_FATAL;
aaf97519 1839 }
5461b305 1840 goto error;
8028d920 1841 }
1657e9bb 1842
5461b305 1843 ret = LTTCOMM_OK;
5e16da05
MD
1844 break;
1845 }
1846 case LTTNG_DESTROY_SESSION:
1847 {
5461b305
DG
1848 /* Setup lttng message with no payload */
1849 ret = setup_lttng_msg(cmd_ctx, 0);
1850 if (ret < 0) {
1851 goto setup_error;
1852 }
1853
f3ed775e
DG
1854 /* Clean kernel session teardown */
1855 teardown_kernel_session(cmd_ctx->session);
1856
1857 ret = destroy_session(cmd_ctx->lsm->session_name);
5e16da05 1858 if (ret < 0) {
f3ed775e 1859 ret = LTTCOMM_FATAL;
5461b305 1860 goto error;
5e16da05 1861 }
1657e9bb 1862
7a485870
DG
1863 /*
1864 * Must notify the kernel thread here to update it's pollfd in order to
1865 * remove the channel(s)' fd just destroyed.
1866 */
1867 ret = notify_kernel_pollfd();
1868 if (ret < 0) {
1869 ret = LTTCOMM_FATAL;
1870 goto error;
1871 }
1872
5461b305
DG
1873 ret = LTTCOMM_OK;
1874 break;
5e16da05 1875 }
f3ed775e 1876 /*
5e16da05
MD
1877 case LTTNG_LIST_TRACES:
1878 {
5461b305 1879 unsigned int trace_count;
1657e9bb 1880
5461b305 1881 trace_count = get_trace_count_per_session(cmd_ctx->session);
5e16da05
MD
1882 if (trace_count == 0) {
1883 ret = LTTCOMM_NO_TRACE;
5461b305 1884 goto error;
1657e9bb 1885 }
df0da139 1886
5461b305
DG
1887 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_trace) * trace_count);
1888 if (ret < 0) {
1889 goto setup_error;
df0da139 1890 }
ca95a216 1891
5461b305
DG
1892 get_traces_per_session(cmd_ctx->session,
1893 (struct lttng_trace *)(cmd_ctx->llm->payload));
1894
1895 ret = LTTCOMM_OK;
5e16da05
MD
1896 break;
1897 }
f3ed775e
DG
1898 */
1899 /*
5e16da05
MD
1900 case UST_CREATE_TRACE:
1901 {
5461b305 1902 ret = setup_lttng_msg(cmd_ctx, 0);
5e16da05 1903 if (ret < 0) {
5461b305 1904 goto setup_error;
fac6795d 1905 }
ce3d728c 1906
5461b305
DG
1907 ret = ust_create_trace(cmd_ctx);
1908 if (ret < 0) {
f3ed775e 1909 goto error;
5461b305
DG
1910 }
1911 break;
5e16da05 1912 }
f3ed775e
DG
1913 */
1914 case LTTNG_LIST_TRACEABLE_APPS:
5e16da05 1915 {
5461b305
DG
1916 unsigned int app_count;
1917
1918 app_count = get_app_count();
1919 DBG("Traceable application count : %d", app_count);
5e16da05
MD
1920 if (app_count == 0) {
1921 ret = LTTCOMM_NO_APPS;
5461b305 1922 goto error;
ce3d728c 1923 }
520ff687 1924
5461b305
DG
1925 ret = setup_lttng_msg(cmd_ctx, sizeof(pid_t) * app_count);
1926 if (ret < 0) {
1927 goto setup_error;
520ff687 1928 }
57167058 1929
5461b305 1930 get_app_list_pids((pid_t *)(cmd_ctx->llm->payload));
5e16da05 1931
5461b305 1932 ret = LTTCOMM_OK;
5e16da05
MD
1933 break;
1934 }
f3ed775e 1935 /*
5e16da05
MD
1936 case UST_START_TRACE:
1937 {
5461b305
DG
1938 ret = setup_lttng_msg(cmd_ctx, 0);
1939 if (ret < 0) {
1940 goto setup_error;
1941 }
ca95a216 1942
5461b305
DG
1943 ret = ust_start_trace(cmd_ctx);
1944 if (ret < 0) {
1945 goto setup_error;
1946 }
1947 break;
5e16da05
MD
1948 }
1949 case UST_STOP_TRACE:
1950 {
5461b305
DG
1951 ret = setup_lttng_msg(cmd_ctx, 0);
1952 if (ret < 0) {
1953 goto setup_error;
1954 }
ca95a216 1955
5461b305
DG
1956 ret = ust_stop_trace(cmd_ctx);
1957 if (ret < 0) {
1958 goto setup_error;
1959 }
1960 break;
5e16da05 1961 }
f3ed775e 1962 */
5e16da05
MD
1963 case LTTNG_LIST_SESSIONS:
1964 {
6c9cc2ab 1965 lock_session_list();
5461b305 1966
6c9cc2ab 1967 if (session_list_ptr->count == 0) {
f3ed775e 1968 ret = LTTCOMM_NO_SESSION;
5461b305 1969 goto error;
57167058 1970 }
5e16da05 1971
6c9cc2ab
DG
1972 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_session) *
1973 session_list_ptr->count);
5461b305
DG
1974 if (ret < 0) {
1975 goto setup_error;
e065084a 1976 }
5e16da05 1977
6c9cc2ab
DG
1978 /* Filled the session array */
1979 list_lttng_sessions((struct lttng_session *)(cmd_ctx->llm->payload));
1980
1981 unlock_session_list();
5e16da05 1982
5461b305 1983 ret = LTTCOMM_OK;
5e16da05
MD
1984 break;
1985 }
1986 default:
1987 /* Undefined command */
5461b305
DG
1988 ret = setup_lttng_msg(cmd_ctx, 0);
1989 if (ret < 0) {
1990 goto setup_error;
1991 }
1992
5e16da05 1993 ret = LTTCOMM_UND;
5461b305 1994 break;
fac6795d
DG
1995 }
1996
5461b305
DG
1997 /* Set return code */
1998 cmd_ctx->llm->ret_code = ret;
ca95a216 1999
b5541356
DG
2000 if (cmd_ctx->session) {
2001 unlock_session(cmd_ctx->session);
2002 }
2003
e065084a
DG
2004 return ret;
2005
5461b305 2006error:
5461b305
DG
2007 if (cmd_ctx->llm == NULL) {
2008 DBG("Missing llm structure. Allocating one.");
894be886 2009 if (setup_lttng_msg(cmd_ctx, 0) < 0) {
5461b305
DG
2010 goto setup_error;
2011 }
2012 }
e065084a 2013 /* Notify client of error */
5461b305 2014 cmd_ctx->llm->ret_code = ret;
e065084a 2015
5461b305 2016setup_error:
b5541356
DG
2017 if (cmd_ctx->session) {
2018 unlock_session(cmd_ctx->session);
2019 }
8028d920 2020 return ret;
fac6795d
DG
2021}
2022
1d4b027a
DG
2023/*
2024 * thread_manage_clients
2025 *
2026 * This thread manage all clients request using the unix
2027 * client socket for communication.
2028 */
2029static void *thread_manage_clients(void *data)
2030{
273ea72c
DG
2031 int sock = 0, ret;
2032 struct command_ctx *cmd_ctx = NULL;
2033 struct pollfd pollfd[2];
1d4b027a
DG
2034
2035 DBG("[thread] Manage client started");
2036
2037 ret = lttcomm_listen_unix_sock(client_sock);
2038 if (ret < 0) {
2039 goto error;
2040 }
2041
273ea72c
DG
2042 /* First fd is always the quit pipe */
2043 pollfd[0].fd = thread_quit_pipe[0];
2044
2045 /* Apps socket */
2046 pollfd[1].fd = client_sock;
2047 pollfd[1].events = POLLIN;
2048
1d4b027a
DG
2049 /* Notify parent pid that we are ready
2050 * to accept command for client side.
2051 */
2052 if (opt_sig_parent) {
2053 kill(ppid, SIGCHLD);
2054 }
2055
2056 while (1) {
1d4b027a 2057 DBG("Accepting client command ...");
273ea72c
DG
2058
2059 /* Inifinite blocking call, waiting for transmission */
2060 ret = poll(pollfd, 2, -1);
2061 if (ret < 0) {
2062 perror("poll client thread");
2063 goto error;
2064 }
2065
2066 /* Thread quit pipe has been closed. Killing thread. */
2067 if (pollfd[0].revents == POLLNVAL) {
2068 goto error;
2069 } else if (pollfd[1].revents == POLLERR) {
2070 ERR("Client socket poll error");
2071 goto error;
2072 }
2073
1d4b027a
DG
2074 sock = lttcomm_accept_unix_sock(client_sock);
2075 if (sock < 0) {
2076 goto error;
2077 }
2078
2079 /* Allocate context command to process the client request */
2080 cmd_ctx = malloc(sizeof(struct command_ctx));
2081
2082 /* Allocate data buffer for reception */
2083 cmd_ctx->lsm = malloc(sizeof(struct lttcomm_session_msg));
2084 cmd_ctx->llm = NULL;
2085 cmd_ctx->session = NULL;
2086
2087 /*
2088 * Data is received from the lttng client. The struct
2089 * lttcomm_session_msg (lsm) contains the command and data request of
2090 * the client.
2091 */
2092 DBG("Receiving data from client ...");
2093 ret = lttcomm_recv_unix_sock(sock, cmd_ctx->lsm, sizeof(struct lttcomm_session_msg));
2094 if (ret <= 0) {
2095 continue;
2096 }
2097
f7776ea7
DG
2098 // TODO: Validate cmd_ctx including sanity check for security purpose.
2099
1d4b027a
DG
2100 /*
2101 * This function dispatch the work to the kernel or userspace tracer
2102 * libs and fill the lttcomm_lttng_msg data structure of all the needed
2103 * informations for the client. The command context struct contains
2104 * everything this function may needs.
2105 */
2106 ret = process_client_msg(cmd_ctx);
2107 if (ret < 0) {
2108 /* TODO: Inform client somehow of the fatal error. At this point,
2109 * ret < 0 means that a malloc failed (ENOMEM). */
2110 /* Error detected but still accept command */
a2fb29a5 2111 clean_command_ctx(&cmd_ctx);
1d4b027a
DG
2112 continue;
2113 }
2114
2115 DBG("Sending response (size: %d, retcode: %d)",
2116 cmd_ctx->lttng_msg_size, cmd_ctx->llm->ret_code);
2117 ret = send_unix_sock(sock, cmd_ctx->llm, cmd_ctx->lttng_msg_size);
2118 if (ret < 0) {
2119 ERR("Failed to send data back to client");
2120 }
2121
a2fb29a5 2122 clean_command_ctx(&cmd_ctx);
d6e4fca4
DG
2123
2124 /* End of transmission */
2125 close(sock);
1d4b027a
DG
2126 }
2127
2128error:
273ea72c
DG
2129 DBG("Client thread dying");
2130 if (client_sock) {
2131 close(client_sock);
2132 }
2133 if (sock) {
2134 close(sock);
2135 }
2136
2137 unlink(client_unix_sock_path);
2138
a2fb29a5 2139 clean_command_ctx(&cmd_ctx);
1d4b027a
DG
2140 return NULL;
2141}
2142
2143
fac6795d
DG
2144/*
2145 * usage function on stderr
2146 */
2147static void usage(void)
2148{
b716ce68 2149 fprintf(stderr, "Usage: %s OPTIONS\n\nOptions:\n", progname);
d6f42150
DG
2150 fprintf(stderr, " -h, --help Display this usage.\n");
2151 fprintf(stderr, " -c, --client-sock PATH Specify path for the client unix socket\n");
2152 fprintf(stderr, " -a, --apps-sock PATH Specify path for apps unix socket\n");
2153 fprintf(stderr, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
2154 fprintf(stderr, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
2155 fprintf(stderr, " -d, --daemonize Start as a daemon.\n");
2156 fprintf(stderr, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
2157 fprintf(stderr, " -V, --version Show version number.\n");
2158 fprintf(stderr, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
2159 fprintf(stderr, " -q, --quiet No output at all.\n");
2160 fprintf(stderr, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
fac6795d
DG
2161}
2162
2163/*
2164 * daemon argument parsing
2165 */
2166static int parse_args(int argc, char **argv)
2167{
2168 int c;
2169
2170 static struct option long_options[] = {
2171 { "client-sock", 1, 0, 'c' },
2172 { "apps-sock", 1, 0, 'a' },
d6f42150
DG
2173 { "kconsumerd-cmd-sock", 1, 0, 0 },
2174 { "kconsumerd-err-sock", 1, 0, 0 },
fac6795d 2175 { "daemonize", 0, 0, 'd' },
5b8719f5 2176 { "sig-parent", 0, 0, 'S' },
fac6795d
DG
2177 { "help", 0, 0, 'h' },
2178 { "group", 1, 0, 'g' },
2179 { "version", 0, 0, 'V' },
75462a81 2180 { "quiet", 0, 0, 'q' },
3f9947db 2181 { "verbose", 0, 0, 'v' },
fac6795d
DG
2182 { NULL, 0, 0, 0 }
2183 };
2184
2185 while (1) {
2186 int option_index = 0;
d6f42150 2187 c = getopt_long(argc, argv, "dhqvVS" "a:c:g:s:E:C:", long_options, &option_index);
fac6795d
DG
2188 if (c == -1) {
2189 break;
2190 }
2191
2192 switch (c) {
2193 case 0:
2194 fprintf(stderr, "option %s", long_options[option_index].name);
2195 if (optarg) {
2196 fprintf(stderr, " with arg %s\n", optarg);
2197 }
2198 break;
b716ce68 2199 case 'c':
fac6795d
DG
2200 snprintf(client_unix_sock_path, PATH_MAX, "%s", optarg);
2201 break;
2202 case 'a':
2203 snprintf(apps_unix_sock_path, PATH_MAX, "%s", optarg);
2204 break;
2205 case 'd':
2206 opt_daemon = 1;
2207 break;
2208 case 'g':
2209 opt_tracing_group = strdup(optarg);
2210 break;
2211 case 'h':
2212 usage();
2213 exit(EXIT_FAILURE);
2214 case 'V':
2215 fprintf(stdout, "%s\n", VERSION);
2216 exit(EXIT_SUCCESS);
5b8719f5
DG
2217 case 'S':
2218 opt_sig_parent = 1;
2219 break;
d6f42150
DG
2220 case 'E':
2221 snprintf(kconsumerd_err_unix_sock_path, PATH_MAX, "%s", optarg);
2222 break;
2223 case 'C':
2224 snprintf(kconsumerd_cmd_unix_sock_path, PATH_MAX, "%s", optarg);
2225 break;
75462a81
DG
2226 case 'q':
2227 opt_quiet = 1;
2228 break;
3f9947db
DG
2229 case 'v':
2230 opt_verbose = 1;
2231 break;
fac6795d
DG
2232 default:
2233 /* Unknown option or other error.
2234 * Error is printed by getopt, just return */
2235 return -1;
2236 }
2237 }
2238
2239 return 0;
2240}
2241
2242/*
2243 * init_daemon_socket
2244 *
2245 * Creates the two needed socket by the daemon.
d6f42150
DG
2246 * apps_sock - The communication socket for all UST apps.
2247 * client_sock - The communication of the cli tool (lttng).
fac6795d
DG
2248 */
2249static int init_daemon_socket()
2250{
2251 int ret = 0;
2252 mode_t old_umask;
2253
2254 old_umask = umask(0);
2255
2256 /* Create client tool unix socket */
d6f42150
DG
2257 client_sock = lttcomm_create_unix_sock(client_unix_sock_path);
2258 if (client_sock < 0) {
2259 ERR("Create unix sock failed: %s", client_unix_sock_path);
fac6795d
DG
2260 ret = -1;
2261 goto end;
2262 }
2263
2264 /* File permission MUST be 660 */
2265 ret = chmod(client_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
2266 if (ret < 0) {
d6f42150 2267 ERR("Set file permissions failed: %s", client_unix_sock_path);
fac6795d
DG
2268 perror("chmod");
2269 goto end;
2270 }
2271
2272 /* Create the application unix socket */
d6f42150
DG
2273 apps_sock = lttcomm_create_unix_sock(apps_unix_sock_path);
2274 if (apps_sock < 0) {
2275 ERR("Create unix sock failed: %s", apps_unix_sock_path);
fac6795d
DG
2276 ret = -1;
2277 goto end;
2278 }
2279
d6f42150 2280 /* File permission MUST be 666 */
fac6795d
DG
2281 ret = chmod(apps_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
2282 if (ret < 0) {
d6f42150 2283 ERR("Set file permissions failed: %s", apps_unix_sock_path);
fac6795d
DG
2284 perror("chmod");
2285 goto end;
2286 }
2287
2288end:
2289 umask(old_umask);
2290 return ret;
2291}
2292
2293/*
2294 * check_existing_daemon
2295 *
2296 * Check if the global socket is available.
62bd06d8 2297 * If yes, error is returned.
fac6795d
DG
2298 */
2299static int check_existing_daemon()
2300{
2301 int ret;
2302
2303 ret = access(client_unix_sock_path, F_OK);
2304 if (ret == 0) {
2305 ret = access(apps_unix_sock_path, F_OK);
2306 }
2307
2308 return ret;
2309}
2310
fac6795d 2311/*
d6f42150 2312 * set_permissions
fac6795d 2313 *
5e16da05
MD
2314 * Set the tracing group gid onto the client socket.
2315 *
2316 * Race window between mkdir and chown is OK because we are going from
a463f419 2317 * more permissive (root.root) to les permissive (root.tracing).
fac6795d 2318 */
d6f42150 2319static int set_permissions(void)
fac6795d
DG
2320{
2321 int ret;
2322 struct group *grp;
2323
2324 /* Decide which group name to use */
2325 (opt_tracing_group != NULL) ?
2326 (grp = getgrnam(opt_tracing_group)) :
2327 (grp = getgrnam(default_tracing_group));
2328
2329 if (grp == NULL) {
a463f419
DG
2330 if (is_root) {
2331 WARN("No tracing group detected");
2332 ret = 0;
2333 } else {
2334 ERR("Missing tracing group. Aborting execution.");
2335 ret = -1;
2336 }
fac6795d
DG
2337 goto end;
2338 }
2339
d6f42150
DG
2340 /* Set lttng run dir */
2341 ret = chown(LTTNG_RUNDIR, 0, grp->gr_gid);
2342 if (ret < 0) {
2343 ERR("Unable to set group on " LTTNG_RUNDIR);
2344 perror("chown");
2345 }
2346
2347 /* lttng client socket path */
fac6795d
DG
2348 ret = chown(client_unix_sock_path, 0, grp->gr_gid);
2349 if (ret < 0) {
d6f42150
DG
2350 ERR("Unable to set group on %s", client_unix_sock_path);
2351 perror("chown");
2352 }
2353
2354 /* kconsumerd error socket path */
2355 ret = chown(kconsumerd_err_unix_sock_path, 0, grp->gr_gid);
2356 if (ret < 0) {
2357 ERR("Unable to set group on %s", kconsumerd_err_unix_sock_path);
fac6795d
DG
2358 perror("chown");
2359 }
2360
d6f42150 2361 DBG("All permissions are set");
e07ae692 2362
fac6795d
DG
2363end:
2364 return ret;
2365}
2366
7a485870
DG
2367/*
2368 * create_kernel_poll_pipe
2369 *
2370 * Create the pipe used to wake up the kernel thread.
2371 */
2372static int create_kernel_poll_pipe(void)
2373{
2374 return pipe2(kernel_poll_pipe, O_CLOEXEC);
2375}
2376
d6f42150
DG
2377/*
2378 * create_lttng_rundir
2379 *
2380 * Create the lttng run directory needed for all
2381 * global sockets and pipe.
2382 */
2383static int create_lttng_rundir(void)
2384{
2385 int ret;
2386
2387 ret = mkdir(LTTNG_RUNDIR, S_IRWXU | S_IRWXG );
2388 if (ret < 0) {
b1f11e69
DG
2389 if (errno != EEXIST) {
2390 ERR("Unable to create " LTTNG_RUNDIR);
2391 goto error;
2392 } else {
2393 ret = 0;
2394 }
d6f42150
DG
2395 }
2396
2397error:
2398 return ret;
2399}
2400
2401/*
2402 * set_kconsumerd_sockets
2403 *
2404 * Setup sockets and directory needed by the kconsumerd
2405 * communication with the session daemon.
2406 */
2407static int set_kconsumerd_sockets(void)
2408{
2409 int ret;
2410
2411 if (strlen(kconsumerd_err_unix_sock_path) == 0) {
2412 snprintf(kconsumerd_err_unix_sock_path, PATH_MAX, KCONSUMERD_ERR_SOCK_PATH);
2413 }
2414
2415 if (strlen(kconsumerd_cmd_unix_sock_path) == 0) {
2416 snprintf(kconsumerd_cmd_unix_sock_path, PATH_MAX, KCONSUMERD_CMD_SOCK_PATH);
2417 }
2418
2419 ret = mkdir(KCONSUMERD_PATH, S_IRWXU | S_IRWXG);
2420 if (ret < 0) {
6beb2242
DG
2421 if (errno != EEXIST) {
2422 ERR("Failed to create " KCONSUMERD_PATH);
2423 goto error;
2424 }
2425 ret = 0;
d6f42150
DG
2426 }
2427
2428 /* Create the kconsumerd error unix socket */
2429 kconsumerd_err_sock = lttcomm_create_unix_sock(kconsumerd_err_unix_sock_path);
2430 if (kconsumerd_err_sock < 0) {
2431 ERR("Create unix sock failed: %s", kconsumerd_err_unix_sock_path);
2432 ret = -1;
2433 goto error;
2434 }
2435
2436 /* File permission MUST be 660 */
2437 ret = chmod(kconsumerd_err_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
2438 if (ret < 0) {
2439 ERR("Set file permissions failed: %s", kconsumerd_err_unix_sock_path);
2440 perror("chmod");
2441 goto error;
2442 }
2443
2444error:
2445 return ret;
2446}
2447
fac6795d 2448/*
62bd06d8 2449 * sighandler
fac6795d 2450 *
62bd06d8 2451 * Signal handler for the daemon
fac6795d
DG
2452 */
2453static void sighandler(int sig)
2454{
2455 switch (sig) {
2456 case SIGPIPE:
e07ae692 2457 DBG("SIGPIPE catched");
87378cf5 2458 return;
fac6795d 2459 case SIGINT:
e07ae692
DG
2460 DBG("SIGINT catched");
2461 cleanup();
2462 break;
fac6795d 2463 case SIGTERM:
e07ae692 2464 DBG("SIGTERM catched");
fac6795d 2465 cleanup();
686204ab 2466 break;
fac6795d
DG
2467 default:
2468 break;
2469 }
2470
2471 exit(EXIT_SUCCESS);
2472}
2473
2474/*
1d4b027a 2475 * set_signal_handler
fac6795d 2476 *
1d4b027a
DG
2477 * Setup signal handler for :
2478 * SIGINT, SIGTERM, SIGPIPE
fac6795d 2479 */
1d4b027a 2480static int set_signal_handler(void)
fac6795d 2481{
1d4b027a
DG
2482 int ret = 0;
2483 struct sigaction sa;
2484 sigset_t sigset;
fac6795d 2485
1d4b027a
DG
2486 if ((ret = sigemptyset(&sigset)) < 0) {
2487 perror("sigemptyset");
2488 return ret;
2489 }
d6f42150 2490
1d4b027a
DG
2491 sa.sa_handler = sighandler;
2492 sa.sa_mask = sigset;
2493 sa.sa_flags = 0;
2494 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
2495 perror("sigaction");
2496 return ret;
d6f42150
DG
2497 }
2498
1d4b027a
DG
2499 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
2500 perror("sigaction");
2501 return ret;
d6f42150 2502 }
aaf26714 2503
1d4b027a
DG
2504 if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) {
2505 perror("sigaction");
2506 return ret;
8c0faa1d
DG
2507 }
2508
1d4b027a
DG
2509 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
2510
2511 return ret;
fac6795d
DG
2512}
2513
f3ed775e
DG
2514/*
2515 * set_ulimit
2516 *
2517 * Set open files limit to unlimited. This daemon can open a large number of
2518 * file descriptors in order to consumer multiple kernel traces.
2519 */
2520static void set_ulimit(void)
2521{
2522 int ret;
2523 struct rlimit lim;
2524
a88df331 2525 /* The kernel does not allowed an infinite limit for open files */
f3ed775e
DG
2526 lim.rlim_cur = 65535;
2527 lim.rlim_max = 65535;
2528
2529 ret = setrlimit(RLIMIT_NOFILE, &lim);
2530 if (ret < 0) {
2531 perror("failed to set open files limit");
2532 }
2533}
2534
fac6795d
DG
2535/*
2536 * main
2537 */
2538int main(int argc, char **argv)
2539{
fac6795d
DG
2540 int ret = 0;
2541 void *status;
b082db07 2542 const char *home_path;
fac6795d 2543
273ea72c
DG
2544 /* Create thread quit pipe */
2545 if (init_thread_quit_pipe() < 0) {
a88df331 2546 goto exit;
273ea72c
DG
2547 }
2548
fac6795d
DG
2549 /* Parse arguments */
2550 progname = argv[0];
2551 if ((ret = parse_args(argc, argv) < 0)) {
a88df331 2552 goto exit;
fac6795d
DG
2553 }
2554
2555 /* Daemonize */
2556 if (opt_daemon) {
53094c05
DG
2557 ret = daemon(0, 0);
2558 if (ret < 0) {
2559 perror("daemon");
a88df331 2560 goto exit;
53094c05 2561 }
fac6795d
DG
2562 }
2563
2564 /* Check if daemon is UID = 0 */
2565 is_root = !getuid();
2566
fac6795d 2567 if (is_root) {
d6f42150
DG
2568 ret = create_lttng_rundir();
2569 if (ret < 0) {
a88df331 2570 goto exit;
d6f42150
DG
2571 }
2572
fac6795d 2573 if (strlen(apps_unix_sock_path) == 0) {
d6f42150
DG
2574 snprintf(apps_unix_sock_path, PATH_MAX,
2575 DEFAULT_GLOBAL_APPS_UNIX_SOCK);
fac6795d
DG
2576 }
2577
2578 if (strlen(client_unix_sock_path) == 0) {
d6f42150
DG
2579 snprintf(client_unix_sock_path, PATH_MAX,
2580 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK);
2581 }
fac6795d 2582 } else {
b082db07
DG
2583 home_path = get_home_dir();
2584 if (home_path == NULL) {
273ea72c
DG
2585 /* TODO: Add --socket PATH option */
2586 ERR("Can't get HOME directory for sockets creation.");
a88df331 2587 goto exit;
b082db07
DG
2588 }
2589
fac6795d 2590 if (strlen(apps_unix_sock_path) == 0) {
d6f42150 2591 snprintf(apps_unix_sock_path, PATH_MAX,
b082db07 2592 DEFAULT_HOME_APPS_UNIX_SOCK, home_path);
fac6795d
DG
2593 }
2594
2595 /* Set the cli tool unix socket path */
2596 if (strlen(client_unix_sock_path) == 0) {
d6f42150 2597 snprintf(client_unix_sock_path, PATH_MAX,
b082db07 2598 DEFAULT_HOME_CLIENT_UNIX_SOCK, home_path);
fac6795d
DG
2599 }
2600 }
2601
847177cd
DG
2602 DBG("Client socket path %s", client_unix_sock_path);
2603 DBG("Application socket path %s", apps_unix_sock_path);
2604
273ea72c
DG
2605 /*
2606 * See if daemon already exist. If any of the two socket needed by the
2607 * daemon are present, this test fails. However, if the daemon is killed
2608 * with a SIGKILL, those unix socket must be unlinked by hand.
fac6795d
DG
2609 */
2610 if ((ret = check_existing_daemon()) == 0) {
75462a81 2611 ERR("Already running daemon.\n");
273ea72c 2612 /*
a88df331
DG
2613 * We do not goto error because we must not cleanup() because a daemon
2614 * is already running.
ab118b20 2615 */
a88df331
DG
2616 goto exit;
2617 }
2618
2619 /* After this point, we can safely call cleanup() so goto error is used */
2620
2621 /*
2622 * These actions must be executed as root. We do that *after* setting up
2623 * the sockets path because we MUST make the check for another daemon using
2624 * those paths *before* trying to set the kernel consumer sockets and init
2625 * kernel tracer.
2626 */
2627 if (is_root) {
2628 ret = set_kconsumerd_sockets();
2629 if (ret < 0) {
2630 goto error;
2631 }
2632
2633 /* Setup kernel tracer */
2634 init_kernel_tracer();
2635
2636 /* Set ulimit for open files */
2637 set_ulimit();
fac6795d
DG
2638 }
2639
2640 if (set_signal_handler() < 0) {
2641 goto error;
2642 }
2643
d6f42150 2644 /* Setup the needed unix socket */
fac6795d
DG
2645 if (init_daemon_socket() < 0) {
2646 goto error;
2647 }
2648
2649 /* Set credentials to socket */
d6f42150 2650 if (is_root && (set_permissions() < 0)) {
fac6795d
DG
2651 goto error;
2652 }
2653
5b8719f5
DG
2654 /* Get parent pid if -S, --sig-parent is specified. */
2655 if (opt_sig_parent) {
2656 ppid = getppid();
2657 }
2658
7a485870
DG
2659 /* Setup the kernel pipe for waking up the kernel thread */
2660 if (create_kernel_poll_pipe() < 0) {
2661 goto error;
2662 }
2663
273ea72c
DG
2664 /*
2665 * Get session list pointer. This pointer MUST NOT be free().
2666 * This list is statically declared in session.c
2667 */
b5541356
DG
2668 session_list_ptr = get_session_list();
2669
fac6795d
DG
2670 while (1) {
2671 /* Create thread to manage the client socket */
1d4b027a 2672 ret = pthread_create(&client_thread, NULL, thread_manage_clients, (void *) NULL);
fac6795d
DG
2673 if (ret != 0) {
2674 perror("pthread_create");
2675 goto error;
2676 }
2677
2678 /* Create thread to manage application socket */
1d4b027a 2679 ret = pthread_create(&apps_thread, NULL, thread_manage_apps, (void *) NULL);
fac6795d
DG
2680 if (ret != 0) {
2681 perror("pthread_create");
2682 goto error;
2683 }
2684
7a485870
DG
2685 /* Create kernel thread to manage kernel event */
2686 ret = pthread_create(&kernel_thread, NULL, thread_manage_kernel, (void *) NULL);
2687 if (ret != 0) {
2688 perror("pthread_create");
2689 goto error;
2690 }
2691
1d4b027a
DG
2692 ret = pthread_join(client_thread, &status);
2693 if (ret != 0) {
2694 perror("pthread_join");
2695 goto error;
fac6795d
DG
2696 }
2697 }
2698
2699 cleanup();
5e16da05 2700 exit(EXIT_SUCCESS);
fac6795d
DG
2701
2702error:
2703 cleanup();
a88df331
DG
2704
2705exit:
5e16da05 2706 exit(EXIT_FAILURE);
fac6795d 2707}
This page took 0.159505 seconds and 4 git commands to generate.