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