bbf50d57b594544f62ffb26f9d7c8a3f23f5a832
[lttng-tools.git] / src / bin / lttng-sessiond / kernel-consumer.c
1 /*
2 * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com>
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #define _GNU_SOURCE
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <sys/stat.h>
23 #include <unistd.h>
24
25 #include <common/common.h>
26 #include <common/defaults.h>
27 #include <common/sessiond-comm/sessiond-comm.h>
28
29 #include "kernel-consumer.h"
30
31 /*
32 * Send all stream fds of kernel channel to the consumer.
33 */
34 int kernel_consumer_send_channel_stream(struct consumer_data *consumer_data,
35 struct ltt_kernel_channel *channel, uid_t uid, gid_t gid)
36 {
37 int ret, count = 0, consumer_sock;
38 struct ltt_kernel_stream *stream;
39 struct lttcomm_consumer_msg lkm;
40
41 DBG("Sending streams of channel %s to kernel consumer",
42 channel->channel->name);
43
44 consumer_sock = consumer_data->cmd_sock;
45
46 /* Send channel */
47 lkm.cmd_type = LTTNG_CONSUMER_ADD_CHANNEL;
48 lkm.u.channel.channel_key = channel->fd;
49 lkm.u.channel.max_sb_size = channel->channel->attr.subbuf_size;
50 lkm.u.channel.mmap_len = 0; /* for kernel */
51 DBG("Sending channel %d to consumer", lkm.u.channel.channel_key);
52 ret = lttcomm_send_unix_sock(consumer_sock, &lkm, sizeof(lkm));
53 if (ret < 0) {
54 PERROR("send consumer channel");
55 goto error;
56 }
57
58 /* Send streams */
59 cds_list_for_each_entry(stream, &channel->stream_list.head, list) {
60 if (!stream->fd) {
61 continue;
62 }
63 /* Reset consumer message structure */
64 memset(&lkm, 0, sizeof(lkm));
65 lkm.cmd_type = LTTNG_CONSUMER_ADD_STREAM;
66 lkm.u.stream.channel_key = channel->fd;
67 lkm.u.stream.stream_key = stream->fd;
68 lkm.u.stream.state = stream->state;
69 lkm.u.stream.output = channel->channel->attr.output;
70 lkm.u.stream.mmap_len = 0; /* for kernel */
71 lkm.u.stream.uid = uid;
72 lkm.u.stream.gid = gid;
73 strncpy(lkm.u.stream.path_name, stream->pathname, PATH_MAX - 1);
74 lkm.u.stream.path_name[PATH_MAX - 1] = '\0';
75 count++;
76
77 DBG("Sending stream %d to consumer", lkm.u.stream.stream_key);
78 ret = lttcomm_send_unix_sock(consumer_sock, &lkm, sizeof(lkm));
79 if (ret < 0) {
80 PERROR("send consumer stream");
81 goto error;
82 }
83 ret = lttcomm_send_fds_unix_sock(consumer_sock, &stream->fd, 1);
84 if (ret < 0) {
85 PERROR("send consumer stream ancillary data");
86 goto error;
87 }
88 }
89
90 DBG("consumer channel streams sent");
91
92 return 0;
93
94 error:
95 return ret;
96 }
97
98 /*
99 * Send all stream fds of the kernel session to the consumer.
100 */
101 int kernel_consumer_send_session(struct consumer_data *consumer_data,
102 struct ltt_kernel_session *session)
103 {
104 int ret;
105 struct ltt_kernel_channel *chan;
106 struct lttcomm_consumer_msg lkm;
107 int sock = session->consumer_fd;
108
109 DBG("Sending metadata stream fd");
110
111 /* Extra protection. It's NOT supposed to be set to -1 at this point */
112 if (session->consumer_fd < 0) {
113 session->consumer_fd = consumer_data->cmd_sock;
114 }
115
116 if (session->metadata_stream_fd >= 0) {
117 /* Send metadata channel fd */
118 lkm.cmd_type = LTTNG_CONSUMER_ADD_CHANNEL;
119 lkm.u.channel.channel_key = session->metadata->fd;
120 lkm.u.channel.max_sb_size = session->metadata->conf->attr.subbuf_size;
121 lkm.u.channel.mmap_len = 0; /* for kernel */
122 DBG("Sending metadata channel %d to consumer", lkm.u.channel.channel_key);
123 ret = lttcomm_send_unix_sock(sock, &lkm, sizeof(lkm));
124 if (ret < 0) {
125 PERROR("send consumer channel");
126 goto error;
127 }
128
129 /* Send metadata stream fd */
130 lkm.cmd_type = LTTNG_CONSUMER_ADD_STREAM;
131 lkm.u.stream.channel_key = session->metadata->fd;
132 lkm.u.stream.stream_key = session->metadata_stream_fd;
133 lkm.u.stream.state = LTTNG_CONSUMER_ACTIVE_STREAM;
134 lkm.u.stream.output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
135 lkm.u.stream.mmap_len = 0; /* for kernel */
136 lkm.u.stream.uid = session->uid;
137 lkm.u.stream.gid = session->gid;
138 strncpy(lkm.u.stream.path_name, session->metadata->pathname,
139 PATH_MAX - 1);
140 lkm.u.stream.path_name[PATH_MAX - 1] = '\0';
141
142 DBG("Sending metadata stream %d to consumer", lkm.u.stream.stream_key);
143 ret = lttcomm_send_unix_sock(sock, &lkm, sizeof(lkm));
144 if (ret < 0) {
145 PERROR("send consumer stream");
146 goto error;
147 }
148 ret = lttcomm_send_fds_unix_sock(sock, &session->metadata_stream_fd, 1);
149 if (ret < 0) {
150 PERROR("send consumer stream");
151 goto error;
152 }
153 }
154
155 cds_list_for_each_entry(chan, &session->channel_list.head, list) {
156 ret = kernel_consumer_send_channel_stream(consumer_data, chan,
157 session->uid, session->gid);
158 if (ret < 0) {
159 goto error;
160 }
161 }
162
163 DBG("consumer fds (metadata and channel streams) sent");
164
165 return 0;
166
167 error:
168 return ret;
169 }
170
This page took 0.044323 seconds and 3 git commands to generate.