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