Create kernel-consumer.c/.h and consumer.h
[lttng-tools.git] / src / bin / lttng-sessiond / kernel-consumer.c
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
18 #define _GNU_SOURCE
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <sys/stat.h>
23 #include <unistd.h>
24
25 #include <common/common.h>
26 #include <common/defaults.h>
27 #include <common/sessiond-comm/sessiond-comm.h>
28
29 #include "kernel-consumer.h"
30
31 /*
32 * Send all stream fds of kernel channel to the consumer.
33 */
34 int kernel_consumer_send_channel_stream(struct consumer_data *consumer_data,
35 int sock, struct ltt_kernel_channel *channel, uid_t uid, gid_t gid)
36 {
37 int ret, count = 0;
38 struct ltt_kernel_stream *stream;
39 struct lttcomm_consumer_msg lkm;
40
41 DBG("Sending streams of channel %s to kernel consumer",
42 channel->channel->name);
43
44 /* Send channel */
45 lkm.cmd_type = LTTNG_CONSUMER_ADD_CHANNEL;
46 lkm.u.channel.channel_key = channel->fd;
47 lkm.u.channel.max_sb_size = channel->channel->attr.subbuf_size;
48 lkm.u.channel.mmap_len = 0; /* for kernel */
49 DBG("Sending channel %d to consumer", lkm.u.channel.channel_key);
50 ret = lttcomm_send_unix_sock(sock, &lkm, sizeof(lkm));
51 if (ret < 0) {
52 PERROR("send consumer channel");
53 goto error;
54 }
55
56 /* Send streams */
57 cds_list_for_each_entry(stream, &channel->stream_list.head, list) {
58 if (!stream->fd) {
59 continue;
60 }
61 /* Reset consumer message structure */
62 memset(&lkm, 0, sizeof(lkm));
63 lkm.cmd_type = LTTNG_CONSUMER_ADD_STREAM;
64 lkm.u.stream.channel_key = channel->fd;
65 lkm.u.stream.stream_key = stream->fd;
66 lkm.u.stream.state = stream->state;
67 lkm.u.stream.output = channel->channel->attr.output;
68 lkm.u.stream.mmap_len = 0; /* for kernel */
69 lkm.u.stream.uid = uid;
70 lkm.u.stream.gid = gid;
71 strncpy(lkm.u.stream.path_name, stream->pathname, PATH_MAX - 1);
72 lkm.u.stream.path_name[PATH_MAX - 1] = '\0';
73 count++;
74
75 DBG("Sending stream %d to consumer", lkm.u.stream.stream_key);
76 ret = lttcomm_send_unix_sock(sock, &lkm, sizeof(lkm));
77 if (ret < 0) {
78 PERROR("send consumer stream");
79 goto error;
80 }
81 ret = lttcomm_send_fds_unix_sock(sock, &stream->fd, 1);
82 if (ret < 0) {
83 PERROR("send consumer stream ancillary data");
84 goto error;
85 }
86 }
87
88 DBG("consumer channel streams sent");
89
90 return 0;
91
92 error:
93 return ret;
94 }
95
96 /*
97 * Send all stream fds of the kernel session to the consumer.
98 */
99 int kernel_consumer_send_session(struct consumer_data *consumer_data,
100 struct ltt_kernel_session *session)
101 {
102 int ret;
103 struct ltt_kernel_channel *chan;
104 struct lttcomm_consumer_msg lkm;
105 int sock = session->consumer_fd;
106
107 DBG("Sending metadata stream fd");
108
109 /* Extra protection. It's NOT supposed to be set to -1 at this point */
110 if (session->consumer_fd < 0) {
111 session->consumer_fd = consumer_data->cmd_sock;
112 }
113
114 if (session->metadata_stream_fd >= 0) {
115 /* Send metadata channel fd */
116 lkm.cmd_type = LTTNG_CONSUMER_ADD_CHANNEL;
117 lkm.u.channel.channel_key = session->metadata->fd;
118 lkm.u.channel.max_sb_size = session->metadata->conf->attr.subbuf_size;
119 lkm.u.channel.mmap_len = 0; /* for kernel */
120 DBG("Sending metadata channel %d to consumer", lkm.u.channel.channel_key);
121 ret = lttcomm_send_unix_sock(sock, &lkm, sizeof(lkm));
122 if (ret < 0) {
123 PERROR("send consumer channel");
124 goto error;
125 }
126
127 /* Send metadata stream fd */
128 lkm.cmd_type = LTTNG_CONSUMER_ADD_STREAM;
129 lkm.u.stream.channel_key = session->metadata->fd;
130 lkm.u.stream.stream_key = session->metadata_stream_fd;
131 lkm.u.stream.state = LTTNG_CONSUMER_ACTIVE_STREAM;
132 lkm.u.stream.output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
133 lkm.u.stream.mmap_len = 0; /* for kernel */
134 lkm.u.stream.uid = session->uid;
135 lkm.u.stream.gid = session->gid;
136 strncpy(lkm.u.stream.path_name, session->metadata->pathname,
137 PATH_MAX - 1);
138 lkm.u.stream.path_name[PATH_MAX - 1] = '\0';
139
140 DBG("Sending metadata stream %d to consumer", lkm.u.stream.stream_key);
141 ret = lttcomm_send_unix_sock(sock, &lkm, sizeof(lkm));
142 if (ret < 0) {
143 PERROR("send consumer stream");
144 goto error;
145 }
146 ret = lttcomm_send_fds_unix_sock(sock, &session->metadata_stream_fd, 1);
147 if (ret < 0) {
148 PERROR("send consumer stream");
149 goto error;
150 }
151 }
152
153 cds_list_for_each_entry(chan, &session->channel_list.head, list) {
154 ret = kernel_consumer_send_channel_stream(consumer_data, sock, chan,
155 session->uid, session->gid);
156 if (ret < 0) {
157 goto error;
158 }
159 }
160
161 DBG("consumer fds (metadata and channel streams) sent");
162
163 return 0;
164
165 error:
166 return ret;
167 }
168
This page took 0.044154 seconds and 5 git commands to generate.