Commit | Line | Data |
---|---|---|
f1e16794 DG |
1 | /* |
2 | * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com> | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify it | |
5 | * under the terms of the GNU General Public License, version 2 only, as | |
6 | * published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, but WITHOUT | |
9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
11 | * more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License along with | |
14 | * this program; if not, write to the Free Software Foundation, Inc., 51 | |
15 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
16 | */ | |
17 | ||
6c1c0768 | 18 | #define _LGPL_SOURCE |
f1e16794 DG |
19 | #include <stdio.h> |
20 | #include <stdlib.h> | |
f1e16794 DG |
21 | #include <sys/stat.h> |
22 | #include <unistd.h> | |
e1f3997a | 23 | #include <inttypes.h> |
f1e16794 DG |
24 | |
25 | #include <common/common.h> | |
26 | #include <common/defaults.h> | |
f5436bfc | 27 | #include <common/compat/string.h> |
f1e16794 | 28 | |
00e2e675 | 29 | #include "consumer.h" |
8782cc74 | 30 | #include "health-sessiond.h" |
f1e16794 | 31 | #include "kernel-consumer.h" |
e9404c27 JG |
32 | #include "notification-thread-commands.h" |
33 | #include "session.h" | |
34 | #include "lttng-sessiond.h" | |
f1e16794 | 35 | |
2bba9e53 DG |
36 | static char *create_channel_path(struct consumer_output *consumer, |
37 | uid_t uid, gid_t gid) | |
00e2e675 DG |
38 | { |
39 | int ret; | |
ffe60014 | 40 | char tmp_path[PATH_MAX]; |
2bba9e53 | 41 | char *pathname = NULL; |
00e2e675 | 42 | |
2bba9e53 | 43 | assert(consumer); |
00e2e675 | 44 | |
ffe60014 DG |
45 | /* Get the right path name destination */ |
46 | if (consumer->type == CONSUMER_DST_LOCAL) { | |
47 | /* Set application path to the destination path */ | |
366a9222 JD |
48 | ret = snprintf(tmp_path, sizeof(tmp_path), "%s%s%s", |
49 | consumer->dst.session_root_path, | |
50 | consumer->chunk_path, | |
51 | consumer->subdir); | |
ffe60014 | 52 | if (ret < 0) { |
2bba9e53 | 53 | PERROR("snprintf kernel channel path"); |
ffe60014 | 54 | goto error; |
dba13f1d JG |
55 | } else if (ret >= sizeof(tmp_path)) { |
56 | ERR("Kernel channel path exceeds the maximal allowed length of of %zu bytes (%i bytes required) with path \"%s%s%s\"", | |
57 | sizeof(tmp_path), ret, | |
58 | consumer->dst.session_root_path, | |
59 | consumer->chunk_path, | |
60 | consumer->subdir); | |
61 | goto error; | |
ffe60014 | 62 | } |
f5436bfc | 63 | pathname = lttng_strndup(tmp_path, sizeof(tmp_path)); |
bb3c4e70 | 64 | if (!pathname) { |
f5436bfc | 65 | PERROR("lttng_strndup"); |
bb3c4e70 MD |
66 | goto error; |
67 | } | |
ffe60014 DG |
68 | |
69 | /* Create directory */ | |
2bba9e53 | 70 | ret = run_as_mkdir_recursive(pathname, S_IRWXU | S_IRWXG, uid, gid); |
ffe60014 | 71 | if (ret < 0) { |
df5b86c8 | 72 | if (errno != EEXIST) { |
ffe60014 DG |
73 | ERR("Trace directory creation error"); |
74 | goto error; | |
75 | } | |
76 | } | |
77 | DBG3("Kernel local consumer tracefile path: %s", pathname); | |
78 | } else { | |
2b29c638 JD |
79 | ret = snprintf(tmp_path, sizeof(tmp_path), "%s%s", |
80 | consumer->dst.net.base_dir, | |
81 | consumer->subdir); | |
ffe60014 | 82 | if (ret < 0) { |
2bba9e53 | 83 | PERROR("snprintf kernel metadata path"); |
ffe60014 | 84 | goto error; |
dba13f1d JG |
85 | } else if (ret >= sizeof(tmp_path)) { |
86 | ERR("Kernel channel path exceeds the maximal allowed length of of %zu bytes (%i bytes required) with path \"%s%s\"", | |
87 | sizeof(tmp_path), ret, | |
88 | consumer->dst.net.base_dir, | |
89 | consumer->subdir); | |
90 | goto error; | |
ffe60014 | 91 | } |
f5436bfc | 92 | pathname = lttng_strndup(tmp_path, sizeof(tmp_path)); |
bb3c4e70 | 93 | if (!pathname) { |
f5436bfc | 94 | PERROR("lttng_strndup"); |
bb3c4e70 MD |
95 | goto error; |
96 | } | |
ffe60014 DG |
97 | DBG3("Kernel network consumer subdir path: %s", pathname); |
98 | } | |
99 | ||
2bba9e53 DG |
100 | return pathname; |
101 | ||
102 | error: | |
103 | free(pathname); | |
104 | return NULL; | |
105 | } | |
106 | ||
107 | /* | |
108 | * Sending a single channel to the consumer with command ADD_CHANNEL. | |
109 | */ | |
110 | int kernel_consumer_add_channel(struct consumer_socket *sock, | |
e9404c27 JG |
111 | struct ltt_kernel_channel *channel, |
112 | struct ltt_kernel_session *ksession, | |
2bba9e53 DG |
113 | unsigned int monitor) |
114 | { | |
115 | int ret; | |
116 | char *pathname; | |
117 | struct lttcomm_consumer_msg lkm; | |
118 | struct consumer_output *consumer; | |
e9404c27 JG |
119 | enum lttng_error_code status; |
120 | struct ltt_session *session; | |
121 | struct lttng_channel_extended *channel_attr_extended; | |
2bba9e53 DG |
122 | |
123 | /* Safety net */ | |
124 | assert(channel); | |
e9404c27 JG |
125 | assert(ksession); |
126 | assert(ksession->consumer); | |
2bba9e53 | 127 | |
e9404c27 JG |
128 | consumer = ksession->consumer; |
129 | channel_attr_extended = (struct lttng_channel_extended *) | |
130 | channel->channel->attr.extended.ptr; | |
2bba9e53 DG |
131 | |
132 | DBG("Kernel consumer adding channel %s to kernel consumer", | |
133 | channel->channel->name); | |
134 | ||
135 | if (monitor) { | |
e9404c27 JG |
136 | pathname = create_channel_path(consumer, ksession->uid, |
137 | ksession->gid); | |
2bba9e53 DG |
138 | } else { |
139 | /* Empty path. */ | |
53efb85a | 140 | pathname = strdup(""); |
2bba9e53 | 141 | } |
bb3c4e70 MD |
142 | if (!pathname) { |
143 | ret = -1; | |
144 | goto error; | |
145 | } | |
2bba9e53 | 146 | |
00e2e675 DG |
147 | /* Prep channel message structure */ |
148 | consumer_init_channel_comm_msg(&lkm, | |
149 | LTTNG_CONSUMER_ADD_CHANNEL, | |
e1f3997a | 150 | channel->key, |
e9404c27 | 151 | ksession->id, |
ffe60014 | 152 | pathname, |
e9404c27 JG |
153 | ksession->uid, |
154 | ksession->gid, | |
ffe60014 | 155 | consumer->net_seq_index, |
c30aaa51 | 156 | channel->channel->name, |
ffe60014 DG |
157 | channel->stream_count, |
158 | channel->channel->attr.output, | |
1624d5b7 JD |
159 | CONSUMER_CHANNEL_TYPE_DATA, |
160 | channel->channel->attr.tracefile_size, | |
2bba9e53 | 161 | channel->channel->attr.tracefile_count, |
ecc48a90 | 162 | monitor, |
e9404c27 JG |
163 | channel->channel->attr.live_timer_interval, |
164 | channel_attr_extended->monitor_timer_interval); | |
00e2e675 | 165 | |
840cb59c | 166 | health_code_update(); |
ca03de58 | 167 | |
00e2e675 DG |
168 | ret = consumer_send_channel(sock, &lkm); |
169 | if (ret < 0) { | |
170 | goto error; | |
171 | } | |
172 | ||
840cb59c | 173 | health_code_update(); |
e9404c27 JG |
174 | rcu_read_lock(); |
175 | session = session_find_by_id(ksession->id); | |
176 | assert(session); | |
ca03de58 | 177 | |
e9404c27 JG |
178 | status = notification_thread_command_add_channel( |
179 | notification_thread_handle, session->name, | |
180 | ksession->uid, ksession->gid, | |
e1f3997a | 181 | channel->channel->name, channel->key, |
e9404c27 JG |
182 | LTTNG_DOMAIN_KERNEL, |
183 | channel->channel->attr.subbuf_size * channel->channel->attr.num_subbuf); | |
184 | rcu_read_unlock(); | |
185 | if (status != LTTNG_OK) { | |
186 | ret = -1; | |
187 | goto error; | |
188 | } | |
753873bf JR |
189 | |
190 | channel->published_to_notification_thread = true; | |
191 | ||
00e2e675 | 192 | error: |
53efb85a | 193 | free(pathname); |
00e2e675 DG |
194 | return ret; |
195 | } | |
196 | ||
197 | /* | |
198 | * Sending metadata to the consumer with command ADD_CHANNEL and ADD_STREAM. | |
9a318688 JG |
199 | * |
200 | * The consumer socket lock must be held by the caller. | |
00e2e675 | 201 | */ |
f50f23d9 | 202 | int kernel_consumer_add_metadata(struct consumer_socket *sock, |
2bba9e53 | 203 | struct ltt_kernel_session *session, unsigned int monitor) |
00e2e675 DG |
204 | { |
205 | int ret; | |
2bba9e53 | 206 | char *pathname; |
00e2e675 | 207 | struct lttcomm_consumer_msg lkm; |
a7d9a3e7 | 208 | struct consumer_output *consumer; |
00e2e675 DG |
209 | |
210 | /* Safety net */ | |
211 | assert(session); | |
212 | assert(session->consumer); | |
f50f23d9 | 213 | assert(sock); |
00e2e675 DG |
214 | |
215 | DBG("Sending metadata %d to kernel consumer", session->metadata_stream_fd); | |
216 | ||
217 | /* Get consumer output pointer */ | |
a7d9a3e7 | 218 | consumer = session->consumer; |
00e2e675 | 219 | |
2bba9e53 DG |
220 | if (monitor) { |
221 | pathname = create_channel_path(consumer, session->uid, session->gid); | |
00e2e675 | 222 | } else { |
2bba9e53 | 223 | /* Empty path. */ |
53efb85a | 224 | pathname = strdup(""); |
00e2e675 | 225 | } |
bb3c4e70 MD |
226 | if (!pathname) { |
227 | ret = -1; | |
228 | goto error; | |
229 | } | |
00e2e675 DG |
230 | |
231 | /* Prep channel message structure */ | |
232 | consumer_init_channel_comm_msg(&lkm, | |
233 | LTTNG_CONSUMER_ADD_CHANNEL, | |
d40f0359 | 234 | session->metadata->key, |
ffe60014 DG |
235 | session->id, |
236 | pathname, | |
237 | session->uid, | |
238 | session->gid, | |
239 | consumer->net_seq_index, | |
30079b6b | 240 | DEFAULT_METADATA_NAME, |
ffe60014 DG |
241 | 1, |
242 | DEFAULT_KERNEL_CHANNEL_OUTPUT, | |
1624d5b7 | 243 | CONSUMER_CHANNEL_TYPE_METADATA, |
2bba9e53 | 244 | 0, 0, |
e9404c27 | 245 | monitor, 0, 0); |
00e2e675 | 246 | |
840cb59c | 247 | health_code_update(); |
ca03de58 | 248 | |
00e2e675 DG |
249 | ret = consumer_send_channel(sock, &lkm); |
250 | if (ret < 0) { | |
251 | goto error; | |
252 | } | |
253 | ||
840cb59c | 254 | health_code_update(); |
ca03de58 | 255 | |
00e2e675 DG |
256 | /* Prep stream message structure */ |
257 | consumer_init_stream_comm_msg(&lkm, | |
258 | LTTNG_CONSUMER_ADD_STREAM, | |
d40f0359 | 259 | session->metadata->key, |
00e2e675 | 260 | session->metadata_stream_fd, |
1624d5b7 | 261 | 0); /* CPU: 0 for metadata. */ |
00e2e675 | 262 | |
840cb59c | 263 | health_code_update(); |
ca03de58 | 264 | |
00e2e675 | 265 | /* Send stream and file descriptor */ |
a7d9a3e7 | 266 | ret = consumer_send_stream(sock, consumer, &lkm, |
00e2e675 DG |
267 | &session->metadata_stream_fd, 1); |
268 | if (ret < 0) { | |
269 | goto error; | |
270 | } | |
271 | ||
840cb59c | 272 | health_code_update(); |
ca03de58 | 273 | |
00e2e675 | 274 | error: |
53efb85a | 275 | free(pathname); |
00e2e675 DG |
276 | return ret; |
277 | } | |
278 | ||
279 | /* | |
280 | * Sending a single stream to the consumer with command ADD_STREAM. | |
281 | */ | |
f50f23d9 DG |
282 | int kernel_consumer_add_stream(struct consumer_socket *sock, |
283 | struct ltt_kernel_channel *channel, struct ltt_kernel_stream *stream, | |
2bba9e53 | 284 | struct ltt_kernel_session *session, unsigned int monitor) |
00e2e675 DG |
285 | { |
286 | int ret; | |
00e2e675 | 287 | struct lttcomm_consumer_msg lkm; |
a7d9a3e7 | 288 | struct consumer_output *consumer; |
00e2e675 DG |
289 | |
290 | assert(channel); | |
291 | assert(stream); | |
292 | assert(session); | |
293 | assert(session->consumer); | |
f50f23d9 | 294 | assert(sock); |
00e2e675 DG |
295 | |
296 | DBG("Sending stream %d of channel %s to kernel consumer", | |
297 | stream->fd, channel->channel->name); | |
298 | ||
299 | /* Get consumer output pointer */ | |
a7d9a3e7 | 300 | consumer = session->consumer; |
00e2e675 | 301 | |
00e2e675 | 302 | /* Prep stream consumer message */ |
ffe60014 DG |
303 | consumer_init_stream_comm_msg(&lkm, |
304 | LTTNG_CONSUMER_ADD_STREAM, | |
e1f3997a | 305 | channel->key, |
00e2e675 | 306 | stream->fd, |
ffe60014 | 307 | stream->cpu); |
00e2e675 | 308 | |
840cb59c | 309 | health_code_update(); |
ca03de58 | 310 | |
00e2e675 | 311 | /* Send stream and file descriptor */ |
a7d9a3e7 | 312 | ret = consumer_send_stream(sock, consumer, &lkm, &stream->fd, 1); |
00e2e675 DG |
313 | if (ret < 0) { |
314 | goto error; | |
315 | } | |
316 | ||
840cb59c | 317 | health_code_update(); |
ca03de58 | 318 | |
00e2e675 DG |
319 | error: |
320 | return ret; | |
321 | } | |
322 | ||
a4baae1b JD |
323 | /* |
324 | * Sending the notification that all streams were sent with STREAMS_SENT. | |
325 | */ | |
326 | int kernel_consumer_streams_sent(struct consumer_socket *sock, | |
327 | struct ltt_kernel_session *session, uint64_t channel_key) | |
328 | { | |
329 | int ret; | |
330 | struct lttcomm_consumer_msg lkm; | |
331 | struct consumer_output *consumer; | |
332 | ||
333 | assert(sock); | |
334 | assert(session); | |
335 | ||
336 | DBG("Sending streams_sent"); | |
337 | /* Get consumer output pointer */ | |
338 | consumer = session->consumer; | |
339 | ||
340 | /* Prep stream consumer message */ | |
341 | consumer_init_streams_sent_comm_msg(&lkm, | |
342 | LTTNG_CONSUMER_STREAMS_SENT, | |
343 | channel_key, consumer->net_seq_index); | |
344 | ||
345 | health_code_update(); | |
346 | ||
347 | /* Send stream and file descriptor */ | |
348 | ret = consumer_send_msg(sock, &lkm); | |
349 | if (ret < 0) { | |
350 | goto error; | |
351 | } | |
352 | ||
353 | error: | |
354 | return ret; | |
355 | } | |
356 | ||
f1e16794 DG |
357 | /* |
358 | * Send all stream fds of kernel channel to the consumer. | |
9a318688 JG |
359 | * |
360 | * The consumer socket lock must be held by the caller. | |
f1e16794 | 361 | */ |
f50f23d9 | 362 | int kernel_consumer_send_channel_stream(struct consumer_socket *sock, |
2bba9e53 DG |
363 | struct ltt_kernel_channel *channel, struct ltt_kernel_session *session, |
364 | unsigned int monitor) | |
f1e16794 | 365 | { |
e99f9447 | 366 | int ret = LTTNG_OK; |
f1e16794 | 367 | struct ltt_kernel_stream *stream; |
00e2e675 DG |
368 | |
369 | /* Safety net */ | |
370 | assert(channel); | |
371 | assert(session); | |
372 | assert(session->consumer); | |
f50f23d9 | 373 | assert(sock); |
00e2e675 DG |
374 | |
375 | /* Bail out if consumer is disabled */ | |
376 | if (!session->consumer->enabled) { | |
f73fabfd | 377 | ret = LTTNG_OK; |
00e2e675 DG |
378 | goto error; |
379 | } | |
f1e16794 DG |
380 | |
381 | DBG("Sending streams of channel %s to kernel consumer", | |
382 | channel->channel->name); | |
383 | ||
e99f9447 MD |
384 | if (!channel->sent_to_consumer) { |
385 | ret = kernel_consumer_add_channel(sock, channel, session, monitor); | |
386 | if (ret < 0) { | |
387 | goto error; | |
388 | } | |
389 | channel->sent_to_consumer = true; | |
f1e16794 DG |
390 | } |
391 | ||
392 | /* Send streams */ | |
393 | cds_list_for_each_entry(stream, &channel->stream_list.head, list) { | |
6986ab9b | 394 | if (!stream->fd || stream->sent_to_consumer) { |
f1e16794 DG |
395 | continue; |
396 | } | |
00e2e675 DG |
397 | |
398 | /* Add stream on the kernel consumer side. */ | |
2bba9e53 DG |
399 | ret = kernel_consumer_add_stream(sock, channel, stream, session, |
400 | monitor); | |
f1e16794 | 401 | if (ret < 0) { |
f1e16794 DG |
402 | goto error; |
403 | } | |
6986ab9b | 404 | stream->sent_to_consumer = true; |
f1e16794 DG |
405 | } |
406 | ||
f1e16794 DG |
407 | error: |
408 | return ret; | |
409 | } | |
410 | ||
411 | /* | |
412 | * Send all stream fds of the kernel session to the consumer. | |
9a318688 JG |
413 | * |
414 | * The consumer socket lock must be held by the caller. | |
f1e16794 | 415 | */ |
f50f23d9 DG |
416 | int kernel_consumer_send_session(struct consumer_socket *sock, |
417 | struct ltt_kernel_session *session) | |
f1e16794 | 418 | { |
2bba9e53 | 419 | int ret, monitor = 0; |
f1e16794 | 420 | struct ltt_kernel_channel *chan; |
f1e16794 | 421 | |
00e2e675 DG |
422 | /* Safety net */ |
423 | assert(session); | |
424 | assert(session->consumer); | |
f50f23d9 | 425 | assert(sock); |
f1e16794 | 426 | |
00e2e675 DG |
427 | /* Bail out if consumer is disabled */ |
428 | if (!session->consumer->enabled) { | |
f73fabfd | 429 | ret = LTTNG_OK; |
00e2e675 | 430 | goto error; |
f1e16794 DG |
431 | } |
432 | ||
2bba9e53 DG |
433 | /* Don't monitor the streams on the consumer if in flight recorder. */ |
434 | if (session->output_traces) { | |
435 | monitor = 1; | |
436 | } | |
437 | ||
00e2e675 DG |
438 | DBG("Sending session stream to kernel consumer"); |
439 | ||
609af759 | 440 | if (session->metadata_stream_fd >= 0 && session->metadata) { |
2bba9e53 | 441 | ret = kernel_consumer_add_metadata(sock, session, monitor); |
f1e16794 | 442 | if (ret < 0) { |
f1e16794 DG |
443 | goto error; |
444 | } | |
f1e16794 DG |
445 | } |
446 | ||
00e2e675 | 447 | /* Send channel and streams of it */ |
f1e16794 | 448 | cds_list_for_each_entry(chan, &session->channel_list.head, list) { |
2bba9e53 DG |
449 | ret = kernel_consumer_send_channel_stream(sock, chan, session, |
450 | monitor); | |
f1e16794 DG |
451 | if (ret < 0) { |
452 | goto error; | |
453 | } | |
601262d6 JD |
454 | if (monitor) { |
455 | /* | |
456 | * Inform the relay that all the streams for the | |
457 | * channel were sent. | |
458 | */ | |
e1f3997a | 459 | ret = kernel_consumer_streams_sent(sock, session, chan->key); |
601262d6 JD |
460 | if (ret < 0) { |
461 | goto error; | |
462 | } | |
463 | } | |
f1e16794 DG |
464 | } |
465 | ||
00e2e675 | 466 | DBG("Kernel consumer FDs of metadata and channel streams sent"); |
f1e16794 | 467 | |
4ce9ff51 | 468 | session->consumer_fds_sent = 1; |
f1e16794 DG |
469 | return 0; |
470 | ||
471 | error: | |
472 | return ret; | |
473 | } | |
07b86b52 JD |
474 | |
475 | int kernel_consumer_destroy_channel(struct consumer_socket *socket, | |
476 | struct ltt_kernel_channel *channel) | |
477 | { | |
478 | int ret; | |
479 | struct lttcomm_consumer_msg msg; | |
480 | ||
481 | assert(channel); | |
482 | assert(socket); | |
07b86b52 | 483 | |
e1f3997a | 484 | DBG("Sending kernel consumer destroy channel key %" PRIu64, channel->key); |
07b86b52 | 485 | |
53efb85a | 486 | memset(&msg, 0, sizeof(msg)); |
07b86b52 | 487 | msg.cmd_type = LTTNG_CONSUMER_DESTROY_CHANNEL; |
e1f3997a | 488 | msg.u.destroy_channel.key = channel->key; |
07b86b52 JD |
489 | |
490 | pthread_mutex_lock(socket->lock); | |
491 | health_code_update(); | |
492 | ||
493 | ret = consumer_send_msg(socket, &msg); | |
494 | if (ret < 0) { | |
495 | goto error; | |
496 | } | |
497 | ||
498 | error: | |
499 | health_code_update(); | |
500 | pthread_mutex_unlock(socket->lock); | |
501 | return ret; | |
502 | } | |
503 | ||
504 | int kernel_consumer_destroy_metadata(struct consumer_socket *socket, | |
505 | struct ltt_kernel_metadata *metadata) | |
506 | { | |
507 | int ret; | |
508 | struct lttcomm_consumer_msg msg; | |
509 | ||
510 | assert(metadata); | |
511 | assert(socket); | |
07b86b52 | 512 | |
d40f0359 | 513 | DBG("Sending kernel consumer destroy channel key %" PRIu64, metadata->key); |
07b86b52 | 514 | |
53efb85a | 515 | memset(&msg, 0, sizeof(msg)); |
07b86b52 | 516 | msg.cmd_type = LTTNG_CONSUMER_DESTROY_CHANNEL; |
d40f0359 | 517 | msg.u.destroy_channel.key = metadata->key; |
07b86b52 JD |
518 | |
519 | pthread_mutex_lock(socket->lock); | |
520 | health_code_update(); | |
521 | ||
522 | ret = consumer_send_msg(socket, &msg); | |
523 | if (ret < 0) { | |
524 | goto error; | |
525 | } | |
526 | ||
527 | error: | |
528 | health_code_update(); | |
529 | pthread_mutex_unlock(socket->lock); | |
530 | return ret; | |
531 | } |