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