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