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" |
7972aab2 | 31 | #include "health.h" |
48842b30 | 32 | #include "ust-consumer.h" |
331744e3 JD |
33 | #include "buffer-registry.h" |
34 | #include "session.h" | |
48842b30 DG |
35 | |
36 | /* | |
ffe60014 DG |
37 | * Return allocated full pathname of the session using the consumer trace path |
38 | * and subdir if available. On a successful allocation, the directory of the | |
39 | * trace is created with the session credentials. | |
40 | * | |
41 | * The caller can safely free(3) the returned value. On error, NULL is | |
42 | * returned. | |
48842b30 | 43 | */ |
ffe60014 DG |
44 | static char *setup_trace_path(struct consumer_output *consumer, |
45 | struct ust_app_session *ua_sess) | |
48842b30 | 46 | { |
ffe60014 DG |
47 | int ret; |
48 | char *pathname; | |
37278a1e | 49 | |
ffe60014 DG |
50 | assert(consumer); |
51 | assert(ua_sess); | |
00e2e675 | 52 | |
840cb59c | 53 | health_code_update(); |
ca03de58 | 54 | |
ffe60014 DG |
55 | /* Allocate our self the string to make sure we never exceed PATH_MAX. */ |
56 | pathname = zmalloc(PATH_MAX); | |
57 | if (!pathname) { | |
48842b30 DG |
58 | goto error; |
59 | } | |
00e2e675 | 60 | |
ffe60014 DG |
61 | /* Get correct path name destination */ |
62 | if (consumer->type == CONSUMER_DST_LOCAL) { | |
63 | /* Set application path to the destination path */ | |
dec56f6c | 64 | ret = snprintf(pathname, PATH_MAX, "%s%s%s", |
ffe60014 DG |
65 | consumer->dst.trace_path, consumer->subdir, ua_sess->path); |
66 | if (ret < 0) { | |
67 | PERROR("snprintf channel path"); | |
68 | goto error; | |
69 | } | |
ca03de58 | 70 | |
ffe60014 | 71 | /* Create directory. Ignore if exist. */ |
7972aab2 DG |
72 | ret = run_as_mkdir_recursive(pathname, S_IRWXU | S_IRWXG, |
73 | ua_sess->euid, ua_sess->egid); | |
ffe60014 DG |
74 | if (ret < 0) { |
75 | if (ret != -EEXIST) { | |
76 | ERR("Trace directory creation error"); | |
77 | goto error; | |
78 | } | |
79 | } | |
80 | } else { | |
dec56f6c | 81 | ret = snprintf(pathname, PATH_MAX, "%s%s", consumer->subdir, |
ffe60014 DG |
82 | ua_sess->path); |
83 | if (ret < 0) { | |
84 | PERROR("snprintf channel path"); | |
85 | goto error; | |
86 | } | |
48842b30 DG |
87 | } |
88 | ||
ffe60014 | 89 | return pathname; |
ca03de58 | 90 | |
37278a1e | 91 | error: |
ffe60014 DG |
92 | free(pathname); |
93 | return NULL; | |
37278a1e DG |
94 | } |
95 | ||
96 | /* | |
ffe60014 DG |
97 | * Send a single channel to the consumer using command ADD_CHANNEL. |
98 | * | |
7972aab2 | 99 | * Consumer socket lock MUST be acquired before calling this. |
37278a1e | 100 | */ |
ffe60014 DG |
101 | static int ask_channel_creation(struct ust_app_session *ua_sess, |
102 | struct ust_app_channel *ua_chan, struct consumer_output *consumer, | |
7972aab2 | 103 | struct consumer_socket *socket, struct ust_registry_session *registry) |
37278a1e | 104 | { |
3f84e025 | 105 | int ret, output; |
7972aab2 DG |
106 | uint32_t chan_id; |
107 | uint64_t key, chan_reg_key; | |
ffe60014 | 108 | char *pathname = NULL; |
37278a1e | 109 | struct lttcomm_consumer_msg msg; |
7972aab2 | 110 | struct ust_registry_channel *chan_reg; |
37278a1e | 111 | |
ffe60014 DG |
112 | assert(ua_sess); |
113 | assert(ua_chan); | |
114 | assert(socket); | |
37278a1e | 115 | assert(consumer); |
7972aab2 | 116 | assert(registry); |
ffe60014 DG |
117 | |
118 | DBG2("Asking UST consumer for channel"); | |
119 | ||
120 | /* Get and create full trace path of session. */ | |
10a50311 JD |
121 | if (ua_sess->output_traces) { |
122 | pathname = setup_trace_path(consumer, ua_sess); | |
123 | if (!pathname) { | |
124 | ret = -1; | |
125 | goto error; | |
126 | } | |
ffe60014 DG |
127 | } |
128 | ||
7972aab2 DG |
129 | /* Depending on the buffer type, a different channel key is used. */ |
130 | if (ua_sess->buffer_type == LTTNG_BUFFER_PER_UID) { | |
131 | chan_reg_key = ua_chan->tracing_channel_id; | |
132 | } else { | |
133 | chan_reg_key = ua_chan->key; | |
134 | } | |
135 | ||
136 | if (ua_chan->attr.type == LTTNG_UST_CHAN_METADATA) { | |
137 | chan_id = -1U; | |
138 | } else { | |
139 | chan_reg = ust_registry_channel_find(registry, chan_reg_key); | |
140 | assert(chan_reg); | |
141 | chan_id = chan_reg->chan_id; | |
142 | } | |
143 | ||
3f84e025 DG |
144 | switch (ua_chan->attr.output) { |
145 | case LTTNG_UST_MMAP: | |
146 | default: | |
147 | output = LTTNG_EVENT_MMAP; | |
148 | break; | |
149 | } | |
150 | ||
ffe60014 DG |
151 | consumer_init_ask_channel_comm_msg(&msg, |
152 | ua_chan->attr.subbuf_size, | |
153 | ua_chan->attr.num_subbuf, | |
154 | ua_chan->attr.overwrite, | |
155 | ua_chan->attr.switch_timer_interval, | |
156 | ua_chan->attr.read_timer_interval, | |
3f84e025 | 157 | output, |
ffe60014 | 158 | (int) ua_chan->attr.type, |
7972aab2 | 159 | ua_sess->tracing_id, |
ca22feea | 160 | pathname, |
ffe60014 | 161 | ua_chan->name, |
7972aab2 DG |
162 | ua_sess->euid, |
163 | ua_sess->egid, | |
ffe60014 DG |
164 | consumer->net_seq_index, |
165 | ua_chan->key, | |
7972aab2 | 166 | registry->uuid, |
1624d5b7 JD |
167 | chan_id, |
168 | ua_chan->tracefile_size, | |
2bba9e53 | 169 | ua_chan->tracefile_count, |
1950109e | 170 | ua_sess->id, |
567eb353 DG |
171 | ua_sess->output_traces, |
172 | ua_sess->uid); | |
37278a1e | 173 | |
840cb59c | 174 | health_code_update(); |
ca03de58 | 175 | |
52898cb1 | 176 | ret = consumer_socket_send(socket, &msg, sizeof(msg)); |
37278a1e DG |
177 | if (ret < 0) { |
178 | goto error; | |
179 | } | |
180 | ||
ffe60014 DG |
181 | ret = consumer_recv_status_channel(socket, &key, |
182 | &ua_chan->expected_stream_count); | |
183 | if (ret < 0) { | |
184 | goto error; | |
185 | } | |
186 | /* Communication protocol error. */ | |
187 | assert(key == ua_chan->key); | |
188 | /* We need at least one where 1 stream for 1 cpu. */ | |
10a50311 JD |
189 | if (ua_sess->output_traces) { |
190 | assert(ua_chan->expected_stream_count > 0); | |
191 | } | |
ffe60014 | 192 | |
d88aee68 | 193 | DBG2("UST ask channel %" PRIu64 " successfully done with %u stream(s)", key, |
ffe60014 | 194 | ua_chan->expected_stream_count); |
ca03de58 | 195 | |
37278a1e | 196 | error: |
ffe60014 DG |
197 | free(pathname); |
198 | health_code_update(); | |
37278a1e DG |
199 | return ret; |
200 | } | |
201 | ||
202 | /* | |
ffe60014 DG |
203 | * Ask consumer to create a channel for a given session. |
204 | * | |
205 | * Returns 0 on success else a negative value. | |
37278a1e | 206 | */ |
ffe60014 DG |
207 | int ust_consumer_ask_channel(struct ust_app_session *ua_sess, |
208 | struct ust_app_channel *ua_chan, struct consumer_output *consumer, | |
7972aab2 | 209 | struct consumer_socket *socket, struct ust_registry_session *registry) |
37278a1e DG |
210 | { |
211 | int ret; | |
37278a1e | 212 | |
ffe60014 DG |
213 | assert(ua_sess); |
214 | assert(ua_chan); | |
215 | assert(consumer); | |
216 | assert(socket); | |
7972aab2 | 217 | assert(registry); |
f50f23d9 | 218 | |
d9078d0c DG |
219 | if (!consumer->enabled) { |
220 | ret = -LTTNG_ERR_NO_CONSUMER; | |
221 | DBG3("Consumer is disabled"); | |
222 | goto error; | |
223 | } | |
224 | ||
ffe60014 | 225 | pthread_mutex_lock(socket->lock); |
37278a1e | 226 | |
7972aab2 | 227 | ret = ask_channel_creation(ua_sess, ua_chan, consumer, socket, registry); |
37278a1e DG |
228 | if (ret < 0) { |
229 | goto error; | |
230 | } | |
231 | ||
48842b30 | 232 | error: |
ffe60014 | 233 | pthread_mutex_unlock(socket->lock); |
48842b30 DG |
234 | return ret; |
235 | } | |
236 | ||
237 | /* | |
ffe60014 DG |
238 | * Send a get channel command to consumer using the given channel key. The |
239 | * channel object is populated and the stream list. | |
240 | * | |
241 | * Return 0 on success else a negative value. | |
48842b30 | 242 | */ |
ffe60014 DG |
243 | int ust_consumer_get_channel(struct consumer_socket *socket, |
244 | struct ust_app_channel *ua_chan) | |
48842b30 | 245 | { |
ffe60014 | 246 | int ret; |
37278a1e | 247 | struct lttcomm_consumer_msg msg; |
48842b30 | 248 | |
ffe60014 DG |
249 | assert(ua_chan); |
250 | assert(socket); | |
48842b30 | 251 | |
ffe60014 DG |
252 | msg.cmd_type = LTTNG_CONSUMER_GET_CHANNEL; |
253 | msg.u.get_channel.key = ua_chan->key; | |
37278a1e | 254 | |
ffe60014 | 255 | pthread_mutex_lock(socket->lock); |
840cb59c | 256 | health_code_update(); |
ca03de58 | 257 | |
ffe60014 DG |
258 | /* Send command and wait for OK reply. */ |
259 | ret = consumer_send_msg(socket, &msg); | |
37278a1e DG |
260 | if (ret < 0) { |
261 | goto error; | |
262 | } | |
263 | ||
ffe60014 | 264 | /* First, get the channel from consumer. */ |
9363801e | 265 | ret = ustctl_recv_channel_from_consumer(*socket->fd_ptr, &ua_chan->obj); |
37278a1e | 266 | if (ret < 0) { |
ffe60014 DG |
267 | if (ret != -EPIPE) { |
268 | ERR("Error recv channel from consumer %d with ret %d", | |
9363801e | 269 | *socket->fd_ptr, ret); |
ffe60014 DG |
270 | } else { |
271 | DBG3("UST app recv channel from consumer. Consumer is dead."); | |
272 | } | |
37278a1e DG |
273 | goto error; |
274 | } | |
00e2e675 | 275 | |
ffe60014 DG |
276 | /* Next, get all streams. */ |
277 | while (1) { | |
278 | struct ust_app_stream *stream; | |
ca03de58 | 279 | |
ffe60014 DG |
280 | /* Create UST stream */ |
281 | stream = ust_app_alloc_stream(); | |
282 | if (stream == NULL) { | |
283 | ret = -ENOMEM; | |
48842b30 DG |
284 | goto error; |
285 | } | |
286 | ||
ffe60014 | 287 | /* Stream object is populated by this call if successful. */ |
9363801e | 288 | ret = ustctl_recv_stream_from_consumer(*socket->fd_ptr, &stream->obj); |
37278a1e | 289 | if (ret < 0) { |
ffe60014 DG |
290 | free(stream); |
291 | if (ret == -LTTNG_UST_ERR_NOENT) { | |
292 | DBG3("UST app consumer has no more stream available"); | |
293 | ret = 0; | |
294 | break; | |
295 | } | |
296 | if (ret != -EPIPE) { | |
297 | ERR("Recv stream from consumer %d with ret %d", | |
9363801e | 298 | *socket->fd_ptr, ret); |
ffe60014 DG |
299 | } else { |
300 | DBG3("UST app recv stream from consumer. Consumer is dead."); | |
00e2e675 | 301 | } |
48842b30 DG |
302 | goto error; |
303 | } | |
37278a1e | 304 | |
ffe60014 DG |
305 | /* Order is important this is why a list is used. */ |
306 | cds_list_add_tail(&stream->list, &ua_chan->streams.head); | |
307 | ua_chan->streams.count++; | |
37278a1e | 308 | |
ffe60014 DG |
309 | DBG2("UST app stream %d received succesfully", ua_chan->streams.count); |
310 | } | |
311 | ||
312 | /* This MUST match or else we have a synchronization problem. */ | |
313 | assert(ua_chan->expected_stream_count == ua_chan->streams.count); | |
ca03de58 | 314 | |
ffe60014 DG |
315 | /* Wait for confirmation that we can proceed with the streams. */ |
316 | ret = consumer_recv_status_reply(socket); | |
37278a1e DG |
317 | if (ret < 0) { |
318 | goto error; | |
319 | } | |
320 | ||
321 | error: | |
ffe60014 DG |
322 | health_code_update(); |
323 | pthread_mutex_unlock(socket->lock); | |
37278a1e DG |
324 | return ret; |
325 | } | |
326 | ||
327 | /* | |
ffe60014 DG |
328 | * Send a destroy channel command to consumer using the given channel key. |
329 | * | |
330 | * Note that this command MUST be used prior to a successful | |
331 | * LTTNG_CONSUMER_GET_CHANNEL because once this command is done successfully, | |
332 | * the streams are dispatched to the consumer threads and MUST be teardown | |
333 | * through the hang up process. | |
334 | * | |
335 | * Return 0 on success else a negative value. | |
37278a1e | 336 | */ |
ffe60014 DG |
337 | int ust_consumer_destroy_channel(struct consumer_socket *socket, |
338 | struct ust_app_channel *ua_chan) | |
37278a1e | 339 | { |
ffe60014 DG |
340 | int ret; |
341 | struct lttcomm_consumer_msg msg; | |
a4b92340 | 342 | |
ffe60014 DG |
343 | assert(ua_chan); |
344 | assert(socket); | |
37278a1e | 345 | |
ffe60014 DG |
346 | msg.cmd_type = LTTNG_CONSUMER_DESTROY_CHANNEL; |
347 | msg.u.destroy_channel.key = ua_chan->key; | |
173af62f | 348 | |
ffe60014 DG |
349 | pthread_mutex_lock(socket->lock); |
350 | health_code_update(); | |
37278a1e | 351 | |
ffe60014 | 352 | ret = consumer_send_msg(socket, &msg); |
37278a1e DG |
353 | if (ret < 0) { |
354 | goto error; | |
48842b30 DG |
355 | } |
356 | ||
ffe60014 DG |
357 | error: |
358 | health_code_update(); | |
359 | pthread_mutex_unlock(socket->lock); | |
360 | return ret; | |
361 | } | |
aeb96892 | 362 | |
ffe60014 DG |
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 | ret = ustctl_send_stream_to_ust(app->sock, channel->obj, stream->obj); | |
381 | if (ret < 0) { | |
382 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { | |
383 | ERR("Error ustctl send stream %s to app pid: %d with ret %d", | |
384 | stream->name, app->pid, ret); | |
385 | } else { | |
386 | DBG3("UST app send stream to ust failed. Application is dead."); | |
48842b30 | 387 | } |
ffe60014 | 388 | goto error; |
48842b30 | 389 | } |
d0b96690 | 390 | channel->handle = channel->obj->handle; |
48842b30 | 391 | |
ffe60014 DG |
392 | error: |
393 | return ret; | |
394 | } | |
395 | ||
396 | /* | |
397 | * Send channel previously received from the consumer to the UST tracer. | |
398 | * | |
399 | * On success return 0 else a negative value. | |
400 | */ | |
401 | int ust_consumer_send_channel_to_ust(struct ust_app *app, | |
402 | struct ust_app_session *ua_sess, struct ust_app_channel *channel) | |
403 | { | |
404 | int ret; | |
405 | ||
406 | assert(app); | |
407 | assert(ua_sess); | |
408 | assert(channel); | |
409 | assert(channel->obj); | |
410 | ||
7972aab2 DG |
411 | DBG2("UST app send channel to sock %d pid %d (name: %s, key: %" PRIu64 ")", |
412 | app->sock, app->pid, channel->name, channel->tracing_channel_id); | |
48842b30 | 413 | |
ffe60014 DG |
414 | /* Send stream to application. */ |
415 | ret = ustctl_send_channel_to_ust(app->sock, ua_sess->handle, channel->obj); | |
416 | if (ret < 0) { | |
417 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { | |
418 | ERR("Error ustctl send channel %s to app pid: %d with ret %d", | |
419 | channel->name, app->pid, ret); | |
420 | } else { | |
421 | DBG3("UST app send channel to ust failed. Application is dead."); | |
422 | } | |
423 | goto error; | |
424 | } | |
48842b30 DG |
425 | |
426 | error: | |
427 | return ret; | |
428 | } | |
331744e3 JD |
429 | |
430 | /* | |
431 | * Handle the metadata requests from the UST consumer | |
432 | * | |
433 | * Return 0 on success else a negative value. | |
434 | */ | |
435 | int ust_consumer_metadata_request(struct consumer_socket *socket) | |
436 | { | |
437 | int ret; | |
438 | ssize_t ret_push; | |
439 | struct lttcomm_metadata_request_msg request; | |
440 | struct buffer_reg_uid *reg_uid; | |
441 | struct ust_registry_session *ust_reg; | |
442 | struct lttcomm_consumer_msg msg; | |
443 | ||
444 | assert(socket); | |
445 | ||
446 | rcu_read_lock(); | |
447 | pthread_mutex_lock(socket->lock); | |
448 | ||
449 | health_code_update(); | |
450 | ||
451 | /* Wait for a metadata request */ | |
52898cb1 DG |
452 | ret = consumer_socket_recv(socket, &request, sizeof(request)); |
453 | if (ret < 0) { | |
331744e3 JD |
454 | goto end; |
455 | } | |
456 | ||
1950109e | 457 | DBG("Metadata request received for session %" PRIu64 ", key %" PRIu64, |
331744e3 JD |
458 | request.session_id, request.key); |
459 | ||
460 | reg_uid = buffer_reg_uid_find(request.session_id, | |
461 | request.bits_per_long, request.uid); | |
462 | if (reg_uid) { | |
463 | ust_reg = reg_uid->registry->reg.ust; | |
464 | } else { | |
465 | struct buffer_reg_pid *reg_pid = | |
1950109e | 466 | buffer_reg_pid_find(request.session_id_per_pid); |
331744e3 | 467 | if (!reg_pid) { |
1950109e JD |
468 | DBG("PID registry not found for session id %" PRIu64, |
469 | request.session_id_per_pid); | |
331744e3 JD |
470 | |
471 | msg.cmd_type = LTTNG_ERR_UND; | |
472 | (void) consumer_send_msg(socket, &msg); | |
473 | /* | |
474 | * This is possible since the session might have been destroyed | |
475 | * during a consumer metadata request. So here, return gracefully | |
476 | * because the destroy session will push the remaining metadata to | |
477 | * the consumer. | |
478 | */ | |
479 | ret = 0; | |
480 | goto end; | |
481 | } | |
482 | ust_reg = reg_pid->registry->reg.ust; | |
483 | } | |
484 | assert(ust_reg); | |
485 | ||
486 | ret_push = ust_app_push_metadata(ust_reg, socket, 1); | |
487 | if (ret_push < 0) { | |
488 | ERR("Pushing metadata"); | |
489 | ret = -1; | |
490 | goto end; | |
491 | } | |
492 | DBG("UST Consumer metadata pushed successfully"); | |
493 | ret = 0; | |
494 | ||
495 | end: | |
496 | pthread_mutex_unlock(socket->lock); | |
497 | rcu_read_unlock(); | |
498 | return ret; | |
499 | } |