2 * Copyright (C) 2011 EfficiOS Inc.
4 * SPDX-License-Identifier: GPL-2.0-only
15 #include <common/compat/errno.hpp>
16 #include <common/common.hpp>
17 #include <common/consumer/consumer.hpp>
18 #include <common/defaults.hpp>
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"
28 namespace lsu
= lttng::sessiond::ust
;
31 * Send a single channel to the consumer using command ASK_CHANNEL_CREATION.
33 * Consumer socket lock MUST be acquired before calling this.
35 static int ask_channel_creation(struct ust_app_session
*ua_sess
,
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
)
44 uint64_t key
, chan_reg_key
;
45 char *pathname
= NULL
;
46 struct lttcomm_consumer_msg msg
;
47 char shm_path
[PATH_MAX
] = "";
48 char root_shm_path
[PATH_MAX
] = "";
50 size_t consumer_path_offset
= 0;
52 LTTNG_ASSERT(ua_sess
);
53 LTTNG_ASSERT(ua_chan
);
55 LTTNG_ASSERT(consumer
);
56 LTTNG_ASSERT(registry
);
58 DBG2("Asking UST consumer for channel");
60 is_local_trace
= consumer
->net_seq_index
== -1ULL;
61 /* Format the channel's path (relative to the current trace chunk). */
62 pathname
= setup_channel_trace_path(consumer
, ua_sess
->path
,
63 &consumer_path_offset
);
69 if (is_local_trace
&& trace_chunk
) {
70 enum lttng_trace_chunk_status chunk_status
;
73 ret
= asprintf(&pathname_index
, "%s/" DEFAULT_INDEX_DIR
,
76 ERR("Failed to format channel index directory");
82 * Create the index subdirectory which will take care
83 * of implicitly creating the channel's path.
85 chunk_status
= lttng_trace_chunk_create_subdirectory(
86 trace_chunk
, pathname_index
);
88 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
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
;
98 chan_reg_key
= ua_chan
->key
;
101 if (ua_chan
->attr
.type
== LTTNG_UST_ABI_CHAN_METADATA
) {
104 * Metadata channels shm_path (buffers) are handled within
105 * session daemon. Consumer daemon should not try to create
106 * those buffer files.
110 auto locked_registry
= registry
->lock();
111 auto& ust_reg_chan
= registry
->get_channel(chan_reg_key
);
113 chan_id
= ust_reg_chan
.id
;
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);
126 strncpy(root_shm_path
, ua_sess
->root_shm_path
, sizeof(root_shm_path
));
127 root_shm_path
[sizeof(root_shm_path
) - 1] = '\0';
130 switch (ua_chan
->attr
.output
) {
131 case LTTNG_UST_ABI_MMAP
:
133 output
= LTTNG_EVENT_MMAP
;
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
,
143 ua_sess
->live_timer_interval
,
144 ua_sess
->live_timer_interval
!= 0,
145 ua_chan
->monitor_timer_interval
,
147 (int) ua_chan
->attr
.type
,
149 &pathname
[consumer_path_offset
],
151 consumer
->net_seq_index
,
155 ua_chan
->tracefile_size
,
156 ua_chan
->tracefile_count
,
158 ua_sess
->output_traces
,
159 lttng_credentials_get_uid(&ua_sess
->real_credentials
),
160 ua_chan
->attr
.blocking_timeout
,
161 root_shm_path
, shm_path
,
163 &ua_sess
->effective_credentials
);
165 health_code_update();
167 ret
= consumer_socket_send(socket
, &msg
, sizeof(msg
));
172 ret
= consumer_recv_status_channel(socket
, &key
,
173 &ua_chan
->expected_stream_count
);
177 /* Communication protocol error. */
178 LTTNG_ASSERT(key
== ua_chan
->key
);
179 /* We need at least one where 1 stream for 1 cpu. */
180 if (ua_sess
->output_traces
) {
181 LTTNG_ASSERT(ua_chan
->expected_stream_count
> 0);
184 DBG2("UST ask channel %" PRIu64
" successfully done with %u stream(s)", key
,
185 ua_chan
->expected_stream_count
);
189 health_code_update();
194 * Ask consumer to create a channel for a given session.
196 * Session list and rcu read side locks must be held by the caller.
198 * Returns 0 on success else a negative value.
200 int ust_consumer_ask_channel(struct ust_app_session
*ua_sess
,
201 struct ust_app_channel
*ua_chan
,
202 struct consumer_output
*consumer
,
203 struct consumer_socket
*socket
,
204 lsu::registry_session
*registry
,
205 struct lttng_trace_chunk
* trace_chunk
)
209 LTTNG_ASSERT(ua_sess
);
210 LTTNG_ASSERT(ua_chan
);
211 LTTNG_ASSERT(consumer
);
212 LTTNG_ASSERT(socket
);
213 LTTNG_ASSERT(registry
);
215 if (!consumer
->enabled
) {
216 ret
= -LTTNG_ERR_NO_CONSUMER
;
217 DBG3("Consumer is disabled");
221 pthread_mutex_lock(socket
->lock
);
222 ret
= ask_channel_creation(ua_sess
, ua_chan
, consumer
, socket
, registry
,
224 pthread_mutex_unlock(socket
->lock
);
226 ERR("ask_channel_creation consumer command failed");
235 * Send a get channel command to consumer using the given channel key. The
236 * channel object is populated and the stream list.
238 * Return 0 on success else a negative value.
240 int ust_consumer_get_channel(struct consumer_socket
*socket
,
241 struct ust_app_channel
*ua_chan
)
244 struct lttcomm_consumer_msg msg
;
246 LTTNG_ASSERT(ua_chan
);
247 LTTNG_ASSERT(socket
);
249 memset(&msg
, 0, sizeof(msg
));
250 msg
.cmd_type
= LTTNG_CONSUMER_GET_CHANNEL
;
251 msg
.u
.get_channel
.key
= ua_chan
->key
;
253 pthread_mutex_lock(socket
->lock
);
254 health_code_update();
256 /* Send command and wait for OK reply. */
257 ret
= consumer_send_msg(socket
, &msg
);
262 /* First, get the channel from consumer. */
263 ret
= lttng_ust_ctl_recv_channel_from_consumer(*socket
->fd_ptr
, &ua_chan
->obj
);
266 ERR("Error recv channel from consumer %d with ret %d",
267 *socket
->fd_ptr
, ret
);
269 DBG3("UST app recv channel from consumer. Consumer is dead.");
274 /* Next, get all streams. */
276 struct ust_app_stream
*stream
;
278 /* Create UST stream */
279 stream
= ust_app_alloc_stream();
280 if (stream
== NULL
) {
285 /* Stream object is populated by this call if successful. */
286 ret
= lttng_ust_ctl_recv_stream_from_consumer(*socket
->fd_ptr
, &stream
->obj
);
289 if (ret
== -LTTNG_UST_ERR_NOENT
) {
290 DBG3("UST app consumer has no more stream available");
294 ERR("Recv stream from consumer %d with ret %d",
295 *socket
->fd_ptr
, ret
);
297 DBG3("UST app recv stream from consumer. Consumer is dead.");
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
++;
306 DBG2("UST app stream %d received successfully", ua_chan
->streams
.count
);
309 /* This MUST match or else we have a synchronization problem. */
310 LTTNG_ASSERT(ua_chan
->expected_stream_count
== ua_chan
->streams
.count
);
312 /* Wait for confirmation that we can proceed with the streams. */
313 ret
= consumer_recv_status_reply(socket
);
319 health_code_update();
320 pthread_mutex_unlock(socket
->lock
);
325 * Send a destroy channel command to consumer using the given channel key.
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.
332 * Return 0 on success else a negative value.
334 int ust_consumer_destroy_channel(struct consumer_socket
*socket
,
335 struct ust_app_channel
*ua_chan
)
338 struct lttcomm_consumer_msg msg
;
340 LTTNG_ASSERT(ua_chan
);
341 LTTNG_ASSERT(socket
);
343 memset(&msg
, 0, sizeof(msg
));
344 msg
.cmd_type
= LTTNG_CONSUMER_DESTROY_CHANNEL
;
345 msg
.u
.destroy_channel
.key
= ua_chan
->key
;
347 pthread_mutex_lock(socket
->lock
);
348 health_code_update();
350 ret
= consumer_send_msg(socket
, &msg
);
356 health_code_update();
357 pthread_mutex_unlock(socket
->lock
);
362 * Send a given stream to UST tracer.
364 * On success return 0 else a negative value.
366 int ust_consumer_send_stream_to_ust(struct ust_app
*app
,
367 struct ust_app_channel
*channel
, struct ust_app_stream
*stream
)
372 LTTNG_ASSERT(stream
);
373 LTTNG_ASSERT(channel
);
375 DBG2("UST consumer send stream to app %d", app
->sock
);
377 /* Relay stream to application. */
378 pthread_mutex_lock(&app
->sock_lock
);
379 ret
= lttng_ust_ctl_send_stream_to_ust(app
->sock
, channel
->obj
, stream
->obj
);
380 pthread_mutex_unlock(&app
->sock_lock
);
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
);
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
,
395 channel
->handle
= channel
->obj
->handle
;
402 * Send channel previously received from the consumer to the UST tracer.
404 * On success return 0 else a negative value.
406 int ust_consumer_send_channel_to_ust(struct ust_app
*app
,
407 struct ust_app_session
*ua_sess
, struct ust_app_channel
*channel
)
412 LTTNG_ASSERT(ua_sess
);
413 LTTNG_ASSERT(channel
);
414 LTTNG_ASSERT(channel
->obj
);
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
);
419 /* Send stream to application. */
420 pthread_mutex_lock(&app
->sock_lock
);
421 ret
= lttng_ust_ctl_send_channel_to_ust(app
->sock
, ua_sess
->handle
, channel
->obj
);
422 pthread_mutex_unlock(&app
->sock_lock
);
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
);
431 ERR("UST app send channel %s, to ust failed with ret %d (pid: %d, sock: %d).",
432 channel
->name
, ret
, app
->pid
,
443 * Handle the metadata requests from the UST consumer
445 * Return 0 on success else a negative value.
447 int ust_consumer_metadata_request(struct consumer_socket
*socket
)
451 struct lttcomm_metadata_request_msg request
;
452 struct buffer_reg_uid
*reg_uid
;
453 lsu::registry_session
*ust_reg
;
454 struct lttcomm_consumer_msg msg
;
456 LTTNG_ASSERT(socket
);
459 health_code_update();
461 /* Wait for a metadata request */
462 pthread_mutex_lock(socket
->lock
);
463 ret
= consumer_socket_recv(socket
, &request
, sizeof(request
));
464 pthread_mutex_unlock(socket
->lock
);
469 DBG("Metadata request received for session %" PRIu64
", key %" PRIu64
,
470 request
.session_id
, request
.key
);
472 reg_uid
= buffer_reg_uid_find(request
.session_id
,
473 request
.bits_per_long
, request
.uid
);
475 ust_reg
= reg_uid
->registry
->reg
.ust
;
477 struct buffer_reg_pid
*reg_pid
=
478 buffer_reg_pid_find(request
.session_id_per_pid
);
480 DBG("PID registry not found for session id %" PRIu64
,
481 request
.session_id_per_pid
);
483 memset(&msg
, 0, sizeof(msg
));
484 msg
.cmd_type
= LTTNG_ERR_UND
;
485 pthread_mutex_lock(socket
->lock
);
486 (void) consumer_send_msg(socket
, &msg
);
487 pthread_mutex_unlock(socket
->lock
);
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
497 ust_reg
= reg_pid
->registry
->reg
.ust
;
499 LTTNG_ASSERT(ust_reg
);
502 auto locked_ust_reg
= ust_reg
->lock();
503 ret_push
= ust_app_push_metadata(locked_ust_reg
, socket
, 1);
505 if (ret_push
== -EPIPE
) {
506 DBG("Application or relay closed while pushing metadata");
507 } else if (ret_push
< 0) {
508 ERR("Pushing metadata");
512 DBG("UST Consumer metadata pushed successfully");