47463c6501eddb48c4963d99c15b3d3c510c035c
[lttng-tools.git] / src / common / kernel-consumer / kernel-consumer.c
1 /*
2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
8 *
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.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #define _GNU_SOURCE
20 #include <assert.h>
21 #include <poll.h>
22 #include <pthread.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/mman.h>
26 #include <sys/socket.h>
27 #include <sys/types.h>
28 #include <inttypes.h>
29 #include <unistd.h>
30 #include <sys/stat.h>
31
32 #include <common/common.h>
33 #include <common/kernel-ctl/kernel-ctl.h>
34 #include <common/sessiond-comm/sessiond-comm.h>
35 #include <common/sessiond-comm/relayd.h>
36 #include <common/compat/fcntl.h>
37 #include <common/relayd/relayd.h>
38
39 #include "kernel-consumer.h"
40
41 extern struct lttng_consumer_global_data consumer_data;
42 extern int consumer_poll_timeout;
43 extern volatile int consumer_quit;
44
45 /*
46 * Take a snapshot for a specific fd
47 *
48 * Returns 0 on success, < 0 on error
49 */
50 int lttng_kconsumer_take_snapshot(struct lttng_consumer_local_data *ctx,
51 struct lttng_consumer_stream *stream)
52 {
53 int ret = 0;
54 int infd = stream->wait_fd;
55
56 ret = kernctl_snapshot(infd);
57 if (ret != 0) {
58 errno = -ret;
59 perror("Getting sub-buffer snapshot.");
60 }
61
62 return ret;
63 }
64
65 /*
66 * Get the produced position
67 *
68 * Returns 0 on success, < 0 on error
69 */
70 int lttng_kconsumer_get_produced_snapshot(
71 struct lttng_consumer_local_data *ctx,
72 struct lttng_consumer_stream *stream,
73 unsigned long *pos)
74 {
75 int ret;
76 int infd = stream->wait_fd;
77
78 ret = kernctl_snapshot_get_produced(infd, pos);
79 if (ret != 0) {
80 errno = -ret;
81 perror("kernctl_snapshot_get_produced");
82 }
83
84 return ret;
85 }
86
87 int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data *ctx,
88 int sock, struct pollfd *consumer_sockpoll)
89 {
90 ssize_t ret;
91 struct lttcomm_consumer_msg msg;
92
93 ret = lttcomm_recv_unix_sock(sock, &msg, sizeof(msg));
94 if (ret != sizeof(msg)) {
95 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_CMD);
96 return ret;
97 }
98 if (msg.cmd_type == LTTNG_CONSUMER_STOP) {
99 return -ENOENT;
100 }
101
102 /* relayd needs RCU read-side protection */
103 rcu_read_lock();
104
105 switch (msg.cmd_type) {
106 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET:
107 {
108 ret = consumer_add_relayd_socket(msg.u.relayd_sock.net_index,
109 msg.u.relayd_sock.type, ctx, sock, consumer_sockpoll,
110 &msg.u.relayd_sock.sock);
111 goto end_nosignal;
112 }
113 case LTTNG_CONSUMER_ADD_CHANNEL:
114 {
115 struct lttng_consumer_channel *new_channel;
116
117 DBG("consumer_add_channel %d", msg.u.channel.channel_key);
118 new_channel = consumer_allocate_channel(msg.u.channel.channel_key,
119 -1, -1,
120 msg.u.channel.mmap_len,
121 msg.u.channel.max_sb_size);
122 if (new_channel == NULL) {
123 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
124 goto end_nosignal;
125 }
126 if (ctx->on_recv_channel != NULL) {
127 ret = ctx->on_recv_channel(new_channel);
128 if (ret == 0) {
129 consumer_add_channel(new_channel);
130 } else if (ret < 0) {
131 goto end_nosignal;
132 }
133 } else {
134 consumer_add_channel(new_channel);
135 }
136 goto end_nosignal;
137 }
138 case LTTNG_CONSUMER_ADD_STREAM:
139 {
140 int fd;
141 struct consumer_relayd_sock_pair *relayd = NULL;
142 struct lttng_consumer_stream *new_stream;
143
144 /* block */
145 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
146 rcu_read_unlock();
147 return -EINTR;
148 }
149
150 /* Get stream file descriptor from socket */
151 ret = lttcomm_recv_fds_unix_sock(sock, &fd, 1);
152 if (ret != sizeof(fd)) {
153 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD);
154 rcu_read_unlock();
155 return ret;
156 }
157
158 new_stream = consumer_allocate_stream(msg.u.stream.channel_key,
159 msg.u.stream.stream_key,
160 fd, fd,
161 msg.u.stream.state,
162 msg.u.stream.mmap_len,
163 msg.u.stream.output,
164 msg.u.stream.path_name,
165 msg.u.stream.uid,
166 msg.u.stream.gid,
167 msg.u.stream.net_index,
168 msg.u.stream.metadata_flag);
169 if (new_stream == NULL) {
170 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
171 goto end_nosignal;
172 }
173
174 /* The stream is not metadata. Get relayd reference if exists. */
175 relayd = consumer_find_relayd(msg.u.stream.net_index);
176 if (relayd != NULL) {
177 /* Add stream on the relayd */
178 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
179 ret = relayd_add_stream(&relayd->control_sock,
180 msg.u.stream.name, msg.u.stream.path_name,
181 &new_stream->relayd_stream_id);
182 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
183 if (ret < 0) {
184 goto end_nosignal;
185 }
186 } else if (msg.u.stream.net_index != -1) {
187 ERR("Network sequence index %d unknown. Not adding stream.",
188 msg.u.stream.net_index);
189 free(new_stream);
190 goto end_nosignal;
191 }
192
193 if (ctx->on_recv_stream != NULL) {
194 ret = ctx->on_recv_stream(new_stream);
195 if (ret == 0) {
196 consumer_add_stream(new_stream);
197 } else if (ret < 0) {
198 goto end_nosignal;
199 }
200 } else {
201 consumer_add_stream(new_stream);
202 }
203
204 DBG("Kernel consumer_add_stream (%d)", fd);
205 break;
206 }
207 case LTTNG_CONSUMER_UPDATE_STREAM:
208 {
209 rcu_read_unlock();
210 return -ENOSYS;
211 }
212 case LTTNG_CONSUMER_DESTROY_RELAYD:
213 {
214 uint64_t index = msg.u.destroy_relayd.net_seq_idx;
215 struct consumer_relayd_sock_pair *relayd;
216
217 DBG("Kernel consumer destroying relayd %" PRIu64, index);
218
219 /* Get relayd reference if exists. */
220 relayd = consumer_find_relayd(index);
221 if (relayd == NULL) {
222 ERR("Unable to find relayd %" PRIu64, index);
223 goto end_nosignal;
224 }
225
226 /*
227 * Each relayd socket pair has a refcount of stream attached to it
228 * which tells if the relayd is still active or not depending on the
229 * refcount value.
230 *
231 * This will set the destroy flag of the relayd object and destroy it
232 * if the refcount reaches zero when called.
233 *
234 * The destroy can happen either here or when a stream fd hangs up.
235 */
236 consumer_flag_relayd_for_destroy(relayd);
237
238 goto end_nosignal;
239 }
240 default:
241 goto end_nosignal;
242 }
243
244 /*
245 * Wake-up the other end by writing a null byte in the pipe (non-blocking).
246 * Important note: Because writing into the pipe is non-blocking (and
247 * therefore we allow dropping wakeup data, as long as there is wakeup data
248 * present in the pipe buffer to wake up the other end), the other end
249 * should perform the following sequence for waiting:
250 *
251 * 1) empty the pipe (reads).
252 * 2) perform update operation.
253 * 3) wait on the pipe (poll).
254 */
255 do {
256 ret = write(ctx->consumer_poll_pipe[1], "", 1);
257 } while (ret < 0 && errno == EINTR);
258 end_nosignal:
259 rcu_read_unlock();
260
261 /*
262 * Return 1 to indicate success since the 0 value can be a socket
263 * shutdown during the recv() or send() call.
264 */
265 return 1;
266 }
267
268 /*
269 * Consume data on a file descriptor and write it on a trace file.
270 */
271 ssize_t lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream *stream,
272 struct lttng_consumer_local_data *ctx)
273 {
274 unsigned long len;
275 int err;
276 ssize_t ret = 0;
277 int infd = stream->wait_fd;
278
279 DBG("In read_subbuffer (infd : %d)", infd);
280 /* Get the next subbuffer */
281 err = kernctl_get_next_subbuf(infd);
282 if (err != 0) {
283 /*
284 * This is a debug message even for single-threaded consumer,
285 * because poll() have more relaxed criterions than get subbuf,
286 * so get_subbuf may fail for short race windows where poll()
287 * would issue wakeups.
288 */
289 DBG("Reserving sub buffer failed (everything is normal, "
290 "it is due to concurrency)");
291 goto end;
292 }
293
294 switch (stream->output) {
295 case LTTNG_EVENT_SPLICE:
296 /* read the whole subbuffer */
297 err = kernctl_get_padded_subbuf_size(infd, &len);
298 if (err != 0) {
299 errno = -ret;
300 perror("Getting sub-buffer len failed.");
301 goto end;
302 }
303
304 /* splice the subbuffer to the tracefile */
305 ret = lttng_consumer_on_read_subbuffer_splice(ctx, stream, len);
306 if (ret != len) {
307 /*
308 * display the error but continue processing to try
309 * to release the subbuffer
310 */
311 ERR("Error splicing to tracefile (ret: %zd != len: %lu)",
312 ret, len);
313 }
314
315 break;
316 case LTTNG_EVENT_MMAP:
317 /* read the used subbuffer size */
318 err = kernctl_get_padded_subbuf_size(infd, &len);
319 if (err != 0) {
320 errno = -ret;
321 perror("Getting sub-buffer len failed.");
322 goto end;
323 }
324 /* write the subbuffer to the tracefile */
325 ret = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, len);
326 if (ret != len) {
327 /*
328 * display the error but continue processing to try
329 * to release the subbuffer
330 */
331 ERR("Error writing to tracefile");
332 }
333 break;
334 default:
335 ERR("Unknown output method");
336 ret = -1;
337 }
338
339 err = kernctl_put_next_subbuf(infd);
340 if (err != 0) {
341 errno = -ret;
342 if (errno == EFAULT) {
343 perror("Error in unreserving sub buffer\n");
344 } else if (errno == EIO) {
345 /* Should never happen with newer LTTng versions */
346 perror("Reader has been pushed by the writer, last sub-buffer corrupted.");
347 }
348 goto end;
349 }
350
351 end:
352 return ret;
353 }
354
355 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream *stream)
356 {
357 int ret;
358
359 /* Opening the tracefile in write mode */
360 if (strlen(stream->path_name) > 0 && stream->net_seq_idx == -1) {
361 ret = run_as_open(stream->path_name,
362 O_WRONLY|O_CREAT|O_TRUNC,
363 S_IRWXU|S_IRWXG|S_IRWXO,
364 stream->uid, stream->gid);
365 if (ret < 0) {
366 ERR("Opening %s", stream->path_name);
367 perror("open");
368 goto error;
369 }
370 stream->out_fd = ret;
371 }
372
373 if (stream->output == LTTNG_EVENT_MMAP) {
374 /* get the len of the mmap region */
375 unsigned long mmap_len;
376
377 ret = kernctl_get_mmap_len(stream->wait_fd, &mmap_len);
378 if (ret != 0) {
379 errno = -ret;
380 perror("kernctl_get_mmap_len");
381 goto error_close_fd;
382 }
383 stream->mmap_len = (size_t) mmap_len;
384
385 stream->mmap_base = mmap(NULL, stream->mmap_len,
386 PROT_READ, MAP_PRIVATE, stream->wait_fd, 0);
387 if (stream->mmap_base == MAP_FAILED) {
388 perror("Error mmaping");
389 ret = -1;
390 goto error_close_fd;
391 }
392 }
393
394 /* we return 0 to let the library handle the FD internally */
395 return 0;
396
397 error_close_fd:
398 {
399 int err;
400
401 err = close(stream->out_fd);
402 assert(!err);
403 }
404 error:
405 return ret;
406 }
407
This page took 0.035748 seconds and 3 git commands to generate.