Fix: function definition and declaration mismatch
[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 _GNU_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.h>
28 #include <common/defaults.h>
29
30 #include "consumer.h"
31 #include "ust-consumer.h"
32
33 /*
34 * Return allocated full pathname of the session using the consumer trace path
35 * and subdir if available. On a successful allocation, the directory of the
36 * trace is created with the session credentials.
37 *
38 * The caller can safely free(3) the returned value. On error, NULL is
39 * returned.
40 */
41 static char *setup_trace_path(struct consumer_output *consumer,
42 struct ust_app_session *ua_sess)
43 {
44 int ret;
45 char *pathname;
46
47 assert(consumer);
48 assert(ua_sess);
49
50 health_code_update();
51
52 /* Allocate our self the string to make sure we never exceed PATH_MAX. */
53 pathname = zmalloc(PATH_MAX);
54 if (!pathname) {
55 goto error;
56 }
57
58 /* Get correct path name destination */
59 if (consumer->type == CONSUMER_DST_LOCAL) {
60 /* Set application path to the destination path */
61 ret = snprintf(pathname, PATH_MAX, "%s/%s/%s",
62 consumer->dst.trace_path, consumer->subdir, ua_sess->path);
63 if (ret < 0) {
64 PERROR("snprintf channel path");
65 goto error;
66 }
67
68 /* Create directory. Ignore if exist. */
69 ret = run_as_mkdir_recursive(pathname, S_IRWXU | S_IRWXG, ua_sess->uid,
70 ua_sess->gid);
71 if (ret < 0) {
72 if (ret != -EEXIST) {
73 ERR("Trace directory creation error");
74 goto error;
75 }
76 }
77 } else {
78 ret = snprintf(pathname, PATH_MAX, "%s/%s", consumer->subdir,
79 ua_sess->path);
80 if (ret < 0) {
81 PERROR("snprintf channel path");
82 goto error;
83 }
84 }
85
86 return pathname;
87
88 error:
89 free(pathname);
90 return NULL;
91 }
92
93 /*
94 * Send a single channel to the consumer using command ADD_CHANNEL.
95 *
96 * Consumer socket MUST be acquired before calling this.
97 */
98 static int ask_channel_creation(struct ust_app_session *ua_sess,
99 struct ust_app_channel *ua_chan, struct consumer_output *consumer,
100 struct consumer_socket *socket)
101 {
102 int ret;
103 uint64_t key;
104 char *pathname = NULL;
105 struct lttcomm_consumer_msg msg;
106
107 assert(ua_sess);
108 assert(ua_chan);
109 assert(socket);
110 assert(consumer);
111
112 DBG2("Asking UST consumer for channel");
113
114 /* Get and create full trace path of session. */
115 pathname = setup_trace_path(consumer, ua_sess);
116 if (!pathname) {
117 ret = -1;
118 goto error;
119 }
120
121 consumer_init_ask_channel_comm_msg(&msg,
122 ua_chan->attr.subbuf_size,
123 ua_chan->attr.num_subbuf,
124 ua_chan->attr.overwrite,
125 ua_chan->attr.switch_timer_interval,
126 ua_chan->attr.read_timer_interval,
127 (int) ua_chan->attr.output,
128 (int) ua_chan->attr.type,
129 ua_sess->id,
130 pathname,
131 ua_chan->name,
132 ua_sess->uid,
133 ua_sess->gid,
134 consumer->net_seq_index,
135 ua_chan->key,
136 ua_sess->registry.uuid);
137
138 health_code_update();
139
140 ret = lttcomm_send_unix_sock(socket->fd, &msg, sizeof(msg));
141 if (ret < 0) {
142 goto error;
143 }
144
145 ret = consumer_recv_status_channel(socket, &key,
146 &ua_chan->expected_stream_count);
147 if (ret < 0) {
148 goto error;
149 }
150 /* Communication protocol error. */
151 assert(key == ua_chan->key);
152 /* We need at least one where 1 stream for 1 cpu. */
153 assert(ua_chan->expected_stream_count > 0);
154
155 DBG2("UST ask channel %" PRIu64 " successfully done with %u stream(s)", key,
156 ua_chan->expected_stream_count);
157
158 error:
159 free(pathname);
160 health_code_update();
161 return ret;
162 }
163
164 /*
165 * Ask consumer to create a channel for a given session.
166 *
167 * Returns 0 on success else a negative value.
168 */
169 int ust_consumer_ask_channel(struct ust_app_session *ua_sess,
170 struct ust_app_channel *ua_chan, struct consumer_output *consumer,
171 struct consumer_socket *socket)
172 {
173 int ret;
174
175 assert(ua_sess);
176 assert(ua_chan);
177 assert(consumer);
178 assert(socket);
179 assert(socket->fd >= 0);
180
181 pthread_mutex_lock(socket->lock);
182
183 ret = ask_channel_creation(ua_sess, ua_chan, consumer, socket);
184 if (ret < 0) {
185 goto error;
186 }
187
188 error:
189 pthread_mutex_unlock(socket->lock);
190 return ret;
191 }
192
193 /*
194 * Send a get channel command to consumer using the given channel key. The
195 * channel object is populated and the stream list.
196 *
197 * Return 0 on success else a negative value.
198 */
199 int ust_consumer_get_channel(struct consumer_socket *socket,
200 struct ust_app_channel *ua_chan)
201 {
202 int ret;
203 struct lttcomm_consumer_msg msg;
204
205 assert(ua_chan);
206 assert(socket);
207 assert(socket->fd >= 0);
208
209 msg.cmd_type = LTTNG_CONSUMER_GET_CHANNEL;
210 msg.u.get_channel.key = ua_chan->key;
211
212 pthread_mutex_lock(socket->lock);
213 health_code_update();
214
215 /* Send command and wait for OK reply. */
216 ret = consumer_send_msg(socket, &msg);
217 if (ret < 0) {
218 goto error;
219 }
220
221 /* First, get the channel from consumer. */
222 ret = ustctl_recv_channel_from_consumer(socket->fd, &ua_chan->obj);
223 if (ret < 0) {
224 if (ret != -EPIPE) {
225 ERR("Error recv channel from consumer %d with ret %d",
226 socket->fd, ret);
227 } else {
228 DBG3("UST app recv channel from consumer. Consumer is dead.");
229 }
230 goto error;
231 }
232
233 /* Next, get all streams. */
234 while (1) {
235 struct ust_app_stream *stream;
236
237 /* Create UST stream */
238 stream = ust_app_alloc_stream();
239 if (stream == NULL) {
240 ret = -ENOMEM;
241 goto error;
242 }
243
244 /* Stream object is populated by this call if successful. */
245 ret = ustctl_recv_stream_from_consumer(socket->fd, &stream->obj);
246 if (ret < 0) {
247 free(stream);
248 if (ret == -LTTNG_UST_ERR_NOENT) {
249 DBG3("UST app consumer has no more stream available");
250 ret = 0;
251 break;
252 }
253 if (ret != -EPIPE) {
254 ERR("Recv stream from consumer %d with ret %d",
255 socket->fd, ret);
256 } else {
257 DBG3("UST app recv stream from consumer. Consumer is dead.");
258 }
259 goto error;
260 }
261
262 /* Order is important this is why a list is used. */
263 cds_list_add_tail(&stream->list, &ua_chan->streams.head);
264 ua_chan->streams.count++;
265
266 DBG2("UST app stream %d received succesfully", ua_chan->streams.count);
267 }
268
269 /* This MUST match or else we have a synchronization problem. */
270 assert(ua_chan->expected_stream_count == ua_chan->streams.count);
271
272 /* Wait for confirmation that we can proceed with the streams. */
273 ret = consumer_recv_status_reply(socket);
274 if (ret < 0) {
275 goto error;
276 }
277
278 error:
279 health_code_update();
280 pthread_mutex_unlock(socket->lock);
281 return ret;
282 }
283
284 /*
285 * Send a destroy channel command to consumer using the given channel key.
286 *
287 * Note that this command MUST be used prior to a successful
288 * LTTNG_CONSUMER_GET_CHANNEL because once this command is done successfully,
289 * the streams are dispatched to the consumer threads and MUST be teardown
290 * through the hang up process.
291 *
292 * Return 0 on success else a negative value.
293 */
294 int ust_consumer_destroy_channel(struct consumer_socket *socket,
295 struct ust_app_channel *ua_chan)
296 {
297 int ret;
298 struct lttcomm_consumer_msg msg;
299
300 assert(ua_chan);
301 assert(socket);
302 assert(socket->fd >= 0);
303
304 msg.cmd_type = LTTNG_CONSUMER_DESTROY_CHANNEL;
305 msg.u.destroy_channel.key = ua_chan->key;
306
307 pthread_mutex_lock(socket->lock);
308 health_code_update();
309
310 ret = consumer_send_msg(socket, &msg);
311 if (ret < 0) {
312 goto error;
313 }
314
315 error:
316 health_code_update();
317 pthread_mutex_unlock(socket->lock);
318 return ret;
319 }
320
321 /*
322 * Send a given stream to UST tracer.
323 *
324 * On success return 0 else a negative value.
325 */
326 int ust_consumer_send_stream_to_ust(struct ust_app *app,
327 struct ust_app_channel *channel, struct ust_app_stream *stream)
328 {
329 int ret;
330
331 assert(app);
332 assert(stream);
333 assert(channel);
334
335 DBG2("UST consumer send stream to app %d", app->sock);
336
337 /* Relay stream to application. */
338 ret = ustctl_send_stream_to_ust(app->sock, channel->obj, stream->obj);
339 if (ret < 0) {
340 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
341 ERR("Error ustctl send stream %s to app pid: %d with ret %d",
342 stream->name, app->pid, ret);
343 } else {
344 DBG3("UST app send stream to ust failed. Application is dead.");
345 }
346 goto error;
347 }
348 channel->handle = channel->obj->handle;
349
350 error:
351 return ret;
352 }
353
354 /*
355 * Send channel previously received from the consumer to the UST tracer.
356 *
357 * On success return 0 else a negative value.
358 */
359 int ust_consumer_send_channel_to_ust(struct ust_app *app,
360 struct ust_app_session *ua_sess, struct ust_app_channel *channel)
361 {
362 int ret;
363
364 assert(app);
365 assert(ua_sess);
366 assert(channel);
367 assert(channel->obj);
368
369 DBG2("UST app send channel to app sock %d pid %d (name: %s, key: %lu)",
370 app->sock, app->pid, channel->name, channel->key);
371
372 /* Send stream to application. */
373 ret = ustctl_send_channel_to_ust(app->sock, ua_sess->handle, channel->obj);
374 if (ret < 0) {
375 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
376 ERR("Error ustctl send channel %s to app pid: %d with ret %d",
377 channel->name, app->pid, ret);
378 } else {
379 DBG3("UST app send channel to ust failed. Application is dead.");
380 }
381 goto error;
382 }
383
384 error:
385 return ret;
386 }
387
388 /*
389 * Send metadata string to consumer.
390 *
391 * Return 0 on success else a negative value.
392 */
393 int ust_consumer_push_metadata(struct consumer_socket *socket,
394 struct ust_app_session *ua_sess, char *metadata_str,
395 size_t len, size_t target_offset)
396 {
397 int ret;
398 struct lttcomm_consumer_msg msg;
399
400 assert(socket);
401 assert(socket->fd >= 0);
402 assert(ua_sess);
403 assert(ua_sess->metadata);
404
405 DBG2("UST consumer push metadata to consumer socket %d", socket->fd);
406
407 msg.cmd_type = LTTNG_CONSUMER_PUSH_METADATA;
408 msg.u.push_metadata.key = ua_sess->metadata->key;
409 msg.u.push_metadata.target_offset = target_offset;
410 msg.u.push_metadata.len = len;
411
412 /*
413 * TODO: reenable these locks when the consumerd gets the ability to
414 * reorder the metadata it receives. This fits with locking in
415 * src/bin/lttng-sessiond/ust-app.c:push_metadata()
416 *
417 * pthread_mutex_lock(socket->lock);
418 */
419
420 health_code_update();
421 ret = consumer_send_msg(socket, &msg);
422 if (ret < 0) {
423 goto error;
424 }
425
426 DBG3("UST consumer push metadata on sock %d of len %lu", socket->fd, len);
427
428 ret = lttcomm_send_unix_sock(socket->fd, metadata_str, len);
429 if (ret < 0) {
430 fprintf(stderr, "send error: %d\n", ret);
431 goto error;
432 }
433
434 health_code_update();
435 ret = consumer_recv_status_reply(socket);
436 if (ret < 0) {
437 goto error;
438 }
439
440 error:
441 health_code_update();
442 /*
443 * pthread_mutex_unlock(socket->lock);
444 */
445 return ret;
446 }
447
448 /*
449 * Send a close metdata command to consumer using the given channel key.
450 *
451 * Return 0 on success else a negative value.
452 */
453 int ust_consumer_close_metadata(struct consumer_socket *socket,
454 struct ust_app_channel *ua_chan)
455 {
456 int ret;
457 struct lttcomm_consumer_msg msg;
458
459 assert(ua_chan);
460 assert(socket);
461 assert(socket->fd >= 0);
462
463 DBG2("UST consumer close metadata channel key %lu", ua_chan->key);
464
465 msg.cmd_type = LTTNG_CONSUMER_CLOSE_METADATA;
466 msg.u.close_metadata.key = ua_chan->key;
467
468 pthread_mutex_lock(socket->lock);
469 health_code_update();
470
471 ret = consumer_send_msg(socket, &msg);
472 if (ret < 0) {
473 goto error;
474 }
475
476 error:
477 health_code_update();
478 pthread_mutex_unlock(socket->lock);
479 return ret;
480 }
481
482 /*
483 * Send a setup metdata command to consumer using the given channel key.
484 *
485 * Return 0 on success else a negative value.
486 */
487 int ust_consumer_setup_metadata(struct consumer_socket *socket,
488 struct ust_app_channel *ua_chan)
489 {
490 int ret;
491 struct lttcomm_consumer_msg msg;
492
493 assert(ua_chan);
494 assert(socket);
495 assert(socket->fd >= 0);
496
497 DBG2("UST consumer setup metadata channel key %lu", ua_chan->key);
498
499 msg.cmd_type = LTTNG_CONSUMER_SETUP_METADATA;
500 msg.u.setup_metadata.key = ua_chan->key;
501
502 pthread_mutex_lock(socket->lock);
503 health_code_update();
504
505 ret = consumer_send_msg(socket, &msg);
506 if (ret < 0) {
507 goto error;
508 }
509
510 error:
511 health_code_update();
512 pthread_mutex_unlock(socket->lock);
513 return ret;
514 }
This page took 0.040956 seconds and 4 git commands to generate.