#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <sys/ipc.h>
#include <sys/mount.h>
-#include <sys/shm.h>
+#include <sys/resource.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
-#include <sys/time.h>
-#include <sys/resource.h>
#include <unistd.h>
-#include <urcu/list.h> /* URCU list library (-lurcu) */
-#include <lttng/lttng.h>
+#include <ltt-kconsumerd.h>
+#include <lttng-sessiond-comm.h>
+#include <lttng/lttng-kconsumerd.h>
+#include <lttngerr.h>
#include "context.h"
-#include "liblttsessiondcomm.h"
-#include "ltt-sessiond.h"
-#include "lttngerr.h"
#include "kernel-ctl.h"
-#include "ust-ctl.h"
-#include "session.h"
+#include "ltt-sessiond.h"
#include "traceable-app.h"
-#include "lttng-kconsumerd.h"
+#include "ust-ctl.h"
#include "utils.h"
/* Const values */
{
if (session->kernel_session != NULL) {
DBG("Tearing down kernel session");
+
+ /*
+ * If a custom kernel consumer was registered, close the socket before
+ * tearing down the complete kernel session structure
+ */
+ if (session->kernel_session->consumer_fd != kconsumerd_cmd_sock) {
+ lttcomm_close_unix_sock(session->kernel_session->consumer_fd);
+ }
+
trace_destroy_kernel_session(session->kernel_session);
/* Extra precaution */
session->kernel_session = NULL;
lkm.fd = stream->fd;
lkm.state = stream->state;
lkm.max_sb_size = channel->channel->attr.subbuf_size;
+ lkm.output = channel->channel->attr.output;
strncpy(lkm.path_name, stream->pathname, PATH_MAX);
+ lkm.path_name[PATH_MAX - 1] = '\0';
DBG("Sending fd %d to kconsumerd", lkm.fd);
/*
* Send all stream fds of the kernel session to the consumer.
*/
-static int send_kconsumerd_fds(int sock, struct ltt_kernel_session *session)
+static int send_kconsumerd_fds(struct ltt_kernel_session *session)
{
int ret;
struct ltt_kernel_channel *chan;
DBG("Sending kconsumerd header for metadata");
- ret = lttcomm_send_unix_sock(sock, &lkh, sizeof(struct lttcomm_kconsumerd_header));
+ ret = lttcomm_send_unix_sock(session->consumer_fd, &lkh, sizeof(struct lttcomm_kconsumerd_header));
if (ret < 0) {
perror("send kconsumerd header");
goto error;
DBG("Sending metadata stream fd");
+ /* Extra protection. It's NOT suppose to be set to 0 at this point */
+ if (session->consumer_fd == 0) {
+ session->consumer_fd = kconsumerd_cmd_sock;
+ }
+
if (session->metadata_stream_fd != 0) {
/* Send metadata stream fd first */
lkm.fd = session->metadata_stream_fd;
lkm.state = ACTIVE_FD;
lkm.max_sb_size = session->metadata->conf->attr.subbuf_size;
+ lkm.output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
strncpy(lkm.path_name, session->metadata->pathname, PATH_MAX);
+ lkm.path_name[PATH_MAX - 1] = '\0';
- ret = lttcomm_send_fds_unix_sock(sock, &lkm, &lkm.fd, 1, sizeof(lkm));
+ ret = lttcomm_send_fds_unix_sock(session->consumer_fd, &lkm, &lkm.fd, 1, sizeof(lkm));
if (ret < 0) {
perror("send kconsumerd fd");
goto error;
}
cds_list_for_each_entry(chan, &session->channel_list.head, list) {
- ret = send_kconsumerd_channel_fds(sock, chan);
+ ret = send_kconsumerd_channel_fds(session->consumer_fd, chan);
if (ret < 0) {
goto error;
}
unlock_session(session);
continue;
}
+
+ /* This is not suppose to be 0 but this is an extra security check */
+ if (session->kernel_session->consumer_fd == 0) {
+ session->kernel_session->consumer_fd = kconsumerd_cmd_sock;
+ }
+
cds_list_for_each_entry(channel, &session->kernel_session->channel_list.head, list) {
if (channel->fd == fd) {
DBG("Channel found, updating kernel streams");
if (ret < 0) {
goto end;
}
+
/*
* Have we already sent fds to the consumer? If yes, it means that
* tracing is started so it is safe to send our updated stream fds.
*/
if (session->kernel_session->kconsumer_fds_sent == 1) {
- ret = send_kconsumerd_channel_fds(kconsumerd_cmd_sock, channel);
+ ret = send_kconsumerd_channel_fds(session->kernel_session->consumer_fd,
+ channel);
if (ret < 0) {
goto end;
}
goto error;
}
+ /* Kconsumerd err socket */
+ pollfd[1].fd = sock;
+ pollfd[1].events = POLLIN;
+
+ /* Inifinite blocking call, waiting for transmission */
+ ret = poll(pollfd, 2, -1);
+ if (ret < 0) {
+ perror("poll kconsumerd thread");
+ goto error;
+ }
+
+ /* Thread quit pipe has been closed. Killing thread. */
+ if (pollfd[0].revents == POLLNVAL) {
+ goto error;
+ } else if (pollfd[1].revents == POLLERR) {
+ ERR("Kconsumerd err socket second poll error");
+ goto error;
+ }
+
/* Wait for any kconsumerd error */
ret = lttcomm_recv_unix_sock(sock, &code, sizeof(enum lttcomm_return_code));
if (ret <= 0) {
}
/*
- * Basic recv here to handle the very simple data
- * that the libust send to register (reg_msg).
+ * Using message-based transmissions to ensure we don't
+ * have to deal with partially received messages.
*/
- ret = recv(sock, ®_msg, sizeof(reg_msg), 0);
+ ret = lttcomm_recv_unix_sock(sock, ®_msg, sizeof(reg_msg));
if (ret < 0) {
perror("recv");
continue;
/* Add application to the global traceable list */
if (reg_msg.reg == 1) {
/* Registering */
+ /*
+ * TODO: socket should be either passed to a
+ * listener thread (for more messages) or
+ * closed. It currently leaks.
+ */
ret = register_traceable_app(reg_msg.pid, reg_msg.uid);
if (ret < 0) {
/* register_traceable_app only return an error with
return ret;
}
+/*
+ * Join kernel consumer thread
+ */
static int join_kconsumerd_thread(void)
{
void *status;
ERR("Unable to launch modprobe for module %s",
kernel_modules_list[i].name);
} else if (kernel_modules_list[i].required
- && WEXITSTATUS(ret) != 0) {
+ && WEXITSTATUS(ret) != 0) {
ERR("Unable to load module %s",
kernel_modules_list[i].name);
} else {
ERR("Unable to launch modprobe --remove for module %s",
kernel_modules_list[i].name);
} else if (kernel_modules_list[i].required
- && WEXITSTATUS(ret) != 0) {
+ && WEXITSTATUS(ret) != 0) {
ERR("Unable to remove module %s",
kernel_modules_list[i].name);
} else {
int ret = 0;
if (session->kconsumer_fds_sent == 0) {
- ret = send_kconsumerd_fds(kconsumerd_cmd_sock, session);
+ /*
+ * Assign default kernel consumer if no consumer assigned to the kernel
+ * session. At this point, it's NOT suppose to be 0 but this is an extra
+ * security check.
+ */
+ if (session->consumer_fd == 0) {
+ session->consumer_fd = kconsumerd_cmd_sock;
+ }
+
+ ret = send_kconsumerd_fds(session);
if (ret < 0) {
ERR("Send kconsumerd fds failed");
ret = LTTCOMM_KERN_CONSUMER_FAIL;
/*
* Allocate a channel structure and fill it.
*/
-static struct lttng_channel *init_default_channel(char *name)
+static struct lttng_channel *init_default_channel(enum lttng_domain_type domain_type,
+ char *name)
{
struct lttng_channel *chan;
if (snprintf(chan->name, NAME_MAX, "%s", name) < 0) {
perror("snprintf channel name");
- return NULL;
+ goto error;
}
chan->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
- chan->attr.subbuf_size = DEFAULT_CHANNEL_SUBBUF_SIZE;
- chan->attr.num_subbuf = DEFAULT_CHANNEL_SUBBUF_NUM;
chan->attr.switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
chan->attr.read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
- chan->attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
-error:
+ switch (domain_type) {
+ case LTTNG_DOMAIN_KERNEL:
+ chan->attr.subbuf_size = DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE;
+ chan->attr.num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM;
+ chan->attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
+ break;
+ /* TODO: add UST */
+ default:
+ goto error; /* Not implemented */
+ }
+
return chan;
+
+error:
+ free(chan);
+ return NULL;
}
/*
goto error;
}
- ret = mkdir_recursive(session->path, S_IRWXU | S_IRWXG,
- geteuid(), allowed_group());
+ /* Set kernel consumer socket fd */
+ if (kconsumerd_cmd_sock) {
+ session->kernel_session->consumer_fd = kconsumerd_cmd_sock;
+ }
+
+ ret = asprintf(&session->kernel_session->trace_path, "%s/kernel",
+ session->path);
+ if (ret < 0) {
+ perror("asprintf kernel traces path");
+ goto error;
+ }
+
+ ret = mkdir_recursive(session->kernel_session->trace_path,
+ S_IRWXU | S_IRWXG, geteuid(), allowed_group());
if (ret < 0) {
if (ret != -EEXIST) {
ERR("Trace directory creation error");
*/
cds_list_for_each_entry(session, &session_list_ptr->head, list) {
strncpy(sessions[i].path, session->path, PATH_MAX);
+ sessions[i].path[PATH_MAX - 1] = '\0';
strncpy(sessions[i].name, session->name, NAME_MAX);
+ sessions[i].name[NAME_MAX - 1] = '\0';
i++;
}
}
/* Kernel channels */
cds_list_for_each_entry(event, &kchan->events_list.head , list) {
strncpy(events[i].name, event->event->name, LTTNG_SYMBOL_NAME_LEN);
+ events[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
events[i].enabled = event->enabled;
switch (event->event->instrumentation) {
case LTTNG_KERNEL_TRACEPOINT:
DBG("Processing client command %d", cmd_ctx->lsm->cmd_type);
- /* Listing commands don't need a session */
+ /*
+ * Commands that DO NOT need a session.
+ */
switch (cmd_ctx->lsm->cmd_type) {
case LTTNG_CREATE_SESSION:
case LTTNG_LIST_SESSIONS:
/* Need a session for kernel command */
switch (cmd_ctx->lsm->cmd_type) {
+ case LTTNG_CALIBRATE:
case LTTNG_CREATE_SESSION:
case LTTNG_LIST_SESSIONS:
case LTTNG_LIST_TRACEPOINTS:
- case LTTNG_CALIBRATE:
break;
default:
if (cmd_ctx->session->kernel_session == NULL) {
}
/* Start the kernel consumer daemon */
- if (kconsumerd_pid == 0) {
+
+ if (kconsumerd_pid == 0 &&
+ cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
ret = start_kconsumerd();
if (ret < 0) {
goto error;
strncpy(kctx.u.perf_counter.name,
cmd_ctx->lsm->u.context.ctx.u.perf_counter.name,
LTTNG_SYMBOL_NAME_LEN);
+ kctx.u.perf_counter.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
/* Add kernel context to kernel tracer. See context.c */
ret = add_kernel_context(cmd_ctx->session->kernel_session, &kctx,
DBG("Creating kernel channel");
ret = kernel_create_channel(cmd_ctx->session->kernel_session,
- &cmd_ctx->lsm->u.channel.chan, cmd_ctx->session->path);
+ &cmd_ctx->lsm->u.channel.chan,
+ cmd_ctx->session->kernel_session->trace_path);
if (ret < 0) {
ret = LTTCOMM_KERN_CHAN_FAIL;
goto error;
switch (cmd_ctx->lsm->domain.type) {
case LTTNG_DOMAIN_KERNEL:
- do {
+ kchan = get_kernel_channel_by_name(channel_name,
+ cmd_ctx->session->kernel_session);
+ if (kchan == NULL) {
+ DBG("Channel not found. Creating channel %s", channel_name);
+
+ chan = init_default_channel(cmd_ctx->lsm->domain.type, channel_name);
+ if (chan == NULL) {
+ ret = LTTCOMM_FATAL;
+ goto error;
+ }
+
+ ret = kernel_create_channel(cmd_ctx->session->kernel_session,
+ chan, cmd_ctx->session->kernel_session->trace_path);
+ if (ret < 0) {
+ ret = LTTCOMM_KERN_CHAN_FAIL;
+ goto error;
+ }
kchan = get_kernel_channel_by_name(channel_name,
cmd_ctx->session->kernel_session);
if (kchan == NULL) {
- DBG("Channel not found. Creating channel %s", channel_name);
-
- chan = init_default_channel(channel_name);
- if (chan == NULL) {
- ret = LTTCOMM_FATAL;
- goto error;
- }
-
- ret = kernel_create_channel(cmd_ctx->session->kernel_session,
- chan, cmd_ctx->session->path);
- if (ret < 0) {
- ret = LTTCOMM_KERN_CHAN_FAIL;
- goto error;
- }
+ ERR("Channel %s not found after creation. Internal error, giving up.",
+ channel_name);
+ ret = LTTCOMM_FATAL;
+ goto error;
}
- } while (kchan == NULL);
+ }
kevent = get_kernel_event_by_name(cmd_ctx->lsm->u.enable.event.name, kchan);
if (kevent == NULL) {
switch (cmd_ctx->lsm->domain.type) {
case LTTNG_DOMAIN_KERNEL:
- do {
+ kchan = get_kernel_channel_by_name(channel_name,
+ cmd_ctx->session->kernel_session);
+ if (kchan == NULL) {
+ DBG("Channel not found. Creating channel %s", channel_name);
+
+ chan = init_default_channel(cmd_ctx->lsm->domain.type, channel_name);
+ if (chan == NULL) {
+ ret = LTTCOMM_FATAL;
+ goto error;
+ }
+
+ ret = kernel_create_channel(cmd_ctx->session->kernel_session,
+ chan, cmd_ctx->session->kernel_session->trace_path);
+ if (ret < 0) {
+ ret = LTTCOMM_KERN_CHAN_FAIL;
+ goto error;
+ }
kchan = get_kernel_channel_by_name(channel_name,
cmd_ctx->session->kernel_session);
if (kchan == NULL) {
- DBG("Channel not found. Creating channel %s", channel_name);
-
- chan = init_default_channel(channel_name);
- if (chan == NULL) {
- ret = LTTCOMM_FATAL;
- goto error;
- }
-
- ret = kernel_create_channel(cmd_ctx->session->kernel_session,
- chan, cmd_ctx->session->path);
- if (ret < 0) {
- ret = LTTCOMM_KERN_CHAN_FAIL;
- goto error;
- }
+ ERR("Channel %s not found after creation. Internal error, giving up.",
+ channel_name);
+ ret = LTTCOMM_FATAL;
+ goto error;
}
- } while (kchan == NULL);
+ }
/* For each event in the kernel session */
cds_list_for_each_entry(kevent, &kchan->events_list.head, list) {
if (cmd_ctx->session->kernel_session->metadata == NULL) {
DBG("Open kernel metadata");
ret = kernel_open_metadata(cmd_ctx->session->kernel_session,
- cmd_ctx->session->path);
+ cmd_ctx->session->kernel_session->trace_path);
if (ret < 0) {
ret = LTTCOMM_KERN_META_FAIL;
goto error;
}
}
- DBG("Start kernel tracing");
- ret = kernel_start_session(cmd_ctx->session->kernel_session);
+ ret = start_kernel_trace(cmd_ctx->session->kernel_session);
if (ret < 0) {
- ERR("Kernel start session failed");
ret = LTTCOMM_KERN_START_FAIL;
goto error;
}
- ret = start_kernel_trace(cmd_ctx->session->kernel_session);
+ DBG("Start kernel tracing");
+ ret = kernel_start_session(cmd_ctx->session->kernel_session);
if (ret < 0) {
+ ERR("Kernel start session failed");
ret = LTTCOMM_KERN_START_FAIL;
goto error;
}
goto setup_error;
}
- DBG("Listing events (%ld events)", nb_event);
+ DBG("Listing events (%zu events)", nb_event);
list_lttng_events(kchan,
(struct lttng_event *)(cmd_ctx->llm->payload));
ret = LTTCOMM_OK;
break;
}
-
case LTTNG_CALIBRATE:
{
/* Setup lttng message with no payload */
ret = LTTCOMM_OK;
break;
}
+ case LTTNG_REGISTER_CONSUMER:
+ {
+ int sock;
+
+ /* Setup lttng message with no payload */
+ ret = setup_lttng_msg(cmd_ctx, 0);
+ if (ret < 0) {
+ goto setup_error;
+ }
+
+ switch (cmd_ctx->lsm->domain.type) {
+ case LTTNG_DOMAIN_KERNEL:
+ {
+ /* Can't register a consumer if there is already one */
+ if (cmd_ctx->session->kernel_session->consumer_fd != 0) {
+ ret = LTTCOMM_CONNECT_FAIL;
+ goto error;
+ }
+
+ sock = lttcomm_connect_unix_sock(cmd_ctx->lsm->u.reg.path);
+ if (sock < 0) {
+ ret = LTTCOMM_CONNECT_FAIL;
+ goto error;
+ }
+
+ cmd_ctx->session->kernel_session->consumer_fd = sock;
+ break;
+ }
+ default:
+ /* TODO: Userspace tracing */
+ ret = LTTCOMM_NOT_IMPLEMENTED;
+ goto error;
+ }
+
+ ret = LTTCOMM_OK;
+ break;
+ }
default:
/* Undefined command */