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 | ||
6c1c0768 | 18 | #define _LGPL_SOURCE |
48842b30 DG |
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> |
c8fea79c | 27 | #include <common/consumer/consumer.h> |
990570ed | 28 | #include <common/defaults.h> |
48842b30 | 29 | |
00e2e675 | 30 | #include "consumer.h" |
8782cc74 | 31 | #include "health-sessiond.h" |
48842b30 | 32 | #include "ust-consumer.h" |
331744e3 JD |
33 | #include "buffer-registry.h" |
34 | #include "session.h" | |
e9404c27 | 35 | #include "lttng-sessiond.h" |
48842b30 | 36 | |
37278a1e | 37 | /* |
e9404c27 | 38 | * Send a single channel to the consumer using command ASK_CHANNEL_CREATION. |
ffe60014 | 39 | * |
7972aab2 | 40 | * Consumer socket lock MUST be acquired before calling this. |
37278a1e | 41 | */ |
ffe60014 | 42 | static int ask_channel_creation(struct ust_app_session *ua_sess, |
e098433c JG |
43 | struct ust_app_channel *ua_chan, |
44 | struct consumer_output *consumer, | |
45 | struct consumer_socket *socket, | |
46 | struct ust_registry_session *registry, | |
e5148e25 | 47 | struct lttng_trace_chunk *trace_chunk) |
37278a1e | 48 | { |
0c759fc9 | 49 | int ret, output; |
7972aab2 DG |
50 | uint32_t chan_id; |
51 | uint64_t key, chan_reg_key; | |
ffe60014 | 52 | char *pathname = NULL; |
37278a1e | 53 | struct lttcomm_consumer_msg msg; |
7972aab2 | 54 | struct ust_registry_channel *chan_reg; |
d7ba1388 | 55 | char shm_path[PATH_MAX] = ""; |
3d071855 | 56 | char root_shm_path[PATH_MAX] = ""; |
e5148e25 | 57 | bool is_local_trace; |
37278a1e | 58 | |
ffe60014 DG |
59 | assert(ua_sess); |
60 | assert(ua_chan); | |
61 | assert(socket); | |
37278a1e | 62 | assert(consumer); |
7972aab2 | 63 | assert(registry); |
ffe60014 DG |
64 | |
65 | DBG2("Asking UST consumer for channel"); | |
66 | ||
e5148e25 JG |
67 | is_local_trace = consumer->net_seq_index == -1ULL; |
68 | /* Format the channel's path (relative to the current trace chunk). */ | |
1819b04a | 69 | pathname = setup_channel_trace_path(consumer, ua_sess->path); |
e5148e25 JG |
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) { | |
10a50311 JD |
95 | ret = -1; |
96 | goto error; | |
97 | } | |
ffe60014 DG |
98 | } |
99 | ||
7972aab2 DG |
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; | |
d7ba1388 MD |
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 | */ | |
7972aab2 DG |
114 | } else { |
115 | chan_reg = ust_registry_channel_find(registry, chan_reg_key); | |
116 | assert(chan_reg); | |
117 | chan_id = chan_reg->chan_id; | |
d7ba1388 MD |
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 | } | |
3d071855 MD |
128 | strncpy(root_shm_path, ua_sess->root_shm_path, sizeof(root_shm_path)); |
129 | root_shm_path[sizeof(root_shm_path) - 1] = '\0'; | |
7972aab2 DG |
130 | } |
131 | ||
0c759fc9 DG |
132 | switch (ua_chan->attr.output) { |
133 | case LTTNG_UST_MMAP: | |
134 | default: | |
135 | output = LTTNG_EVENT_MMAP; | |
136 | break; | |
137 | } | |
138 | ||
ffe60014 DG |
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, | |
ecc48a90 | 145 | ua_sess->live_timer_interval, |
e9404c27 | 146 | ua_chan->monitor_timer_interval, |
0c759fc9 | 147 | output, |
ffe60014 | 148 | (int) ua_chan->attr.type, |
7972aab2 | 149 | ua_sess->tracing_id, |
ca22feea | 150 | pathname, |
ffe60014 | 151 | ua_chan->name, |
ffe60014 DG |
152 | consumer->net_seq_index, |
153 | ua_chan->key, | |
7972aab2 | 154 | registry->uuid, |
1624d5b7 JD |
155 | chan_id, |
156 | ua_chan->tracefile_size, | |
2bba9e53 | 157 | ua_chan->tracefile_count, |
1950109e | 158 | ua_sess->id, |
567eb353 | 159 | ua_sess->output_traces, |
c51311d6 | 160 | ua_sess->real_credentials.uid, |
491d1539 | 161 | ua_chan->attr.blocking_timeout, |
e098433c | 162 | root_shm_path, shm_path, |
c51311d6 JG |
163 | trace_chunk, |
164 | &ua_sess->effective_credentials); | |
37278a1e | 165 | |
840cb59c | 166 | health_code_update(); |
ca03de58 | 167 | |
52898cb1 | 168 | ret = consumer_socket_send(socket, &msg, sizeof(msg)); |
37278a1e DG |
169 | if (ret < 0) { |
170 | goto error; | |
171 | } | |
172 | ||
ffe60014 DG |
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. */ | |
10a50311 JD |
181 | if (ua_sess->output_traces) { |
182 | assert(ua_chan->expected_stream_count > 0); | |
183 | } | |
ffe60014 | 184 | |
d88aee68 | 185 | DBG2("UST ask channel %" PRIu64 " successfully done with %u stream(s)", key, |
ffe60014 | 186 | ua_chan->expected_stream_count); |
ca03de58 | 187 | |
37278a1e | 188 | error: |
ffe60014 DG |
189 | free(pathname); |
190 | health_code_update(); | |
37278a1e DG |
191 | return ret; |
192 | } | |
193 | ||
194 | /* | |
ffe60014 DG |
195 | * Ask consumer to create a channel for a given session. |
196 | * | |
e9404c27 JG |
197 | * Session list and rcu read side locks must be held by the caller. |
198 | * | |
ffe60014 | 199 | * Returns 0 on success else a negative value. |
37278a1e | 200 | */ |
ffe60014 | 201 | int ust_consumer_ask_channel(struct ust_app_session *ua_sess, |
e098433c JG |
202 | struct ust_app_channel *ua_chan, |
203 | struct consumer_output *consumer, | |
204 | struct consumer_socket *socket, | |
205 | struct ust_registry_session *registry, | |
e5148e25 | 206 | struct lttng_trace_chunk * trace_chunk) |
37278a1e DG |
207 | { |
208 | int ret; | |
37278a1e | 209 | |
ffe60014 DG |
210 | assert(ua_sess); |
211 | assert(ua_chan); | |
212 | assert(consumer); | |
213 | assert(socket); | |
7972aab2 | 214 | assert(registry); |
f50f23d9 | 215 | |
d9078d0c DG |
216 | if (!consumer->enabled) { |
217 | ret = -LTTNG_ERR_NO_CONSUMER; | |
218 | DBG3("Consumer is disabled"); | |
219 | goto error; | |
220 | } | |
221 | ||
ffe60014 | 222 | pthread_mutex_lock(socket->lock); |
e098433c | 223 | ret = ask_channel_creation(ua_sess, ua_chan, consumer, socket, registry, |
e5148e25 | 224 | trace_chunk); |
2898de39 | 225 | pthread_mutex_unlock(socket->lock); |
37278a1e | 226 | if (ret < 0) { |
e9404c27 | 227 | ERR("ask_channel_creation consumer command failed"); |
37278a1e DG |
228 | goto error; |
229 | } | |
230 | ||
48842b30 DG |
231 | error: |
232 | return ret; | |
233 | } | |
234 | ||
235 | /* | |
ffe60014 DG |
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. | |
48842b30 | 240 | */ |
ffe60014 DG |
241 | int ust_consumer_get_channel(struct consumer_socket *socket, |
242 | struct ust_app_channel *ua_chan) | |
48842b30 | 243 | { |
ffe60014 | 244 | int ret; |
37278a1e | 245 | struct lttcomm_consumer_msg msg; |
48842b30 | 246 | |
ffe60014 DG |
247 | assert(ua_chan); |
248 | assert(socket); | |
48842b30 | 249 | |
53efb85a | 250 | memset(&msg, 0, sizeof(msg)); |
ffe60014 DG |
251 | msg.cmd_type = LTTNG_CONSUMER_GET_CHANNEL; |
252 | msg.u.get_channel.key = ua_chan->key; | |
37278a1e | 253 | |
ffe60014 | 254 | pthread_mutex_lock(socket->lock); |
840cb59c | 255 | health_code_update(); |
ca03de58 | 256 | |
ffe60014 DG |
257 | /* Send command and wait for OK reply. */ |
258 | ret = consumer_send_msg(socket, &msg); | |
37278a1e DG |
259 | if (ret < 0) { |
260 | goto error; | |
261 | } | |
262 | ||
ffe60014 | 263 | /* First, get the channel from consumer. */ |
9363801e | 264 | ret = ustctl_recv_channel_from_consumer(*socket->fd_ptr, &ua_chan->obj); |
37278a1e | 265 | if (ret < 0) { |
ffe60014 DG |
266 | if (ret != -EPIPE) { |
267 | ERR("Error recv channel from consumer %d with ret %d", | |
9363801e | 268 | *socket->fd_ptr, ret); |
ffe60014 DG |
269 | } else { |
270 | DBG3("UST app recv channel from consumer. Consumer is dead."); | |
271 | } | |
37278a1e DG |
272 | goto error; |
273 | } | |
00e2e675 | 274 | |
ffe60014 DG |
275 | /* Next, get all streams. */ |
276 | while (1) { | |
277 | struct ust_app_stream *stream; | |
ca03de58 | 278 | |
ffe60014 DG |
279 | /* Create UST stream */ |
280 | stream = ust_app_alloc_stream(); | |
281 | if (stream == NULL) { | |
282 | ret = -ENOMEM; | |
48842b30 DG |
283 | goto error; |
284 | } | |
285 | ||
ffe60014 | 286 | /* Stream object is populated by this call if successful. */ |
9363801e | 287 | ret = ustctl_recv_stream_from_consumer(*socket->fd_ptr, &stream->obj); |
37278a1e | 288 | if (ret < 0) { |
ffe60014 DG |
289 | free(stream); |
290 | if (ret == -LTTNG_UST_ERR_NOENT) { | |
291 | DBG3("UST app consumer has no more stream available"); | |
ffe60014 DG |
292 | break; |
293 | } | |
294 | if (ret != -EPIPE) { | |
295 | ERR("Recv stream from consumer %d with ret %d", | |
9363801e | 296 | *socket->fd_ptr, ret); |
ffe60014 DG |
297 | } else { |
298 | DBG3("UST app recv stream from consumer. Consumer is dead."); | |
00e2e675 | 299 | } |
48842b30 DG |
300 | goto error; |
301 | } | |
37278a1e | 302 | |
ffe60014 DG |
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++; | |
37278a1e | 306 | |
5368d366 | 307 | DBG2("UST app stream %d received successfully", ua_chan->streams.count); |
ffe60014 DG |
308 | } |
309 | ||
310 | /* This MUST match or else we have a synchronization problem. */ | |
311 | assert(ua_chan->expected_stream_count == ua_chan->streams.count); | |
ca03de58 | 312 | |
ffe60014 DG |
313 | /* Wait for confirmation that we can proceed with the streams. */ |
314 | ret = consumer_recv_status_reply(socket); | |
37278a1e DG |
315 | if (ret < 0) { |
316 | goto error; | |
317 | } | |
318 | ||
319 | error: | |
ffe60014 DG |
320 | health_code_update(); |
321 | pthread_mutex_unlock(socket->lock); | |
37278a1e DG |
322 | return ret; |
323 | } | |
324 | ||
325 | /* | |
ffe60014 DG |
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. | |
37278a1e | 334 | */ |
ffe60014 DG |
335 | int ust_consumer_destroy_channel(struct consumer_socket *socket, |
336 | struct ust_app_channel *ua_chan) | |
37278a1e | 337 | { |
ffe60014 DG |
338 | int ret; |
339 | struct lttcomm_consumer_msg msg; | |
a4b92340 | 340 | |
ffe60014 DG |
341 | assert(ua_chan); |
342 | assert(socket); | |
37278a1e | 343 | |
53efb85a | 344 | memset(&msg, 0, sizeof(msg)); |
ffe60014 DG |
345 | msg.cmd_type = LTTNG_CONSUMER_DESTROY_CHANNEL; |
346 | msg.u.destroy_channel.key = ua_chan->key; | |
173af62f | 347 | |
ffe60014 DG |
348 | pthread_mutex_lock(socket->lock); |
349 | health_code_update(); | |
37278a1e | 350 | |
ffe60014 | 351 | ret = consumer_send_msg(socket, &msg); |
37278a1e DG |
352 | if (ret < 0) { |
353 | goto error; | |
48842b30 DG |
354 | } |
355 | ||
ffe60014 DG |
356 | error: |
357 | health_code_update(); | |
358 | pthread_mutex_unlock(socket->lock); | |
359 | return ret; | |
360 | } | |
aeb96892 | 361 | |
ffe60014 DG |
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. */ | |
fb45065e | 379 | pthread_mutex_lock(&app->sock_lock); |
ffe60014 | 380 | ret = ustctl_send_stream_to_ust(app->sock, channel->obj, stream->obj); |
fb45065e | 381 | pthread_mutex_unlock(&app->sock_lock); |
ffe60014 DG |
382 | if (ret < 0) { |
383 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { | |
8cf93def DG |
384 | ERR("ustctl send stream handle %d to app pid: %d with ret %d", |
385 | stream->obj->handle, app->pid, ret); | |
ffe60014 DG |
386 | } else { |
387 | DBG3("UST app send stream to ust failed. Application is dead."); | |
48842b30 | 388 | } |
ffe60014 | 389 | goto error; |
48842b30 | 390 | } |
d0b96690 | 391 | channel->handle = channel->obj->handle; |
48842b30 | 392 | |
ffe60014 DG |
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 | ||
7972aab2 DG |
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); | |
48842b30 | 414 | |
ffe60014 | 415 | /* Send stream to application. */ |
fb45065e | 416 | pthread_mutex_lock(&app->sock_lock); |
ffe60014 | 417 | ret = ustctl_send_channel_to_ust(app->sock, ua_sess->handle, channel->obj); |
fb45065e | 418 | pthread_mutex_unlock(&app->sock_lock); |
ffe60014 DG |
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 | } | |
48842b30 DG |
428 | |
429 | error: | |
430 | return ret; | |
431 | } | |
331744e3 JD |
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(); | |
331744e3 JD |
450 | health_code_update(); |
451 | ||
452 | /* Wait for a metadata request */ | |
dc2bbdae | 453 | pthread_mutex_lock(socket->lock); |
52898cb1 | 454 | ret = consumer_socket_recv(socket, &request, sizeof(request)); |
dc2bbdae | 455 | pthread_mutex_unlock(socket->lock); |
52898cb1 | 456 | if (ret < 0) { |
331744e3 JD |
457 | goto end; |
458 | } | |
459 | ||
1950109e | 460 | DBG("Metadata request received for session %" PRIu64 ", key %" PRIu64, |
331744e3 JD |
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 = | |
1950109e | 469 | buffer_reg_pid_find(request.session_id_per_pid); |
331744e3 | 470 | if (!reg_pid) { |
1950109e JD |
471 | DBG("PID registry not found for session id %" PRIu64, |
472 | request.session_id_per_pid); | |
331744e3 | 473 | |
53efb85a | 474 | memset(&msg, 0, sizeof(msg)); |
331744e3 | 475 | msg.cmd_type = LTTNG_ERR_UND; |
cb7d882c | 476 | pthread_mutex_lock(socket->lock); |
331744e3 | 477 | (void) consumer_send_msg(socket, &msg); |
cb7d882c | 478 | pthread_mutex_unlock(socket->lock); |
331744e3 JD |
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 | ||
dc2bbdae | 492 | pthread_mutex_lock(&ust_reg->lock); |
331744e3 | 493 | ret_push = ust_app_push_metadata(ust_reg, socket, 1); |
dc2bbdae | 494 | pthread_mutex_unlock(&ust_reg->lock); |
2c57e06d MD |
495 | if (ret_push == -EPIPE) { |
496 | DBG("Application or relay closed while pushing metadata"); | |
497 | } else if (ret_push < 0) { | |
331744e3 JD |
498 | ERR("Pushing metadata"); |
499 | ret = -1; | |
500 | goto end; | |
2c57e06d MD |
501 | } else { |
502 | DBG("UST Consumer metadata pushed successfully"); | |
331744e3 | 503 | } |
331744e3 JD |
504 | ret = 0; |
505 | ||
506 | end: | |
331744e3 JD |
507 | rcu_read_unlock(); |
508 | return ret; | |
509 | } |