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