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