Fix: consumer should await for initial streams
[lttng-tools.git] / src / bin / lttng-sessiond / ust-consumer.c
1 /*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #define _GNU_SOURCE
19 #include <errno.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24
25 #include <common/common.h>
26 #include <common/consumer.h>
27 #include <common/defaults.h>
28
29 #include "consumer.h"
30 #include "ust-consumer.h"
31
32 /*
33 * Send a single channel to the consumer using command ADD_CHANNEL.
34 */
35 static int send_channel(int sock, struct ust_app_channel *uchan)
36 {
37 int ret, fd;
38 struct lttcomm_consumer_msg msg;
39
40 /* Safety net */
41 assert(uchan);
42
43 if (sock < 0) {
44 ret = -EINVAL;
45 goto error;
46 }
47
48 DBG2("Sending channel %s to UST consumer", uchan->name);
49
50 consumer_init_channel_comm_msg(&msg,
51 LTTNG_CONSUMER_ADD_CHANNEL,
52 uchan->obj->shm_fd,
53 uchan->attr.subbuf_size,
54 uchan->obj->memory_map_size,
55 uchan->name,
56 uchan->streams.count);
57
58 ret = consumer_send_channel(sock, &msg);
59 if (ret < 0) {
60 goto error;
61 }
62
63 fd = uchan->obj->shm_fd;
64 ret = consumer_send_fds(sock, &fd, 1);
65 if (ret < 0) {
66 goto error;
67 }
68
69 error:
70 return ret;
71 }
72
73 /*
74 * Send a single stream to the consumer using ADD_STREAM command.
75 */
76 static int send_channel_stream(int sock, struct ust_app_channel *uchan,
77 struct ust_app_session *usess, struct ltt_ust_stream *stream,
78 struct consumer_output *consumer, const char *pathname)
79 {
80 int ret, fds[2];
81 struct lttcomm_consumer_msg msg;
82
83 /* Safety net */
84 assert(uchan);
85 assert(usess);
86 assert(stream);
87 assert(consumer);
88
89 DBG2("Sending stream %d of channel %s to kernel consumer",
90 stream->obj->shm_fd, uchan->name);
91
92 consumer_init_stream_comm_msg(&msg,
93 LTTNG_CONSUMER_ADD_STREAM,
94 uchan->obj->shm_fd,
95 stream->obj->shm_fd,
96 LTTNG_CONSUMER_ACTIVE_STREAM,
97 DEFAULT_UST_CHANNEL_OUTPUT,
98 stream->obj->memory_map_size,
99 usess->uid,
100 usess->gid,
101 consumer->net_seq_index,
102 0, /* Metadata flag unset */
103 stream->name,
104 pathname);
105
106 /* Send stream and file descriptor */
107 fds[0] = stream->obj->shm_fd;
108 fds[1] = stream->obj->wait_fd;
109 ret = consumer_send_stream(sock, consumer, &msg, fds, 2);
110 if (ret < 0) {
111 goto error;
112 }
113
114 error:
115 return ret;
116 }
117
118 /*
119 * Send all stream fds of UST channel to the consumer.
120 */
121 static int send_channel_streams(int sock,
122 struct ust_app_channel *uchan, struct ust_app_session *usess,
123 struct consumer_output *consumer)
124 {
125 int ret;
126 char tmp_path[PATH_MAX];
127 const char *pathname;
128 struct ltt_ust_stream *stream, *tmp;
129
130 DBG("Sending streams of channel %s to UST consumer", uchan->name);
131
132 ret = send_channel(sock, uchan);
133 if (ret < 0) {
134 goto error;
135 }
136
137 /* Get the right path name destination */
138 if (consumer->type == CONSUMER_DST_LOCAL) {
139 /* Set application path to the destination path */
140 ret = snprintf(tmp_path, sizeof(tmp_path), "%s/%s/%s",
141 consumer->dst.trace_path, consumer->subdir, usess->path);
142 if (ret < 0) {
143 PERROR("snprintf stream path");
144 goto error;
145 }
146 pathname = tmp_path;
147 DBG3("UST local consumer tracefile path: %s", pathname);
148 } else {
149 ret = snprintf(tmp_path, sizeof(tmp_path), "%s/%s",
150 consumer->subdir, usess->path);
151 if (ret < 0) {
152 PERROR("snprintf stream path");
153 goto error;
154 }
155 pathname = tmp_path;
156 DBG3("UST network consumer subdir path: %s", pathname);
157 }
158
159 cds_list_for_each_entry_safe(stream, tmp, &uchan->streams.head, list) {
160 if (!stream->obj->shm_fd) {
161 continue;
162 }
163
164 ret = send_channel_stream(sock, uchan, usess, stream, consumer,
165 pathname);
166 if (ret < 0) {
167 goto error;
168 }
169 }
170
171 DBG("UST consumer channel streams sent");
172
173 return 0;
174
175 error:
176 return ret;
177 }
178
179 /*
180 * Sending metadata to the consumer with command ADD_CHANNEL and ADD_STREAM.
181 */
182 static int send_metadata(int sock, struct ust_app_session *usess,
183 struct consumer_output *consumer)
184 {
185 int ret, fd, fds[2];
186 char tmp_path[PATH_MAX];
187 const char *pathname;
188 struct lttcomm_consumer_msg msg;
189
190 /* Safety net */
191 assert(usess);
192 assert(consumer);
193
194 if (sock < 0) {
195 ERR("Consumer socket is negative (%d)", sock);
196 return -EINVAL;
197 }
198
199 if (usess->metadata->obj->shm_fd == 0) {
200 ERR("Metadata obj shm_fd is 0");
201 ret = -1;
202 goto error;
203 }
204
205 DBG("UST consumer sending metadata stream fd");
206
207 consumer_init_channel_comm_msg(&msg,
208 LTTNG_CONSUMER_ADD_CHANNEL,
209 usess->metadata->obj->shm_fd,
210 usess->metadata->attr.subbuf_size,
211 usess->metadata->obj->memory_map_size,
212 "metadata",
213 1);
214
215 ret = consumer_send_channel(sock, &msg);
216 if (ret < 0) {
217 goto error;
218 }
219
220 /* Sending metadata shared memory fd */
221 fd = usess->metadata->obj->shm_fd;
222 ret = consumer_send_fds(sock, &fd, 1);
223 if (ret < 0) {
224 goto error;
225 }
226
227 /* Get correct path name destination */
228 if (consumer->type == CONSUMER_DST_LOCAL) {
229 /* Set application path to the destination path */
230 ret = snprintf(tmp_path, sizeof(tmp_path), "%s/%s/%s",
231 consumer->dst.trace_path, consumer->subdir, usess->path);
232 if (ret < 0) {
233 PERROR("snprintf stream path");
234 goto error;
235 }
236 pathname = tmp_path;
237
238 /* Create directory */
239 ret = run_as_mkdir_recursive(pathname, S_IRWXU | S_IRWXG,
240 usess->uid, usess->gid);
241 if (ret < 0) {
242 if (ret != -EEXIST) {
243 ERR("Trace directory creation error");
244 goto error;
245 }
246 }
247 } else {
248 ret = snprintf(tmp_path, sizeof(tmp_path), "%s/%s",
249 consumer->subdir, usess->path);
250 if (ret < 0) {
251 PERROR("snprintf metadata path");
252 goto error;
253 }
254 pathname = tmp_path;
255 }
256
257 consumer_init_stream_comm_msg(&msg,
258 LTTNG_CONSUMER_ADD_STREAM,
259 usess->metadata->obj->shm_fd,
260 usess->metadata->stream_obj->shm_fd,
261 LTTNG_CONSUMER_ACTIVE_STREAM,
262 DEFAULT_UST_CHANNEL_OUTPUT,
263 usess->metadata->stream_obj->memory_map_size,
264 usess->uid,
265 usess->gid,
266 consumer->net_seq_index,
267 1, /* Flag metadata set */
268 "metadata",
269 pathname);
270
271 /* Send stream and file descriptor */
272 fds[0] = usess->metadata->stream_obj->shm_fd;
273 fds[1] = usess->metadata->stream_obj->wait_fd;
274 ret = consumer_send_stream(sock, consumer, &msg, fds, 2);
275 if (ret < 0) {
276 goto error;
277 }
278
279 error:
280 return ret;
281 }
282
283 /*
284 * Send all stream fds of the UST session to the consumer.
285 */
286 int ust_consumer_send_session(struct ust_app_session *usess,
287 struct consumer_output *consumer, struct consumer_socket *sock)
288 {
289 int ret = 0;
290 struct lttng_ht_iter iter;
291 struct ust_app_channel *ua_chan;
292
293 assert(usess);
294
295 if (consumer == NULL || sock == NULL) {
296 /* There is no consumer so just ignoring the command. */
297 DBG("UST consumer does not exist. Not sending streams");
298 return 0;
299 }
300
301 DBG("Sending metadata stream fd to consumer on %d", sock->fd);
302
303 pthread_mutex_lock(sock->lock);
304
305 /* Sending metadata information to the consumer */
306 ret = send_metadata(sock->fd, usess, consumer);
307 if (ret < 0) {
308 goto error;
309 }
310
311 /* Send each channel fd streams of session */
312 rcu_read_lock();
313 cds_lfht_for_each_entry(usess->channels->ht, &iter.iter, ua_chan,
314 node.node) {
315 /*
316 * Indicate that the channel was not created on the tracer side so skip
317 * sending unexisting streams.
318 */
319 if (ua_chan->obj == NULL) {
320 continue;
321 }
322
323 ret = send_channel_streams(sock->fd, ua_chan, usess, consumer);
324 if (ret < 0) {
325 rcu_read_unlock();
326 goto error;
327 }
328 }
329 rcu_read_unlock();
330
331 DBG("consumer fds (metadata and channel streams) sent");
332
333 /* All good! */
334 ret = 0;
335
336 error:
337 pthread_mutex_unlock(sock->lock);
338 return ret;
339 }
This page took 0.036212 seconds and 4 git commands to generate.