Replace explicit rcu_read_lock/unlock with lttng::urcu::read_lock_guard
[lttng-tools.git] / src / bin / lttng-sessiond / ust-consumer.cpp
CommitLineData
48842b30 1/*
21cf9b6b 2 * Copyright (C) 2011 EfficiOS Inc.
48842b30 3 *
ab5be9fa 4 * SPDX-License-Identifier: GPL-2.0-only
48842b30 5 *
48842b30
DG
6 */
7
6c1c0768 8#define _LGPL_SOURCE
28ab034a
JG
9#include "buffer-registry.hpp"
10#include "consumer.hpp"
11#include "health-sessiond.hpp"
12#include "lttng-sessiond.hpp"
13#include "lttng-ust-error.hpp"
14#include "session.hpp"
15#include "ust-consumer.hpp"
48842b30 16
c9e313bc 17#include <common/common.hpp>
28ab034a 18#include <common/compat/errno.hpp>
c9e313bc
SM
19#include <common/consumer/consumer.hpp>
20#include <common/defaults.hpp>
48842b30 21
28ab034a
JG
22#include <inttypes.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <unistd.h>
48842b30 27
d7bfb9b0
JG
28namespace lsu = lttng::sessiond::ust;
29
37278a1e 30/*
e9404c27 31 * Send a single channel to the consumer using command ASK_CHANNEL_CREATION.
ffe60014 32 *
7972aab2 33 * Consumer socket lock MUST be acquired before calling this.
37278a1e 34 */
ffe60014 35static int ask_channel_creation(struct ust_app_session *ua_sess,
28ab034a
JG
36 struct ust_app_channel *ua_chan,
37 struct consumer_output *consumer,
38 struct consumer_socket *socket,
39 lsu::registry_session *registry,
40 struct lttng_trace_chunk *trace_chunk)
37278a1e 41{
0c759fc9 42 int ret, output;
7972aab2
DG
43 uint32_t chan_id;
44 uint64_t key, chan_reg_key;
cd9adb8b 45 char *pathname = nullptr;
37278a1e 46 struct lttcomm_consumer_msg msg;
d7ba1388 47 char shm_path[PATH_MAX] = "";
3d071855 48 char root_shm_path[PATH_MAX] = "";
d2956687 49 bool is_local_trace;
5da88b0f 50 size_t consumer_path_offset = 0;
37278a1e 51
a0377dfe
FD
52 LTTNG_ASSERT(ua_sess);
53 LTTNG_ASSERT(ua_chan);
54 LTTNG_ASSERT(socket);
55 LTTNG_ASSERT(consumer);
56 LTTNG_ASSERT(registry);
ffe60014
DG
57
58 DBG2("Asking UST consumer for channel");
59
d2956687
JG
60 is_local_trace = consumer->net_seq_index == -1ULL;
61 /* Format the channel's path (relative to the current trace chunk). */
28ab034a 62 pathname = setup_channel_trace_path(consumer, ua_sess->path, &consumer_path_offset);
d2956687
JG
63 if (!pathname) {
64 ret = -1;
65 goto error;
66 }
67
68 if (is_local_trace && trace_chunk) {
69 enum lttng_trace_chunk_status chunk_status;
70 char *pathname_index;
71
28ab034a 72 ret = asprintf(&pathname_index, "%s/" DEFAULT_INDEX_DIR, pathname);
d2956687
JG
73 if (ret < 0) {
74 ERR("Failed to format channel index directory");
75 ret = -1;
76 goto error;
77 }
78
79 /*
80 * Create the index subdirectory which will take care
81 * of implicitly creating the channel's path.
82 */
28ab034a 83 chunk_status = lttng_trace_chunk_create_subdirectory(trace_chunk, pathname_index);
d2956687
JG
84 free(pathname_index);
85 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
10a50311
JD
86 ret = -1;
87 goto error;
88 }
ffe60014
DG
89 }
90
7972aab2
DG
91 /* Depending on the buffer type, a different channel key is used. */
92 if (ua_sess->buffer_type == LTTNG_BUFFER_PER_UID) {
93 chan_reg_key = ua_chan->tracing_channel_id;
94 } else {
95 chan_reg_key = ua_chan->key;
96 }
97
fc4b93fa 98 if (ua_chan->attr.type == LTTNG_UST_ABI_CHAN_METADATA) {
7972aab2 99 chan_id = -1U;
d7ba1388
MD
100 /*
101 * Metadata channels shm_path (buffers) are handled within
102 * session daemon. Consumer daemon should not try to create
103 * those buffer files.
104 */
7972aab2 105 } else {
d7bfb9b0
JG
106 {
107 auto locked_registry = registry->lock();
4bcf2294 108 auto& ust_reg_chan = registry->channel(chan_reg_key);
d7bfb9b0
JG
109
110 chan_id = ust_reg_chan.id;
111 }
112
d7ba1388
MD
113 if (ua_sess->shm_path[0]) {
114 strncpy(shm_path, ua_sess->shm_path, sizeof(shm_path));
115 shm_path[sizeof(shm_path) - 1] = '\0';
28ab034a
JG
116 strncat(shm_path, "/", sizeof(shm_path) - strlen(shm_path) - 1);
117 strncat(shm_path, ua_chan->name, sizeof(shm_path) - strlen(shm_path) - 1);
118 strncat(shm_path, "_", sizeof(shm_path) - strlen(shm_path) - 1);
d7ba1388 119 }
3d071855
MD
120 strncpy(root_shm_path, ua_sess->root_shm_path, sizeof(root_shm_path));
121 root_shm_path[sizeof(root_shm_path) - 1] = '\0';
7972aab2
DG
122 }
123
0c759fc9 124 switch (ua_chan->attr.output) {
fc4b93fa 125 case LTTNG_UST_ABI_MMAP:
0c759fc9
DG
126 default:
127 output = LTTNG_EVENT_MMAP;
128 break;
129 }
130
ffe60014 131 consumer_init_ask_channel_comm_msg(&msg,
28ab034a
JG
132 ua_chan->attr.subbuf_size,
133 ua_chan->attr.num_subbuf,
134 ua_chan->attr.overwrite,
135 ua_chan->attr.switch_timer_interval,
136 ua_chan->attr.read_timer_interval,
137 ua_sess->live_timer_interval,
138 ua_sess->live_timer_interval != 0,
139 ua_chan->monitor_timer_interval,
140 output,
141 (int) ua_chan->attr.type,
142 ua_sess->tracing_id,
143 &pathname[consumer_path_offset],
144 ua_chan->name,
145 consumer->net_seq_index,
146 ua_chan->key,
147 registry->uuid,
148 chan_id,
149 ua_chan->tracefile_size,
150 ua_chan->tracefile_count,
151 ua_sess->id,
152 ua_sess->output_traces,
153 lttng_credentials_get_uid(&ua_sess->real_credentials),
154 ua_chan->attr.blocking_timeout,
155 root_shm_path,
156 shm_path,
157 trace_chunk,
158 &ua_sess->effective_credentials);
37278a1e 159
840cb59c 160 health_code_update();
ca03de58 161
52898cb1 162 ret = consumer_socket_send(socket, &msg, sizeof(msg));
37278a1e
DG
163 if (ret < 0) {
164 goto error;
165 }
166
28ab034a 167 ret = consumer_recv_status_channel(socket, &key, &ua_chan->expected_stream_count);
ffe60014
DG
168 if (ret < 0) {
169 goto error;
170 }
171 /* Communication protocol error. */
a0377dfe 172 LTTNG_ASSERT(key == ua_chan->key);
ffe60014 173 /* We need at least one where 1 stream for 1 cpu. */
10a50311 174 if (ua_sess->output_traces) {
a0377dfe 175 LTTNG_ASSERT(ua_chan->expected_stream_count > 0);
10a50311 176 }
ffe60014 177
28ab034a
JG
178 DBG2("UST ask channel %" PRIu64 " successfully done with %u stream(s)",
179 key,
180 ua_chan->expected_stream_count);
ca03de58 181
37278a1e 182error:
ffe60014
DG
183 free(pathname);
184 health_code_update();
37278a1e
DG
185 return ret;
186}
187
188/*
ffe60014
DG
189 * Ask consumer to create a channel for a given session.
190 *
e9404c27
JG
191 * Session list and rcu read side locks must be held by the caller.
192 *
ffe60014 193 * Returns 0 on success else a negative value.
37278a1e 194 */
ffe60014 195int ust_consumer_ask_channel(struct ust_app_session *ua_sess,
28ab034a
JG
196 struct ust_app_channel *ua_chan,
197 struct consumer_output *consumer,
198 struct consumer_socket *socket,
199 lsu::registry_session *registry,
200 struct lttng_trace_chunk *trace_chunk)
37278a1e
DG
201{
202 int ret;
37278a1e 203
a0377dfe
FD
204 LTTNG_ASSERT(ua_sess);
205 LTTNG_ASSERT(ua_chan);
206 LTTNG_ASSERT(consumer);
207 LTTNG_ASSERT(socket);
208 LTTNG_ASSERT(registry);
f50f23d9 209
d9078d0c
DG
210 if (!consumer->enabled) {
211 ret = -LTTNG_ERR_NO_CONSUMER;
212 DBG3("Consumer is disabled");
213 goto error;
214 }
215
ffe60014 216 pthread_mutex_lock(socket->lock);
28ab034a 217 ret = ask_channel_creation(ua_sess, ua_chan, consumer, socket, registry, trace_chunk);
2898de39 218 pthread_mutex_unlock(socket->lock);
37278a1e 219 if (ret < 0) {
e9404c27 220 ERR("ask_channel_creation consumer command failed");
37278a1e
DG
221 goto error;
222 }
223
48842b30
DG
224error:
225 return ret;
226}
227
228/*
ffe60014
DG
229 * Send a get channel command to consumer using the given channel key. The
230 * channel object is populated and the stream list.
231 *
232 * Return 0 on success else a negative value.
48842b30 233 */
28ab034a 234int ust_consumer_get_channel(struct consumer_socket *socket, struct ust_app_channel *ua_chan)
48842b30 235{
ffe60014 236 int ret;
37278a1e 237 struct lttcomm_consumer_msg msg;
48842b30 238
a0377dfe
FD
239 LTTNG_ASSERT(ua_chan);
240 LTTNG_ASSERT(socket);
48842b30 241
53efb85a 242 memset(&msg, 0, sizeof(msg));
ffe60014
DG
243 msg.cmd_type = LTTNG_CONSUMER_GET_CHANNEL;
244 msg.u.get_channel.key = ua_chan->key;
37278a1e 245
ffe60014 246 pthread_mutex_lock(socket->lock);
840cb59c 247 health_code_update();
ca03de58 248
ffe60014
DG
249 /* Send command and wait for OK reply. */
250 ret = consumer_send_msg(socket, &msg);
37278a1e
DG
251 if (ret < 0) {
252 goto error;
253 }
254
ffe60014 255 /* First, get the channel from consumer. */
b623cb6a 256 ret = lttng_ust_ctl_recv_channel_from_consumer(*socket->fd_ptr, &ua_chan->obj);
37278a1e 257 if (ret < 0) {
ffe60014
DG
258 if (ret != -EPIPE) {
259 ERR("Error recv channel from consumer %d with ret %d",
28ab034a
JG
260 *socket->fd_ptr,
261 ret);
ffe60014
DG
262 } else {
263 DBG3("UST app recv channel from consumer. Consumer is dead.");
264 }
37278a1e
DG
265 goto error;
266 }
00e2e675 267
ffe60014 268 /* Next, get all streams. */
cd9adb8b 269 while (true) {
ffe60014 270 struct ust_app_stream *stream;
ca03de58 271
ffe60014
DG
272 /* Create UST stream */
273 stream = ust_app_alloc_stream();
cd9adb8b 274 if (stream == nullptr) {
ffe60014 275 ret = -ENOMEM;
48842b30
DG
276 goto error;
277 }
278
ffe60014 279 /* Stream object is populated by this call if successful. */
b623cb6a 280 ret = lttng_ust_ctl_recv_stream_from_consumer(*socket->fd_ptr, &stream->obj);
37278a1e 281 if (ret < 0) {
ffe60014
DG
282 free(stream);
283 if (ret == -LTTNG_UST_ERR_NOENT) {
284 DBG3("UST app consumer has no more stream available");
ffe60014
DG
285 break;
286 }
287 if (ret != -EPIPE) {
288 ERR("Recv stream from consumer %d with ret %d",
28ab034a
JG
289 *socket->fd_ptr,
290 ret);
ffe60014
DG
291 } else {
292 DBG3("UST app recv stream from consumer. Consumer is dead.");
00e2e675 293 }
48842b30
DG
294 goto error;
295 }
37278a1e 296
ffe60014
DG
297 /* Order is important this is why a list is used. */
298 cds_list_add_tail(&stream->list, &ua_chan->streams.head);
299 ua_chan->streams.count++;
37278a1e 300
5368d366 301 DBG2("UST app stream %d received successfully", ua_chan->streams.count);
ffe60014
DG
302 }
303
304 /* This MUST match or else we have a synchronization problem. */
a0377dfe 305 LTTNG_ASSERT(ua_chan->expected_stream_count == ua_chan->streams.count);
ca03de58 306
ffe60014
DG
307 /* Wait for confirmation that we can proceed with the streams. */
308 ret = consumer_recv_status_reply(socket);
37278a1e
DG
309 if (ret < 0) {
310 goto error;
311 }
312
313error:
ffe60014
DG
314 health_code_update();
315 pthread_mutex_unlock(socket->lock);
37278a1e
DG
316 return ret;
317}
318
319/*
ffe60014
DG
320 * Send a destroy channel command to consumer using the given channel key.
321 *
322 * Note that this command MUST be used prior to a successful
323 * LTTNG_CONSUMER_GET_CHANNEL because once this command is done successfully,
324 * the streams are dispatched to the consumer threads and MUST be teardown
325 * through the hang up process.
326 *
327 * Return 0 on success else a negative value.
37278a1e 328 */
28ab034a 329int ust_consumer_destroy_channel(struct consumer_socket *socket, struct ust_app_channel *ua_chan)
37278a1e 330{
ffe60014
DG
331 int ret;
332 struct lttcomm_consumer_msg msg;
a4b92340 333
a0377dfe
FD
334 LTTNG_ASSERT(ua_chan);
335 LTTNG_ASSERT(socket);
37278a1e 336
53efb85a 337 memset(&msg, 0, sizeof(msg));
ffe60014
DG
338 msg.cmd_type = LTTNG_CONSUMER_DESTROY_CHANNEL;
339 msg.u.destroy_channel.key = ua_chan->key;
173af62f 340
ffe60014
DG
341 pthread_mutex_lock(socket->lock);
342 health_code_update();
37278a1e 343
ffe60014 344 ret = consumer_send_msg(socket, &msg);
37278a1e
DG
345 if (ret < 0) {
346 goto error;
48842b30
DG
347 }
348
ffe60014
DG
349error:
350 health_code_update();
351 pthread_mutex_unlock(socket->lock);
352 return ret;
353}
aeb96892 354
ffe60014
DG
355/*
356 * Send a given stream to UST tracer.
357 *
358 * On success return 0 else a negative value.
359 */
360int ust_consumer_send_stream_to_ust(struct ust_app *app,
28ab034a
JG
361 struct ust_app_channel *channel,
362 struct ust_app_stream *stream)
ffe60014
DG
363{
364 int ret;
365
a0377dfe
FD
366 LTTNG_ASSERT(app);
367 LTTNG_ASSERT(stream);
368 LTTNG_ASSERT(channel);
ffe60014
DG
369
370 DBG2("UST consumer send stream to app %d", app->sock);
371
372 /* Relay stream to application. */
fb45065e 373 pthread_mutex_lock(&app->sock_lock);
b623cb6a 374 ret = lttng_ust_ctl_send_stream_to_ust(app->sock, channel->obj, stream->obj);
fb45065e 375 pthread_mutex_unlock(&app->sock_lock);
ffe60014 376 if (ret < 0) {
be355079
JR
377 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
378 DBG3("UST app send stream to ust failed. Application is dead. (pid: %d, sock: %d).",
28ab034a
JG
379 app->pid,
380 app->sock);
be355079
JR
381 } else if (ret == -EAGAIN) {
382 WARN("UST app send stream to ust failed. Communication time out (pid: %d, sock: %d).",
28ab034a
JG
383 app->pid,
384 app->sock);
ffe60014 385 } else {
be355079 386 ERR("UST app send stream, handle %d, to ust failed with ret %d (pid: %d, sock: %d).",
28ab034a
JG
387 stream->obj->handle,
388 ret,
389 app->pid,
390 app->sock);
48842b30 391 }
ffe60014 392 goto error;
48842b30 393 }
d0b96690 394 channel->handle = channel->obj->handle;
48842b30 395
ffe60014
DG
396error:
397 return ret;
398}
399
400/*
401 * Send channel previously received from the consumer to the UST tracer.
402 *
403 * On success return 0 else a negative value.
404 */
405int ust_consumer_send_channel_to_ust(struct ust_app *app,
28ab034a
JG
406 struct ust_app_session *ua_sess,
407 struct ust_app_channel *channel)
ffe60014
DG
408{
409 int ret;
410
a0377dfe
FD
411 LTTNG_ASSERT(app);
412 LTTNG_ASSERT(ua_sess);
413 LTTNG_ASSERT(channel);
414 LTTNG_ASSERT(channel->obj);
ffe60014 415
7972aab2 416 DBG2("UST app send channel to sock %d pid %d (name: %s, key: %" PRIu64 ")",
28ab034a
JG
417 app->sock,
418 app->pid,
419 channel->name,
420 channel->tracing_channel_id);
48842b30 421
ffe60014 422 /* Send stream to application. */
fb45065e 423 pthread_mutex_lock(&app->sock_lock);
b623cb6a 424 ret = lttng_ust_ctl_send_channel_to_ust(app->sock, ua_sess->handle, channel->obj);
fb45065e 425 pthread_mutex_unlock(&app->sock_lock);
ffe60014 426 if (ret < 0) {
be355079
JR
427 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
428 DBG3("UST app send channel to ust failed. Application is dead (pid: %d, sock: %d).",
28ab034a
JG
429 app->pid,
430 app->sock);
be355079
JR
431 } else if (ret == -EAGAIN) {
432 WARN("UST app send channel to ust failed. Communication timeout (pid: %d, sock: %d).",
28ab034a
JG
433 app->pid,
434 app->sock);
ffe60014 435 } else {
be355079 436 ERR("UST app send channel %s, to ust failed with ret %d (pid: %d, sock: %d).",
28ab034a
JG
437 channel->name,
438 ret,
439 app->pid,
440 app->sock);
ffe60014
DG
441 }
442 goto error;
443 }
48842b30
DG
444
445error:
446 return ret;
447}
331744e3
JD
448
449/*
450 * Handle the metadata requests from the UST consumer
451 *
452 * Return 0 on success else a negative value.
453 */
454int ust_consumer_metadata_request(struct consumer_socket *socket)
455{
456 int ret;
457 ssize_t ret_push;
458 struct lttcomm_metadata_request_msg request;
459 struct buffer_reg_uid *reg_uid;
b0f2e8db 460 lsu::registry_session *ust_reg;
331744e3
JD
461 struct lttcomm_consumer_msg msg;
462
a0377dfe 463 LTTNG_ASSERT(socket);
331744e3 464
56047f5a 465 lttng::urcu::read_lock_guard read_lock;
331744e3
JD
466 health_code_update();
467
468 /* Wait for a metadata request */
dc2bbdae 469 pthread_mutex_lock(socket->lock);
52898cb1 470 ret = consumer_socket_recv(socket, &request, sizeof(request));
dc2bbdae 471 pthread_mutex_unlock(socket->lock);
52898cb1 472 if (ret < 0) {
331744e3
JD
473 goto end;
474 }
475
1950109e 476 DBG("Metadata request received for session %" PRIu64 ", key %" PRIu64,
28ab034a
JG
477 request.session_id,
478 request.key);
331744e3 479
28ab034a 480 reg_uid = buffer_reg_uid_find(request.session_id, request.bits_per_long, request.uid);
331744e3
JD
481 if (reg_uid) {
482 ust_reg = reg_uid->registry->reg.ust;
483 } else {
28ab034a 484 struct buffer_reg_pid *reg_pid = buffer_reg_pid_find(request.session_id_per_pid);
331744e3 485 if (!reg_pid) {
1950109e 486 DBG("PID registry not found for session id %" PRIu64,
28ab034a 487 request.session_id_per_pid);
331744e3 488
53efb85a 489 memset(&msg, 0, sizeof(msg));
331744e3 490 msg.cmd_type = LTTNG_ERR_UND;
cb7d882c 491 pthread_mutex_lock(socket->lock);
331744e3 492 (void) consumer_send_msg(socket, &msg);
cb7d882c 493 pthread_mutex_unlock(socket->lock);
331744e3
JD
494 /*
495 * This is possible since the session might have been destroyed
496 * during a consumer metadata request. So here, return gracefully
497 * because the destroy session will push the remaining metadata to
498 * the consumer.
499 */
500 ret = 0;
501 goto end;
502 }
503 ust_reg = reg_pid->registry->reg.ust;
504 }
a0377dfe 505 LTTNG_ASSERT(ust_reg);
331744e3 506
d7bfb9b0
JG
507 {
508 auto locked_ust_reg = ust_reg->lock();
509 ret_push = ust_app_push_metadata(locked_ust_reg, socket, 1);
510 }
2c57e06d
MD
511 if (ret_push == -EPIPE) {
512 DBG("Application or relay closed while pushing metadata");
513 } else if (ret_push < 0) {
331744e3
JD
514 ERR("Pushing metadata");
515 ret = -1;
516 goto end;
2c57e06d
MD
517 } else {
518 DBG("UST Consumer metadata pushed successfully");
331744e3 519 }
331744e3
JD
520 ret = 0;
521
522end:
331744e3
JD
523 return ret;
524}
This page took 0.105748 seconds and 4 git commands to generate.