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