Commit | Line | Data |
---|---|---|
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 | ||
18 | #define _GNU_SOURCE | |
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> |
db758600 | 27 | #include <common/consumer.h> |
990570ed | 28 | #include <common/defaults.h> |
48842b30 | 29 | |
00e2e675 | 30 | #include "consumer.h" |
48842b30 DG |
31 | #include "ust-consumer.h" |
32 | ||
33 | /* | |
ffe60014 DG |
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. | |
48842b30 | 40 | */ |
ffe60014 DG |
41 | static char *setup_trace_path(struct consumer_output *consumer, |
42 | struct ust_app_session *ua_sess) | |
48842b30 | 43 | { |
ffe60014 DG |
44 | int ret; |
45 | char *pathname; | |
37278a1e | 46 | |
ffe60014 DG |
47 | assert(consumer); |
48 | assert(ua_sess); | |
00e2e675 | 49 | |
840cb59c | 50 | health_code_update(); |
ca03de58 | 51 | |
ffe60014 DG |
52 | /* Allocate our self the string to make sure we never exceed PATH_MAX. */ |
53 | pathname = zmalloc(PATH_MAX); | |
54 | if (!pathname) { | |
48842b30 DG |
55 | goto error; |
56 | } | |
00e2e675 | 57 | |
ffe60014 DG |
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 | } | |
ca03de58 | 67 | |
ffe60014 DG |
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 | } | |
48842b30 DG |
84 | } |
85 | ||
ffe60014 | 86 | return pathname; |
ca03de58 | 87 | |
37278a1e | 88 | error: |
ffe60014 DG |
89 | free(pathname); |
90 | return NULL; | |
37278a1e DG |
91 | } |
92 | ||
93 | /* | |
ffe60014 DG |
94 | * Send a single channel to the consumer using command ADD_CHANNEL. |
95 | * | |
96 | * Consumer socket MUST be acquired before calling this. | |
37278a1e | 97 | */ |
ffe60014 DG |
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) | |
37278a1e | 101 | { |
ffe60014 | 102 | int ret; |
d88aee68 | 103 | uint64_t key; |
ffe60014 | 104 | char *pathname = NULL; |
37278a1e DG |
105 | struct lttcomm_consumer_msg msg; |
106 | ||
ffe60014 DG |
107 | assert(ua_sess); |
108 | assert(ua_chan); | |
109 | assert(socket); | |
37278a1e | 110 | assert(consumer); |
ffe60014 DG |
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, | |
ca22feea | 130 | pathname, |
ffe60014 DG |
131 | ua_chan->name, |
132 | ua_sess->uid, | |
133 | ua_sess->gid, | |
134 | consumer->net_seq_index, | |
135 | ua_chan->key, | |
45893984 | 136 | ua_sess->registry->uuid); |
37278a1e | 137 | |
840cb59c | 138 | health_code_update(); |
ca03de58 | 139 | |
ffe60014 | 140 | ret = lttcomm_send_unix_sock(socket->fd, &msg, sizeof(msg)); |
37278a1e DG |
141 | if (ret < 0) { |
142 | goto error; | |
143 | } | |
144 | ||
ffe60014 DG |
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 | ||
d88aee68 | 155 | DBG2("UST ask channel %" PRIu64 " successfully done with %u stream(s)", key, |
ffe60014 | 156 | ua_chan->expected_stream_count); |
ca03de58 | 157 | |
37278a1e | 158 | error: |
ffe60014 DG |
159 | free(pathname); |
160 | health_code_update(); | |
37278a1e DG |
161 | return ret; |
162 | } | |
163 | ||
164 | /* | |
ffe60014 DG |
165 | * Ask consumer to create a channel for a given session. |
166 | * | |
167 | * Returns 0 on success else a negative value. | |
37278a1e | 168 | */ |
ffe60014 DG |
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) | |
37278a1e DG |
172 | { |
173 | int ret; | |
37278a1e | 174 | |
ffe60014 DG |
175 | assert(ua_sess); |
176 | assert(ua_chan); | |
177 | assert(consumer); | |
178 | assert(socket); | |
179 | assert(socket->fd >= 0); | |
f50f23d9 | 180 | |
ffe60014 | 181 | pthread_mutex_lock(socket->lock); |
37278a1e | 182 | |
ffe60014 | 183 | ret = ask_channel_creation(ua_sess, ua_chan, consumer, socket); |
37278a1e DG |
184 | if (ret < 0) { |
185 | goto error; | |
186 | } | |
187 | ||
48842b30 | 188 | error: |
ffe60014 | 189 | pthread_mutex_unlock(socket->lock); |
48842b30 DG |
190 | return ret; |
191 | } | |
192 | ||
193 | /* | |
ffe60014 DG |
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. | |
48842b30 | 198 | */ |
ffe60014 DG |
199 | int ust_consumer_get_channel(struct consumer_socket *socket, |
200 | struct ust_app_channel *ua_chan) | |
48842b30 | 201 | { |
ffe60014 | 202 | int ret; |
37278a1e | 203 | struct lttcomm_consumer_msg msg; |
48842b30 | 204 | |
ffe60014 DG |
205 | assert(ua_chan); |
206 | assert(socket); | |
207 | assert(socket->fd >= 0); | |
48842b30 | 208 | |
ffe60014 DG |
209 | msg.cmd_type = LTTNG_CONSUMER_GET_CHANNEL; |
210 | msg.u.get_channel.key = ua_chan->key; | |
37278a1e | 211 | |
ffe60014 | 212 | pthread_mutex_lock(socket->lock); |
840cb59c | 213 | health_code_update(); |
ca03de58 | 214 | |
ffe60014 DG |
215 | /* Send command and wait for OK reply. */ |
216 | ret = consumer_send_msg(socket, &msg); | |
37278a1e DG |
217 | if (ret < 0) { |
218 | goto error; | |
219 | } | |
220 | ||
ffe60014 DG |
221 | /* First, get the channel from consumer. */ |
222 | ret = ustctl_recv_channel_from_consumer(socket->fd, &ua_chan->obj); | |
37278a1e | 223 | if (ret < 0) { |
ffe60014 DG |
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 | } | |
37278a1e DG |
230 | goto error; |
231 | } | |
00e2e675 | 232 | |
ffe60014 DG |
233 | /* Next, get all streams. */ |
234 | while (1) { | |
235 | struct ust_app_stream *stream; | |
ca03de58 | 236 | |
ffe60014 DG |
237 | /* Create UST stream */ |
238 | stream = ust_app_alloc_stream(); | |
239 | if (stream == NULL) { | |
240 | ret = -ENOMEM; | |
48842b30 DG |
241 | goto error; |
242 | } | |
243 | ||
ffe60014 DG |
244 | /* Stream object is populated by this call if successful. */ |
245 | ret = ustctl_recv_stream_from_consumer(socket->fd, &stream->obj); | |
37278a1e | 246 | if (ret < 0) { |
ffe60014 DG |
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."); | |
00e2e675 | 258 | } |
48842b30 DG |
259 | goto error; |
260 | } | |
37278a1e | 261 | |
ffe60014 DG |
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++; | |
37278a1e | 265 | |
ffe60014 DG |
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); | |
ca03de58 | 271 | |
ffe60014 DG |
272 | /* Wait for confirmation that we can proceed with the streams. */ |
273 | ret = consumer_recv_status_reply(socket); | |
37278a1e DG |
274 | if (ret < 0) { |
275 | goto error; | |
276 | } | |
277 | ||
278 | error: | |
ffe60014 DG |
279 | health_code_update(); |
280 | pthread_mutex_unlock(socket->lock); | |
37278a1e DG |
281 | return ret; |
282 | } | |
283 | ||
284 | /* | |
ffe60014 DG |
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. | |
37278a1e | 293 | */ |
ffe60014 DG |
294 | int ust_consumer_destroy_channel(struct consumer_socket *socket, |
295 | struct ust_app_channel *ua_chan) | |
37278a1e | 296 | { |
ffe60014 DG |
297 | int ret; |
298 | struct lttcomm_consumer_msg msg; | |
a4b92340 | 299 | |
ffe60014 DG |
300 | assert(ua_chan); |
301 | assert(socket); | |
302 | assert(socket->fd >= 0); | |
37278a1e | 303 | |
ffe60014 DG |
304 | msg.cmd_type = LTTNG_CONSUMER_DESTROY_CHANNEL; |
305 | msg.u.destroy_channel.key = ua_chan->key; | |
173af62f | 306 | |
ffe60014 DG |
307 | pthread_mutex_lock(socket->lock); |
308 | health_code_update(); | |
37278a1e | 309 | |
ffe60014 | 310 | ret = consumer_send_msg(socket, &msg); |
37278a1e DG |
311 | if (ret < 0) { |
312 | goto error; | |
48842b30 DG |
313 | } |
314 | ||
ffe60014 DG |
315 | error: |
316 | health_code_update(); | |
317 | pthread_mutex_unlock(socket->lock); | |
318 | return ret; | |
319 | } | |
aeb96892 | 320 | |
ffe60014 DG |
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."); | |
48842b30 | 345 | } |
ffe60014 | 346 | goto error; |
48842b30 | 347 | } |
d0b96690 | 348 | channel->handle = channel->obj->handle; |
48842b30 | 349 | |
ffe60014 DG |
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); | |
48842b30 | 371 | |
ffe60014 DG |
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 | } | |
48842b30 DG |
383 | |
384 | error: | |
385 | return ret; | |
386 | } | |
d88aee68 DG |
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 | } |