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