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