Fix: Possible buffer overflows in strncat() usage
[lttng-tools.git] / src / common / ust-consumer / ust-consumer.c
CommitLineData
3bd1e081
MD
1/*
2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
d14d33bf
AM
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.
3bd1e081
MD
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 *
d14d33bf
AM
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.
3bd1e081
MD
17 */
18
19#define _GNU_SOURCE
20#include <assert.h>
f02e1e8a 21#include <lttng/ust-ctl.h>
3bd1e081
MD
22#include <poll.h>
23#include <pthread.h>
24#include <stdlib.h>
25#include <string.h>
26#include <sys/mman.h>
27#include <sys/socket.h>
dbb5dfe6 28#include <sys/stat.h>
3bd1e081 29#include <sys/types.h>
77c7c900 30#include <inttypes.h>
3bd1e081 31#include <unistd.h>
0857097f 32
990570ed 33#include <common/common.h>
10a8a223 34#include <common/sessiond-comm/sessiond-comm.h>
00e2e675 35#include <common/relayd/relayd.h>
dbb5dfe6 36#include <common/compat/fcntl.h>
10a8a223
DG
37
38#include "ust-consumer.h"
3bd1e081
MD
39
40extern struct lttng_consumer_global_data consumer_data;
41extern int consumer_poll_timeout;
42extern volatile int consumer_quit;
43
44/*
f02e1e8a
DG
45 * Wrapper over the mmap() read offset from ust-ctl library. Since this can be
46 * compiled out, we isolate it in this library.
3bd1e081 47 */
f02e1e8a
DG
48int lttng_ustctl_get_mmap_read_offset(struct lttng_ust_shm_handle *handle,
49 struct lttng_ust_lib_ring_buffer *buf, unsigned long *off)
3bd1e081 50{
f02e1e8a
DG
51 return ustctl_get_mmap_read_offset(handle, buf, off);
52};
3bd1e081
MD
53
54/*
55 * Take a snapshot for a specific fd
56 *
57 * Returns 0 on success, < 0 on error
58 */
59int lttng_ustconsumer_take_snapshot(struct lttng_consumer_local_data *ctx,
60 struct lttng_consumer_stream *stream)
61{
62 int ret = 0;
63
64 ret = ustctl_snapshot(stream->chan->handle, stream->buf);
65 if (ret != 0) {
87dc6a9c 66 errno = -ret;
4c462e79 67 PERROR("Getting sub-buffer snapshot.");
3bd1e081
MD
68 }
69
70 return ret;
71}
72
73/*
74 * Get the produced position
75 *
76 * Returns 0 on success, < 0 on error
77 */
78int lttng_ustconsumer_get_produced_snapshot(
79 struct lttng_consumer_local_data *ctx,
80 struct lttng_consumer_stream *stream,
81 unsigned long *pos)
82{
83 int ret;
84
85 ret = ustctl_snapshot_get_produced(stream->chan->handle,
86 stream->buf, pos);
87 if (ret != 0) {
87dc6a9c 88 errno = -ret;
4c462e79 89 PERROR("kernctl_snapshot_get_produced");
3bd1e081
MD
90 }
91
92 return ret;
93}
94
95int lttng_ustconsumer_recv_cmd(struct lttng_consumer_local_data *ctx,
96 int sock, struct pollfd *consumer_sockpoll)
97{
98 ssize_t ret;
99 struct lttcomm_consumer_msg msg;
100
101 ret = lttcomm_recv_unix_sock(sock, &msg, sizeof(msg));
102 if (ret != sizeof(msg)) {
173af62f
DG
103 DBG("Consumer received unexpected message size %zd (expects %zu)",
104 ret, sizeof(msg));
3bd1e081
MD
105 lttng_consumer_send_error(ctx, CONSUMERD_ERROR_RECV_FD);
106 return ret;
107 }
108 if (msg.cmd_type == LTTNG_CONSUMER_STOP) {
109 return -ENOENT;
110 }
111
3f8e211f 112 /* relayd needs RCU read-side lock */
b0b335c8
MD
113 rcu_read_lock();
114
3bd1e081 115 switch (msg.cmd_type) {
00e2e675
DG
116 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET:
117 {
118 int fd;
119 struct consumer_relayd_sock_pair *relayd;
120
121 DBG("UST Consumer adding relayd socket");
122
123 /* Get relayd reference if exists. */
124 relayd = consumer_find_relayd(msg.u.relayd_sock.net_index);
125 if (relayd == NULL) {
126 /* Not found. Allocate one. */
127 relayd = consumer_allocate_relayd_sock_pair(
128 msg.u.relayd_sock.net_index);
129 if (relayd == NULL) {
130 lttng_consumer_send_error(ctx, CONSUMERD_OUTFD_ERROR);
131 goto end_nosignal;
132 }
133 }
134
135 /* Poll on consumer socket. */
136 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
3f8e211f 137 rcu_read_unlock();
00e2e675
DG
138 return -EINTR;
139 }
140
141 /* Get relayd socket from session daemon */
142 ret = lttcomm_recv_fds_unix_sock(sock, &fd, 1);
143 if (ret != sizeof(fd)) {
144 lttng_consumer_send_error(ctx, CONSUMERD_ERROR_RECV_FD);
145 goto end_nosignal;
146 }
147
148 /* Copy socket information and received FD */
149 switch (msg.u.relayd_sock.type) {
150 case LTTNG_STREAM_CONTROL:
151 /* Copy received lttcomm socket */
152 lttcomm_copy_sock(&relayd->control_sock, &msg.u.relayd_sock.sock);
153 ret = lttcomm_create_sock(&relayd->control_sock);
154 if (ret < 0) {
155 goto end_nosignal;
156 }
157
158 /* Close the created socket fd which is useless */
159 close(relayd->control_sock.fd);
160
161 /* Assign new file descriptor */
162 relayd->control_sock.fd = fd;
163 break;
164 case LTTNG_STREAM_DATA:
165 /* Copy received lttcomm socket */
166 lttcomm_copy_sock(&relayd->data_sock, &msg.u.relayd_sock.sock);
167 ret = lttcomm_create_sock(&relayd->data_sock);
168 if (ret < 0) {
169 goto end_nosignal;
170 }
171
172 /* Close the created socket fd which is useless */
173 close(relayd->data_sock.fd);
174
175 /* Assign new file descriptor */
176 relayd->data_sock.fd = fd;
177 break;
178 default:
179 ERR("Unknown relayd socket type");
180 goto end_nosignal;
181 }
182
183 DBG("Consumer %s socket created successfully with net idx %d (fd: %d)",
184 msg.u.relayd_sock.type == LTTNG_STREAM_CONTROL ? "control" : "data",
185 relayd->net_seq_idx, fd);
186
187 /*
188 * Add relayd socket pair to consumer data hashtable. If object already
189 * exists or on error, the function gracefully returns.
190 */
191 consumer_add_relayd(relayd);
192
193 goto end_nosignal;
194 }
3bd1e081
MD
195 case LTTNG_CONSUMER_ADD_CHANNEL:
196 {
197 struct lttng_consumer_channel *new_channel;
198 int fds[1];
199 size_t nb_fd = 1;
200
173af62f
DG
201 DBG("UST Consumer adding channel");
202
3bd1e081
MD
203 /* block */
204 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
3f8e211f 205 rcu_read_unlock();
3bd1e081
MD
206 return -EINTR;
207 }
208 ret = lttcomm_recv_fds_unix_sock(sock, fds, nb_fd);
209 if (ret != sizeof(fds)) {
210 lttng_consumer_send_error(ctx, CONSUMERD_ERROR_RECV_FD);
3f8e211f 211 rcu_read_unlock();
3bd1e081
MD
212 return ret;
213 }
214
215 DBG("consumer_add_channel %d", msg.u.channel.channel_key);
216
217 new_channel = consumer_allocate_channel(msg.u.channel.channel_key,
218 fds[0], -1,
219 msg.u.channel.mmap_len,
220 msg.u.channel.max_sb_size);
221 if (new_channel == NULL) {
222 lttng_consumer_send_error(ctx, CONSUMERD_OUTFD_ERROR);
223 goto end_nosignal;
224 }
225 if (ctx->on_recv_channel != NULL) {
226 ret = ctx->on_recv_channel(new_channel);
227 if (ret == 0) {
228 consumer_add_channel(new_channel);
229 } else if (ret < 0) {
230 goto end_nosignal;
231 }
232 } else {
233 consumer_add_channel(new_channel);
234 }
235 goto end_nosignal;
236 }
237 case LTTNG_CONSUMER_ADD_STREAM:
238 {
239 struct lttng_consumer_stream *new_stream;
240 int fds[2];
241 size_t nb_fd = 2;
00e2e675 242 struct consumer_relayd_sock_pair *relayd = NULL;
3bd1e081 243
173af62f
DG
244 DBG("UST Consumer adding stream");
245
3bd1e081
MD
246 /* block */
247 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
3f8e211f 248 rcu_read_unlock();
3bd1e081
MD
249 return -EINTR;
250 }
251 ret = lttcomm_recv_fds_unix_sock(sock, fds, nb_fd);
252 if (ret != sizeof(fds)) {
253 lttng_consumer_send_error(ctx, CONSUMERD_ERROR_RECV_FD);
3f8e211f 254 rcu_read_unlock();
3bd1e081
MD
255 return ret;
256 }
257
173af62f
DG
258 DBG("consumer_add_stream chan %d stream %d",
259 msg.u.stream.channel_key,
260 msg.u.stream.stream_key);
261
d41f73b7 262 assert(msg.u.stream.output == LTTNG_EVENT_MMAP);
173af62f 263 new_stream = consumer_allocate_stream(msg.u.stream.channel_key,
3bd1e081
MD
264 msg.u.stream.stream_key,
265 fds[0], fds[1],
266 msg.u.stream.state,
267 msg.u.stream.mmap_len,
268 msg.u.stream.output,
6df2e2c9
MD
269 msg.u.stream.path_name,
270 msg.u.stream.uid,
00e2e675
DG
271 msg.u.stream.gid,
272 msg.u.stream.net_index,
273 msg.u.stream.metadata_flag);
3bd1e081
MD
274 if (new_stream == NULL) {
275 lttng_consumer_send_error(ctx, CONSUMERD_OUTFD_ERROR);
3f8e211f 276 goto end_nosignal;
3bd1e081 277 }
00e2e675
DG
278
279 /* The stream is not metadata. Get relayd reference if exists. */
280 relayd = consumer_find_relayd(msg.u.stream.net_index);
281 if (relayd != NULL) {
282 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
283 /* Add stream on the relayd */
284 ret = relayd_add_stream(&relayd->control_sock,
285 msg.u.stream.name, msg.u.stream.path_name,
286 &new_stream->relayd_stream_id);
287 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
288 if (ret < 0) {
3f8e211f 289 goto end_nosignal;
00e2e675
DG
290 }
291 } else if (msg.u.stream.net_index != -1) {
292 ERR("Network sequence index %d unknown. Not adding stream.",
293 msg.u.stream.net_index);
294 free(new_stream);
3f8e211f 295 goto end_nosignal;
00e2e675
DG
296 }
297
3bd1e081
MD
298 if (ctx->on_recv_stream != NULL) {
299 ret = ctx->on_recv_stream(new_stream);
300 if (ret == 0) {
301 consumer_add_stream(new_stream);
302 } else if (ret < 0) {
3f8e211f 303 goto end_nosignal;
3bd1e081
MD
304 }
305 } else {
306 consumer_add_stream(new_stream);
307 }
00e2e675 308
77c7c900 309 DBG("UST consumer_add_stream %s (%d,%d) with relayd id %" PRIu64,
00e2e675
DG
310 msg.u.stream.path_name, fds[0], fds[1],
311 new_stream->relayd_stream_id);
3bd1e081
MD
312 break;
313 }
173af62f
DG
314 case LTTNG_CONSUMER_DESTROY_RELAYD:
315 {
316 struct consumer_relayd_sock_pair *relayd;
317
77c7c900 318 DBG("UST consumer destroying relayd %" PRIu64,
173af62f
DG
319 msg.u.destroy_relayd.net_seq_idx);
320
321 /* Get relayd reference if exists. */
322 relayd = consumer_find_relayd(msg.u.destroy_relayd.net_seq_idx);
323 if (relayd == NULL) {
77c7c900 324 ERR("Unable to find relayd %" PRIu64, msg.u.destroy_relayd.net_seq_idx);
3f8e211f 325 goto end_nosignal;
173af62f
DG
326 }
327
328 /* Set destroy flag for this object */
329 uatomic_set(&relayd->destroy_flag, 1);
330
331 /* Destroy the relayd if refcount is 0 else set the destroy flag. */
332 if (uatomic_read(&relayd->refcount) == 0) {
333 consumer_destroy_relayd(relayd);
334 }
3f8e211f 335 goto end_nosignal;
173af62f 336 }
3bd1e081
MD
337 case LTTNG_CONSUMER_UPDATE_STREAM:
338 {
3f8e211f 339 rcu_read_unlock();
7ad0a0cb
MD
340 return -ENOSYS;
341#if 0
3bd1e081
MD
342 if (ctx->on_update_stream != NULL) {
343 ret = ctx->on_update_stream(msg.u.stream.stream_key, msg.u.stream.state);
344 if (ret == 0) {
345 consumer_change_stream_state(msg.u.stream.stream_key, msg.u.stream.state);
346 } else if (ret < 0) {
347 goto end;
348 }
349 } else {
350 consumer_change_stream_state(msg.u.stream.stream_key,
351 msg.u.stream.state);
352 }
353 break;
3f8e211f 354#endif
3bd1e081
MD
355 }
356 default:
357 break;
358 }
3f8e211f 359
04fdd819 360 /*
3f8e211f
DG
361 * Wake-up the other end by writing a null byte in the pipe (non-blocking).
362 * Important note: Because writing into the pipe is non-blocking (and
363 * therefore we allow dropping wakeup data, as long as there is wakeup data
364 * present in the pipe buffer to wake up the other end), the other end
365 * should perform the following sequence for waiting:
366 *
04fdd819
MD
367 * 1) empty the pipe (reads).
368 * 2) perform update operation.
369 * 3) wait on the pipe (poll).
370 */
371 do {
372 ret = write(ctx->consumer_poll_pipe[1], "", 1);
6f94560a 373 } while (ret < 0 && errno == EINTR);
3bd1e081 374end_nosignal:
b0b335c8 375 rcu_read_unlock();
3bd1e081
MD
376 return 0;
377}
378
379int lttng_ustconsumer_allocate_channel(struct lttng_consumer_channel *chan)
380{
13161846 381 struct lttng_ust_object_data obj;
3bd1e081
MD
382
383 obj.handle = -1;
384 obj.shm_fd = chan->shm_fd;
385 obj.wait_fd = chan->wait_fd;
386 obj.memory_map_size = chan->mmap_len;
387 chan->handle = ustctl_map_channel(&obj);
388 if (!chan->handle) {
389 return -ENOMEM;
390 }
b5c5fc29 391 chan->wait_fd_is_copy = 1;
2c1dd183 392 chan->shm_fd = -1;
b5c5fc29 393
3bd1e081
MD
394 return 0;
395}
396
d056b477
MD
397void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream *stream)
398{
399 ustctl_flush_buffer(stream->chan->handle, stream->buf, 0);
effcf122 400 stream->hangup_flush_done = 1;
d056b477
MD
401}
402
3bd1e081
MD
403void lttng_ustconsumer_del_channel(struct lttng_consumer_channel *chan)
404{
405 ustctl_unmap_channel(chan->handle);
406}
407
408int lttng_ustconsumer_allocate_stream(struct lttng_consumer_stream *stream)
409{
13161846 410 struct lttng_ust_object_data obj;
3bd1e081
MD
411 int ret;
412
413 obj.handle = -1;
414 obj.shm_fd = stream->shm_fd;
415 obj.wait_fd = stream->wait_fd;
416 obj.memory_map_size = stream->mmap_len;
417 ret = ustctl_add_stream(stream->chan->handle, &obj);
418 if (ret)
419 return ret;
420 stream->buf = ustctl_open_stream_read(stream->chan->handle, stream->cpu);
421 if (!stream->buf)
422 return -EBUSY;
2c1dd183
MD
423 /* ustctl_open_stream_read has closed the shm fd. */
424 stream->wait_fd_is_copy = 1;
425 stream->shm_fd = -1;
426
3bd1e081
MD
427 stream->mmap_base = ustctl_get_mmap_base(stream->chan->handle, stream->buf);
428 if (!stream->mmap_base) {
429 return -EINVAL;
430 }
ee77a7b0 431
3bd1e081
MD
432 return 0;
433}
434
435void lttng_ustconsumer_del_stream(struct lttng_consumer_stream *stream)
436{
437 ustctl_close_stream_read(stream->chan->handle, stream->buf);
438}
d41f73b7
MD
439
440
441int lttng_ustconsumer_read_subbuffer(struct lttng_consumer_stream *stream,
442 struct lttng_consumer_local_data *ctx)
443{
444 unsigned long len;
445 int err;
446 long ret = 0;
447 struct lttng_ust_shm_handle *handle;
448 struct lttng_ust_lib_ring_buffer *buf;
449 char dummy;
450 ssize_t readlen;
451
452 DBG("In read_subbuffer (wait_fd: %d, stream key: %d)",
453 stream->wait_fd, stream->key);
454
455 /* We can consume the 1 byte written into the wait_fd by UST */
effcf122
MD
456 if (!stream->hangup_flush_done) {
457 do {
458 readlen = read(stream->wait_fd, &dummy, 1);
87dc6a9c 459 } while (readlen == -1 && errno == EINTR);
effcf122
MD
460 if (readlen == -1) {
461 ret = readlen;
462 goto end;
463 }
d41f73b7
MD
464 }
465
466 buf = stream->buf;
467 handle = stream->chan->handle;
468 /* Get the next subbuffer */
469 err = ustctl_get_next_subbuf(handle, buf);
470 if (err != 0) {
effcf122 471 ret = -ret; /* ustctl_get_next_subbuf returns negative, caller expect positive. */
d41f73b7
MD
472 /*
473 * This is a debug message even for single-threaded consumer,
474 * because poll() have more relaxed criterions than get subbuf,
475 * so get_subbuf may fail for short race windows where poll()
476 * would issue wakeups.
477 */
478 DBG("Reserving sub buffer failed (everything is normal, "
479 "it is due to concurrency)");
480 goto end;
481 }
482 assert(stream->output == LTTNG_EVENT_MMAP);
483 /* read the used subbuffer size */
484 err = ustctl_get_padded_subbuf_size(handle, buf, &len);
effcf122 485 assert(err == 0);
d41f73b7
MD
486 /* write the subbuffer to the tracefile */
487 ret = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, len);
47e81c02 488 if (ret != len) {
d41f73b7
MD
489 /*
490 * display the error but continue processing to try
491 * to release the subbuffer
492 */
a4b92340 493 ERR("Error writing to tracefile (expected: %ld, got: %ld)", ret, len);
d41f73b7
MD
494 }
495 err = ustctl_put_next_subbuf(handle, buf);
effcf122 496 assert(err == 0);
d41f73b7
MD
497end:
498 return ret;
499}
500
501int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream *stream)
502{
503 int ret;
504
505 /* Opening the tracefile in write mode */
00e2e675 506 if (stream->path_name != NULL && stream->net_seq_idx == -1) {
e11d277b 507 ret = run_as_open(stream->path_name,
60b6c79c
MD
508 O_WRONLY|O_CREAT|O_TRUNC,
509 S_IRWXU|S_IRWXG|S_IRWXO,
510 stream->uid, stream->gid);
d41f73b7
MD
511 if (ret < 0) {
512 ERR("Opening %s", stream->path_name);
4c462e79 513 PERROR("open");
d41f73b7
MD
514 goto error;
515 }
516 stream->out_fd = ret;
517 }
518
519 /* we return 0 to let the library handle the FD internally */
520 return 0;
521
522error:
523 return ret;
524}
This page took 0.054764 seconds and 4 git commands to generate.