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