Add lttng-error.h containing every API err. code
[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));
f73fabfd 105 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD);
3bd1e081
MD
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 {
7735ef9e
DG
118 ret = consumer_add_relayd_socket(msg.u.relayd_sock.net_index,
119 msg.u.relayd_sock.type, ctx, sock, consumer_sockpoll,
120 &msg.u.relayd_sock.sock);
00e2e675
DG
121 goto end_nosignal;
122 }
3bd1e081
MD
123 case LTTNG_CONSUMER_ADD_CHANNEL:
124 {
125 struct lttng_consumer_channel *new_channel;
126 int fds[1];
127 size_t nb_fd = 1;
128
173af62f
DG
129 DBG("UST Consumer adding channel");
130
3bd1e081
MD
131 /* block */
132 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
3f8e211f 133 rcu_read_unlock();
3bd1e081
MD
134 return -EINTR;
135 }
136 ret = lttcomm_recv_fds_unix_sock(sock, fds, nb_fd);
137 if (ret != sizeof(fds)) {
f73fabfd 138 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD);
3f8e211f 139 rcu_read_unlock();
3bd1e081
MD
140 return ret;
141 }
142
143 DBG("consumer_add_channel %d", msg.u.channel.channel_key);
144
145 new_channel = consumer_allocate_channel(msg.u.channel.channel_key,
146 fds[0], -1,
147 msg.u.channel.mmap_len,
148 msg.u.channel.max_sb_size);
149 if (new_channel == NULL) {
f73fabfd 150 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
3bd1e081
MD
151 goto end_nosignal;
152 }
153 if (ctx->on_recv_channel != NULL) {
154 ret = ctx->on_recv_channel(new_channel);
155 if (ret == 0) {
156 consumer_add_channel(new_channel);
157 } else if (ret < 0) {
158 goto end_nosignal;
159 }
160 } else {
161 consumer_add_channel(new_channel);
162 }
163 goto end_nosignal;
164 }
165 case LTTNG_CONSUMER_ADD_STREAM:
166 {
167 struct lttng_consumer_stream *new_stream;
168 int fds[2];
169 size_t nb_fd = 2;
00e2e675 170 struct consumer_relayd_sock_pair *relayd = NULL;
3bd1e081 171
173af62f
DG
172 DBG("UST Consumer adding stream");
173
3bd1e081
MD
174 /* block */
175 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
3f8e211f 176 rcu_read_unlock();
3bd1e081
MD
177 return -EINTR;
178 }
179 ret = lttcomm_recv_fds_unix_sock(sock, fds, nb_fd);
180 if (ret != sizeof(fds)) {
f73fabfd 181 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD);
3f8e211f 182 rcu_read_unlock();
3bd1e081
MD
183 return ret;
184 }
185
173af62f
DG
186 DBG("consumer_add_stream chan %d stream %d",
187 msg.u.stream.channel_key,
188 msg.u.stream.stream_key);
189
d41f73b7 190 assert(msg.u.stream.output == LTTNG_EVENT_MMAP);
173af62f 191 new_stream = consumer_allocate_stream(msg.u.stream.channel_key,
3bd1e081
MD
192 msg.u.stream.stream_key,
193 fds[0], fds[1],
194 msg.u.stream.state,
195 msg.u.stream.mmap_len,
196 msg.u.stream.output,
6df2e2c9
MD
197 msg.u.stream.path_name,
198 msg.u.stream.uid,
00e2e675
DG
199 msg.u.stream.gid,
200 msg.u.stream.net_index,
201 msg.u.stream.metadata_flag);
3bd1e081 202 if (new_stream == NULL) {
f73fabfd 203 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
3f8e211f 204 goto end_nosignal;
3bd1e081 205 }
00e2e675
DG
206
207 /* The stream is not metadata. Get relayd reference if exists. */
208 relayd = consumer_find_relayd(msg.u.stream.net_index);
209 if (relayd != NULL) {
210 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
211 /* Add stream on the relayd */
212 ret = relayd_add_stream(&relayd->control_sock,
213 msg.u.stream.name, msg.u.stream.path_name,
214 &new_stream->relayd_stream_id);
215 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
216 if (ret < 0) {
3f8e211f 217 goto end_nosignal;
00e2e675
DG
218 }
219 } else if (msg.u.stream.net_index != -1) {
220 ERR("Network sequence index %d unknown. Not adding stream.",
221 msg.u.stream.net_index);
222 free(new_stream);
3f8e211f 223 goto end_nosignal;
00e2e675
DG
224 }
225
3bd1e081
MD
226 if (ctx->on_recv_stream != NULL) {
227 ret = ctx->on_recv_stream(new_stream);
228 if (ret == 0) {
229 consumer_add_stream(new_stream);
230 } else if (ret < 0) {
3f8e211f 231 goto end_nosignal;
3bd1e081
MD
232 }
233 } else {
234 consumer_add_stream(new_stream);
235 }
00e2e675 236
77c7c900 237 DBG("UST consumer_add_stream %s (%d,%d) with relayd id %" PRIu64,
00e2e675
DG
238 msg.u.stream.path_name, fds[0], fds[1],
239 new_stream->relayd_stream_id);
3bd1e081
MD
240 break;
241 }
173af62f
DG
242 case LTTNG_CONSUMER_DESTROY_RELAYD:
243 {
a6ba4fe1 244 uint64_t index = msg.u.destroy_relayd.net_seq_idx;
173af62f
DG
245 struct consumer_relayd_sock_pair *relayd;
246
a6ba4fe1 247 DBG("UST consumer destroying relayd %" PRIu64, index);
173af62f
DG
248
249 /* Get relayd reference if exists. */
a6ba4fe1 250 relayd = consumer_find_relayd(index);
173af62f 251 if (relayd == NULL) {
a6ba4fe1 252 ERR("Unable to find relayd %" PRIu64, index);
3f8e211f 253 goto end_nosignal;
173af62f
DG
254 }
255
a6ba4fe1
DG
256 /*
257 * Each relayd socket pair has a refcount of stream attached to it
258 * which tells if the relayd is still active or not depending on the
259 * refcount value.
260 *
261 * This will set the destroy flag of the relayd object and destroy it
262 * if the refcount reaches zero when called.
263 *
264 * The destroy can happen either here or when a stream fd hangs up.
265 */
266 consumer_flag_relayd_for_destroy(relayd);
173af62f 267
3f8e211f 268 goto end_nosignal;
173af62f 269 }
3bd1e081
MD
270 case LTTNG_CONSUMER_UPDATE_STREAM:
271 {
3f8e211f 272 rcu_read_unlock();
7ad0a0cb
MD
273 return -ENOSYS;
274#if 0
3bd1e081
MD
275 if (ctx->on_update_stream != NULL) {
276 ret = ctx->on_update_stream(msg.u.stream.stream_key, msg.u.stream.state);
277 if (ret == 0) {
278 consumer_change_stream_state(msg.u.stream.stream_key, msg.u.stream.state);
279 } else if (ret < 0) {
280 goto end;
281 }
282 } else {
283 consumer_change_stream_state(msg.u.stream.stream_key,
284 msg.u.stream.state);
285 }
286 break;
3f8e211f 287#endif
3bd1e081
MD
288 }
289 default:
290 break;
291 }
3f8e211f 292
04fdd819 293 /*
3f8e211f
DG
294 * Wake-up the other end by writing a null byte in the pipe (non-blocking).
295 * Important note: Because writing into the pipe is non-blocking (and
296 * therefore we allow dropping wakeup data, as long as there is wakeup data
297 * present in the pipe buffer to wake up the other end), the other end
298 * should perform the following sequence for waiting:
299 *
04fdd819
MD
300 * 1) empty the pipe (reads).
301 * 2) perform update operation.
302 * 3) wait on the pipe (poll).
303 */
304 do {
305 ret = write(ctx->consumer_poll_pipe[1], "", 1);
6f94560a 306 } while (ret < 0 && errno == EINTR);
3bd1e081 307end_nosignal:
7735ef9e 308 /* XXX: At some point we might want to return something else than zero */
b0b335c8 309 rcu_read_unlock();
3bd1e081
MD
310 return 0;
311}
312
313int lttng_ustconsumer_allocate_channel(struct lttng_consumer_channel *chan)
314{
13161846 315 struct lttng_ust_object_data obj;
3bd1e081
MD
316
317 obj.handle = -1;
318 obj.shm_fd = chan->shm_fd;
319 obj.wait_fd = chan->wait_fd;
320 obj.memory_map_size = chan->mmap_len;
321 chan->handle = ustctl_map_channel(&obj);
322 if (!chan->handle) {
323 return -ENOMEM;
324 }
b5c5fc29 325 chan->wait_fd_is_copy = 1;
2c1dd183 326 chan->shm_fd = -1;
b5c5fc29 327
3bd1e081
MD
328 return 0;
329}
330
d056b477
MD
331void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream *stream)
332{
333 ustctl_flush_buffer(stream->chan->handle, stream->buf, 0);
effcf122 334 stream->hangup_flush_done = 1;
d056b477
MD
335}
336
3bd1e081
MD
337void lttng_ustconsumer_del_channel(struct lttng_consumer_channel *chan)
338{
339 ustctl_unmap_channel(chan->handle);
340}
341
342int lttng_ustconsumer_allocate_stream(struct lttng_consumer_stream *stream)
343{
13161846 344 struct lttng_ust_object_data obj;
3bd1e081
MD
345 int ret;
346
347 obj.handle = -1;
348 obj.shm_fd = stream->shm_fd;
349 obj.wait_fd = stream->wait_fd;
350 obj.memory_map_size = stream->mmap_len;
351 ret = ustctl_add_stream(stream->chan->handle, &obj);
352 if (ret)
353 return ret;
354 stream->buf = ustctl_open_stream_read(stream->chan->handle, stream->cpu);
355 if (!stream->buf)
356 return -EBUSY;
2c1dd183
MD
357 /* ustctl_open_stream_read has closed the shm fd. */
358 stream->wait_fd_is_copy = 1;
359 stream->shm_fd = -1;
360
3bd1e081
MD
361 stream->mmap_base = ustctl_get_mmap_base(stream->chan->handle, stream->buf);
362 if (!stream->mmap_base) {
363 return -EINVAL;
364 }
ee77a7b0 365
3bd1e081
MD
366 return 0;
367}
368
369void lttng_ustconsumer_del_stream(struct lttng_consumer_stream *stream)
370{
371 ustctl_close_stream_read(stream->chan->handle, stream->buf);
372}
d41f73b7
MD
373
374
375int lttng_ustconsumer_read_subbuffer(struct lttng_consumer_stream *stream,
376 struct lttng_consumer_local_data *ctx)
377{
378 unsigned long len;
379 int err;
380 long ret = 0;
381 struct lttng_ust_shm_handle *handle;
382 struct lttng_ust_lib_ring_buffer *buf;
383 char dummy;
384 ssize_t readlen;
385
386 DBG("In read_subbuffer (wait_fd: %d, stream key: %d)",
387 stream->wait_fd, stream->key);
388
389 /* We can consume the 1 byte written into the wait_fd by UST */
effcf122
MD
390 if (!stream->hangup_flush_done) {
391 do {
392 readlen = read(stream->wait_fd, &dummy, 1);
87dc6a9c 393 } while (readlen == -1 && errno == EINTR);
effcf122
MD
394 if (readlen == -1) {
395 ret = readlen;
396 goto end;
397 }
d41f73b7
MD
398 }
399
400 buf = stream->buf;
401 handle = stream->chan->handle;
402 /* Get the next subbuffer */
403 err = ustctl_get_next_subbuf(handle, buf);
404 if (err != 0) {
effcf122 405 ret = -ret; /* ustctl_get_next_subbuf returns negative, caller expect positive. */
d41f73b7
MD
406 /*
407 * This is a debug message even for single-threaded consumer,
408 * because poll() have more relaxed criterions than get subbuf,
409 * so get_subbuf may fail for short race windows where poll()
410 * would issue wakeups.
411 */
412 DBG("Reserving sub buffer failed (everything is normal, "
413 "it is due to concurrency)");
414 goto end;
415 }
416 assert(stream->output == LTTNG_EVENT_MMAP);
417 /* read the used subbuffer size */
418 err = ustctl_get_padded_subbuf_size(handle, buf, &len);
effcf122 419 assert(err == 0);
d41f73b7
MD
420 /* write the subbuffer to the tracefile */
421 ret = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, len);
47e81c02 422 if (ret != len) {
d41f73b7
MD
423 /*
424 * display the error but continue processing to try
425 * to release the subbuffer
426 */
a4b92340 427 ERR("Error writing to tracefile (expected: %ld, got: %ld)", ret, len);
d41f73b7
MD
428 }
429 err = ustctl_put_next_subbuf(handle, buf);
effcf122 430 assert(err == 0);
d41f73b7
MD
431end:
432 return ret;
433}
434
435int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream *stream)
436{
437 int ret;
438
439 /* Opening the tracefile in write mode */
00e2e675 440 if (stream->path_name != NULL && stream->net_seq_idx == -1) {
e11d277b 441 ret = run_as_open(stream->path_name,
60b6c79c
MD
442 O_WRONLY|O_CREAT|O_TRUNC,
443 S_IRWXU|S_IRWXG|S_IRWXO,
444 stream->uid, stream->gid);
d41f73b7
MD
445 if (ret < 0) {
446 ERR("Opening %s", stream->path_name);
4c462e79 447 PERROR("open");
d41f73b7
MD
448 goto error;
449 }
450 stream->out_fd = ret;
451 }
452
453 /* we return 0 to let the library handle the FD internally */
454 return 0;
455
456error:
457 return ret;
458}
This page took 0.054146 seconds and 4 git commands to generate.