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 */ | |
dec56f6c | 48 | ret = snprintf(tmp_path, sizeof(tmp_path), "%s%s", |
ffe60014 DG |
49 | consumer->dst.trace_path, consumer->subdir); |
50 | if (ret < 0) { | |
2bba9e53 | 51 | PERROR("snprintf kernel channel path"); |
ffe60014 DG |
52 | goto error; |
53 | } | |
f5436bfc | 54 | pathname = lttng_strndup(tmp_path, sizeof(tmp_path)); |
bb3c4e70 | 55 | if (!pathname) { |
f5436bfc | 56 | PERROR("lttng_strndup"); |
bb3c4e70 MD |
57 | goto error; |
58 | } | |
ffe60014 DG |
59 | |
60 | /* Create directory */ | |
2bba9e53 | 61 | ret = run_as_mkdir_recursive(pathname, S_IRWXU | S_IRWXG, uid, gid); |
ffe60014 | 62 | if (ret < 0) { |
df5b86c8 | 63 | if (errno != EEXIST) { |
ffe60014 DG |
64 | ERR("Trace directory creation error"); |
65 | goto error; | |
66 | } | |
67 | } | |
68 | DBG3("Kernel local consumer tracefile path: %s", pathname); | |
69 | } else { | |
70 | ret = snprintf(tmp_path, sizeof(tmp_path), "%s", consumer->subdir); | |
71 | if (ret < 0) { | |
2bba9e53 | 72 | PERROR("snprintf kernel metadata path"); |
ffe60014 DG |
73 | goto error; |
74 | } | |
f5436bfc | 75 | pathname = lttng_strndup(tmp_path, sizeof(tmp_path)); |
bb3c4e70 | 76 | if (!pathname) { |
f5436bfc | 77 | PERROR("lttng_strndup"); |
bb3c4e70 MD |
78 | goto error; |
79 | } | |
ffe60014 DG |
80 | DBG3("Kernel network consumer subdir path: %s", pathname); |
81 | } | |
82 | ||
2bba9e53 DG |
83 | return pathname; |
84 | ||
85 | error: | |
86 | free(pathname); | |
87 | return NULL; | |
88 | } | |
89 | ||
90 | /* | |
91 | * Sending a single channel to the consumer with command ADD_CHANNEL. | |
92 | */ | |
93 | int kernel_consumer_add_channel(struct consumer_socket *sock, | |
e9404c27 JG |
94 | struct ltt_kernel_channel *channel, |
95 | struct ltt_kernel_session *ksession, | |
2bba9e53 DG |
96 | unsigned int monitor) |
97 | { | |
98 | int ret; | |
99 | char *pathname; | |
100 | struct lttcomm_consumer_msg lkm; | |
101 | struct consumer_output *consumer; | |
e9404c27 JG |
102 | enum lttng_error_code status; |
103 | struct ltt_session *session; | |
104 | struct lttng_channel_extended *channel_attr_extended; | |
2bba9e53 DG |
105 | |
106 | /* Safety net */ | |
107 | assert(channel); | |
e9404c27 JG |
108 | assert(ksession); |
109 | assert(ksession->consumer); | |
2bba9e53 | 110 | |
e9404c27 JG |
111 | consumer = ksession->consumer; |
112 | channel_attr_extended = (struct lttng_channel_extended *) | |
113 | channel->channel->attr.extended.ptr; | |
2bba9e53 DG |
114 | |
115 | DBG("Kernel consumer adding channel %s to kernel consumer", | |
116 | channel->channel->name); | |
117 | ||
118 | if (monitor) { | |
e9404c27 JG |
119 | pathname = create_channel_path(consumer, ksession->uid, |
120 | ksession->gid); | |
2bba9e53 DG |
121 | } else { |
122 | /* Empty path. */ | |
53efb85a | 123 | pathname = strdup(""); |
2bba9e53 | 124 | } |
bb3c4e70 MD |
125 | if (!pathname) { |
126 | ret = -1; | |
127 | goto error; | |
128 | } | |
2bba9e53 | 129 | |
00e2e675 DG |
130 | /* Prep channel message structure */ |
131 | consumer_init_channel_comm_msg(&lkm, | |
132 | LTTNG_CONSUMER_ADD_CHANNEL, | |
e1f3997a | 133 | channel->key, |
e9404c27 | 134 | ksession->id, |
ffe60014 | 135 | pathname, |
e9404c27 JG |
136 | ksession->uid, |
137 | ksession->gid, | |
ffe60014 | 138 | consumer->net_seq_index, |
c30aaa51 | 139 | channel->channel->name, |
ffe60014 DG |
140 | channel->stream_count, |
141 | channel->channel->attr.output, | |
1624d5b7 JD |
142 | CONSUMER_CHANNEL_TYPE_DATA, |
143 | channel->channel->attr.tracefile_size, | |
2bba9e53 | 144 | channel->channel->attr.tracefile_count, |
ecc48a90 | 145 | monitor, |
e9404c27 JG |
146 | channel->channel->attr.live_timer_interval, |
147 | channel_attr_extended->monitor_timer_interval); | |
00e2e675 | 148 | |
840cb59c | 149 | health_code_update(); |
ca03de58 | 150 | |
00e2e675 DG |
151 | ret = consumer_send_channel(sock, &lkm); |
152 | if (ret < 0) { | |
153 | goto error; | |
154 | } | |
155 | ||
840cb59c | 156 | health_code_update(); |
e9404c27 JG |
157 | rcu_read_lock(); |
158 | session = session_find_by_id(ksession->id); | |
159 | assert(session); | |
ca03de58 | 160 | |
e9404c27 JG |
161 | status = notification_thread_command_add_channel( |
162 | notification_thread_handle, session->name, | |
163 | ksession->uid, ksession->gid, | |
e1f3997a | 164 | channel->channel->name, channel->key, |
e9404c27 JG |
165 | LTTNG_DOMAIN_KERNEL, |
166 | channel->channel->attr.subbuf_size * channel->channel->attr.num_subbuf); | |
167 | rcu_read_unlock(); | |
168 | if (status != LTTNG_OK) { | |
169 | ret = -1; | |
170 | goto error; | |
171 | } | |
753873bf JR |
172 | |
173 | channel->published_to_notification_thread = true; | |
174 | ||
00e2e675 | 175 | error: |
53efb85a | 176 | free(pathname); |
00e2e675 DG |
177 | return ret; |
178 | } | |
179 | ||
180 | /* | |
181 | * Sending metadata to the consumer with command ADD_CHANNEL and ADD_STREAM. | |
9a318688 JG |
182 | * |
183 | * The consumer socket lock must be held by the caller. | |
00e2e675 | 184 | */ |
f50f23d9 | 185 | int kernel_consumer_add_metadata(struct consumer_socket *sock, |
2bba9e53 | 186 | struct ltt_kernel_session *session, unsigned int monitor) |
00e2e675 DG |
187 | { |
188 | int ret; | |
2bba9e53 | 189 | char *pathname; |
00e2e675 | 190 | struct lttcomm_consumer_msg lkm; |
a7d9a3e7 | 191 | struct consumer_output *consumer; |
00e2e675 DG |
192 | |
193 | /* Safety net */ | |
194 | assert(session); | |
195 | assert(session->consumer); | |
f50f23d9 | 196 | assert(sock); |
00e2e675 DG |
197 | |
198 | DBG("Sending metadata %d to kernel consumer", session->metadata_stream_fd); | |
199 | ||
200 | /* Get consumer output pointer */ | |
a7d9a3e7 | 201 | consumer = session->consumer; |
00e2e675 | 202 | |
2bba9e53 DG |
203 | if (monitor) { |
204 | pathname = create_channel_path(consumer, session->uid, session->gid); | |
00e2e675 | 205 | } else { |
2bba9e53 | 206 | /* Empty path. */ |
53efb85a | 207 | pathname = strdup(""); |
00e2e675 | 208 | } |
bb3c4e70 MD |
209 | if (!pathname) { |
210 | ret = -1; | |
211 | goto error; | |
212 | } | |
00e2e675 DG |
213 | |
214 | /* Prep channel message structure */ | |
215 | consumer_init_channel_comm_msg(&lkm, | |
216 | LTTNG_CONSUMER_ADD_CHANNEL, | |
217 | session->metadata->fd, | |
ffe60014 DG |
218 | session->id, |
219 | pathname, | |
220 | session->uid, | |
221 | session->gid, | |
222 | consumer->net_seq_index, | |
30079b6b | 223 | DEFAULT_METADATA_NAME, |
ffe60014 DG |
224 | 1, |
225 | DEFAULT_KERNEL_CHANNEL_OUTPUT, | |
1624d5b7 | 226 | CONSUMER_CHANNEL_TYPE_METADATA, |
2bba9e53 | 227 | 0, 0, |
e9404c27 | 228 | monitor, 0, 0); |
00e2e675 | 229 | |
840cb59c | 230 | health_code_update(); |
ca03de58 | 231 | |
00e2e675 DG |
232 | ret = consumer_send_channel(sock, &lkm); |
233 | if (ret < 0) { | |
234 | goto error; | |
235 | } | |
236 | ||
840cb59c | 237 | health_code_update(); |
ca03de58 | 238 | |
00e2e675 DG |
239 | /* Prep stream message structure */ |
240 | consumer_init_stream_comm_msg(&lkm, | |
241 | LTTNG_CONSUMER_ADD_STREAM, | |
242 | session->metadata->fd, | |
243 | session->metadata_stream_fd, | |
1624d5b7 | 244 | 0); /* CPU: 0 for metadata. */ |
00e2e675 | 245 | |
840cb59c | 246 | health_code_update(); |
ca03de58 | 247 | |
00e2e675 | 248 | /* Send stream and file descriptor */ |
a7d9a3e7 | 249 | ret = consumer_send_stream(sock, consumer, &lkm, |
00e2e675 DG |
250 | &session->metadata_stream_fd, 1); |
251 | if (ret < 0) { | |
252 | goto error; | |
253 | } | |
254 | ||
840cb59c | 255 | health_code_update(); |
ca03de58 | 256 | |
00e2e675 | 257 | error: |
53efb85a | 258 | free(pathname); |
00e2e675 DG |
259 | return ret; |
260 | } | |
261 | ||
262 | /* | |
263 | * Sending a single stream to the consumer with command ADD_STREAM. | |
264 | */ | |
f50f23d9 DG |
265 | int kernel_consumer_add_stream(struct consumer_socket *sock, |
266 | struct ltt_kernel_channel *channel, struct ltt_kernel_stream *stream, | |
2bba9e53 | 267 | struct ltt_kernel_session *session, unsigned int monitor) |
00e2e675 DG |
268 | { |
269 | int ret; | |
00e2e675 | 270 | struct lttcomm_consumer_msg lkm; |
a7d9a3e7 | 271 | struct consumer_output *consumer; |
00e2e675 DG |
272 | |
273 | assert(channel); | |
274 | assert(stream); | |
275 | assert(session); | |
276 | assert(session->consumer); | |
f50f23d9 | 277 | assert(sock); |
00e2e675 DG |
278 | |
279 | DBG("Sending stream %d of channel %s to kernel consumer", | |
280 | stream->fd, channel->channel->name); | |
281 | ||
282 | /* Get consumer output pointer */ | |
a7d9a3e7 | 283 | consumer = session->consumer; |
00e2e675 | 284 | |
00e2e675 | 285 | /* Prep stream consumer message */ |
ffe60014 DG |
286 | consumer_init_stream_comm_msg(&lkm, |
287 | LTTNG_CONSUMER_ADD_STREAM, | |
e1f3997a | 288 | channel->key, |
00e2e675 | 289 | stream->fd, |
ffe60014 | 290 | stream->cpu); |
00e2e675 | 291 | |
840cb59c | 292 | health_code_update(); |
ca03de58 | 293 | |
00e2e675 | 294 | /* Send stream and file descriptor */ |
a7d9a3e7 | 295 | ret = consumer_send_stream(sock, consumer, &lkm, &stream->fd, 1); |
00e2e675 DG |
296 | if (ret < 0) { |
297 | goto error; | |
298 | } | |
299 | ||
840cb59c | 300 | health_code_update(); |
ca03de58 | 301 | |
00e2e675 DG |
302 | error: |
303 | return ret; | |
304 | } | |
305 | ||
a4baae1b JD |
306 | /* |
307 | * Sending the notification that all streams were sent with STREAMS_SENT. | |
308 | */ | |
309 | int kernel_consumer_streams_sent(struct consumer_socket *sock, | |
310 | struct ltt_kernel_session *session, uint64_t channel_key) | |
311 | { | |
312 | int ret; | |
313 | struct lttcomm_consumer_msg lkm; | |
314 | struct consumer_output *consumer; | |
315 | ||
316 | assert(sock); | |
317 | assert(session); | |
318 | ||
319 | DBG("Sending streams_sent"); | |
320 | /* Get consumer output pointer */ | |
321 | consumer = session->consumer; | |
322 | ||
323 | /* Prep stream consumer message */ | |
324 | consumer_init_streams_sent_comm_msg(&lkm, | |
325 | LTTNG_CONSUMER_STREAMS_SENT, | |
326 | channel_key, consumer->net_seq_index); | |
327 | ||
328 | health_code_update(); | |
329 | ||
330 | /* Send stream and file descriptor */ | |
331 | ret = consumer_send_msg(sock, &lkm); | |
332 | if (ret < 0) { | |
333 | goto error; | |
334 | } | |
335 | ||
336 | error: | |
337 | return ret; | |
338 | } | |
339 | ||
f1e16794 DG |
340 | /* |
341 | * Send all stream fds of kernel channel to the consumer. | |
9a318688 JG |
342 | * |
343 | * The consumer socket lock must be held by the caller. | |
f1e16794 | 344 | */ |
f50f23d9 | 345 | int kernel_consumer_send_channel_stream(struct consumer_socket *sock, |
2bba9e53 DG |
346 | struct ltt_kernel_channel *channel, struct ltt_kernel_session *session, |
347 | unsigned int monitor) | |
f1e16794 | 348 | { |
e99f9447 | 349 | int ret = LTTNG_OK; |
f1e16794 | 350 | struct ltt_kernel_stream *stream; |
00e2e675 DG |
351 | |
352 | /* Safety net */ | |
353 | assert(channel); | |
354 | assert(session); | |
355 | assert(session->consumer); | |
f50f23d9 | 356 | assert(sock); |
00e2e675 DG |
357 | |
358 | /* Bail out if consumer is disabled */ | |
359 | if (!session->consumer->enabled) { | |
f73fabfd | 360 | ret = LTTNG_OK; |
00e2e675 DG |
361 | goto error; |
362 | } | |
f1e16794 DG |
363 | |
364 | DBG("Sending streams of channel %s to kernel consumer", | |
365 | channel->channel->name); | |
366 | ||
e99f9447 MD |
367 | if (!channel->sent_to_consumer) { |
368 | ret = kernel_consumer_add_channel(sock, channel, session, monitor); | |
369 | if (ret < 0) { | |
370 | goto error; | |
371 | } | |
372 | channel->sent_to_consumer = true; | |
f1e16794 DG |
373 | } |
374 | ||
375 | /* Send streams */ | |
376 | cds_list_for_each_entry(stream, &channel->stream_list.head, list) { | |
6986ab9b | 377 | if (!stream->fd || stream->sent_to_consumer) { |
f1e16794 DG |
378 | continue; |
379 | } | |
00e2e675 DG |
380 | |
381 | /* Add stream on the kernel consumer side. */ | |
2bba9e53 DG |
382 | ret = kernel_consumer_add_stream(sock, channel, stream, session, |
383 | monitor); | |
f1e16794 | 384 | if (ret < 0) { |
f1e16794 DG |
385 | goto error; |
386 | } | |
6986ab9b | 387 | stream->sent_to_consumer = true; |
f1e16794 DG |
388 | } |
389 | ||
f1e16794 DG |
390 | error: |
391 | return ret; | |
392 | } | |
393 | ||
394 | /* | |
395 | * Send all stream fds of the kernel session to the consumer. | |
9a318688 JG |
396 | * |
397 | * The consumer socket lock must be held by the caller. | |
f1e16794 | 398 | */ |
f50f23d9 DG |
399 | int kernel_consumer_send_session(struct consumer_socket *sock, |
400 | struct ltt_kernel_session *session) | |
f1e16794 | 401 | { |
2bba9e53 | 402 | int ret, monitor = 0; |
f1e16794 | 403 | struct ltt_kernel_channel *chan; |
f1e16794 | 404 | |
00e2e675 DG |
405 | /* Safety net */ |
406 | assert(session); | |
407 | assert(session->consumer); | |
f50f23d9 | 408 | assert(sock); |
f1e16794 | 409 | |
00e2e675 DG |
410 | /* Bail out if consumer is disabled */ |
411 | if (!session->consumer->enabled) { | |
f73fabfd | 412 | ret = LTTNG_OK; |
00e2e675 | 413 | goto error; |
f1e16794 DG |
414 | } |
415 | ||
2bba9e53 DG |
416 | /* Don't monitor the streams on the consumer if in flight recorder. */ |
417 | if (session->output_traces) { | |
418 | monitor = 1; | |
419 | } | |
420 | ||
00e2e675 DG |
421 | DBG("Sending session stream to kernel consumer"); |
422 | ||
609af759 | 423 | if (session->metadata_stream_fd >= 0 && session->metadata) { |
2bba9e53 | 424 | ret = kernel_consumer_add_metadata(sock, session, monitor); |
f1e16794 | 425 | if (ret < 0) { |
f1e16794 DG |
426 | goto error; |
427 | } | |
f1e16794 DG |
428 | } |
429 | ||
00e2e675 | 430 | /* Send channel and streams of it */ |
f1e16794 | 431 | cds_list_for_each_entry(chan, &session->channel_list.head, list) { |
2bba9e53 DG |
432 | ret = kernel_consumer_send_channel_stream(sock, chan, session, |
433 | monitor); | |
f1e16794 DG |
434 | if (ret < 0) { |
435 | goto error; | |
436 | } | |
601262d6 JD |
437 | if (monitor) { |
438 | /* | |
439 | * Inform the relay that all the streams for the | |
440 | * channel were sent. | |
441 | */ | |
e1f3997a | 442 | ret = kernel_consumer_streams_sent(sock, session, chan->key); |
601262d6 JD |
443 | if (ret < 0) { |
444 | goto error; | |
445 | } | |
446 | } | |
f1e16794 DG |
447 | } |
448 | ||
00e2e675 | 449 | DBG("Kernel consumer FDs of metadata and channel streams sent"); |
f1e16794 | 450 | |
4ce9ff51 | 451 | session->consumer_fds_sent = 1; |
f1e16794 DG |
452 | return 0; |
453 | ||
454 | error: | |
455 | return ret; | |
456 | } | |
07b86b52 JD |
457 | |
458 | int kernel_consumer_destroy_channel(struct consumer_socket *socket, | |
459 | struct ltt_kernel_channel *channel) | |
460 | { | |
461 | int ret; | |
462 | struct lttcomm_consumer_msg msg; | |
463 | ||
464 | assert(channel); | |
465 | assert(socket); | |
07b86b52 | 466 | |
e1f3997a | 467 | DBG("Sending kernel consumer destroy channel key %" PRIu64, channel->key); |
07b86b52 | 468 | |
53efb85a | 469 | memset(&msg, 0, sizeof(msg)); |
07b86b52 | 470 | msg.cmd_type = LTTNG_CONSUMER_DESTROY_CHANNEL; |
e1f3997a | 471 | msg.u.destroy_channel.key = channel->key; |
07b86b52 JD |
472 | |
473 | pthread_mutex_lock(socket->lock); | |
474 | health_code_update(); | |
475 | ||
476 | ret = consumer_send_msg(socket, &msg); | |
477 | if (ret < 0) { | |
478 | goto error; | |
479 | } | |
480 | ||
481 | error: | |
482 | health_code_update(); | |
483 | pthread_mutex_unlock(socket->lock); | |
484 | return ret; | |
485 | } | |
486 | ||
487 | int kernel_consumer_destroy_metadata(struct consumer_socket *socket, | |
488 | struct ltt_kernel_metadata *metadata) | |
489 | { | |
490 | int ret; | |
491 | struct lttcomm_consumer_msg msg; | |
492 | ||
493 | assert(metadata); | |
494 | assert(socket); | |
07b86b52 JD |
495 | |
496 | DBG("Sending kernel consumer destroy channel key %d", metadata->fd); | |
497 | ||
53efb85a | 498 | memset(&msg, 0, sizeof(msg)); |
07b86b52 JD |
499 | msg.cmd_type = LTTNG_CONSUMER_DESTROY_CHANNEL; |
500 | msg.u.destroy_channel.key = metadata->fd; | |
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 | } |