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