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