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