Build fix: unknown warning -Wduplicated-branches
[lttng-tools.git] / src / bin / lttng-sessiond / ust-consumer.cpp
1 /*
2 * Copyright (C) 2011 EfficiOS Inc.
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #define _LGPL_SOURCE
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <unistd.h>
13 #include <inttypes.h>
14
15 #include <common/compat/errno.hpp>
16 #include <common/common.hpp>
17 #include <common/consumer/consumer.hpp>
18 #include <common/defaults.hpp>
19
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"
27
28 namespace lsu = lttng::sessiond::ust;
29
30 /*
31 * Send a single channel to the consumer using command ASK_CHANNEL_CREATION.
32 *
33 * Consumer socket lock MUST be acquired before calling this.
34 */
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)
41 {
42 int ret, output;
43 uint32_t chan_id;
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] = "";
49 bool is_local_trace;
50 size_t consumer_path_offset = 0;
51
52 LTTNG_ASSERT(ua_sess);
53 LTTNG_ASSERT(ua_chan);
54 LTTNG_ASSERT(socket);
55 LTTNG_ASSERT(consumer);
56 LTTNG_ASSERT(registry);
57
58 DBG2("Asking UST consumer for channel");
59
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);
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) {
89 ret = -1;
90 goto error;
91 }
92 }
93
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
101 if (ua_chan->attr.type == LTTNG_UST_ABI_CHAN_METADATA) {
102 chan_id = -1U;
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 */
108 } else {
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
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 }
126 strncpy(root_shm_path, ua_sess->root_shm_path, sizeof(root_shm_path));
127 root_shm_path[sizeof(root_shm_path) - 1] = '\0';
128 }
129
130 switch (ua_chan->attr.output) {
131 case LTTNG_UST_ABI_MMAP:
132 default:
133 output = LTTNG_EVENT_MMAP;
134 break;
135 }
136
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,
146 output,
147 (int) ua_chan->attr.type,
148 ua_sess->tracing_id,
149 &pathname[consumer_path_offset],
150 ua_chan->name,
151 consumer->net_seq_index,
152 ua_chan->key,
153 registry->uuid,
154 chan_id,
155 ua_chan->tracefile_size,
156 ua_chan->tracefile_count,
157 ua_sess->id,
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,
162 trace_chunk,
163 &ua_sess->effective_credentials);
164
165 health_code_update();
166
167 ret = consumer_socket_send(socket, &msg, sizeof(msg));
168 if (ret < 0) {
169 goto error;
170 }
171
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. */
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);
182 }
183
184 DBG2("UST ask channel %" PRIu64 " successfully done with %u stream(s)", key,
185 ua_chan->expected_stream_count);
186
187 error:
188 free(pathname);
189 health_code_update();
190 return ret;
191 }
192
193 /*
194 * Ask consumer to create a channel for a given session.
195 *
196 * Session list and rcu read side locks must be held by the caller.
197 *
198 * Returns 0 on success else a negative value.
199 */
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)
206 {
207 int ret;
208
209 LTTNG_ASSERT(ua_sess);
210 LTTNG_ASSERT(ua_chan);
211 LTTNG_ASSERT(consumer);
212 LTTNG_ASSERT(socket);
213 LTTNG_ASSERT(registry);
214
215 if (!consumer->enabled) {
216 ret = -LTTNG_ERR_NO_CONSUMER;
217 DBG3("Consumer is disabled");
218 goto error;
219 }
220
221 pthread_mutex_lock(socket->lock);
222 ret = ask_channel_creation(ua_sess, ua_chan, consumer, socket, registry,
223 trace_chunk);
224 pthread_mutex_unlock(socket->lock);
225 if (ret < 0) {
226 ERR("ask_channel_creation consumer command failed");
227 goto error;
228 }
229
230 error:
231 return ret;
232 }
233
234 /*
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.
239 */
240 int ust_consumer_get_channel(struct consumer_socket *socket,
241 struct ust_app_channel *ua_chan)
242 {
243 int ret;
244 struct lttcomm_consumer_msg msg;
245
246 LTTNG_ASSERT(ua_chan);
247 LTTNG_ASSERT(socket);
248
249 memset(&msg, 0, sizeof(msg));
250 msg.cmd_type = LTTNG_CONSUMER_GET_CHANNEL;
251 msg.u.get_channel.key = ua_chan->key;
252
253 pthread_mutex_lock(socket->lock);
254 health_code_update();
255
256 /* Send command and wait for OK reply. */
257 ret = consumer_send_msg(socket, &msg);
258 if (ret < 0) {
259 goto error;
260 }
261
262 /* First, get the channel from consumer. */
263 ret = lttng_ust_ctl_recv_channel_from_consumer(*socket->fd_ptr, &ua_chan->obj);
264 if (ret < 0) {
265 if (ret != -EPIPE) {
266 ERR("Error recv channel from consumer %d with ret %d",
267 *socket->fd_ptr, ret);
268 } else {
269 DBG3("UST app recv channel from consumer. Consumer is dead.");
270 }
271 goto error;
272 }
273
274 /* Next, get all streams. */
275 while (1) {
276 struct ust_app_stream *stream;
277
278 /* Create UST stream */
279 stream = ust_app_alloc_stream();
280 if (stream == NULL) {
281 ret = -ENOMEM;
282 goto error;
283 }
284
285 /* Stream object is populated by this call if successful. */
286 ret = lttng_ust_ctl_recv_stream_from_consumer(*socket->fd_ptr, &stream->obj);
287 if (ret < 0) {
288 free(stream);
289 if (ret == -LTTNG_UST_ERR_NOENT) {
290 DBG3("UST app consumer has no more stream available");
291 break;
292 }
293 if (ret != -EPIPE) {
294 ERR("Recv stream from consumer %d with ret %d",
295 *socket->fd_ptr, ret);
296 } else {
297 DBG3("UST app recv stream from consumer. Consumer is dead.");
298 }
299 goto error;
300 }
301
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++;
305
306 DBG2("UST app stream %d received successfully", ua_chan->streams.count);
307 }
308
309 /* This MUST match or else we have a synchronization problem. */
310 LTTNG_ASSERT(ua_chan->expected_stream_count == ua_chan->streams.count);
311
312 /* Wait for confirmation that we can proceed with the streams. */
313 ret = consumer_recv_status_reply(socket);
314 if (ret < 0) {
315 goto error;
316 }
317
318 error:
319 health_code_update();
320 pthread_mutex_unlock(socket->lock);
321 return ret;
322 }
323
324 /*
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.
333 */
334 int ust_consumer_destroy_channel(struct consumer_socket *socket,
335 struct ust_app_channel *ua_chan)
336 {
337 int ret;
338 struct lttcomm_consumer_msg msg;
339
340 LTTNG_ASSERT(ua_chan);
341 LTTNG_ASSERT(socket);
342
343 memset(&msg, 0, sizeof(msg));
344 msg.cmd_type = LTTNG_CONSUMER_DESTROY_CHANNEL;
345 msg.u.destroy_channel.key = ua_chan->key;
346
347 pthread_mutex_lock(socket->lock);
348 health_code_update();
349
350 ret = consumer_send_msg(socket, &msg);
351 if (ret < 0) {
352 goto error;
353 }
354
355 error:
356 health_code_update();
357 pthread_mutex_unlock(socket->lock);
358 return ret;
359 }
360
361 /*
362 * Send a given stream to UST tracer.
363 *
364 * On success return 0 else a negative value.
365 */
366 int 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
371 LTTNG_ASSERT(app);
372 LTTNG_ASSERT(stream);
373 LTTNG_ASSERT(channel);
374
375 DBG2("UST consumer send stream to app %d", app->sock);
376
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);
381 if (ret < 0) {
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);
388 } else {
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);
392 }
393 goto error;
394 }
395 channel->handle = channel->obj->handle;
396
397 error:
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 */
406 int 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
411 LTTNG_ASSERT(app);
412 LTTNG_ASSERT(ua_sess);
413 LTTNG_ASSERT(channel);
414 LTTNG_ASSERT(channel->obj);
415
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);
418
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);
423 if (ret < 0) {
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);
430 } else {
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);
434 }
435 goto error;
436 }
437
438 error:
439 return ret;
440 }
441
442 /*
443 * Handle the metadata requests from the UST consumer
444 *
445 * Return 0 on success else a negative value.
446 */
447 int 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;
453 lsu::registry_session *ust_reg;
454 struct lttcomm_consumer_msg msg;
455
456 LTTNG_ASSERT(socket);
457
458 rcu_read_lock();
459 health_code_update();
460
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);
465 if (ret < 0) {
466 goto end;
467 }
468
469 DBG("Metadata request received for session %" PRIu64 ", key %" PRIu64,
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 =
478 buffer_reg_pid_find(request.session_id_per_pid);
479 if (!reg_pid) {
480 DBG("PID registry not found for session id %" PRIu64,
481 request.session_id_per_pid);
482
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);
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 }
499 LTTNG_ASSERT(ust_reg);
500
501 {
502 auto locked_ust_reg = ust_reg->lock();
503 ret_push = ust_app_push_metadata(locked_ust_reg, socket, 1);
504 }
505 if (ret_push == -EPIPE) {
506 DBG("Application or relay closed while pushing metadata");
507 } else if (ret_push < 0) {
508 ERR("Pushing metadata");
509 ret = -1;
510 goto end;
511 } else {
512 DBG("UST Consumer metadata pushed successfully");
513 }
514 ret = 0;
515
516 end:
517 rcu_read_unlock();
518 return ret;
519 }
This page took 0.039973 seconds and 4 git commands to generate.